scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +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 ImageDecodeBench : public SkBenchmark { |
| 22 | public: |
| 23 | ImageDecodeBench(void* p, const char* filename) |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 24 | : fName("image_decode_") |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 25 | , fFilename(filename) |
| 26 | , fStream() |
| 27 | , fValid(false) { |
| 28 | fName.append(SkOSPath::SkBasename(filename)); |
| 29 | fIsRendering = false; |
| 30 | } |
| 31 | |
| 32 | protected: |
| 33 | virtual const char* onGetName() SK_OVERRIDE { |
| 34 | return fName.c_str(); |
| 35 | } |
| 36 | |
| 37 | virtual void onPreDraw() SK_OVERRIDE { |
| 38 | SkFILEStream fileStream(fFilename.c_str()); |
| 39 | fValid = fileStream.isValid() && fileStream.getLength() > 0; |
| 40 | if (fValid) { |
| 41 | const size_t size = fileStream.getLength(); |
| 42 | void* data = sk_malloc_throw(size); |
| 43 | if (fileStream.read(data, size) < size) { |
| 44 | fValid = false; |
| 45 | } else { |
| 46 | SkAutoTUnref<SkData> skdata(SkData::NewFromMalloc(data, size)); |
| 47 | fStream.setData(skdata.get()); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | virtual void onDraw(SkCanvas*) SK_OVERRIDE { |
| 53 | #ifdef SK_DEBUG |
| 54 | if (!fValid) { |
| 55 | SkDebugf("stream was invalid: %s\n", fName.c_str()); |
| 56 | return; |
| 57 | } |
| 58 | #endif |
| 59 | // Decode a bunch of times |
| 60 | SkBitmap bm; |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 61 | for (int i = 0; i < this->getLoops(); ++i) { |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 62 | SkDEBUGCODE(bool success =) SkImageDecoder::DecodeStream(&fStream, &bm); |
| 63 | #ifdef SK_DEBUG |
| 64 | if (!success) { |
| 65 | SkDebugf("failed to decode %s\n", fName.c_str()); |
| 66 | return; |
| 67 | } |
| 68 | #endif |
| 69 | SkDEBUGCODE(success =) fStream.rewind(); |
| 70 | #ifdef SK_DEBUG |
| 71 | if (!success) { |
| 72 | SkDebugf("failed to rewind %s\n", fName.c_str()); |
| 73 | return; |
| 74 | } |
| 75 | #endif |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | SkString fName; |
| 81 | const SkString fFilename; |
| 82 | SkMemoryStream fStream; |
| 83 | bool fValid; |
| 84 | |
| 85 | typedef SkBenchmark INHERITED; |
| 86 | }; |
| 87 | |
| 88 | // These are files which call decodePalette |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 89 | //DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/hal_163x90.png")); ) |
| 90 | //DEF_BENCH( return SkNEW_ARGS(ImageDecodeBench, ("/usr/local/google/home/scroggo/Downloads/images/box_19_top-left.png")); ) |