blob: 1384480f43e96ae6bda1e49a0777e2e1e1825dd4 [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"
scroggo60869a42015-04-01 12:09:17 -070012#include "SkOSFile.h"
13
14CodecBench::CodecBench(SkString baseName, SkData* encoded, SkColorType colorType)
15 : fColorType(colorType)
16 , fData(SkRef(encoded))
17{
18 // Parse filename and the color type to give the benchmark a useful name
msarett7f691442015-09-22 11:56:16 -070019 fName.printf("Codec_%s_%s", baseName.c_str(), color_type_to_str(colorType));
scroggo60869a42015-04-01 12:09:17 -070020#ifdef SK_DEBUG
21 // Ensure that we can create an SkCodec from this data.
22 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
23 SkASSERT(codec);
24#endif
25}
26
27const char* CodecBench::onGetName() {
28 return fName.c_str();
29}
30
31bool CodecBench::isSuitableFor(Backend backend) {
32 return kNonRendering_Backend == backend;
33}
34
joshualitt8a6697a2015-09-30 12:11:07 -070035void CodecBench::onDelayedSetup() {
scroggo60869a42015-04-01 12:09:17 -070036 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
scroggo21027992015-04-02 13:22:38 -070037
38 fInfo = codec->getInfo().makeColorType(fColorType);
39 SkAlphaType alphaType;
40 // Caller should not have created this CodecBench if the alpha type was
41 // invalid.
42 SkAssertResult(SkColorTypeValidateAlphaType(fColorType, fInfo.alphaType(),
43 &alphaType));
44 if (alphaType != fInfo.alphaType()) {
45 fInfo = fInfo.makeAlphaType(alphaType);
46 }
47
48 fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes()));
scroggo60869a42015-04-01 12:09:17 -070049}
50
mtkleina1ebeb22015-10-01 09:43:39 -070051void CodecBench::onDraw(int n, SkCanvas* canvas) {
scroggo60869a42015-04-01 12:09:17 -070052 SkAutoTDelete<SkCodec> codec;
scroggo21027992015-04-02 13:22:38 -070053 SkPMColor colorTable[256];
54 int colorCount;
scroggo60869a42015-04-01 12:09:17 -070055 for (int i = 0; i < n; i++) {
scroggo21027992015-04-02 13:22:38 -070056 colorCount = 256;
scroggo60869a42015-04-01 12:09:17 -070057 codec.reset(SkCodec::NewFromData(fData));
58#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(),
halcanary96fcdcc2015-08-27 07:41:13 -070062 nullptr, colorTable, &colorCount);
scroggoeb602a52015-07-09 08:16:03 -070063 SkASSERT(result == SkCodec::kSuccess
64 || result == SkCodec::kIncompleteInput);
scroggo60869a42015-04-01 12:09:17 -070065 }
66}