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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 bitmap.unlockPixels(); | 68 bitmap.unlockPixels(); |
69 | 69 |
70 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { | 70 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
71 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); | 71 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
72 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); | 72 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
73 REPORTER_ASSERT(reporter, NULL == data); | 73 REPORTER_ASSERT(reporter, NULL == data); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 /** | 77 /** |
| 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 divisibl
e |
| 88 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t |
| 89 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his 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 |
| 135 // ASTC is for RGBA data, and the decompressed buffer |
| 136 // won't match the size and contents of the original. |
| 137 // TODO: Create separate tests for RGB and RGBA data once |
| 138 // ASTC decompression is implemented. |
| 139 if (SkTextureCompressor::kASTC_12x12_Format == fmt) { |
| 140 continue; |
| 141 } |
| 142 |
| 143 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 144 REPORTER_ASSERT(reporter, NULL != data); |
| 145 if (NULL == data) { |
| 146 continue; |
| 147 } |
| 148 |
| 149 bool decompResult = |
| 150 SkTextureCompressor::DecompressBufferFromFormat( |
| 151 decompBuffer, kWidth, |
| 152 data->bytes(), |
| 153 kWidth, kHeight, fmt); |
| 154 REPORTER_ASSERT(reporter, decompResult); |
| 155 |
| 156 SkAutoLockPixels alp(bitmap); |
| 157 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
| 158 REPORTER_ASSERT(reporter, NULL != pixels); |
| 159 if (NULL == pixels) { |
| 160 continue; |
| 161 } |
| 162 |
| 163 for (int y = 0; y < kHeight; ++y) { |
| 164 for (int x = 0; x < kWidth; ++x) { |
| 165 bool ok = pixels[y*bitmap.rowBytes() + x] == decompBuffer[y*kWid
th + x]; |
| 166 REPORTER_ASSERT(reporter, ok); |
| 167 } |
| 168 } |
| 169 } |
| 170 } |
| 171 |
| 172 /** |
78 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results | 173 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results |
79 */ | 174 */ |
80 DEF_TEST(CompressLATC, reporter) { | 175 DEF_TEST(CompressLATC, reporter) { |
81 | 176 |
82 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; | 177 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; |
83 static const int kLATCEncodedBlockSize = 8; | 178 static const int kLATCEncodedBlockSize = 8; |
84 | 179 |
85 SkBitmap bitmap; | 180 SkBitmap bitmap; |
86 static const int kWidth = 8; | 181 static const int kWidth = 8; |
87 static const int kHeight = 8; | 182 static const int kHeight = 8; |
(...skipping 13 matching lines...) Expand all Loading... |
101 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); | 196 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); |
102 const size_t kSizeToBe = | 197 const size_t kSizeToBe = |
103 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight)
; | 198 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight)
; |
104 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSiz
e)/16)); | 199 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSiz
e)/16)); |
105 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); | 200 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); |
106 | 201 |
107 for (int lum = 0; lum < 256; ++lum) { | 202 for (int lum = 0; lum < 256; ++lum) { |
108 bitmap.lockPixels(); | 203 bitmap.lockPixels(); |
109 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); | 204 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
110 REPORTER_ASSERT(reporter, NULL != pixels); | 205 REPORTER_ASSERT(reporter, NULL != pixels); |
| 206 if (NULL == pixels) { |
| 207 bitmap.unlockPixels(); |
| 208 continue; |
| 209 } |
111 | 210 |
112 for (int i = 0; i < kWidth*kHeight; ++i) { | 211 for (int i = 0; i < kWidth*kHeight; ++i) { |
113 pixels[i] = lum; | 212 pixels[i] = lum; |
114 } | 213 } |
115 bitmap.unlockPixels(); | 214 bitmap.unlockPixels(); |
116 | 215 |
117 SkAutoDataUnref latcData( | 216 SkAutoDataUnref latcData( |
118 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); | 217 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); |
119 REPORTER_ASSERT(reporter, NULL != latcData); | 218 REPORTER_ASSERT(reporter, NULL != latcData); |
| 219 if (NULL == latcData) { |
| 220 continue; |
| 221 } |
| 222 |
120 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); | 223 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
121 | 224 |
122 // Make sure that it all matches a given block encoding. Since we have | 225 // Make sure that it all matches a given block encoding. Since we have |
123 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are us
ing | 226 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are us
ing |
124 // an approximation scheme that optimizes for speed against coverage map
s. | 227 // an approximation scheme that optimizes for speed against coverage map
s. |
125 // That means that each palette in the encoded block is exactly the same
, | 228 // That means that each palette in the encoded block is exactly the same
, |
126 // and that the three bits saved per pixel are computed from the top thr
ee | 229 // and that the three bits saved per pixel are computed from the top thr
ee |
127 // bits of the luminance value. | 230 // bits of the luminance value. |
128 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 }; | 231 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 }; |
129 const uint64_t kIndex = kIndexEncodingMap[lum >> 5]; | 232 const uint64_t kIndex = kIndexEncodingMap[lum >> 5]; |
130 const uint64_t kConstColorEncoding = | 233 const uint64_t kConstColorEncoding = |
131 SkEndian_SwapLE64( | 234 SkEndian_SwapLE64( |
132 255 | | 235 255 | |
133 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25
) | | 236 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25
) | |
134 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37
) | | 237 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37
) | |
135 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49
) | | 238 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49
) | |
136 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61
)); | 239 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61
)); |
137 | 240 |
138 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); | 241 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); |
139 for (size_t i = 0; i < (kSizeToBe/8); ++i) { | 242 for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
140 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); | 243 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
141 } | 244 } |
142 } | 245 } |
143 } | 246 } |
OLD | NEW |