blob: d9b08d1e8642c34af15f84f2ab1546d9583ee950 [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));
108 canvas->drawImage(image, 0, 0);
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000109 }
110
111private:
112 typedef GM INHERITED;
113};
114
krajcevski99ffe242014-06-03 13:04:35 -0700115// This class specializes ETC1BitmapGM to load the mandrill_128.pkm file.
116class ETC1Bitmap_PKM_GM : public ETC1BitmapGM {
117public:
118 ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { }
119 virtual ~ETC1Bitmap_PKM_GM() { }
120
121protected:
122
mtklein36352bf2015-03-25 18:17:31 -0700123 SkString fileExtension() const override { return SkString("pkm"); }
krajcevski99ffe242014-06-03 13:04:35 -0700124
125private:
126 typedef ETC1BitmapGM INHERITED;
127};
128
129// This class specializes ETC1BitmapGM to load the mandrill_128.ktx file.
130class ETC1Bitmap_KTX_GM : public ETC1BitmapGM {
131public:
132 ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { }
133 virtual ~ETC1Bitmap_KTX_GM() { }
134
135protected:
136
mtklein36352bf2015-03-25 18:17:31 -0700137 SkString fileExtension() const override { return SkString("ktx"); }
krajcevski99ffe242014-06-03 13:04:35 -0700138
139private:
140 typedef ETC1BitmapGM INHERITED;
141};
142
krajcevskia50b8f02014-08-08 11:38:27 -0700143// This class specializes ETC1BitmapGM to load the mandrill_128.r11.ktx file.
144class ETC1Bitmap_R11_KTX_GM : public ETC1BitmapGM {
145public:
146 ETC1Bitmap_R11_KTX_GM() : ETC1BitmapGM() { }
147 virtual ~ETC1Bitmap_R11_KTX_GM() { }
148
149protected:
150
mtklein36352bf2015-03-25 18:17:31 -0700151 SkString fileExtension() const override { return SkString("r11.ktx"); }
krajcevskia50b8f02014-08-08 11:38:27 -0700152
153private:
154 typedef ETC1BitmapGM INHERITED;
155};
156
krajcevski2dc337c2014-06-04 09:57:47 -0700157#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 */
164class ETC1Bitmap_NPOT_GM : public GM {
165public:
166 ETC1Bitmap_NPOT_GM() { }
167 virtual ~ETC1Bitmap_NPOT_GM() { }
168
169protected:
mtklein36352bf2015-03-25 18:17:31 -0700170 SkString onShortName() override {
krajcevski2dc337c2014-06-04 09:57:47 -0700171 return SkString("etc1bitmap_npot");
172 }
173
mtklein36352bf2015-03-25 18:17:31 -0700174 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -0700175 return SkISize::Make(124, 124);
krajcevski2dc337c2014-06-04 09:57:47 -0700176 }
177
mtklein36352bf2015-03-25 18:17:31 -0700178 void onDraw(SkCanvas* canvas) override {
krajcevski2dc337c2014-06-04 09:57:47 -0700179 SkBitmap bm;
tfarinac846f4a2014-07-01 12:35:49 -0700180 SkString pkmFilename = GetResourcePath("mandrill_128.pkm");
181 SkAutoDataUnref fileData(SkData::NewFromFileName(pkmFilename.c_str()));
halcanary96fcdcc2015-08-27 07:41:13 -0700182 if (nullptr == fileData) {
krajcevski2dc337c2014-06-04 09:57:47 -0700183 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
reed74bd9532015-09-14 08:52:12 -0700202 SkAutoTUnref<SkImage> image(SkImage::NewFromEncoded(nonPOTData));
203 canvas->drawImage(image, 0, 0);
krajcevski2dc337c2014-06-04 09:57:47 -0700204 }
205
206private:
207 typedef GM INHERITED;
208};
209#endif // SK_IGNORE_ETC1_SUPPORT
210
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000211} // namespace skiagm
212
213//////////////////////////////////////////////////////////////////////////////
214
halcanary385fe4d2015-08-26 13:07:48 -0700215DEF_GM(return new skiagm::ETC1Bitmap_PKM_GM;)
216DEF_GM(return new skiagm::ETC1Bitmap_KTX_GM;)
217DEF_GM(return new skiagm::ETC1Bitmap_R11_KTX_GM;)
krajcevski2dc337c2014-06-04 09:57:47 -0700218
219#ifndef SK_IGNORE_ETC1_SUPPORT
halcanary385fe4d2015-08-26 13:07:48 -0700220DEF_GM(return new skiagm::ETC1Bitmap_NPOT_GM;)
krajcevski2dc337c2014-06-04 09:57:47 -0700221#endif // SK_IGNORE_ETC1_SUPPORT