blob: ffd86703ec2271e40fdc27c1679fe914f4b958ac [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 "SubsetZoomBench.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 * Choose subsets to mimic a user zooming in or out on a photo.
21 *
22 */
23
24SubsetZoomBench::SubsetZoomBench(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("%sSubsetZoom_%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* SubsetZoomBench::onGetName() {
49 return fName.c_str();
50}
51
52bool SubsetZoomBench::isSuitableFor(Backend backend) {
53 return kNonRendering_Backend == backend;
54}
55
mtkleina1ebeb22015-10-01 09:43:39 -070056void SubsetZoomBench::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 const int centerX = info.width() / 2;
69 const int centerY = info.height() / 2;
70 int w = fSubsetWidth;
71 int h = fSubsetHeight;
72 do {
73 const int subsetStartX = SkTMax(0, centerX - w / 2);
74 const int subsetStartY = SkTMax(0, centerY - h / 2);
75 const int subsetWidth = SkTMin(w, info.width() - subsetStartX);
76 const int subsetHeight = SkTMin(h, info.height() - subsetStartY);
77 // Note that if we subsetted and scaled in a single step, we could use the
78 // same bitmap - as is often done in actual use cases.
79 SkBitmap bitmap;
msarett7f6283b2015-06-30 13:29:37 -070080 SkImageInfo subsetInfo = info.makeWH(subsetWidth, subsetHeight);
81 alloc_pixels(&bitmap, subsetInfo, colors, colorCount);
msarettb23e6aa2015-06-09 13:56:10 -070082
83 uint32_t bpp = info.bytesPerPixel();
scroggo46c57472015-09-30 08:57:13 -070084 codec->skipScanlines(subsetStartY);
msarettb23e6aa2015-06-09 13:56:10 -070085 for (int y = 0; y < subsetHeight; y++) {
scroggo46c57472015-09-30 08:57:13 -070086 codec->getScanlines(row.get(), 1, 0);
msarettb23e6aa2015-06-09 13:56:10 -070087 memcpy(bitmap.getAddr(0, y), row.get() + subsetStartX * bpp,
88 subsetWidth * bpp);
89 }
90 w <<= 1;
91 h <<= 1;
92 } while (w < 2 * info.width() || h < 2 * info.height());
93 }
94 } else {
95 for (int count = 0; count < n; count++) {
96 int width, height;
97 SkAutoTDelete<SkImageDecoder> decoder(SkImageDecoder::Factory(fStream));
98 decoder->buildTileIndex(fStream->duplicate(), &width, &height);
99
100 const int centerX = width / 2;
101 const int centerY = height / 2;
102 int w = fSubsetWidth;
103 int h = fSubsetHeight;
104 do {
105 const int subsetStartX = SkTMax(0, centerX - w / 2);
106 const int subsetStartY = SkTMax(0, centerY - h / 2);
107 const int subsetWidth = SkTMin(w, width - subsetStartX);
108 const int subsetHeight = SkTMin(h, height - subsetStartY);
109 SkBitmap bitmap;
110 SkIRect rect = SkIRect::MakeXYWH(subsetStartX, subsetStartY, subsetWidth,
111 subsetHeight);
112 decoder->decodeSubset(&bitmap, rect, fColorType);
113 w <<= 1;
114 h <<= 1;
115 } while (w < 2 * width || h < 2 * height);
116 }
117 }
118}