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