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 
8 #include "CodecBench.h"
9 #include "CodecBenchPriv.h"
10 #include "SkBitmap.h"
11 #include "SkCodec.h"
12 #include "SkCommandLineFlags.h"
13 #include "SkOSFile.h"
14 
15 // Actually zeroing the memory would throw off timing, so we just lie.
16 DEFINE_bool(zero_init, false, "Pretend our destination is zero-intialized, simulating Android?");
17 
CodecBench(SkString baseName,SkData * encoded,SkColorType colorType,SkAlphaType alphaType)18 CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType,
19         SkAlphaType alphaType)
20     : fColorType(colorType)
21     , fAlphaType(alphaType)
22     , fData(SkRef(encoded))
23 {
24     // Parse filename and the color type to give the benchmark a useful name
25     fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType),
26             alpha_type_to_str(alphaType));
27     // Ensure that we can create an SkCodec from this data.
28     SkASSERT(SkCodec::MakeFromData(fData));
29 }
30 
onGetName()31 const char* CodecBench::onGetName() {
32     return fName.c_str();
33 }
34 
isSuitableFor(Backend backend)35 bool CodecBench::isSuitableFor(Backend backend) {
36     return kNonRendering_Backend == backend;
37 }
38 
onDelayedSetup()39 void CodecBench::onDelayedSetup() {
40     std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData);
41 
42     fInfo = codec->getInfo().makeColorType(fColorType)
43                             .makeAlphaType(fAlphaType)
44                             .makeColorSpace(nullptr);
45 
46     fPixelStorage.reset(fInfo.computeMinByteSize());
47 }
48 
onDraw(int n,SkCanvas * canvas)49 void CodecBench::onDraw(int n, SkCanvas* canvas) {
50     std::unique_ptr<SkCodec> codec;
51     SkCodec::Options options;
52     if (FLAGS_zero_init) {
53         options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
54     }
55     for (int i = 0; i < n; i++) {
56         codec = SkCodec::MakeFromData(fData);
57 #ifdef SK_DEBUG
58         const SkCodec::Result result =
59 #endif
60         codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
61                          &options);
62         SkASSERT(result == SkCodec::kSuccess
63                  || result == SkCodec::kIncompleteInput);
64     }
65 }
66