Coverage Report

Created: 2021-08-22 09:07

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