blob: 568d4d14b6b71ca57091abc954f2f4896265ada3 [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) {
krajcevski2b310e42014-06-11 12:26:49 -070045 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070046 static const int kWidth = 17;
47 static const int kHeight = 17;
krajcevski2b310e42014-06-11 12:26:49 -070048 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
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
58 bool setInfoSuccess = bitmap.setInfo(info);
59 REPORTER_ASSERT(reporter, setInfoSuccess);
60
reed84825042014-09-02 12:50:45 -070061 bitmap.allocPixels(info);
krajcevski2b310e42014-06-11 12:26:49 -070062 bitmap.unlockPixels();
krajcevskib5294e82014-07-30 08:34:51 -070063
64 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
65 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070066 if (!compresses_a8(fmt)) {
67 continue;
68 }
krajcevskib5294e82014-07-30 08:34:51 -070069 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
70 REPORTER_ASSERT(reporter, NULL == data);
71 }
krajcevski2b310e42014-06-11 12:26:49 -070072}
73
74/**
75 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070076 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070077 */
krajcevskib5294e82014-07-30 08:34:51 -070078DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070079 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070080 static const int kWidth = 12;
81 static const int kHeight = 12;
krajcevski2b310e42014-06-11 12:26:49 -070082 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070083
84 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
85 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
86 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
87 // need to be updated.
88 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
89 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070090
91 bool setInfoSuccess = bitmap.setInfo(info);
92 REPORTER_ASSERT(reporter, setInfoSuccess);
93
reed84825042014-09-02 12:50:45 -070094 bitmap.allocPixels(info);
krajcevski2b310e42014-06-11 12:26:49 -070095 bitmap.unlockPixels();
96
krajcevskib5294e82014-07-30 08:34:51 -070097 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
98 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070099 if (!compresses_a8(fmt)) {
100 continue;
101 }
krajcevskib5294e82014-07-30 08:34:51 -0700102 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
103 REPORTER_ASSERT(reporter, NULL == data);
104 }
krajcevski2b310e42014-06-11 12:26:49 -0700105}
106
107/**
krajcevski4ad76e32014-07-31 14:12:50 -0700108 * Make sure that if you compress a texture with alternating black/white pixels, and
109 * then decompress it, you get what you started with.
110 */
111DEF_TEST(CompressCheckerboard, reporter) {
112 SkBitmap bitmap;
113 static const int kWidth = 48; // We need the number to be divisible by both
114 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
115 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
116
117 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
118 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
119 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
120 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
121 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
122 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
123 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
124 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
125
126 bool setInfoSuccess = bitmap.setInfo(info);
127 REPORTER_ASSERT(reporter, setInfoSuccess);
128
reed84825042014-09-02 12:50:45 -0700129 bitmap.allocPixels(info);
krajcevski4ad76e32014-07-31 14:12:50 -0700130 bitmap.unlockPixels();
131
132 // Populate bitmap
133 {
134 SkAutoLockPixels alp(bitmap);
135
136 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
bsalomon49f085d2014-09-05 13:34:00 -0700137 REPORTER_ASSERT(reporter, pixels);
krajcevski4ad76e32014-07-31 14:12:50 -0700138 if (NULL == pixels) {
139 return;
140 }
141
142 for (int y = 0; y < kHeight; ++y) {
143 for (int x = 0; x < kWidth; ++x) {
144 if ((x ^ y) & 1) {
145 pixels[x] = 0xFF;
146 } else {
147 pixels[x] = 0;
148 }
149 }
150 pixels += bitmap.rowBytes();
151 }
152 }
153
154 SkAutoMalloc decompMemory(kWidth*kHeight);
155 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get());
bsalomon49f085d2014-09-05 13:34:00 -0700156 REPORTER_ASSERT(reporter, decompBuffer);
krajcevski4ad76e32014-07-31 14:12:50 -0700157 if (NULL == decompBuffer) {
158 return;
159 }
160
161 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
162 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
163
krajcevskie90c9002014-08-05 07:37:26 -0700164 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700165 // won't match the size and contents of the original.
krajcevski95b1b3d2014-08-07 12:58:38 -0700166 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
krajcevski4ad76e32014-07-31 14:12:50 -0700167 continue;
168 }
169
170 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
bsalomon49f085d2014-09-05 13:34:00 -0700171 REPORTER_ASSERT(reporter, data);
krajcevski4ad76e32014-07-31 14:12:50 -0700172 if (NULL == data) {
173 continue;
174 }
175
176 bool decompResult =
177 SkTextureCompressor::DecompressBufferFromFormat(
178 decompBuffer, kWidth,
179 data->bytes(),
180 kWidth, kHeight, fmt);
181 REPORTER_ASSERT(reporter, decompResult);
182
183 SkAutoLockPixels alp(bitmap);
184 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
bsalomon49f085d2014-09-05 13:34:00 -0700185 REPORTER_ASSERT(reporter, pixels);
krajcevski4ad76e32014-07-31 14:12:50 -0700186 if (NULL == pixels) {
187 continue;
188 }
189
190 for (int y = 0; y < kHeight; ++y) {
191 for (int x = 0; x < kWidth; ++x) {
192 bool ok = pixels[y*bitmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
193 REPORTER_ASSERT(reporter, ok);
194 }
195 }
196 }
197}
198
199/**
krajcevski2b310e42014-06-11 12:26:49 -0700200 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
201 */
202DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700203
204 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
205 static const int kLATCEncodedBlockSize = 8;
206
krajcevski2b310e42014-06-11 12:26:49 -0700207 SkBitmap bitmap;
208 static const int kWidth = 8;
209 static const int kHeight = 8;
210 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
211
212 bool setInfoSuccess = bitmap.setInfo(info);
213 REPORTER_ASSERT(reporter, setInfoSuccess);
214
reed84825042014-09-02 12:50:45 -0700215 bitmap.allocPixels(info);
krajcevski2b310e42014-06-11 12:26:49 -0700216 bitmap.unlockPixels();
217
krajcevskib5294e82014-07-30 08:34:51 -0700218 int latcDimX, latcDimY;
219 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
220
221 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
222 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
223 const size_t kSizeToBe =
224 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
225 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
226 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700227
228 for (int lum = 0; lum < 256; ++lum) {
229 bitmap.lockPixels();
230 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
bsalomon49f085d2014-09-05 13:34:00 -0700231 REPORTER_ASSERT(reporter, pixels);
krajcevski4ad76e32014-07-31 14:12:50 -0700232 if (NULL == pixels) {
233 bitmap.unlockPixels();
234 continue;
235 }
krajcevski2b310e42014-06-11 12:26:49 -0700236
237 for (int i = 0; i < kWidth*kHeight; ++i) {
238 pixels[i] = lum;
239 }
240 bitmap.unlockPixels();
241
krajcevski2b310e42014-06-11 12:26:49 -0700242 SkAutoDataUnref latcData(
243 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat));
bsalomon49f085d2014-09-05 13:34:00 -0700244 REPORTER_ASSERT(reporter, latcData);
krajcevski4ad76e32014-07-31 14:12:50 -0700245 if (NULL == latcData) {
246 continue;
247 }
248
krajcevski2b310e42014-06-11 12:26:49 -0700249 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
250
krajcevskib5294e82014-07-30 08:34:51 -0700251 // Make sure that it all matches a given block encoding. Since we have
252 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
253 // an approximation scheme that optimizes for speed against coverage maps.
254 // That means that each palette in the encoded block is exactly the same,
255 // and that the three bits saved per pixel are computed from the top three
256 // bits of the luminance value.
257 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
pavel47eedcc2014-10-23 13:18:50 -0700258
259 // Quantize to three bits in the same way that we do our LATC compression:
260 // 1. Divide by two
261 // 2. Add 9
262 // 3. Divide by two
263 // 4. Approximate division by three twice
264 uint32_t quant = static_cast<uint32_t>(lum);
265 quant >>= 1; // 1
266 quant += 9; // 2
267 quant >>= 1; // 3
268
269 uint32_t a, b, c, ar, br, cr;
270
271 // First division by three
272 a = quant >> 2;
273 ar = (quant & 0x3) << 4;
274 b = quant >> 4;
275 br = (quant & 0xF) << 2;
276 c = quant >> 6;
277 cr = (quant & 0x3F);
278 quant = (a + b + c) + ((ar + br + cr) >> 6);
279
280 // Second division by three
281 a = quant >> 2;
282 ar = (quant & 0x3) << 4;
283 b = quant >> 4;
284 br = (quant & 0xF) << 2;
285 c = quant >> 6;
286 cr = (quant & 0x3F);
287 quant = (a + b + c) + ((ar + br + cr) >> 6);
288
289 const uint64_t kIndex = kIndexEncodingMap[quant];
290
krajcevskib5294e82014-07-30 08:34:51 -0700291 const uint64_t kConstColorEncoding =
292 SkEndian_SwapLE64(
293 255 |
294 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
295 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
296 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
297 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
298
krajcevski2b310e42014-06-11 12:26:49 -0700299 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700300 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700301 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
302 }
303 }
304}