msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 8 | #include "CodecBenchPriv.h" |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 9 | #include "DecodingBench.h" |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 10 | #include "SkBitmap.h" |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkImageDecoder.h" |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 13 | #include "SkMallocPixelRef.h" |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 14 | #include "SkOSFile.h" |
| 15 | #include "SkStream.h" |
| 16 | |
| 17 | /* |
| 18 | * |
| 19 | * This benchmark is designed to test the performance of image decoding. |
| 20 | * It is invoked from the nanobench.cpp file. |
| 21 | * |
| 22 | */ |
| 23 | DecodingBench::DecodingBench(SkString path, SkColorType colorType) |
| 24 | : fColorType(colorType) |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 25 | , fData(SkData::NewFromFileName(path.c_str())) |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 26 | { |
| 27 | // Parse filename and the color type to give the benchmark a useful name |
| 28 | SkString baseName = SkOSPath::Basename(path.c_str()); |
msarett | 7f69144 | 2015-09-22 11:56:16 -0700 | [diff] [blame] | 29 | fName.printf("Decode_%s_%s", baseName.c_str(), color_type_to_str(colorType)); |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 30 | |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 31 | #ifdef SK_DEBUG |
| 32 | // Ensure that we can create a decoder. |
| 33 | SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); |
| 34 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | SkASSERT(decoder != nullptr); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 36 | #endif |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | const char* DecodingBench::onGetName() { |
| 40 | return fName.c_str(); |
| 41 | } |
| 42 | |
| 43 | bool DecodingBench::isSuitableFor(Backend backend) { |
| 44 | return kNonRendering_Backend == backend; |
| 45 | } |
| 46 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame^] | 47 | void DecodingBench::onDelayedSetup() { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 48 | // Allocate the pixels now, to remove it from the loop. |
| 49 | SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); |
| 50 | SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 51 | SkBitmap bm; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 52 | #ifdef SK_DEBUG |
| 53 | SkImageDecoder::Result result = |
| 54 | #endif |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 55 | decoder->decode(stream, &bm, fColorType, SkImageDecoder::kDecodeBounds_Mode); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 56 | SkASSERT(SkImageDecoder::kFailure != result); |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 57 | |
| 58 | const size_t rowBytes = bm.info().minRowBytes(); |
| 59 | fPixelStorage.reset(bm.info().getSafeSize(rowBytes)); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 62 | // Allocator which just uses an existing block of memory. |
| 63 | class TargetAllocator : public SkBitmap::Allocator { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 64 | public: |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 65 | explicit TargetAllocator(void* storage) |
| 66 | : fPixelStorage(storage) {} |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 67 | |
| 68 | bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) override { |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 69 | // We depend on the fact that this will only ever be used to |
| 70 | // decode to a bitmap with the same settings used to create |
| 71 | // fPixelStorage. |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 72 | bm->setPixelRef(SkMallocPixelRef::NewDirect(bm->info(), |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 73 | fPixelStorage, bm->rowBytes(), ct))->unref(); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 74 | return true; |
| 75 | } |
| 76 | |
| 77 | private: |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 78 | void* fPixelStorage; // Unowned. DecodingBench owns this. |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 81 | void DecodingBench::onDraw(const int n, SkCanvas* canvas) { |
| 82 | SkBitmap bitmap; |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 83 | // Declare the allocator before the decoder, so it will outlive the |
| 84 | // decoder, which will unref it. |
scroggo | 2102799 | 2015-04-02 13:22:38 -0700 | [diff] [blame] | 85 | TargetAllocator allocator(fPixelStorage.get()); |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 86 | SkAutoTDelete<SkImageDecoder> decoder; |
| 87 | SkAutoTDelete<SkStreamRewindable> stream; |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 88 | for (int i = 0; i < n; i++) { |
scroggo | 60869a4 | 2015-04-01 12:09:17 -0700 | [diff] [blame] | 89 | // create a new stream and a new decoder to mimic the behavior of |
| 90 | // CodecBench. |
| 91 | stream.reset(new SkMemoryStream(fData)); |
| 92 | decoder.reset(SkImageDecoder::Factory(stream)); |
| 93 | decoder->setAllocator(&allocator); |
| 94 | decoder->decode(stream, &bitmap, fColorType, |
| 95 | SkImageDecoder::kDecodePixels_Mode); |
msarett | 95f192d | 2015-02-13 09:05:41 -0800 | [diff] [blame] | 96 | } |
| 97 | } |