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" |
| 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: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | SkString onShortName() override { |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 85 | SkString str = SkString("etc1bitmap_"); |
| 86 | str.append(this->fileExtension()); |
| 87 | return str; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 88 | } |
| 89 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 90 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 91 | return SkISize::Make(128, 128); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 92 | } |
| 93 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 94 | virtual SkString fileExtension() const = 0; |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 97 | SkBitmap bm; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 98 | SkString filename = GetResourcePath("mandrill_128."); |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 99 | filename.append(this->fileExtension()); |
mtklein | 04cd368 | 2014-06-03 09:04:33 -0700 | [diff] [blame] | 100 | SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 101 | if (nullptr == fileData) { |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 102 | 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] | 103 | return; |
| 104 | } |
krajcevski | 9c0e629 | 2014-06-02 07:38:14 -0700 | [diff] [blame] | 105 | |
reed | 74bd953 | 2015-09-14 08:52:12 -0700 | [diff] [blame] | 106 | SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(fileData)); |
| 107 | canvas->drawImage(image, 0, 0); |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef GM INHERITED; |
| 112 | }; |
| 113 | |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 114 | // This class specializes ETC1BitmapGM to load the mandrill_128.pkm file. |
| 115 | class ETC1Bitmap_PKM_GM : public ETC1BitmapGM { |
| 116 | public: |
| 117 | ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { } |
| 118 | virtual ~ETC1Bitmap_PKM_GM() { } |
| 119 | |
| 120 | protected: |
| 121 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | SkString fileExtension() const override { return SkString("pkm"); } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 123 | |
| 124 | private: |
| 125 | typedef ETC1BitmapGM INHERITED; |
| 126 | }; |
| 127 | |
| 128 | // This class specializes ETC1BitmapGM to load the mandrill_128.ktx file. |
| 129 | class ETC1Bitmap_KTX_GM : public ETC1BitmapGM { |
| 130 | public: |
| 131 | ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { } |
| 132 | virtual ~ETC1Bitmap_KTX_GM() { } |
| 133 | |
| 134 | protected: |
| 135 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 136 | SkString fileExtension() const override { return SkString("ktx"); } |
krajcevski | 99ffe24 | 2014-06-03 13:04:35 -0700 | [diff] [blame] | 137 | |
| 138 | private: |
| 139 | typedef ETC1BitmapGM INHERITED; |
| 140 | }; |
| 141 | |
krajcevski | a50b8f0 | 2014-08-08 11:38:27 -0700 | [diff] [blame] | 142 | // This class specializes ETC1BitmapGM to load the mandrill_128.r11.ktx file. |
| 143 | class ETC1Bitmap_R11_KTX_GM : public ETC1BitmapGM { |
| 144 | public: |
| 145 | ETC1Bitmap_R11_KTX_GM() : ETC1BitmapGM() { } |
| 146 | virtual ~ETC1Bitmap_R11_KTX_GM() { } |
| 147 | |
| 148 | protected: |
| 149 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 150 | SkString fileExtension() const override { return SkString("r11.ktx"); } |
krajcevski | a50b8f0 | 2014-08-08 11:38:27 -0700 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | typedef ETC1BitmapGM INHERITED; |
| 154 | }; |
| 155 | |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 156 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 157 | /** |
| 158 | * Test decoding an image from a PKM file and then |
| 159 | * from non-power-of-two compressed ETC1 data. First slice |
| 160 | * off a row and column of blocks in order to make it non-power |
| 161 | * of two. |
| 162 | */ |
| 163 | class ETC1Bitmap_NPOT_GM : public GM { |
| 164 | public: |
| 165 | ETC1Bitmap_NPOT_GM() { } |
| 166 | virtual ~ETC1Bitmap_NPOT_GM() { } |
| 167 | |
| 168 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 169 | SkString onShortName() override { |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 170 | return SkString("etc1bitmap_npot"); |
| 171 | } |
| 172 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 174 | return SkISize::Make(124, 124); |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 175 | } |
| 176 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 177 | void onDraw(SkCanvas* canvas) override { |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 178 | SkBitmap bm; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 179 | SkString pkmFilename = GetResourcePath("mandrill_128.pkm"); |
| 180 | SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 181 | if (nullptr == fileData) { |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 182 | SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n"); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | SkAutoMalloc am(fileData->size()); |
| 187 | memcpy(am.get(), fileData->data(), fileData->size()); |
| 188 | |
| 189 | int width, height; |
| 190 | if (!slice_etc1_data(am.get(), &width, &height)) { |
| 191 | SkDebugf("ETC1 Data is poorly formatted.\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | SkASSERT(124 == width); |
| 196 | SkASSERT(124 == height); |
| 197 | |
| 198 | size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE; |
| 199 | SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz)); |
| 200 | |
reed | 74bd953 | 2015-09-14 08:52:12 -0700 | [diff] [blame] | 201 | SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(nonPOTData)); |
| 202 | canvas->drawImage(image, 0, 0); |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private: |
| 206 | typedef GM INHERITED; |
| 207 | }; |
| 208 | #endif // SK_IGNORE_ETC1_SUPPORT |
| 209 | |
robertphillips@google.com | 8cf81e0 | 2014-05-22 18:40:29 +0000 | [diff] [blame] | 210 | } // namespace skiagm |
| 211 | |
| 212 | ////////////////////////////////////////////////////////////////////////////// |
| 213 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 214 | DEF_GM(return new skiagm::ETC1Bitmap_PKM_GM;) |
| 215 | DEF_GM(return new skiagm::ETC1Bitmap_KTX_GM;) |
| 216 | DEF_GM(return new skiagm::ETC1Bitmap_R11_KTX_GM;) |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 217 | |
| 218 | #ifndef SK_IGNORE_ETC1_SUPPORT |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 219 | DEF_GM(return new skiagm::ETC1Bitmap_NPOT_GM;) |
krajcevski | 2dc337c | 2014-06-04 09:57:47 -0700 | [diff] [blame] | 220 | #endif // SK_IGNORE_ETC1_SUPPORT |