blob: d82bf07a1e797a0e698472c4b60dbd9827d5eb0f [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
33 kASTC_12x12_Format, // 12x12 blocks, compresses A8, decompresses RGBA
krajcevskiae614402014-06-10 14:52:28 -070034
krajcevskib2ef1812014-07-25 07:33:01 -070035 kLast_Format = kASTC_12x12_Format
krajcevskiae614402014-06-10 14:52:28 -070036 };
37 static const int kFormatCnt = kLast_Format + 1;
38
krajcevski6c354882014-07-22 07:44:00 -070039 // Returns the size of the compressed data given the width, height, and
40 // desired compression format. If the width and height are not an appropriate
41 // multiple of the block size, then this function returns an error (-1).
42 int GetCompressedDataSize(Format fmt, int width, int height);
43
krajcevskiae614402014-06-10 14:52:28 -070044 // Returns an SkData holding a blob of compressed data that corresponds
45 // to the bitmap. If the bitmap colorType cannot be compressed using the
46 // associated format, then we return NULL. The caller is responsible for
47 // calling unref() on the returned data.
48 SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format);
krajcevskieecc35f2014-06-20 11:43:00 -070049
50 // Compresses the given src data into dst. The src data is assumed to be
51 // large enough to hold width*height pixels. The dst data is expected to
52 // be large enough to hold the compressed data according to the format.
53 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType,
krajcevski630598c2014-07-14 12:00:04 -070054 int width, int height, int rowBytes, Format format,
55 bool opt = true /* Use optimization if available */);
56
krajcevski4ad76e32014-07-31 14:12:50 -070057 // Decompresses the given src data from the format specified into the
58 // destination buffer. The width and height of the data passed corresponds
59 // to the width and height of the uncompressed image. The destination buffer (dst)
60 // is assumed to be large enough to hold the entire decompressed image. The
krajcevskie90c9002014-08-05 07:37:26 -070061 // decompressed image colors are determined based on the passed format.
krajcevski4ad76e32014-07-31 14:12:50 -070062 //
63 // Note, CompressBufferToFormat compresses A8 data into ASTC. However,
64 // general ASTC data encodes RGBA data, so that is what the decompressor
65 // operates on.
66 //
67 // Returns true if successfully decompresses the src data.
68 bool DecompressBufferFromFormat(uint8_t* dst, int dstRowBytes, const uint8_t* src,
69 int width, int height, Format format);
70
krajcevski630598c2014-07-14 12:00:04 -070071 // This typedef defines what the nominal aspects of a compression function
72 // are. The typedef is not meant to be used by clients of the API, but rather
73 // allows SIMD optimized compression functions to be implemented.
74 typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src,
75 int width, int height, int rowBytes);
krajcevskiad1df152014-07-21 11:44:37 -070076
krajcevskib8ccc2f2014-08-07 08:15:14 -070077 // Returns true if there exists a blitter for the specified format.
78 inline bool ExistsBlitterForFormat(Format format) {
79 switch (format) {
80 case kLATC_Format:
81 case kR11_EAC_Format:
82 case kASTC_12x12_Format:
83 return true;
84
85 default:
86 return false;
87 }
88 }
89
krajcevski6c354882014-07-22 07:44:00 -070090 // Returns the blitter for the given compression format. Note, the blitter
91 // is intended to be used with the proper input. I.e. if you try to blit
92 // RGB source data into an R11 EAC texture, you're gonna have a bad time.
93 SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuffer,
krajcevskib8ccc2f2014-08-07 08:15:14 -070094 SkTBlitterAllocator *allocator, Format format);
krajcevski25a67bc2014-07-29 11:44:26 -070095
96 // Returns the desired dimensions of the block size for the given format. These dimensions
krajcevski4ad76e32014-07-31 14:12:50 -070097 // don't necessarily correspond to the specification's dimensions, since there may
98 // be specialized algorithms that operate on multiple blocks at once. If the
99 // flag 'matchSpec' is true, then the actual dimensions from the specification are
100 // returned. If the flag is false, then these dimensions reflect the appropriate operable
101 // dimensions of the compression functions.
102 void GetBlockDimensions(Format format, int* dimX, int* dimY, bool matchSpec = false);
krajcevskiae614402014-06-10 14:52:28 -0700103}
104
105#endif