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 | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 45 | SkBitmap bitmap; |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 46 | static const int kWidth = 17; |
| 47 | static const int kHeight = 17; |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 48 | SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 57 | |
| 58 | bool setInfoSuccess = bitmap.setInfo(info); |
| 59 | REPORTER_ASSERT(reporter, setInfoSuccess); |
| 60 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 61 | bitmap.allocPixels(info); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 62 | bitmap.unlockPixels(); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 63 | |
| 64 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 65 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 66 | if (!compresses_a8(fmt)) { |
| 67 | continue; |
| 68 | } |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 69 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt)); |
| 70 | REPORTER_ASSERT(reporter, NULL == data); |
| 71 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * 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] | 76 | * compressed textures can (currently) only be created from A8 bitmaps. |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 77 | */ |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 78 | DEF_TEST(CompressAlphaFailColorType, reporter) { |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 79 | SkBitmap bitmap; |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 80 | static const int kWidth = 12; |
| 81 | static const int kHeight = 12; |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 82 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 90 | |
| 91 | bool setInfoSuccess = bitmap.setInfo(info); |
| 92 | REPORTER_ASSERT(reporter, setInfoSuccess); |
| 93 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 94 | bitmap.allocPixels(info); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 95 | bitmap.unlockPixels(); |
| 96 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 97 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 98 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 99 | if (!compresses_a8(fmt)) { |
| 100 | continue; |
| 101 | } |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 102 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt)); |
| 103 | REPORTER_ASSERT(reporter, NULL == data); |
| 104 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 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 | */ |
| 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 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 129 | bitmap.allocPixels(info); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 130 | bitmap.unlockPixels(); |
| 131 | |
| 132 | // Populate bitmap |
| 133 | { |
| 134 | SkAutoLockPixels alp(bitmap); |
| 135 | |
| 136 | uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 137 | REPORTER_ASSERT(reporter, pixels); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 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()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 156 | REPORTER_ASSERT(reporter, decompBuffer); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 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 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 164 | // Ignore formats for RGBA data, since the decompressed buffer |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 165 | // won't match the size and contents of the original. |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 166 | if (!decompresses_a8(fmt) || !compresses_a8(fmt)) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 167 | continue; |
| 168 | } |
| 169 | |
| 170 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap, fmt)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, data); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 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()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 185 | REPORTER_ASSERT(reporter, pixels); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 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 | /** |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 200 | * Make sure that if we pass in a solid color bitmap that we get the appropriate results |
| 201 | */ |
| 202 | DEF_TEST(CompressLATC, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 203 | |
| 204 | const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format; |
| 205 | static const int kLATCEncodedBlockSize = 8; |
| 206 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 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 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 215 | bitmap.allocPixels(info); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 216 | bitmap.unlockPixels(); |
| 217 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 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); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 227 | |
| 228 | for (int lum = 0; lum < 256; ++lum) { |
| 229 | bitmap.lockPixels(); |
| 230 | uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 231 | REPORTER_ASSERT(reporter, pixels); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 232 | if (NULL == pixels) { |
| 233 | bitmap.unlockPixels(); |
| 234 | continue; |
| 235 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 236 | |
| 237 | for (int i = 0; i < kWidth*kHeight; ++i) { |
| 238 | pixels[i] = lum; |
| 239 | } |
| 240 | bitmap.unlockPixels(); |
| 241 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 242 | SkAutoDataUnref latcData( |
| 243 | SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 244 | REPORTER_ASSERT(reporter, latcData); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 245 | if (NULL == latcData) { |
| 246 | continue; |
| 247 | } |
| 248 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 249 | REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
| 250 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 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 }; |
pavel | 47eedcc | 2014-10-23 13:18:50 -0700 | [diff] [blame] | 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 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 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 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 299 | const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data()); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 300 | for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 301 | REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
| 302 | } |
| 303 | } |
| 304 | } |