Add ICU message format support
[chromium-blink-merge.git] / cc / raster / texture_compressor_etc1.h
blob2d2771a888c0c6c1e5bff4fb8eea3f5c6896707c
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
3 // found in the LICENSE file.
5 #ifndef CC_RASTER_TEXTURE_COMPRESSOR_ETC1_H_
6 #define CC_RASTER_TEXTURE_COMPRESSOR_ETC1_H_
8 #include "cc/raster/texture_compressor.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
13 namespace cc {
15 template <typename T>
16 inline T clamp(T val, T min, T max) {
17 return val < min ? min : (val > max ? max : val);
20 inline uint8_t round_to_5_bits(float val) {
21 return clamp<uint8_t>(val * 31.0f / 255.0f + 0.5f, 0, 31);
24 inline uint8_t round_to_4_bits(float val) {
25 return clamp<uint8_t>(val * 15.0f / 255.0f + 0.5f, 0, 15);
28 union Color {
29 struct BgraColorType {
30 uint8_t b;
31 uint8_t g;
32 uint8_t r;
33 uint8_t a;
34 } channels;
35 uint8_t components[4];
36 uint32_t bits;
39 // Codeword tables.
40 // See: Table 3.17.2
41 ALIGNAS(16) static const int16_t g_codeword_tables[8][4] = {
42 {-8, -2, 2, 8},
43 {-17, -5, 5, 17},
44 {-29, -9, 9, 29},
45 {-42, -13, 13, 42},
46 {-60, -18, 18, 60},
47 {-80, -24, 24, 80},
48 {-106, -33, 33, 106},
49 {-183, -47, 47, 183}};
51 // Maps modifier indices to pixel index values.
52 // See: Table 3.17.3
53 static const uint8_t g_mod_to_pix[4] = {3, 2, 0, 1};
55 // The ETC1 specification index texels as follows:
56 // [a][e][i][m] [ 0][ 4][ 8][12]
57 // [b][f][j][n] <-> [ 1][ 5][ 9][13]
58 // [c][g][k][o] [ 2][ 6][10][14]
59 // [d][h][l][p] [ 3][ 7][11][15]
61 // [ 0][ 1][ 2][ 3] [ 0][ 1][ 4][ 5]
62 // [ 4][ 5][ 6][ 7] <-> [ 8][ 9][12][13]
63 // [ 8][ 9][10][11] [ 2][ 3][ 6][ 7]
64 // [12][13][14][15] [10][11][14][15]
66 // However, when extracting sub blocks from BGRA data the natural array
67 // indexing order ends up different:
68 // vertical0: [a][e][b][f] horizontal0: [a][e][i][m]
69 // [c][g][d][h] [b][f][j][n]
70 // vertical1: [i][m][j][n] horizontal1: [c][g][k][o]
71 // [k][o][l][p] [d][h][l][p]
73 // In order to translate from the natural array indices in a sub block to the
74 // indices (number) used by specification and hardware we use this table.
75 static const uint8_t g_idx_to_num[4][8] = {
76 {0, 4, 1, 5, 2, 6, 3, 7}, // Vertical block 0.
77 {8, 12, 9, 13, 10, 14, 11, 15}, // Vertical block 1.
78 {0, 4, 8, 12, 1, 5, 9, 13}, // Horizontal block 0.
79 {2, 6, 10, 14, 3, 7, 11, 15} // Horizontal block 1.
82 inline void WriteColors444(uint8_t* block,
83 const Color& color0,
84 const Color& color1) {
85 // Write output color for BGRA textures.
86 block[0] = (color0.channels.r & 0xf0) | (color1.channels.r >> 4);
87 block[1] = (color0.channels.g & 0xf0) | (color1.channels.g >> 4);
88 block[2] = (color0.channels.b & 0xf0) | (color1.channels.b >> 4);
91 inline void WriteColors555(uint8_t* block,
92 const Color& color0,
93 const Color& color1) {
94 // Table for conversion to 3-bit two complement format.
95 static const uint8_t two_compl_trans_table[8] = {
96 4, // -4 (100b)
97 5, // -3 (101b)
98 6, // -2 (110b)
99 7, // -1 (111b)
100 0, // 0 (000b)
101 1, // 1 (001b)
102 2, // 2 (010b)
103 3, // 3 (011b)
106 int16_t delta_r =
107 static_cast<int16_t>(color1.channels.r >> 3) - (color0.channels.r >> 3);
108 int16_t delta_g =
109 static_cast<int16_t>(color1.channels.g >> 3) - (color0.channels.g >> 3);
110 int16_t delta_b =
111 static_cast<int16_t>(color1.channels.b >> 3) - (color0.channels.b >> 3);
112 DCHECK_GE(delta_r, -4);
113 DCHECK_LE(delta_r, 3);
114 DCHECK_GE(delta_g, -4);
115 DCHECK_LE(delta_g, 3);
116 DCHECK_GE(delta_b, -4);
117 DCHECK_LE(delta_b, 3);
119 // Write output color for BGRA textures.
120 block[0] = (color0.channels.r & 0xf8) | two_compl_trans_table[delta_r + 4];
121 block[1] = (color0.channels.g & 0xf8) | two_compl_trans_table[delta_g + 4];
122 block[2] = (color0.channels.b & 0xf8) | two_compl_trans_table[delta_b + 4];
125 inline void WriteCodewordTable(uint8_t* block,
126 uint8_t sub_block_id,
127 uint8_t table) {
128 DCHECK_LT(sub_block_id, 2);
129 DCHECK_LT(table, 8);
131 uint8_t shift = (2 + (3 - sub_block_id * 3));
132 block[3] &= ~(0x07 << shift);
133 block[3] |= table << shift;
136 inline void WritePixelData(uint8_t* block, uint32_t pixel_data) {
137 block[4] |= pixel_data >> 24;
138 block[5] |= (pixel_data >> 16) & 0xff;
139 block[6] |= (pixel_data >> 8) & 0xff;
140 block[7] |= pixel_data & 0xff;
143 inline void WriteFlip(uint8_t* block, bool flip) {
144 block[3] &= ~0x01;
145 block[3] |= static_cast<uint8_t>(flip);
148 inline void WriteDiff(uint8_t* block, bool diff) {
149 block[3] &= ~0x02;
150 block[3] |= static_cast<uint8_t>(diff) << 1;
153 // Compress and rounds BGR888 into BGR444. The resulting BGR444 color is
154 // expanded to BGR888 as it would be in hardware after decompression. The
155 // actual 444-bit data is available in the four most significant bits of each
156 // channel.
157 inline Color MakeColor444(const float* bgr) {
158 uint8_t b4 = round_to_4_bits(bgr[0]);
159 uint8_t g4 = round_to_4_bits(bgr[1]);
160 uint8_t r4 = round_to_4_bits(bgr[2]);
161 Color bgr444;
162 bgr444.channels.b = (b4 << 4) | b4;
163 bgr444.channels.g = (g4 << 4) | g4;
164 bgr444.channels.r = (r4 << 4) | r4;
165 // Added to distinguish between expanded 555 and 444 colors.
166 bgr444.channels.a = 0x44;
167 return bgr444;
170 // Compress and rounds BGR888 into BGR555. The resulting BGR555 color is
171 // expanded to BGR888 as it would be in hardware after decompression. The
172 // actual 555-bit data is available in the five most significant bits of each
173 // channel.
174 inline Color MakeColor555(const float* bgr) {
175 uint8_t b5 = round_to_5_bits(bgr[0]);
176 uint8_t g5 = round_to_5_bits(bgr[1]);
177 uint8_t r5 = round_to_5_bits(bgr[2]);
178 Color bgr555;
179 bgr555.channels.b = (b5 << 3) | (b5 >> 2);
180 bgr555.channels.g = (g5 << 3) | (g5 >> 2);
181 bgr555.channels.r = (r5 << 3) | (r5 >> 2);
182 // Added to distinguish between expanded 555 and 444 colors.
183 bgr555.channels.a = 0x55;
184 return bgr555;
187 class CC_EXPORT TextureCompressorETC1 : public TextureCompressor {
188 public:
189 TextureCompressorETC1() {}
191 // Compress a texture using ETC1. Note that the |quality| parameter is
192 // ignored. The current implementation does not support different quality
193 // settings.
194 void Compress(const uint8_t* src,
195 uint8_t* dst,
196 int width,
197 int height,
198 Quality quality) override;
200 private:
201 DISALLOW_COPY_AND_ASSIGN(TextureCompressorETC1);
204 } // namespace cc
206 #endif // CC_RASTER_TEXTURE_COMPRESSOR_ETC1_H_