blob: 708ab60870d4a935fc6a258c9fc1cd1647d5a4c1 [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 // Ensure that we can create an SkCodec from this data.
Mike Reedede7bac2017-07-23 15:30:02 -040028 SkASSERT(SkCodec::MakeFromData(fData));
scroggo60869a42015-04-01 12:09:17 -070029}
30
31const char* CodecBench::onGetName() {
32 return fName.c_str();
33}
34
35bool CodecBench::isSuitableFor(Backend backend) {
36 return kNonRendering_Backend == backend;
37}
38
joshualitt8a6697a2015-09-30 12:11:07 -070039void CodecBench::onDelayedSetup() {
Mike Reedede7bac2017-07-23 15:30:02 -040040 std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(fData);
scroggo21027992015-04-02 13:22:38 -070041
msarett50ce1f22016-07-29 06:23:33 -070042 fInfo = codec->getInfo().makeColorType(fColorType)
43 .makeAlphaType(fAlphaType)
44 .makeColorSpace(nullptr);
scroggo21027992015-04-02 13:22:38 -070045
Mike Reedf0ffb892017-10-03 14:47:21 -040046 fPixelStorage.reset(fInfo.computeMinByteSize());
scroggo60869a42015-04-01 12:09:17 -070047}
48
mtkleina1ebeb22015-10-01 09:43:39 -070049void CodecBench::onDraw(int n, SkCanvas* canvas) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040050 std::unique_ptr<SkCodec> codec;
mtkleinc146aa62016-01-08 14:20:36 -080051 SkCodec::Options options;
52 if (FLAGS_zero_init) {
53 options.fZeroInitialized = SkCodec::kYes_ZeroInitialized;
54 }
scroggo60869a42015-04-01 12:09:17 -070055 for (int i = 0; i < n; i++) {
Mike Reedede7bac2017-07-23 15:30:02 -040056 codec = SkCodec::MakeFromData(fData);
scroggo60869a42015-04-01 12:09:17 -070057#ifdef SK_DEBUG
scroggoeb602a52015-07-09 08:16:03 -070058 const SkCodec::Result result =
scroggo60869a42015-04-01 12:09:17 -070059#endif
scroggo21027992015-04-02 13:22:38 -070060 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
Leon Scroggins571b30f2017-07-11 17:35:31 +000061 &options);
scroggoeb602a52015-07-09 08:16:03 -070062 SkASSERT(result == SkCodec::kSuccess
63 || result == SkCodec::kIncompleteInput);
scroggo60869a42015-04-01 12:09:17 -070064 }
65}