blob: e2f6c2dc41b586fe955253366cedfee4f75a0416 [file] [log] [blame]
robertphillips@google.com8cf81e02014-05-22 18:40:29 +00001/*
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"
tfarinabcbc1782014-06-18 14:32:48 -07009
10#include "Resources.h"
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000011#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070012#include "SkData.h"
reed5965c8a2015-01-07 18:04:45 -080013#include "SkImageGenerator.h"
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000014#include "SkImageDecoder.h"
15#include "SkOSFile.h"
scroggo565901d2015-12-10 10:44:13 -080016#include "SkTemplates.h"
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000017
krajcevski2dc337c2014-06-04 09:57:47 -070018#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 */
27bool 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;
scroggo565901d2015-12-10 10:44:13 -080050 SkAutoTMalloc<etc1_byte> am(newDataSz);
krajcevski2dc337c2014-06-04 09:57:47 -070051
scroggo565901d2015-12-10 10:44:13 -080052 etc1_byte* newData = am.get();
krajcevski2dc337c2014-06-04 09:57:47 -070053
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.com8cf81e02014-05-22 18:40:29 +000073namespace skiagm {
74
75/**
krajcevski99ffe242014-06-03 13:04:35 -070076 * Test decoding an image from a PKM or KTX file and then
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000077 * from compressed ETC1 data.
78 */
79class ETC1BitmapGM : public GM {
80public:
81 ETC1BitmapGM() { }
82 virtual ~ETC1BitmapGM() { }
83
84protected:
mtklein36352bf2015-03-25 18:17:31 -070085 SkString onShortName() override {
krajcevski99ffe242014-06-03 13:04:35 -070086 SkString str = SkString("etc1bitmap_");
87 str.append(this->fileExtension());
88 return str;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000089 }
90
mtklein36352bf2015-03-25 18:17:31 -070091 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070092 return SkISize::Make(128, 128);
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000093 }
94
krajcevski99ffe242014-06-03 13:04:35 -070095 virtual SkString fileExtension() const = 0;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000096
mtklein36352bf2015-03-25 18:17:31 -070097 void onDraw(SkCanvas* canvas) override {
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000098 SkBitmap bm;
tfarinac846f4a2014-07-01 12:35:49 -070099 SkString filename = GetResourcePath("mandrill_128.");
krajcevski99ffe242014-06-03 13:04:35 -0700100 filename.append(this->fileExtension());
mtklein04cd3682014-06-03 09:04:33 -0700101 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -0700102 if (nullptr == fileData) {
krajcevski9c0e6292014-06-02 07:38:14 -0700103 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000104 return;
105 }
krajcevski9c0e6292014-06-02 07:38:14 -0700106
reed74bd9532015-09-14 08:52:12 -0700107 SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(fileData));
djsollen19aa2b02016-01-07 07:22:47 -0800108 if (nullptr == image) {
109 SkDebugf("Could not decode the ETC file. ETC may not be included in this platform.\n");
110 return;
111 }
reed74bd9532015-09-14 08:52:12 -0700112 canvas->drawImage(image, 0, 0);
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000113 }
114
115private:
116 typedef GM INHERITED;
117};
118
krajcevski99ffe242014-06-03 13:04:35 -0700119// This class specializes ETC1BitmapGM to load the mandrill_128.pkm file.
120class ETC1Bitmap_PKM_GM : public ETC1BitmapGM {
121public:
122 ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { }
123 virtual ~ETC1Bitmap_PKM_GM() { }
124
125protected:
126
mtklein36352bf2015-03-25 18:17:31 -0700127 SkString fileExtension() const override { return SkString("pkm"); }
krajcevski99ffe242014-06-03 13:04:35 -0700128
129private:
130 typedef ETC1BitmapGM INHERITED;
131};
132
133// This class specializes ETC1BitmapGM to load the mandrill_128.ktx file.
134class ETC1Bitmap_KTX_GM : public ETC1BitmapGM {
135public:
136 ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { }
137 virtual ~ETC1Bitmap_KTX_GM() { }
138
139protected:
140
mtklein36352bf2015-03-25 18:17:31 -0700141 SkString fileExtension() const override { return SkString("ktx"); }
krajcevski99ffe242014-06-03 13:04:35 -0700142
143private:
144 typedef ETC1BitmapGM INHERITED;
145};
146
krajcevskia50b8f02014-08-08 11:38:27 -0700147// This class specializes ETC1BitmapGM to load the mandrill_128.r11.ktx file.
148class ETC1Bitmap_R11_KTX_GM : public ETC1BitmapGM {
149public:
150 ETC1Bitmap_R11_KTX_GM() : ETC1BitmapGM() { }
151 virtual ~ETC1Bitmap_R11_KTX_GM() { }
152
153protected:
154
mtklein36352bf2015-03-25 18:17:31 -0700155 SkString fileExtension() const override { return SkString("r11.ktx"); }
krajcevskia50b8f02014-08-08 11:38:27 -0700156
157private:
158 typedef ETC1BitmapGM INHERITED;
159};
160
krajcevski2dc337c2014-06-04 09:57:47 -0700161#ifndef SK_IGNORE_ETC1_SUPPORT
162/**
163 * Test decoding an image from a PKM file and then
164 * from non-power-of-two compressed ETC1 data. First slice
165 * off a row and column of blocks in order to make it non-power
166 * of two.
167 */
168class ETC1Bitmap_NPOT_GM : public GM {
169public:
170 ETC1Bitmap_NPOT_GM() { }
171 virtual ~ETC1Bitmap_NPOT_GM() { }
172
173protected:
mtklein36352bf2015-03-25 18:17:31 -0700174 SkString onShortName() override {
krajcevski2dc337c2014-06-04 09:57:47 -0700175 return SkString("etc1bitmap_npot");
176 }
177
mtklein36352bf2015-03-25 18:17:31 -0700178 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700179 return SkISize::Make(124, 124);
krajcevski2dc337c2014-06-04 09:57:47 -0700180 }
181
mtklein36352bf2015-03-25 18:17:31 -0700182 void onDraw(SkCanvas* canvas) override {
krajcevski2dc337c2014-06-04 09:57:47 -0700183 SkBitmap bm;
tfarinac846f4a2014-07-01 12:35:49 -0700184 SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
185 SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -0700186 if (nullptr == fileData) {
krajcevski2dc337c2014-06-04 09:57:47 -0700187 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
188 return;
189 }
190
191 SkAutoMalloc am(fileData->size());
192 memcpy(am.get(), fileData->data(), fileData->size());
193
194 int width, height;
195 if (!slice_etc1_data(am.get(), &width, &height)) {
196 SkDebugf("ETC1 Data is poorly formatted.\n");
197 return;
198 }
199
200 SkASSERT(124 == width);
201 SkASSERT(124 == height);
202
203 size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE;
204 SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz));
205
reed74bd9532015-09-14 08:52:12 -0700206 SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(nonPOTData));
207 canvas->drawImage(image, 0, 0);
krajcevski2dc337c2014-06-04 09:57:47 -0700208 }
209
210private:
211 typedef GM INHERITED;
212};
213#endif // SK_IGNORE_ETC1_SUPPORT
214
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000215} // namespace skiagm
216
217//////////////////////////////////////////////////////////////////////////////
218
halcanary385fe4d2015-08-26 13:07:48 -0700219DEF_GM(return new skiagm::ETC1Bitmap_PKM_GM;)
220DEF_GM(return new skiagm::ETC1Bitmap_KTX_GM;)
221DEF_GM(return new skiagm::ETC1Bitmap_R11_KTX_GM;)
krajcevski2dc337c2014-06-04 09:57:47 -0700222
223#ifndef SK_IGNORE_ETC1_SUPPORT
halcanary385fe4d2015-08-26 13:07:48 -0700224DEF_GM(return new skiagm::ETC1Bitmap_NPOT_GM;)
krajcevski2dc337c2014-06-04 09:57:47 -0700225#endif // SK_IGNORE_ETC1_SUPPORT