blob: 1a5f61d2a60ab89f43b4cf9ede86b6fa3d086569 [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
tfarinabcbc1782014-06-18 14:32:48 -07009#include "Resources.h"
scroggo@google.com111fd112013-09-25 21:42:12 +000010#include "SkBitmap.h"
11#include "SkData.h"
12#include "SkForceLinking.h"
13#include "SkImageDecoder.h"
14#include "SkOSFile.h"
15#include "SkStream.h"
16#include "SkString.h"
17
18__SK_FORCE_IMAGE_DECODER_LINKING;
19
20class SkCanvas;
21
tfarinaf168b862014-06-19 12:32:29 -070022class SkipZeroesBench : public Benchmark {
scroggo@google.com111fd112013-09-25 21:42:12 +000023public:
24 SkipZeroesBench(const char* filename, bool skipZeroes)
25 : fName("SkipZeroes_")
halcanary96fcdcc2015-08-27 07:41:13 -070026 , fDecoder(nullptr)
scroggo@google.com111fd112013-09-25 21:42:12 +000027 , fFilename(filename)
28 , fStream()
29 , fSkipZeroes(skipZeroes)
30 , fValid(false) {
31 fName.append(filename);
32 if (skipZeroes) {
33 fName.append("_skip_zeroes");
34 } else {
35 fName.append("_write_zeroes");
36 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040 return backend == kNonRendering_Backend;
scroggo@google.com111fd112013-09-25 21:42:12 +000041 }
42
43protected:
mtklein36352bf2015-03-25 18:17:31 -070044 const char* onGetName() override {
scroggo@google.com111fd112013-09-25 21:42:12 +000045 return fName.c_str();
46 }
47
joshualitt8a6697a2015-09-30 12:11:07 -070048 void onDelayedSetup() override {
tfarina880914c2014-06-09 12:05:34 -070049 SkString resourcePath = GetResourcePath();
50 if (resourcePath.isEmpty()) {
scroggo@google.com111fd112013-09-25 21:42:12 +000051 fValid = false;
52 return;
53 }
54
tfarinaa8e2e152014-07-28 19:26:58 -070055 SkString fullPath = SkOSPath::Join(resourcePath.c_str(), fFilename.c_str());
scroggo@google.com111fd112013-09-25 21:42:12 +000056 SkFILEStream fileStream(fullPath.c_str());
57 fValid = fileStream.isValid() && fileStream.getLength() > 0;
58 if (fValid) {
59 const size_t size = fileStream.getLength();
60 void* data = sk_malloc_throw(size);
61 if (fileStream.read(data, size) < size) {
62 fValid = false;
63 } else {
64 SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size));
65 fStream.setData(skdata.get());
66 fDecoder.reset(SkImageDecoder::Factory(&fStream));
67 if (fDecoder.get()) {
scroggo@google.com8d239242013-10-01 17:27:15 +000068 fDecoder->setSkipWritingZeroes(fSkipZeroes);
scroggo@google.com111fd112013-09-25 21:42:12 +000069 } else {
70 fValid = false;
71 }
72 }
73 }
74 }
75
mtkleina1ebeb22015-10-01 09:43:39 -070076 void onDraw(int loops, SkCanvas*) override {
scroggo@google.com111fd112013-09-25 21:42:12 +000077 if (!fValid) {
78#ifdef SK_DEBUG
79 SkDebugf("stream was invalid: %s\n", fFilename.c_str());
80#endif
81 return;
82 }
83 // Decode a bunch of times
84 SkBitmap bm;
commit-bot@chromium.org33614712013-12-03 18:17:16 +000085 for (int i = 0; i < loops; ++i) {
scroggo2a120802014-10-22 12:07:00 -070086 SkDEBUGCODE(SkImageDecoder::Result result =) fDecoder->decode(&fStream, &bm,
87 SkImageDecoder::kDecodePixels_Mode);
scroggo@google.com111fd112013-09-25 21:42:12 +000088#ifdef SK_DEBUG
scroggo2a120802014-10-22 12:07:00 -070089 if (SkImageDecoder::kFailure == result) {
scroggo@google.com111fd112013-09-25 21:42:12 +000090 SkDebugf("failed to decode %s\n", fFilename.c_str());
91 return;
92 }
93#endif
scroggo2a120802014-10-22 12:07:00 -070094 SkDEBUGCODE(bool success =) fStream.rewind();
scroggo@google.com111fd112013-09-25 21:42:12 +000095#ifdef SK_DEBUG
96 if (!success) {
97 SkDebugf("failed to rewind %s\n", fFilename.c_str());
98 return;
99 }
100#endif
101 }
102 }
103
104private:
105 SkString fName;
106 SkAutoTDelete<SkImageDecoder> fDecoder;
107 const SkString fFilename;
108 SkMemoryStream fStream;
109 bool fSkipZeroes;
110 bool fValid;
111
tfarinaf168b862014-06-19 12:32:29 -0700112 typedef Benchmark INHERITED;
scroggo@google.com111fd112013-09-25 21:42:12 +0000113};
114
115// Enable the true version once the feature is checked in.
halcanary385fe4d2015-08-26 13:07:48 -0700116DEF_BENCH(return new SkipZeroesBench("arrow.png", true));
117DEF_BENCH(return new SkipZeroesBench("arrow.png", false));