blob: 5d72bf4c681c826a4d8989059885986642083e0f [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
mtklein36352bf2015-03-25 18:17:31 -0700147 void onPreDraw() 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
153 // Install pixel ref
reed5965c8a2015-01-07 18:04:45 -0800154 if (!SkInstallDiscardablePixelRef(fPKMData, &(this->fBitmap))) {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700155 SkDebugf("Could not install discardable pixel ref.\n");
156 return;
157 }
158
159 // Decompress it if necessary
160 if (this->fDecompress) {
161 this->fBitmap.lockPixels();
162 }
163 }
164
mtklein36352bf2015-03-25 18:17:31 -0700165 void onDraw(const int loops, SkCanvas* canvas) override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700166 for (int i = 0; i < loops; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
krajcevski9a3cdbb2014-06-05 07:03:39 -0700168 }
169 }
170
171protected:
172 SkBitmap fBitmap;
173 bool decompress() const { return fDecompress; }
174 Backend backend() const { return fBackend; }
175private:
176 const bool fDecompress;
177 const Backend fBackend;
178 typedef ETCBitmapBenchBase INHERITED;
179};
180
181// This benchmark is identical to the previous benchmark, but it explicitly forces
182// an upload to the GPU before each draw call. We do this by notifying the bitmap
183// that the pixels have changed (even though they haven't).
184class ETCBitmapUploadBench : public ETCBitmapBench {
185public:
186 ETCBitmapUploadBench(bool decompress, Backend backend)
187 : ETCBitmapBench(decompress, backend) { }
188
189protected:
mtklein36352bf2015-03-25 18:17:31 -0700190 const char* onGetName() override {
krajcevski9a3cdbb2014-06-05 07:03:39 -0700191 if (kGPU_Backend == this->backend()) {
192 if (this->decompress()) {
193 return "etc1bitmap_upload_gpu_decompressed";
194 } else {
195 return "etc1bitmap_upload_gpu_compressed";
196 }
197 } else {
198 SkASSERT(kRaster_Backend == this->backend());
199 if (this->decompress()) {
200 return "etc1bitmap_upload_raster_decompressed";
201 } else {
202 return "etc1bitmap_upload_raster_compressed";
203 }
204 }
205 }
206
mtklein36352bf2015-03-25 18:17:31 -0700207 void onDraw(const int loops, SkCanvas* canvas) override {
reedbbf3e892014-06-20 11:33:59 -0700208 SkPixelRef* pr = fBitmap.pixelRef();
krajcevski9a3cdbb2014-06-05 07:03:39 -0700209 for (int i = 0; i < loops; ++i) {
reedbbf3e892014-06-20 11:33:59 -0700210 if (pr) {
211 pr->notifyPixelsChanged();
212 }
halcanary96fcdcc2015-08-27 07:41:13 -0700213 canvas->drawBitmap(this->fBitmap, 0, 0, nullptr);
krajcevski9a3cdbb2014-06-05 07:03:39 -0700214 }
215 }
216
217private:
218 typedef ETCBitmapBench INHERITED;
219};
220
tfarinaf168b862014-06-19 12:32:29 -0700221DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kRaster_Backend);)
222DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700223
tfarinaf168b862014-06-19 12:32:29 -0700224DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kGPU_Backend);)
225DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700226
tfarinaf168b862014-06-19 12:32:29 -0700227DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kRaster_Backend);)
228DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kRaster_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700229
tfarinaf168b862014-06-19 12:32:29 -0700230DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kGPU_Backend);)
231DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kGPU_Backend);)
krajcevski9a3cdbb2014-06-05 07:03:39 -0700232
233#endif // SK_IGNORE_ETC1_SUPPORT