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 cc { | |
22 | |
23 namespace { | 21 namespace { |
24 | 22 |
25 // Constructs a color from a given base color and luminance value. | 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 /** |
| 193 * Constructs a color from a given base color and luminance value. |
| 194 */ |
26 inline Color MakeColor(const Color& base, int16_t lum) { | 195 inline Color MakeColor(const Color& base, int16_t lum) { |
27 int b = static_cast<int>(base.channels.b) + lum; | 196 int b = static_cast<int>(base.channels.b) + lum; |
28 int g = static_cast<int>(base.channels.g) + lum; | 197 int g = static_cast<int>(base.channels.g) + lum; |
29 int r = static_cast<int>(base.channels.r) + lum; | 198 int r = static_cast<int>(base.channels.r) + lum; |
30 Color color; | 199 Color color; |
31 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); | 200 color.channels.b = static_cast<uint8_t>(clamp(b, 0, 255)); |
32 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); | 201 color.channels.g = static_cast<uint8_t>(clamp(g, 0, 255)); |
33 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255)); | 202 color.channels.r = static_cast<uint8_t>(clamp(r, 0, 255)); |
34 return color; | 203 return color; |
35 } | 204 } |
36 | 205 |
37 // Calculates the error metric for two colors. A small error signals that the | 206 /** |
38 // colors are similar to each other, a large error the signals the opposite. | 207 * Calculates the error metric for two colors. A small error signals that the |
| 208 * colors are similar to each other, a large error the signals the opposite. |
| 209 */ |
39 inline uint32_t GetColorError(const Color& u, const Color& v) { | 210 inline uint32_t GetColorError(const Color& u, const Color& v) { |
40 #ifdef USE_PERCEIVED_ERROR_METRIC | 211 #ifdef USE_PERCEIVED_ERROR_METRIC |
41 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; | 212 float delta_b = static_cast<float>(u.channels.b) - v.channels.b; |
42 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; | 213 float delta_g = static_cast<float>(u.channels.g) - v.channels.g; |
43 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; | 214 float delta_r = static_cast<float>(u.channels.r) - v.channels.r; |
44 return static_cast<uint32_t>(0.299f * delta_b * delta_b + | 215 return static_cast<uint32_t>(0.299f * delta_b * delta_b + |
45 0.587f * delta_g * delta_g + | 216 0.587f * delta_g * delta_g + |
46 0.114f * delta_r * delta_r); | 217 0.114f * delta_r * delta_r); |
47 #else | 218 #else |
48 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; | 219 int delta_b = static_cast<int>(u.channels.b) - v.channels.b; |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 sub_block_avg[sub_block_off_0], 0, | 454 sub_block_avg[sub_block_off_0], 0, |
284 g_idx_to_num[sub_block_off_0]); | 455 g_idx_to_num[sub_block_off_0]); |
285 // Compute luminance for the second sub block. | 456 // Compute luminance for the second sub block. |
286 ComputeLuminance(dst, sub_block_src[sub_block_off_1], | 457 ComputeLuminance(dst, sub_block_src[sub_block_off_1], |
287 sub_block_avg[sub_block_off_1], 1, | 458 sub_block_avg[sub_block_off_1], 1, |
288 g_idx_to_num[sub_block_off_1]); | 459 g_idx_to_num[sub_block_off_1]); |
289 } | 460 } |
290 | 461 |
291 } // namespace | 462 } // namespace |
292 | 463 |
| 464 namespace cc { |
| 465 |
293 void TextureCompressorETC1::Compress(const uint8_t* src, | 466 void TextureCompressorETC1::Compress(const uint8_t* src, |
294 uint8_t* dst, | 467 uint8_t* dst, |
295 int width, | 468 int width, |
296 int height, | 469 int height, |
297 Quality quality) { | 470 Quality quality) { |
298 DCHECK_GE(width, 4); | 471 DCHECK(width >= 4 && (width & 3) == 0); |
299 DCHECK_EQ((width & 3), 0); | 472 DCHECK(height >= 4 && (height & 3) == 0); |
300 DCHECK_GE(height, 4); | |
301 DCHECK_EQ((height & 3), 0); | |
302 | 473 |
303 Color ver_blocks[16]; | 474 Color ver_blocks[16]; |
304 Color hor_blocks[16]; | 475 Color hor_blocks[16]; |
305 | 476 |
306 for (int y = 0; y < height; y += 4, src += width * 4 * 4) { | 477 for (int y = 0; y < height; y += 4, src += width * 4 * 4) { |
307 for (int x = 0; x < width; x += 4, dst += 8) { | 478 for (int x = 0; x < width; x += 4, dst += 8) { |
308 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4); | 479 const Color* row0 = reinterpret_cast<const Color*>(src + x * 4); |
309 const Color* row1 = row0 + width; | 480 const Color* row1 = row0 + width; |
310 const Color* row2 = row1 + width; | 481 const Color* row2 = row1 + width; |
311 const Color* row3 = row2 + width; | 482 const Color* row3 = row2 + width; |
(...skipping 11 matching lines...) Expand all Loading... |
323 memcpy(hor_blocks + 4, row1, 16); | 494 memcpy(hor_blocks + 4, row1, 16); |
324 memcpy(hor_blocks + 8, row2, 16); | 495 memcpy(hor_blocks + 8, row2, 16); |
325 memcpy(hor_blocks + 12, row3, 16); | 496 memcpy(hor_blocks + 12, row3, 16); |
326 | 497 |
327 CompressBlock(dst, ver_blocks, hor_blocks); | 498 CompressBlock(dst, ver_blocks, hor_blocks); |
328 } | 499 } |
329 } | 500 } |
330 } | 501 } |
331 | 502 |
332 } // namespace cc | 503 } // namespace cc |
OLD | NEW |