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" |
halcanary | 4dbbd04 | 2016-06-07 17:21:10 -0700 | [diff] [blame] | 17 | #include "SkMathPriv.h" |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 18 | #include "SkOpts.h" |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 39 | if (nullptr == dimX || nullptr == dimY) { |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 43 | if (!matchSpec && SkOpts::fill_block_dimensions(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; |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 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, |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 123 | int width, int height, size_t rowBytes, Format format) { |
| 124 | SkOpts::TextureCompressor proc = SkOpts::texture_compressor(srcColorType, format); |
| 125 | if (proc && proc(dst, src, width, height, rowBytes)) { |
| 126 | return true; |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 127 | } |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 128 | |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 129 | switch (srcColorType) { |
| 130 | case kAlpha_8_SkColorType: |
| 131 | if (format == kLATC_Format) { proc = CompressA8ToLATC; } |
| 132 | if (format == kR11_EAC_Format) { proc = CompressA8ToR11EAC; } |
| 133 | if (format == kASTC_12x12_Format) { proc = CompressA8To12x12ASTC; } |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 134 | break; |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 135 | case kRGB_565_SkColorType: |
| 136 | if (format == kETC1_Format) { proc = compress_etc1_565; } |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 137 | break; |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 138 | default: |
| 139 | break; |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 140 | } |
mtklein | b639474 | 2015-08-06 08:17:16 -0700 | [diff] [blame] | 141 | if (proc && proc(dst, src, width, height, rowBytes)) { |
| 142 | return true; |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return false; |
| 146 | } |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 147 | |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 148 | sk_sp<SkData> CompressBitmapToFormat(const SkPixmap& pixmap, Format format) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 149 | int compressedDataSize = GetCompressedDataSize(format, pixmap.width(), pixmap.height()); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 150 | if (compressedDataSize < 0) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 151 | return nullptr; |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 152 | } |
| 153 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 154 | const uint8_t* src = reinterpret_cast<const uint8_t*>(pixmap.addr()); |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 155 | sk_sp<SkData> dst(SkData::MakeUninitialized(compressedDataSize)); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 156 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 157 | if (!CompressBufferToFormat((uint8_t*)dst->writable_data(), src, pixmap.colorType(), |
| 158 | pixmap.width(), pixmap.height(), pixmap.rowBytes(), format)) { |
bungeman | 38d909e | 2016-08-02 14:40:46 -0700 | [diff] [blame] | 159 | return nullptr; |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 160 | } |
reed | 33a3050 | 2014-09-11 08:42:36 -0700 | [diff] [blame] | 161 | return dst; |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 162 | } |
| 163 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 164 | SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer, |
| 165 | SkTBlitterAllocator *allocator, Format format) { |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 166 | switch(format) { |
| 167 | case kLATC_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 168 | return CreateLATCBlitter(width, height, compressedBuffer, allocator); |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 169 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 170 | case kR11_EAC_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 171 | return CreateR11EACBlitter(width, height, compressedBuffer, allocator); |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 172 | |
krajcevski | 10a350c | 2014-07-29 07:24:58 -0700 | [diff] [blame] | 173 | case kASTC_12x12_Format: |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 174 | return CreateASTCBlitter(width, height, compressedBuffer, allocator); |
krajcevski | 10a350c | 2014-07-29 07:24:58 -0700 | [diff] [blame] | 175 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 176 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 177 | return nullptr; |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 178 | } |
| 179 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 180 | return nullptr; |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 181 | } |
| 182 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 183 | bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src, |
| 184 | int width, int height, Format format) { |
| 185 | int dimX, dimY; |
| 186 | GetBlockDimensions(format, &dimX, &dimY, true); |
| 187 | |
| 188 | if (width < 0 || ((width % dimX) != 0) || height < 0 || ((height % dimY) != 0)) { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | switch(format) { |
| 193 | case kLATC_Format: |
| 194 | DecompressLATC(dst, dstRowBytes, src, width, height); |
| 195 | return true; |
| 196 | |
| 197 | case kR11_EAC_Format: |
| 198 | DecompressR11EAC(dst, dstRowBytes, src, width, height); |
| 199 | return true; |
| 200 | |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 201 | #ifndef SK_IGNORE_ETC1_SUPPORT |
| 202 | case kETC1_Format: |
| 203 | return 0 == etc1_decode_image(src, dst, width, height, 3, dstRowBytes); |
| 204 | #endif |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 205 | |
| 206 | case kASTC_4x4_Format: |
| 207 | case kASTC_5x4_Format: |
| 208 | case kASTC_5x5_Format: |
| 209 | case kASTC_6x5_Format: |
| 210 | case kASTC_6x6_Format: |
| 211 | case kASTC_8x5_Format: |
| 212 | case kASTC_8x6_Format: |
| 213 | case kASTC_8x8_Format: |
| 214 | case kASTC_10x5_Format: |
| 215 | case kASTC_10x6_Format: |
| 216 | case kASTC_10x8_Format: |
| 217 | case kASTC_10x10_Format: |
| 218 | case kASTC_12x10_Format: |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 219 | case kASTC_12x12_Format: |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 220 | DecompressASTC(dst, dstRowBytes, src, width, height, dimX, dimY); |
| 221 | return true; |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 222 | |
| 223 | default: |
| 224 | // Do nothing... |
| 225 | break; |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 231 | } // namespace SkTextureCompressor |