blob: 7baa219a28de88d05e2a6966b85b9d45ab2534e6 [file] [log] [blame]
krajcevski6c354882014-07-22 07:44:00 -07001/*
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.h"
krajcevskid5e46c72014-07-28 14:14:16 -07009#include "SkTextureCompressor_Blitter.h"
krajcevski6c354882014-07-22 07:44:00 -070010
11#include "SkEndian.h"
12
13// #define COMPRESS_R11_EAC_SLOW 1
14// #define COMPRESS_R11_EAC_FAST 1
15#define COMPRESS_R11_EAC_FASTEST 1
16
17// Blocks compressed into R11 EAC are represented as follows:
18// 0000000000000000000000000000000000000000000000000000000000000000
19// |base_cw|mod|mul| ----------------- indices -------------------
20//
21// To reconstruct the value of a given pixel, we use the formula:
22// clamp[0, 2047](base_cw * 8 + 4 + mod_val*mul*8)
23//
24// mod_val is chosen from a palette of values based on the index of the
25// given pixel. The palette is chosen by the value stored in mod.
26// This formula returns a value between 0 and 2047, which is converted
27// to a float from 0 to 1 in OpenGL.
28//
29// If mul is zero, then we set mul = 1/8, so that the formula becomes
30// clamp[0, 2047](base_cw * 8 + 4 + mod_val)
31
krajcevski6c354882014-07-22 07:44:00 -070032static const int kNumR11EACPalettes = 16;
33static const int kR11EACPaletteSize = 8;
34static const int kR11EACModifierPalettes[kNumR11EACPalettes][kR11EACPaletteSize] = {
35 {-3, -6, -9, -15, 2, 5, 8, 14},
36 {-3, -7, -10, -13, 2, 6, 9, 12},
37 {-2, -5, -8, -13, 1, 4, 7, 12},
38 {-2, -4, -6, -13, 1, 3, 5, 12},
39 {-3, -6, -8, -12, 2, 5, 7, 11},
40 {-3, -7, -9, -11, 2, 6, 8, 10},
41 {-4, -7, -8, -11, 3, 6, 7, 10},
42 {-3, -5, -8, -11, 2, 4, 7, 10},
43 {-2, -6, -8, -10, 1, 5, 7, 9},
44 {-2, -5, -8, -10, 1, 4, 7, 9},
45 {-2, -4, -8, -10, 1, 3, 7, 9},
46 {-2, -5, -7, -10, 1, 4, 6, 9},
47 {-3, -4, -7, -10, 2, 3, 6, 9},
48 {-1, -2, -3, -10, 0, 1, 2, 9},
49 {-4, -6, -8, -9, 3, 5, 7, 8},
50 {-3, -5, -7, -9, 2, 4, 6, 8}
51};
52
krajcevski4ad76e32014-07-31 14:12:50 -070053#if COMPRESS_R11_EAC_SLOW
54
krajcevski6c354882014-07-22 07:44:00 -070055// Pack the base codeword, palette, and multiplier into the 64 bits necessary
56// to decode it.
57static uint64_t pack_r11eac_block(uint16_t base_cw, uint16_t palette, uint16_t multiplier,
58 uint64_t indices) {
59 SkASSERT(palette < 16);
60 SkASSERT(multiplier < 16);
61 SkASSERT(indices < (static_cast<uint64_t>(1) << 48));
62
63 const uint64_t b = static_cast<uint64_t>(base_cw) << 56;
64 const uint64_t m = static_cast<uint64_t>(multiplier) << 52;
65 const uint64_t p = static_cast<uint64_t>(palette) << 48;
66 return SkEndian_SwapBE64(b | m | p | indices);
67}
68
69// Given a base codeword, a modifier, and a multiplier, compute the proper
70// pixel value in the range [0, 2047].
71static uint16_t compute_r11eac_pixel(int base_cw, int modifier, int multiplier) {
72 int ret = (base_cw * 8 + 4) + (modifier * multiplier * 8);
73 return (ret > 2047)? 2047 : ((ret < 0)? 0 : ret);
74}
75
76// Compress a block into R11 EAC format.
77// The compression works as follows:
78// 1. Find the center of the span of the block's values. Use this as the base codeword.
79// 2. Choose a multiplier based roughly on the size of the span of block values
80// 3. Iterate through each palette and choose the one with the most accurate
81// modifiers.
82static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
83 // Find the center of the data...
84 uint16_t bmin = block[0];
85 uint16_t bmax = block[0];
86 for (int i = 1; i < 16; ++i) {
87 bmin = SkTMin<uint16_t>(bmin, block[i]);
88 bmax = SkTMax<uint16_t>(bmax, block[i]);
89 }
90
91 uint16_t center = (bmax + bmin) >> 1;
92 SkASSERT(center <= 255);
93
94 // Based on the min and max, we can guesstimate a proper multiplier
95 // This is kind of a magic choice to start with.
96 uint16_t multiplier = (bmax - center) / 10;
97
98 // Now convert the block to 11 bits and transpose it to match
99 // the proper layout
100 uint16_t cblock[16];
101 for (int i = 0; i < 4; ++i) {
102 for (int j = 0; j < 4; ++j) {
103 int srcIdx = i*4+j;
104 int dstIdx = j*4+i;
105 cblock[dstIdx] = (block[srcIdx] << 3) | (block[srcIdx] >> 5);
106 }
107 }
108
109 // Finally, choose the proper palette and indices
110 uint32_t bestError = 0xFFFFFFFF;
111 uint64_t bestIndices = 0;
112 uint16_t bestPalette = 0;
113 for (uint16_t paletteIdx = 0; paletteIdx < kNumR11EACPalettes; ++paletteIdx) {
114 const int *palette = kR11EACModifierPalettes[paletteIdx];
115
116 // Iterate through each pixel to find the best palette index
117 // and update the indices with the choice. Also store the error
118 // for this palette to be compared against the best error...
119 uint32_t error = 0;
120 uint64_t indices = 0;
121 for (int pixelIdx = 0; pixelIdx < 16; ++pixelIdx) {
122 const uint16_t pixel = cblock[pixelIdx];
123
124 // Iterate through each palette value to find the best index
125 // for this particular pixel for this particular palette.
126 uint16_t bestPixelError =
127 abs_diff(pixel, compute_r11eac_pixel(center, palette[0], multiplier));
128 int bestIndex = 0;
129 for (int i = 1; i < kR11EACPaletteSize; ++i) {
130 const uint16_t p = compute_r11eac_pixel(center, palette[i], multiplier);
131 const uint16_t perror = abs_diff(pixel, p);
132
133 // Is this index better?
134 if (perror < bestPixelError) {
135 bestIndex = i;
136 bestPixelError = perror;
137 }
138 }
139
140 SkASSERT(bestIndex < 8);
141
142 error += bestPixelError;
143 indices <<= 3;
144 indices |= bestIndex;
145 }
146
147 SkASSERT(indices < (static_cast<uint64_t>(1) << 48));
148
149 // Is this palette better?
150 if (error < bestError) {
151 bestPalette = paletteIdx;
152 bestIndices = indices;
153 bestError = error;
154 }
155 }
156
157 // Finally, pack everything together...
158 return pack_r11eac_block(center, bestPalette, multiplier, bestIndices);
159}
160#endif // COMPRESS_R11_EAC_SLOW
161
162#if COMPRESS_R11_EAC_FAST
163// This function takes into account that most blocks that we compress have a gradation from
164// fully opaque to fully transparent. The compression scheme works by selecting the
165// palette and multiplier that has the tightest fit to the 0-255 range. This is encoded
166// as the block header (0x8490). The indices are then selected by considering the top
167// three bits of each alpha value. For alpha masks, this reduces the dynamic range from
168// 17 to 8, but the quality is still acceptable.
169//
170// There are a few caveats that need to be taken care of...
171//
172// 1. The block is read in as scanlines, so the indices are stored as:
173// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
174// However, the decomrpession routine reads them in column-major order, so they
175// need to be packed as:
176// 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15
177// So when reading, they must be transposed.
178//
179// 2. We cannot use the top three bits as an index directly, since the R11 EAC palettes
180// above store the modulation values first decreasing and then increasing:
181// e.g. {-3, -6, -9, -15, 2, 5, 8, 14}
182// Hence, we need to convert the indices with the following mapping:
183// From: 0 1 2 3 4 5 6 7
184// To: 3 2 1 0 4 5 6 7
185static inline uint64_t compress_heterogeneous_r11eac_block(const uint8_t block[16]) {
186 uint64_t retVal = static_cast<uint64_t>(0x8490) << 48;
187 for(int i = 0; i < 4; ++i) {
188 for(int j = 0; j < 4; ++j) {
189 const int shift = 45-3*(j*4+i);
190 SkASSERT(shift <= 45);
191 const uint64_t idx = block[i*4+j] >> 5;
192 SkASSERT(idx < 8);
193
194 // !SPEED! This is slightly faster than having an if-statement.
195 switch(idx) {
196 case 0:
197 case 1:
198 case 2:
199 case 3:
200 retVal |= (3-idx) << shift;
201 break;
202 default:
203 retVal |= idx << shift;
204 break;
205 }
206 }
207 }
208
209 return SkEndian_SwapBE64(retVal);
210}
211#endif // COMPRESS_R11_EAC_FAST
212
213#if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
214static uint64_t compress_r11eac_block(const uint8_t block[16]) {
215 // Are all blocks a solid color?
216 bool solid = true;
217 for (int i = 1; i < 16; ++i) {
218 if (block[i] != block[0]) {
219 solid = false;
220 break;
221 }
222 }
223
224 if (solid) {
225 switch(block[0]) {
226 // Fully transparent? We know the encoding...
227 case 0:
228 // (0x0020 << 48) produces the following:
229 // basw_cw: 0
230 // mod: 0, palette: {-3, -6, -9, -15, 2, 5, 8, 14}
231 // multiplier: 2
232 // mod_val: -3
233 //
234 // this gives the following formula:
235 // clamp[0, 2047](0*8+4+(-3)*2*8) = 0
236 //
237 // Furthermore, it is impervious to endianness:
238 // 0x0020000000002000ULL
239 // Will produce one pixel with index 2, which gives:
240 // clamp[0, 2047](0*8+4+(-9)*2*8) = 0
241 return 0x0020000000002000ULL;
242
243 // Fully opaque? We know this encoding too...
244 case 255:
245
246 // -1 produces the following:
247 // basw_cw: 255
248 // mod: 15, palette: {-3, -5, -7, -9, 2, 4, 6, 8}
249 // mod_val: 8
250 //
251 // this gives the following formula:
252 // clamp[0, 2047](255*8+4+8*8*8) = clamp[0, 2047](2556) = 2047
253 return 0xFFFFFFFFFFFFFFFFULL;
254
255 default:
256 // !TODO! krajcevski:
257 // This will probably never happen, since we're using this format
258 // primarily for compressing alpha maps. Usually the only
259 // non-fullly opaque or fully transparent blocks are not a solid
260 // intermediate color. If we notice that they are, then we can
261 // add another optimization...
262 break;
263 }
264 }
265
266 return compress_heterogeneous_r11eac_block(block);
267}
268
269// This function is used by R11 EAC to compress 4x4 blocks
270// of 8-bit alpha into 64-bit values that comprise the compressed data.
271// We need to make sure that the dimensions of the src pixels are divisible
272// by 4, and copy 4x4 blocks one at a time for compression.
273typedef uint64_t (*A84x4To64BitProc)(const uint8_t block[]);
274
275static bool compress_4x4_a8_to_64bit(uint8_t* dst, const uint8_t* src,
276 int width, int height, int rowBytes,
277 A84x4To64BitProc proc) {
278 // Make sure that our data is well-formed enough to be considered for compression
279 if (0 == width || 0 == height || (width % 4) != 0 || (height % 4) != 0) {
280 return false;
281 }
282
283 int blocksX = width >> 2;
284 int blocksY = height >> 2;
285
286 uint8_t block[16];
287 uint64_t* encPtr = reinterpret_cast<uint64_t*>(dst);
288 for (int y = 0; y < blocksY; ++y) {
289 for (int x = 0; x < blocksX; ++x) {
290 // Load block
291 for (int k = 0; k < 4; ++k) {
292 memcpy(block + k*4, src + k*rowBytes + 4*x, 4);
293 }
294
295 // Compress it
296 *encPtr = proc(block);
297 ++encPtr;
298 }
299 src += 4 * rowBytes;
300 }
301
302 return true;
303}
304#endif // (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
305
krajcevskid5e46c72014-07-28 14:14:16 -0700306// This function converts an integer containing four bytes of alpha
307// values into an integer containing four bytes of indices into R11 EAC.
308// Note, there needs to be a mapping of indices:
309// 0 1 2 3 4 5 6 7
310// 3 2 1 0 4 5 6 7
311//
312// To compute this, we first negate each byte, and then add three, which
313// gives the mapping
314// 3 2 1 0 -1 -2 -3 -4
315//
316// Then we mask out the negative values, take their absolute value, and
317// add three.
318//
319// Most of the voodoo in this function comes from Hacker's Delight, section 2-18
320static inline uint32_t convert_indices(uint32_t x) {
321 // Take the top three bits...
322 x = (x & 0xE0E0E0E0) >> 5;
323
324 // Negate...
325 x = ~((0x80808080 - x) ^ 0x7F7F7F7F);
326
327 // Add three
328 const uint32_t s = (x & 0x7F7F7F7F) + 0x03030303;
329 x = ((x ^ 0x03030303) & 0x80808080) ^ s;
330
331 // Absolute value
332 const uint32_t a = x & 0x80808080;
333 const uint32_t b = a >> 7;
334
335 // Aside: mask negatives (m is three if the byte was negative)
336 const uint32_t m = (a >> 6) | b;
337
338 // .. continue absolute value
339 x = (x ^ ((a - b) | a)) + b;
340
341 // Add three
342 return x + m;
343}
344
krajcevski6c354882014-07-22 07:44:00 -0700345#if COMPRESS_R11_EAC_FASTEST
346template<unsigned shift>
347static inline uint64_t swap_shift(uint64_t x, uint64_t mask) {
348 const uint64_t t = (x ^ (x >> shift)) & mask;
349 return x ^ t ^ (t << shift);
350}
351
352static inline uint64_t interleave6(uint64_t topRows, uint64_t bottomRows) {
353 // If our 3-bit block indices are laid out as:
354 // a b c d
355 // e f g h
356 // i j k l
357 // m n o p
358 //
359 // This function expects topRows and bottomRows to contain the first two rows
360 // of indices interleaved in the least significant bits of a and b. In other words...
361 //
362 // If the architecture is big endian, then topRows and bottomRows will contain the following:
363 // Bits 31-0:
364 // a: 00 a e 00 b f 00 c g 00 d h
365 // b: 00 i m 00 j n 00 k o 00 l p
366 //
367 // If the architecture is little endian, then topRows and bottomRows will contain
368 // the following:
369 // Bits 31-0:
370 // a: 00 d h 00 c g 00 b f 00 a e
371 // b: 00 l p 00 k o 00 j n 00 i m
372 //
373 // This function returns a 48-bit packing of the form:
374 // a e i m b f j n c g k o d h l p
375 //
376 // !SPEED! this function might be even faster if certain SIMD intrinsics are
377 // used..
378
379 // For both architectures, we can figure out a packing of the bits by
380 // using a shuffle and a few shift-rotates...
381 uint64_t x = (static_cast<uint64_t>(topRows) << 32) | static_cast<uint64_t>(bottomRows);
382
383 // x: 00 a e 00 b f 00 c g 00 d h 00 i m 00 j n 00 k o 00 l p
384
385 x = swap_shift<10>(x, 0x3FC0003FC00000ULL);
386
387 // x: b f 00 00 00 a e c g i m 00 00 00 d h j n 00 k o 00 l p
388
389 x = (x | ((x << 52) & (0x3FULL << 52)) | ((x << 20) & (0x3FULL << 28))) >> 16;
390
391 // x: 00 00 00 00 00 00 00 00 b f l p a e c g i m k o d h j n
392
393 x = swap_shift<6>(x, 0xFC0000ULL);
394
395#if defined (SK_CPU_BENDIAN)
396 // x: 00 00 00 00 00 00 00 00 b f l p a e i m c g k o d h j n
397
398 x = swap_shift<36>(x, 0x3FULL);
399
400 // x: 00 00 00 00 00 00 00 00 b f j n a e i m c g k o d h l p
401
402 x = swap_shift<12>(x, 0xFFF000000ULL);
403#else
404 // If our CPU is little endian, then the above logic will
405 // produce the following indices:
406 // x: 00 00 00 00 00 00 00 00 c g i m d h l p b f j n a e k o
407
408 x = swap_shift<36>(x, 0xFC0ULL);
409
410 // x: 00 00 00 00 00 00 00 00 a e i m d h l p b f j n c g k o
411
412 x = (x & (0xFFFULL << 36)) | ((x & 0xFFFFFFULL) << 12) | ((x >> 24) & 0xFFFULL);
413#endif
414
415 // x: 00 00 00 00 00 00 00 00 a e i m b f j n c g k o d h l p
416 return x;
417}
418
krajcevski6c354882014-07-22 07:44:00 -0700419// This function follows the same basic procedure as compress_heterogeneous_r11eac_block
420// above when COMPRESS_R11_EAC_FAST is defined, but it avoids a few loads/stores and
421// tries to optimize where it can using SIMD.
422static uint64_t compress_r11eac_block_fast(const uint8_t* src, int rowBytes) {
423 // Store each row of alpha values in an integer
424 const uint32_t alphaRow1 = *(reinterpret_cast<const uint32_t*>(src));
425 const uint32_t alphaRow2 = *(reinterpret_cast<const uint32_t*>(src + rowBytes));
426 const uint32_t alphaRow3 = *(reinterpret_cast<const uint32_t*>(src + 2*rowBytes));
427 const uint32_t alphaRow4 = *(reinterpret_cast<const uint32_t*>(src + 3*rowBytes));
428
429 // Check for solid blocks. The explanations for these values
430 // can be found in the comments of compress_r11eac_block above
431 if (alphaRow1 == alphaRow2 && alphaRow1 == alphaRow3 && alphaRow1 == alphaRow4) {
432 if (0 == alphaRow1) {
433 // Fully transparent block
434 return 0x0020000000002000ULL;
435 } else if (0xFFFFFFFF == alphaRow1) {
436 // Fully opaque block
437 return 0xFFFFFFFFFFFFFFFFULL;
438 }
439 }
440
441 // Convert each integer of alpha values into an integer of indices
442 const uint32_t indexRow1 = convert_indices(alphaRow1);
443 const uint32_t indexRow2 = convert_indices(alphaRow2);
444 const uint32_t indexRow3 = convert_indices(alphaRow3);
445 const uint32_t indexRow4 = convert_indices(alphaRow4);
446
447 // Interleave the indices from the top two rows and bottom two rows
448 // prior to passing them to interleave6. Since each index is at most
449 // three bits, then each byte can hold two indices... The way that the
450 // compression scheme expects the packing allows us to efficiently pack
451 // the top two rows and bottom two rows. Interleaving each 6-bit sequence
452 // and tightly packing it into a uint64_t is a little trickier, which is
453 // taken care of in interleave6.
454 const uint32_t r1r2 = (indexRow1 << 3) | indexRow2;
455 const uint32_t r3r4 = (indexRow3 << 3) | indexRow4;
456 const uint64_t indices = interleave6(r1r2, r3r4);
457
458 // Return the packed incdices in the least significant bits with the magic header
459 return SkEndian_SwapBE64(0x8490000000000000ULL | indices);
460}
461
462static bool compress_a8_to_r11eac_fast(uint8_t* dst, const uint8_t* src,
463 int width, int height, int rowBytes) {
464 // Make sure that our data is well-formed enough to be considered for compression
465 if (0 == width || 0 == height || (width % 4) != 0 || (height % 4) != 0) {
466 return false;
467 }
468
469 const int blocksX = width >> 2;
470 const int blocksY = height >> 2;
471
472 uint64_t* encPtr = reinterpret_cast<uint64_t*>(dst);
473 for (int y = 0; y < blocksY; ++y) {
474 for (int x = 0; x < blocksX; ++x) {
475 // Compress it
476 *encPtr = compress_r11eac_block_fast(src + 4*x, rowBytes);
477 ++encPtr;
478 }
479 src += 4 * rowBytes;
480 }
481 return true;
482}
483#endif // COMPRESS_R11_EAC_FASTEST
484
485////////////////////////////////////////////////////////////////////////////////
486//
487// Utility functions used by the blitter
488//
489////////////////////////////////////////////////////////////////////////////////
490
491// The R11 EAC format expects that indices are given in column-major order. Since
492// we receive alpha values in raster order, this usually means that we have to use
493// pack6 above to properly pack our indices. However, if our indices come from the
494// blitter, then each integer will be a column of indices, and hence can be efficiently
495// packed. This function takes the bottom three bits of each byte and places them in
496// the least significant 12 bits of the resulting integer.
497static inline uint32_t pack_indices_vertical(uint32_t x) {
498#if defined (SK_CPU_BENDIAN)
499 return
500 (x & 7) |
501 ((x >> 5) & (7 << 3)) |
502 ((x >> 10) & (7 << 6)) |
503 ((x >> 15) & (7 << 9));
504#else
505 return
506 ((x >> 24) & 7) |
507 ((x >> 13) & (7 << 3)) |
508 ((x >> 2) & (7 << 6)) |
509 ((x << 9) & (7 << 9));
510#endif
511}
512
513// This function returns the compressed format of a block given as four columns of
514// alpha values. Each column is assumed to be loaded from top to bottom, and hence
515// must first be converted to indices and then packed into the resulting 64-bit
516// integer.
krajcevskid5e46c72014-07-28 14:14:16 -0700517inline void compress_block_vertical(uint8_t* dstPtr, const uint8_t *block) {
518
519 const uint32_t* src = reinterpret_cast<const uint32_t*>(block);
520 uint64_t* dst = reinterpret_cast<uint64_t*>(dstPtr);
521
522 const uint32_t alphaColumn0 = src[0];
523 const uint32_t alphaColumn1 = src[1];
524 const uint32_t alphaColumn2 = src[2];
525 const uint32_t alphaColumn3 = src[3];
krajcevski6c354882014-07-22 07:44:00 -0700526
527 if (alphaColumn0 == alphaColumn1 &&
528 alphaColumn2 == alphaColumn3 &&
529 alphaColumn0 == alphaColumn2) {
530
531 if (0 == alphaColumn0) {
532 // Transparent
krajcevskid5e46c72014-07-28 14:14:16 -0700533 *dst = 0x0020000000002000ULL;
534 return;
krajcevski6c354882014-07-22 07:44:00 -0700535 }
536 else if (0xFFFFFFFF == alphaColumn0) {
537 // Opaque
krajcevskid5e46c72014-07-28 14:14:16 -0700538 *dst = 0xFFFFFFFFFFFFFFFFULL;
539 return;
krajcevski6c354882014-07-22 07:44:00 -0700540 }
541 }
542
543 const uint32_t indexColumn0 = convert_indices(alphaColumn0);
544 const uint32_t indexColumn1 = convert_indices(alphaColumn1);
545 const uint32_t indexColumn2 = convert_indices(alphaColumn2);
546 const uint32_t indexColumn3 = convert_indices(alphaColumn3);
547
548 const uint32_t packedIndexColumn0 = pack_indices_vertical(indexColumn0);
549 const uint32_t packedIndexColumn1 = pack_indices_vertical(indexColumn1);
550 const uint32_t packedIndexColumn2 = pack_indices_vertical(indexColumn2);
551 const uint32_t packedIndexColumn3 = pack_indices_vertical(indexColumn3);
552
krajcevskid5e46c72014-07-28 14:14:16 -0700553 *dst = SkEndian_SwapBE64(0x8490000000000000ULL |
krajcevski6c354882014-07-22 07:44:00 -0700554 (static_cast<uint64_t>(packedIndexColumn0) << 36) |
555 (static_cast<uint64_t>(packedIndexColumn1) << 24) |
556 static_cast<uint64_t>(packedIndexColumn2 << 12) |
557 static_cast<uint64_t>(packedIndexColumn3));
krajcevski6c354882014-07-22 07:44:00 -0700558}
559
krajcevski4ad76e32014-07-31 14:12:50 -0700560static inline int get_r11_eac_index(uint64_t block, int x, int y) {
561 SkASSERT(x >= 0 && x < 4);
562 SkASSERT(y >= 0 && y < 4);
563 const int idx = x*4 + y;
564 return (block >> ((15-idx)*3)) & 0x7;
565}
566
567static void decompress_r11_eac_block(uint8_t* dst, int dstRowBytes, const uint8_t* src) {
568 const uint64_t block = SkEndian_SwapBE64(*(reinterpret_cast<const uint64_t *>(src)));
569
570 const int base_cw = (block >> 56) & 0xFF;
571 const int mod = (block >> 52) & 0xF;
572 const int palette_idx = (block >> 48) & 0xF;
573
574 const int* palette = kR11EACModifierPalettes[palette_idx];
575
576 for (int j = 0; j < 4; ++j) {
577 for (int i = 0; i < 4; ++i) {
578 const int idx = get_r11_eac_index(block, i, j);
579 const int val = base_cw*8 + 4 + palette[idx]*mod*8;
580 if (val < 0) {
581 dst[i] = 0;
582 } else if (val > 2047) {
583 dst[i] = 0xFF;
584 } else {
585 dst[i] = (val >> 3) & 0xFF;
586 }
587 }
588 dst += dstRowBytes;
589 }
590}
591
krajcevski6c354882014-07-22 07:44:00 -0700592////////////////////////////////////////////////////////////////////////////////
593
594namespace SkTextureCompressor {
595
596bool CompressA8ToR11EAC(uint8_t* dst, const uint8_t* src, int width, int height, int rowBytes) {
597
598#if (COMPRESS_R11_EAC_SLOW) || (COMPRESS_R11_EAC_FAST)
599
600 return compress_4x4_a8_to_64bit(dst, src, width, height, rowBytes, compress_r11eac_block);
601
602#elif COMPRESS_R11_EAC_FASTEST
603
604 return compress_a8_to_r11eac_fast(dst, src, width, height, rowBytes);
605
606#else
607#error "Must choose R11 EAC algorithm"
608#endif
609}
610
krajcevski6c354882014-07-22 07:44:00 -0700611SkBlitter* CreateR11EACBlitter(int width, int height, void* outputBuffer) {
krajcevskid5e46c72014-07-28 14:14:16 -0700612 return new
613 SkTCompressedAlphaBlitter<4, 8, compress_block_vertical>
614 (width, height, outputBuffer);
krajcevski6c354882014-07-22 07:44:00 -0700615}
616
krajcevski4ad76e32014-07-31 14:12:50 -0700617void DecompressR11EAC(uint8_t* dst, int dstRowBytes, const uint8_t* src, int width, int height) {
618 for (int j = 0; j < height; j += 4) {
619 for (int i = 0; i < width; i += 4) {
620 decompress_r11_eac_block(dst + i, dstRowBytes, src);
621 src += 8;
622 }
623 dst += 4 * dstRowBytes;
624 }
625}
626
krajcevski6c354882014-07-22 07:44:00 -0700627} // namespace SkTextureCompressor