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" |
11 #include "SkImageInfo.h" | 11 #include "SkImageInfo.h" |
12 #include "SkTextureCompressor.h" | 12 #include "SkTextureCompressor.h" |
13 #include "Test.h" | 13 #include "Test.h" |
14 | 14 |
15 static const int kLATCBlockDimension = 4; | |
16 static const int kLATCEncodedBlockSize = 8; | |
17 | |
18 /** | 15 /** |
19 * Make sure that we properly fail when we don't have multiple of four image dim
ensions. | 16 * Make sure that we properly fail when we don't have multiple of four image dim
ensions. |
20 */ | 17 */ |
21 DEF_TEST(CompressLATCFailDimensions, reporter) { | 18 DEF_TEST(CompressAlphaFailDimensions, reporter) { |
22 SkBitmap bitmap; | 19 SkBitmap bitmap; |
23 static const int kWidth = 63; | 20 static const int kWidth = 17; |
24 static const int kHeight = 63; | 21 static const int kHeight = 17; |
25 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); | 22 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
26 REPORTER_ASSERT(reporter, kWidth % kLATCBlockDimension != 0); | 23 |
27 REPORTER_ASSERT(reporter, kHeight % kLATCBlockDimension != 0); | 24 // R11_EAC and LATC are both dimensions of 4, so we need to make sure that w
e |
| 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 i
s |
| 27 // also not divisible by 12. Our dimensions are prime, so any block dimensio
n |
| 28 // larger than 1 should fail. |
| 29 REPORTER_ASSERT(reporter, kWidth % 4 != 0); |
| 30 REPORTER_ASSERT(reporter, kHeight % 4 != 0); |
28 | 31 |
29 bool setInfoSuccess = bitmap.setInfo(info); | 32 bool setInfoSuccess = bitmap.setInfo(info); |
30 REPORTER_ASSERT(reporter, setInfoSuccess); | 33 REPORTER_ASSERT(reporter, setInfoSuccess); |
31 | 34 |
32 bool allocPixelsSuccess = bitmap.allocPixels(info); | 35 bool allocPixelsSuccess = bitmap.allocPixels(info); |
33 REPORTER_ASSERT(reporter, allocPixelsSuccess); | 36 REPORTER_ASSERT(reporter, allocPixelsSuccess); |
34 bitmap.unlockPixels(); | 37 bitmap.unlockPixels(); |
35 | 38 |
36 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; | 39 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
37 SkAutoDataUnref latcData(SkTextureCompressor::CompressBitmapToFormat(bitmap,
kLATCFormat)); | 40 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
38 REPORTER_ASSERT(reporter, NULL == latcData); | 41 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 42 REPORTER_ASSERT(reporter, NULL == data); |
| 43 } |
39 } | 44 } |
40 | 45 |
41 /** | 46 /** |
42 * Make sure that we properly fail when we don't have the correct bitmap type. | 47 * Make sure that we properly fail when we don't have the correct bitmap type. |
43 * LATC compressed textures can only be created from A8 bitmaps. | 48 * compressed textures can (currently) only be created from A8 bitmaps. |
44 */ | 49 */ |
45 DEF_TEST(CompressLATCFailColorType, reporter) { | 50 DEF_TEST(CompressAlphaFailColorType, reporter) { |
46 SkBitmap bitmap; | 51 SkBitmap bitmap; |
47 static const int kWidth = 64; | 52 static const int kWidth = 12; |
48 static const int kHeight = 64; | 53 static const int kHeight = 12; |
49 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); | 54 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
50 REPORTER_ASSERT(reporter, kWidth % kLATCBlockDimension == 0); | 55 |
51 REPORTER_ASSERT(reporter, kHeight % kLATCBlockDimension == 0); | 56 // ASTC is at most 12x12, and any dimension divisible by 12 is also divisibl
e |
| 57 // by 4, which is the dimensions of R11_EAC and LATC. In the future, we migh
t |
| 58 // support additional variants of ASTC, such as 5x6 and 8x8, in which case t
his would |
| 59 // need to be updated. |
| 60 REPORTER_ASSERT(reporter, kWidth % 12 == 0); |
| 61 REPORTER_ASSERT(reporter, kHeight % 12 == 0); |
52 | 62 |
53 bool setInfoSuccess = bitmap.setInfo(info); | 63 bool setInfoSuccess = bitmap.setInfo(info); |
54 REPORTER_ASSERT(reporter, setInfoSuccess); | 64 REPORTER_ASSERT(reporter, setInfoSuccess); |
55 | 65 |
56 bool allocPixelsSuccess = bitmap.allocPixels(info); | 66 bool allocPixelsSuccess = bitmap.allocPixels(info); |
57 REPORTER_ASSERT(reporter, allocPixelsSuccess); | 67 REPORTER_ASSERT(reporter, allocPixelsSuccess); |
58 bitmap.unlockPixels(); | 68 bitmap.unlockPixels(); |
59 | 69 |
60 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; | 70 for (int i = 0; i < SkTextureCompressor::kFormatCnt; ++i) { |
61 SkAutoDataUnref latcData(SkTextureCompressor::CompressBitmapToFormat(bitmap,
kLATCFormat)); | 71 const SkTextureCompressor::Format fmt = static_cast<SkTextureCompressor:
:Format>(i); |
62 REPORTER_ASSERT(reporter, NULL == latcData); | 72 SkAutoDataUnref data(SkTextureCompressor::CompressBitmapToFormat(bitmap,
fmt)); |
| 73 REPORTER_ASSERT(reporter, NULL == data); |
| 74 } |
63 } | 75 } |
64 | 76 |
65 /** | 77 /** |
66 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results | 78 * Make sure that if we pass in a solid color bitmap that we get the appropriate
results |
67 */ | 79 */ |
68 DEF_TEST(CompressLATC, reporter) { | 80 DEF_TEST(CompressLATC, reporter) { |
| 81 |
| 82 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLATC_F
ormat; |
| 83 static const int kLATCEncodedBlockSize = 8; |
| 84 |
69 SkBitmap bitmap; | 85 SkBitmap bitmap; |
70 static const int kWidth = 8; | 86 static const int kWidth = 8; |
71 static const int kHeight = 8; | 87 static const int kHeight = 8; |
72 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); | 88 SkImageInfo info = SkImageInfo::MakeA8(kWidth, kHeight); |
73 | 89 |
74 bool setInfoSuccess = bitmap.setInfo(info); | 90 bool setInfoSuccess = bitmap.setInfo(info); |
75 REPORTER_ASSERT(reporter, setInfoSuccess); | 91 REPORTER_ASSERT(reporter, setInfoSuccess); |
76 | 92 |
77 bool allocPixelsSuccess = bitmap.allocPixels(info); | 93 bool allocPixelsSuccess = bitmap.allocPixels(info); |
78 REPORTER_ASSERT(reporter, allocPixelsSuccess); | 94 REPORTER_ASSERT(reporter, allocPixelsSuccess); |
79 bitmap.unlockPixels(); | 95 bitmap.unlockPixels(); |
80 | 96 |
81 REPORTER_ASSERT(reporter, kWidth % kLATCBlockDimension == 0); | 97 int latcDimX, latcDimY; |
82 REPORTER_ASSERT(reporter, kHeight % kLATCBlockDimension == 0); | 98 SkTextureCompressor::GetBlockDimensions(kLATCFormat, &latcDimX, &latcDimY); |
83 const int numBlocks = (kWidth / kLATCBlockDimension) * (kHeight / kLATCBlock
Dimension); | 99 |
84 const size_t kSizeToBe = static_cast<size_t>(kLATCEncodedBlockSize * numBloc
ks); | 100 REPORTER_ASSERT(reporter, kWidth % latcDimX == 0); |
| 101 REPORTER_ASSERT(reporter, kHeight % latcDimY == 0); |
| 102 const size_t kSizeToBe = |
| 103 SkTextureCompressor::GetCompressedDataSize(kLATCFormat, kWidth, kHeight)
; |
| 104 REPORTER_ASSERT(reporter, kSizeToBe == ((kWidth*kHeight*kLATCEncodedBlockSiz
e)/16)); |
| 105 REPORTER_ASSERT(reporter, (kSizeToBe % kLATCEncodedBlockSize) == 0); |
85 | 106 |
86 for (int lum = 0; lum < 256; ++lum) { | 107 for (int lum = 0; lum < 256; ++lum) { |
87 bitmap.lockPixels(); | 108 bitmap.lockPixels(); |
88 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); | 109 uint8_t* pixels = reinterpret_cast<uint8_t*>(bitmap.getPixels()); |
89 REPORTER_ASSERT(reporter, NULL != pixels); | 110 REPORTER_ASSERT(reporter, NULL != pixels); |
90 | 111 |
91 for (int i = 0; i < kWidth*kHeight; ++i) { | 112 for (int i = 0; i < kWidth*kHeight; ++i) { |
92 pixels[i] = lum; | 113 pixels[i] = lum; |
93 } | 114 } |
94 bitmap.unlockPixels(); | 115 bitmap.unlockPixels(); |
95 | 116 |
96 const SkTextureCompressor::Format kLATCFormat = SkTextureCompressor::kLA
TC_Format; | |
97 SkAutoDataUnref latcData( | 117 SkAutoDataUnref latcData( |
98 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); | 118 SkTextureCompressor::CompressBitmapToFormat(bitmap, kLATCFormat)); |
99 REPORTER_ASSERT(reporter, NULL != latcData); | 119 REPORTER_ASSERT(reporter, NULL != latcData); |
100 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); | 120 REPORTER_ASSERT(reporter, kSizeToBe == latcData->size()); |
101 | 121 |
102 // Make sure that it all matches a given block encoding. If the entire b
itmap | 122 // Make sure that it all matches a given block encoding. Since we have |
103 // is a single value, then the lower two bytes of the encoded data shoul
d be that | 123 // COMPRESS_LATC_FAST defined in SkTextureCompressor_LATC.cpp, we are us
ing |
104 // value. The remaining indices can be any value, and since we try to ma
tch the pixels | 124 // an approximation scheme that optimizes for speed against coverage map
s. |
105 // in the chosen palette in increasing index order, each one should be z
ero. Hence, | 125 // That means that each palette in the encoded block is exactly the same
, |
106 // the correct encoding should be just the two luminance values in the b
ottom two | 126 // and that the three bits saved per pixel are computed from the top thr
ee |
107 // bytes of the block encoding. | 127 // bits of the luminance value. |
108 const uint64_t kConstColorEncoding = SkEndian_SwapLE64(lum | (lum << 8))
; | 128 const uint64_t kIndexEncodingMap[8] = { 1, 7, 6, 5, 4, 3, 2, 0 }; |
| 129 const uint64_t kIndex = kIndexEncodingMap[lum >> 5]; |
| 130 const uint64_t kConstColorEncoding = |
| 131 SkEndian_SwapLE64( |
| 132 255 | |
| 133 (kIndex << 16) | (kIndex << 19) | (kIndex << 22) | (kIndex << 25
) | |
| 134 (kIndex << 28) | (kIndex << 31) | (kIndex << 34) | (kIndex << 37
) | |
| 135 (kIndex << 40) | (kIndex << 43) | (kIndex << 46) | (kIndex << 49
) | |
| 136 (kIndex << 52) | (kIndex << 55) | (kIndex << 58) | (kIndex << 61
)); |
| 137 |
109 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); | 138 const uint64_t* blockPtr = reinterpret_cast<const uint64_t*>(latcData->d
ata()); |
110 for (int i = 0; i < numBlocks; ++i) { | 139 for (size_t i = 0; i < (kSizeToBe/8); ++i) { |
111 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); | 140 REPORTER_ASSERT(reporter, blockPtr[i] == kConstColorEncoding); |
112 } | 141 } |
113 } | 142 } |
114 } | 143 } |
OLD | NEW |