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 | |
| 47 | // This typedef defines what the nominal aspects of a compression function |
| 48 | // are. The typedef is not meant to be used by clients of the API, but rather |
| 49 | // allows SIMD optimized compression functions to be implemented. |
| 50 | typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src, |
| 51 | int width, int height, int rowBytes); |
krajcevski | ad1df15 | 2014-07-21 11:44:37 -0700 | [diff] [blame] | 52 | |
krajcevski | 6c35488 | 2014-07-22 07:44:00 -0700 | [diff] [blame] | 53 | // Returns the blitter for the given compression format. Note, the blitter |
| 54 | // is intended to be used with the proper input. I.e. if you try to blit |
| 55 | // RGB source data into an R11 EAC texture, you're gonna have a bad time. |
| 56 | SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer, |
| 57 | Format format); |
krajcevski | 25a67bc | 2014-07-29 11:44:26 -0700 | [diff] [blame^] | 58 | |
| 59 | // Returns the desired dimensions of the block size for the given format. These dimensions |
| 60 | // don't necessarily correspond to the hardware-specified dimensions, since there may |
| 61 | // be specialized algorithms that operate on multiple blocks at once. These dimensions |
| 62 | // reflect that optimization and return the appropriate operable dimensions. |
| 63 | void GetBlockDimensions(Format format, int* dimX, int* dimY); |
krajcevski | ae61440 | 2014-06-10 14:52:28 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #endif |