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 | #ifndef SkTextureCompressor_DEFINED |
| 9 | #define SkTextureCompressor_DEFINED |
| 10 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 11 | #include "SkBitmapProcShader.h" |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 12 | #include "SkImageInfo.h" |
| 13 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 14 | class SkBitmap; |
krajcevski | 40a1e11 | 2014-08-05 14:13:36 -0700 | [diff] [blame] | 15 | class SkBlitter; |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 16 | class SkData; |
| 17 | |
| 18 | namespace SkTextureCompressor { |
| 19 | // Various texture compression formats that we support. |
| 20 | enum Format { |
krajcevski | f3d15dc | 2014-06-30 08:47:33 -0700 | [diff] [blame] | 21 | // Alpha only formats. |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 22 | kLATC_Format, // 4x4 blocks, (de)compresses A8 |
| 23 | kR11_EAC_Format, // 4x4 blocks, (de)compresses A8 |
| 24 | |
| 25 | // RGB only formats |
| 26 | kETC1_Format, // 4x4 blocks, compresses RGB 565, decompresses 8-bit RGB |
| 27 | // NOTE: ETC1 supports 8-bit RGB compression, but we |
| 28 | // currently don't have any RGB8 SkColorTypes. We could |
| 29 | // support 8-bit RGBA but we would have to preprocess the |
| 30 | // bitmap to insert alphas. |
| 31 | |
| 32 | // Multi-purpose formats |
krajcevski | 95b1b3d | 2014-08-07 12:58:38 -0700 | [diff] [blame] | 33 | kASTC_4x4_Format, // 4x4 blocks, no compression, decompresses RGBA |
| 34 | kASTC_5x4_Format, // 5x4 blocks, no compression, decompresses RGBA |
| 35 | kASTC_5x5_Format, // 5x5 blocks, no compression, decompresses RGBA |
| 36 | kASTC_6x5_Format, // 6x5 blocks, no compression, decompresses RGBA |
| 37 | kASTC_6x6_Format, // 6x6 blocks, no compression, decompresses RGBA |
| 38 | kASTC_8x5_Format, // 8x5 blocks, no compression, decompresses RGBA |
| 39 | kASTC_8x6_Format, // 8x6 blocks, no compression, decompresses RGBA |
| 40 | kASTC_8x8_Format, // 8x8 blocks, no compression, decompresses RGBA |
| 41 | kASTC_10x5_Format, // 10x5 blocks, no compression, decompresses RGBA |
| 42 | kASTC_10x6_Format, // 10x6 blocks, no compression, decompresses RGBA |
| 43 | kASTC_10x8_Format, // 10x8 blocks, no compression, decompresses RGBA |
| 44 | kASTC_10x10_Format, // 10x10 blocks, no compression, decompresses RGBA |
| 45 | kASTC_12x10_Format, // 12x10 blocks, no compression, decompresses RGBA |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 46 | kASTC_12x12_Format, // 12x12 blocks, compresses A8, decompresses RGBA |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 47 | |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 48 | kLast_Format = kASTC_12x12_Format |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 49 | }; |
| 50 | static const int kFormatCnt = kLast_Format + 1; |
| 51 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 52 | // Returns the size of the compressed data given the width, height, and |
| 53 | // desired compression format. If the width and height are not an appropriate |
| 54 | // multiple of the block size, then this function returns an error (-1). |
| 55 | int GetCompressedDataSize(Format fmt, int width, int height); |
| 56 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 57 | // Returns an SkData holding a blob of compressed data that corresponds |
| 58 | // to the bitmap. If the bitmap colorType cannot be compressed using the |
| 59 | // associated format, then we return NULL. The caller is responsible for |
| 60 | // calling unref() on the returned data. |
| 61 | SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format); |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 62 | |
| 63 | // Compresses the given src data into dst. The src data is assumed to be |
| 64 | // large enough to hold width*height pixels. The dst data is expected to |
| 65 | // be large enough to hold the compressed data according to the format. |
| 66 | bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType, |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 67 | int width, int height, size_t rowBytes, Format format, |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 68 | bool opt = true /* Use optimization if available */); |
| 69 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 70 | // Decompresses the given src data from the format specified into the |
| 71 | // destination buffer. The width and height of the data passed corresponds |
| 72 | // to the width and height of the uncompressed image. The destination buffer (dst) |
| 73 | // is assumed to be large enough to hold the entire decompressed image. The |
krajcevski | e90c900 | 2014-08-05 07:37:26 -0700 | [diff] [blame] | 74 | // decompressed image colors are determined based on the passed format. |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 75 | // |
| 76 | // Note, CompressBufferToFormat compresses A8 data into ASTC. However, |
| 77 | // general ASTC data encodes RGBA data, so that is what the decompressor |
| 78 | // operates on. |
| 79 | // |
| 80 | // Returns true if successfully decompresses the src data. |
| 81 | bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src, |
| 82 | int width, int height, Format format); |
| 83 | |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 84 | // This typedef defines what the nominal aspects of a compression function |
| 85 | // are. The typedef is not meant to be used by clients of the API, but rather |
| 86 | // allows SIMD optimized compression functions to be implemented. |
| 87 | typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src, |
bsalomon | 9880607 | 2014-12-12 15:11:17 -0800 | [diff] [blame] | 88 | int width, int height, size_t rowBytes); |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 89 | |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 90 | // Returns true if there exists a blitter for the specified format. |
| 91 | inline bool ExistsBlitterForFormat(Format format) { |
| 92 | switch (format) { |
| 93 | case kLATC_Format: |
| 94 | case kR11_EAC_Format: |
| 95 | case kASTC_12x12_Format: |
| 96 | return true; |
| 97 | |
| 98 | default: |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 103 | // Returns the blitter for the given compression format. Note, the blitter |
| 104 | // is intended to be used with the proper input. I.e. if you try to blit |
| 105 | // RGB source data into an R11 EAC texture, you're gonna have a bad time. |
| 106 | SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer, |
krajcevski | b8ccc2f | 2014-08-07 08:15:14 -0700 | [diff] [blame] | 107 | SkTBlitterAllocator *allocator, Format format); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 108 | |
| 109 | // Returns the desired dimensions of the block size for the given format. These dimensions |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame] | 110 | // don't necessarily correspond to the specification's dimensions, since there may |
| 111 | // be specialized algorithms that operate on multiple blocks at once. If the |
| 112 | // flag 'matchSpec' is true, then the actual dimensions from the specification are |
| 113 | // returned. If the flag is false, then these dimensions reflect the appropriate operable |
| 114 | // dimensions of the compression functions. |
| 115 | void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec = false); |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #endif |