blob: 232c159a0baea1030e442bc2365b6702991126f4 [file] [log] [blame]
msarettb23e6aa2015-06-09 13:56:10 -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
msarett7f691442015-09-22 11:56:16 -07008#include "CodecBenchPriv.h"
msarettb23e6aa2015-06-09 13:56:10 -07009#include "SubsetSingleBench.h"
10#include "SubsetBenchPriv.h"
11#include "SkData.h"
12#include "SkCodec.h"
13#include "SkImageDecoder.h"
14#include "SkOSFile.h"
15#include "SkStream.h"
16
17/*
18 *
19 * This benchmark is designed to test the performance of subset decoding.
20 * It uses an input width, height, left, and top to decode a single subset.
21 *
22 */
23
24SubsetSingleBench::SubsetSingleBench(const SkString& path,
25 SkColorType colorType,
26 uint32_t subsetWidth,
27 uint32_t subsetHeight,
28 uint32_t offsetLeft,
29 uint32_t offsetTop,
30 bool useCodec)
31 : fColorType(colorType)
32 , fSubsetWidth(subsetWidth)
33 , fSubsetHeight(subsetHeight)
34 , fOffsetLeft(offsetLeft)
35 , fOffsetTop(offsetTop)
36 , fUseCodec(useCodec)
37{
38 // Parse the filename
39 SkString baseName = SkOSPath::Basename(path.c_str());
40
41 // Choose an informative color name
msarett7f691442015-09-22 11:56:16 -070042 const char* colorName = color_type_to_str(fColorType);
msarettb23e6aa2015-06-09 13:56:10 -070043
44 fName.printf("%sSubsetSingle_%dx%d +%d_+%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth,
45 fSubsetHeight, fOffsetLeft, fOffsetTop, baseName.c_str(), colorName);
46
47 // Perform the decode setup
48 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
49 fStream.reset(new SkMemoryStream(encoded));
50}
51
52const char* SubsetSingleBench::onGetName() {
53 return fName.c_str();
54}
55
56bool SubsetSingleBench::isSuitableFor(Backend backend) {
57 return kNonRendering_Backend == backend;
58}
59
mtkleina1ebeb22015-10-01 09:43:39 -070060void SubsetSingleBench::onDraw(int n, SkCanvas* canvas) {
msarettb23e6aa2015-06-09 13:56:10 -070061 // When the color type is kIndex8, we will need to store the color table. If it is
62 // used, it will be initialized by the codec.
63 int colorCount;
64 SkPMColor colors[256];
65 if (fUseCodec) {
66 for (int count = 0; count < n; count++) {
scroggo46c57472015-09-30 08:57:13 -070067 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate()));
68 const SkImageInfo info = codec->getInfo().makeColorType(fColorType);
halcanary385fe4d2015-08-26 13:07:48 -070069 SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]);
scroggo46c57472015-09-30 08:57:13 -070070 codec->startScanlineDecode(info, nullptr, colors, &colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070071
72 SkBitmap bitmap;
msarett7f6283b2015-06-30 13:29:37 -070073 SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
74 alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070075
scroggo46c57472015-09-30 08:57:13 -070076 codec->skipScanlines(fOffsetTop);
msarettb23e6aa2015-06-09 13:56:10 -070077 uint32_t bpp = info.bytesPerPixel();
78 for (uint32_t y = 0; y < fSubsetHeight; y++) {
scroggo46c57472015-09-30 08:57:13 -070079 codec->getScanlines(row.get(), 1, 0);
msarettb23e6aa2015-06-09 13:56:10 -070080 memcpy(bitmap.getAddr(0, y), row.get() + fOffsetLeft * bpp,
81 fSubsetWidth * bpp);
82 }
83 }
84 } else {
85 for (int count = 0; count < n; count++) {
86 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
87 int width, height;
88 decoder->buildTileIndex(fStream->duplicate(), &width, &height);
89 SkBitmap bitmap;
90 SkIRect rect = SkIRect::MakeXYWH(fOffsetLeft, fOffsetTop, fSubsetWidth,
91 fSubsetHeight);
92 decoder->decodeSubset(&bitmap, rect, fColorType);
93 }
94 }
95}