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 "SkData.h" | 9 #include "SkData.h" |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
| 11 #include "SkMallocPixelRef.h" |
11 #include "SkOSFile.h" | 12 #include "SkOSFile.h" |
| 13 #include "SkPixelRef.h" |
12 #include "SkStream.h" | 14 #include "SkStream.h" |
13 | 15 |
14 /* | 16 /* |
15 * | 17 * |
16 * This benchmark is designed to test the performance of image decoding. | 18 * This benchmark is designed to test the performance of image decoding. |
17 * It is invoked from the nanobench.cpp file. | 19 * It is invoked from the nanobench.cpp file. |
18 * | 20 * |
19 */ | 21 */ |
20 DecodingBench::DecodingBench(SkString path, SkColorType colorType) | 22 DecodingBench::DecodingBench(SkString path, SkColorType colorType) |
21 : fColorType(colorType) | 23 : fColorType(colorType) |
| 24 , fData(SkData::NewFromFileName(path.c_str())) |
22 { | 25 { |
23 // Parse filename and the color type to give the benchmark a useful name | 26 // Parse filename and the color type to give the benchmark a useful name |
24 SkString baseName = SkOSPath::Basename(path.c_str()); | 27 SkString baseName = SkOSPath::Basename(path.c_str()); |
25 const char* colorName; | 28 const char* colorName; |
26 switch(colorType) { | 29 switch(colorType) { |
27 case kN32_SkColorType: | 30 case kN32_SkColorType: |
28 colorName = "N32"; | 31 colorName = "N32"; |
29 break; | 32 break; |
30 case kRGB_565_SkColorType: | 33 case kRGB_565_SkColorType: |
31 colorName = "565"; | 34 colorName = "565"; |
32 break; | 35 break; |
33 case kAlpha_8_SkColorType: | 36 case kAlpha_8_SkColorType: |
34 colorName = "Alpha8"; | 37 colorName = "Alpha8"; |
35 break; | 38 break; |
36 default: | 39 default: |
37 colorName = "Unknown"; | 40 colorName = "Unknown"; |
38 } | 41 } |
39 fName.printf("Decode_%s_%s", baseName.c_str(), colorName); | 42 fName.printf("Decode_%s_%s", baseName.c_str(), colorName); |
40 | 43 |
41 // Perform setup for the decode | 44 #ifdef SK_DEBUG |
42 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); | 45 // Ensure that we can create a decoder. |
43 fStream.reset(new SkMemoryStream(encoded)); | 46 SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); |
44 fDecoder.reset(SkImageDecoder::Factory(fStream.get())); | 47 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
| 48 SkASSERT(decoder != NULL); |
| 49 #endif |
45 } | 50 } |
46 | 51 |
47 const char* DecodingBench::onGetName() { | 52 const char* DecodingBench::onGetName() { |
48 return fName.c_str(); | 53 return fName.c_str(); |
49 } | 54 } |
50 | 55 |
51 bool DecodingBench::isSuitableFor(Backend backend) { | 56 bool DecodingBench::isSuitableFor(Backend backend) { |
52 return kNonRendering_Backend == backend; | 57 return kNonRendering_Backend == backend; |
53 } | 58 } |
54 | 59 |
| 60 void DecodingBench::onPreDraw() { |
| 61 // Allocate the pixels now, to remove it from the loop. |
| 62 SkAutoTDelete<SkStreamRewindable> stream(new SkMemoryStream(fData)); |
| 63 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(stream)); |
| 64 #ifdef SK_DEBUG |
| 65 SkImageDecoder::Result result = |
| 66 #endif |
| 67 decoder->decode(stream, &fBitmap, fColorType, |
| 68 SkImageDecoder::kDecodeBounds_Mode); |
| 69 SkASSERT(SkImageDecoder::kFailure != result); |
| 70 fBitmap.allocPixels(fBitmap.info()); |
| 71 } |
| 72 |
| 73 // Allocator which just reuses the pixels from an existing SkPixelRef. |
| 74 class UseExistingAllocator : public SkBitmap::Allocator { |
| 75 public: |
| 76 explicit UseExistingAllocator(SkPixelRef* pr) |
| 77 : fPixelRef(SkRef(pr)) {} |
| 78 |
| 79 bool allocPixelRef(SkBitmap* bm, SkColorTable* ct) override { |
| 80 // We depend on the fact that fPixelRef is an SkMallocPixelRef, which |
| 81 // is always locked, and the fact that this will only ever be used to |
| 82 // decode to a bitmap with the same settings used to create the |
| 83 // original pixel ref. |
| 84 bm->setPixelRef(SkMallocPixelRef::NewDirect(bm->info(), |
| 85 fPixelRef->pixels(), bm->rowBytes(), ct))->unref(); |
| 86 return true; |
| 87 } |
| 88 |
| 89 private: |
| 90 SkAutoTUnref<SkPixelRef> fPixelRef; |
| 91 }; |
| 92 |
55 void DecodingBench::onDraw(const int n, SkCanvas* canvas) { | 93 void DecodingBench::onDraw(const int n, SkCanvas* canvas) { |
56 SkBitmap bitmap; | 94 SkBitmap bitmap; |
| 95 // Declare the allocator before the decoder, so it will outlive the |
| 96 // decoder, which will unref it. |
| 97 UseExistingAllocator allocator(fBitmap.pixelRef()); |
| 98 SkAutoTDelete<SkImageDecoder> decoder; |
| 99 SkAutoTDelete<SkStreamRewindable> stream; |
57 for (int i = 0; i < n; i++) { | 100 for (int i = 0; i < n; i++) { |
58 fStream->rewind(); | 101 // create a new stream and a new decoder to mimic the behavior of |
59 fDecoder->decode(fStream, &bitmap, fColorType, | 102 // CodecBench. |
60 SkImageDecoder::kDecodePixels_Mode); | 103 stream.reset(new SkMemoryStream(fData)); |
| 104 decoder.reset(SkImageDecoder::Factory(stream)); |
| 105 decoder->setAllocator(&allocator); |
| 106 decoder->decode(stream, &bitmap, fColorType, |
| 107 SkImageDecoder::kDecodePixels_Mode); |
61 } | 108 } |
62 } | 109 } |
OLD | NEW |