blob: 2c56838d588169a6078b6f7c0b97a9ce263a9f6a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
scroggo60869a42015-04-01 12:09:17 -070014
mtkleinc146aa62016-01-08 14:20:36 -080015// Actually zeroing the memory would throw off timing, so we just lie.
Mike Klein84836b72019-03-21 11:31:36 -050016static DEFINE_bool(zero_init, false,
17 "Pretend our destination is zero-intialized, simulating Android?");
mtkleinc146aa62016-01-08 14:20:36 -080018
msarettc7796b92016-01-07 14:20:20 -080019CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType,
20 SkAlphaType alphaType)
scroggo60869a42015-04-01 12:09:17 -070021 : fColorType(colorType)
msarettc7796b92016-01-07 14:20:20 -080022 , fAlphaType(alphaType)
scroggo60869a42015-04-01 12:09:17 -070023 , fData(SkRef(encoded))
24{
25 // Parse filename and the color type to give the benchmark a useful name
msarettc7796b92016-01-07 14:20:20 -080026 fName.printf("Codec_%s_%s%s", baseName.c_str(), color_type_to_str(colorType),
27 alpha_type_to_str(alphaType));
scroggo60869a42015-04-01 12:09:17 -070028 // Ensure that we can create an SkCodec from this data.
Mike Reedede7bac2017-07-23 15:30:02 -040029 SkASSERT(SkCodec::MakeFromData(fData));
scroggo60869a42015-04-01 12:09:17 -070030}
31
32const char* CodecBench::onGetName() {
33 return fName.c_str();
34}
35
36bool CodecBench::isSuitableFor(Backend backend) {
37 return kNonRendering_Backend == backend;
38}
39
joshualitt8a6697a2015-09-30 12:11:07 -070040void CodecBench::onDelayedSetup() {
Mike Reedede7bac2017-07-23 15:30:02 -040041 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData);
scroggo21027992015-04-02 13:22:38 -070042
msarett50ce1f22016-07-29 06:23:33 -070043 fInfo = codec->getInfo().makeColorType(fColorType)
44 .makeAlphaType(fAlphaType)
45 .makeColorSpace(nullptr);
scroggo21027992015-04-02 13:22:38 -070046
Mike Reedf0ffb892017-10-03 14:47:21 -040047 fPixelStorage.reset(fInfo.computeMinByteSize());
scroggo60869a42015-04-01 12:09:17 -070048}
49
mtkleina1ebeb22015-10-01 09:43:39 -070050void CodecBench::onDraw(int n, SkCanvas* canvas) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040051 std::unique_ptr<SkCodec> codec;
mtkleinc146aa62016-01-08 14:20:36 -080052 SkCodec::Options options;
53 if (FLAGS_zero_init) {
54 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
55 }
scroggo60869a42015-04-01 12:09:17 -070056 for (int i = 0; i < n; i++) {
Mike Reedede7bac2017-07-23 15:30:02 -040057 codec = SkCodec::MakeFromData(fData);
scroggo60869a42015-04-01 12:09:17 -070058#ifdef SK_DEBUG
scroggoeb602a52015-07-09 08:16:03 -070059 const SkCodec::Result result =
scroggo60869a42015-04-01 12:09:17 -070060#endif
scroggo21027992015-04-02 13:22:38 -070061 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000062 &options);
scroggoeb602a52015-07-09 08:16:03 -070063 SkASSERT(result == SkCodec::kSuccess
64 || result == SkCodec::kIncompleteInput);
scroggo60869a42015-04-01 12:09:17 -070065 }
66}