blob: fc52edfb07bcda61fe6b40921e00c71a110608c6 [file] [log] [blame]
scroggo60869a42015-04-01 12:09:17 -07001/*
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"
msarett7f691442015-09-22 11:56:16 -07009#include "CodecBenchPriv.h"
scroggo60869a42015-04-01 12:09:17 -070010#include "SkBitmap.h"
11#include "SkCodec.h"
mtkleinc146aa62016-01-08 14:20:36 -080012#include "SkCommandLineFlags.h"
scroggo60869a42015-04-01 12:09:17 -070013#include "SkOSFile.h"
14
mtkleinc146aa62016-01-08 14:20:36 -080015// 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
msarettc7796b92016-01-07 14:20:20 -080018CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType,
19 SkAlphaType alphaType)
scroggo60869a42015-04-01 12:09:17 -070020 : fColorType(colorType)
msarettc7796b92016-01-07 14:20:20 -080021 , fAlphaType(alphaType)
scroggo60869a42015-04-01 12:09:17 -070022 , fData(SkRef(encoded))
23{
24 // Parse filename and the color type to give the benchmark a useful name
msarettc7796b92016-01-07 14:20:20 -080025 fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType),
26 alpha_type_to_str(alphaType));
scroggo60869a42015-04-01 12:09:17 -070027#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
joshualitt8a6697a2015-09-30 12:11:07 -070042void CodecBench::onDelayedSetup() {
scroggo60869a42015-04-01 12:09:17 -070043 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
scroggo21027992015-04-02 13:22:38 -070044
msarettc7796b92016-01-07 14:20:20 -080045 fInfo = codec->getInfo().makeColorType(fColorType).makeAlphaType(fAlphaType);
scroggo21027992015-04-02 13:22:38 -070046
47 fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes()));
scroggo60869a42015-04-01 12:09:17 -070048}
49
mtkleina1ebeb22015-10-01 09:43:39 -070050void CodecBench::onDraw(int n, SkCanvas* canvas) {
scroggo60869a42015-04-01 12:09:17 -070051 SkAutoTDelete<SkCodec> codec;
scroggo21027992015-04-02 13:22:38 -070052 SkPMColor colorTable[256];
53 int colorCount;
mtkleinc146aa62016-01-08 14:20:36 -080054 SkCodec::Options options;
55 if (FLAGS_zero_init) {
56 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
57 }
scroggo60869a42015-04-01 12:09:17 -070058 for (int i = 0; i < n; i++) {
scroggo21027992015-04-02 13:22:38 -070059 colorCount = 256;
scroggo60869a42015-04-01 12:09:17 -070060 codec.reset(SkCodec::NewFromData(fData));
61#ifdef SK_DEBUG
scroggoeb602a52015-07-09 08:16:03 -070062 const SkCodec::Result result =
scroggo60869a42015-04-01 12:09:17 -070063#endif
scroggo21027992015-04-02 13:22:38 -070064 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
mtkleinc146aa62016-01-08 14:20:36 -080065 &options, colorTable, &colorCount);
scroggoeb602a52015-07-09 08:16:03 -070066 SkASSERT(result == SkCodec::kSuccess
67 || result == SkCodec::kIncompleteInput);
scroggo60869a42015-04-01 12:09:17 -070068 }
69}