xref: /third_party/skia/bench/CodecBench.cpp (revision cb93a386)
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.
16static DEFINE_bool(zero_init, false,
17                   "Pretend our destination is zero-intialized, simulating Android?");
18
19CodecBench::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
32const char* CodecBench::onGetName() {
33    return fName.c_str();
34}
35
36bool CodecBench::isSuitableFor(Backend backend) {
37    return kNonRendering_Backend == backend;
38}
39
40void 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
50void 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