blob: 305f49210850f338880ec859f4aa1ec68eda1204 [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
scroggo026388a2016-02-10 11:15:21 -080076// Defined in SkImageDecoder_ktx.cpp
77extern SkImageGenerator* decoder_image_generator(SkData*);
78
krajcevski9a3cdbb2014-06-05 07:03:39 -070079// This is the base class for all of the benches in this file. In general
80// the ETC1 benches should all be working on the same data. Due to the
81// simplicity of the PKM file, that data is the 128x128 mandrill etc1
82// compressed texture repeated by some factor (currently 8 -> 1024x1024)
tfarinaf168b862014-06-19 12:32:29 -070083class ETCBitmapBenchBase : public Benchmark {
krajcevski9a3cdbb2014-06-05 07:03:39 -070084public:
85 ETCBitmapBenchBase() : fPKMData(loadPKM()) {
halcanary96fcdcc2015-08-27 07:41:13 -070086 if (nullptr == fPKMData) {
cdalton20b4ca12015-12-22 10:50:56 -080087 SkDebugf("Could not load PKM data!\n");
krajcevski9a3cdbb2014-06-05 07:03:39 -070088 }
89 }
90
91protected:
92 SkAutoDataUnref fPKMData;
93
94private:
tfarinac846f4a2014-07-01 12:35:49 -070095 SkData* loadPKM() {
96 SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
krajcevski9a3cdbb2014-06-05 07:03:39 -070097 // Expand the data
tfarinac846f4a2014-07-01 12:35:49 -070098 SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -070099 if (nullptr == fileData) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700100 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700101 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700102 }
103
104 const etc1_uint32 kExpansionFactor = 8;
105 etc1_byte* expandedETC1 =
106 create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor);
halcanary96fcdcc2015-08-27 07:41:13 -0700107 if (nullptr == expandedETC1) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700108 SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor);
halcanary96fcdcc2015-08-27 07:41:13 -0700109 return nullptr;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700110 }
111
112 etc1_uint32 width = etc1_pkm_get_width(expandedETC1);
113 etc1_uint32 height = etc1_pkm_get_width(expandedETC1);
114 etc1_uint32 dataSz = ETC_PKM_HEADER_SIZE + etc1_get_encoded_data_size(width, height);
115 return SkData::NewFromMalloc(expandedETC1, dataSz);
116 }
117
tfarinaf168b862014-06-19 12:32:29 -0700118 typedef Benchmark INHERITED;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700119};
120
121// This is the rendering benchmark. Prior to rendering the data, create a
122// bitmap using the etc1 data.
123class ETCBitmapBench : public ETCBitmapBenchBase {
124public:
125 ETCBitmapBench(bool decompress, Backend backend)
126 : fDecompress(decompress), fBackend(backend) { }
127
mtklein36352bf2015-03-25 18:17:31 -0700128 bool isSuitableFor(Backend backend) override {
bsalomon2f8ac352015-10-19 08:29:16 -0700129 return SkToBool(fImage) && backend == this->fBackend;
krajcevski9a3cdbb2014-06-05 07:03:39 -0700130 }
131
132protected:
mtklein36352bf2015-03-25 18:17:31 -0700133 const char* onGetName() override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700134 if (kGPU_Backend == this->fBackend) {
135 if (this->fDecompress) {
136 return "etc1bitmap_render_gpu_decompressed";
137 } else {
138 return "etc1bitmap_render_gpu_compressed";
139 }
140 } else {
141 SkASSERT(kRaster_Backend == this->fBackend);
142 if (this->fDecompress) {
143 return "etc1bitmap_render_raster_decompressed";
144 } else {
145 return "etc1bitmap_render_raster_compressed";
146 }
147 }
148 }
149
joshualitt8a6697a2015-09-30 12:11:07 -0700150 void onDelayedSetup() override {
halcanary96fcdcc2015-08-27 07:41:13 -0700151 if (nullptr == fPKMData) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700152 SkDebugf("Failed to load PKM data!\n");
153 return;
154 }
155
reed74bd9532015-09-14 08:52:12 -0700156 if (fDecompress) {
scroggo026388a2016-02-10 11:15:21 -0800157 SkAutoTDelete<SkImageGenerator> gen(decoder_image_generator(fPKMData));
reed74bd9532015-09-14 08:52:12 -0700158 gen->generateBitmap(&fBitmap);
159 } else {
scroggo026388a2016-02-10 11:15:21 -0800160 fImage.reset(SkImage::NewFromGenerator(decoder_image_generator(fPKMData)));
krajcevski9a3cdbb2014-06-05 07:03:39 -0700161 }
162 }
163
mtkleina1ebeb22015-10-01 09:43:39 -0700164 void onDraw(int loops, SkCanvas* canvas) override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700165 for (int i = 0; i < loops; ++i) {
reed74bd9532015-09-14 08:52:12 -0700166 if (fDecompress) {
167 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
168 } else {
169 canvas->drawImage(fImage, 0, 0, nullptr);
170 }
krajcevski9a3cdbb2014-06-05 07:03:39 -0700171 }
172 }
173
174protected:
175 SkBitmap fBitmap;
reed74bd9532015-09-14 08:52:12 -0700176 SkAutoTUnref<SkImage> fImage;
177
krajcevski9a3cdbb2014-06-05 07:03:39 -0700178 bool decompress() const { return fDecompress; }
179 Backend backend() const { return fBackend; }
180private:
181 const bool fDecompress;
182 const Backend fBackend;
183 typedef ETCBitmapBenchBase INHERITED;
184};
185
186// This benchmark is identical to the previous benchmark, but it explicitly forces
187// an upload to the GPU before each draw call. We do this by notifying the bitmap
188// that the pixels have changed (even though they haven't).
189class ETCBitmapUploadBench : public ETCBitmapBench {
190public:
191 ETCBitmapUploadBench(bool decompress, Backend backend)
192 : ETCBitmapBench(decompress, backend) { }
193
194protected:
mtklein36352bf2015-03-25 18:17:31 -0700195 const char* onGetName() override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700196 if (kGPU_Backend == this->backend()) {
197 if (this->decompress()) {
198 return "etc1bitmap_upload_gpu_decompressed";
199 } else {
200 return "etc1bitmap_upload_gpu_compressed";
201 }
202 } else {
203 SkASSERT(kRaster_Backend == this->backend());
204 if (this->decompress()) {
205 return "etc1bitmap_upload_raster_decompressed";
206 } else {
207 return "etc1bitmap_upload_raster_compressed";
208 }
209 }
210 }
211
mtkleina1ebeb22015-10-01 09:43:39 -0700212 void onDraw(int loops, SkCanvas* canvas) override {
reedbbf3e892014-06-20 11:33:59 -0700213 SkPixelRef* pr = fBitmap.pixelRef();
krajcevski9a3cdbb2014-06-05 07:03:39 -0700214 for (int i = 0; i < loops; ++i) {
reedbbf3e892014-06-20 11:33:59 -0700215 if (pr) {
216 pr->notifyPixelsChanged();
217 }
halcanary96fcdcc2015-08-27 07:41:13 -0700218 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
krajcevski9a3cdbb2014-06-05 07:03:39 -0700219 }
220 }
221
222private:
223 typedef ETCBitmapBench INHERITED;
224};
225
tfarinaf168b862014-06-19 12:32:29 -0700226DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kRaster_Backend);)
227DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700228
tfarinaf168b862014-06-19 12:32:29 -0700229DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kGPU_Backend);)
230DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700231
tfarinaf168b862014-06-19 12:32:29 -0700232DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kRaster_Backend);)
233DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700234
tfarinaf168b862014-06-19 12:32:29 -0700235DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kGPU_Backend);)
236DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700237
238#endif // SK_IGNORE_ETC1_SUPPORT