robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [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 | |
| 8 | #include "gm.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 9 | |
| 10 | #include "Resources.h" |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 12 | #include "SkData.h" |
| 13 | #include "SkDecodingImageGenerator.h" |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 14 | #include "SkImageDecoder.h" |
| 15 | #include "SkOSFile.h" |
| 16 | |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 17 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 18 | |
| 19 | #include "etc1.h" |
| 20 | |
| 21 | /** |
| 22 | * Remove the last row and column of ETC1 blocks, effectively |
| 23 | * making a texture that started as power of two into a texture |
| 24 | * that is no longer power of two... |
| 25 | */ |
| 26 | bool slice_etc1_data(void *data, int* width, int* height) { |
| 27 | |
| 28 | // First, parse the data and get to it... |
| 29 | etc1_byte *origData = reinterpret_cast<etc1_byte *>(data); |
| 30 | if (!etc1_pkm_is_valid(origData)) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | int origW = etc1_pkm_get_width(origData); |
| 35 | int origH = etc1_pkm_get_height(origData); |
| 36 | |
| 37 | int blockWidth = (origW + 3) >> 2; |
| 38 | int blockHeight = (origH + 3) >> 2; |
| 39 | |
| 40 | // Make sure that we have blocks to trim off.. |
| 41 | if (blockWidth < 2 || blockHeight < 2) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | int newWidth = (blockWidth - 1) << 2; |
| 46 | int newHeight = (blockHeight - 1) << 2; |
| 47 | |
| 48 | size_t newDataSz = etc1_get_encoded_data_size(newWidth, newHeight) + ETC_PKM_HEADER_SIZE; |
| 49 | SkAutoMalloc am(newDataSz); |
| 50 | |
| 51 | etc1_byte *newData = reinterpret_cast<etc1_byte *>(am.get()); |
| 52 | |
| 53 | etc1_pkm_format_header(newData, newWidth, newHeight); |
| 54 | newData += ETC_PKM_HEADER_SIZE; |
| 55 | origData += ETC_PKM_HEADER_SIZE; |
| 56 | |
| 57 | for (int j = 0; j < blockHeight - 1; ++j) { |
| 58 | memcpy(newData, origData, (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE); |
| 59 | origData += blockWidth*ETC1_ENCODED_BLOCK_SIZE; |
| 60 | newData += (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE; |
| 61 | } |
| 62 | |
| 63 | // Stick the data back whence it came |
| 64 | memcpy(data, am.get(), newDataSz); |
| 65 | *width = newWidth; |
| 66 | *height = newHeight; |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | #endif // SK_IGNORE_ETC1_SUPPORT |
| 71 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 72 | namespace skiagm { |
| 73 | |
| 74 | /** |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 75 | * Test decoding an image from a PKM or KTX file and then |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 76 | * from compressed ETC1 data. |
| 77 | */ |
| 78 | class ETC1BitmapGM : public GM { |
| 79 | public: |
| 80 | ETC1BitmapGM() { } |
| 81 | virtual ~ETC1BitmapGM() { } |
| 82 | |
| 83 | protected: |
| 84 | virtual SkString onShortName() SK_OVERRIDE { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 85 | SkString str = SkString("etc1bitmap_"); |
| 86 | str.append(this->fileExtension()); |
sugoi | d9ec549 | 2014-09-10 13:40:18 -0700 | [diff] [blame] | 87 | if (this->isVolatile()) { |
| 88 | str.append("_volatile"); |
| 89 | } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 90 | return str; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | virtual SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 94 | return SkISize::Make(128, 128); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
sugoi | d9ec549 | 2014-09-10 13:40:18 -0700 | [diff] [blame] | 97 | virtual bool isVolatile() const { return false; } |
| 98 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 99 | virtual SkString fileExtension() const = 0; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 100 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 101 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 102 | SkBitmap bm; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 103 | SkString filename = GetResourcePath("mandrill_128."); |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 104 | filename.append(this->fileExtension()); |
mtklein | 04cd368 | 2014-06-03 09:04:33 -0700 | [diff] [blame] | 105 | SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str())); |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 106 | if (NULL == fileData) { |
| 107 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 108 | return; |
| 109 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 110 | |
| 111 | if (!SkInstallDiscardablePixelRef( |
| 112 | SkDecodingImageGenerator::Create( |
| 113 | fileData, SkDecodingImageGenerator::Options()), &bm)) { |
| 114 | SkDebugf("Could not install discardable pixel ref.\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
sugoi | d9ec549 | 2014-09-10 13:40:18 -0700 | [diff] [blame] | 118 | bm.setIsVolatile(this->isVolatile()); |
| 119 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 120 | canvas->drawBitmap(bm, 0, 0); |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | typedef GM INHERITED; |
| 125 | }; |
| 126 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 127 | // This class specializes ETC1BitmapGM to load the mandrill_128.pkm file. |
| 128 | class ETC1Bitmap_PKM_GM : public ETC1BitmapGM { |
| 129 | public: |
| 130 | ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { } |
| 131 | virtual ~ETC1Bitmap_PKM_GM() { } |
| 132 | |
| 133 | protected: |
| 134 | |
| 135 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("pkm"); } |
| 136 | |
| 137 | private: |
| 138 | typedef ETC1BitmapGM INHERITED; |
| 139 | }; |
| 140 | |
sugoi | d9ec549 | 2014-09-10 13:40:18 -0700 | [diff] [blame] | 141 | // This class specializes ETC1BitmapGM to load the mandrill_128.pkm file in a volatile bitmap. |
| 142 | class ETC1Bitmap_PKM_VOLATILE_GM : public ETC1BitmapGM { |
| 143 | public: |
| 144 | ETC1Bitmap_PKM_VOLATILE_GM() : ETC1BitmapGM() { } |
| 145 | virtual ~ETC1Bitmap_PKM_VOLATILE_GM() { } |
| 146 | |
| 147 | protected: |
| 148 | |
| 149 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("pkm"); } |
| 150 | virtual bool isVolatile() const SK_OVERRIDE { return true; } |
| 151 | |
| 152 | private: |
| 153 | typedef ETC1BitmapGM INHERITED; |
| 154 | }; |
| 155 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 156 | // This class specializes ETC1BitmapGM to load the mandrill_128.ktx file. |
| 157 | class ETC1Bitmap_KTX_GM : public ETC1BitmapGM { |
| 158 | public: |
| 159 | ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { } |
| 160 | virtual ~ETC1Bitmap_KTX_GM() { } |
| 161 | |
| 162 | protected: |
| 163 | |
| 164 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("ktx"); } |
| 165 | |
| 166 | private: |
| 167 | typedef ETC1BitmapGM INHERITED; |
| 168 | }; |
| 169 | |
krajcevski | a50b8f0 | 2014-08-08 11:38:27 -0700 | [diff] [blame] | 170 | // This class specializes ETC1BitmapGM to load the mandrill_128.r11.ktx file. |
| 171 | class ETC1Bitmap_R11_KTX_GM : public ETC1BitmapGM { |
| 172 | public: |
| 173 | ETC1Bitmap_R11_KTX_GM() : ETC1BitmapGM() { } |
| 174 | virtual ~ETC1Bitmap_R11_KTX_GM() { } |
| 175 | |
| 176 | protected: |
| 177 | |
| 178 | virtual SkString fileExtension() const SK_OVERRIDE { return SkString("r11.ktx"); } |
| 179 | |
| 180 | private: |
| 181 | typedef ETC1BitmapGM INHERITED; |
| 182 | }; |
| 183 | |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 184 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 185 | /** |
| 186 | * Test decoding an image from a PKM file and then |
| 187 | * from non-power-of-two compressed ETC1 data. First slice |
| 188 | * off a row and column of blocks in order to make it non-power |
| 189 | * of two. |
| 190 | */ |
| 191 | class ETC1Bitmap_NPOT_GM : public GM { |
| 192 | public: |
| 193 | ETC1Bitmap_NPOT_GM() { } |
| 194 | virtual ~ETC1Bitmap_NPOT_GM() { } |
| 195 | |
| 196 | protected: |
| 197 | virtual SkString onShortName() SK_OVERRIDE { |
| 198 | return SkString("etc1bitmap_npot"); |
| 199 | } |
| 200 | |
| 201 | virtual SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 202 | return SkISize::Make(124, 124); |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 206 | SkBitmap bm; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 207 | SkString pkmFilename = GetResourcePath("mandrill_128.pkm"); |
| 208 | SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str())); |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 209 | if (NULL == fileData) { |
| 210 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | SkAutoMalloc am(fileData->size()); |
| 215 | memcpy(am.get(), fileData->data(), fileData->size()); |
| 216 | |
| 217 | int width, height; |
| 218 | if (!slice_etc1_data(am.get(), &width, &height)) { |
| 219 | SkDebugf("ETC1 Data is poorly formatted.\n"); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | SkASSERT(124 == width); |
| 224 | SkASSERT(124 == height); |
| 225 | |
| 226 | size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE; |
| 227 | SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz)); |
| 228 | |
| 229 | if (!SkInstallDiscardablePixelRef( |
| 230 | SkDecodingImageGenerator::Create( |
| 231 | nonPOTData, SkDecodingImageGenerator::Options()), &bm)) { |
| 232 | SkDebugf("Could not install discardable pixel ref.\n"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | canvas->drawBitmap(bm, 0, 0); |
| 237 | } |
| 238 | |
| 239 | private: |
| 240 | typedef GM INHERITED; |
| 241 | }; |
| 242 | #endif // SK_IGNORE_ETC1_SUPPORT |
| 243 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 244 | } // namespace skiagm |
| 245 | |
| 246 | ////////////////////////////////////////////////////////////////////////////// |
| 247 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 248 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); ) |
sugoi | d9ec549 | 2014-09-10 13:40:18 -0700 | [diff] [blame] | 249 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_VOLATILE_GM); ) |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 250 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); ) |
krajcevski | a50b8f0 | 2014-08-08 11:38:27 -0700 | [diff] [blame] | 251 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_R11_KTX_GM); ) |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 252 | |
| 253 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 254 | DEF_GM( return SkNEW(skiagm::ETC1Bitmap_NPOT_GM); ) |
| 255 | #endif // SK_IGNORE_ETC1_SUPPORT |