blob: e44e7b353dcd13c3919b1de3f89019df8d0aaf05 [file] [log] [blame]
krajcevskiae614402014-06-10 14:52:28 -07001/*
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
krajcevskieecc35f2014-06-20 11:43:00 -070011#include "SkImageInfo.h"
12
krajcevskiae614402014-06-10 14:52:28 -070013class SkBitmap;
krajcevski40a1e112014-08-05 14:13:36 -070014class SkBlitter;
krajcevskiae614402014-06-10 14:52:28 -070015class SkData;
16
17namespace SkTextureCompressor {
18 // Various texture compression formats that we support.
19 enum Format {
krajcevskif3d15dc2014-06-30 08:47:33 -070020 // Alpha only formats.
krajcevskie90c9002014-08-05 07:37:26 -070021 kLATC_Format, // 4x4 blocks, (de)compresses A8
22 kR11_EAC_Format, // 4x4 blocks, (de)compresses A8
23
24 // RGB only formats
25 kETC1_Format, // 4x4 blocks, compresses RGB 565, decompresses 8-bit RGB
26 // NOTE: ETC1 supports 8-bit RGB compression, but we
27 // currently don't have any RGB8 SkColorTypes. We could
28 // support 8-bit RGBA but we would have to preprocess the
29 // bitmap to insert alphas.
30
31 // Multi-purpose formats
32 kASTC_12x12_Format, // 12x12 blocks, compresses A8, decompresses RGBA
krajcevskiae614402014-06-10 14:52:28 -070033
krajcevskib2ef1812014-07-25 07:33:01 -070034 kLast_Format = kASTC_12x12_Format
krajcevskiae614402014-06-10 14:52:28 -070035 };
36 static const int kFormatCnt = kLast_Format + 1;
37
krajcevski6c354882014-07-22 07:44:00 -070038 // Returns the size of the compressed data given the width, height, and
39 // desired compression format. If the width and height are not an appropriate
40 // multiple of the block size, then this function returns an error (-1).
41 int GetCompressedDataSize(Format fmt, int width, int height);
42
krajcevskiae614402014-06-10 14:52:28 -070043 // Returns an SkData holding a blob of compressed data that corresponds
44 // to the bitmap. If the bitmap colorType cannot be compressed using the
45 // associated format, then we return NULL. The caller is responsible for
46 // calling unref() on the returned data.
47 SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format);
krajcevskieecc35f2014-06-20 11:43:00 -070048
49 // Compresses the given src data into dst. The src data is assumed to be
50 // large enough to hold width*height pixels. The dst data is expected to
51 // be large enough to hold the compressed data according to the format.
52 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType,
krajcevski630598c2014-07-14 12:00:04 -070053 int width, int height, int rowBytes, Format format,
54 bool opt = true /* Use optimization if available */);
55
krajcevski4ad76e32014-07-31 14:12:50 -070056 // Decompresses the given src data from the format specified into the
57 // destination buffer. The width and height of the data passed corresponds
58 // to the width and height of the uncompressed image. The destination buffer (dst)
59 // is assumed to be large enough to hold the entire decompressed image. The
krajcevskie90c9002014-08-05 07:37:26 -070060 // decompressed image colors are determined based on the passed format.
krajcevski4ad76e32014-07-31 14:12:50 -070061 //
62 // Note, CompressBufferToFormat compresses A8 data into ASTC. However,
63 // general ASTC data encodes RGBA data, so that is what the decompressor
64 // operates on.
65 //
66 // Returns true if successfully decompresses the src data.
67 bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src,
68 int width, int height, Format format);
69
krajcevski630598c2014-07-14 12:00:04 -070070 // This typedef defines what the nominal aspects of a compression function
71 // are. The typedef is not meant to be used by clients of the API, but rather
72 // allows SIMD optimized compression functions to be implemented.
73 typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src,
74 int width, int height, int rowBytes);
krajcevskiad1df152014-07-21 11:44:37 -070075
krajcevski6c354882014-07-22 07:44:00 -070076 // Returns the blitter for the given compression format. Note, the blitter
77 // is intended to be used with the proper input. I.e. if you try to blit
78 // RGB source data into an R11 EAC texture, you're gonna have a bad time.
79 SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer,
80 Format format);
krajcevski25a67bc2014-07-29 11:44:26 -070081
82 // Returns the desired dimensions of the block size for the given format. These dimensions
krajcevski4ad76e32014-07-31 14:12:50 -070083 // don't necessarily correspond to the specification's dimensions, since there may
84 // be specialized algorithms that operate on multiple blocks at once. If the
85 // flag 'matchSpec' is true, then the actual dimensions from the specification are
86 // returned. If the flag is false, then these dimensions reflect the appropriate operable
87 // dimensions of the compression functions.
88 void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec = false);
krajcevskiae614402014-06-10 14:52:28 -070089}
90
91#endif