scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | class SkCanvas; |
| 20 | |
| 21 | class SkipZeroesBench : public SkBenchmark { |
| 22 | public: |
| 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 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
| 39 | return backend == kNonRendering_Backend; |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | protected: |
| 43 | virtual const char* onGetName() SK_OVERRIDE { |
| 44 | return fName.c_str(); |
| 45 | } |
| 46 | |
| 47 | virtual void onPreDraw() SK_OVERRIDE { |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame^] | 48 | SkString resourcePath = GetResourcePath(); |
| 49 | if (resourcePath.isEmpty()) { |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 50 | fValid = false; |
| 51 | return; |
| 52 | } |
| 53 | |
tfarina | 880914c | 2014-06-09 12:05:34 -0700 | [diff] [blame^] | 54 | SkString fullPath = SkOSPath::SkPathJoin(resourcePath.c_str(), |
| 55 | fFilename.c_str()); |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 56 | 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.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 68 | fDecoder->setSkipWritingZeroes(fSkipZeroes); |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 69 | } else { |
| 70 | fValid = false; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 76 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 77 | 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.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 85 | for (int i = 0; i < loops; ++i) { |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 86 | SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm, |
| 87 | SkImageDecoder::kDecodePixels_Mode); |
| 88 | #ifdef SK_DEBUG |
| 89 | if (!success) { |
| 90 | SkDebugf("failed to decode %s\n", fFilename.c_str()); |
| 91 | return; |
| 92 | } |
| 93 | #endif |
| 94 | SkDEBUGCODE(success =) fStream.rewind(); |
| 95 | #ifdef SK_DEBUG |
| 96 | if (!success) { |
| 97 | SkDebugf("failed to rewind %s\n", fFilename.c_str()); |
| 98 | return; |
| 99 | } |
| 100 | #endif |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | SkString fName; |
| 106 | SkAutoTDelete<SkImageDecoder> fDecoder; |
| 107 | const SkString fFilename; |
| 108 | SkMemoryStream fStream; |
| 109 | bool fSkipZeroes; |
| 110 | bool fValid; |
| 111 | |
| 112 | typedef SkBenchmark INHERITED; |
| 113 | }; |
| 114 | |
| 115 | // Enable the true version once the feature is checked in. |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame] | 116 | DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true))); |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 117 | DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false))); |