blob: 02f919bbbfdf375e9c824048aad6a2b0ec17c01a [file] [log] [blame]
Robert Phillipsd4f68312020-01-31 10:15:05 -05001/*
2 * Copyright 2020 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/gm.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkImage.h"
11#include "include/core/SkStream.h"
Adlai Holler4caa9352020-07-16 10:58:58 -040012#include "include/gpu/GrDirectContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040013#include "include/gpu/GrRecordingContext.h"
Robert Phillipsd4f68312020-01-31 10:15:05 -050014#include "src/core/SkCompressedDataUtils.h"
Mike Reed13711eb2020-07-14 17:16:32 -040015#include "src/core/SkMipmap.h"
Robert Phillips95c250c2020-06-29 15:36:12 -040016#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillipsd4f68312020-01-31 10:15:05 -050017#include "src/gpu/gl/GrGLDefines.h"
18#include "src/image/SkImage_Base.h"
Robert Phillipsa84caa32020-07-28 09:57:26 -040019#include "src/image/SkImage_GpuBase.h"
Robert Phillipsd4f68312020-01-31 10:15:05 -050020
21#include "tools/Resources.h"
22
23//-------------------------------------------------------------------------------------------------
24struct ImageInfo {
25 SkISize fDim;
Brian Salomon40a40622020-07-21 10:32:07 -040026 GrMipmapped fMipmapped;
Robert Phillipsd4f68312020-01-31 10:15:05 -050027 SkImage::CompressionType fCompressionType;
28};
29
30/*
31 * Get an int from a buffer
32 * This method is unsafe, the caller is responsible for performing a check
33 */
34static inline uint32_t get_uint(uint8_t* buffer, uint32_t i) {
35 uint32_t result;
36 memcpy(&result, &(buffer[i]), 4);
37 return result;
38}
39
40// This KTX loader is barely sufficient to load the specific files this GM requires. Use
41// at your own peril.
42static sk_sp<SkData> load_ktx(const char* filename, ImageInfo* imageInfo) {
43 SkFILEStream input(filename);
44 if (!input.isValid()) {
45 return nullptr;
46 }
47
48 constexpr int kKTXIdentifierSize = 12;
49 constexpr int kKTXHeaderSize = kKTXIdentifierSize + 13 * sizeof(uint32_t);
50 uint8_t header[kKTXHeaderSize];
51
52 if (input.read(header, kKTXHeaderSize) != kKTXHeaderSize) {
53 return nullptr;
54 }
55
56 static const uint8_t kExpectedIdentifier[kKTXIdentifierSize] = {
57 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A
58 };
59
John Stilesc1c3c6d2020-08-15 23:22:53 -040060 if (0 != memcmp(header, kExpectedIdentifier, kKTXIdentifierSize)) {
Robert Phillipsd4f68312020-01-31 10:15:05 -050061 return nullptr;
62 }
63
64 uint32_t endianness = get_uint(header, 12);
65 if (endianness != 0x04030201) {
66 // TODO: need to swap rest of header and, if glTypeSize is > 1, all
67 // the texture data.
68 return nullptr;
69 }
70
71 uint32_t glType = get_uint(header, 16);
72 SkDEBUGCODE(uint32_t glTypeSize = get_uint(header, 20);)
73 uint32_t glFormat = get_uint(header, 24);
74 uint32_t glInternalFormat = get_uint(header, 28);
75 //uint32_t glBaseInternalFormat = get_uint(header, 32);
76 uint32_t pixelWidth = get_uint(header, 36);
77 uint32_t pixelHeight = get_uint(header, 40);
78 uint32_t pixelDepth = get_uint(header, 44);
79 //uint32_t numberOfArrayElements = get_uint(header, 48);
80 uint32_t numberOfFaces = get_uint(header, 52);
81 int numberOfMipmapLevels = get_uint(header, 56);
82 uint32_t bytesOfKeyValueData = get_uint(header, 60);
83
84 if (glType != 0 || glFormat != 0) { // only care about compressed data for now
85 return nullptr;
86 }
87 SkASSERT(glTypeSize == 1); // required for compressed data
88
89 // We only handle these four formats right now
90 switch (glInternalFormat) {
91 case GR_GL_COMPRESSED_ETC1_RGB8:
92 case GR_GL_COMPRESSED_RGB8_ETC2:
93 imageInfo->fCompressionType = SkImage::CompressionType::kETC2_RGB8_UNORM;
94 break;
95 case GR_GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
96 imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGB8_UNORM;
97 break;
98 case GR_GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
99 imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGBA8_UNORM;
100 break;
101 default:
102 return nullptr;
103 }
104
105 imageInfo->fDim.fWidth = pixelWidth;
106 imageInfo->fDim.fHeight = pixelHeight;
107
108 if (pixelDepth != 0) {
109 return nullptr; // pixel depth is always zero for 2D textures
110 }
111
112 if (numberOfFaces != 1) {
113 return nullptr; // we don't support cube maps right now
114 }
115
116 if (numberOfMipmapLevels == 1) {
Brian Salomon40a40622020-07-21 10:32:07 -0400117 imageInfo->fMipmapped = GrMipmapped::kNo;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500118 } else {
Mike Reed13711eb2020-07-14 17:16:32 -0400119 int numRequiredMipLevels = SkMipmap::ComputeLevelCount(pixelWidth, pixelHeight)+1;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500120 if (numberOfMipmapLevels != numRequiredMipLevels) {
121 return nullptr;
122 }
Brian Salomon40a40622020-07-21 10:32:07 -0400123 imageInfo->fMipmapped = GrMipmapped::kYes;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500124 }
125
126 if (bytesOfKeyValueData != 0) {
127 return nullptr;
128 }
129
130 SkTArray<size_t> individualMipOffsets(numberOfMipmapLevels);
131
132 size_t dataSize = SkCompressedDataSize(imageInfo->fCompressionType,
133 { (int) pixelWidth, (int) pixelHeight },
134 &individualMipOffsets,
Brian Salomon40a40622020-07-21 10:32:07 -0400135 imageInfo->fMipmapped == GrMipmapped::kYes);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500136 SkASSERT(individualMipOffsets.size() == (size_t) numberOfMipmapLevels);
137
138 sk_sp<SkData> data = SkData::MakeUninitialized(dataSize);
139
140 uint8_t* dest = (uint8_t*) data->writable_data();
141
142 size_t offset = 0;
143 for (int i = 0; i < numberOfMipmapLevels; ++i) {
144 uint32_t imageSize;
145
146 if (input.read(&imageSize, 4) != 4) {
147 return nullptr;
148 }
149
150 SkASSERT(offset + imageSize <= dataSize);
151 SkASSERT(offset == individualMipOffsets[i]);
152
153 if (input.read(&dest[offset], imageSize) != imageSize) {
154 return nullptr;
155 }
156
157 offset += imageSize;
158 }
159
160 return data;
161}
162
163//-------------------------------------------------------------------------------------------------
164typedef uint32_t DWORD;
165
166// Values for the DDS_PIXELFORMAT 'dwFlags' field
167constexpr unsigned int kDDPF_FOURCC = 0x4;
168
169struct DDS_PIXELFORMAT {
170 DWORD dwSize;
171 DWORD dwFlags;
172 DWORD dwFourCC;
173 DWORD dwRGBBitCount;
174 DWORD dwRBitMask;
175 DWORD dwGBitMask;
176 DWORD dwBBitMask;
177 DWORD dwABitMask;
178};
179
180// Values for the DDS_HEADER 'dwFlags' field
181constexpr unsigned int kDDSD_CAPS = 0x1; // required
182constexpr unsigned int kDDSD_HEIGHT = 0x2; // required
183constexpr unsigned int kDDSD_WIDTH = 0x4; // required
184constexpr unsigned int kDDSD_PITCH = 0x8;
185constexpr unsigned int kDDSD_PIXELFORMAT = 0x001000; // required
186constexpr unsigned int kDDSD_MIPMAPCOUNT = 0x020000;
187constexpr unsigned int kDDSD_LINEARSIZE = 0x080000;
188constexpr unsigned int kDDSD_DEPTH = 0x800000;
189
190constexpr unsigned int kDDSD_REQUIRED = kDDSD_CAPS | kDDSD_HEIGHT | kDDSD_WIDTH | kDDSD_PIXELFORMAT;
191
192typedef struct {
193 DWORD dwSize;
194 DWORD dwFlags;
195 DWORD dwHeight;
196 DWORD dwWidth;
197 DWORD dwPitchOrLinearSize;
198 DWORD dwDepth;
199 DWORD dwMipMapCount;
200 DWORD dwReserved1[11];
201 DDS_PIXELFORMAT ddspf;
202 DWORD dwCaps;
203 DWORD dwCaps2;
204 DWORD dwCaps3;
205 DWORD dwCaps4;
206 DWORD dwReserved2;
207} DDS_HEADER;
208
209// This DDS loader is barely sufficient to load the specific files this GM requires. Use
210// at your own peril.
211static sk_sp<SkData> load_dds(const char* filename, ImageInfo* imageInfo) {
212 SkFILEStream input(filename);
213 if (!input.isValid()) {
214 return nullptr;
215 }
216
217 constexpr uint32_t kMagic = 0x20534444;
218 uint32_t magic;
219
220 if (input.read(&magic, 4) != 4) {
221 return nullptr;
222 }
223
224 if (magic != kMagic) {
225 return nullptr;
226 }
227
228 constexpr size_t kDDSHeaderSize = sizeof(DDS_HEADER);
229 static_assert(kDDSHeaderSize == 124);
230 constexpr size_t kDDSPixelFormatSize = sizeof(DDS_PIXELFORMAT);
231 static_assert(kDDSPixelFormatSize == 32);
232
233 DDS_HEADER header;
234
235 if (input.read(&header, kDDSHeaderSize) != kDDSHeaderSize) {
236 return nullptr;
237 }
238
239 if (header.dwSize != kDDSHeaderSize ||
240 header.ddspf.dwSize != kDDSPixelFormatSize) {
241 return nullptr;
242 }
243
244 if ((header.dwFlags & kDDSD_REQUIRED) != kDDSD_REQUIRED) {
245 return nullptr;
246 }
247
248 if (header.dwFlags & (kDDSD_PITCH | kDDSD_LINEARSIZE | kDDSD_DEPTH)) {
249 // TODO: support these features
250 }
251
252 imageInfo->fDim.fWidth = header.dwWidth;
253 imageInfo->fDim.fHeight = header.dwHeight;
254
255 int numberOfMipmapLevels = 1;
256 if (header.dwFlags & kDDSD_MIPMAPCOUNT) {
257 if (header.dwMipMapCount == 1) {
Brian Salomon40a40622020-07-21 10:32:07 -0400258 imageInfo->fMipmapped = GrMipmapped::kNo;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500259 } else {
Mike Reed13711eb2020-07-14 17:16:32 -0400260 int numRequiredLevels = SkMipmap::ComputeLevelCount(header.dwWidth, header.dwHeight)+1;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500261 if (header.dwMipMapCount != (unsigned) numRequiredLevels) {
262 return nullptr;
263 }
Brian Salomon40a40622020-07-21 10:32:07 -0400264 imageInfo->fMipmapped = GrMipmapped::kYes;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500265 numberOfMipmapLevels = numRequiredLevels;
266 }
267 } else {
Brian Salomon40a40622020-07-21 10:32:07 -0400268 imageInfo->fMipmapped = GrMipmapped::kNo;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500269 }
270
271 if (!(header.ddspf.dwFlags & kDDPF_FOURCC)) {
272 return nullptr;
273 }
274
275 // We only handle these one format right now
276 switch (header.ddspf.dwFourCC) {
277 case 0x31545844: // DXT1
278 imageInfo->fCompressionType = SkImage::CompressionType::kBC1_RGB8_UNORM;
279 break;
280 default:
281 return nullptr;
282 }
283
284 SkTArray<size_t> individualMipOffsets(numberOfMipmapLevels);
285
286 size_t dataSize = SkCompressedDataSize(imageInfo->fCompressionType,
287 { (int) header.dwWidth, (int) header.dwHeight },
288 &individualMipOffsets,
Brian Salomon40a40622020-07-21 10:32:07 -0400289 imageInfo->fMipmapped == GrMipmapped::kYes);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500290 SkASSERT(individualMipOffsets.size() == (size_t) numberOfMipmapLevels);
291
292 sk_sp<SkData> data = SkData::MakeUninitialized(dataSize);
293
294 uint8_t* dest = (uint8_t*) data->writable_data();
295
296 size_t amountRead = input.read(dest, dataSize);
297 if (amountRead != dataSize) {
298 return nullptr;
299 }
300
301 return data;
302}
303
304//-------------------------------------------------------------------------------------------------
Adlai Holler4caa9352020-07-16 10:58:58 -0400305static sk_sp<SkImage> data_to_img(GrDirectContext *direct, sk_sp<SkData> data,
306 const ImageInfo& info) {
307 if (direct) {
308 return SkImage::MakeTextureFromCompressed(direct, std::move(data),
Robert Phillipsd4f68312020-01-31 10:15:05 -0500309 info.fDim.fWidth,
310 info.fDim.fHeight,
311 info.fCompressionType,
Brian Salomon40a40622020-07-21 10:32:07 -0400312 info.fMipmapped);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500313 } else {
314 return SkImage::MakeRasterFromCompressed(std::move(data),
315 info.fDim.fWidth,
316 info.fDim.fHeight,
317 info.fCompressionType);
318 }
319}
320
321namespace skiagm {
322
323// This GM exercises our handling of some of the more exotic formats using externally
324// generated content. Right now it only tests ETC1 and BC1.
325class ExoticFormatsGM : public GM {
326public:
327 ExoticFormatsGM() {
Robert Phillipseb77a762020-02-03 11:43:20 -0500328 this->setBGColor(SK_ColorBLACK);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500329 }
330
331protected:
332 SkString onShortName() override {
333 return SkString("exoticformats");
334 }
335
336 SkISize onISize() override {
Robert Phillipseb77a762020-02-03 11:43:20 -0500337 return SkISize::Make(2*kImgWidthHeight + 3 * kPad, kImgWidthHeight + 2 * kPad);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500338 }
339
Robert Phillipsa84caa32020-07-28 09:57:26 -0400340 bool loadImages(GrDirectContext *direct) {
341 SkASSERT(!fETC1Image && !fBC1Image);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500342
Robert Phillipsa84caa32020-07-28 09:57:26 -0400343 {
Robert Phillipsd4f68312020-01-31 10:15:05 -0500344 ImageInfo info;
345 sk_sp<SkData> data = load_ktx(GetResourcePath("images/flower-etc1.ktx").c_str(), &info);
346 if (data) {
347 SkASSERT(info.fDim.equals(kImgWidthHeight, kImgWidthHeight));
Brian Salomon40a40622020-07-21 10:32:07 -0400348 SkASSERT(info.fMipmapped == GrMipmapped::kNo);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500349 SkASSERT(info.fCompressionType == SkImage::CompressionType::kETC2_RGB8_UNORM);
350
Adlai Holler4caa9352020-07-16 10:58:58 -0400351 fETC1Image = data_to_img(direct, std::move(data), info);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500352 } else {
353 SkDebugf("failed to load flower-etc1.ktx\n");
Robert Phillipsa84caa32020-07-28 09:57:26 -0400354 return false;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500355 }
356 }
357
Robert Phillipsa84caa32020-07-28 09:57:26 -0400358 {
Robert Phillipsd4f68312020-01-31 10:15:05 -0500359 ImageInfo info;
360 sk_sp<SkData> data = load_dds(GetResourcePath("images/flower-bc1.dds").c_str(), &info);
361 if (data) {
362 SkASSERT(info.fDim.equals(kImgWidthHeight, kImgWidthHeight));
Brian Salomon40a40622020-07-21 10:32:07 -0400363 SkASSERT(info.fMipmapped == GrMipmapped::kNo);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500364 SkASSERT(info.fCompressionType == SkImage::CompressionType::kBC1_RGB8_UNORM);
365
Adlai Holler4caa9352020-07-16 10:58:58 -0400366 fBC1Image = data_to_img(direct, std::move(data), info);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500367 } else {
368 SkDebugf("failed to load flower-bc1.dds\n");
Robert Phillipsa84caa32020-07-28 09:57:26 -0400369 return false;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500370 }
371 }
372
Robert Phillipsa84caa32020-07-28 09:57:26 -0400373 return true;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500374 }
375
Robert Phillipsa84caa32020-07-28 09:57:26 -0400376 void drawImage(SkCanvas* canvas, SkImage* image, int x, int y) {
Robert Phillipsd4f68312020-01-31 10:15:05 -0500377 if (!image) {
378 return;
379 }
380
381 bool isCompressed = false;
382 if (image->isTextureBacked()) {
Robert Phillipsa84caa32020-07-28 09:57:26 -0400383 GrRecordingContext* rContext = ((SkImage_GpuBase*) image)->context();
384 const GrCaps* caps = rContext->priv().caps();
Robert Phillipsd4f68312020-01-31 10:15:05 -0500385
386 GrTextureProxy* proxy = as_IB(image)->peekProxy();
387 isCompressed = caps->isFormatCompressed(proxy->backendFormat());
388 }
389
390 canvas->drawImage(image, x, y);
391
392 if (!isCompressed) {
393 // Make it obvious which drawImages used decompressed images
394 SkRect r = SkRect::MakeXYWH(x, y, kImgWidthHeight, kImgWidthHeight);
395 SkPaint paint;
396 paint.setColor(SK_ColorRED);
397 paint.setStyle(SkPaint::kStroke_Style);
Robert Phillipseb77a762020-02-03 11:43:20 -0500398 paint.setStrokeWidth(2.0f);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500399 canvas->drawRect(r, paint);
400 }
401 }
402
Robert Phillipsa84caa32020-07-28 09:57:26 -0400403 DrawResult onGpuSetup(GrDirectContext* dContext, SkString* errorMsg) override {
404 if (dContext && dContext->abandoned()) {
405 // This isn't a GpuGM so a null 'context' is okay but an abandoned context
406 // if forbidden.
407 return DrawResult::kSkip;
Adlai Holler4caa9352020-07-16 10:58:58 -0400408 }
Robert Phillipsd4f68312020-01-31 10:15:05 -0500409
Robert Phillipsa84caa32020-07-28 09:57:26 -0400410 if (!this->loadImages(dContext)) {
411 *errorMsg = "Failed to create images.";
412 return DrawResult::kFail;
413 }
Adlai Holler4caa9352020-07-16 10:58:58 -0400414
Robert Phillipsa84caa32020-07-28 09:57:26 -0400415 return DrawResult::kOk;
416 }
417
418 void onGpuTeardown() override {
419 fETC1Image = nullptr;
420 fBC1Image = nullptr;
421 }
422
423 void onDraw(SkCanvas* canvas) override {
424 SkASSERT(fETC1Image && fBC1Image);
425
426 this->drawImage(canvas, fETC1Image.get(), kPad, kPad);
427 this->drawImage(canvas, fBC1Image.get(), kImgWidthHeight + 2 * kPad, kPad);
Robert Phillipsd4f68312020-01-31 10:15:05 -0500428 }
429
430private:
431 static const int kImgWidthHeight = 128;
Robert Phillipseb77a762020-02-03 11:43:20 -0500432 static const int kPad = 4;
Robert Phillipsd4f68312020-01-31 10:15:05 -0500433
434 sk_sp<SkImage> fETC1Image;
435 sk_sp<SkImage> fBC1Image;
436
437 typedef GM INHERITED;
438};
439
440//////////////////////////////////////////////////////////////////////////////
441
442DEF_GM(return new ExoticFormatsGM;)
John Stilesa6841be2020-08-06 14:11:56 -0400443} // namespace skiagm