blob: e0a59b0c4f5314a2ba9787750a4c84cbe2925a9c [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"
13#include "SkDecodingImageGenerator.h"
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000014#include "SkImageDecoder.h"
15#include "SkOSFile.h"
16
krajcevski2dc337c2014-06-04 09:57:47 -070017#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 */
26bool 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.com8cf81e02014-05-22 18:40:29 +000072namespace skiagm {
73
74/**
krajcevski99ffe242014-06-03 13:04:35 -070075 * Test decoding an image from a PKM or KTX file and then
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000076 * from compressed ETC1 data.
77 */
78class ETC1BitmapGM : public GM {
79public:
80 ETC1BitmapGM() { }
81 virtual ~ETC1BitmapGM() { }
82
83protected:
84 virtual SkString onShortName() SK_OVERRIDE {
krajcevski99ffe242014-06-03 13:04:35 -070085 SkString str = SkString("etc1bitmap_");
86 str.append(this->fileExtension());
87 return str;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000088 }
89
90 virtual SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070091 return SkISize::Make(128, 128);
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000092 }
93
krajcevski99ffe242014-06-03 13:04:35 -070094 virtual SkString fileExtension() const = 0;
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000095
krajcevski99ffe242014-06-03 13:04:35 -070096 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.com8cf81e02014-05-22 18:40:29 +000097 SkBitmap bm;
tfarinabcbc1782014-06-18 14:32:48 -070098 SkString resourcePath = GetResourcePath();
99 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "mandrill_128.");
krajcevski99ffe242014-06-03 13:04:35 -0700100 filename.append(this->fileExtension());
krajcevski9c0e6292014-06-02 07:38:14 -0700101
mtklein04cd3682014-06-03 09:04:33 -0700102 SkAutoTUnref<SkData> fileData(SkData::NewFromFileName(filename.c_str()));
krajcevski9c0e6292014-06-02 07:38:14 -0700103 if (NULL == fileData) {
104 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000105 return;
106 }
krajcevski9c0e6292014-06-02 07:38:14 -0700107
108 if (!SkInstallDiscardablePixelRef(
109 SkDecodingImageGenerator::Create(
110 fileData, SkDecodingImageGenerator::Options()), &bm)) {
111 SkDebugf("Could not install discardable pixel ref.\n");
112 return;
113 }
114
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000115 canvas->drawBitmap(bm, 0, 0);
116 }
117
118private:
119 typedef GM INHERITED;
120};
121
krajcevski99ffe242014-06-03 13:04:35 -0700122// This class specializes ETC1BitmapGM to load the mandrill_128.pkm file.
123class ETC1Bitmap_PKM_GM : public ETC1BitmapGM {
124public:
125 ETC1Bitmap_PKM_GM() : ETC1BitmapGM() { }
126 virtual ~ETC1Bitmap_PKM_GM() { }
127
128protected:
129
130 virtual SkString fileExtension() const SK_OVERRIDE { return SkString("pkm"); }
131
132private:
133 typedef ETC1BitmapGM INHERITED;
134};
135
136// This class specializes ETC1BitmapGM to load the mandrill_128.ktx file.
137class ETC1Bitmap_KTX_GM : public ETC1BitmapGM {
138public:
139 ETC1Bitmap_KTX_GM() : ETC1BitmapGM() { }
140 virtual ~ETC1Bitmap_KTX_GM() { }
141
142protected:
143
144 virtual SkString fileExtension() const SK_OVERRIDE { return SkString("ktx"); }
145
146private:
147 typedef ETC1BitmapGM INHERITED;
148};
149
krajcevski2dc337c2014-06-04 09:57:47 -0700150#ifndef SK_IGNORE_ETC1_SUPPORT
151/**
152 * Test decoding an image from a PKM file and then
153 * from non-power-of-two compressed ETC1 data. First slice
154 * off a row and column of blocks in order to make it non-power
155 * of two.
156 */
157class ETC1Bitmap_NPOT_GM : public GM {
158public:
159 ETC1Bitmap_NPOT_GM() { }
160 virtual ~ETC1Bitmap_NPOT_GM() { }
161
162protected:
163 virtual SkString onShortName() SK_OVERRIDE {
164 return SkString("etc1bitmap_npot");
165 }
166
167 virtual SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -0700168 return SkISize::Make(124, 124);
krajcevski2dc337c2014-06-04 09:57:47 -0700169 }
170
171 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
krajcevski2dc337c2014-06-04 09:57:47 -0700172 SkBitmap bm;
tfarinabcbc1782014-06-18 14:32:48 -0700173 SkString resourcePath = GetResourcePath();
174 SkString filename = SkOSPath::SkPathJoin(resourcePath.c_str(), "mandrill_128.pkm");
krajcevski2dc337c2014-06-04 09:57:47 -0700175
176 SkAutoDataUnref fileData(SkData::NewFromFileName(filename.c_str()));
177 if (NULL == fileData) {
178 SkDebugf("Could not open the file. Did you forget to set the resourcePath?\n");
179 return;
180 }
181
182 SkAutoMalloc am(fileData->size());
183 memcpy(am.get(), fileData->data(), fileData->size());
184
185 int width, height;
186 if (!slice_etc1_data(am.get(), &width, &height)) {
187 SkDebugf("ETC1 Data is poorly formatted.\n");
188 return;
189 }
190
191 SkASSERT(124 == width);
192 SkASSERT(124 == height);
193
194 size_t dataSz = etc1_get_encoded_data_size(width, height) + ETC_PKM_HEADER_SIZE;
195 SkAutoDataUnref nonPOTData(SkData::NewWithCopy(am.get(), dataSz));
196
197 if (!SkInstallDiscardablePixelRef(
198 SkDecodingImageGenerator::Create(
199 nonPOTData, SkDecodingImageGenerator::Options()), &bm)) {
200 SkDebugf("Could not install discardable pixel ref.\n");
201 return;
202 }
203
204 canvas->drawBitmap(bm, 0, 0);
205 }
206
207private:
208 typedef GM INHERITED;
209};
210#endif // SK_IGNORE_ETC1_SUPPORT
211
robertphillips@google.com8cf81e02014-05-22 18:40:29 +0000212} // namespace skiagm
213
214//////////////////////////////////////////////////////////////////////////////
215
krajcevski99ffe242014-06-03 13:04:35 -0700216DEF_GM( return SkNEW(skiagm::ETC1Bitmap_PKM_GM); )
217DEF_GM( return SkNEW(skiagm::ETC1Bitmap_KTX_GM); )
krajcevski2dc337c2014-06-04 09:57:47 -0700218
219#ifndef SK_IGNORE_ETC1_SUPPORT
220DEF_GM( return SkNEW(skiagm::ETC1Bitmap_NPOT_GM); )
221#endif // SK_IGNORE_ETC1_SUPPORT