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