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()) { |
| 64 | // Disabling until the feature is checked in. |
| 65 | // See https://codereview.chromium.org/24269006/ |
| 66 | //fDecoder->setSkipWritingZeroes(fSkipZeroes); |
scroggo@google.com | b90a2ca | 2013-09-25 22:01:17 +0000 | [diff] [blame] | 67 | if (fSkipZeroes) { |
| 68 | // This printf will never happen since this bench is |
| 69 | // only currently created with false. Remove once |
| 70 | // the above code is uncommented. |
| 71 | SkDebugf("Should be skipping zeroes...\n"); |
| 72 | } |
scroggo@google.com | 111fd11 | 2013-09-25 21:42:12 +0000 | [diff] [blame] | 73 | } else { |
| 74 | fValid = false; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 81 | if (!fValid) { |
| 82 | #ifdef SK_DEBUG |
| 83 | SkDebugf("stream was invalid: %s\n", fFilename.c_str()); |
| 84 | #endif |
| 85 | return; |
| 86 | } |
| 87 | // Decode a bunch of times |
| 88 | SkBitmap bm; |
| 89 | for (int i = 0; i < this->getLoops(); ++i) { |
| 90 | SkDEBUGCODE(bool success =) fDecoder->decode(&fStream, &bm, |
| 91 | SkImageDecoder::kDecodePixels_Mode); |
| 92 | #ifdef SK_DEBUG |
| 93 | if (!success) { |
| 94 | SkDebugf("failed to decode %s\n", fFilename.c_str()); |
| 95 | return; |
| 96 | } |
| 97 | #endif |
| 98 | SkDEBUGCODE(success =) fStream.rewind(); |
| 99 | #ifdef SK_DEBUG |
| 100 | if (!success) { |
| 101 | SkDebugf("failed to rewind %s\n", fFilename.c_str()); |
| 102 | return; |
| 103 | } |
| 104 | #endif |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | SkString fName; |
| 110 | SkAutoTDelete<SkImageDecoder> fDecoder; |
| 111 | const SkString fFilename; |
| 112 | SkMemoryStream fStream; |
| 113 | bool fSkipZeroes; |
| 114 | bool fValid; |
| 115 | |
| 116 | typedef SkBenchmark INHERITED; |
| 117 | }; |
| 118 | |
| 119 | // Enable the true version once the feature is checked in. |
| 120 | //DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", true))); |
| 121 | DEF_BENCH( return SkNEW_ARGS(SkipZeroesBench, ("arrow.png", false))); |