1 /*
2 * Copyright 2020 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 "src/core/SkCompressedDataUtils.h"
9
10 #include "include/core/SkColorPriv.h"
11 #include "include/core/SkData.h"
12 #include "include/private/SkColorData.h"
13 #include "src/core/SkMathPriv.h"
14 #include "src/core/SkMipMap.h"
15
16 struct ETC1Block {
17 uint32_t fHigh;
18 uint32_t fLow;
19 };
20
21 constexpr uint32_t kFlipBit = 0x1; // set -> T/B sub-blocks; not-set -> L/R sub-blocks
22 constexpr uint32_t kDiffBit = 0x2; // set -> differential; not-set -> individual
23
extend_4To8bits(int b)24 static inline int extend_4To8bits(int b) {
25 int c = b & 0xf;
26 return (c << 4) | c;
27 }
28
extend_5To8bits(int b)29 static inline int extend_5To8bits(int b) {
30 int c = b & 0x1f;
31 return (c << 3) | (c >> 2);
32 }
33
extend_5plus3To8Bits(int base,int diff)34 static inline int extend_5plus3To8Bits(int base, int diff) {
35 static const int kLookup[8] = { 0, 1, 2, 3, -4, -3, -2, -1 };
36
37 return extend_5To8bits((0x1f & base) + kLookup[0x7 & diff]);
38 }
39
40 static const int kNumETC1ModifierTables = 8;
41 static const int kNumETC1PixelIndices = 4;
42
43 // The index of each row in this table is the ETC1 table codeword
44 // The index of each column in this table is the ETC1 pixel index value
45 static const int kETC1ModifierTables[kNumETC1ModifierTables][kNumETC1PixelIndices] = {
46 /* 0 */ { 2, 8, -2, -8 },
47 /* 1 */ { 5, 17, -5, -17 },
48 /* 2 */ { 9, 29, -9, -29 },
49 /* 3 */ { 13, 42, -13, -42 },
50 /* 4 */ { 18, 60, -18, -60 },
51 /* 5 */ { 24, 80, -24, -80 },
52 /* 6 */ { 33, 106, -33, -106 },
53 /* 7 */ { 47, 183, -47, -183 }
54 };
55
num_4x4_blocks(int size)56 static int num_4x4_blocks(int size) {
57 return ((size + 3) & ~3) >> 2;
58 }
59
60 // Return which sub-block a given x,y location in the overall 4x4 block belongs to
xy_to_subblock_index(int x,int y,bool flip)61 static int xy_to_subblock_index(int x, int y, bool flip) {
62 SkASSERT(x >= 0 && x < 4);
63 SkASSERT(y >= 0 && y < 4);
64
65 if (flip) {
66 return y < 2 ? 0 : 1; // sub-block 1 is on top of sub-block 2
67 } else {
68 return x < 2 ? 0 : 1; // sub-block 1 is to the left of sub-block 2
69 }
70 }
71
72 struct IColor {
73 int fR, fG, fB;
74 };
75
add_delta_and_clamp(const IColor & col,int delta)76 static SkPMColor add_delta_and_clamp(const IColor& col, int delta) {
77 int r8 = SkTPin(col.fR + delta, 0, 255);
78 int g8 = SkTPin(col.fG + delta, 0, 255);
79 int b8 = SkTPin(col.fB + delta, 0, 255);
80
81 return SkPackARGB32(0xFF, r8, g8, b8);
82 }
83
decompress_etc1(SkISize dimensions,const uint8_t * srcData,SkBitmap * dst)84 static bool decompress_etc1(SkISize dimensions, const uint8_t* srcData, SkBitmap* dst) {
85 const ETC1Block* srcBlocks = reinterpret_cast<const ETC1Block*>(srcData);
86
87 int numXBlocks = num_4x4_blocks(dimensions.width());
88 int numYBlocks = num_4x4_blocks(dimensions.height());
89
90 for (int y = 0; y < numYBlocks; ++y) {
91 for (int x = 0; x < numXBlocks; ++x) {
92 const ETC1Block* curBlock1 = &srcBlocks[y * numXBlocks + x];
93 uint32_t high = SkBSwap32(curBlock1->fHigh);
94 uint32_t low = SkBSwap32(curBlock1->fLow);
95
96 bool flipped = SkToBool(high & kFlipBit);
97 bool differential = SkToBool(high & kDiffBit);
98
99 IColor colors[2];
100
101 if (differential) {
102 colors[0].fR = extend_5To8bits(high >> 27);
103 colors[1].fR = extend_5plus3To8Bits(high >> 27, high >> 24);
104 colors[0].fG = extend_5To8bits(high >> 19);
105 colors[1].fG = extend_5plus3To8Bits(high >> 19, high >> 16);
106 colors[0].fB = extend_5To8bits(high >> 11);
107 colors[1].fB = extend_5plus3To8Bits(high >> 11, high >> 8);
108 } else {
109 colors[0].fR = extend_4To8bits(high >> 28);
110 colors[1].fR = extend_4To8bits(high >> 24);
111 colors[0].fG = extend_4To8bits(high >> 20);
112 colors[1].fG = extend_4To8bits(high >> 16);
113 colors[0].fB = extend_4To8bits(high >> 12);
114 colors[1].fB = extend_4To8bits(high >> 8);
115 }
116
117 int tableIndex0 = (high >> 5) & 0x7;
118 int tableIndex1 = (high >> 2) & 0x7;
119 const int* tables[2] = {
120 kETC1ModifierTables[tableIndex0],
121 kETC1ModifierTables[tableIndex1]
122 };
123
124 int baseShift = 0;
125 int offsetX = 4 * x, offsetY = 4 * y;
126 for (int i = 0; i < 4; ++i, ++baseShift) {
127 for (int j = 0; j < 4; ++j) {
128 if (offsetX + j >= dst->width() || offsetY + i >= dst->height()) {
129 // This can happen for the topmost levels of a mipmap and for
130 // non-multiple of 4 textures
131 continue;
132 }
133
134 int subBlockIndex = xy_to_subblock_index(j, i, flipped);
135 int pixelIndex = ((low >> (baseShift+(j*4))) & 0x1) |
136 (low >> (baseShift+(j*4)+15) & 0x2);
137
138 SkASSERT(subBlockIndex == 0 || subBlockIndex == 1);
139 SkASSERT(pixelIndex >= 0 && pixelIndex < 4);
140
141 int delta = tables[subBlockIndex][pixelIndex];
142 *dst->getAddr32(offsetX + j, offsetY + i) =
143 add_delta_and_clamp(colors[subBlockIndex], delta);
144 }
145 }
146 }
147 }
148
149 return true;
150 }
151
152 //------------------------------------------------------------------------------------------------
153 struct BC1Block {
154 uint16_t fColor0;
155 uint16_t fColor1;
156 uint32_t fIndices;
157 };
158
from565(uint16_t rgb565)159 static SkPMColor from565(uint16_t rgb565) {
160 uint8_t r8 = SkR16ToR32((rgb565 >> 11) & 0x1F);
161 uint8_t g8 = SkG16ToG32((rgb565 >> 5) & 0x3F);
162 uint8_t b8 = SkB16ToB32(rgb565 & 0x1F);
163
164 return SkPackARGB32(0xFF, r8, g8, b8);
165 }
166
167 // return t*col0 + (1-t)*col1
lerp(float t,SkPMColor col0,SkPMColor col1)168 static SkPMColor lerp(float t, SkPMColor col0, SkPMColor col1) {
169 SkASSERT(SkGetPackedA32(col0) == 0xFF && SkGetPackedA32(col1) == 0xFF);
170
171 // TODO: given 't' is only either 1/3 or 2/3 this could be done faster
172 uint8_t r8 = SkScalarRoundToInt(t * SkGetPackedR32(col0) + (1.0f - t) * SkGetPackedR32(col1));
173 uint8_t g8 = SkScalarRoundToInt(t * SkGetPackedG32(col0) + (1.0f - t) * SkGetPackedG32(col1));
174 uint8_t b8 = SkScalarRoundToInt(t * SkGetPackedB32(col0) + (1.0f - t) * SkGetPackedB32(col1));
175 return SkPackARGB32(0xFF, r8, g8, b8);
176 }
177
decompress_bc1(SkISize dimensions,const uint8_t * srcData,bool isOpaque,SkBitmap * dst)178 static bool decompress_bc1(SkISize dimensions, const uint8_t* srcData,
179 bool isOpaque, SkBitmap* dst) {
180 const BC1Block* srcBlocks = reinterpret_cast<const BC1Block*>(srcData);
181
182 int numXBlocks = num_4x4_blocks(dimensions.width());
183 int numYBlocks = num_4x4_blocks(dimensions.height());
184
185 SkPMColor colors[4];
186
187 for (int y = 0; y < numYBlocks; ++y) {
188 for (int x = 0; x < numXBlocks; ++x) {
189 const BC1Block* curBlock = &srcBlocks[y * numXBlocks + x];
190
191 colors[0] = from565(curBlock->fColor0);
192 colors[1] = from565(curBlock->fColor1);
193 if (curBlock->fColor0 <= curBlock->fColor1) { // signal for a transparent block
194 colors[2] = SkPackARGB32(
195 0xFF,
196 (SkGetPackedR32(colors[0]) + SkGetPackedR32(colors[1])) >> 1,
197 (SkGetPackedG32(colors[0]) + SkGetPackedG32(colors[1])) >> 1,
198 (SkGetPackedB32(colors[0]) + SkGetPackedB32(colors[1])) >> 1);
199 // The opacity of the overall texture trumps the per-block transparency
200 colors[3] = SkPackARGB32(isOpaque ? 0xFF : 0, 0, 0, 0);
201 } else {
202 colors[2] = lerp(2.0f/3.0f, colors[0], colors[1]);
203 colors[3] = lerp(1.0f/3.0f, colors[0], colors[1]);
204 }
205
206 int shift = 0;
207 int offsetX = 4 * x, offsetY = 4 * y;
208 for (int i = 0; i < 4; ++i) {
209 for (int j = 0; j < 4; ++j, shift += 2) {
210 if (offsetX + j >= dst->width() || offsetY + i >= dst->height()) {
211 // This can happen for the topmost levels of a mipmap and for
212 // non-multiple of 4 textures
213 continue;
214 }
215
216 int index = (curBlock->fIndices >> shift) & 0x3;
217 *dst->getAddr32(offsetX + j, offsetY + i) = colors[index];
218 }
219 }
220 }
221 }
222
223 return true;
224 }
225
SkDecompress(sk_sp<SkData> data,SkISize dimensions,SkImage::CompressionType compressionType,SkBitmap * dst)226 bool SkDecompress(sk_sp<SkData> data,
227 SkISize dimensions,
228 SkImage::CompressionType compressionType,
229 SkBitmap* dst) {
230 using Type = SkImage::CompressionType;
231
232 const uint8_t* bytes = data->bytes();
233 switch (compressionType) {
234 case Type::kNone: return false;
235 case Type::kETC2_RGB8_UNORM: return decompress_etc1(dimensions, bytes, dst);
236 case Type::kBC1_RGB8_UNORM: return decompress_bc1(dimensions, bytes, true, dst);
237 case Type::kBC1_RGBA8_UNORM: return decompress_bc1(dimensions, bytes, false, dst);
238 }
239
240 SkUNREACHABLE;
241 return false;
242 }
243
SkCompressedDataSize(SkImage::CompressionType type,SkISize dimensions,SkTArray<size_t> * individualMipOffsets,bool mipMapped)244 size_t SkCompressedDataSize(SkImage::CompressionType type, SkISize dimensions,
245 SkTArray<size_t>* individualMipOffsets, bool mipMapped) {
246 SkASSERT(!individualMipOffsets || !individualMipOffsets->count());
247
248 int numMipLevels = 1;
249 if (mipMapped) {
250 numMipLevels = SkMipMap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
251 }
252
253 size_t totalSize = 0;
254 switch (type) {
255 case SkImage::CompressionType::kNone:
256 break;
257 case SkImage::CompressionType::kETC2_RGB8_UNORM:
258 case SkImage::CompressionType::kBC1_RGB8_UNORM:
259 case SkImage::CompressionType::kBC1_RGBA8_UNORM: {
260 for (int i = 0; i < numMipLevels; ++i) {
261 int numBlocks = num_4x4_blocks(dimensions.width()) *
262 num_4x4_blocks(dimensions.height());
263
264 if (individualMipOffsets) {
265 individualMipOffsets->push_back(totalSize);
266 }
267
268 static_assert(sizeof(ETC1Block) == sizeof(BC1Block));
269 totalSize += numBlocks * sizeof(ETC1Block);
270
271 dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)};
272 }
273 break;
274 }
275 }
276
277 return totalSize;
278 }
279
SkCompressedFormatDataSize(SkImage::CompressionType compressionType,SkISize dimensions,bool mipMapped)280 size_t SkCompressedFormatDataSize(SkImage::CompressionType compressionType,
281 SkISize dimensions, bool mipMapped) {
282 return SkCompressedDataSize(compressionType, dimensions, nullptr, mipMapped);
283 }
284