2013-09-25 21:42:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
#include "Benchmark.h"
|
2014-06-18 21:32:48 +00:00
|
|
|
#include "Resources.h"
|
2013-09-25 21:42:12 +00:00
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkForceLinking.h"
|
|
|
|
#include "SkImageDecoder.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
|
|
|
|
__SK_FORCE_IMAGE_DECODER_LINKING;
|
|
|
|
|
|
|
|
class SkCanvas;
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
class SkipZeroesBench : public Benchmark {
|
2013-09-25 21:42:12 +00:00
|
|
|
public:
|
|
|
|
SkipZeroesBench(const char* filename, bool skipZeroes)
|
|
|
|
: fName("SkipZeroes_")
|
2015-08-27 14:41:13 +00:00
|
|
|
, fDecoder(nullptr)
|
2013-09-25 21:42:12 +00:00
|
|
|
, fFilename(filename)
|
|
|
|
, fStream()
|
|
|
|
, fSkipZeroes(skipZeroes)
|
|
|
|
, fValid(false) {
|
|
|
|
fName.append(filename);
|
|
|
|
if (skipZeroes) {
|
|
|
|
fName.append("_skip_zeroes");
|
|
|
|
} else {
|
|
|
|
fName.append("_write_zeroes");
|
|
|
|
}
|
2013-11-21 06:21:58 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isSuitableFor(Backend backend) override {
|
2013-11-21 06:21:58 +00:00
|
|
|
return backend == kNonRendering_Backend;
|
2013-09-25 21:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* onGetName() override {
|
2013-09-25 21:42:12 +00:00
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
2015-09-30 19:11:07 +00:00
|
|
|
void onDelayedSetup() override {
|
2014-06-09 19:05:34 +00:00
|
|
|
SkString resourcePath = GetResourcePath();
|
|
|
|
if (resourcePath.isEmpty()) {
|
2013-09-25 21:42:12 +00:00
|
|
|
fValid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString fullPath = SkOSPath::Join(resourcePath.c_str(), fFilename.c_str());
|
2013-09-25 21:42:12 +00:00
|
|
|
SkFILEStream fileStream(fullPath.c_str());
|
|
|
|
fValid = fileStream.isValid() && fileStream.getLength() > 0;
|
|
|
|
if (fValid) {
|
|
|
|
const size_t size = fileStream.getLength();
|
|
|
|
void* data = sk_malloc_throw(size);
|
|
|
|
if (fileStream.read(data, size) < size) {
|
|
|
|
fValid = false;
|
|
|
|
} else {
|
|
|
|
SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
|
|
|
|
fStream.setData(skdata.get());
|
|
|
|
fDecoder.reset(SkImageDecoder::Factory(&fStream));
|
|
|
|
if (fDecoder.get()) {
|
Add an option on SkImageDecoder to skip writing 0s.
Only implemented for PNG.
Add a getter and setter, and sets the default to false in the
constructor. Also copies the setting in copyFieldsToOther.
Fix an indpendent bug where fDitherImage was not being copied in
copyFieldsToOther.
In SkScaledBitmapSampler::begin, consolidate the settings passed in
by passing a const reference to the decoder. The decoder can be
referenced for its settings of dither, unpremultiplied, and now
skipping writing zeroes. Update callers to use the new API. In png
decoder, rather than passing around a pointer to an initial
read of getDitherImage, and potentially changing it, look at the
field on the decoder itself, and modify it directly. This is a
change in behavior - now if that same decoder is used to decode
a different image, the dither setting has changed. I think this is
okay because A) the typical use case is to use a new decoder for
each decode, B) we do not make any promises that a decode does not
change the decoder and C) it makes the code in SkScaledBitmapSampler
much cleaner.
In SkScaledBitmapScampler, add new row procs for skipping zeroes. Now
that choosing the row proc has five dimensions (src config, dst config,
dither, skip writing zeroes, unpremultiplied), use a new method: each
src/dst combination has a function for choosing the right proc depending
on the decoder.
SkScaledBitmapScampler::RowProc is now public for convenience.
Remove Sample_Gray_D8888_Unpremul, which is effectively no different
from Sample_Gray_D8888.
In cases where unpremultiplied was trivial, such as 565 and when
sampling from gray, decoding may now succeed.
Add a benchmark (currently disabled) for comparing the speed of skipping
writing zeroes versus not skipping. For this particular image, which is
mostly transparent pixels, normal decoding took about 3.6 milliseconds,
while skipping zeroes in the decode took only about 2.5 milliseconds
(this is on a Nexus 4). Presumably it would be slower on an image
with a small amount of transparency, but there will be no slowdown
for an image which reports that it has no transparency.
In SkImageRef_ashmem, always skip writing zeroes, since ashmem
memory is guaranteed to be initialized to 0.
Add a flag to skip writing zeroes in skimage.
Add a regression test for choosing the rowproc to ensure I did not
change any behavior accidentally.
BUG=skia:1661
R=reed@google.com
Review URL: https://codereview.chromium.org/24269006
git-svn-id: http://skia.googlecode.com/svn/trunk@11558 2bbb7eff-a529-9590-31e7-b0007b416f81
2013-10-01 17:27:15 +00:00
|
|
|
fDecoder->setSkipWritingZeroes(fSkipZeroes);
|
2013-09-25 21:42:12 +00:00
|
|
|
} else {
|
|
|
|
fValid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2013-09-25 21:42:12 +00:00
|
|
|
if (!fValid) {
|
|
|
|
#ifdef SK_DEBUG
|
|
|
|
SkDebugf("stream was invalid: %s\n", fFilename.c_str());
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Decode a bunch of times
|
|
|
|
SkBitmap bm;
|
2013-12-03 18:17:16 +00:00
|
|
|
for (int i = 0; i < loops; ++i) {
|
2014-10-22 19:07:00 +00:00
|
|
|
SkDEBUGCODE(SkImageDecoder::Result result =) fDecoder->decode(&fStream, &bm,
|
|
|
|
SkImageDecoder::kDecodePixels_Mode);
|
2013-09-25 21:42:12 +00:00
|
|
|
#ifdef SK_DEBUG
|
2014-10-22 19:07:00 +00:00
|
|
|
if (SkImageDecoder::kFailure == result) {
|
2013-09-25 21:42:12 +00:00
|
|
|
SkDebugf("failed to decode %s\n", fFilename.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-22 19:07:00 +00:00
|
|
|
SkDEBUGCODE(bool success =) fStream.rewind();
|
2013-09-25 21:42:12 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
if (!success) {
|
|
|
|
SkDebugf("failed to rewind %s\n", fFilename.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkString fName;
|
|
|
|
SkAutoTDelete<SkImageDecoder> fDecoder;
|
|
|
|
const SkString fFilename;
|
|
|
|
SkMemoryStream fStream;
|
|
|
|
bool fSkipZeroes;
|
|
|
|
bool fValid;
|
|
|
|
|
2014-06-19 19:32:29 +00:00
|
|
|
typedef Benchmark INHERITED;
|
2013-09-25 21:42:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Enable the true version once the feature is checked in.
|
2015-08-26 20:07:48 +00:00
|
|
|
DEF_BENCH(return new SkipZeroesBench("arrow.png", true));
|
|
|
|
DEF_BENCH(return new SkipZeroesBench("arrow.png", false));
|