blob: e9aee76a4aab7944f2af95a294643e3d6133097c [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() {
tfarina880914c2014-06-09 12:05:34 -070092 SkString resourcePath = GetResourcePath();
93 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(),
94 "mandrill_128.pkm");
krajcevski9a3cdbb2014-06-05 07:03:39 -070095
96 // Expand the data
97 SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str()));
98 if (NULL == fileData) {
99 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
100 return NULL;
101 }
102
103 const etc1_uint32 kExpansionFactor = 8;
104 etc1_byte* expandedETC1 =
105 create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor);
106 if (NULL == expandedETC1) {
107 SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor);
108 return NULL;
109 }
110
111 etc1_uint32 width = etc1_pkm_get_width(expandedETC1);
112 etc1_uint32 height = etc1_pkm_get_width(expandedETC1);
113 etc1_uint32 dataSz = ETC_PKM_HEADER_SIZE + etc1_get_encoded_data_size(width, height);
114 return SkData::NewFromMalloc(expandedETC1, dataSz);
115 }
116
117 typedef SkBenchmark INHERITED;
118};
119
120// This is the rendering benchmark. Prior to rendering the data, create a
121// bitmap using the etc1 data.
122class ETCBitmapBench : public ETCBitmapBenchBase {
123public:
124 ETCBitmapBench(bool decompress, Backend backend)
125 : fDecompress(decompress), fBackend(backend) { }
126
127 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
128 return backend == this->fBackend;
129 }
130
131protected:
132 virtual const char* onGetName() SK_OVERRIDE {
133 if (kGPU_Backend == this->fBackend) {
134 if (this->fDecompress) {
135 return "etc1bitmap_render_gpu_decompressed";
136 } else {
137 return "etc1bitmap_render_gpu_compressed";
138 }
139 } else {
140 SkASSERT(kRaster_Backend == this->fBackend);
141 if (this->fDecompress) {
142 return "etc1bitmap_render_raster_decompressed";
143 } else {
144 return "etc1bitmap_render_raster_compressed";
145 }
146 }
147 }
148
149 virtual void onPreDraw() SK_OVERRIDE {
150 if (NULL == fPKMData) {
151 SkDebugf("Failed to load PKM data!\n");
152 return;
153 }
154
155 // Install pixel ref
156 if (!SkInstallDiscardablePixelRef(
157 SkDecodingImageGenerator::Create(
158 fPKMData, SkDecodingImageGenerator::Options()), &(this->fBitmap))) {
159 SkDebugf("Could not install discardable pixel ref.\n");
160 return;
161 }
162
163 // Decompress it if necessary
164 if (this->fDecompress) {
165 this->fBitmap.lockPixels();
166 }
167 }
168
169 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
170 for (int i = 0; i < loops; ++i) {
171 canvas->drawBitmap(this->fBitmap, 0, 0, NULL);
172 }
173 }
174
175protected:
176 SkBitmap fBitmap;
177 bool decompress() const { return fDecompress; }
178 Backend backend() const { return fBackend; }
179private:
180 const bool fDecompress;
181 const Backend fBackend;
182 typedef ETCBitmapBenchBase INHERITED;
183};
184
185// This benchmark is identical to the previous benchmark, but it explicitly forces
186// an upload to the GPU before each draw call. We do this by notifying the bitmap
187// that the pixels have changed (even though they haven't).
188class ETCBitmapUploadBench : public ETCBitmapBench {
189public:
190 ETCBitmapUploadBench(bool decompress, Backend backend)
191 : ETCBitmapBench(decompress, backend) { }
192
193protected:
194 virtual const char* onGetName() SK_OVERRIDE {
195 if (kGPU_Backend == this->backend()) {
196 if (this->decompress()) {
197 return "etc1bitmap_upload_gpu_decompressed";
198 } else {
199 return "etc1bitmap_upload_gpu_compressed";
200 }
201 } else {
202 SkASSERT(kRaster_Backend == this->backend());
203 if (this->decompress()) {
204 return "etc1bitmap_upload_raster_decompressed";
205 } else {
206 return "etc1bitmap_upload_raster_compressed";
207 }
208 }
209 }
210
211 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
212 for (int i = 0; i < loops; ++i) {
213 this->fBitmap.pixelRef()->notifyPixelsChanged();
214 canvas->drawBitmap(this->fBitmap, 0, 0, NULL);
215 }
216 }
217
218private:
219 typedef ETCBitmapBench INHERITED;
220};
221
222DEF_BENCH(return new ETCBitmapBench(false, SkBenchmark::kRaster_Backend);)
223DEF_BENCH(return new ETCBitmapBench(true, SkBenchmark::kRaster_Backend);)
224
225DEF_BENCH(return new ETCBitmapBench(false, SkBenchmark::kGPU_Backend);)
226DEF_BENCH(return new ETCBitmapBench(true, SkBenchmark::kGPU_Backend);)
227
228DEF_BENCH(return new ETCBitmapUploadBench(false, SkBenchmark::kRaster_Backend);)
229DEF_BENCH(return new ETCBitmapUploadBench(true, SkBenchmark::kRaster_Backend);)
230
231DEF_BENCH(return new ETCBitmapUploadBench(false, SkBenchmark::kGPU_Backend);)
232DEF_BENCH(return new ETCBitmapUploadBench(true, SkBenchmark::kGPU_Backend);)
233
234#endif // SK_IGNORE_ETC1_SUPPORT