blob: 8f6f2ea21b5d8665aa6ca53209e634920e5cdba4 [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
8#include "SubsetTranslateBench.h"
9#include "SubsetBenchPriv.h"
10#include "SkData.h"
11#include "SkCodec.h"
12#include "SkImageDecoder.h"
13#include "SkOSFile.h"
scroggoeb602a52015-07-09 08:16:03 -070014#include "SkScanlineDecoder.h"
msarettb23e6aa2015-06-09 13:56:10 -070015#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
38 const char* colorName = get_color_name(fColorType);
39
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
56void SubsetTranslateBench::onDraw(const int n, SkCanvas* canvas) {
57 // 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++) {
scroggo1c005e42015-08-04 09:24:45 -070063 SkAutoTDelete<SkScanlineDecoder> scanlineDecoder(
64 SkScanlineDecoder::NewFromStream(fStream->duplicate()));
65 const SkImageInfo info = scanlineDecoder->getInfo().makeColorType(fColorType);
halcanary385fe4d2015-08-26 13:07:48 -070066 SkAutoTDeleteArray<uint8_t> row(new uint8_t[info.minRowBytes()]);
halcanary96fcdcc2015-08-27 07:41:13 -070067 scanlineDecoder->start(info, nullptr, colors, &colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070068
69 SkBitmap bitmap;
70 // Note that we use the same bitmap for all of the subsets.
71 // It might be larger than necessary for the end subsets.
msarett7f6283b2015-06-30 13:29:37 -070072 SkImageInfo subsetInfo = info.makeWH(fSubsetWidth, fSubsetHeight);
73 alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070074
75 for (int x = 0; x < info.width(); x += fSubsetWidth) {
76 for (int y = 0; y < info.height(); y += fSubsetHeight) {
77 scanlineDecoder->skipScanlines(y);
78 const uint32_t currSubsetWidth =
79 x + (int) fSubsetWidth > info.width() ?
80 info.width() - x : fSubsetWidth;
81 const uint32_t currSubsetHeight =
82 y + (int) fSubsetHeight > info.height() ?
83 info.height() - y : fSubsetHeight;
84 const uint32_t bpp = info.bytesPerPixel();
85 for (uint32_t y = 0; y < currSubsetHeight; y++) {
86 scanlineDecoder->getScanlines(row.get(), 1, 0);
87 memcpy(bitmap.getAddr(0, y), row.get() + x * bpp,
88 currSubsetWidth * bpp);
89 }
90 }
91 }
92 }
93 } else {
94 // We create a color table here to satisfy allocPixels() when the output
95 // type is kIndex8. It's okay that this is uninitialized since we never
96 // use it.
halcanary385fe4d2015-08-26 13:07:48 -070097 SkColorTable* colorTable = new SkColorTable(colors, 0);
msarettb23e6aa2015-06-09 13:56:10 -070098 for (int count = 0; count < n; count++) {
99 int width, height;
100 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
101 decoder->buildTileIndex(fStream->duplicate(), &width, &height);
102 SkBitmap bitmap;
103 // Note that we use the same bitmap for all of the subsets.
104 // It might be larger than necessary for the end subsets.
105 // If we do not include this step, decodeSubset() would allocate space
106 // for the pixels automatically, but this would not allow us to reuse the
107 // same bitmap as the other subsets. We want to reuse the same bitmap
108 // because it gives a more fair comparison with SkCodec and is a common
109 // use case of BitmapRegionDecoder.
110 bitmap.allocPixels(SkImageInfo::Make(fSubsetWidth, fSubsetHeight,
halcanary96fcdcc2015-08-27 07:41:13 -0700111 fColorType, kOpaque_SkAlphaType), nullptr, colorTable);
msarettb23e6aa2015-06-09 13:56:10 -0700112
113 for (int x = 0; x < width; x += fSubsetWidth) {
114 for (int y = 0; y < height; y += fSubsetHeight) {
115 const uint32_t currSubsetWidth = x + (int) fSubsetWidth > width ?
116 width - x : fSubsetWidth;
117 const uint32_t currSubsetHeight = y + (int) fSubsetHeight > height ?
118 height - y : fSubsetHeight;
119 SkIRect rect = SkIRect::MakeXYWH(x, y, currSubsetWidth,
120 currSubsetHeight);
121 decoder->decodeSubset(&bitmap, rect, fColorType);
122 }
123 }
124 }
125 }
126}