blob: 4b9cd6ea939dd783ef1c6ca2df2d378825a94d5f [file] [log] [blame]
krajcevski9a3cdbb2014-06-05 07:03:39 -07001/*
2 * Copyright 2014 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 "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkData.h"
11#include "SkDecodingImageGenerator.h"
12#include "SkImageDecoder.h"
13#include "SkOSFile.h"
14#include "SkPixelRef.h"
15
16#ifndef SK_IGNORE_ETC1_SUPPORT
17
18#include "etc1.h"
19
20// This takes the etc1 data pointed to by orig, and copies it `factor` times in each
21// dimension. The return value is the new data or NULL on error.
22static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) {
23 SkASSERT(NULL != orig);
24 SkASSERT(factor > 1);
25
26 const etc1_byte* origData = reinterpret_cast<const etc1_byte*>(orig);
27 if (!etc1_pkm_is_valid(orig)) {
28 return NULL;
29 }
30
31 etc1_uint32 origWidth = etc1_pkm_get_width(origData);
32 etc1_uint32 origHeight = etc1_pkm_get_height(origData);
33
34 // The width and height must be aligned along block boundaries
35 static const etc1_uint32 kETC1BlockWidth = 4;
36 static const etc1_uint32 kETC1BlockHeight = 4;
37 if ((origWidth % kETC1BlockWidth) != 0 ||
38 (origHeight % kETC1BlockHeight) != 0) {
39 return NULL;
40 }
41
42 // The picture must be at least as large as a block.
43 if (origWidth <= kETC1BlockWidth || origHeight <= kETC1BlockHeight) {
44 return NULL;
45 }
46
47 etc1_uint32 newWidth = origWidth * factor;
48 etc1_uint32 newHeight = origHeight * factor;
49
50 etc1_uint32 newDataSz = etc1_get_encoded_data_size(newWidth, newHeight);
51 etc1_byte* newData = reinterpret_cast<etc1_byte *>(
52 sk_malloc_throw(newDataSz + ETC_PKM_HEADER_SIZE));
53 etc1_pkm_format_header(newData, newWidth, newHeight);
54
55 etc1_byte* copyInto = newData;
56
57 copyInto += ETC_PKM_HEADER_SIZE;
58 origData += ETC_PKM_HEADER_SIZE;
59
60 etc1_uint32 origBlocksX = (origWidth >> 2);
61 etc1_uint32 origBlocksY = (origHeight >> 2);
62 etc1_uint32 newBlocksY = (newHeight >> 2);
63 etc1_uint32 origRowSzInBytes = origBlocksX * ETC1_ENCODED_BLOCK_SIZE;
64
65 for (etc1_uint32 j = 0; j < newBlocksY; ++j) {
66 const etc1_byte* rowStart = origData + ((j % origBlocksY) * origRowSzInBytes);
67 for(etc1_uint32 i = 0; i < newWidth; i += origWidth) {
68 memcpy(copyInto, rowStart, origRowSzInBytes);
69 copyInto += origRowSzInBytes;
70 }
71 }
72 return newData;
73}
74
75// This is the base class for all of the benches in this file. In general
76// the ETC1 benches should all be working on the same data. Due to the
77// simplicity of the PKM file, that data is the 128x128 mandrill etc1
78// compressed texture repeated by some factor (currently 8 -> 1024x1024)
79class ETCBitmapBenchBase : public SkBenchmark {
80public:
81 ETCBitmapBenchBase() : fPKMData(loadPKM()) {
82 if (NULL == fPKMData) {
83 SkDebugf("Could not load PKM data!");
84 }
85 }
86
87protected:
88 SkAutoDataUnref fPKMData;
89
90private:
91 SkData *loadPKM() {
92 SkString filename = SkOSPath::SkPathJoin(
93 INHERITED::GetResourcePath().c_str(), "mandrill_128.pkm");
94
95 // Expand the data
96 SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str()));
97 if (NULL == fileData) {
98 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
99 return NULL;
100 }
101
102 const etc1_uint32 kExpansionFactor = 8;
103 etc1_byte* expandedETC1 =
104 create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor);
105 if (NULL == expandedETC1) {
106 SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor);
107 return NULL;
108 }
109
110 etc1_uint32 width = etc1_pkm_get_width(expandedETC1);
111 etc1_uint32 height = etc1_pkm_get_width(expandedETC1);
112 etc1_uint32 dataSz = ETC_PKM_HEADER_SIZE + etc1_get_encoded_data_size(width, height);
113 return SkData::NewFromMalloc(expandedETC1, dataSz);
114 }
115
116 typedef SkBenchmark INHERITED;
117};
118
119// This is the rendering benchmark. Prior to rendering the data, create a
120// bitmap using the etc1 data.
121class ETCBitmapBench : public ETCBitmapBenchBase {
122public:
123 ETCBitmapBench(bool decompress, Backend backend)
124 : fDecompress(decompress), fBackend(backend) { }
125
126 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
127 return backend == this->fBackend;
128 }
129
130protected:
131 virtual const char* onGetName() SK_OVERRIDE {
132 if (kGPU_Backend == this->fBackend) {
133 if (this->fDecompress) {
134 return "etc1bitmap_render_gpu_decompressed";
135 } else {
136 return "etc1bitmap_render_gpu_compressed";
137 }
138 } else {
139 SkASSERT(kRaster_Backend == this->fBackend);
140 if (this->fDecompress) {
141 return "etc1bitmap_render_raster_decompressed";
142 } else {
143 return "etc1bitmap_render_raster_compressed";
144 }
145 }
146 }
147
148 virtual void onPreDraw() SK_OVERRIDE {
149 if (NULL == fPKMData) {
150 SkDebugf("Failed to load PKM data!\n");
151 return;
152 }
153
154 // Install pixel ref
155 if (!SkInstallDiscardablePixelRef(
156 SkDecodingImageGenerator::Create(
157 fPKMData, SkDecodingImageGenerator::Options()), &(this->fBitmap))) {
158 SkDebugf("Could not install discardable pixel ref.\n");
159 return;
160 }
161
162 // Decompress it if necessary
163 if (this->fDecompress) {
164 this->fBitmap.lockPixels();
165 }
166 }
167
168 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
169 for (int i = 0; i < loops; ++i) {
170 canvas->drawBitmap(this->fBitmap, 0, 0, NULL);
171 }
172 }
173
174protected:
175 SkBitmap fBitmap;
176 bool decompress() const { return fDecompress; }
177 Backend backend() const { return fBackend; }
178private:
179 const bool fDecompress;
180 const Backend fBackend;
181 typedef ETCBitmapBenchBase INHERITED;
182};
183
184// This benchmark is identical to the previous benchmark, but it explicitly forces
185// an upload to the GPU before each draw call. We do this by notifying the bitmap
186// that the pixels have changed (even though they haven't).
187class ETCBitmapUploadBench : public ETCBitmapBench {
188public:
189 ETCBitmapUploadBench(bool decompress, Backend backend)
190 : ETCBitmapBench(decompress, backend) { }
191
192protected:
193 virtual const char* onGetName() SK_OVERRIDE {
194 if (kGPU_Backend == this->backend()) {
195 if (this->decompress()) {
196 return "etc1bitmap_upload_gpu_decompressed";
197 } else {
198 return "etc1bitmap_upload_gpu_compressed";
199 }
200 } else {
201 SkASSERT(kRaster_Backend == this->backend());
202 if (this->decompress()) {
203 return "etc1bitmap_upload_raster_decompressed";
204 } else {
205 return "etc1bitmap_upload_raster_compressed";
206 }
207 }
208 }
209
210 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
211 for (int i = 0; i < loops; ++i) {
212 this->fBitmap.pixelRef()->notifyPixelsChanged();
213 canvas->drawBitmap(this->fBitmap, 0, 0, NULL);
214 }
215 }
216
217private:
218 typedef ETCBitmapBench INHERITED;
219};
220
221DEF_BENCH(return new ETCBitmapBench(false, SkBenchmark::kRaster_Backend);)
222DEF_BENCH(return new ETCBitmapBench(true, SkBenchmark::kRaster_Backend);)
223
224DEF_BENCH(return new ETCBitmapBench(false, SkBenchmark::kGPU_Backend);)
225DEF_BENCH(return new ETCBitmapBench(true, SkBenchmark::kGPU_Backend);)
226
227DEF_BENCH(return new ETCBitmapUploadBench(false, SkBenchmark::kRaster_Backend);)
228DEF_BENCH(return new ETCBitmapUploadBench(true, SkBenchmark::kRaster_Backend);)
229
230DEF_BENCH(return new ETCBitmapUploadBench(false, SkBenchmark::kGPU_Backend);)
231DEF_BENCH(return new ETCBitmapUploadBench(true, SkBenchmark::kGPU_Backend);)
232
233#endif // SK_IGNORE_ETC1_SUPPORT