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 | |
robertphillips | c5035e7 | 2016-03-17 06:58:39 -0700 | [diff] [blame] | 8 | #include "SkAutoPixmapStorage.h" |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkData.h" |
| 11 | #include "SkEndian.h" |
| 12 | #include "SkImageInfo.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 13 | #include "SkTemplates.h" |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 14 | #include "SkTextureCompressor.h" |
| 15 | #include "Test.h" |
| 16 | |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 17 | // TODO: Create separate tests for RGB and RGBA data once |
| 18 | // ASTC and ETC1 decompression is implemented. |
| 19 | |
| 20 | static bool decompresses_a8(SkTextureCompressor::Format fmt) { |
| 21 | switch (fmt) { |
| 22 | case SkTextureCompressor::kLATC_Format: |
| 23 | case SkTextureCompressor::kR11_EAC_Format: |
| 24 | return true; |
| 25 | |
| 26 | default: |
| 27 | return false; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static bool compresses_a8(SkTextureCompressor::Format fmt) { |
| 32 | switch (fmt) { |
| 33 | case SkTextureCompressor::kLATC_Format: |
| 34 | case SkTextureCompressor::kR11_EAC_Format: |
| 35 | case SkTextureCompressor::kASTC_12x12_Format: |
| 36 | return true; |
| 37 | |
| 38 | default: |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Make sure that we properly fail when we don't have multiple of four image dimensions. |
| 45 | */ |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 46 | DEF_TEST(CompressAlphaFailDimensions, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 47 | static const int kWidth = 17; |
| 48 | static const int kHeight = 17; |
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 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 58 | SkAutoPixmapStorage pixmap; |
| 59 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
| 60 | // leaving the pixels uninitialized, as they don't affect the test... |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 61 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 62 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 63 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 64 | if (!compresses_a8(fmt)) { |
| 65 | continue; |
| 66 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 67 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, nullptr == data); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 69 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * 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] | 74 | * compressed textures can (currently) only be created from A8 bitmaps. |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 75 | */ |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 76 | DEF_TEST(CompressAlphaFailColorType, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 77 | static const int kWidth = 12; |
| 78 | static const int kHeight = 12; |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 79 | |
| 80 | // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible |
| 81 | // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might |
| 82 | // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would |
| 83 | // need to be updated. |
| 84 | REPORTER_ASSERT(reporter, kWidth % 12 == 0); |
| 85 | REPORTER_ASSERT(reporter, kHeight % 12 == 0); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 86 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 87 | SkAutoPixmapStorage pixmap; |
| 88 | pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight)); |
| 89 | // leaving the pixels uninitialized, as they don't affect the test... |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 90 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 91 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 92 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 93 | if (!compresses_a8(fmt)) { |
| 94 | continue; |
| 95 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 96 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, nullptr == data); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 98 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 102 | * Make sure that if you compress a texture with alternating black/white pixels, and |
| 103 | * then decompress it, you get what you started with. |
| 104 | */ |
| 105 | DEF_TEST(CompressCheckerboard, reporter) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 106 | static const int kWidth = 48; // We need the number to be divisible by both |
| 107 | static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC). |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 108 | |
| 109 | // ASTC is at most 12x12, and any dimension divisible by 12 is also divisible |
| 110 | // by 4, which is the dimensions of R11_EAC and LATC. In the future, we might |
| 111 | // support additional variants of ASTC, such as 5x6 and 8x8, in which case this would |
| 112 | // need to be updated. Additionally, ARM NEON and SSE code paths support up to |
| 113 | // four blocks of R11 EAC at once, so they operate on 16-wide blocks. Hence, the |
| 114 | // valid width and height is going to be the LCM of 12 and 16 which is 4*4*3 = 48 |
| 115 | REPORTER_ASSERT(reporter, kWidth % 48 == 0); |
| 116 | REPORTER_ASSERT(reporter, kHeight % 48 == 0); |
| 117 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 118 | SkAutoPixmapStorage pixmap; |
| 119 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 120 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 121 | // Populate the pixels |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 122 | { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 123 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, pixels); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 125 | if (nullptr == pixels) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | for (int y = 0; y < kHeight; ++y) { |
| 130 | for (int x = 0; x < kWidth; ++x) { |
| 131 | if ((x ^ y) & 1) { |
| 132 | pixels[x] = 0xFF; |
| 133 | } else { |
| 134 | pixels[x] = 0; |
| 135 | } |
| 136 | } |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 137 | pixels += pixmap.rowBytes(); |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 141 | SkAutoTMalloc<uint8_t> decompMemory(kWidth*kHeight); |
| 142 | uint8_t* decompBuffer = decompMemory.get(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, decompBuffer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 144 | if (nullptr == decompBuffer) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | |
| 148 | for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 149 | const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor::Format>(i); |
| 150 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 151 | // Ignore formats for RGBA data, since the decompressed buffer |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 152 | // won't match the size and contents of the original. |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 153 | if (!decompresses_a8(fmt) || !compresses_a8(fmt)) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 154 | continue; |
| 155 | } |
| 156 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 157 | SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap, fmt)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, data); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | if (nullptr == data) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
| 163 | bool decompResult = |
| 164 | SkTextureCompressor::DecompressBufferFromFormat( |
| 165 | decompBuffer, kWidth, |
| 166 | data->bytes(), |
| 167 | kWidth, kHeight, fmt); |
| 168 | REPORTER_ASSERT(reporter, decompResult); |
| 169 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 170 | const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr()); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 171 | REPORTER_ASSERT(reporter, pixels); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | if (nullptr == pixels) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 173 | continue; |
| 174 | } |
| 175 | |
| 176 | for (int y = 0; y < kHeight; ++y) { |
| 177 | for (int x = 0; x < kWidth; ++x) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 178 | bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWidth + x]; |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 179 | REPORTER_ASSERT(reporter, ok); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 186 | * Make sure that if we pass in a solid color bitmap that we get the appropriate results |
| 187 | */ |
| 188 | DEF_TEST(CompressLATC, reporter) { |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 189 | |
| 190 | const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format; |
| 191 | static const int kLATCEncodedBlockSize = 8; |
| 192 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 193 | static const int kWidth = 8; |
| 194 | static const int kHeight = 8; |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 195 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 196 | SkAutoPixmapStorage pixmap; |
| 197 | pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 198 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 199 | int latcDimX, latcDimY; |
| 200 | SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY); |
| 201 | |
| 202 | REPORTER_ASSERT(reporter, kWidth % latcDimX == 0); |
| 203 | REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); |
| 204 | const size_t kSizeToBe = |
| 205 | SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight); |
| 206 | REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSize)/16)); |
| 207 | REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 208 | |
| 209 | for (int lum = 0; lum < 256; ++lum) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 210 | uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 211 | for (int i = 0; i < kWidth*kHeight; ++i) { |
| 212 | pixels[i] = lum; |
| 213 | } |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 214 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 215 | SkAutoDataUnref latcData( |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 216 | SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat)); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 217 | REPORTER_ASSERT(reporter, latcData); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 218 | if (nullptr == latcData) { |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 219 | continue; |
| 220 | } |
| 221 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 222 | REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
| 223 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 224 | // Make sure that it all matches a given block encoding. Since we have |
| 225 | // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are using |
| 226 | // an approximation scheme that optimizes for speed against coverage maps. |
| 227 | // That means that each palette in the encoded block is exactly the same, |
| 228 | // and that the three bits saved per pixel are computed from the top three |
| 229 | // bits of the luminance value. |
| 230 | const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 }; |
pavel | 47eedcc | 2014-10-23 13:18:50 -0700 | [diff] [blame] | 231 | |
| 232 | // Quantize to three bits in the same way that we do our LATC compression: |
| 233 | // 1. Divide by two |
| 234 | // 2. Add 9 |
| 235 | // 3. Divide by two |
| 236 | // 4. Approximate division by three twice |
| 237 | uint32_t quant = static_cast<uint32_t>(lum); |
| 238 | quant >>= 1; // 1 |
| 239 | quant += 9; // 2 |
| 240 | quant >>= 1; // 3 |
| 241 | |
| 242 | uint32_t a, b, c, ar, br, cr; |
| 243 | |
| 244 | // First division by three |
| 245 | a = quant >> 2; |
| 246 | ar = (quant & 0x3) << 4; |
| 247 | b = quant >> 4; |
| 248 | br = (quant & 0xF) << 2; |
| 249 | c = quant >> 6; |
| 250 | cr = (quant & 0x3F); |
| 251 | quant = (a + b + c) + ((ar + br + cr) >> 6); |
| 252 | |
| 253 | // Second division by three |
| 254 | a = quant >> 2; |
| 255 | ar = (quant & 0x3) << 4; |
| 256 | b = quant >> 4; |
| 257 | br = (quant & 0xF) << 2; |
| 258 | c = quant >> 6; |
| 259 | cr = (quant & 0x3F); |
| 260 | quant = (a + b + c) + ((ar + br + cr) >> 6); |
| 261 | |
| 262 | const uint64_t kIndex = kIndexEncodingMap[quant]; |
| 263 | |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 264 | const uint64_t kConstColorEncoding = |
| 265 | SkEndian_SwapLE64( |
| 266 | 255 | |
| 267 | (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25) | |
| 268 | (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37) | |
| 269 | (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49) | |
| 270 | (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61)); |
| 271 | |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 272 | const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data()); |
krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 273 | for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 274 | REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
| 275 | } |
| 276 | } |
| 277 | } |