OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 #include "SkCodecPriv.h" | |
8 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
9 #include "SkSwizzler.h" | 10 #include "SkSwizzler.h" |
10 #include "SkTemplates.h" | 11 #include "SkTemplates.h" |
11 | 12 |
12 // index | 13 inline SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha, |
14 uint8_t maxAlpha) { | |
15 // In the transparent case, this returns 0x0000 | |
16 // In the opaque case, this returns 0xFFFF | |
17 // If the row is neither transparent nor opaque, returns something else | |
18 return (((uint16_t) maxAlpha) << 8) | zeroAlpha; | |
19 } | |
13 | 20 |
14 #define A32_MASK_IN_PLACE (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT) | 21 // kIndex1, kIndex2, kIndex4 |
15 | 22 |
16 static bool swizzle_index_to_n32(void* SK_RESTRICT dstRow, | 23 static SkSwizzler::ResultAlpha swizzle_small_index_to_n32( |
17 const uint8_t* SK_RESTRICT src, | 24 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
18 int width, int deltaSrc, int, const SkPMColor c table[]) { | 25 int bitsPerPixel, int y, const SkPMColor ctable[]) { |
26 | |
27 SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow; | |
28 INIT_RESULT_ALPHA; | |
29 const uint32_t pixelsPerByte = 8 / bitsPerPixel; | |
30 const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte); | |
31 const uint8_t mask = (1 << bitsPerPixel) - 1; | |
32 int x = 0; | |
33 for (uint32_t byte = 0; byte < rowBytes; byte++) { | |
34 uint8_t pixelData = src[byte]; | |
35 for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) { | |
36 uint8_t index = (pixelData >> (8 - bitsPerPixel)) & mask; | |
37 SkPMColor c = ctable[index]; | |
38 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); | |
39 dst[x] = c; | |
40 pixelData <<= bitsPerPixel; | |
41 x++; | |
42 } | |
43 } | |
44 return COMPUTE_RESULT_ALPHA; | |
45 } | |
46 | |
47 // kIndex | |
48 | |
49 static SkSwizzler::ResultAlpha swizzle_index_to_n32( | |
50 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | |
51 int bytesPerPixel, int y, const SkPMColor ctable[]) { | |
19 | 52 |
20 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 53 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
21 SkPMColor cc = A32_MASK_IN_PLACE; | 54 INIT_RESULT_ALPHA; |
22 for (int x = 0; x < width; x++) { | 55 for (int x = 0; x < width; x++) { |
23 SkPMColor c = ctable[*src]; | 56 SkPMColor c = ctable[*src]; |
24 cc &= c; | 57 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
25 dst[x] = c; | 58 dst[x] = c; |
26 src += deltaSrc; | 59 src++; |
27 } | 60 } |
28 return cc != A32_MASK_IN_PLACE; | 61 return COMPUTE_RESULT_ALPHA; |
29 } | 62 } |
30 | 63 |
31 static bool swizzle_index_to_n32_skipZ(void* SK_RESTRICT dstRow, | 64 static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( |
32 const uint8_t* SK_RESTRICT src, | 65 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
33 int width, int deltaSrc, int, | 66 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
34 const SkPMColor ctable[]) { | |
35 | 67 |
36 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 68 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
37 SkPMColor cc = A32_MASK_IN_PLACE; | 69 INIT_RESULT_ALPHA; |
38 for (int x = 0; x < width; x++) { | 70 for (int x = 0; x < width; x++) { |
39 SkPMColor c = ctable[*src]; | 71 SkPMColor c = ctable[*src]; |
40 cc &= c; | 72 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
41 if (c != 0) { | 73 if (c != 0) { |
42 dst[x] = c; | 74 dst[x] = c; |
43 } | 75 } |
44 src += deltaSrc; | 76 src++; |
45 } | 77 } |
46 return cc != A32_MASK_IN_PLACE; | 78 return COMPUTE_RESULT_ALPHA; |
47 } | 79 } |
48 | 80 |
49 #undef A32_MASK_IN_PLACE | 81 #undef A32_MASK_IN_PLACE |
50 | 82 |
83 static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( | |
84 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | |
85 int bytesPerPixel, int y, const SkPMColor ctable[]) { | |
86 | |
87 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | |
88 for (int x = 0; x < width; x++) { | |
89 dst[x] = SkPackARGB32NoCheck(0xFF, src[2], src[1], src[0]); | |
90 src += bytesPerPixel; | |
91 } | |
92 return SkSwizzler::kOpaque_ResultAlpha; | |
93 } | |
94 | |
95 // kBGRA | |
96 | |
97 static SkSwizzler::ResultAlpha swizzle_bgra_to_n32( | |
98 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | |
99 int bytesPerPixel, int y, const SkPMColor ctable[]) { | |
100 | |
101 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | |
102 INIT_RESULT_ALPHA; | |
103 for (int x = 0; x < width; x++) { | |
104 uint8_t alpha = src[3]; | |
105 UPDATE_RESULT_ALPHA(alpha); | |
106 dst[x] = SkPackARGB32NoCheck(alpha, src[2], src[1], src[0]); | |
107 src += bytesPerPixel; | |
108 } | |
109 return COMPUTE_RESULT_ALPHA; | |
110 } | |
111 | |
51 // n32 | 112 // n32 |
52 static bool swizzle_rgbx_to_n32(void* SK_RESTRICT dstRow, | 113 static SkSwizzler::ResultAlpha swizzle_rgbx_to_n32( |
53 const uint8_t* SK_RESTRICT src, | 114 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
54 int width, int deltaSrc, int, const SkPMColor[]) { | 115 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
116 | |
55 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 117 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
56 for (int x = 0; x < width; x++) { | 118 for (int x = 0; x < width; x++) { |
57 dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); | 119 dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); |
58 src += deltaSrc; | 120 src += bytesPerPixel; |
59 } | 121 } |
60 return false; | 122 return SkSwizzler::kOpaque_ResultAlpha; |
61 } | 123 } |
62 | 124 |
63 static bool swizzle_rgba_to_n32_premul(void* SK_RESTRICT dstRow, | 125 static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul( |
64 const uint8_t* SK_RESTRICT src, | 126 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
65 int width, int deltaSrc, int, const SkPMC olor[]) { | 127 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
128 | |
66 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 129 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
67 unsigned alphaMask = 0xFF; | 130 INIT_RESULT_ALPHA; |
68 for (int x = 0; x < width; x++) { | 131 for (int x = 0; x < width; x++) { |
69 unsigned alpha = src[3]; | 132 unsigned alpha = src[3]; |
133 UPDATE_RESULT_ALPHA(alpha); | |
70 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); | 134 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
71 src += deltaSrc; | 135 src += bytesPerPixel; |
72 alphaMask &= alpha; | |
73 } | 136 } |
74 return alphaMask != 0xFF; | 137 return COMPUTE_RESULT_ALPHA; |
75 } | 138 } |
76 | 139 |
77 static bool swizzle_rgba_to_n32_unpremul(void* SK_RESTRICT dstRow, | 140 static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_unpremul( |
78 const uint8_t* SK_RESTRICT src, | 141 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
79 int width, int deltaSrc, int, | 142 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
80 const SkPMColor[]) { | 143 |
81 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); | 144 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
82 unsigned alphaMask = 0xFF; | 145 INIT_RESULT_ALPHA; |
83 for (int x = 0; x < width; x++) { | 146 for (int x = 0; x < width; x++) { |
84 unsigned alpha = src[3]; | 147 unsigned alpha = src[3]; |
148 UPDATE_RESULT_ALPHA(alpha); | |
85 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); | 149 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
86 src += deltaSrc; | 150 src += bytesPerPixel; |
87 alphaMask &= alpha; | |
88 } | 151 } |
89 return alphaMask != 0xFF; | 152 return COMPUTE_RESULT_ALPHA; |
90 } | 153 } |
91 | 154 |
92 static bool swizzle_rgba_to_n32_premul_skipZ(void* SK_RESTRICT dstRow, | 155 static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul_skipZ( |
93 const uint8_t* SK_RESTRICT src, | 156 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
94 int width, int deltaSrc, int, | 157 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
95 const SkPMColor[]) { | 158 |
96 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 159 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
97 unsigned alphaMask = 0xFF; | 160 INIT_RESULT_ALPHA; |
98 for (int x = 0; x < width; x++) { | 161 for (int x = 0; x < width; x++) { |
99 unsigned alpha = src[3]; | 162 unsigned alpha = src[3]; |
163 UPDATE_RESULT_ALPHA(alpha); | |
100 if (0 != alpha) { | 164 if (0 != alpha) { |
101 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); | 165 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
102 } | 166 } |
103 src += deltaSrc; | 167 src += bytesPerPixel; |
104 alphaMask &= alpha; | |
105 } | 168 } |
106 return alphaMask != 0xFF; | 169 return COMPUTE_RESULT_ALPHA; |
107 } | 170 } |
108 | 171 |
109 /** | 172 /** |
110 FIXME: This was my idea to cheat in order to continue taking advantage of sk ipping zeroes. | 173 FIXME: This was my idea to cheat in order to continue taking advantage of sk ipping zeroes. |
111 This would be fine for drawing normally, but not for drawing with transfer m odes. Being | 174 This would be fine for drawing normally, but not for drawing with transfer m odes. Being |
112 honest means we can draw correctly with transfer modes, with the cost of not being able | 175 honest means we can draw correctly with transfer modes, with the cost of not being able |
113 to take advantage of Android's free unwritten pages. Something to keep in mi nd when we | 176 to take advantage of Android's free unwritten pages. Something to keep in mi nd when we |
114 decide whether to switch to unpremul default. | 177 decide whether to switch to unpremul default. |
115 static bool swizzle_rgba_to_n32_unpremul_skipZ(void* SK_RESTRICT dstRow, | 178 static bool swizzle_rgba_to_n32_unpremul_skipZ(void* SK_RESTRICT dstRow, |
116 const uint8_t* SK_RESTRICT src, | 179 const uint8_t* SK_RESTRICT src, |
117 int width, int deltaSrc, int, | 180 int width, int bitsPerPixel, |
118 const SkPMColor[]) { | 181 const SkPMColor[]) { |
119 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 182 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
120 unsigned alphaMask = 0xFF; | 183 unsigned alphaMask = 0xFF; |
121 for (int x = 0; x < width; x++) { | 184 for (int x = 0; x < width; x++) { |
122 unsigned alpha = src[3]; | 185 unsigned alpha = src[3]; |
123 // NOTE: We cheat here. The caller requested unpremul and skip zeroes. I t's possible | 186 // NOTE: We cheat here. The caller requested unpremul and skip zeroes. I t's possible |
124 // the color components are not zero, but we skip them anyway, meaning t hey'll remain | 187 // the color components are not zero, but we skip them anyway, meaning t hey'll remain |
125 // zero (implied by the request to skip zeroes). | 188 // zero (implied by the request to skip zeroes). |
126 if (0 != alpha) { | 189 if (0 != alpha) { |
127 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); | 190 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
128 } | 191 } |
129 src += deltaSrc; | 192 src += deltaSrc; |
130 alphaMask &= alpha; | 193 alphaMask &= alpha; |
131 } | 194 } |
132 return alphaMask != 0xFF; | 195 return alphaMask != 0xFF; |
133 } | 196 } |
134 */ | 197 */ |
135 | 198 |
136 SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, const SkPMColor * ctable, | 199 SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
200 const SkPMColor* ctable, | |
137 const SkImageInfo& info, void* dst, | 201 const SkImageInfo& info, void* dst, |
138 size_t dstRowBytes, bool skipZeroes) { | 202 size_t dstRowBytes, bool skipZeroes) { |
139 if (info.colorType() == kUnknown_SkColorType) { | 203 if (kUnknown_SkColorType == info.colorType()) { |
140 return NULL; | 204 return NULL; |
141 } | 205 } |
142 if (info.minRowBytes() > dstRowBytes) { | 206 if (info.minRowBytes() > dstRowBytes) { |
143 return NULL; | 207 return NULL; |
144 } | 208 } |
145 if (kIndex == sc && NULL == ctable) { | 209 if ((kIndex == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc) |
210 && NULL == ctable) { | |
146 return NULL; | 211 return NULL; |
147 } | 212 } |
148 RowProc proc = NULL; | 213 RowProc proc = NULL; |
149 switch (sc) { | 214 switch (sc) { |
215 case kIndex1: | |
216 case kIndex2: | |
217 case kIndex4: | |
218 switch (info.colorType()) { | |
219 case kN32_SkColorType: | |
220 proc = &swizzle_small_index_to_n32; | |
221 break; | |
222 default: | |
223 break; | |
224 } | |
225 break; | |
150 case kIndex: | 226 case kIndex: |
151 switch (info.colorType()) { | 227 switch (info.colorType()) { |
152 case kN32_SkColorType: | 228 case kN32_SkColorType: |
153 // We assume the color premultiplied ctable (or not) as desi red. | |
154 if (skipZeroes) { | 229 if (skipZeroes) { |
155 proc = &swizzle_index_to_n32_skipZ; | 230 proc = &swizzle_index_to_n32_skipZ; |
156 } else { | 231 } else { |
157 proc = &swizzle_index_to_n32; | 232 proc = &swizzle_index_to_n32; |
158 } | 233 } |
159 break; | 234 break; |
160 | |
161 default: | 235 default: |
162 break; | 236 break; |
163 } | 237 } |
238 break; | |
239 case kBGR: | |
240 case kBGRX: | |
241 switch (info.colorType()) { | |
242 case kN32_SkColorType: | |
243 proc = &swizzle_bgrx_to_n32; | |
244 break; | |
245 default: | |
246 break; | |
247 } | |
248 break; | |
249 case kBGRA: | |
250 switch (info.colorType()) { | |
251 case kN32_SkColorType: | |
252 proc = &swizzle_bgra_to_n32; | |
253 break; | |
254 default: | |
255 break; | |
256 } | |
164 break; | 257 break; |
165 case kRGBX: | 258 case kRGBX: |
166 // TODO: Support other swizzles. | 259 // TODO: Support other swizzles. |
167 switch (info.colorType()) { | 260 switch (info.colorType()) { |
168 case kN32_SkColorType: | 261 case kN32_SkColorType: |
169 proc = &swizzle_rgbx_to_n32; | 262 proc = &swizzle_rgbx_to_n32; |
170 break; | 263 break; |
171 default: | 264 default: |
172 break; | 265 break; |
173 } | 266 } |
174 break; | 267 break; |
175 case kRGBA: | 268 case kRGBA: |
176 switch (info.colorType()) { | 269 switch (info.colorType()) { |
177 case kN32_SkColorType: | 270 case kN32_SkColorType: |
178 if (info.alphaType() == kUnpremul_SkAlphaType) { | 271 if (info.alphaType() == kUnpremul_SkAlphaType) { |
179 // Respect skipZeroes? | 272 // Respect skipZeroes? |
180 proc = &swizzle_rgba_to_n32_unpremul; | 273 proc = &swizzle_rgba_to_n32_unpremul; |
181 } else { | 274 } else { |
182 if (skipZeroes) { | 275 if (skipZeroes) { |
183 proc = &swizzle_rgba_to_n32_premul_skipZ; | 276 proc = &swizzle_rgba_to_n32_premul_skipZ; |
184 } else { | 277 } else { |
185 proc = &swizzle_rgba_to_n32_premul; | 278 proc = &swizzle_rgba_to_n32_premul; |
186 } | 279 } |
187 } | 280 } |
188 break; | 281 break; |
189 default: | 282 default: |
190 break; | 283 break; |
191 } | 284 } |
192 break; | 285 break; |
286 case kRGB: | |
287 switch (info.colorType()) { | |
288 case kN32_SkColorType: | |
289 proc = &swizzle_rgbx_to_n32; | |
290 break; | |
291 default: | |
292 break; | |
293 } | |
294 break; | |
193 default: | 295 default: |
194 break; | 296 break; |
195 } | 297 } |
196 if (NULL == proc) { | 298 if (NULL == proc) { |
197 return NULL; | 299 return NULL; |
198 } | 300 } |
199 return SkNEW_ARGS(SkSwizzler, (proc, ctable, BytesPerPixel(sc), info, dst, d stRowBytes)); | 301 |
302 // Store deltaSrc in bytes if it is an even multiple, otherwise use bits | |
303 int deltaSrc = SkIsAlign8(BitsPerPixel(sc)) ? BytesPerPixel(sc) : | |
scroggo
2015/03/13 14:06:59
Again, I think you want SkIsAlign4.
scroggo
2015/03/16 13:49:40
Just for posterity, this was my misunderstanding.
| |
304 BitsPerPixel(sc); | |
305 return SkNEW_ARGS(SkSwizzler, (proc, ctable, deltaSrc, info, dst, | |
306 dstRowBytes)); | |
200 } | 307 } |
201 | 308 |
202 SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBpp, | 309 SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, |
203 const SkImageInfo& info, void* dst, size_t rowBytes) | 310 int deltaSrc, const SkImageInfo& info, void* dst, |
311 size_t rowBytes) | |
204 : fRowProc(proc) | 312 : fRowProc(proc) |
205 , fColorTable(ctable) | 313 , fColorTable(ctable) |
206 , fSrcPixelSize(srcBpp) | 314 , fDeltaSrc(deltaSrc) |
207 , fDstInfo(info) | 315 , fDstInfo(info) |
208 , fDstRow(dst) | 316 , fDstRow(dst) |
209 , fDstRowBytes(rowBytes) | 317 , fDstRowBytes(rowBytes) |
210 , fCurrY(0) | 318 , fCurrY(0) |
211 { | 319 { |
320 SkDEBUGCODE(fNextMode = kUninitialized_NextMode); | |
212 } | 321 } |
213 | 322 |
214 bool SkSwizzler::next(const uint8_t* SK_RESTRICT src) { | 323 SkSwizzler::ResultAlpha SkSwizzler::next(const uint8_t* SK_RESTRICT src) { |
215 SkASSERT(fCurrY < fDstInfo.height()); | 324 SkASSERT(0 <= fCurrY && fCurrY < fDstInfo.height()); |
216 const bool hadAlpha = fRowProc(fDstRow, src, fDstInfo.width(), fSrcPixelSize , | 325 SkASSERT(kDesignateRow_NextMode != fNextMode); |
217 fCurrY, fColorTable); | 326 SkDEBUGCODE(fNextMode = kConsecutive_NextMode); |
218 fCurrY++; | 327 |
328 // Decode a row | |
329 const ResultAlpha result = fRowProc(fDstRow, src, fDstInfo.width(), | |
330 fDeltaSrc, fCurrY, fColorTable); | |
331 | |
332 // Move to the next row and return the result | |
219 fDstRow = SkTAddOffset<void>(fDstRow, fDstRowBytes); | 333 fDstRow = SkTAddOffset<void>(fDstRow, fDstRowBytes); |
220 return hadAlpha; | 334 return result; |
221 } | 335 } |
336 | |
337 SkSwizzler::ResultAlpha SkSwizzler::next(const uint8_t* SK_RESTRICT src, | |
338 int y) { | |
339 SkASSERT(0 <= y && y < fDstInfo.height()); | |
340 SkASSERT(kConsecutive_NextMode != fNextMode); | |
341 SkDEBUGCODE(fNextMode = kDesignateRow_NextMode); | |
342 | |
343 // Choose the row | |
344 void* row = SkTAddOffset<void>(fDstRow, y*fDstRowBytes); | |
345 | |
346 // Decode the row | |
347 return fRowProc(row, src, fDstInfo.width(), fDeltaSrc, fCurrY, | |
348 fColorTable); | |
349 } | |
OLD | NEW |