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