• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
15 // TODO: Create separate tests for RGB and RGBA data once
16 // ASTC and ETC1 decompression is implemented.
17 
decompresses_a8(SkTextureCompressor::Format fmt)18 static 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 
compresses_a8(SkTextureCompressor::Format fmt)29 static 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 
41 /**
42  * Make sure that we properly fail when we don't have multiple of four image dimensions.
43  */
DEF_TEST(CompressAlphaFailDimensions,reporter)44 DEF_TEST(CompressAlphaFailDimensions, reporter) {
45     SkBitmap bitmap;
46     static const int kWidth = 17;
47     static const int kHeight = 17;
48     SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight);
49 
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);
57 
58     bool setInfoSuccess = bitmap.setInfo(info);
59     REPORTER_ASSERT(reporter, setInfoSuccess);
60 
61     bitmap.allocPixels(info);
62     bitmap.unlockPixels();
63 
64     for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
65         const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
66         if (!compresses_a8(fmt)) {
67             continue;
68         }
69         SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
70         REPORTER_ASSERT(reporter, NULL == data);
71     }
72 }
73 
74 /**
75  * Make sure that we properly fail when we don't have the correct bitmap type.
76  * compressed textures can (currently) only be created from A8 bitmaps.
77  */
DEF_TEST(CompressAlphaFailColorType,reporter)78 DEF_TEST(CompressAlphaFailColorType, reporter) {
79     SkBitmap bitmap;
80     static const int kWidth = 12;
81     static const int kHeight = 12;
82     SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
83 
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);
90 
91     bool setInfoSuccess = bitmap.setInfo(info);
92     REPORTER_ASSERT(reporter, setInfoSuccess);
93 
94     bitmap.allocPixels(info);
95     bitmap.unlockPixels();
96 
97     for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) {
98         const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i);
99         if (!compresses_a8(fmt)) {
100             continue;
101         }
102         SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
103         REPORTER_ASSERT(reporter, NULL == data);
104     }
105 }
106 
107 /**
108  * 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  */
DEF_TEST(CompressCheckerboard,reporter)111 DEF_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 
129     bitmap.allocPixels(info);
130     bitmap.unlockPixels();
131 
132     // Populate bitmap
133     {
134         SkAutoLockPixels alp(bitmap);
135 
136         uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
137         REPORTER_ASSERT(reporter, pixels);
138         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());
156     REPORTER_ASSERT(reporter, decompBuffer);
157     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 
164         // Ignore formats for RGBA data, since the decompressed buffer
165         // won't match the size and contents of the original.
166         if (!decompresses_a8(fmt) || !compresses_a8(fmt)) {
167             continue;
168         }
169 
170         SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt));
171         REPORTER_ASSERT(reporter, data);
172         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());
185         REPORTER_ASSERT(reporter, pixels);
186         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 /**
200  * Make sure that if we pass in a solid color bitmap that we get the appropriate results
201  */
DEF_TEST(CompressLATC,reporter)202 DEF_TEST(CompressLATC, reporter) {
203 
204     const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format;
205     static const int kLATCEncodedBlockSize = 8;
206 
207     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 
215     bitmap.allocPixels(info);
216     bitmap.unlockPixels();
217 
218     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);
227 
228     for (int lum = 0; lum < 256; ++lum) {
229         bitmap.lockPixels();
230         uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels());
231         REPORTER_ASSERT(reporter, pixels);
232         if (NULL == pixels) {
233             bitmap.unlockPixels();
234             continue;
235         }
236 
237         for (int i = 0; i < kWidth*kHeight; ++i) {
238             pixels[i] = lum;
239         }
240         bitmap.unlockPixels();
241 
242         SkAutoDataUnref latcData(
243             SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat));
244         REPORTER_ASSERT(reporter, latcData);
245         if (NULL == latcData) {
246             continue;
247         }
248 
249         REPORTER_ASSERT(reporter, kSizeToBe == latcData->size());
250 
251         // 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 };
258 
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 
291         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 
299         const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data());
300         for (size_t i = 0; i < (kSizeToBe/8); ++i) {
301             REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding);
302         }
303     }
304 }
305