krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 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 | |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 15 | // TODO: Create separate tests for RGB and RGBA data once |
| 16 | // ASTC and ETC1 decompression is implemented. |
| 17 | |
| 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 | |
| 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 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Make sure that we properly fail when we don't have multiple of four image dimensions. |
| 43 | */ |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 44 | DEF_TEST(CompressAlphaFailDimensions, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 45 | static const int kWidth = 17; |
| 46 | static const int kHeight = 17; |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 47 | |
| 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 55 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 56 | SkAutoPixmapStorage pixmap; |
| 57 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
| 58 | // leaving the pixels uninitialized, as they don't affect the test... |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 59 | |
| 60 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 61 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 62 | if (!compresses_a8(fmt)) { |
| 63 | continue; |
| 64 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 65 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, nullptr == data); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 67 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Make sure that we properly fail when we don't have the correct bitmap type. |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 72 | * compressed textures can (currently) only be created from A8 bitmaps. |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 73 | */ |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 74 | DEF_TEST(CompressAlphaFailColorType, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 75 | static const int kWidth = 12; |
| 76 | static const int kHeight = 12; |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 77 | |
| 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 84 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 85 | SkAutoPixmapStorage pixmap; |
| 86 | pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight)); |
| 87 | // leaving the pixels uninitialized, as they don't affect the test... |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 88 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 89 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 90 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 91 | if (!compresses_a8(fmt)) { |
| 92 | continue; |
| 93 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 94 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, nullptr == data); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 96 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 100 | * 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 | */ |
| 103 | DEF_TEST(CompressCheckerboard, reporter) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 104 | 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). |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 106 | |
| 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 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 116 | SkAutoPixmapStorage pixmap; |
| 117 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 118 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 119 | // Populate the pixels |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 120 | { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 121 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, pixels); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | if (nullptr == pixels) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 124 | 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 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 135 | pixels += pixmap.rowBytes(); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | SkAutoMalloc decompMemory(kWidth*kHeight); |
| 140 | uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 141 | REPORTER_ASSERT(reporter, decompBuffer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 142 | if (nullptr == decompBuffer) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| 146 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 147 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
| 148 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 149 | // Ignore formats for RGBA data, since the decompressed buffer |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 150 | // won't match the size and contents of the original. |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 151 | if (!decompresses_a8(fmt) || !compresses_a8(fmt)) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 152 | continue; |
| 153 | } |
| 154 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 155 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 157 | if (nullptr == data) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 158 | 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 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 168 | const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 169 | REPORTER_ASSERT(reporter, pixels); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 170 | if (nullptr == pixels) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 171 | continue; |
| 172 | } |
| 173 | |
| 174 | for (int y = 0; y < kHeight; ++y) { |
| 175 | for (int x = 0; x < kWidth; ++x) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 176 | bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWidth + x]; |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 177 | REPORTER_ASSERT(reporter, ok); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 184 | * Make sure that if we pass in a solid color bitmap that we get the appropriate results |
| 185 | */ |
| 186 | DEF_TEST(CompressLATC, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 187 | |
| 188 | const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format; |
| 189 | static const int kLATCEncodedBlockSize = 8; |
| 190 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 191 | static const int kWidth = 8; |
| 192 | static const int kHeight = 8; |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 193 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 194 | SkAutoPixmapStorage pixmap; |
| 195 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 196 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 197 | 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 206 | |
| 207 | for (int lum = 0; lum < 256; ++lum) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 208 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 209 | for (int i = 0; i < kWidth*kHeight; ++i) { |
| 210 | pixels[i] = lum; |
| 211 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 212 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 213 | SkAutoDataUnref latcData( |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 214 | SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 215 | REPORTER_ASSERT(reporter, latcData); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 216 | if (nullptr == latcData) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 217 | continue; |
| 218 | } |
| 219 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 220 | REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
| 221 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 222 | // 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 }; |
pavel | 47eedcc | 2014-10-23 13:18:50 -0700 | [diff] [blame] | 229 | |
| 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 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 262 | 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 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 270 | const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data()); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 271 | for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 272 | REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
| 273 | } |
| 274 | } |
| 275 | } |