blob: ea57934c642d8a1cbe8cf5a5aefe0e3c24f73535 [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"
krajcevskiad1df152014-07-21 11:44:37 -070012#include "SkBlitter.h"
krajcevskieecc35f2014-06-20 11:43:00 -070013
krajcevskiae614402014-06-10 14:52:28 -070014class SkBitmap;
15class 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.
krajcevskiae614402014-06-10 14:52:28 -070021 kLATC_Format,
krajcevskif3d15dc2014-06-30 08:47:33 -070022 kR11_EAC_Format,
krajcevskiae614402014-06-10 14:52:28 -070023
krajcevskif3d15dc2014-06-30 08:47:33 -070024 kLast_Format = kR11_EAC_Format
krajcevskiae614402014-06-10 14:52:28 -070025 };
26 static const int kFormatCnt = kLast_Format + 1;
27
28 // Returns an SkData holding a blob of compressed data that corresponds
29 // to the bitmap. If the bitmap colorType cannot be compressed using the
30 // associated format, then we return NULL. The caller is responsible for
31 // calling unref() on the returned data.
32 SkData* CompressBitmapToFormat(const SkBitmap& bitmap, Format format);
krajcevskieecc35f2014-06-20 11:43:00 -070033
34 // 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
36 // be large enough to hold the compressed data according to the format.
37 bool CompressBufferToFormat(uint8_t* dst, const uint8_t* src, SkColorType srcColorType,
krajcevski630598c2014-07-14 12:00:04 -070038 int width, int height, int rowBytes, Format format,
39 bool opt = true /* Use optimization if available */);
40
41 // 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 rather
43 // allows SIMD optimized compression functions to be implemented.
44 typedef bool (*CompressionProc)(uint8_t* dst, const uint8_t* src,
45 int width, int height, int rowBytes);
krajcevskiad1df152014-07-21 11:44:37 -070046
47 // This class implements a blitter that blits directly into a buffer that will
48 // be used as an R11 EAC compressed texture. We compute this buffer by
49 // buffering four scan lines and then outputting them all at once. This blitter
50 // is only expected to be used with alpha masks, i.e. kAlpha8_SkColorType.
51 class R11_EACBlitter : public SkBlitter {
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 values.
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 into
74 // this function (which is entirely possible), it needs to be implemented.
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 only
80 // requires a write to the compressed buffer
81 // 2. Replace the indices of each block with the proper indices based
82 // on the alpha value. This requires a read and write of the compressed
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 function
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_OVERRIDE {
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 into
102 // this function (which is entirely possible), it needs to be implemented.
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-patch
109 // of block encodings that represent the rectangle, and write them
110 // to the compressed buffer as necessary. Whether or not the blocks
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 into
121 // this function (which is entirely possible), it needs to be implemented.
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 ignore
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 exceed
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 buffer
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 };
krajcevskiae614402014-06-10 14:52:28 -0700207}
208
209#endif