OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // See the following specification for details on the ETC1 format: | 5 // See the following specification for details on the ETC1 format: |
6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8
_texture.txt | 6 // https://www.khronos.org/registry/gles/extensions/OES/OES_compressed_ETC1_RGB8
_texture.txt |
7 | 7 |
8 #include "cc/resources/texture_compressor_etc1.h" | 8 #include "cc/resources/texture_compressor_etc1.h" |
9 | 9 |
10 #include <string.h> | 10 #include <string.h> |
11 #include <limits> | 11 #include <limits> |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 | 14 |
15 // Defining the following macro will cause the error metric function to weigh | 15 // Defining the following macro will cause the error metric function to weigh |
16 // each color channel differently depending on how the human eye can perceive | 16 // each color channel differently depending on how the human eye can perceive |
17 // them. This can give a slight improvement in image quality at the cost of a | 17 // them. This can give a slight improvement in image quality at the cost of a |
18 // performance hit. | 18 // performance hit. |
19 // #define USE_PERCEIVED_ERROR_METRIC | 19 // #define USE_PERCEIVED_ERROR_METRIC |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 template <typename T> | |
24 inline T clamp(T val, T min, T max) { | |
25 return val < min ? min : (val > max ? max : val); | |
26 } | |
27 | |
28 inline uint8_t round_to_5_bits(float val) { | |
29 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31); | |
30 } | |
31 | |
32 inline uint8_t round_to_4_bits(float val) { | |
33 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15); | |
34 } | |
35 | |
36 union Color { | |
37 struct BgraColorType { | |
38 uint8_t b; | |
39 uint8_t g; | |
40 uint8_t r; | |
41 uint8_t a; | |
42 } channels; | |
43 uint8_t components[4]; | |
44 uint32_t bits; | |
45 }; | |
46 | |
47 /* | |
48 * Codeword tables. | |
49 * See: Table 3.17.2 | |
50 */ | |
51 static const int16_t g_codeword_tables[8][4] = {{-8, -2, 2, 8}, | |
52 {-17, -5, 5, 17}, | |
53 {-29, -9, 9, 29}, | |
54 {-42, -13, 13, 42}, | |
55 {-60, -18, 18, 60}, | |
56 {-80, -24, 24, 80}, | |
57 {-106, -33, 33, 106}, | |
58 {-183, -47, 47, 183}}; | |
59 | |
60 /* | |
61 * Maps modifier indices to pixel index values. | |
62 * See: Table 3.17.3 | |
63 */ | |
64 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1}; | |
65 | |
66 /* | |
67 * The ETC1 specification index texels as follows: | |
68 * | |
69 * [a][e][i][m] [ 0][ 4][ 8][12] | |
70 * [b][f][j][n] <-> [ 1][ 5][ 9][13] | |
71 * [c][g][k][o] [ 2][ 6][10][14] | |
72 * [d][h][l][p] [ 3][ 7][11][15] | |
73 * | |
74 * However, when extracting sub blocks from BGRA data the natural array | |
75 * indexing order ends up different: | |
76 * | |
77 * vertical0: [a][e][b][f] horizontal0: [a][e][i][m] | |
78 * [c][g][d][h] [b][f][j][n] | |
79 * vertical1: [i][m][j][n] horizontal1: [c][g][k][o] | |
80 * [k][o][l][p] [d][h][l][p] | |
81 * | |
82 * In order to translate from the natural array indices in a sub block to the | |
83 * indices (number) used by specification and hardware we use this table. | |
84 */ | |
85 static const uint8_t g_idx_to_num[4][8] = { | |
86 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0. | |
87 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1. | |
88 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0. | |
89 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1. | |
90 }; | |
91 | |
92 inline void WriteColors444(uint8_t* block, | |
93 const Color& color0, | |
94 const Color& color1) { | |
95 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4); | |
96 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4); | |
97 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4); | |
98 } | |
99 | |
100 inline void WriteColors555(uint8_t* block, | |
101 const Color& color0, | |
102 const Color& color1) { | |
103 // Table for conversion to 3-bit two complement format. | |
104 static const uint8_t two_compl_trans_table[8] = { | |
105 4, // -4 (100b) | |
106 5, // -3 (101b) | |
107 6, // -2 (110b) | |
108 7, // -1 (111b) | |
109 0, // 0 (000b) | |
110 1, // 1 (001b) | |
111 2, // 2 (010b) | |
112 3, // 3 (011b) | |
113 }; | |
114 | |
115 int16_t delta_r = | |
116 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3); | |
117 int16_t delta_g = | |
118 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3); | |
119 int16_t delta_b = | |
120 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3); | |
121 DCHECK(delta_r >= -4 && delta_r <= 3); | |
122 DCHECK(delta_g >= -4 && delta_g <= 3); | |
123 DCHECK(delta_b >= -4 && delta_b <= 3); | |
124 | |
125 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4]; | |
126 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4]; | |
127 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4]; | |
128 } | |
129 | |
130 inline void WriteCodewordTable(uint8_t* block, | |
131 uint8_t sub_block_id, | |
132 uint8_t table) { | |
133 DCHECK_LT(sub_block_id, 2); | |
134 DCHECK_LT(table, 8); | |
135 | |
136 uint8_t shift = (2 + (3 - sub_block_id * 3)); | |
137 block[3] &= ~(0x07 << shift); | |
138 block[3] |= table << shift; | |
139 } | |
140 | |
141 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) { | |
142 block[4] |= pixel_data >> 24; | |
143 block[5] |= (pixel_data >> 16) & 0xff; | |
144 block[6] |= (pixel_data >> 8) & 0xff; | |
145 block[7] |= pixel_data & 0xff; | |
146 } | |
147 | |
148 inline void WriteFlip(uint8_t* block, bool flip) { | |
149 block[3] &= ~0x01; | |
150 block[3] |= static_cast<uint8_t>(flip); | |
151 } | |
152 | |
153 inline void WriteDiff(uint8_t* block, bool diff) { | |
154 block[3] &= ~0x02; | |
155 block[3] |= static_cast<uint8_t>(diff) << 1; | |
156 } | |
157 | |
158 /** | |
159 * Compress and rounds BGR888 into BGR444. The resulting BGR444 color is | |
160 * expanded to BGR888 as it would be in hardware after decompression. The | |
161 * actual 444-bit data is available in the four most significant bits of each | |
162 * channel. | |
163 */ | |
164 inline Color MakeColor444(const float* bgr) { | |
165 uint8_t b4 = round_to_4_bits(bgr[0]); | |
166 uint8_t g4 = round_to_4_bits(bgr[1]); | |
167 uint8_t r4 = round_to_4_bits(bgr[2]); | |
168 Color bgr444; | |
169 bgr444.channels.b = (b4 << 4) | b4; | |
170 bgr444.channels.g = (g4 << 4) | g4; | |
171 bgr444.channels.r = (r4 << 4) | r4; | |
172 return bgr444; | |
173 } | |
174 | |
175 /** | |
176 * Compress and rounds BGR888 into BGR555. The resulting BGR555 color is | |
177 * expanded to BGR888 as it would be in hardware after decompression. The | |
178 * actual 555-bit data is available in the five most significant bits of each | |
179 * channel. | |
180 */ | |
181 inline Color MakeColor555(const float* bgr) { | |
182 uint8_t b5 = round_to_5_bits(bgr[0]); | |
183 uint8_t g5 = round_to_5_bits(bgr[1]); | |
184 uint8_t r5 = round_to_5_bits(bgr[2]); | |
185 Color bgr555; | |
186 bgr555.channels.b = (b5 << 3) | (b5 >> 2); | |
187 bgr555.channels.g = (g5 << 3) | (g5 >> 2); | |
188 bgr555.channels.r = (r5 << 3) | (r5 >> 2); | |
189 return bgr555; | |
190 } | |
191 | |
192 /** | 23 /** |
193 * Constructs a color from a given base color and luminance value. | 24 * Constructs a color from a given base color and luminance value. |
194 */ | 25 */ |
195 inline Color MakeColor(const Color& base, int16_t lum) { | 26 inline Color MakeColor(const Color& base, int16_t lum) { |
196 int b = static_cast<int>(base.channels.b) + lum; | 27 int b = static_cast<int>(base.channels.b) + lum; |
197 int g = static_cast<int>(base.channels.g) + lum; | 28 int g = static_cast<int>(base.channels.g) + lum; |
198 int r = static_cast<int>(base.channels.r) + lum; | 29 int r = static_cast<int>(base.channels.r) + lum; |
199 Color color; | 30 Color color; |
200 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); | 31 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); |
201 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); | 32 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 memcpy(hor_blocks + 4, row1, 16); | 325 memcpy(hor_blocks + 4, row1, 16); |
495 memcpy(hor_blocks + 8, row2, 16); | 326 memcpy(hor_blocks + 8, row2, 16); |
496 memcpy(hor_blocks + 12, row3, 16); | 327 memcpy(hor_blocks + 12, row3, 16); |
497 | 328 |
498 CompressBlock(dst, ver_blocks, hor_blocks); | 329 CompressBlock(dst, ver_blocks, hor_blocks); |
499 } | 330 } |
500 } | 331 } |
501 } | 332 } |
502 | 333 |
503 } // namespace cc | 334 } // namespace cc |
OLD | NEW |