blob: d4068ca88e069e575c04185dc2648cb8eb58262d [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
tfarinabcbc1782014-06-18 14:32:48 -07009#include "Resources.h"
krajcevski9a3cdbb2014-06-05 07:03:39 -070010#include "SkCanvas.h"
11#include "SkData.h"
reed5965c8a2015-01-07 18:04:45 -080012#include "SkImageGenerator.h"
krajcevski9a3cdbb2014-06-05 07:03:39 -070013#include "SkImageDecoder.h"
14#include "SkOSFile.h"
15#include "SkPixelRef.h"
16
17#ifndef SK_IGNORE_ETC1_SUPPORT
18
19#include "etc1.h"
20
21// This takes the etc1 data pointed to by orig, and copies it `factor` times in each
halcanary96fcdcc2015-08-27 07:41:13 -070022// dimension. The return value is the new data or nullptr on error.
krajcevski9a3cdbb2014-06-05 07:03:39 -070023static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) {
bsalomon49f085d2014-09-05 13:34:00 -070024 SkASSERT(orig);
krajcevski9a3cdbb2014-06-05 07:03:39 -070025 SkASSERT(factor > 1);
26
27 const etc1_byte* origData = reinterpret_cast<const etc1_byte*>(orig);
28 if (!etc1_pkm_is_valid(orig)) {
halcanary96fcdcc2015-08-27 07:41:13 -070029 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -070030 }
31
32 etc1_uint32 origWidth = etc1_pkm_get_width(origData);
33 etc1_uint32 origHeight = etc1_pkm_get_height(origData);
34
35 // The width and height must be aligned along block boundaries
36 static const etc1_uint32 kETC1BlockWidth = 4;
37 static const etc1_uint32 kETC1BlockHeight = 4;
38 if ((origWidth % kETC1BlockWidth) != 0 ||
39 (origHeight % kETC1BlockHeight) != 0) {
halcanary96fcdcc2015-08-27 07:41:13 -070040 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -070041 }
42
43 // The picture must be at least as large as a block.
44 if (origWidth <= kETC1BlockWidth || origHeight <= kETC1BlockHeight) {
halcanary96fcdcc2015-08-27 07:41:13 -070045 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -070046 }
47
48 etc1_uint32 newWidth = origWidth * factor;
49 etc1_uint32 newHeight = origHeight * factor;
50
51 etc1_uint32 newDataSz = etc1_get_encoded_data_size(newWidth, newHeight);
52 etc1_byte* newData = reinterpret_cast<etc1_byte *>(
53 sk_malloc_throw(newDataSz + ETC_PKM_HEADER_SIZE));
54 etc1_pkm_format_header(newData, newWidth, newHeight);
55
56 etc1_byte* copyInto = newData;
57
58 copyInto += ETC_PKM_HEADER_SIZE;
59 origData += ETC_PKM_HEADER_SIZE;
60
61 etc1_uint32 origBlocksX = (origWidth >> 2);
62 etc1_uint32 origBlocksY = (origHeight >> 2);
63 etc1_uint32 newBlocksY = (newHeight >> 2);
64 etc1_uint32 origRowSzInBytes = origBlocksX * ETC1_ENCODED_BLOCK_SIZE;
65
66 for (etc1_uint32 j = 0; j < newBlocksY; ++j) {
67 const etc1_byte* rowStart = origData + ((j % origBlocksY) * origRowSzInBytes);
68 for(etc1_uint32 i = 0; i < newWidth; i += origWidth) {
69 memcpy(copyInto, rowStart, origRowSzInBytes);
70 copyInto += origRowSzInBytes;
71 }
72 }
73 return newData;
74}
75
76// This is the base class for all of the benches in this file. In general
77// the ETC1 benches should all be working on the same data. Due to the
78// simplicity of the PKM file, that data is the 128x128 mandrill etc1
79// compressed texture repeated by some factor (currently 8 -> 1024x1024)
tfarinaf168b862014-06-19 12:32:29 -070080class ETCBitmapBenchBase : public Benchmark {
krajcevski9a3cdbb2014-06-05 07:03:39 -070081public:
82 ETCBitmapBenchBase() : fPKMData(loadPKM()) {
halcanary96fcdcc2015-08-27 07:41:13 -070083 if (nullptr == fPKMData) {
krajcevski9a3cdbb2014-06-05 07:03:39 -070084 SkDebugf("Could not load PKM data!");
85 }
86 }
87
88protected:
89 SkAutoDataUnref fPKMData;
90
91private:
tfarinac846f4a2014-07-01 12:35:49 -070092 SkData* loadPKM() {
93 SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
krajcevski9a3cdbb2014-06-05 07:03:39 -070094 // Expand the data
tfarinac846f4a2014-07-01 12:35:49 -070095 SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -070096 if (nullptr == fileData) {
krajcevski9a3cdbb2014-06-05 07:03:39 -070097 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
halcanary96fcdcc2015-08-27 07:41:13 -070098 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -070099 }
100
101 const etc1_uint32 kExpansionFactor = 8;
102 etc1_byte* expandedETC1 =
103 create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor);
halcanary96fcdcc2015-08-27 07:41:13 -0700104 if (nullptr == expandedETC1) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700105 SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor);
halcanary96fcdcc2015-08-27 07:41:13 -0700106 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700107 }
108
109 etc1_uint32 width = etc1_pkm_get_width(expandedETC1);
110 etc1_uint32 height = etc1_pkm_get_width(expandedETC1);
111 etc1_uint32 dataSz = ETC_PKM_HEADER_SIZE + etc1_get_encoded_data_size(width, height);
112 return SkData::NewFromMalloc(expandedETC1, dataSz);
113 }
114
tfarinaf168b862014-06-19 12:32:29 -0700115 typedef Benchmark INHERITED;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700116};
117
118// This is the rendering benchmark. Prior to rendering the data, create a
119// bitmap using the etc1 data.
120class ETCBitmapBench : public ETCBitmapBenchBase {
121public:
122 ETCBitmapBench(bool decompress, Backend backend)
123 : fDecompress(decompress), fBackend(backend) { }
124
mtklein36352bf2015-03-25 18:17:31 -0700125 bool isSuitableFor(Backend backend) override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700126 return backend == this->fBackend;
127 }
128
129protected:
mtklein36352bf2015-03-25 18:17:31 -0700130 const char* onGetName() override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700131 if (kGPU_Backend == this->fBackend) {
132 if (this->fDecompress) {
133 return "etc1bitmap_render_gpu_decompressed";
134 } else {
135 return "etc1bitmap_render_gpu_compressed";
136 }
137 } else {
138 SkASSERT(kRaster_Backend == this->fBackend);
139 if (this->fDecompress) {
140 return "etc1bitmap_render_raster_decompressed";
141 } else {
142 return "etc1bitmap_render_raster_compressed";
143 }
144 }
145 }
146
joshualitt8a6697a2015-09-30 12:11:07 -0700147 void onDelayedSetup() override {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 if (nullptr == fPKMData) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700149 SkDebugf("Failed to load PKM data!\n");
150 return;
151 }
152
reed74bd9532015-09-14 08:52:12 -0700153 if (fDecompress) {
154 SkAutoTDelete<SkImageGenerator> gen(SkImageGenerator::NewFromEncoded(fPKMData));
155 gen->generateBitmap(&fBitmap);
156 } else {
157 fImage.reset(SkImage::NewFromEncoded(fPKMData));
krajcevski9a3cdbb2014-06-05 07:03:39 -0700158 }
159 }
160
mtkleina1ebeb22015-10-01 09:43:39 -0700161 void onDraw(int loops, SkCanvas* canvas) override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700162 for (int i = 0; i < loops; ++i) {
reed74bd9532015-09-14 08:52:12 -0700163 if (fDecompress) {
164 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
165 } else {
166 canvas->drawImage(fImage, 0, 0, nullptr);
167 }
krajcevski9a3cdbb2014-06-05 07:03:39 -0700168 }
169 }
170
171protected:
172 SkBitmap fBitmap;
reed74bd9532015-09-14 08:52:12 -0700173 SkAutoTUnref<SkImage> fImage;
174
krajcevski9a3cdbb2014-06-05 07:03:39 -0700175 bool decompress() const { return fDecompress; }
176 Backend backend() const { return fBackend; }
177private:
178 const bool fDecompress;
179 const Backend fBackend;
180 typedef ETCBitmapBenchBase INHERITED;
181};
182
183// This benchmark is identical to the previous benchmark, but it explicitly forces
184// an upload to the GPU before each draw call. We do this by notifying the bitmap
185// that the pixels have changed (even though they haven't).
186class ETCBitmapUploadBench : public ETCBitmapBench {
187public:
188 ETCBitmapUploadBench(bool decompress, Backend backend)
189 : ETCBitmapBench(decompress, backend) { }
190
191protected:
mtklein36352bf2015-03-25 18:17:31 -0700192 const char* onGetName() override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700193 if (kGPU_Backend == this->backend()) {
194 if (this->decompress()) {
195 return "etc1bitmap_upload_gpu_decompressed";
196 } else {
197 return "etc1bitmap_upload_gpu_compressed";
198 }
199 } else {
200 SkASSERT(kRaster_Backend == this->backend());
201 if (this->decompress()) {
202 return "etc1bitmap_upload_raster_decompressed";
203 } else {
204 return "etc1bitmap_upload_raster_compressed";
205 }
206 }
207 }
208
mtkleina1ebeb22015-10-01 09:43:39 -0700209 void onDraw(int loops, SkCanvas* canvas) override {
reedbbf3e892014-06-20 11:33:59 -0700210 SkPixelRef* pr = fBitmap.pixelRef();
krajcevski9a3cdbb2014-06-05 07:03:39 -0700211 for (int i = 0; i < loops; ++i) {
reedbbf3e892014-06-20 11:33:59 -0700212 if (pr) {
213 pr->notifyPixelsChanged();
214 }
halcanary96fcdcc2015-08-27 07:41:13 -0700215 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
krajcevski9a3cdbb2014-06-05 07:03:39 -0700216 }
217 }
218
219private:
220 typedef ETCBitmapBench INHERITED;
221};
222
tfarinaf168b862014-06-19 12:32:29 -0700223DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kRaster_Backend);)
224DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700225
tfarinaf168b862014-06-19 12:32:29 -0700226DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kGPU_Backend);)
227DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700228
tfarinaf168b862014-06-19 12:32:29 -0700229DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kRaster_Backend);)
230DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700231
tfarinaf168b862014-06-19 12:32:29 -0700232DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kGPU_Backend);)
233DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700234
235#endif // SK_IGNORE_ETC1_SUPPORT