blob: e1992a035943bc90baedee5cda81e00279f5fe2e [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
robertphillipsc5035e72016-03-17 06:58:39 -07008#include "SkAutoPixmapStorage.h"
krajcevski2b310e42014-06-11 12:26:49 -07009#include "SkBitmap.h"
10#include "SkData.h"
11#include "SkEndian.h"
12#include "SkImageInfo.h"
scroggo565901d2015-12-10 10:44:13 -080013#include "SkTemplates.h"
krajcevski2b310e42014-06-11 12:26:49 -070014#include "SkTextureCompressor.h"
15#include "Test.h"
16
krajcevski95b1b3d2014-08-07 12:58:38 -070017// TODO: Create separate tests for RGB and RGBA data once
18// ASTC and ETC1 decompression is implemented.
19
20static bool decompresses_a8(SkTextureCompressor::Format fmt) {
21 switch (fmt) {
22 case SkTextureCompressor::kLATC_Format:
23 case SkTextureCompressor::kR11_EAC_Format:
24 return true;
25
26 default:
27 return false;
28 }
29}
30
31static bool compresses_a8(SkTextureCompressor::Format fmt) {
32 switch (fmt) {
33 case SkTextureCompressor::kLATC_Format:
34 case SkTextureCompressor::kR11_EAC_Format:
35 case SkTextureCompressor::kASTC_12x12_Format:
36 return true;
37
38 default:
39 return false;
40 }
41}
42
krajcevski2b310e42014-06-11 12:26:49 -070043/**
44 * Make sure that we properly fail when we don't have multiple of four image dimensions.
45 */
krajcevskib5294e82014-07-30 08:34:51 -070046DEF_TEST(CompressAlphaFailDimensions, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070047 static const int kWidth = 17;
48 static const int kHeight = 17;
krajcevskib5294e82014-07-30 08:34:51 -070049
50 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that we
51 // are violating those assumptions. And if we are, then we're also violating the
52 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 is
53 // also not divisible by 12. Our dimensions are prime, so any block dimension
54 // larger than 1 should fail.
55 REPORTER_ASSERT(reporter, kWidth % 4 != 0);
56 REPORTER_ASSERT(reporter, kHeight % 4 != 0);
krajcevski2b310e42014-06-11 12:26:49 -070057
reed41e010c2015-06-09 12:16:53 -070058 SkAutoPixmapStorage pixmap;
59 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
60 // leaving the pixels uninitialized, as they don't affect the test...
halcanary9d524f22016-03-29 09:03:52 -070061
krajcevskib5294e82014-07-30 08:34:51 -070062 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
63 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070064 if (!compresses_a8(fmt)) {
65 continue;
66 }
reed41e010c2015-06-09 12:16:53 -070067 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070068 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070069 }
krajcevski2b310e42014-06-11 12:26:49 -070070}
71
72/**
73 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070074 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070075 */
krajcevskib5294e82014-07-30 08:34:51 -070076DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070077 static const int kWidth = 12;
78 static const int kHeight = 12;
krajcevskib5294e82014-07-30 08:34:51 -070079
80 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
81 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
82 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
83 // need to be updated.
84 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
85 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070086
reed41e010c2015-06-09 12:16:53 -070087 SkAutoPixmapStorage pixmap;
88 pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight));
89 // leaving the pixels uninitialized, as they don't affect the test...
krajcevski2b310e42014-06-11 12:26:49 -070090
krajcevskib5294e82014-07-30 08:34:51 -070091 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
92 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070093 if (!compresses_a8(fmt)) {
94 continue;
95 }
reed41e010c2015-06-09 12:16:53 -070096 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070097 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070098 }
krajcevski2b310e42014-06-11 12:26:49 -070099}
100
101/**
krajcevski4ad76e32014-07-31 14:12:50 -0700102 * Make sure that if you compress a texture with alternating black/white pixels, and
103 * then decompress it, you get what you started with.
104 */
105DEF_TEST(CompressCheckerboard, reporter) {
krajcevski4ad76e32014-07-31 14:12:50 -0700106 static const int kWidth = 48; // We need the number to be divisible by both
107 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
krajcevski4ad76e32014-07-31 14:12:50 -0700108
109 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
110 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
111 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
112 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
113 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
114 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
115 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
116 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
117
reed41e010c2015-06-09 12:16:53 -0700118 SkAutoPixmapStorage pixmap;
119 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski4ad76e32014-07-31 14:12:50 -0700120
reed41e010c2015-06-09 12:16:53 -0700121 // Populate the pixels
krajcevski4ad76e32014-07-31 14:12:50 -0700122 {
reed41e010c2015-06-09 12:16:53 -0700123 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
bsalomon49f085d2014-09-05 13:34:00 -0700124 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700125 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700126 return;
127 }
128
129 for (int y = 0; y < kHeight; ++y) {
130 for (int x = 0; x < kWidth; ++x) {
131 if ((x ^ y) & 1) {
132 pixels[x] = 0xFF;
133 } else {
134 pixels[x] = 0;
135 }
136 }
reed41e010c2015-06-09 12:16:53 -0700137 pixels += pixmap.rowBytes();
krajcevski4ad76e32014-07-31 14:12:50 -0700138 }
139 }
140
scroggo565901d2015-12-10 10:44:13 -0800141 SkAutoTMalloc<uint8_t> decompMemory(kWidth*kHeight);
142 uint8_t* decompBuffer = decompMemory.get();
bsalomon49f085d2014-09-05 13:34:00 -0700143 REPORTER_ASSERT(reporter, decompBuffer);
halcanary96fcdcc2015-08-27 07:41:13 -0700144 if (nullptr == decompBuffer) {
krajcevski4ad76e32014-07-31 14:12:50 -0700145 return;
146 }
147
148 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
149 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
150
krajcevskie90c9002014-08-05 07:37:26 -0700151 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700152 // won't match the size and contents of the original.
krajcevski95b1b3d2014-08-07 12:58:38 -0700153 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
krajcevski4ad76e32014-07-31 14:12:50 -0700154 continue;
155 }
156
reed41e010c2015-06-09 12:16:53 -0700157 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
bsalomon49f085d2014-09-05 13:34:00 -0700158 REPORTER_ASSERT(reporter, data);
halcanary96fcdcc2015-08-27 07:41:13 -0700159 if (nullptr == data) {
krajcevski4ad76e32014-07-31 14:12:50 -0700160 continue;
161 }
162
163 bool decompResult =
164 SkTextureCompressor::DecompressBufferFromFormat(
165 decompBuffer, kWidth,
166 data->bytes(),
167 kWidth, kHeight, fmt);
168 REPORTER_ASSERT(reporter, decompResult);
169
reed41e010c2015-06-09 12:16:53 -0700170 const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr());
bsalomon49f085d2014-09-05 13:34:00 -0700171 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700172 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700173 continue;
174 }
175
176 for (int y = 0; y < kHeight; ++y) {
177 for (int x = 0; x < kWidth; ++x) {
reed41e010c2015-06-09 12:16:53 -0700178 bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
krajcevski4ad76e32014-07-31 14:12:50 -0700179 REPORTER_ASSERT(reporter, ok);
180 }
181 }
182 }
183}
184
185/**
krajcevski2b310e42014-06-11 12:26:49 -0700186 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
187 */
188DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700189
190 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
191 static const int kLATCEncodedBlockSize = 8;
192
krajcevski2b310e42014-06-11 12:26:49 -0700193 static const int kWidth = 8;
194 static const int kHeight = 8;
krajcevski2b310e42014-06-11 12:26:49 -0700195
reed41e010c2015-06-09 12:16:53 -0700196 SkAutoPixmapStorage pixmap;
197 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski2b310e42014-06-11 12:26:49 -0700198
krajcevskib5294e82014-07-30 08:34:51 -0700199 int latcDimX, latcDimY;
200 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
201
202 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
203 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
204 const size_t kSizeToBe =
205 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
206 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
207 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700208
209 for (int lum = 0; lum < 256; ++lum) {
reed41e010c2015-06-09 12:16:53 -0700210 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
krajcevski2b310e42014-06-11 12:26:49 -0700211 for (int i = 0; i < kWidth*kHeight; ++i) {
212 pixels[i] = lum;
213 }
krajcevski2b310e42014-06-11 12:26:49 -0700214
krajcevski2b310e42014-06-11 12:26:49 -0700215 SkAutoDataUnref latcData(
reed41e010c2015-06-09 12:16:53 -0700216 SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat));
bsalomon49f085d2014-09-05 13:34:00 -0700217 REPORTER_ASSERT(reporter, latcData);
halcanary96fcdcc2015-08-27 07:41:13 -0700218 if (nullptr == latcData) {
krajcevski4ad76e32014-07-31 14:12:50 -0700219 continue;
220 }
221
krajcevski2b310e42014-06-11 12:26:49 -0700222 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
223
krajcevskib5294e82014-07-30 08:34:51 -0700224 // Make sure that it all matches a given block encoding. Since we have
225 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
226 // an approximation scheme that optimizes for speed against coverage maps.
227 // That means that each palette in the encoded block is exactly the same,
228 // and that the three bits saved per pixel are computed from the top three
229 // bits of the luminance value.
230 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
pavel47eedcc2014-10-23 13:18:50 -0700231
232 // Quantize to three bits in the same way that we do our LATC compression:
233 // 1. Divide by two
234 // 2. Add 9
235 // 3. Divide by two
236 // 4. Approximate division by three twice
237 uint32_t quant = static_cast<uint32_t>(lum);
238 quant >>= 1; // 1
239 quant += 9; // 2
240 quant >>= 1; // 3
241
242 uint32_t a, b, c, ar, br, cr;
243
244 // First division by three
245 a = quant >> 2;
246 ar = (quant & 0x3) << 4;
247 b = quant >> 4;
248 br = (quant & 0xF) << 2;
249 c = quant >> 6;
250 cr = (quant & 0x3F);
251 quant = (a + b + c) + ((ar + br + cr) >> 6);
252
253 // Second division by three
254 a = quant >> 2;
255 ar = (quant & 0x3) << 4;
256 b = quant >> 4;
257 br = (quant & 0xF) << 2;
258 c = quant >> 6;
259 cr = (quant & 0x3F);
260 quant = (a + b + c) + ((ar + br + cr) >> 6);
261
262 const uint64_t kIndex = kIndexEncodingMap[quant];
263
krajcevskib5294e82014-07-30 08:34:51 -0700264 const uint64_t kConstColorEncoding =
265 SkEndian_SwapLE64(
266 255 |
267 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
268 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
269 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
270 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
271
krajcevski2b310e42014-06-11 12:26:49 -0700272 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700273 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700274 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
275 }
276 }
277}