OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTextureCompressor_DEFINED | 8 #ifndef SkTextureCompressor_DEFINED |
9 #define SkTextureCompressor_DEFINED | 9 #define SkTextureCompressor_DEFINED |
10 | 10 |
11 #include "SkImageInfo.h" | 11 #include "SkImageInfo.h" |
12 #include "SkBlitter.h" | 12 #include "SkBlitter.h" |
13 | 13 |
14 class SkBitmap; | 14 class SkBitmap; |
15 class SkData; | 15 class SkData; |
16 | 16 |
17 namespace SkTextureCompressor { | 17 namespace SkTextureCompressor { |
18 // Various texture compression formats that we support. | 18 // Various texture compression formats that we support. |
19 enum Format { | 19 enum Format { |
20 // Alpha only formats. | 20 // Alpha only formats. |
21 kLATC_Format, | 21 kLATC_Format, // 4x4 blocks, compresses A8 |
22 kR11_EAC_Format, | 22 kR11_EAC_Format, // 4x4 blocks, compresses A8 |
23 | 23 |
24 kLast_Format = kR11_EAC_Format | 24 kLast_Format = kR11_EAC_Format |
25 }; | 25 }; |
26 static const int kFormatCnt = kLast_Format + 1; | 26 static const int kFormatCnt = kLast_Format + 1; |
27 | 27 |
| 28 // Returns the size of the compressed data given the width, height, and |
| 29 // desired compression format. If the width and height are not an appropriat
e |
| 30 // multiple of the block size, then this function returns an error (-1). |
| 31 int GetCompressedDataSize(Format fmt, int width, int height); |
| 32 |
28 // Returns an SkData holding a blob of compressed data that corresponds | 33 // Returns an SkData holding a blob of compressed data that corresponds |
29 // to the bitmap. If the bitmap colorType cannot be compressed using the | 34 // to the bitmap. If the bitmap colorType cannot be compressed using the |
30 // associated format, then we return NULL. The caller is responsible for | 35 // associated format, then we return NULL. The caller is responsible for |
31 // calling unref() on the returned data. | 36 // calling unref() on the returned data. |
32 SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format); | 37 SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format); |
33 | 38 |
34 // Compresses the given src data into dst. The src data is assumed to be | 39 // Compresses the given src data into dst. The src data is assumed to be |
35 // large enough to hold width*height pixels. The dst data is expected to | 40 // large enough to hold width*height pixels. The dst data is expected to |
36 // be large enough to hold the compressed data according to the format. | 41 // be large enough to hold the compressed data according to the format. |
37 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType sr
cColorType, | 42 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType sr
cColorType, |
38 int width, int height, int rowBytes, Format form
at, | 43 int width, int height, int rowBytes, Format form
at, |
39 bool opt = true /* Use optimization if available
*/); | 44 bool opt = true /* Use optimization if available
*/); |
40 | 45 |
41 // This typedef defines what the nominal aspects of a compression function | 46 // This typedef defines what the nominal aspects of a compression function |
42 // are. The typedef is not meant to be used by clients of the API, but rathe
r | 47 // are. The typedef is not meant to be used by clients of the API, but rathe
r |
43 // allows SIMD optimized compression functions to be implemented. | 48 // allows SIMD optimized compression functions to be implemented. |
44 typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src, | 49 typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src, |
45 int width, int height, int rowBytes); | 50 int width, int height, int rowBytes); |
46 | 51 |
47 // This class implements a blitter that blits directly into a buffer that wi
ll | 52 // Returns the blitter for the given compression format. Note, the blitter |
48 // be used as an R11 EAC compressed texture. We compute this buffer by | 53 // is intended to be used with the proper input. I.e. if you try to blit |
49 // buffering four scan lines and then outputting them all at once. This blit
ter | 54 // RGB source data into an R11 EAC texture, you're gonna have a bad time. |
50 // is only expected to be used with alpha masks, i.e. kAlpha8_SkColorType. | 55 SkBlitter* CreateBlitterForFormat(int width, int height, void* compressedBuf
fer, |
51 class R11_EACBlitter : public SkBlitter { | 56 Format format); |
52 public: | |
53 R11_EACBlitter(int width, int height, void *compressedBuffer); | |
54 virtual ~R11_EACBlitter() { this->flushRuns(); } | |
55 | |
56 // Blit a horizontal run of one or more pixels. | |
57 virtual void blitH(int x, int y, int width) SK_OVERRIDE { | |
58 // This function is intended to be called from any standard RGB | |
59 // buffer, so we should never encounter it. However, if some code | |
60 // path does end up here, then this needs to be investigated. | |
61 SkFAIL("Not implemented!"); | |
62 } | |
63 | |
64 /// Blit a horizontal run of antialiased pixels; runs[] is a *sparse* | |
65 /// zero-terminated run-length encoding of spans of constant alpha value
s. | |
66 virtual void blitAntiH(int x, int y, | |
67 const SkAlpha antialias[], | |
68 const int16_t runs[]) SK_OVERRIDE; | |
69 | |
70 // Blit a vertical run of pixels with a constant alpha value. | |
71 virtual void blitV(int x, int y, int height, SkAlpha alpha) SK_OVERRIDE
{ | |
72 // This function is currently not implemented. It is not explicitly | |
73 // required by the contract, but if at some time a code path runs in
to | |
74 // this function (which is entirely possible), it needs to be implem
ented. | |
75 // | |
76 // TODO (krajcevski): | |
77 // This function will be most easily implemented in one of two ways: | |
78 // 1. Buffer each vertical column value and then construct a list | |
79 // of alpha values and output all of the blocks at once. This onl
y | |
80 // requires a write to the compressed buffer | |
81 // 2. Replace the indices of each block with the proper indices base
d | |
82 // on the alpha value. This requires a read and write of the comp
ressed | |
83 // buffer, but much less overhead. | |
84 SkFAIL("Not implemented!"); | |
85 } | |
86 | |
87 // Blit a solid rectangle one or more pixels wide. | |
88 virtual void blitRect(int x, int y, int width, int height) SK_OVERRIDE { | |
89 // Analogous to blitRow, this function is intended for RGB targets | |
90 // and should never be called by this blitter. Any calls to this fun
ction | |
91 // are probably a bug and should be investigated. | |
92 SkFAIL("Not implemented!"); | |
93 } | |
94 | |
95 // Blit a rectangle with one alpha-blended column on the left, | |
96 // width (zero or more) opaque pixels, and one alpha-blended column | |
97 // on the right. The result will always be at least two pixels wide. | |
98 virtual void blitAntiRect(int x, int y, int width, int height, | |
99 SkAlpha leftAlpha, SkAlpha rightAlpha) SK_OVER
RIDE { | |
100 // This function is currently not implemented. It is not explicitly | |
101 // required by the contract, but if at some time a code path runs in
to | |
102 // this function (which is entirely possible), it needs to be implem
ented. | |
103 // | |
104 // TODO (krajcevski): | |
105 // This function will be most easily implemented as follows: | |
106 // 1. If width/height are smaller than a block, then update the | |
107 // indices of the affected blocks. | |
108 // 2. If width/height are larger than a block, then construct a 9-pa
tch | |
109 // of block encodings that represent the rectangle, and write the
m | |
110 // to the compressed buffer as necessary. Whether or not the bloc
ks | |
111 // are overwritten by zeros or just their indices are updated is
up | |
112 // to debate. | |
113 SkFAIL("Not implemented!"); | |
114 } | |
115 | |
116 // Blit a pattern of pixels defined by a rectangle-clipped mask; | |
117 // typically used for text. | |
118 virtual void blitMask(const SkMask&, const SkIRect& clip) SK_OVERRIDE { | |
119 // This function is currently not implemented. It is not explicitly | |
120 // required by the contract, but if at some time a code path runs in
to | |
121 // this function (which is entirely possible), it needs to be implem
ented. | |
122 // | |
123 // TODO (krajcevski): | |
124 // This function will be most easily implemented in the same way as | |
125 // blitAntiRect above. | |
126 SkFAIL("Not implemented!"); | |
127 } | |
128 | |
129 // If the blitter just sets a single value for each pixel, return the | |
130 // bitmap it draws into, and assign value. If not, return NULL and ignor
e | |
131 // the value parameter. | |
132 virtual const SkBitmap* justAnOpaqueColor(uint32_t* value) SK_OVERRIDE { | |
133 return NULL; | |
134 } | |
135 | |
136 /** | |
137 * Compressed texture blitters only really work correctly if they get | |
138 * four blocks at a time. That being said, this blitter tries it's best | |
139 * to preserve semantics if blitAntiH doesn't get called in too many | |
140 * weird ways... | |
141 */ | |
142 virtual int requestRowsPreserved() const { return kR11_EACBlockSz; } | |
143 | |
144 protected: | |
145 virtual void onNotifyFinished() { this->flushRuns(); } | |
146 | |
147 private: | |
148 static const int kR11_EACBlockSz = 4; | |
149 static const int kPixelsPerBlock = kR11_EACBlockSz * kR11_EACBlockSz; | |
150 | |
151 // The longest possible run of pixels that this blitter will receive. | |
152 // This is initialized in the constructor to 0x7FFE, which is one less | |
153 // than the largest positive 16-bit integer. We make sure that it's one | |
154 // less for debugging purposes. We also don't make this variable static | |
155 // in order to make sure that we can construct a valid pointer to it. | |
156 const int16_t kLongestRun; | |
157 | |
158 // Usually used in conjunction with kLongestRun. This is initialized to | |
159 // zero. | |
160 const SkAlpha kZeroAlpha; | |
161 | |
162 // This is the information that we buffer whenever we're asked to blit | |
163 // a row with this blitter. | |
164 struct BufferedRun { | |
165 const SkAlpha* fAlphas; | |
166 const int16_t* fRuns; | |
167 int fX, fY; | |
168 } fBufferedRuns[kR11_EACBlockSz]; | |
169 | |
170 // The next row (0-3) that we need to blit. This value should never exce
ed | |
171 // the number of rows that we have (kR11_EACBlockSz) | |
172 int fNextRun; | |
173 | |
174 // The width and height of the image that we're blitting | |
175 const int fWidth; | |
176 const int fHeight; | |
177 | |
178 // The R11 EAC buffer that we're blitting into. It is assumed that the b
uffer | |
179 // is large enough to store a compressed image of size fWidth*fHeight. | |
180 uint64_t* const fBuffer; | |
181 | |
182 // Various utility functions | |
183 int blocksWide() const { return fWidth / kR11_EACBlockSz; } | |
184 int blocksTall() const { return fHeight / kR11_EACBlockSz; } | |
185 int totalBlocks() const { return (fWidth * fHeight) / kPixelsPerBlock; } | |
186 | |
187 // Returns the block index for the block containing pixel (x, y). Block | |
188 // indices start at zero and proceed in raster order. | |
189 int getBlockOffset(int x, int y) const { | |
190 SkASSERT(x < fWidth); | |
191 SkASSERT(y < fHeight); | |
192 const int blockCol = x / kR11_EACBlockSz; | |
193 const int blockRow = y / kR11_EACBlockSz; | |
194 return blockRow * this->blocksWide() + blockCol; | |
195 } | |
196 | |
197 // Returns a pointer to the block containing pixel (x, y) | |
198 uint64_t *getBlock(int x, int y) const { | |
199 return fBuffer + this->getBlockOffset(x, y); | |
200 } | |
201 | |
202 // The following function writes the buffered runs to compressed blocks. | |
203 // If fNextRun < 4, then we fill the runs that we haven't buffered with | |
204 // the constant zero buffer. | |
205 void flushRuns(); | |
206 }; | |
207 } | 57 } |
208 | 58 |
209 #endif | 59 #endif |
OLD | NEW |