blob: cdf8617a35627e317feb96de0ea8e09148265af0 [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"
9#include "SkCanvas.h"
krajcevski9c0e6292014-06-02 07:38:14 -070010#include "SkData.h"
11#include "SkDecodingImageGenerator.h"
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000012#include "SkImageDecoder.h"
13#include "SkOSFile.h"
14
krajcevski2dc337c2014-06-04 09:57:47 -070015#ifndef SK_IGNORE_ETC1_SUPPORT
16
17#include "etc1.h"
18
19/**
20 * Remove the last row and column of ETC1 blocks, effectively
21 * making a texture that started as power of two into a texture
22 * that is no longer power of two...
23 */
24bool slice_etc1_data(void *data, int* width, int* height) {
25
26 // First, parse the data and get to it...
27 etc1_byte *origData = reinterpret_cast<etc1_byte *>(data);
28 if (!etc1_pkm_is_valid(origData)) {
29 return false;
30 }
31
32 int origW = etc1_pkm_get_width(origData);
33 int origH = etc1_pkm_get_height(origData);
34
35 int blockWidth = (origW + 3) >> 2;
36 int blockHeight = (origH + 3) >> 2;
37
38 // Make sure that we have blocks to trim off..
39 if (blockWidth < 2 || blockHeight < 2) {
40 return false;
41 }
42
43 int newWidth = (blockWidth - 1) << 2;
44 int newHeight = (blockHeight - 1) << 2;
45
46 size_t newDataSz = etc1_get_encoded_data_size(newWidth, newHeight) + ETC_PKM_HEADER_SIZE;
47 SkAutoMalloc am(newDataSz);
48
49 etc1_byte *newData = reinterpret_cast<etc1_byte *>(am.get());
50
51 etc1_pkm_format_header(newData, newWidth, newHeight);
52 newData += ETC_PKM_HEADER_SIZE;
53 origData += ETC_PKM_HEADER_SIZE;
54
55 for (int j = 0; j < blockHeight - 1; ++j) {
56 memcpy(newData, origData, (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE);
57 origData += blockWidth*ETC1_ENCODED_BLOCK_SIZE;
58 newData += (blockWidth - 1)*ETC1_ENCODED_BLOCK_SIZE;
59 }
60
61 // Stick the data back whence it came
62 memcpy(data, am.get(), newDataSz);
63 *width = newWidth;
64 *height = newHeight;
65
66 return true;
67}
68#endif // SK_IGNORE_ETC1_SUPPORT
69
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000070namespace skiagm {
71
72/**
krajcevski99ffe242014-06-03 13:04:35 -070073 * Test decoding an image from a PKM or KTX file and then
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000074 * from compressed ETC1 data.
75 */
76class ETC1BitmapGM : public GM {
77public:
78 ETC1BitmapGM() { }
79 virtual ~ETC1BitmapGM() { }
80
81protected:
82 virtual SkString onShortName() SK_OVERRIDE {
krajcevski99ffe242014-06-03 13:04:35 -070083 SkString str = SkString("etc1bitmap_");
84 str.append(this->fileExtension());
85 return str;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000086 }
87
88 virtual SkISize onISize() SK_OVERRIDE {
krajcevski99ffe242014-06-03 13:04:35 -070089 return make_isize(128, 128);
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000090 }
91
krajcevski99ffe242014-06-03 13:04:35 -070092 virtual SkString fileExtension() const = 0;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000093
krajcevski99ffe242014-06-03 13:04:35 -070094 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000095 SkBitmap bm;
mtklein91359be2014-06-08 07:02:47 -070096 SkString filename = SkOSPath::SkPathJoin(
97 INHERITED::gResourcePath.c_str(), "mandrill_128.");
krajcevski99ffe242014-06-03 13:04:35 -070098 filename.append(this->fileExtension());
krajcevski9c0e6292014-06-02 07:38:14 -070099
mtklein04cd3682014-06-03 09:04:33 -0700100 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str()));
krajcevski9c0e6292014-06-02 07:38:14 -0700101 if (NULL == fileData) {
102 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000103 return;
104 }
krajcevski9c0e6292014-06-02 07:38:14 -0700105
106 if (!SkInstallDiscardablePixelRef(
107 SkDecodingImageGenerator::Create(
108 fileData, SkDecodingImageGenerator::Options()), &bm)) {
109 SkDebugf("Could not install discardable pixel ref.\n");
110 return;
111 }
112
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000113 canvas->drawBitmap(bm, 0, 0);
114 }
115
116private:
117 typedef GM INHERITED;
118};
119
krajcevski99ffe242014-06-03 13:04:35 -0700120// This class specializes ETC1BitmapGM to load the mandrill_128.pkm file.
121class ETC1Bitmap_PKM_GM : public ETC1BitmapGM {
122public:
123 ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { }
124 virtual ~ETC1Bitmap_PKM_GM() { }
125
126protected:
127
128 virtual SkString fileExtension() const SK_OVERRIDE { return SkString("pkm"); }
129
130private:
131 typedef ETC1BitmapGM INHERITED;
132};
133
134// This class specializes ETC1BitmapGM to load the mandrill_128.ktx file.
135class ETC1Bitmap_KTX_GM : public ETC1BitmapGM {
136public:
137 ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { }
138 virtual ~ETC1Bitmap_KTX_GM() { }
139
140protected:
141
142 virtual SkString fileExtension() const SK_OVERRIDE { return SkString("ktx"); }
143
144private:
145 typedef ETC1BitmapGM INHERITED;
146};
147
krajcevski2dc337c2014-06-04 09:57:47 -0700148#ifndef SK_IGNORE_ETC1_SUPPORT
149/**
150 * Test decoding an image from a PKM file and then
151 * from non-power-of-two compressed ETC1 data. First slice
152 * off a row and column of blocks in order to make it non-power
153 * of two.
154 */
155class ETC1Bitmap_NPOT_GM : public GM {
156public:
157 ETC1Bitmap_NPOT_GM() { }
158 virtual ~ETC1Bitmap_NPOT_GM() { }
159
160protected:
161 virtual SkString onShortName() SK_OVERRIDE {
162 return SkString("etc1bitmap_npot");
163 }
164
165 virtual SkISize onISize() SK_OVERRIDE {
166 return make_isize(124, 124);
167 }
168
169 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
170
171 SkBitmap bm;
mtklein91359be2014-06-08 07:02:47 -0700172 SkString filename = SkOSPath::SkPathJoin(
173 INHERITED::gResourcePath.c_str(), "mandrill_128.pkm");
krajcevski2dc337c2014-06-04 09:57:47 -0700174
175 SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str()));
176 if (NULL == fileData) {
177 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
178 return;
179 }
180
181 SkAutoMalloc am(fileData->size());
182 memcpy(am.get(), fileData->data(), fileData->size());
183
184 int width, height;
185 if (!slice_etc1_data(am.get(), &width, &height)) {
186 SkDebugf("ETC1 Data is poorly formatted.\n");
187 return;
188 }
189
190 SkASSERT(124 == width);
191 SkASSERT(124 == height);
192
193 size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE;
194 SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz));
195
196 if (!SkInstallDiscardablePixelRef(
197 SkDecodingImageGenerator::Create(
198 nonPOTData, SkDecodingImageGenerator::Options()), &bm)) {
199 SkDebugf("Could not install discardable pixel ref.\n");
200 return;
201 }
202
203 canvas->drawBitmap(bm, 0, 0);
204 }
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
krajcevski99ffe242014-06-03 13:04:35 -0700215DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); )
216DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); )
krajcevski2dc337c2014-06-04 09:57:47 -0700217
218#ifndef SK_IGNORE_ETC1_SUPPORT
219DEF_GM( return SkNEW(skiagm::ETC1Bitmap_NPOT_GM); )
220#endif // SK_IGNORE_ETC1_SUPPORT