| 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 | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Make sure that we properly fail when we don't have multiple of four image dimensions. |
| 17 | */ |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 18 | DEF_TEST(CompressAlphaFailDimensions, reporter) { |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 19 | SkBitmap bitmap; |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 20 | static const int kWidth = 17; |
| 21 | static const int kHeight = 17; |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 22 | SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 23 | |
| 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); |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 31 | |
| 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(); |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 38 | |
| 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 | } |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 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] | 48 | * compressed textures can (currently) only be created from A8 bitmaps. |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 49 | */ |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 50 | DEF_TEST(CompressAlphaFailColorType, reporter) { |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 51 | SkBitmap bitmap; |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 52 | static const int kWidth = 12; |
| 53 | static const int kHeight = 12; |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 54 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 55 | |
| 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); |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 62 | |
| 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 | |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 70 | 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 | } |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 78 | * 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 | */ |
| 81 | DEF_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 | |
| krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame^] | 135 | // Ignore formats for RGBA data, since the decompressed buffer |
| krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 136 | // won't match the size and contents of the original. |
| 137 | // TODO: Create separate tests for RGB and RGBA data once |
| krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame^] | 138 | // ASTC and ETC1 decompression is implemented. |
| 139 | if (SkTextureCompressor::kASTC_12x12_Format == fmt || |
| 140 | SkTextureCompressor::kETC1_Format == fmt) { |
| krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 141 | 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 | /** |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 174 | * Make sure that if we pass in a solid color bitmap that we get the appropriate results |
| 175 | */ |
| 176 | DEF_TEST(CompressLATC, reporter) { |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 177 | |
| 178 | const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_Format; |
| 179 | static const int kLATCEncodedBlockSize = 8; |
| 180 | |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 181 | 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 | |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 193 | 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); |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 202 | |
| 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); |
| krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 207 | if (NULL == pixels) { |
| 208 | bitmap.unlockPixels(); |
| 209 | continue; |
| 210 | } |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 211 | |
| 212 | for (int i = 0; i < kWidth*kHeight; ++i) { |
| 213 | pixels[i] = lum; |
| 214 | } |
| 215 | bitmap.unlockPixels(); |
| 216 | |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 217 | SkAutoDataUnref latcData( |
| 218 | SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); |
| 219 | REPORTER_ASSERT(reporter, NULL != latcData); |
| krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 220 | if (NULL == latcData) { |
| 221 | continue; |
| 222 | } |
| 223 | |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 224 | REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
| 225 | |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 226 | // 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 | |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 242 | const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->data()); |
| krajcevski | b5294e8 | 2014-07-30 08:34:51 -0700 | [diff] [blame] | 243 | for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
| krajcevski | 2b310e4 | 2014-06-11 12:26:49 -0700 | [diff] [blame] | 244 | REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
| 245 | } |
| 246 | } |
| 247 | } |