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.
16DEFINE_bool(zero_init, false, "Pretend our destination is zero-intialized, simulating Android?");
17
18CodecBench::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#ifdef SK_DEBUG
28    // Ensure that we can create an SkCodec from this data.
29    SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
30    SkASSERT(codec);
31#endif
32}
33
34const char* CodecBench::onGetName() {
35    return fName.c_str();
36}
37
38bool CodecBench::isSuitableFor(Backend backend) {
39    return kNonRendering_Backend == backend;
40}
41
42void CodecBench::onDelayedSetup() {
43    SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
44
45    fInfo = codec->getInfo().makeColorType(fColorType).makeAlphaType(fAlphaType);
46
47    fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes()));
48}
49
50void CodecBench::onDraw(int n, SkCanvas* canvas) {
51    SkAutoTDelete<SkCodec> codec;
52    SkPMColor colorTable[256];
53    int colorCount;
54    SkCodec::Options options;
55    if (FLAGS_zero_init) {
56        options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
57    }
58    for (int i = 0; i < n; i++) {
59        colorCount = 256;
60        codec.reset(SkCodec::NewFromData(fData));
61#ifdef SK_DEBUG
62        const SkCodec::Result result =
63#endif
64        codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
65                         &options, colorTable, &colorCount);
66        SkASSERT(result == SkCodec::kSuccess
67                 || result == SkCodec::kIncompleteInput);
68    }
69}
70