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 | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 11 | #include "SkImageInfo.h" |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 12 | #include "SkBlitter.h" |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 13 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 14 | class SkBitmap; |
| 15 | class SkData; |
| 16 | |
| 17 | namespace SkTextureCompressor { |
| 18 | // Various texture compression formats that we support. |
| 19 | enum Format { |
krajcevski | f3d15dc | 2014-06-30 08:47:33 -0700 | [diff] [blame] | 20 | // Alpha only formats. |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 21 | kLATC_Format, // 4x4 blocks, compresses A8 |
| 22 | kR11_EAC_Format, // 4x4 blocks, compresses A8 |
| 23 | kASTC_12x12_Format, // 12x12 blocks, compresses A8 |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 24 | |
krajcevski | b2ef181 | 2014-07-25 07:33:01 -0700 | [diff] [blame] | 25 | kLast_Format = kASTC_12x12_Format |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 26 | }; |
| 27 | static const int kFormatCnt = kLast_Format + 1; |
| 28 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 29 | // Returns the size of the compressed data given the width, height, and |
| 30 | // desired compression format. If the width and height are not an appropriate |
| 31 | // multiple of the block size, then this function returns an error (-1). |
| 32 | int GetCompressedDataSize(Format fmt, int width, int height); |
| 33 | |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 34 | // Returns an SkData holding a blob of compressed data that corresponds |
| 35 | // to the bitmap. If the bitmap colorType cannot be compressed using the |
| 36 | // associated format, then we return NULL. The caller is responsible for |
| 37 | // calling unref() on the returned data. |
| 38 | SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format); |
krajcevski | eecc35f | 2014-06-20 11:43:00 -0700 | [diff] [blame] | 39 | |
| 40 | // Compresses the given src data into dst. The src data is assumed to be |
| 41 | // large enough to hold width*height pixels. The dst data is expected to |
| 42 | // be large enough to hold the compressed data according to the format. |
| 43 | bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType, |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 44 | int width, int height, int rowBytes, Format format, |
| 45 | bool opt = true /* Use optimization if available */); |
| 46 | |
krajcevski | 4ad76e3 | 2014-07-31 14:12:50 -0700 | [diff] [blame^] | 47 | // Decompresses the given src data from the format specified into the |
| 48 | // destination buffer. The width and height of the data passed corresponds |
| 49 | // to the width and height of the uncompressed image. The destination buffer (dst) |
| 50 | // is assumed to be large enough to hold the entire decompressed image. The |
| 51 | // decompressed image colors are determined based on the passed format: |
| 52 | // |
| 53 | // LATC -> Alpha 8 |
| 54 | // R11_EAC -> Alpha 8 |
| 55 | // ASTC -> RGBA |
| 56 | // |
| 57 | // Note, CompressBufferToFormat compresses A8 data into ASTC. However, |
| 58 | // general ASTC data encodes RGBA data, so that is what the decompressor |
| 59 | // operates on. |
| 60 | // |
| 61 | // Returns true if successfully decompresses the src data. |
| 62 | bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src, |
| 63 | int width, int height, Format format); |
| 64 | |
krajcevski | 630598c | 2014-07-14 12:00:04 -0700 | [diff] [blame] | 65 | // This typedef defines what the nominal aspects of a compression function |
| 66 | // are. The typedef is not meant to be used by clients of the API, but rather |
| 67 | // allows SIMD optimized compression functions to be implemented. |
| 68 | typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src, |
| 69 | int width, int height, int rowBytes); |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 70 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 71 | // Returns the blitter for the given compression format. Note, the blitter |
| 72 | // is intended to be used with the proper input. I.e. if you try to blit |
| 73 | // RGB source data into an R11 EAC texture, you're gonna have a bad time. |
| 74 | SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer, |
| 75 | Format format); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame] | 76 | |
| 77 | // 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^] | 78 | // don't necessarily correspond to the specification's dimensions, since there may |
| 79 | // be specialized algorithms that operate on multiple blocks at once. If the |
| 80 | // flag 'matchSpec' is true, then the actual dimensions from the specification are |
| 81 | // returned. If the flag is false, then these dimensions reflect the appropriate operable |
| 82 | // dimensions of the compression functions. |
| 83 | void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec = false); |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | #endif |