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