blob: da7a87bd41d36530e5b48283f2cd675a59f45e33 [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
61 bool allocPixelsSuccess = bitmap.allocPixels(info);
62 REPORTER_ASSERT(reporter, allocPixelsSuccess);
63 bitmap.unlockPixels();
krajcevskib5294e82014-07-30 08:34:51 -070064
65 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
66 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -070067 if (!compresses_a8(fmt)) {
68 continue;
69 }
krajcevskib5294e82014-07-30 08:34:51 -070070 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
71 REPORTER_ASSERT(reporter, NULL == data);
72 }
krajcevski2b310e42014-06-11 12:26:49 -070073}
74
75/**
76 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070077 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070078 */
krajcevskib5294e82014-07-30 08:34:51 -070079DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070080 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070081 static const int kWidth = 12;
82 static const int kHeight = 12;
krajcevski2b310e42014-06-11 12:26:49 -070083 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070084
85 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
86 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
87 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
88 // need to be updated.
89 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
90 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070091
92 bool setInfoSuccess = bitmap.setInfo(info);
93 REPORTER_ASSERT(reporter, setInfoSuccess);
94
95 bool allocPixelsSuccess = bitmap.allocPixels(info);
96 REPORTER_ASSERT(reporter, allocPixelsSuccess);
97 bitmap.unlockPixels();
98
krajcevskib5294e82014-07-30 08:34:51 -070099 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
100 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
krajcevski95b1b3d2014-08-07 12:58:38 -0700101 if (!compresses_a8(fmt)) {
102 continue;
103 }
krajcevskib5294e82014-07-30 08:34:51 -0700104 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
105 REPORTER_ASSERT(reporter, NULL == data);
106 }
krajcevski2b310e42014-06-11 12:26:49 -0700107}
108
109/**
krajcevski4ad76e32014-07-31 14:12:50 -0700110 * Make sure that if you compress a texture with alternating black/white pixels, and
111 * then decompress it, you get what you started with.
112 */
113DEF_TEST(CompressCheckerboard, reporter) {
114 SkBitmap bitmap;
115 static const int kWidth = 48; // We need the number to be divisible by both
116 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
117 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
118
119 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
120 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
121 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
122 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
123 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
124 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
125 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
126 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
127
128 bool setInfoSuccess = bitmap.setInfo(info);
129 REPORTER_ASSERT(reporter, setInfoSuccess);
130
131 bool allocPixelsSuccess = bitmap.allocPixels(info);
132 REPORTER_ASSERT(reporter, allocPixelsSuccess);
133 bitmap.unlockPixels();
134
135 // Populate bitmap
136 {
137 SkAutoLockPixels alp(bitmap);
138
139 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
140 REPORTER_ASSERT(reporter, NULL != pixels);
141 if (NULL == pixels) {
142 return;
143 }
144
145 for (int y = 0; y < kHeight; ++y) {
146 for (int x = 0; x < kWidth; ++x) {
147 if ((x ^ y) & 1) {
148 pixels[x] = 0xFF;
149 } else {
150 pixels[x] = 0;
151 }
152 }
153 pixels += bitmap.rowBytes();
154 }
155 }
156
157 SkAutoMalloc decompMemory(kWidth*kHeight);
158 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get());
159 REPORTER_ASSERT(reporter, NULL != decompBuffer);
160 if (NULL == decompBuffer) {
161 return;
162 }
163
164 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
165 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
166
krajcevskie90c9002014-08-05 07:37:26 -0700167 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700168 // won't match the size and contents of the original.
krajcevski95b1b3d2014-08-07 12:58:38 -0700169 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
krajcevski4ad76e32014-07-31 14:12:50 -0700170 continue;
171 }
172
173 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
174 REPORTER_ASSERT(reporter, NULL != data);
175 if (NULL == data) {
176 continue;
177 }
178
179 bool decompResult =
180 SkTextureCompressor::DecompressBufferFromFormat(
181 decompBuffer, kWidth,
182 data->bytes(),
183 kWidth, kHeight, fmt);
184 REPORTER_ASSERT(reporter, decompResult);
185
186 SkAutoLockPixels alp(bitmap);
187 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
188 REPORTER_ASSERT(reporter, NULL != pixels);
189 if (NULL == pixels) {
190 continue;
191 }
192
193 for (int y = 0; y < kHeight; ++y) {
194 for (int x = 0; x < kWidth; ++x) {
195 bool ok = pixels[y*bitmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
196 REPORTER_ASSERT(reporter, ok);
197 }
198 }
199 }
200}
201
202/**
krajcevski2b310e42014-06-11 12:26:49 -0700203 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
204 */
205DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700206
207 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
208 static const int kLATCEncodedBlockSize = 8;
209
krajcevski2b310e42014-06-11 12:26:49 -0700210 SkBitmap bitmap;
211 static const int kWidth = 8;
212 static const int kHeight = 8;
213 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
214
215 bool setInfoSuccess = bitmap.setInfo(info);
216 REPORTER_ASSERT(reporter, setInfoSuccess);
217
218 bool allocPixelsSuccess = bitmap.allocPixels(info);
219 REPORTER_ASSERT(reporter, allocPixelsSuccess);
220 bitmap.unlockPixels();
221
krajcevskib5294e82014-07-30 08:34:51 -0700222 int latcDimX, latcDimY;
223 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
224
225 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
226 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
227 const size_t kSizeToBe =
228 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
229 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
230 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700231
232 for (int lum = 0; lum < 256; ++lum) {
233 bitmap.lockPixels();
234 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
235 REPORTER_ASSERT(reporter, NULL != pixels);
krajcevski4ad76e32014-07-31 14:12:50 -0700236 if (NULL == pixels) {
237 bitmap.unlockPixels();
238 continue;
239 }
krajcevski2b310e42014-06-11 12:26:49 -0700240
241 for (int i = 0; i < kWidth*kHeight; ++i) {
242 pixels[i] = lum;
243 }
244 bitmap.unlockPixels();
245
krajcevski2b310e42014-06-11 12:26:49 -0700246 SkAutoDataUnref latcData(
247 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat));
248 REPORTER_ASSERT(reporter, NULL != latcData);
krajcevski4ad76e32014-07-31 14:12:50 -0700249 if (NULL == latcData) {
250 continue;
251 }
252
krajcevski2b310e42014-06-11 12:26:49 -0700253 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
254
krajcevskib5294e82014-07-30 08:34:51 -0700255 // Make sure that it all matches a given block encoding. Since we have
256 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
257 // an approximation scheme that optimizes for speed against coverage maps.
258 // That means that each palette in the encoded block is exactly the same,
259 // and that the three bits saved per pixel are computed from the top three
260 // bits of the luminance value.
261 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
262 const uint64_t kIndex = kIndexEncodingMap[lum >> 5];
263 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}