blob: d2e6db209b6a6d3af1258dddd06190932ac3c7df [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"
12#include "SkTextureCompressor.h"
13#include "Test.h"
14
krajcevski95b1b3d2014-08-07 12:58:38 -070015// TODO: Create separate tests for RGB and RGBA data once
16// ASTC and ETC1 decompression is implemented.
17
18static bool decompresses_a8(SkTextureCompressor::Format fmt) {
19 switch (fmt) {
20 case SkTextureCompressor::kLATC_Format:
21 case SkTextureCompressor::kR11_EAC_Format:
22 return true;
23
24 default:
25 return false;
26 }
27}
28
29static bool compresses_a8(SkTextureCompressor::Format fmt) {
30 switch (fmt) {
31 case SkTextureCompressor::kLATC_Format:
32 case SkTextureCompressor::kR11_EAC_Format:
33 case SkTextureCompressor::kASTC_12x12_Format:
34 return true;
35
36 default:
37 return false;
38 }
39}
40
krajcevski2b310e42014-06-11 12:26:49 -070041/**
42 * Make sure that we properly fail when we don't have multiple of four image dimensions.
43 */
krajcevskib5294e82014-07-30 08:34:51 -070044DEF_TEST(CompressAlphaFailDimensions, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070045 static const int kWidth = 17;
46 static const int kHeight = 17;
krajcevskib5294e82014-07-30 08:34:51 -070047
48 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that we
49 // are violating those assumptions. And if we are, then we're also violating the
50 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 is
51 // also not divisible by 12. Our dimensions are prime, so any block dimension
52 // larger than 1 should fail.
53 REPORTER_ASSERT(reporter, kWidth % 4 != 0);
54 REPORTER_ASSERT(reporter, kHeight % 4 != 0);
krajcevski2b310e42014-06-11 12:26:49 -070055
reed41e010c2015-06-09 12:16:53 -070056 SkAutoPixmapStorage pixmap;
57 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
58 // leaving the pixels uninitialized, as they don't affect the test...
krajcevskib5294e82014-07-30 08:34:51 -070059
60 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
61 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070062 if (!compresses_a8(fmt)) {
63 continue;
64 }
reed41e010c2015-06-09 12:16:53 -070065 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070066 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070067 }
krajcevski2b310e42014-06-11 12:26:49 -070068}
69
70/**
71 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070072 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070073 */
krajcevskib5294e82014-07-30 08:34:51 -070074DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -070075 static const int kWidth = 12;
76 static const int kHeight = 12;
krajcevskib5294e82014-07-30 08:34:51 -070077
78 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
79 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
80 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
81 // need to be updated.
82 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
83 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070084
reed41e010c2015-06-09 12:16:53 -070085 SkAutoPixmapStorage pixmap;
86 pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight));
87 // leaving the pixels uninitialized, as they don't affect the test...
krajcevski2b310e42014-06-11 12:26:49 -070088
krajcevskib5294e82014-07-30 08:34:51 -070089 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
90 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070091 if (!compresses_a8(fmt)) {
92 continue;
93 }
reed41e010c2015-06-09 12:16:53 -070094 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
halcanary96fcdcc2015-08-27 07:41:13 -070095 REPORTER_ASSERT(reporter, nullptr == data);
krajcevskib5294e82014-07-30 08:34:51 -070096 }
krajcevski2b310e42014-06-11 12:26:49 -070097}
98
99/**
krajcevski4ad76e32014-07-31 14:12:50 -0700100 * Make sure that if you compress a texture with alternating black/white pixels, and
101 * then decompress it, you get what you started with.
102 */
103DEF_TEST(CompressCheckerboard, reporter) {
krajcevski4ad76e32014-07-31 14:12:50 -0700104 static const int kWidth = 48; // We need the number to be divisible by both
105 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
krajcevski4ad76e32014-07-31 14:12:50 -0700106
107 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
108 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
109 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
110 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
111 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
112 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
113 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
114 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
115
reed41e010c2015-06-09 12:16:53 -0700116 SkAutoPixmapStorage pixmap;
117 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski4ad76e32014-07-31 14:12:50 -0700118
reed41e010c2015-06-09 12:16:53 -0700119 // Populate the pixels
krajcevski4ad76e32014-07-31 14:12:50 -0700120 {
reed41e010c2015-06-09 12:16:53 -0700121 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
bsalomon49f085d2014-09-05 13:34:00 -0700122 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700123 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700124 return;
125 }
126
127 for (int y = 0; y < kHeight; ++y) {
128 for (int x = 0; x < kWidth; ++x) {
129 if ((x ^ y) & 1) {
130 pixels[x] = 0xFF;
131 } else {
132 pixels[x] = 0;
133 }
134 }
reed41e010c2015-06-09 12:16:53 -0700135 pixels += pixmap.rowBytes();
krajcevski4ad76e32014-07-31 14:12:50 -0700136 }
137 }
138
139 SkAutoMalloc decompMemory(kWidth*kHeight);
140 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get());
bsalomon49f085d2014-09-05 13:34:00 -0700141 REPORTER_ASSERT(reporter, decompBuffer);
halcanary96fcdcc2015-08-27 07:41:13 -0700142 if (nullptr == decompBuffer) {
krajcevski4ad76e32014-07-31 14:12:50 -0700143 return;
144 }
145
146 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
147 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
148
krajcevskie90c9002014-08-05 07:37:26 -0700149 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700150 // won't match the size and contents of the original.
krajcevski95b1b3d2014-08-07 12:58:38 -0700151 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
krajcevski4ad76e32014-07-31 14:12:50 -0700152 continue;
153 }
154
reed41e010c2015-06-09 12:16:53 -0700155 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt));
bsalomon49f085d2014-09-05 13:34:00 -0700156 REPORTER_ASSERT(reporter, data);
halcanary96fcdcc2015-08-27 07:41:13 -0700157 if (nullptr == data) {
krajcevski4ad76e32014-07-31 14:12:50 -0700158 continue;
159 }
160
161 bool decompResult =
162 SkTextureCompressor::DecompressBufferFromFormat(
163 decompBuffer, kWidth,
164 data->bytes(),
165 kWidth, kHeight, fmt);
166 REPORTER_ASSERT(reporter, decompResult);
167
reed41e010c2015-06-09 12:16:53 -0700168 const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr());
bsalomon49f085d2014-09-05 13:34:00 -0700169 REPORTER_ASSERT(reporter, pixels);
halcanary96fcdcc2015-08-27 07:41:13 -0700170 if (nullptr == pixels) {
krajcevski4ad76e32014-07-31 14:12:50 -0700171 continue;
172 }
173
174 for (int y = 0; y < kHeight; ++y) {
175 for (int x = 0; x < kWidth; ++x) {
reed41e010c2015-06-09 12:16:53 -0700176 bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
krajcevski4ad76e32014-07-31 14:12:50 -0700177 REPORTER_ASSERT(reporter, ok);
178 }
179 }
180 }
181}
182
183/**
krajcevski2b310e42014-06-11 12:26:49 -0700184 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
185 */
186DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700187
188 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
189 static const int kLATCEncodedBlockSize = 8;
190
krajcevski2b310e42014-06-11 12:26:49 -0700191 static const int kWidth = 8;
192 static const int kHeight = 8;
krajcevski2b310e42014-06-11 12:26:49 -0700193
reed41e010c2015-06-09 12:16:53 -0700194 SkAutoPixmapStorage pixmap;
195 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight));
krajcevski2b310e42014-06-11 12:26:49 -0700196
krajcevskib5294e82014-07-30 08:34:51 -0700197 int latcDimX, latcDimY;
198 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
199
200 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
201 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
202 const size_t kSizeToBe =
203 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
204 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
205 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700206
207 for (int lum = 0; lum < 256; ++lum) {
reed41e010c2015-06-09 12:16:53 -0700208 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr());
krajcevski2b310e42014-06-11 12:26:49 -0700209 for (int i = 0; i < kWidth*kHeight; ++i) {
210 pixels[i] = lum;
211 }
krajcevski2b310e42014-06-11 12:26:49 -0700212
krajcevski2b310e42014-06-11 12:26:49 -0700213 SkAutoDataUnref latcData(
reed41e010c2015-06-09 12:16:53 -0700214 SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat));
bsalomon49f085d2014-09-05 13:34:00 -0700215 REPORTER_ASSERT(reporter, latcData);
halcanary96fcdcc2015-08-27 07:41:13 -0700216 if (nullptr == latcData) {
krajcevski4ad76e32014-07-31 14:12:50 -0700217 continue;
218 }
219
krajcevski2b310e42014-06-11 12:26:49 -0700220 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
221
krajcevskib5294e82014-07-30 08:34:51 -0700222 // Make sure that it all matches a given block encoding. Since we have
223 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
224 // an approximation scheme that optimizes for speed against coverage maps.
225 // That means that each palette in the encoded block is exactly the same,
226 // and that the three bits saved per pixel are computed from the top three
227 // bits of the luminance value.
228 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
pavel47eedcc2014-10-23 13:18:50 -0700229
230 // Quantize to three bits in the same way that we do our LATC compression:
231 // 1. Divide by two
232 // 2. Add 9
233 // 3. Divide by two
234 // 4. Approximate division by three twice
235 uint32_t quant = static_cast<uint32_t>(lum);
236 quant >>= 1; // 1
237 quant += 9; // 2
238 quant >>= 1; // 3
239
240 uint32_t a, b, c, ar, br, cr;
241
242 // First division by three
243 a = quant >> 2;
244 ar = (quant & 0x3) << 4;
245 b = quant >> 4;
246 br = (quant & 0xF) << 2;
247 c = quant >> 6;
248 cr = (quant & 0x3F);
249 quant = (a + b + c) + ((ar + br + cr) >> 6);
250
251 // Second division by three
252 a = quant >> 2;
253 ar = (quant & 0x3) << 4;
254 b = quant >> 4;
255 br = (quant & 0xF) << 2;
256 c = quant >> 6;
257 cr = (quant & 0x3F);
258 quant = (a + b + c) + ((ar + br + cr) >> 6);
259
260 const uint64_t kIndex = kIndexEncodingMap[quant];
261
krajcevskib5294e82014-07-30 08:34:51 -0700262 const uint64_t kConstColorEncoding =
263 SkEndian_SwapLE64(
264 255 |
265 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
266 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
267 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
268 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
269
krajcevski2b310e42014-06-11 12:26:49 -0700270 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700271 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700272 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
273 }
274 }
275}