2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
12 #include "SkForceLinking.h"
13 #include "SkImageDecoder.h"
18 __SK_FORCE_IMAGE_DECODER_LINKING;
22 class SkipZeroesBench : public Benchmark {
24 SkipZeroesBench(const char* filename, bool skipZeroes)
25 : fName("SkipZeroes_")
29 , fSkipZeroes(skipZeroes)
31 fName.append(filename);
33 fName.append("_skip_zeroes");
35 fName.append("_write_zeroes");
39 bool isSuitableFor(Backend backend) SK_OVERRIDE {
40 return backend == kNonRendering_Backend;
44 const char* onGetName() SK_OVERRIDE {
48 void onPreDraw() SK_OVERRIDE {
49 SkString resourcePath = GetResourcePath();
50 if (resourcePath.isEmpty()) {
55 SkString fullPath = SkOSPath::Join(resourcePath.c_str(), fFilename.c_str());
56 SkFILEStream fileStream(fullPath.c_str());
57 fValid = fileStream.isValid() && fileStream.getLength() > 0;
59 const size_t size = fileStream.getLength();
60 void* data = sk_malloc_throw(size);
61 if (fileStream.read(data, size) < size) {
64 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
65 fStream.setData(skdata.get());
66 fDecoder.reset(SkImageDecoder::Factory(&fStream));
68 fDecoder->setSkipWritingZeroes(fSkipZeroes);
76 void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
79 SkDebugf("stream was invalid: %s\n", fFilename.c_str());
83 // Decode a bunch of times
85 for (int i = 0; i < loops; ++i) {
86 SkDEBUGCODE(SkImageDecoder::Result result =) fDecoder->decode(&fStream, &bm,
87 SkImageDecoder::kDecodePixels_Mode);
89 if (SkImageDecoder::kFailure == result) {
90 SkDebugf("failed to decode %s\n", fFilename.c_str());
94 SkDEBUGCODE(bool success =) fStream.rewind();
97 SkDebugf("failed to rewind %s\n", fFilename.c_str());
106 SkAutoTDelete<SkImageDecoder> fDecoder;
107 const SkString fFilename;
108 SkMemoryStream fStream;
112 typedef Benchmark INHERITED;
115 // Enable the true version once the feature is checked in.
116 DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true)));
117 DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false)));