Debian Codegraph @

tools/gpu/CompressedTexture.cpp

raw
1 /*
2 * Copyright 2025 Google LLC
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 "tools/gpu/CompressedTexture.h"
9
10 // The following four helpers are copied from src/gpu/DataUtils.cpp to support the test only
11 // GrTwoColorBC1Compress function. Ideally we would copy the test function into DataUtils.cpp
12 // instead, but we're currently trying to avoid using the GPU_TEST_UTILS define in src/gpu.
13
14 static int num_4x4_blocks(int size) { return ((size + 3) & ~3) >> 2; }
15
16 struct BC1Block {
17 uint16_t fColor0;
18 uint16_t fColor1;
19 uint32_t fIndices;
20 };
21
22 static uint16_t to565(SkColor col) {
23 int r5 = SkMulDiv255Round(31, SkColorGetR(col));
24 int g6 = SkMulDiv255Round(63, SkColorGetG(col));
25 int b5 = SkMulDiv255Round(31, SkColorGetB(col));
26
27 return (r5 << 11) | (g6 << 5) | b5;
28 }
29
30 // Create a BC1 compressed block that has two colors but is initialized to 'col0'
31 static void create_BC1_block(SkColor col0, SkColor col1, BC1Block* block) {
32 block->fColor0 = to565(col0);
33 block->fColor1 = to565(col1);
34 SkASSERT(block->fColor0 <= block->fColor1); // we always assume transparent blocks
35
36 if (col0 == SK_ColorTRANSPARENT) {
37 // This sets all 16 pixels to just use color3 (under the assumption
38 // that this is a kBC1_RGBA8_UNORM texture. Note that in this case
39 // fColor0 will be opaque black.
40 block->fIndices = 0xFFFFFFFF;
41 } else {
42 // This sets all 16 pixels to just use 'fColor0'
43 block->fIndices = 0;
44 }
45 }
46
47 namespace sk_gpu_test {
48 // Fill in 'dstPixels' with BC1 blocks derived from the 'pixmap'.
49 void TwoColorBC1Compress(const SkPixmap& pixmap, SkColor otherColor, char* dstPixels) {
50 BC1Block* dstBlocks = reinterpret_cast<BC1Block*>(dstPixels);
51 SkASSERT(pixmap.colorType() == SkColorType::kRGBA_8888_SkColorType);
52
53 BC1Block block;
54
55 // black -> fColor0, otherColor -> fColor1
56 create_BC1_block(SK_ColorBLACK, otherColor, &block);
57
58 int numXBlocks = num_4x4_blocks(pixmap.width());
59 int numYBlocks = num_4x4_blocks(pixmap.height());
60
61 for (int y = 0; y < numYBlocks; ++y) {
62 for (int x = 0; x < numXBlocks; ++x) {
63 int shift = 0;
64 int offsetX = 4 * x, offsetY = 4 * y;
65 block.fIndices = 0; // init all the pixels to color0 (i.e., opaque black)
66 for (int i = 0; i < 4; ++i) {
67 for (int j = 0; j < 4; ++j, shift += 2) {
68 if (offsetX + j >= pixmap.width() || offsetY + i >= pixmap.height()) {
69 // This can happen for the topmost levels of a mipmap and for
70 // non-multiple of 4 textures
71 continue;
72 }
73
74 SkColor tmp = pixmap.getColor(offsetX + j, offsetY + i);
75 if (tmp == SK_ColorTRANSPARENT) {
76 // For RGBA BC1 images color3 is set to transparent black
77 block.fIndices |= 3 << shift;
78 } else if (tmp != SK_ColorBLACK) {
79 block.fIndices |= 1 << shift; // color1
80 }
81 }
82 }
83
84 dstBlocks[y * numXBlocks + x] = block;
85 }
86 }
87 }
88
89 } // namespace sk_gpu_test