| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "DecodingBench.h" | 8 #include "DecodingBench.h" |
| 9 #include "SkBitmap.h" |
| 9 #include "SkData.h" | 10 #include "SkData.h" |
| 10 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
| 11 #include "SkMallocPixelRef.h" | 12 #include "SkMallocPixelRef.h" |
| 12 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
| 13 #include "SkPixelRef.h" | |
| 14 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 | 15 |
| 16 /* | 16 /* |
| 17 * | 17 * |
| 18 * This benchmark is designed to test the performance of image decoding. | 18 * This benchmark is designed to test the performance of image decoding. |
| 19 * It is invoked from the nanobench.cpp file. | 19 * It is invoked from the nanobench.cpp file. |
| 20 * | 20 * |
| 21 */ | 21 */ |
| 22 DecodingBench::DecodingBench(SkString path, SkColorType colorType) | 22 DecodingBench::DecodingBench(SkString path, SkColorType colorType) |
| 23 : fColorType(colorType) | 23 : fColorType(colorType) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool DecodingBench::isSuitableFor(Backend backend) { | 56 bool DecodingBench::isSuitableFor(Backend backend) { |
| 57 return kNonRendering_Backend == backend; | 57 return kNonRendering_Backend == backend; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void DecodingBench::onPreDraw() { | 60 void DecodingBench::onPreDraw() { |
| 61 // Allocate the pixels now, to remove it from the loop. | 61 // Allocate the pixels now, to remove it from the loop. |
| 62 SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); | 62 SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); |
| 63 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); | 63 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
| 64 SkBitmap bm; |
| 64 #ifdef SK_DEBUG | 65 #ifdef SK_DEBUG |
| 65 SkImageDecoder::Result result = | 66 SkImageDecoder::Result result = |
| 66 #endif | 67 #endif |
| 67 decoder->decode(stream, &fBitmap, fColorType, | 68 decoder->decode(stream, &bm, fColorType, SkImageDecoder::kDecodeBounds_Mode)
; |
| 68 SkImageDecoder::kDecodeBounds_Mode); | |
| 69 SkASSERT(SkImageDecoder::kFailure != result); | 69 SkASSERT(SkImageDecoder::kFailure != result); |
| 70 fBitmap.allocPixels(fBitmap.info()); | 70 |
| 71 const size_t rowBytes = bm.info().minRowBytes(); |
| 72 fStorage.reset(bm.info().getSafeSize(rowBytes)); |
| 71 } | 73 } |
| 72 | 74 |
| 73 // Allocator which just reuses the pixels from an existing SkPixelRef. | 75 // Allocator which just uses an existing block of memory. |
| 74 class UseExistingAllocator : public SkBitmap::Allocator { | 76 class TargetAllocator : public SkBitmap::Allocator { |
| 75 public: | 77 public: |
| 76 explicit UseExistingAllocator(SkPixelRef* pr) | 78 explicit TargetAllocator(void* storage) |
| 77 : fPixelRef(SkRef(pr)) {} | 79 : fStorage(storage) {} |
| 78 | 80 |
| 79 bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) override { | 81 bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) override { |
| 80 // We depend on the fact that fPixelRef is an SkMallocPixelRef, which | 82 // We depend on the fact that this will only ever be used to |
| 81 // is always locked, and the fact that this will only ever be used to | 83 // decode to a bitmap with the same settings used to create |
| 82 // decode to a bitmap with the same settings used to create the | 84 // fStorage. |
| 83 // original pixel ref. | |
| 84 bm->setPixelRef(SkMallocPixelRef::NewDirect(bm->info(), | 85 bm->setPixelRef(SkMallocPixelRef::NewDirect(bm->info(), |
| 85 fPixelRef->pixels(), bm->rowBytes(), ct))->unref(); | 86 fStorage, bm->rowBytes(), ct))->unref(); |
| 86 return true; | 87 return true; |
| 87 } | 88 } |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 SkAutoTUnref<SkPixelRef> fPixelRef; | 91 void* fStorage; // Unowned. DecodingBench owns this. |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 void DecodingBench::onDraw(const int n, SkCanvas* canvas) { | 94 void DecodingBench::onDraw(const int n, SkCanvas* canvas) { |
| 94 SkBitmap bitmap; | 95 SkBitmap bitmap; |
| 95 // Declare the allocator before the decoder, so it will outlive the | 96 // Declare the allocator before the decoder, so it will outlive the |
| 96 // decoder, which will unref it. | 97 // decoder, which will unref it. |
| 97 UseExistingAllocator allocator(fBitmap.pixelRef()); | 98 TargetAllocator allocator(fStorage.get()); |
| 98 SkAutoTDelete<SkImageDecoder> decoder; | 99 SkAutoTDelete<SkImageDecoder> decoder; |
| 99 SkAutoTDelete<SkStreamRewindable> stream; | 100 SkAutoTDelete<SkStreamRewindable> stream; |
| 100 for (int i = 0; i < n; i++) { | 101 for (int i = 0; i < n; i++) { |
| 101 // create a new stream and a new decoder to mimic the behavior of | 102 // create a new stream and a new decoder to mimic the behavior of |
| 102 // CodecBench. | 103 // CodecBench. |
| 103 stream.reset(new SkMemoryStream(fData)); | 104 stream.reset(new SkMemoryStream(fData)); |
| 104 decoder.reset(SkImageDecoder::Factory(stream)); | 105 decoder.reset(SkImageDecoder::Factory(stream)); |
| 105 decoder->setAllocator(&allocator); | 106 decoder->setAllocator(&allocator); |
| 106 decoder->decode(stream, &bitmap, fColorType, | 107 decoder->decode(stream, &bitmap, fColorType, |
| 107 SkImageDecoder::kDecodePixels_Mode); | 108 SkImageDecoder::kDecodePixels_Mode); |
| 108 } | 109 } |
| 109 } | 110 } |
| OLD | NEW |