krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkData.h" |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 12 | #include "SkImageGenerator.h" |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 13 | #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 |
| 22 | // dimension. The return value is the new data or NULL on error. |
| 23 | static etc1_byte* create_expanded_etc1_bitmap(const uint8_t* orig, int factor) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 24 | SkASSERT(orig); |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 25 | SkASSERT(factor > 1); |
| 26 | |
| 27 | const etc1_byte* origData = reinterpret_cast<const etc1_byte*>(orig); |
| 28 | if (!etc1_pkm_is_valid(orig)) { |
| 29 | return NULL; |
| 30 | } |
| 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) { |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | // The picture must be at least as large as a block. |
| 44 | if (origWidth <= kETC1BlockWidth || origHeight <= kETC1BlockHeight) { |
| 45 | return NULL; |
| 46 | } |
| 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) |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 80 | class ETCBitmapBenchBase : public Benchmark { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 81 | public: |
| 82 | ETCBitmapBenchBase() : fPKMData(loadPKM()) { |
| 83 | if (NULL == fPKMData) { |
| 84 | SkDebugf("Could not load PKM data!"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | protected: |
| 89 | SkAutoDataUnref fPKMData; |
| 90 | |
| 91 | private: |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 92 | SkData* loadPKM() { |
| 93 | SkString pkmFilename = GetResourcePath("mandrill_128.pkm"); |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 94 | // Expand the data |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 95 | SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str())); |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 96 | if (NULL == fileData) { |
| 97 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | const etc1_uint32 kExpansionFactor = 8; |
| 102 | etc1_byte* expandedETC1 = |
| 103 | create_expanded_etc1_bitmap(fileData->bytes(), kExpansionFactor); |
| 104 | if (NULL == expandedETC1) { |
| 105 | SkDebugf("Error expanding ETC1 data by factor of %d\n", kExpansionFactor); |
| 106 | return NULL; |
| 107 | } |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 115 | typedef Benchmark INHERITED; |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | // This is the rendering benchmark. Prior to rendering the data, create a |
| 119 | // bitmap using the etc1 data. |
| 120 | class ETCBitmapBench : public ETCBitmapBenchBase { |
| 121 | public: |
| 122 | ETCBitmapBench(bool decompress, Backend backend) |
| 123 | : fDecompress(decompress), fBackend(backend) { } |
| 124 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 125 | bool isSuitableFor(Backend backend) override { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 126 | return backend == this->fBackend; |
| 127 | } |
| 128 | |
| 129 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | const char* onGetName() override { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 131 | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | void onPreDraw() override { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 148 | if (NULL == fPKMData) { |
| 149 | SkDebugf("Failed to load PKM data!\n"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // Install pixel ref |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 154 | if (!SkInstallDiscardablePixelRef(fPKMData, &(this->fBitmap))) { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 155 | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 165 | void onDraw(const int loops, SkCanvas* canvas) override { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 166 | for (int i = 0; i < loops; ++i) { |
| 167 | canvas->drawBitmap(this->fBitmap, 0, 0, NULL); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | protected: |
| 172 | SkBitmap fBitmap; |
| 173 | bool decompress() const { return fDecompress; } |
| 174 | Backend backend() const { return fBackend; } |
| 175 | private: |
| 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). |
| 184 | class ETCBitmapUploadBench : public ETCBitmapBench { |
| 185 | public: |
| 186 | ETCBitmapUploadBench(bool decompress, Backend backend) |
| 187 | : ETCBitmapBench(decompress, backend) { } |
| 188 | |
| 189 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 190 | const char* onGetName() override { |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 191 | 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 207 | void onDraw(const int loops, SkCanvas* canvas) override { |
reed | bbf3e89 | 2014-06-20 11:33:59 -0700 | [diff] [blame] | 208 | SkPixelRef* pr = fBitmap.pixelRef(); |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 209 | for (int i = 0; i < loops; ++i) { |
reed | bbf3e89 | 2014-06-20 11:33:59 -0700 | [diff] [blame] | 210 | if (pr) { |
| 211 | pr->notifyPixelsChanged(); |
| 212 | } |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 213 | canvas->drawBitmap(this->fBitmap, 0, 0, NULL); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private: |
| 218 | typedef ETCBitmapBench INHERITED; |
| 219 | }; |
| 220 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 221 | DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kRaster_Backend);) |
| 222 | DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kRaster_Backend);) |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 223 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 224 | DEF_BENCH(return new ETCBitmapBench(false, Benchmark::kGPU_Backend);) |
| 225 | DEF_BENCH(return new ETCBitmapBench(true, Benchmark::kGPU_Backend);) |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 226 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 227 | DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kRaster_Backend);) |
| 228 | DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kRaster_Backend);) |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 229 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 230 | DEF_BENCH(return new ETCBitmapUploadBench(false, Benchmark::kGPU_Backend);) |
| 231 | DEF_BENCH(return new ETCBitmapUploadBench(true, Benchmark::kGPU_Backend);) |
krajcevski | 9a3cdbb | 2014-06-05 07:03:39 -0700 | [diff] [blame] | 232 | |
| 233 | #endif // SK_IGNORE_ETC1_SUPPORT |