blob: 6e4b54bdf69c1ecebec9d4eaf43ed0e13464308c [file] [log] [blame]
scroggo@google.com111fd112013-09-25 21:42:12 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkData.h"
11#include "SkForceLinking.h"
12#include "SkImageDecoder.h"
13#include "SkOSFile.h"
14#include "SkStream.h"
15#include "SkString.h"
16
17__SK_FORCE_IMAGE_DECODER_LINKING;
18
19class SkCanvas;
20
21class SkipZeroesBench : public SkBenchmark {
22public:
23 SkipZeroesBench(const char* filename, bool skipZeroes)
24 : fName("SkipZeroes_")
25 , fDecoder(NULL)
26 , fFilename(filename)
27 , fStream()
28 , fSkipZeroes(skipZeroes)
29 , fValid(false) {
30 fName.append(filename);
31 if (skipZeroes) {
32 fName.append("_skip_zeroes");
33 } else {
34 fName.append("_write_zeroes");
35 }
36 fIsRendering = false;
37 }
38
39protected:
40 virtual const char* onGetName() SK_OVERRIDE {
41 return fName.c_str();
42 }
43
44 virtual void onPreDraw() SK_OVERRIDE {
45 const char* resPath = GetResourcePath().c_str();
46 if (NULL == resPath) {
47 fValid = false;
48 return;
49 }
50
51 SkString fullPath = SkOSPath::SkPathJoin(resPath, fFilename.c_str());
52 SkFILEStream fileStream(fullPath.c_str());
53 fValid = fileStream.isValid() && fileStream.getLength() > 0;
54 if (fValid) {
55 const size_t size = fileStream.getLength();
56 void* data = sk_malloc_throw(size);
57 if (fileStream.read(data, size) < size) {
58 fValid = false;
59 } else {
60 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
61 fStream.setData(skdata.get());
62 fDecoder.reset(SkImageDecoder::Factory(&fStream));
63 if (fDecoder.get()) {
64 // Disabling until the feature is checked in.
65 // See https://codereview.chromium.org/24269006/
66 //fDecoder->setSkipWritingZeroes(fSkipZeroes);
67 } else {
68 fValid = false;
69 }
70 }
71 }
72 }
73
74 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
75 if (!fValid) {
76#ifdef SK_DEBUG
77 SkDebugf("stream was invalid: %s\n", fFilename.c_str());
78#endif
79 return;
80 }
81 // Decode a bunch of times
82 SkBitmap bm;
83 for (int i = 0; i < this->getLoops(); ++i) {
84 SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm,
85 SkImageDecoder::kDecodePixels_Mode);
86#ifdef SK_DEBUG
87 if (!success) {
88 SkDebugf("failed to decode %s\n", fFilename.c_str());
89 return;
90 }
91#endif
92 SkDEBUGCODE(success =) fStream.rewind();
93#ifdef SK_DEBUG
94 if (!success) {
95 SkDebugf("failed to rewind %s\n", fFilename.c_str());
96 return;
97 }
98#endif
99 }
100 }
101
102private:
103 SkString fName;
104 SkAutoTDelete<SkImageDecoder> fDecoder;
105 const SkString fFilename;
106 SkMemoryStream fStream;
107 bool fSkipZeroes;
108 bool fValid;
109
110 typedef SkBenchmark INHERITED;
111};
112
113// Enable the true version once the feature is checked in.
114//DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true)));
115DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false)));