| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkData.h" | 9 #include "SkData.h" |
| 10 #include "SkEndian.h" | 10 #include "SkEndian.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 default: | 36 default: |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 /** | 41 /** |
| 42 * Make sure that we properly fail when we don't have multiple of four image dim
ensions. | 42 * Make sure that we properly fail when we don't have multiple of four image dim
ensions. |
| 43 */ | 43 */ |
| 44 DEF_TEST(CompressAlphaFailDimensions, reporter) { | 44 DEF_TEST(CompressAlphaFailDimensions, reporter) { |
| 45 SkBitmap bitmap; |
| 45 static const int kWidth = 17; | 46 static const int kWidth = 17; |
| 46 static const int kHeight = 17; | 47 static const int kHeight = 17; |
| 48 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
| 47 | 49 |
| 48 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that w
e | 50 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that w
e |
| 49 // are violating those assumptions. And if we are, then we're also violating
the | 51 // 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 i
s | 52 // assumptions of ASTC, which is 12x12 since any number not divisible by 4 i
s |
| 51 // also not divisible by 12. Our dimensions are prime, so any block dimensio
n | 53 // also not divisible by 12. Our dimensions are prime, so any block dimensio
n |
| 52 // larger than 1 should fail. | 54 // larger than 1 should fail. |
| 53 REPORTER_ASSERT(reporter, kWidth % 4 != 0); | 55 REPORTER_ASSERT(reporter, kWidth % 4 != 0); |
| 54 REPORTER_ASSERT(reporter, kHeight % 4 != 0); | 56 REPORTER_ASSERT(reporter, kHeight % 4 != 0); |
| 55 | 57 |
| 56 SkAutoPixmapStorage pixmap; | 58 bool setInfoSuccess = bitmap.setInfo(info); |
| 57 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); | 59 REPORTER_ASSERT(reporter, setInfoSuccess); |
| 58 // leaving the pixels uninitialized, as they don't affect the test... | 60 |
| 61 bitmap.allocPixels(info); |
| 62 bitmap.unlockPixels(); |
| 59 | 63 |
| 60 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { | 64 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 61 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); | 65 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
| 62 if (!compresses_a8(fmt)) { | 66 if (!compresses_a8(fmt)) { |
| 63 continue; | 67 continue; |
| 64 } | 68 } |
| 65 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap,
fmt)); | 69 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 66 REPORTER_ASSERT(reporter, NULL == data); | 70 REPORTER_ASSERT(reporter, NULL == data); |
| 67 } | 71 } |
| 68 } | 72 } |
| 69 | 73 |
| 70 /** | 74 /** |
| 71 * Make sure that we properly fail when we don't have the correct bitmap type. | 75 * Make sure that we properly fail when we don't have the correct bitmap type. |
| 72 * compressed textures can (currently) only be created from A8 bitmaps. | 76 * compressed textures can (currently) only be created from A8 bitmaps. |
| 73 */ | 77 */ |
| 74 DEF_TEST(CompressAlphaFailColorType, reporter) { | 78 DEF_TEST(CompressAlphaFailColorType, reporter) { |
| 79 SkBitmap bitmap; |
| 75 static const int kWidth = 12; | 80 static const int kWidth = 12; |
| 76 static const int kHeight = 12; | 81 static const int kHeight = 12; |
| 82 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 77 | 83 |
| 78 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisibl
e | 84 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisibl
e |
| 79 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t | 85 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t |
| 80 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his would | 86 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his would |
| 81 // need to be updated. | 87 // need to be updated. |
| 82 REPORTER_ASSERT(reporter, kWidth % 12 == 0); | 88 REPORTER_ASSERT(reporter, kWidth % 12 == 0); |
| 83 REPORTER_ASSERT(reporter, kHeight % 12 == 0); | 89 REPORTER_ASSERT(reporter, kHeight % 12 == 0); |
| 84 | 90 |
| 85 SkAutoPixmapStorage pixmap; | 91 bool setInfoSuccess = bitmap.setInfo(info); |
| 86 pixmap.alloc(SkImageInfo::MakeN32Premul(kWidth, kHeight)); | 92 REPORTER_ASSERT(reporter, setInfoSuccess); |
| 87 // leaving the pixels uninitialized, as they don't affect the test... | 93 |
| 94 bitmap.allocPixels(info); |
| 95 bitmap.unlockPixels(); |
| 88 | 96 |
| 89 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { | 97 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 90 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); | 98 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
| 91 if (!compresses_a8(fmt)) { | 99 if (!compresses_a8(fmt)) { |
| 92 continue; | 100 continue; |
| 93 } | 101 } |
| 94 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap,
fmt)); | 102 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 95 REPORTER_ASSERT(reporter, NULL == data); | 103 REPORTER_ASSERT(reporter, NULL == data); |
| 96 } | 104 } |
| 97 } | 105 } |
| 98 | 106 |
| 99 /** | 107 /** |
| 100 * Make sure that if you compress a texture with alternating black/white pixels,
and | 108 * Make sure that if you compress a texture with alternating black/white pixels,
and |
| 101 * then decompress it, you get what you started with. | 109 * then decompress it, you get what you started with. |
| 102 */ | 110 */ |
| 103 DEF_TEST(CompressCheckerboard, reporter) { | 111 DEF_TEST(CompressCheckerboard, reporter) { |
| 112 SkBitmap bitmap; |
| 104 static const int kWidth = 48; // We need the number to be divisible by both | 113 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). | 114 static const int kHeight = 48; // 12 (ASTC) and 16 (ARM NEON R11 EAC). |
| 115 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
| 106 | 116 |
| 107 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisibl
e | 117 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisibl
e |
| 108 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t | 118 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t |
| 109 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his would | 119 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his would |
| 110 // need to be updated. Additionally, ARM NEON and SSE code paths support up
to | 120 // 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 | 121 // 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 | 122 // 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); | 123 REPORTER_ASSERT(reporter, kWidth % 48 == 0); |
| 114 REPORTER_ASSERT(reporter, kHeight % 48 == 0); | 124 REPORTER_ASSERT(reporter, kHeight % 48 == 0); |
| 115 | 125 |
| 116 SkAutoPixmapStorage pixmap; | 126 bool setInfoSuccess = bitmap.setInfo(info); |
| 117 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); | 127 REPORTER_ASSERT(reporter, setInfoSuccess); |
| 118 | 128 |
| 119 // Populate the pixels | 129 bitmap.allocPixels(info); |
| 130 bitmap.unlockPixels(); |
| 131 |
| 132 // Populate bitmap |
| 120 { | 133 { |
| 121 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); | 134 SkAutoLockPixels alp(bitmap); |
| 135 |
| 136 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
| 122 REPORTER_ASSERT(reporter, pixels); | 137 REPORTER_ASSERT(reporter, pixels); |
| 123 if (NULL == pixels) { | 138 if (NULL == pixels) { |
| 124 return; | 139 return; |
| 125 } | 140 } |
| 126 | 141 |
| 127 for (int y = 0; y < kHeight; ++y) { | 142 for (int y = 0; y < kHeight; ++y) { |
| 128 for (int x = 0; x < kWidth; ++x) { | 143 for (int x = 0; x < kWidth; ++x) { |
| 129 if ((x ^ y) & 1) { | 144 if ((x ^ y) & 1) { |
| 130 pixels[x] = 0xFF; | 145 pixels[x] = 0xFF; |
| 131 } else { | 146 } else { |
| 132 pixels[x] = 0; | 147 pixels[x] = 0; |
| 133 } | 148 } |
| 134 } | 149 } |
| 135 pixels += pixmap.rowBytes(); | 150 pixels += bitmap.rowBytes(); |
| 136 } | 151 } |
| 137 } | 152 } |
| 138 | 153 |
| 139 SkAutoMalloc decompMemory(kWidth*kHeight); | 154 SkAutoMalloc decompMemory(kWidth*kHeight); |
| 140 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get()); | 155 uint8_t* decompBuffer = reinterpret_cast<uint8_t*>(decompMemory.get()); |
| 141 REPORTER_ASSERT(reporter, decompBuffer); | 156 REPORTER_ASSERT(reporter, decompBuffer); |
| 142 if (NULL == decompBuffer) { | 157 if (NULL == decompBuffer) { |
| 143 return; | 158 return; |
| 144 } | 159 } |
| 145 | 160 |
| 146 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { | 161 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
| 147 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); | 162 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
| 148 | 163 |
| 149 // Ignore formats for RGBA data, since the decompressed buffer | 164 // Ignore formats for RGBA data, since the decompressed buffer |
| 150 // won't match the size and contents of the original. | 165 // won't match the size and contents of the original. |
| 151 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) { | 166 if (!decompresses_a8(fmt) || !compresses_a8(fmt)) { |
| 152 continue; | 167 continue; |
| 153 } | 168 } |
| 154 | 169 |
| 155 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(pixmap,
fmt)); | 170 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 156 REPORTER_ASSERT(reporter, data); | 171 REPORTER_ASSERT(reporter, data); |
| 157 if (NULL == data) { | 172 if (NULL == data) { |
| 158 continue; | 173 continue; |
| 159 } | 174 } |
| 160 | 175 |
| 161 bool decompResult = | 176 bool decompResult = |
| 162 SkTextureCompressor::DecompressBufferFromFormat( | 177 SkTextureCompressor::DecompressBufferFromFormat( |
| 163 decompBuffer, kWidth, | 178 decompBuffer, kWidth, |
| 164 data->bytes(), | 179 data->bytes(), |
| 165 kWidth, kHeight, fmt); | 180 kWidth, kHeight, fmt); |
| 166 REPORTER_ASSERT(reporter, decompResult); | 181 REPORTER_ASSERT(reporter, decompResult); |
| 167 | 182 |
| 168 const uint8_t* pixels = reinterpret_cast<const uint8_t*>(pixmap.addr()); | 183 SkAutoLockPixels alp(bitmap); |
| 184 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
| 169 REPORTER_ASSERT(reporter, pixels); | 185 REPORTER_ASSERT(reporter, pixels); |
| 170 if (NULL == pixels) { | 186 if (NULL == pixels) { |
| 171 continue; | 187 continue; |
| 172 } | 188 } |
| 173 | 189 |
| 174 for (int y = 0; y < kHeight; ++y) { | 190 for (int y = 0; y < kHeight; ++y) { |
| 175 for (int x = 0; x < kWidth; ++x) { | 191 for (int x = 0; x < kWidth; ++x) { |
| 176 bool ok = pixels[y*pixmap.rowBytes() + x] == decompBuffer[y*kWid
th + x]; | 192 bool ok = pixels[y*bitmap.rowBytes() + x] == decompBuffer[y*kWid
th + x]; |
| 177 REPORTER_ASSERT(reporter, ok); | 193 REPORTER_ASSERT(reporter, ok); |
| 178 } | 194 } |
| 179 } | 195 } |
| 180 } | 196 } |
| 181 } | 197 } |
| 182 | 198 |
| 183 /** | 199 /** |
| 184 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results | 200 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results |
| 185 */ | 201 */ |
| 186 DEF_TEST(CompressLATC, reporter) { | 202 DEF_TEST(CompressLATC, reporter) { |
| 187 | 203 |
| 188 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; | 204 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; |
| 189 static const int kLATCEncodedBlockSize = 8; | 205 static const int kLATCEncodedBlockSize = 8; |
| 190 | 206 |
| 207 SkBitmap bitmap; |
| 191 static const int kWidth = 8; | 208 static const int kWidth = 8; |
| 192 static const int kHeight = 8; | 209 static const int kHeight = 8; |
| 210 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
| 193 | 211 |
| 194 SkAutoPixmapStorage pixmap; | 212 bool setInfoSuccess = bitmap.setInfo(info); |
| 195 pixmap.alloc(SkImageInfo::MakeA8(kWidth, kHeight)); | 213 REPORTER_ASSERT(reporter, setInfoSuccess); |
| 214 |
| 215 bitmap.allocPixels(info); |
| 216 bitmap.unlockPixels(); |
| 196 | 217 |
| 197 int latcDimX, latcDimY; | 218 int latcDimX, latcDimY; |
| 198 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY); | 219 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY); |
| 199 | 220 |
| 200 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0); | 221 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0); |
| 201 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); | 222 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); |
| 202 const size_t kSizeToBe = | 223 const size_t kSizeToBe = |
| 203 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight)
; | 224 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight)
; |
| 204 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSiz
e)/16)); | 225 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSiz
e)/16)); |
| 205 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); | 226 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); |
| 206 | 227 |
| 207 for (int lum = 0; lum < 256; ++lum) { | 228 for (int lum = 0; lum < 256; ++lum) { |
| 208 uint8_t* pixels = reinterpret_cast<uint8_t*>(pixmap.writable_addr()); | 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 |
| 209 for (int i = 0; i < kWidth*kHeight; ++i) { | 237 for (int i = 0; i < kWidth*kHeight; ++i) { |
| 210 pixels[i] = lum; | 238 pixels[i] = lum; |
| 211 } | 239 } |
| 240 bitmap.unlockPixels(); |
| 212 | 241 |
| 213 SkAutoDataUnref latcData( | 242 SkAutoDataUnref latcData( |
| 214 SkTextureCompressor::CompressBitmapToFormat(pixmap, kLATCFormat)); | 243 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); |
| 215 REPORTER_ASSERT(reporter, latcData); | 244 REPORTER_ASSERT(reporter, latcData); |
| 216 if (NULL == latcData) { | 245 if (NULL == latcData) { |
| 217 continue; | 246 continue; |
| 218 } | 247 } |
| 219 | 248 |
| 220 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); | 249 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
| 221 | 250 |
| 222 // Make sure that it all matches a given block encoding. Since we have | 251 // Make sure that it all matches a given block encoding. Since we have |
| 223 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are us
ing | 252 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are us
ing |
| 224 // an approximation scheme that optimizes for speed against coverage map
s. | 253 // an approximation scheme that optimizes for speed against coverage map
s. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37
) | | 295 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37
) | |
| 267 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49
) | | 296 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49
) | |
| 268 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61
)); | 297 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61
)); |
| 269 | 298 |
| 270 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); | 299 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); |
| 271 for (size_t i = 0; i < (kSizeToBe/8); ++i) { | 300 for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
| 272 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); | 301 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
| 273 } | 302 } |
| 274 } | 303 } |
| 275 } | 304 } |
| OLD | NEW |