Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: src/utils/SkTextureCompressor_LATC.cpp

Issue 403383003: Refactor texture compressors into separate files (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Handle improper dimensions Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/utils/SkTextureCompressor_LATC.h ('k') | src/utils/SkTextureCompressor_R11EAC.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2014 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 "SkTextureCompressor_LATC.h"
9
10 #include "SkEndian.h"
11
12 ////////////////////////////////////////////////////////////////////////////////
13 //
14 // Utility Functions
15 //
16 ////////////////////////////////////////////////////////////////////////////////
17
18 // Absolute difference between two values. More correct than SkTAbs(a - b)
19 // because it works on unsigned values.
20 template <typename T> inline T abs_diff(const T &a, const T &b) {
21 return (a > b) ? (a - b) : (b - a);
22 }
23
24 static bool is_extremal(uint8_t pixel) {
25 return 0 == pixel || 255 == pixel;
26 }
27
28 typedef uint64_t (*A84x4To64BitProc)(const uint8_t block[]);
29
30 // This function is used by both R11 EAC and LATC to compress 4x4 blocks
31 // of 8-bit alpha into 64-bit values that comprise the compressed data.
32 // For both formats, we need to make sure that the dimensions of the
33 // src pixels are divisible by 4, and copy 4x4 blocks one at a time
34 // for compression.
35 static bool compress_4x4_a8_to_64bit(uint8_t* dst, const uint8_t* src,
36 int width, int height, int rowBytes,
37 A84x4To64BitProc proc) {
38 // Make sure that our data is well-formed enough to be considered for compre ssion
39 if (0 == width || 0 == height || (width % 4) != 0 || (height % 4) != 0) {
40 return false;
41 }
42
43 int blocksX = width >> 2;
44 int blocksY = height >> 2;
45
46 uint8_t block[16];
47 uint64_t* encPtr = reinterpret_cast<uint64_t*>(dst);
48 for (int y = 0; y < blocksY; ++y) {
49 for (int x = 0; x < blocksX; ++x) {
50 // Load block
51 for (int k = 0; k < 4; ++k) {
52 memcpy(block + k*4, src + k*rowBytes + 4*x, 4);
53 }
54
55 // Compress it
56 *encPtr = proc(block);
57 ++encPtr;
58 }
59 src += 4 * rowBytes;
60 }
61
62 return true;
63 }
64
65 ////////////////////////////////////////////////////////////////////////////////
66 //
67 // LATC compressor
68 //
69 ////////////////////////////////////////////////////////////////////////////////
70
71 // LATC compressed texels down into square 4x4 blocks
72 static const int kLATCPaletteSize = 8;
73 static const int kLATCBlockSize = 4;
74 static const int kLATCPixelsPerBlock = kLATCBlockSize * kLATCBlockSize;
75
76 // Generates an LATC palette. LATC constructs
77 // a palette of eight colors from LUM0 and LUM1 using the algorithm:
78 //
79 // LUM0, if lum0 > lum1 and code(x,y) == 0
80 // LUM1, if lum0 > lum1 and code(x,y) == 1
81 // (6*LUM0+ LUM1)/7, if lum0 > lum1 and code(x,y) == 2
82 // (5*LUM0+2*LUM1)/7, if lum0 > lum1 and code(x,y) == 3
83 // (4*LUM0+3*LUM1)/7, if lum0 > lum1 and code(x,y) == 4
84 // (3*LUM0+4*LUM1)/7, if lum0 > lum1 and code(x,y) == 5
85 // (2*LUM0+5*LUM1)/7, if lum0 > lum1 and code(x,y) == 6
86 // ( LUM0+6*LUM1)/7, if lum0 > lum1 and code(x,y) == 7
87 //
88 // LUM0, if lum0 <= lum1 and code(x,y) == 0
89 // LUM1, if lum0 <= lum1 and code(x,y) == 1
90 // (4*LUM0+ LUM1)/5, if lum0 <= lum1 and code(x,y) == 2
91 // (3*LUM0+2*LUM1)/5, if lum0 <= lum1 and code(x,y) == 3
92 // (2*LUM0+3*LUM1)/5, if lum0 <= lum1 and code(x,y) == 4
93 // ( LUM0+4*LUM1)/5, if lum0 <= lum1 and code(x,y) == 5
94 // 0, if lum0 <= lum1 and code(x,y) == 6
95 // 255, if lum0 <= lum1 and code(x,y) == 7
96
97 static void generate_latc_palette(uint8_t palette[], uint8_t lum0, uint8_t lum1) {
98 palette[0] = lum0;
99 palette[1] = lum1;
100 if (lum0 > lum1) {
101 for (int i = 1; i < 7; i++) {
102 palette[i+1] = ((7-i)*lum0 + i*lum1) / 7;
103 }
104 } else {
105 for (int i = 1; i < 5; i++) {
106 palette[i+1] = ((5-i)*lum0 + i*lum1) / 5;
107 }
108 palette[6] = 0;
109 palette[7] = 255;
110 }
111 }
112
113 // Compress a block by using the bounding box of the pixels. It is assumed that
114 // there are no extremal pixels in this block otherwise we would have used
115 // compressBlockBBIgnoreExtremal.
116 static uint64_t compress_latc_block_bb(const uint8_t pixels[]) {
117 uint8_t minVal = 255;
118 uint8_t maxVal = 0;
119 for (int i = 0; i < kLATCPixelsPerBlock; ++i) {
120 minVal = SkTMin(pixels[i], minVal);
121 maxVal = SkTMax(pixels[i], maxVal);
122 }
123
124 SkASSERT(!is_extremal(minVal));
125 SkASSERT(!is_extremal(maxVal));
126
127 uint8_t palette[kLATCPaletteSize];
128 generate_latc_palette(palette, maxVal, minVal);
129
130 uint64_t indices = 0;
131 for (int i = kLATCPixelsPerBlock - 1; i >= 0; --i) {
132
133 // Find the best palette index
134 uint8_t bestError = abs_diff(pixels[i], palette[0]);
135 uint8_t idx = 0;
136 for (int j = 1; j < kLATCPaletteSize; ++j) {
137 uint8_t error = abs_diff(pixels[i], palette[j]);
138 if (error < bestError) {
139 bestError = error;
140 idx = j;
141 }
142 }
143
144 indices <<= 3;
145 indices |= idx;
146 }
147
148 return
149 SkEndian_SwapLE64(
150 static_cast<uint64_t>(maxVal) |
151 (static_cast<uint64_t>(minVal) << 8) |
152 (indices << 16));
153 }
154
155 // Compress a block by using the bounding box of the pixels without taking into
156 // account the extremal values. The generated palette will contain extremal valu es
157 // and fewer points along the line segment to interpolate.
158 static uint64_t compress_latc_block_bb_ignore_extremal(const uint8_t pixels[]) {
159 uint8_t minVal = 255;
160 uint8_t maxVal = 0;
161 for (int i = 0; i < kLATCPixelsPerBlock; ++i) {
162 if (is_extremal(pixels[i])) {
163 continue;
164 }
165
166 minVal = SkTMin(pixels[i], minVal);
167 maxVal = SkTMax(pixels[i], maxVal);
168 }
169
170 SkASSERT(!is_extremal(minVal));
171 SkASSERT(!is_extremal(maxVal));
172
173 uint8_t palette[kLATCPaletteSize];
174 generate_latc_palette(palette, minVal, maxVal);
175
176 uint64_t indices = 0;
177 for (int i = kLATCPixelsPerBlock - 1; i >= 0; --i) {
178
179 // Find the best palette index
180 uint8_t idx = 0;
181 if (is_extremal(pixels[i])) {
182 if (0xFF == pixels[i]) {
183 idx = 7;
184 } else if (0 == pixels[i]) {
185 idx = 6;
186 } else {
187 SkFAIL("Pixel is extremal but not really?!");
188 }
189 } else {
190 uint8_t bestError = abs_diff(pixels[i], palette[0]);
191 for (int j = 1; j < kLATCPaletteSize - 2; ++j) {
192 uint8_t error = abs_diff(pixels[i], palette[j]);
193 if (error < bestError) {
194 bestError = error;
195 idx = j;
196 }
197 }
198 }
199
200 indices <<= 3;
201 indices |= idx;
202 }
203
204 return
205 SkEndian_SwapLE64(
206 static_cast<uint64_t>(minVal) |
207 (static_cast<uint64_t>(maxVal) << 8) |
208 (indices << 16));
209 }
210
211
212 // Compress LATC block. Each 4x4 block of pixels is decompressed by LATC from tw o
213 // values LUM0 and LUM1, and an index into the generated palette. Details of how
214 // the palette is generated can be found in the comments of generatePalette abov e.
215 //
216 // We choose which palette type to use based on whether or not 'pixels' contains
217 // any extremal values (0 or 255). If there are extremal values, then we use the
218 // palette that has the extremal values built in. Otherwise, we use the full bou nding
219 // box.
220
221 static uint64_t compress_latc_block(const uint8_t pixels[]) {
222 // Collect unique pixels
223 int nUniquePixels = 0;
224 uint8_t uniquePixels[kLATCPixelsPerBlock];
225 for (int i = 0; i < kLATCPixelsPerBlock; ++i) {
226 bool foundPixel = false;
227 for (int j = 0; j < nUniquePixels; ++j) {
228 foundPixel = foundPixel || uniquePixels[j] == pixels[i];
229 }
230
231 if (!foundPixel) {
232 uniquePixels[nUniquePixels] = pixels[i];
233 ++nUniquePixels;
234 }
235 }
236
237 // If there's only one unique pixel, then our compression is easy.
238 if (1 == nUniquePixels) {
239 return SkEndian_SwapLE64(pixels[0] | (pixels[0] << 8));
240
241 // Similarly, if there are only two unique pixels, then our compression is
242 // easy again: place the pixels in the block header, and assign the indices
243 // with one or zero depending on which pixel they belong to.
244 } else if (2 == nUniquePixels) {
245 uint64_t outBlock = 0;
246 for (int i = kLATCPixelsPerBlock - 1; i >= 0; --i) {
247 int idx = 0;
248 if (pixels[i] == uniquePixels[1]) {
249 idx = 1;
250 }
251
252 outBlock <<= 3;
253 outBlock |= idx;
254 }
255 outBlock <<= 16;
256 outBlock |= (uniquePixels[0] | (uniquePixels[1] << 8));
257 return SkEndian_SwapLE64(outBlock);
258 }
259
260 // Count non-maximal pixel values
261 int nonExtremalPixels = 0;
262 for (int i = 0; i < nUniquePixels; ++i) {
263 if (!is_extremal(uniquePixels[i])) {
264 ++nonExtremalPixels;
265 }
266 }
267
268 // If all the pixels are nonmaximal then compute the palette using
269 // the bounding box of all the pixels.
270 if (nonExtremalPixels == nUniquePixels) {
271 // This is really just for correctness, in all of my tests we
272 // never take this step. We don't lose too much perf here because
273 // most of the processing in this function is worth it for the
274 // 1 == nUniquePixels optimization.
275 return compress_latc_block_bb(pixels);
276 } else {
277 return compress_latc_block_bb_ignore_extremal(pixels);
278 }
279 }
280
281 ////////////////////////////////////////////////////////////////////////////////
282
283 namespace SkTextureCompressor {
284
285 bool CompressA8ToLATC(uint8_t* dst, const uint8_t* src, int width, int height, i nt rowBytes) {
286 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_ latc_block);
287 }
288
289 SkBlitter* CreateLATCBlitter(int width, int height, void* outputBuffer) {
290 // TODO (krajcevski)
291 return NULL;
292 }
293
294 } // SkTextureCompressor
OLDNEW
« no previous file with comments | « src/utils/SkTextureCompressor_LATC.h ('k') | src/utils/SkTextureCompressor_R11EAC.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698