2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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"
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?");
19 CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType,
20 SkAlphaType alphaType)
21 : fColorType(colorType)
22 , fAlphaType(alphaType)
23 , fData(SkRef(encoded))
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));
32 const char* CodecBench::onGetName() {
36 bool CodecBench::isSuitableFor(Backend backend) {
37 return kNonRendering_Backend == backend;
40 void CodecBench::onDelayedSetup() {
41 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData);
43 fInfo = codec->getInfo().makeColorType(fColorType)
44 .makeAlphaType(fAlphaType)
45 .makeColorSpace(nullptr);
47 fPixelStorage.reset(fInfo.computeMinByteSize());
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;
56 for (int i = 0; i < n; i++) {
57 codec = SkCodec::MakeFromData(fData);
59 const SkCodec::Result result =
61 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
63 SkASSERT(result == SkCodec::kSuccess
64 || result == SkCodec::kIncompleteInput);