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) |
(...skipping 12 matching lines...) Expand all Loading... | |
34 colorName = "Alpha8"; | 36 colorName = "Alpha8"; |
35 break; | 37 break; |
36 default: | 38 default: |
37 colorName = "Unknown"; | 39 colorName = "Unknown"; |
38 } | 40 } |
39 fName.printf("Decode_%s_%s", baseName.c_str(), colorName); | 41 fName.printf("Decode_%s_%s", baseName.c_str(), colorName); |
40 | 42 |
41 // Perform setup for the decode | 43 // Perform setup for the decode |
42 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); | 44 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str())); |
43 fStream.reset(new SkMemoryStream(encoded)); | 45 fStream.reset(new SkMemoryStream(encoded)); |
44 fDecoder.reset(SkImageDecoder::Factory(fStream.get())); | 46 #ifdef SK_DEBUG |
47 // Ensure that we can create a decoder. | |
48 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | |
49 SkASSERT(decoder != NULL); | |
djsollen
2015/04/01 17:23:50
move this to line 64.
scroggo
2015/04/01 17:40:54
Again, do you think doing less work here is better
| |
50 #endif | |
45 } | 51 } |
46 | 52 |
47 const char* DecodingBench::onGetName() { | 53 const char* DecodingBench::onGetName() { |
48 return fName.c_str(); | 54 return fName.c_str(); |
49 } | 55 } |
50 | 56 |
51 bool DecodingBench::isSuitableFor(Backend backend) { | 57 bool DecodingBench::isSuitableFor(Backend backend) { |
52 return kNonRendering_Backend == backend; | 58 return kNonRendering_Backend == backend; |
53 } | 59 } |
54 | 60 |
61 void DecodingBench::onPreDraw() { | |
62 // Allocate the pixels now, to remove it from the loop. | |
63 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream)); | |
64 #ifdef SK_DEBUG | |
65 SkImageDecoder::Result result = | |
66 #endif | |
67 decoder->decode(fStream, &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 // duplicate the stream and create a new decoder to mimic the behavior o f CodecBench. |
59 fDecoder->decode(fStream, &bitmap, fColorType, | 102 stream.reset(fStream->duplicate()); |
60 SkImageDecoder::kDecodePixels_Mode); | 103 decoder.reset(SkImageDecoder::Factory(stream)); |
104 decoder->setAllocator(&allocator); | |
105 decoder->decode(stream, &bitmap, fColorType, | |
106 SkImageDecoder::kDecodePixels_Mode); | |
61 } | 107 } |
62 } | 108 } |
OLD | NEW |