krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 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" |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 9 | #include "SkTextureCompressor_ASTC.h" |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 10 | #include "SkTextureCompressor_LATC.h" |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 11 | #include "SkTextureCompressor_R11EAC.h" |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 12 | |
| 13 | #include "SkBitmap.h" |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 14 | #include "SkBitmapProcShader.h" |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 15 | #include "SkData.h" |
| 16 | #include "SkEndian.h" |
| 17 | |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 18 | #include "SkTextureCompression_opts.h" |
| 19 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 20 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 21 | # include "etc1.h" |
| 22 | #endif |
| 23 | |
| 24 | // Convert ETC1 functions to our function signatures |
| 25 | static bool compress_etc1_565(uint8_t* dst, const uint8_t* src, |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 26 | int width, int height, size_t rowBytes) { |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 27 | #ifndef SK_IGNORE_ETC1_SUPPORT |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 28 | return 0 == etc1_encode_image(src, width, height, 2, SkToInt(rowBytes), dst); |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 29 | #else |
| 30 | return false; |
| 31 | #endif |
| 32 | } |
| 33 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 34 | //////////////////////////////////////////////////////////////////////////////// |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 35 | |
| 36 | namespace SkTextureCompressor { |
| 37 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 38 | void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec) { |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 39 | if (NULL == dimX || NULL == dimY) { |
| 40 | return; |
| 41 | } |
| 42 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 43 | if (!matchSpec && SkTextureCompressorGetPlatformDims(format, dimX, dimY)) { |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 47 | // No specialized arguments, return the dimensions as they are in the spec. |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 48 | static const struct FormatDimensions { |
| 49 | const int fBlockSizeX; |
| 50 | const int fBlockSizeY; |
| 51 | } kFormatDimensions[kFormatCnt] = { |
| 52 | { 4, 4 }, // kLATC_Format |
| 53 | { 4, 4 }, // kR11_EAC_Format |
| 54 | { 4, 4 }, // kETC1_Format |
| 55 | { 4, 4 }, // kASTC_4x4_Format |
| 56 | { 5, 4 }, // kASTC_5x4_Format |
| 57 | { 5, 5 }, // kASTC_5x5_Format |
| 58 | { 6, 5 }, // kASTC_6x5_Format |
| 59 | { 6, 6 }, // kASTC_6x6_Format |
| 60 | { 8, 5 }, // kASTC_8x5_Format |
| 61 | { 8, 6 }, // kASTC_8x6_Format |
| 62 | { 8, 8 }, // kASTC_8x8_Format |
| 63 | { 10, 5 }, // kASTC_10x5_Format |
| 64 | { 10, 6 }, // kASTC_10x6_Format |
| 65 | { 10, 8 }, // kASTC_10x8_Format |
| 66 | { 10, 10 }, // kASTC_10x10_Format |
| 67 | { 12, 10 }, // kASTC_12x10_Format |
| 68 | { 12, 12 }, // kASTC_12x12_Format |
| 69 | }; |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 70 | |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 71 | *dimX = kFormatDimensions[format].fBlockSizeX; |
| 72 | *dimY = kFormatDimensions[format].fBlockSizeY; |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 73 | } |
| 74 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 75 | int GetCompressedDataSize(Format fmt, int width, int height) { |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 76 | int dimX, dimY; |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 77 | GetBlockDimensions(fmt, &dimX, &dimY, true); |
| 78 | |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 79 | int encodedBlockSize = 0; |
| 80 | |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 81 | switch (fmt) { |
krajcevski | 1459be5 | 2014-07-09 09:15:45 -0700 | [diff] [blame] | 82 | // These formats are 64 bits per 4x4 block. |
krajcevski | 5049557 | 2014-08-04 09:47:31 -0700 | [diff] [blame] | 83 | case kLATC_Format: |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 84 | case kR11_EAC_Format: |
| 85 | case kETC1_Format: |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 86 | encodedBlockSize = 8; |
| 87 | break; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 88 | |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 89 | // This format is 128 bits. |
| 90 | case kASTC_4x4_Format: |
| 91 | case kASTC_5x4_Format: |
| 92 | case kASTC_5x5_Format: |
| 93 | case kASTC_6x5_Format: |
| 94 | case kASTC_6x6_Format: |
| 95 | case kASTC_8x5_Format: |
| 96 | case kASTC_8x6_Format: |
| 97 | case kASTC_8x8_Format: |
| 98 | case kASTC_10x5_Format: |
| 99 | case kASTC_10x6_Format: |
| 100 | case kASTC_10x8_Format: |
| 101 | case kASTC_10x10_Format: |
| 102 | case kASTC_12x10_Format: |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 103 | case kASTC_12x12_Format: |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 104 | encodedBlockSize = 16; |
| 105 | break; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 106 | |
| 107 | default: |
| 108 | SkFAIL("Unknown compressed format!"); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 109 | return -1; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 110 | } |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 111 | |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 112 | if(((width % dimX) == 0) && ((height % dimY) == 0)) { |
| 113 | const int blocksX = width / dimX; |
| 114 | const int blocksY = height / dimY; |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 115 | |
| 116 | return blocksX * blocksY * encodedBlockSize; |
| 117 | } |
| 118 | |
| 119 | return -1; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 120 | } |
| 121 | |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 122 | bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType, |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 123 | int width, int height, size_t rowBytes, Format format, bool opt) { |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 124 | CompressionProc proc = NULL; |
| 125 | if (opt) { |
| 126 | proc = SkTextureCompressorGetPlatformProc(srcColorType, format); |
| 127 | } |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 128 | |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 129 | if (NULL == proc) { |
| 130 | switch (srcColorType) { |
| 131 | case kAlpha_8_SkColorType: |
| 132 | { |
| 133 | switch (format) { |
| 134 | case kLATC_Format: |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 135 | proc = CompressA8ToLATC; |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 136 | break; |
| 137 | case kR11_EAC_Format: |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 138 | proc = CompressA8ToR11EAC; |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 139 | break; |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 140 | case kASTC_12x12_Format: |
| 141 | proc = CompressA8To12x12ASTC; |
| 142 | break; |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 143 | default: |
| 144 | // Do nothing... |
| 145 | break; |
| 146 | } |
| 147 | } |
| 148 | break; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 149 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 150 | case kRGB_565_SkColorType: |
| 151 | { |
| 152 | switch (format) { |
| 153 | case kETC1_Format: |
| 154 | proc = compress_etc1_565; |
| 155 | break; |
| 156 | default: |
| 157 | // Do nothing... |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | break; |
| 162 | |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 163 | default: |
| 164 | // Do nothing... |
| 165 | break; |
| 166 | } |
| 167 | } |
krajcevski | 1459be5 | 2014-07-09 09:15:45 -0700 | [diff] [blame] | 168 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 169 | if (proc) { |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 170 | return proc(dst, src, width, height, rowBytes); |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 175 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 176 | SkData* CompressBitmapToFormat(const SkPixmap& pixmap, Format format) { |
| 177 | int compressedDataSize = GetCompressedDataSize(format, pixmap.width(), pixmap.height()); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 178 | if (compressedDataSize < 0) { |
| 179 | return NULL; |
| 180 | } |
| 181 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 182 | const uint8_t* src = reinterpret_cast<const uint8_t*>(pixmap.addr()); |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 183 | SkData* dst = SkData::NewUninitialized(compressedDataSize); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 184 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 185 | if (!CompressBufferToFormat((uint8_t*)dst->writable_data(), src, pixmap.colorType(), |
| 186 | pixmap.width(), pixmap.height(), pixmap.rowBytes(), format)) { |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 187 | dst->unref(); |
| 188 | dst = NULL; |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 189 | } |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 190 | return dst; |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 191 | } |
| 192 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 193 | SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer, |
| 194 | SkTBlitterAllocator *allocator, Format format) { |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 195 | switch(format) { |
| 196 | case kLATC_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 197 | return CreateLATCBlitter(width, height, compressedBuffer, allocator); |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 198 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 199 | case kR11_EAC_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 200 | return CreateR11EACBlitter(width, height, compressedBuffer, allocator); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 201 | |
krajcevski | 10a350c | 2014-07-29 07:24:58 -0700 | [diff] [blame] | 202 | case kASTC_12x12_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 203 | return CreateASTCBlitter(width, height, compressedBuffer, allocator); |
krajcevski | 10a350c | 2014-07-29 07:24:58 -0700 | [diff] [blame] | 204 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 205 | default: |
| 206 | return NULL; |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 207 | } |
| 208 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 209 | return NULL; |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 210 | } |
| 211 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 212 | bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src, |
| 213 | int width, int height, Format format) { |
| 214 | int dimX, dimY; |
| 215 | GetBlockDimensions(format, &dimX, &dimY, true); |
| 216 | |
| 217 | if (width < 0 || ((width % dimX) != 0) || height < 0 || ((height % dimY) != 0)) { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | switch(format) { |
| 222 | case kLATC_Format: |
| 223 | DecompressLATC(dst, dstRowBytes, src, width, height); |
| 224 | return true; |
| 225 | |
| 226 | case kR11_EAC_Format: |
| 227 | DecompressR11EAC(dst, dstRowBytes, src, width, height); |
| 228 | return true; |
| 229 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 230 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 231 | case kETC1_Format: |
| 232 | return 0 == etc1_decode_image(src, dst, width, height, 3, dstRowBytes); |
| 233 | #endif |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 234 | |
| 235 | case kASTC_4x4_Format: |
| 236 | case kASTC_5x4_Format: |
| 237 | case kASTC_5x5_Format: |
| 238 | case kASTC_6x5_Format: |
| 239 | case kASTC_6x6_Format: |
| 240 | case kASTC_8x5_Format: |
| 241 | case kASTC_8x6_Format: |
| 242 | case kASTC_8x8_Format: |
| 243 | case kASTC_10x5_Format: |
| 244 | case kASTC_10x6_Format: |
| 245 | case kASTC_10x8_Format: |
| 246 | case kASTC_10x10_Format: |
| 247 | case kASTC_12x10_Format: |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 248 | case kASTC_12x12_Format: |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 249 | DecompressASTC(dst, dstRowBytes, src, width, height, dimX, dimY); |
| 250 | return true; |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 251 | |
| 252 | default: |
| 253 | // Do nothing... |
| 254 | break; |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 260 | } // namespace SkTextureCompressor |