blob: 8694389fe7ad0e4b1ea7dfd199630076cb223c36 [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
krajcevski2b310e42014-06-11 12:26:49 -070015/**
16 * Make sure that we properly fail when we don't have multiple of four image dimensions.
17 */
krajcevskib5294e82014-07-30 08:34:51 -070018DEF_TEST(CompressAlphaFailDimensions, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070019 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070020 static const int kWidth = 17;
21 static const int kHeight = 17;
krajcevski2b310e42014-06-11 12:26:49 -070022 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070023
24 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that we
25 // are violating those assumptions. And if we are, then we're also violating the
26 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 is
27 // also not divisible by 12. Our dimensions are prime, so any block dimension
28 // larger than 1 should fail.
29 REPORTER_ASSERT(reporter, kWidth % 4 != 0);
30 REPORTER_ASSERT(reporter, kHeight % 4 != 0);
krajcevski2b310e42014-06-11 12:26:49 -070031
32 bool setInfoSuccess = bitmap.setInfo(info);
33 REPORTER_ASSERT(reporter, setInfoSuccess);
34
35 bool allocPixelsSuccess = bitmap.allocPixels(info);
36 REPORTER_ASSERT(reporter, allocPixelsSuccess);
37 bitmap.unlockPixels();
krajcevskib5294e82014-07-30 08:34:51 -070038
39 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
40 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
41 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
42 REPORTER_ASSERT(reporter, NULL == data);
43 }
krajcevski2b310e42014-06-11 12:26:49 -070044}
45
46/**
47 * Make sure that we properly fail when we don't have the correct bitmap type.
krajcevskib5294e82014-07-30 08:34:51 -070048 * compressed textures can (currently) only be created from A8 bitmaps.
krajcevski2b310e42014-06-11 12:26:49 -070049 */
krajcevskib5294e82014-07-30 08:34:51 -070050DEF_TEST(CompressAlphaFailColorType, reporter) {
krajcevski2b310e42014-06-11 12:26:49 -070051 SkBitmap bitmap;
krajcevskib5294e82014-07-30 08:34:51 -070052 static const int kWidth = 12;
53 static const int kHeight = 12;
krajcevski2b310e42014-06-11 12:26:49 -070054 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
krajcevskib5294e82014-07-30 08:34:51 -070055
56 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
57 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
58 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
59 // need to be updated.
60 REPORTER_ASSERT(reporter, kWidth % 12 == 0);
61 REPORTER_ASSERT(reporter, kHeight % 12 == 0);
krajcevski2b310e42014-06-11 12:26:49 -070062
63 bool setInfoSuccess = bitmap.setInfo(info);
64 REPORTER_ASSERT(reporter, setInfoSuccess);
65
66 bool allocPixelsSuccess = bitmap.allocPixels(info);
67 REPORTER_ASSERT(reporter, allocPixelsSuccess);
68 bitmap.unlockPixels();
69
krajcevskib5294e82014-07-30 08:34:51 -070070 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
71 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
72 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
73 REPORTER_ASSERT(reporter, NULL == data);
74 }
krajcevski2b310e42014-06-11 12:26:49 -070075}
76
77/**
krajcevski4ad76e32014-07-31 14:12:50 -070078 * Make sure that if you compress a texture with alternating black/white pixels, and
79 * then decompress it, you get what you started with.
80 */
81DEF_TEST(CompressCheckerboard, reporter) {
82 SkBitmap bitmap;
83 static const int kWidth = 48; // We need the number to be divisible by both
84 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC).
85 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
86
87 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible
88 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might
89 // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would
90 // need to be updated. Additionally, ARM NEON and SSE code paths support up to
91 // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the
92 // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48
93 REPORTER_ASSERT(reporter, kWidth % 48 == 0);
94 REPORTER_ASSERT(reporter, kHeight % 48 == 0);
95
96 bool setInfoSuccess = bitmap.setInfo(info);
97 REPORTER_ASSERT(reporter, setInfoSuccess);
98
99 bool allocPixelsSuccess = bitmap.allocPixels(info);
100 REPORTER_ASSERT(reporter, allocPixelsSuccess);
101 bitmap.unlockPixels();
102
103 // Populate bitmap
104 {
105 SkAutoLockPixels alp(bitmap);
106
107 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
108 REPORTER_ASSERT(reporter, NULL != pixels);
109 if (NULL == pixels) {
110 return;
111 }
112
113 for (int y = 0; y < kHeight; ++y) {
114 for (int x = 0; x < kWidth; ++x) {
115 if ((x ^ y) & 1) {
116 pixels[x] = 0xFF;
117 } else {
118 pixels[x] = 0;
119 }
120 }
121 pixels += bitmap.rowBytes();
122 }
123 }
124
125 SkAutoMalloc decompMemory(kWidth*kHeight);
126 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get());
127 REPORTER_ASSERT(reporter, NULL != decompBuffer);
128 if (NULL == decompBuffer) {
129 return;
130 }
131
132 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
133 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
134
krajcevskie90c9002014-08-05 07:37:26 -0700135 // Ignore formats for RGBA data, since the decompressed buffer
krajcevski4ad76e32014-07-31 14:12:50 -0700136 // won't match the size and contents of the original.
137 // TODO: Create separate tests for RGB and RGBA data once
krajcevskie90c9002014-08-05 07:37:26 -0700138 // ASTC and ETC1 decompression is implemented.
139 if (SkTextureCompressor::kASTC_12x12_Format == fmt ||
140 SkTextureCompressor::kETC1_Format == fmt) {
krajcevski4ad76e32014-07-31 14:12:50 -0700141 continue;
142 }
143
144 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
145 REPORTER_ASSERT(reporter, NULL != data);
146 if (NULL == data) {
147 continue;
148 }
149
150 bool decompResult =
151 SkTextureCompressor::DecompressBufferFromFormat(
152 decompBuffer, kWidth,
153 data->bytes(),
154 kWidth, kHeight, fmt);
155 REPORTER_ASSERT(reporter, decompResult);
156
157 SkAutoLockPixels alp(bitmap);
158 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
159 REPORTER_ASSERT(reporter, NULL != pixels);
160 if (NULL == pixels) {
161 continue;
162 }
163
164 for (int y = 0; y < kHeight; ++y) {
165 for (int x = 0; x < kWidth; ++x) {
166 bool ok = pixels[y*bitmap.rowBytes() + x] == decompBuffer[y*kWidth + x];
167 REPORTER_ASSERT(reporter, ok);
168 }
169 }
170 }
171}
172
173/**
krajcevski2b310e42014-06-11 12:26:49 -0700174 * Make sure that if we pass in a solid color bitmap that we get the appropriate results
175 */
176DEF_TEST(CompressLATC, reporter) {
krajcevskib5294e82014-07-30 08:34:51 -0700177
178 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
179 static const int kLATCEncodedBlockSize = 8;
180
krajcevski2b310e42014-06-11 12:26:49 -0700181 SkBitmap bitmap;
182 static const int kWidth = 8;
183 static const int kHeight = 8;
184 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
185
186 bool setInfoSuccess = bitmap.setInfo(info);
187 REPORTER_ASSERT(reporter, setInfoSuccess);
188
189 bool allocPixelsSuccess = bitmap.allocPixels(info);
190 REPORTER_ASSERT(reporter, allocPixelsSuccess);
191 bitmap.unlockPixels();
192
krajcevskib5294e82014-07-30 08:34:51 -0700193 int latcDimX, latcDimY;
194 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY);
195
196 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0);
197 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0);
198 const size_t kSizeToBe =
199 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight);
200 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16));
201 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0);
krajcevski2b310e42014-06-11 12:26:49 -0700202
203 for (int lum = 0; lum < 256; ++lum) {
204 bitmap.lockPixels();
205 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
206 REPORTER_ASSERT(reporter, NULL != pixels);
krajcevski4ad76e32014-07-31 14:12:50 -0700207 if (NULL == pixels) {
208 bitmap.unlockPixels();
209 continue;
210 }
krajcevski2b310e42014-06-11 12:26:49 -0700211
212 for (int i = 0; i < kWidth*kHeight; ++i) {
213 pixels[i] = lum;
214 }
215 bitmap.unlockPixels();
216
krajcevski2b310e42014-06-11 12:26:49 -0700217 SkAutoDataUnref latcData(
218 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat));
219 REPORTER_ASSERT(reporter, NULL != latcData);
krajcevski4ad76e32014-07-31 14:12:50 -0700220 if (NULL == latcData) {
221 continue;
222 }
223
krajcevski2b310e42014-06-11 12:26:49 -0700224 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
225
krajcevskib5294e82014-07-30 08:34:51 -0700226 // Make sure that it all matches a given block encoding. Since we have
227 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using
228 // an approximation scheme that optimizes for speed against coverage maps.
229 // That means that each palette in the encoded block is exactly the same,
230 // and that the three bits saved per pixel are computed from the top three
231 // bits of the luminance value.
232 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 };
233 const uint64_t kIndex = kIndexEncodingMap[lum >> 5];
234 const uint64_t kConstColorEncoding =
235 SkEndian_SwapLE64(
236 255 |
237 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) |
238 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) |
239 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) |
240 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61));
241
krajcevski2b310e42014-06-11 12:26:49 -0700242 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
krajcevskib5294e82014-07-30 08:34:51 -0700243 for (size_t i = 0; i < (kSizeToBe/8); ++i) {
krajcevski2b310e42014-06-11 12:26:49 -0700244 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
245 }
246 }
247}