blob: 18afebeff2c39175724a0ee5c492448a17b2ffdb [file] [log] [blame]
krajcevski2b310e42014-06-11 12:26:49 -07001/*
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 "SkBitmap.h"
9#include "SkData.h"
10#include "SkEndian.h"
11#include "SkImageInfo.h"
scroggo565901d2015-12-10 10:44:13 -080012#include "SkTemplates.h"
krajcevski2b310e42014-06-11 12:26:49 -070013#include "SkTextureCompressor.h"
14#include "Test.h"
15
krajcevski95b1b3d2014-08-07 12:58:38 -070016// TODO: Create separate tests for RGB and RGBA data once
17// ASTC and ETC1 decompression is implemented.
18
19static bool decompresses_a8(SkTextureCompressor::Format fmt) {
20 switch (fmt) {
21 case SkTextureCompressor::kLATC_Format:
22 case SkTextureCompressor::kR11_EAC_Format:
23 return true;
24
25 default:
26 return false;
27 }
28}
29
30static bool compresses_a8(SkTextureCompressor::Format fmt) {
31 switch (fmt) {
32 case SkTextureCompressor::kLATC_Format:
33 case SkTextureCompressor::kR11_EAC_Format:
34 case SkTextureCompressor::kASTC_12x12_Format:
35 return true;
36
37 default:
38 return false;
39 }
40}
41
krajcevski2b310e42014-06-11 12:26:49 -070042/**
43 * Make sure that we properly fail when we don't have multiple of four image dimensions.
44 */
krajcevskib5294e82014-07-30 08:34:51 -070045DEF_TEST(CompressAlphaFailDimensions, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070046 static const int kWidth = 17;
47 static const int kHeight = 17;
krajcevskib5294e82014-07-30 08:34:51 -070048
49 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that we
50 // are violating those assumptions. And if we are, then we're also violating the
51 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 is
52 // also not divisible by 12. Our dimensions are prime, so any block dimension
53 // larger than 1 should fail.
54 REPORTER_ASSERT(reporter, kWidth % 4 != 0);
55 REPORTER_ASSERT(reporter, kHeight % 4 != 0);
krajcevski2b310e42014-06-11 12:26:49 -070056
reed41e010c2015-06-09 12:16:53 -070057 SkAutoPixmapStorage pixmap;
58 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
59 // leaving the pixels uninitialized, as they don't affect the test...
krajcevskib5294e82014-07-30 08:34:51 -070060
61 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
62 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070063 if (!compresses_a8(fmt)) {
64 continue;
65 }
reed41e010c2015-06-09 12:16:53 -070066 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070067 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070068 }
krajcevski2b310e42014-06-11 12:26:49 -070069}
70
71/**
72 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070073 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070074 */
krajcevskib5294e82014-07-30 08:34:51 -070075DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070076 static const int kWidth = 12;
77 static const int kHeight = 12;
krajcevskib5294e82014-07-30 08:34:51 -070078
79 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
80 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
81 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
82 // need to be updated.
83 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
84 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070085
reed41e010c2015-06-09 12:16:53 -070086 SkAutoPixmapStorage pixmap;
87 pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight));
88 // leaving the pixels uninitialized, as they don't affect the test...
krajcevski2b310e42014-06-11 12:26:49 -070089
krajcevskib5294e82014-07-30 08:34:51 -070090 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
91 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070092 if (!compresses_a8(fmt)) {
93 continue;
94 }
reed41e010c2015-06-09 12:16:53 -070095 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070096 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070097 }
krajcevski2b310e42014-06-11 12:26:49 -070098}
99
100/**
krajcevski4ad76e32014-07-31 14:12:50 -0700101 * Make sure that if you compress a texture with alternating black/white pixels, and
102 * then decompress it, you get what you started with.
103 */
104DEF_TEST(CompressCheckerboard, reporter) {
krajcevski4ad76e32014-07-31 14:12:50 -0700105 static const int kWidth = 48; // We need the number to be divisible by both
106 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
krajcevski4ad76e32014-07-31 14:12:50 -0700107
108 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
109 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
110 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
111 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
112 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
113 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
114 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
115 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
116
reed41e010c2015-06-09 12:16:53 -0700117 SkAutoPixmapStorage pixmap;
118 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski4ad76e32014-07-31 14:12:50 -0700119
reed41e010c2015-06-09 12:16:53 -0700120 // Populate the pixels
krajcevski4ad76e32014-07-31 14:12:50 -0700121 {
reed41e010c2015-06-09 12:16:53 -0700122 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
bsalomon49f085d2014-09-05 13:34:00 -0700123 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700124 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700125 return;
126 }
127
128 for (int y = 0; y < kHeight; ++y) {
129 for (int x = 0; x < kWidth; ++x) {
130 if ((x ^ y) & 1) {
131 pixels[x] = 0xFF;
132 } else {
133 pixels[x] = 0;
134 }
135 }
reed41e010c2015-06-09 12:16:53 -0700136 pixels += pixmap.rowBytes();
krajcevski4ad76e32014-07-31 14:12:50 -0700137 }
138 }
139
scroggo565901d2015-12-10 10:44:13 -0800140 SkAutoTMalloc<uint8_t> decompMemory(kWidth*kHeight);
141 uint8_t* decompBuffer = decompMemory.get();
bsalomon49f085d2014-09-05 13:34:00 -0700142 REPORTER_ASSERT(reporter, decompBuffer);
halcanary96fcdcc2015-08-27 07:41:13 -0700143 if (nullptr == decompBuffer) {
krajcevski4ad76e32014-07-31 14:12:50 -0700144 return;
145 }
146
147 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
148 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
149
krajcevskie90c9002014-08-05 07:37:26 -0700150 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700151 // won't match the size and contents of the original.
krajcevski95b1b3d2014-08-07 12:58:38 -0700152 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
krajcevski4ad76e32014-07-31 14:12:50 -0700153 continue;
154 }
155
reed41e010c2015-06-09 12:16:53 -0700156 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
bsalomon49f085d2014-09-05 13:34:00 -0700157 REPORTER_ASSERT(reporter, data);
halcanary96fcdcc2015-08-27 07:41:13 -0700158 if (nullptr == data) {
krajcevski4ad76e32014-07-31 14:12:50 -0700159 continue;
160 }
161
162 bool decompResult =
163 SkTextureCompressor::DecompressBufferFromFormat(
164 decompBuffer, kWidth,
165 data->bytes(),
166 kWidth, kHeight, fmt);
167 REPORTER_ASSERT(reporter, decompResult);
168
reed41e010c2015-06-09 12:16:53 -0700169 const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr());
bsalomon49f085d2014-09-05 13:34:00 -0700170 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700171 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700172 continue;
173 }
174
175 for (int y = 0; y < kHeight; ++y) {
176 for (int x = 0; x < kWidth; ++x) {
reed41e010c2015-06-09 12:16:53 -0700177 bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
krajcevski4ad76e32014-07-31 14:12:50 -0700178 REPORTER_ASSERT(reporter, ok);
179 }
180 }
181 }
182}
183
184/**
krajcevski2b310e42014-06-11 12:26:49 -0700185 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
186 */
187DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700188
189 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
190 static const int kLATCEncodedBlockSize = 8;
191
krajcevski2b310e42014-06-11 12:26:49 -0700192 static const int kWidth = 8;
193 static const int kHeight = 8;
krajcevski2b310e42014-06-11 12:26:49 -0700194
reed41e010c2015-06-09 12:16:53 -0700195 SkAutoPixmapStorage pixmap;
196 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski2b310e42014-06-11 12:26:49 -0700197
krajcevskib5294e82014-07-30 08:34:51 -0700198 int latcDimX, latcDimY;
199 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
200
201 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
202 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
203 const size_t kSizeToBe =
204 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
205 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
206 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700207
208 for (int lum = 0; lum < 256; ++lum) {
reed41e010c2015-06-09 12:16:53 -0700209 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
krajcevski2b310e42014-06-11 12:26:49 -0700210 for (int i = 0; i < kWidth*kHeight; ++i) {
211 pixels[i] = lum;
212 }
krajcevski2b310e42014-06-11 12:26:49 -0700213
krajcevski2b310e42014-06-11 12:26:49 -0700214 SkAutoDataUnref latcData(
reed41e010c2015-06-09 12:16:53 -0700215 SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat));
bsalomon49f085d2014-09-05 13:34:00 -0700216 REPORTER_ASSERT(reporter, latcData);
halcanary96fcdcc2015-08-27 07:41:13 -0700217 if (nullptr == latcData) {
krajcevski4ad76e32014-07-31 14:12:50 -0700218 continue;
219 }
220
krajcevski2b310e42014-06-11 12:26:49 -0700221 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
222
krajcevskib5294e82014-07-30 08:34:51 -0700223 // Make sure that it all matches a given block encoding. Since we have
224 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
225 // an approximation scheme that optimizes for speed against coverage maps.
226 // That means that each palette in the encoded block is exactly the same,
227 // and that the three bits saved per pixel are computed from the top three
228 // bits of the luminance value.
229 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
pavel47eedcc2014-10-23 13:18:50 -0700230
231 // Quantize to three bits in the same way that we do our LATC compression:
232 // 1. Divide by two
233 // 2. Add 9
234 // 3. Divide by two
235 // 4. Approximate division by three twice
236 uint32_t quant = static_cast<uint32_t>(lum);
237 quant >>= 1; // 1
238 quant += 9; // 2
239 quant >>= 1; // 3
240
241 uint32_t a, b, c, ar, br, cr;
242
243 // First division by three
244 a = quant >> 2;
245 ar = (quant & 0x3) << 4;
246 b = quant >> 4;
247 br = (quant & 0xF) << 2;
248 c = quant >> 6;
249 cr = (quant & 0x3F);
250 quant = (a + b + c) + ((ar + br + cr) >> 6);
251
252 // Second division by three
253 a = quant >> 2;
254 ar = (quant & 0x3) << 4;
255 b = quant >> 4;
256 br = (quant & 0xF) << 2;
257 c = quant >> 6;
258 cr = (quant & 0x3F);
259 quant = (a + b + c) + ((ar + br + cr) >> 6);
260
261 const uint64_t kIndex = kIndexEncodingMap[quant];
262
krajcevskib5294e82014-07-30 08:34:51 -0700263 const uint64_t kConstColorEncoding =
264 SkEndian_SwapLE64(
265 255 |
266 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
267 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
268 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
269 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
270
krajcevski2b310e42014-06-11 12:26:49 -0700271 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700272 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700273 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
274 }
275 }
276}