blob: d4d48967a3dd66c235158d63d8acb5881d5c1310 [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
krajcevskib8ccc2f2014-08-07 08:15:14 -070011#include "SkBitmapProcShader.h"
krajcevskieecc35f2014-06-20 11:43:00 -070012#include "SkImageInfo.h"
13
krajcevskiae614402014-06-10 14:52:28 -070014class SkBitmap;
krajcevski40a1e112014-08-05 14:13:36 -070015class SkBlitter;
krajcevskiae614402014-06-10 14:52:28 -070016class SkData;
17
18namespace SkTextureCompressor {
19 // Various texture compression formats that we support.
20 enum Format {
krajcevskif3d15dc2014-06-30 08:47:33 -070021 // Alpha only formats.
krajcevskie90c9002014-08-05 07:37:26 -070022 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
krajcevski95b1b3d2014-08-07 12:58:38 -070033 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
krajcevskie90c9002014-08-05 07:37:26 -070046 kASTC_12x12_Format, // 12x12 blocks, compresses A8, decompresses RGBA
krajcevskiae614402014-06-10 14:52:28 -070047
krajcevskib2ef1812014-07-25 07:33:01 -070048 kLast_Format = kASTC_12x12_Format
krajcevskiae614402014-06-10 14:52:28 -070049 };
50 static const int kFormatCnt = kLast_Format + 1;
51
krajcevski6c354882014-07-22 07:44:00 -070052 // 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
krajcevskiae614402014-06-10 14:52:28 -070057 // Returns an SkData holding a blob of compressed data that corresponds
mtkleinb6394742015-08-06 08:17:16 -070058 // to the pixmap. If the pixmap colorType cannot be compressed using the
halcanary96fcdcc2015-08-27 07:41:13 -070059 // associated format, then we return nullptr. The caller is responsible for
krajcevskiae614402014-06-10 14:52:28 -070060 // calling unref() on the returned data.
reed41e010c2015-06-09 12:16:53 -070061 SkData* CompressBitmapToFormat(const SkPixmap&, Format format);
krajcevskieecc35f2014-06-20 11:43:00 -070062
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,
mtkleinb6394742015-08-06 08:17:16 -070067 int width, int height, size_t rowBytes, Format format);
krajcevski630598c2014-07-14 12:00:04 -070068
krajcevski4ad76e32014-07-31 14:12:50 -070069 // Decompresses the given src data from the format specified into the
70 // destination buffer. The width and height of the data passed corresponds
71 // to the width and height of the uncompressed image. The destination buffer (dst)
72 // is assumed to be large enough to hold the entire decompressed image. The
krajcevskie90c9002014-08-05 07:37:26 -070073 // decompressed image colors are determined based on the passed format.
krajcevski4ad76e32014-07-31 14:12:50 -070074 //
75 // Note, CompressBufferToFormat compresses A8 data into ASTC. However,
76 // general ASTC data encodes RGBA data, so that is what the decompressor
77 // operates on.
78 //
79 // Returns true if successfully decompresses the src data.
80 bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src,
81 int width, int height, Format format);
82
krajcevskib8ccc2f2014-08-07 08:15:14 -070083 // Returns true if there exists a blitter for the specified format.
84 inline bool ExistsBlitterForFormat(Format format) {
85 switch (format) {
86 case kLATC_Format:
87 case kR11_EAC_Format:
88 case kASTC_12x12_Format:
89 return true;
90
91 default:
92 return false;
93 }
94 }
95
krajcevski6c354882014-07-22 07:44:00 -070096 // Returns the blitter for the given compression format. Note, the blitter
97 // is intended to be used with the proper input. I.e. if you try to blit
98 // RGB source data into an R11 EAC texture, you're gonna have a bad time.
99 SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer,
krajcevskib8ccc2f2014-08-07 08:15:14 -0700100 SkTBlitterAllocator *allocator, Format format);
krajcevski25a67bc2014-07-29 11:44:26 -0700101
102 // Returns the desired dimensions of the block size for the given format. These dimensions
krajcevski4ad76e32014-07-31 14:12:50 -0700103 // don't necessarily correspond to the specification's dimensions, since there may
104 // be specialized algorithms that operate on multiple blocks at once. If the
105 // flag 'matchSpec' is true, then the actual dimensions from the specification are
106 // returned. If the flag is false, then these dimensions reflect the appropriate operable
107 // dimensions of the compression functions.
108 void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec = false);
krajcevskiae614402014-06-10 14:52:28 -0700109}
110
111#endif