blob: debda712b7bd2a12e2e53d370c9f1aecad81752f [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"
9#include "SkBitmap.h"
10#include "SkCodec.h"
11#include "SkImageGenerator.h"
12#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
19 const char* colorName;
20 switch(colorType) {
21 case kN32_SkColorType:
22 colorName = "N32";
23 break;
24 case kRGB_565_SkColorType:
25 colorName = "565";
26 break;
27 case kAlpha_8_SkColorType:
28 colorName = "Alpha8";
29 break;
30 default:
31 colorName = "Unknown";
32 }
33 fName.printf("Codec_%s_%s", baseName.c_str(), colorName);
34#ifdef SK_DEBUG
35 // Ensure that we can create an SkCodec from this data.
36 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
37 SkASSERT(codec);
38#endif
39}
40
41const char* CodecBench::onGetName() {
42 return fName.c_str();
43}
44
45bool CodecBench::isSuitableFor(Backend backend) {
46 return kNonRendering_Backend == backend;
47}
48
49void CodecBench::onPreDraw() {
50 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromData(fData));
scroggo21027992015-04-02 13:22:38 -070051
52 fInfo = codec->getInfo().makeColorType(fColorType);
53 SkAlphaType alphaType;
54 // Caller should not have created this CodecBench if the alpha type was
55 // invalid.
56 SkAssertResult(SkColorTypeValidateAlphaType(fColorType, fInfo.alphaType(),
57 &alphaType));
58 if (alphaType != fInfo.alphaType()) {
59 fInfo = fInfo.makeAlphaType(alphaType);
60 }
61
62 fPixelStorage.reset(fInfo.getSafeSize(fInfo.minRowBytes()));
scroggo60869a42015-04-01 12:09:17 -070063}
64
65void CodecBench::onDraw(const int n, SkCanvas* canvas) {
66 SkAutoTDelete<SkCodec> codec;
scroggo21027992015-04-02 13:22:38 -070067 SkPMColor colorTable[256];
68 int colorCount;
scroggo60869a42015-04-01 12:09:17 -070069 for (int i = 0; i < n; i++) {
scroggo21027992015-04-02 13:22:38 -070070 colorCount = 256;
scroggo60869a42015-04-01 12:09:17 -070071 codec.reset(SkCodec::NewFromData(fData));
72#ifdef SK_DEBUG
73 const SkImageGenerator::Result result =
74#endif
scroggo21027992015-04-02 13:22:38 -070075 codec->getPixels(fInfo, fPixelStorage.get(), fInfo.minRowBytes(),
76 NULL, colorTable, &colorCount);
scroggo60869a42015-04-01 12:09:17 -070077 SkASSERT(result == SkImageGenerator::kSuccess
78 || result == SkImageGenerator::kIncompleteInput);
79 }
80}