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 | } |
| 36 | fIsRendering = false; |
| 37 | } |
| 38 | |
| 39 | protected: |
| 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()) { |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame^] | 64 | fDecoder->setSkipWritingZeroes(fSkipZeroes); |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 65 | } else { |
| 66 | fValid = false; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 73 | if (!fValid) { |
| 74 | #ifdef SK_DEBUG |
| 75 | SkDebugf("stream was invalid: %s\n", fFilename.c_str()); |
| 76 | #endif |
| 77 | return; |
| 78 | } |
| 79 | // Decode a bunch of times |
| 80 | SkBitmap bm; |
| 81 | for (int i = 0; i < this->getLoops(); ++i) { |
| 82 | SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm, |
| 83 | SkImageDecoder::kDecodePixels_Mode); |
| 84 | #ifdef SK_DEBUG |
| 85 | if (!success) { |
| 86 | SkDebugf("failed to decode %s\n", fFilename.c_str()); |
| 87 | return; |
| 88 | } |
| 89 | #endif |
| 90 | SkDEBUGCODE(success =) fStream.rewind(); |
| 91 | #ifdef SK_DEBUG |
| 92 | if (!success) { |
| 93 | SkDebugf("failed to rewind %s\n", fFilename.c_str()); |
| 94 | return; |
| 95 | } |
| 96 | #endif |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private: |
| 101 | SkString fName; |
| 102 | SkAutoTDelete<SkImageDecoder> fDecoder; |
| 103 | const SkString fFilename; |
| 104 | SkMemoryStream fStream; |
| 105 | bool fSkipZeroes; |
| 106 | bool fValid; |
| 107 | |
| 108 | typedef SkBenchmark INHERITED; |
| 109 | }; |
| 110 | |
| 111 | // Enable the true version once the feature is checked in. |
scroggo@google.com | 8d23924 | 2013-10-01 17:27:15 +0000 | [diff] [blame^] | 112 | DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true))); |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 113 | DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false))); |