blob: 8f29ba8fa011f25ef74eee4eb09d2ebb09fcb73a [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 "SubsetTranslateBench.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 input dimensions to decode the entire image where each block is susbetW x subsetH.
21 *
22 */
23
24SubsetTranslateBench::SubsetTranslateBench(const SkString& path,
25 SkColorType colorType,
26 uint32_t subsetWidth,
27 uint32_t subsetHeight,
28 bool useCodec)
29 : fColorType(colorType)
30 , fSubsetWidth(subsetWidth)
31 , fSubsetHeight(subsetHeight)
32 , fUseCodec(useCodec)
33{
34 // Parse the filename
35 SkString baseName = SkOSPath::Basename(path.c_str());
36
37 // Choose an informative color name
msarett7f691442015-09-22 11:56:16 -070038 const char* colorName = color_type_to_str(fColorType);
msarettb23e6aa2015-06-09 13:56:10 -070039
40 fName.printf("%sSubsetTranslate_%dx%d_%s_%s", fUseCodec ? "Codec" : "Image", fSubsetWidth,
41 fSubsetHeight, baseName.c_str(), colorName);
42
43 // Perform the decode setup
44 SkAutoTUnref<SkData> encoded(SkData::NewFromFileName(path.c_str()));
45 fStream.reset(new SkMemoryStream(encoded));
46}
47
48const char* SubsetTranslateBench::onGetName() {
49 return fName.c_str();
50}
51
52bool SubsetTranslateBench::isSuitableFor(Backend backend) {
53 return kNonRendering_Backend == backend;
54}
55
mtkleina1ebeb22015-10-01 09:43:39 -070056void SubsetTranslateBench::onDraw(int n, SkCanvas* canvas) {
msarettb23e6aa2015-06-09 13:56:10 -070057 // When the color type is kIndex8, we will need to store the color table. If it is
58 // used, it will be initialized by the codec.
59 int colorCount;
60 SkPMColor colors[256];
61 if (fUseCodec) {
62 for (int count = 0; count < n; count++) {
scroggo46c57472015-09-30 08:57:13 -070063 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(fStream->duplicate()));
64 const SkImageInfo info = codec->getInfo().makeColorType(fColorType);
halcanary385fe4d2015-08-26 13:07:48 -070065 SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]);
scroggo46c57472015-09-30 08:57:13 -070066 codec->startScanlineDecode(info, nullptr, colors, &colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070067
68 SkBitmap bitmap;
69 // Note that we use the same bitmap for all of the subsets.
70 // It might be larger than necessary for the end subsets.
msarett7f6283b2015-06-30 13:29:37 -070071 SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
72 alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070073
74 for (int x = 0; x < info.width(); x += fSubsetWidth) {
75 for (int y = 0; y < info.height(); y += fSubsetHeight) {
scroggo46c57472015-09-30 08:57:13 -070076 codec->skipScanlines(y);
msarettb23e6aa2015-06-09 13:56:10 -070077 const uint32_t currSubsetWidth =
78 x + (int) fSubsetWidth > info.width() ?
79 info.width() - x : fSubsetWidth;
80 const uint32_t currSubsetHeight =
81 y + (int) fSubsetHeight > info.height() ?
82 info.height() - y : fSubsetHeight;
83 const uint32_t bpp = info.bytesPerPixel();
84 for (uint32_t y = 0; y < currSubsetHeight; y++) {
scroggo46c57472015-09-30 08:57:13 -070085 codec->getScanlines(row.get(), 1, 0);
msarettb23e6aa2015-06-09 13:56:10 -070086 memcpy(bitmap.getAddr(0, y), row.get() + x * bpp,
87 currSubsetWidth * bpp);
88 }
89 }
90 }
91 }
92 } else {
93 // We create a color table here to satisfy allocPixels() when the output
94 // type is kIndex8. It's okay that this is uninitialized since we never
95 // use it.
halcanary385fe4d2015-08-26 13:07:48 -070096 SkColorTable* colorTable = new SkColorTable(colors, 0);
msarettb23e6aa2015-06-09 13:56:10 -070097 for (int count = 0; count < n; count++) {
98 int width, height;
99 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
100 decoder->buildTileIndex(fStream->duplicate(), &width, &height);
101 SkBitmap bitmap;
102 // Note that we use the same bitmap for all of the subsets.
103 // It might be larger than necessary for the end subsets.
104 // If we do not include this step, decodeSubset() would allocate space
105 // for the pixels automatically, but this would not allow us to reuse the
106 // same bitmap as the other subsets. We want to reuse the same bitmap
107 // because it gives a more fair comparison with SkCodec and is a common
108 // use case of BitmapRegionDecoder.
109 bitmap.allocPixels(SkImageInfo::Make(fSubsetWidth, fSubsetHeight,
halcanary96fcdcc2015-08-27 07:41:13 -0700110 fColorType, kOpaque_SkAlphaType), nullptr, colorTable);
msarettb23e6aa2015-06-09 13:56:10 -0700111
112 for (int x = 0; x < width; x += fSubsetWidth) {
113 for (int y = 0; y < height; y += fSubsetHeight) {
114 const uint32_t currSubsetWidth = x + (int) fSubsetWidth > width ?
115 width - x : fSubsetWidth;
116 const uint32_t currSubsetHeight = y + (int) fSubsetHeight > height ?
117 height - y : fSubsetHeight;
118 SkIRect rect = SkIRect::MakeXYWH(x, y, currSubsetWidth,
119 currSubsetHeight);
120 decoder->decodeSubset(&bitmap, rect, fColorType);
121 }
122 }
123 }
124 }
125}