blob: eeae007d9e0e9aef0b97617d676d409ee1b124b5 [file] [log] [blame]
msarett74114382015-03-16 11:55:18 -07001/*
2 * Copyright 2015 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#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -04009#include "SkColorData.h"
msarett74114382015-03-16 11:55:18 -070010#include "SkMaskSwizzler.h"
11
msarett34e0ec42016-04-22 16:27:24 -070012static void swizzle_mask16_to_rgba_opaque(
msarett5406d6f2015-08-31 06:55:13 -070013 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
14 uint32_t startX, uint32_t sampleX) {
msarett74114382015-03-16 11:55:18 -070015
16 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -070017 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
msarett74114382015-03-16 11:55:18 -070018 SkPMColor* dstPtr = (SkPMColor*) dstRow;
19 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -070020 uint16_t p = srcPtr[0];
msarett74114382015-03-16 11:55:18 -070021 uint8_t red = masks->getRed(p);
22 uint8_t green = masks->getGreen(p);
23 uint8_t blue = masks->getBlue(p);
msarett34e0ec42016-04-22 16:27:24 -070024 dstPtr[i] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -070025 srcPtr += sampleX;
msarett74114382015-03-16 11:55:18 -070026 }
msarett74114382015-03-16 11:55:18 -070027}
28
msarett34e0ec42016-04-22 16:27:24 -070029static void swizzle_mask16_to_bgra_opaque(
30 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
31 uint32_t startX, uint32_t sampleX) {
32
33 // Use the masks to decode to the destination
34 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
35 SkPMColor* dstPtr = (SkPMColor*) dstRow;
36 for (int i = 0; i < width; i++) {
37 uint16_t p = srcPtr[0];
38 uint8_t red = masks->getRed(p);
39 uint8_t green = masks->getGreen(p);
40 uint8_t blue = masks->getBlue(p);
41 dstPtr[i] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
42 srcPtr += sampleX;
43 }
44}
45
46static void swizzle_mask16_to_rgba_unpremul(
msarett5406d6f2015-08-31 06:55:13 -070047 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
48 uint32_t startX, uint32_t sampleX) {
msarett74114382015-03-16 11:55:18 -070049
50 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -070051 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
msarett74114382015-03-16 11:55:18 -070052 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msarett74114382015-03-16 11:55:18 -070053 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -070054 uint16_t p = srcPtr[0];
msarett74114382015-03-16 11:55:18 -070055 uint8_t red = masks->getRed(p);
56 uint8_t green = masks->getGreen(p);
57 uint8_t blue = masks->getBlue(p);
58 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -070059 dstPtr[i] = SkPackARGB_as_RGBA(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -070060 srcPtr += sampleX;
msarett74114382015-03-16 11:55:18 -070061 }
msarett74114382015-03-16 11:55:18 -070062}
63
msarett34e0ec42016-04-22 16:27:24 -070064static void swizzle_mask16_to_bgra_unpremul(
msarett5406d6f2015-08-31 06:55:13 -070065 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
66 uint32_t startX, uint32_t sampleX) {
msaretteed039b2015-03-18 11:11:19 -070067
68 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -070069 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
msaretteed039b2015-03-18 11:11:19 -070070 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msaretteed039b2015-03-18 11:11:19 -070071 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -070072 uint16_t p = srcPtr[0];
msaretteed039b2015-03-18 11:11:19 -070073 uint8_t red = masks->getRed(p);
74 uint8_t green = masks->getGreen(p);
75 uint8_t blue = masks->getBlue(p);
76 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -070077 dstPtr[i] = SkPackARGB_as_BGRA(alpha, red, green, blue);
78 srcPtr += sampleX;
79 }
80}
81
82static void swizzle_mask16_to_rgba_premul(
83 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
84 uint32_t startX, uint32_t sampleX) {
85
86 // Use the masks to decode to the destination
87 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
88 SkPMColor* dstPtr = (SkPMColor*) dstRow;
89 for (int i = 0; i < width; i++) {
90 uint16_t p = srcPtr[0];
91 uint8_t red = masks->getRed(p);
92 uint8_t green = masks->getGreen(p);
93 uint8_t blue = masks->getBlue(p);
94 uint8_t alpha = masks->getAlpha(p);
95 dstPtr[i] = premultiply_argb_as_rgba(alpha, red, green, blue);
96 srcPtr += sampleX;
97 }
98}
99
100static void swizzle_mask16_to_bgra_premul(
101 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
102 uint32_t startX, uint32_t sampleX) {
103
104 // Use the masks to decode to the destination
105 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
106 SkPMColor* dstPtr = (SkPMColor*) dstRow;
107 for (int i = 0; i < width; i++) {
108 uint16_t p = srcPtr[0];
109 uint8_t red = masks->getRed(p);
110 uint8_t green = masks->getGreen(p);
111 uint8_t blue = masks->getBlue(p);
112 uint8_t alpha = masks->getAlpha(p);
113 dstPtr[i] = premultiply_argb_as_bgra(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700114 srcPtr += sampleX;
msaretteed039b2015-03-18 11:11:19 -0700115 }
msaretteed039b2015-03-18 11:11:19 -0700116}
117
scroggocc2feb12015-08-14 08:32:46 -0700118// TODO (msarett): We have promoted a two byte per pixel image to 8888, only to
119// convert it back to 565. Instead, we should swizzle to 565 directly.
msaretta4970dc2016-01-11 07:23:23 -0800120static void swizzle_mask16_to_565(
msarett5406d6f2015-08-31 06:55:13 -0700121 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
122 uint32_t startX, uint32_t sampleX) {
scroggocc2feb12015-08-14 08:32:46 -0700123
124 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -0700125 uint16_t* srcPtr = ((uint16_t*) srcRow) + startX;
scroggocc2feb12015-08-14 08:32:46 -0700126 uint16_t* dstPtr = (uint16_t*) dstRow;
127 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -0700128 uint16_t p = srcPtr[0];
scroggocc2feb12015-08-14 08:32:46 -0700129 uint8_t red = masks->getRed(p);
130 uint8_t green = masks->getGreen(p);
131 uint8_t blue = masks->getBlue(p);
132 dstPtr[i] = SkPack888ToRGB16(red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700133 srcPtr += sampleX;
scroggocc2feb12015-08-14 08:32:46 -0700134 }
scroggocc2feb12015-08-14 08:32:46 -0700135}
136
msarett34e0ec42016-04-22 16:27:24 -0700137static void swizzle_mask24_to_rgba_opaque(
msarett5406d6f2015-08-31 06:55:13 -0700138 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
139 uint32_t startX, uint32_t sampleX) {
msarett74114382015-03-16 11:55:18 -0700140
141 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -0700142 srcRow += 3 * startX;
msarett74114382015-03-16 11:55:18 -0700143 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msarett5406d6f2015-08-31 06:55:13 -0700144 for (int i = 0; i < width; i++) {
145 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
msarett74114382015-03-16 11:55:18 -0700146 uint8_t red = masks->getRed(p);
147 uint8_t green = masks->getGreen(p);
148 uint8_t blue = masks->getBlue(p);
msarett34e0ec42016-04-22 16:27:24 -0700149 dstPtr[i] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700150 srcRow += 3 * sampleX;
msarett74114382015-03-16 11:55:18 -0700151 }
msarett74114382015-03-16 11:55:18 -0700152}
153
msarett34e0ec42016-04-22 16:27:24 -0700154static void swizzle_mask24_to_bgra_opaque(
155 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
156 uint32_t startX, uint32_t sampleX) {
157
158 // Use the masks to decode to the destination
159 srcRow += 3 * startX;
160 SkPMColor* dstPtr = (SkPMColor*) dstRow;
161 for (int i = 0; i < width; i++) {
162 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
163 uint8_t red = masks->getRed(p);
164 uint8_t green = masks->getGreen(p);
165 uint8_t blue = masks->getBlue(p);
166 dstPtr[i] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
167 srcRow += 3 * sampleX;
168 }
169}
170
171static void swizzle_mask24_to_rgba_unpremul(
msarett5406d6f2015-08-31 06:55:13 -0700172 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
173 uint32_t startX, uint32_t sampleX) {
msarett74114382015-03-16 11:55:18 -0700174
175 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -0700176 srcRow += 3 * startX;
msarett74114382015-03-16 11:55:18 -0700177 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msarett74114382015-03-16 11:55:18 -0700178 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -0700179 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
msarett74114382015-03-16 11:55:18 -0700180 uint8_t red = masks->getRed(p);
181 uint8_t green = masks->getGreen(p);
182 uint8_t blue = masks->getBlue(p);
183 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -0700184 dstPtr[i] = SkPackARGB_as_RGBA(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700185 srcRow += 3 * sampleX;
msarett74114382015-03-16 11:55:18 -0700186 }
msarett74114382015-03-16 11:55:18 -0700187}
188
msarett34e0ec42016-04-22 16:27:24 -0700189static void swizzle_mask24_to_bgra_unpremul(
msarett5406d6f2015-08-31 06:55:13 -0700190 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
191 uint32_t startX, uint32_t sampleX) {
msaretteed039b2015-03-18 11:11:19 -0700192
193 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -0700194 srcRow += 3 * startX;
msaretteed039b2015-03-18 11:11:19 -0700195 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msaretteed039b2015-03-18 11:11:19 -0700196 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -0700197 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
msaretteed039b2015-03-18 11:11:19 -0700198 uint8_t red = masks->getRed(p);
199 uint8_t green = masks->getGreen(p);
200 uint8_t blue = masks->getBlue(p);
201 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -0700202 dstPtr[i] = SkPackARGB_as_BGRA(alpha, red, green, blue);
203 srcRow += 3 * sampleX;
204 }
205}
206
207static void swizzle_mask24_to_rgba_premul(
208 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
209 uint32_t startX, uint32_t sampleX) {
210
211 // Use the masks to decode to the destination
212 srcRow += 3 * startX;
213 SkPMColor* dstPtr = (SkPMColor*) dstRow;
214 for (int i = 0; i < width; i++) {
215 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
216 uint8_t red = masks->getRed(p);
217 uint8_t green = masks->getGreen(p);
218 uint8_t blue = masks->getBlue(p);
219 uint8_t alpha = masks->getAlpha(p);
220 dstPtr[i] = premultiply_argb_as_rgba(alpha, red, green, blue);
221 srcRow += 3 * sampleX;
222 }
223}
224
225static void swizzle_mask24_to_bgra_premul(
226 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
227 uint32_t startX, uint32_t sampleX) {
228
229 // Use the masks to decode to the destination
230 srcRow += 3 * startX;
231 SkPMColor* dstPtr = (SkPMColor*) dstRow;
232 for (int i = 0; i < width; i++) {
233 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
234 uint8_t red = masks->getRed(p);
235 uint8_t green = masks->getGreen(p);
236 uint8_t blue = masks->getBlue(p);
237 uint8_t alpha = masks->getAlpha(p);
238 dstPtr[i] = premultiply_argb_as_bgra(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700239 srcRow += 3 * sampleX;
240 }
msarett5406d6f2015-08-31 06:55:13 -0700241}
242
msaretta4970dc2016-01-11 07:23:23 -0800243static void swizzle_mask24_to_565(
msarett5406d6f2015-08-31 06:55:13 -0700244 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
245 uint32_t startX, uint32_t sampleX) {
246
247 // Use the masks to decode to the destination
248 srcRow += 3 * startX;
249 uint16_t* dstPtr = (uint16_t*) dstRow;
250 for (int i = 0; i < width; i++) {
251 uint32_t p = srcRow[0] | (srcRow[1] << 8) | srcRow[2] << 16;
252 uint8_t red = masks->getRed(p);
253 uint8_t green = masks->getGreen(p);
254 uint8_t blue = masks->getBlue(p);
255 dstPtr[i] = SkPack888ToRGB16(red, green, blue);
256 srcRow += 3 * sampleX;
257 }
msarett5406d6f2015-08-31 06:55:13 -0700258}
259
msarett34e0ec42016-04-22 16:27:24 -0700260static void swizzle_mask32_to_rgba_opaque(
msarett5406d6f2015-08-31 06:55:13 -0700261 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
262 uint32_t startX, uint32_t sampleX) {
263
264 // Use the masks to decode to the destination
265 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
266 SkPMColor* dstPtr = (SkPMColor*) dstRow;
267 for (int i = 0; i < width; i++) {
268 uint32_t p = srcPtr[0];
269 uint8_t red = masks->getRed(p);
270 uint8_t green = masks->getGreen(p);
271 uint8_t blue = masks->getBlue(p);
msarett34e0ec42016-04-22 16:27:24 -0700272 dstPtr[i] = SkPackARGB_as_RGBA(0xFF, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700273 srcPtr += sampleX;
274 }
msarett5406d6f2015-08-31 06:55:13 -0700275}
276
msarett34e0ec42016-04-22 16:27:24 -0700277static void swizzle_mask32_to_bgra_opaque(
278 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
279 uint32_t startX, uint32_t sampleX) {
280
281 // Use the masks to decode to the destination
282 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
283 SkPMColor* dstPtr = (SkPMColor*) dstRow;
284 for (int i = 0; i < width; i++) {
285 uint32_t p = srcPtr[0];
286 uint8_t red = masks->getRed(p);
287 uint8_t green = masks->getGreen(p);
288 uint8_t blue = masks->getBlue(p);
289 dstPtr[i] = SkPackARGB_as_BGRA(0xFF, red, green, blue);
290 srcPtr += sampleX;
291 }
292}
293
294static void swizzle_mask32_to_rgba_unpremul(
msarett5406d6f2015-08-31 06:55:13 -0700295 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
296 uint32_t startX, uint32_t sampleX) {
297
298 // Use the masks to decode to the destination
299 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
300 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msarett5406d6f2015-08-31 06:55:13 -0700301 for (int i = 0; i < width; i++) {
302 uint32_t p = srcPtr[0];
303 uint8_t red = masks->getRed(p);
304 uint8_t green = masks->getGreen(p);
305 uint8_t blue = masks->getBlue(p);
306 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -0700307 dstPtr[i] = SkPackARGB_as_RGBA(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700308 srcPtr += sampleX;
309 }
msarett5406d6f2015-08-31 06:55:13 -0700310}
311
msarett34e0ec42016-04-22 16:27:24 -0700312static void swizzle_mask32_to_bgra_unpremul(
msarett5406d6f2015-08-31 06:55:13 -0700313 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
314 uint32_t startX, uint32_t sampleX) {
315
316 // Use the masks to decode to the destination
317 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
318 SkPMColor* dstPtr = (SkPMColor*) dstRow;
msarett5406d6f2015-08-31 06:55:13 -0700319 for (int i = 0; i < width; i++) {
320 uint32_t p = srcPtr[0];
321 uint8_t red = masks->getRed(p);
322 uint8_t green = masks->getGreen(p);
323 uint8_t blue = masks->getBlue(p);
324 uint8_t alpha = masks->getAlpha(p);
msarett34e0ec42016-04-22 16:27:24 -0700325 dstPtr[i] = SkPackARGB_as_BGRA(alpha, red, green, blue);
326 srcPtr += sampleX;
327 }
328}
329
330static void swizzle_mask32_to_rgba_premul(
331 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
332 uint32_t startX, uint32_t sampleX) {
333
334 // Use the masks to decode to the destination
335 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
336 SkPMColor* dstPtr = (SkPMColor*) dstRow;
337 for (int i = 0; i < width; i++) {
338 uint32_t p = srcPtr[0];
339 uint8_t red = masks->getRed(p);
340 uint8_t green = masks->getGreen(p);
341 uint8_t blue = masks->getBlue(p);
342 uint8_t alpha = masks->getAlpha(p);
343 dstPtr[i] = premultiply_argb_as_rgba(alpha, red, green, blue);
344 srcPtr += sampleX;
345 }
346}
347
348static void swizzle_mask32_to_bgra_premul(
349 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
350 uint32_t startX, uint32_t sampleX) {
351
352 // Use the masks to decode to the destination
353 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
354 SkPMColor* dstPtr = (SkPMColor*) dstRow;
355 for (int i = 0; i < width; i++) {
356 uint32_t p = srcPtr[0];
357 uint8_t red = masks->getRed(p);
358 uint8_t green = masks->getGreen(p);
359 uint8_t blue = masks->getBlue(p);
360 uint8_t alpha = masks->getAlpha(p);
361 dstPtr[i] = premultiply_argb_as_bgra(alpha, red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700362 srcPtr += sampleX;
msaretteed039b2015-03-18 11:11:19 -0700363 }
msaretteed039b2015-03-18 11:11:19 -0700364}
365
msaretta4970dc2016-01-11 07:23:23 -0800366static void swizzle_mask32_to_565(
msarett5406d6f2015-08-31 06:55:13 -0700367 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks,
368 uint32_t startX, uint32_t sampleX) {
scroggocc2feb12015-08-14 08:32:46 -0700369 // Use the masks to decode to the destination
msarett5406d6f2015-08-31 06:55:13 -0700370 uint32_t* srcPtr = ((uint32_t*) srcRow) + startX;
scroggocc2feb12015-08-14 08:32:46 -0700371 uint16_t* dstPtr = (uint16_t*) dstRow;
372 for (int i = 0; i < width; i++) {
msarett5406d6f2015-08-31 06:55:13 -0700373 uint32_t p = srcPtr[0];
scroggocc2feb12015-08-14 08:32:46 -0700374 uint8_t red = masks->getRed(p);
375 uint8_t green = masks->getGreen(p);
376 uint8_t blue = masks->getBlue(p);
377 dstPtr[i] = SkPack888ToRGB16(red, green, blue);
msarett5406d6f2015-08-31 06:55:13 -0700378 srcPtr += sampleX;
scroggocc2feb12015-08-14 08:32:46 -0700379 }
scroggocc2feb12015-08-14 08:32:46 -0700380}
381
msarett74114382015-03-16 11:55:18 -0700382/*
383 *
384 * Create a new mask swizzler
385 *
386 */
msarettfdb47572015-10-13 12:50:14 -0700387SkMaskSwizzler* SkMaskSwizzler::CreateMaskSwizzler(const SkImageInfo& dstInfo,
Leon Scroggins III712476e2018-10-03 15:47:00 -0400388 bool srcIsOpaque, SkMasks* masks, uint32_t bitsPerPixel,
msarettfdb47572015-10-13 12:50:14 -0700389 const SkCodec::Options& options) {
msarett74114382015-03-16 11:55:18 -0700390
391 // Choose the appropriate row procedure
halcanary96fcdcc2015-08-27 07:41:13 -0700392 RowProc proc = nullptr;
msarett74114382015-03-16 11:55:18 -0700393 switch (bitsPerPixel) {
394 case 16:
msarett5406d6f2015-08-31 06:55:13 -0700395 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700396 case kRGBA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400397 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700398 proc = &swizzle_mask16_to_rgba_opaque;
msarettebf44082016-02-03 13:12:38 -0800399 } else {
400 switch (dstInfo.alphaType()) {
401 case kUnpremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700402 proc = &swizzle_mask16_to_rgba_unpremul;
msarettebf44082016-02-03 13:12:38 -0800403 break;
404 case kPremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700405 proc = &swizzle_mask16_to_rgba_premul;
406 break;
407 default:
408 break;
409 }
410 }
411 break;
412 case kBGRA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400413 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700414 proc = &swizzle_mask16_to_bgra_opaque;
415 } else {
416 switch (dstInfo.alphaType()) {
417 case kUnpremul_SkAlphaType:
418 proc = &swizzle_mask16_to_bgra_unpremul;
419 break;
420 case kPremul_SkAlphaType:
421 proc = &swizzle_mask16_to_bgra_premul;
msarettebf44082016-02-03 13:12:38 -0800422 break;
423 default:
424 break;
425 }
msaretteed039b2015-03-18 11:11:19 -0700426 }
427 break;
scroggocc2feb12015-08-14 08:32:46 -0700428 case kRGB_565_SkColorType:
scroggoc5560be2016-02-03 09:42:42 -0800429 proc = &swizzle_mask16_to_565;
scroggocc2feb12015-08-14 08:32:46 -0700430 break;
msaretteed039b2015-03-18 11:11:19 -0700431 default:
432 break;
msarett74114382015-03-16 11:55:18 -0700433 }
434 break;
435 case 24:
msarett5406d6f2015-08-31 06:55:13 -0700436 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700437 case kRGBA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400438 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700439 proc = &swizzle_mask24_to_rgba_opaque;
msarettebf44082016-02-03 13:12:38 -0800440 } else {
441 switch (dstInfo.alphaType()) {
442 case kUnpremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700443 proc = &swizzle_mask24_to_rgba_unpremul;
msarettebf44082016-02-03 13:12:38 -0800444 break;
445 case kPremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700446 proc = &swizzle_mask24_to_rgba_premul;
447 break;
448 default:
449 break;
450 }
451 }
452 break;
453 case kBGRA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400454 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700455 proc = &swizzle_mask24_to_bgra_opaque;
456 } else {
457 switch (dstInfo.alphaType()) {
458 case kUnpremul_SkAlphaType:
459 proc = &swizzle_mask24_to_bgra_unpremul;
460 break;
461 case kPremul_SkAlphaType:
462 proc = &swizzle_mask24_to_bgra_premul;
msarettebf44082016-02-03 13:12:38 -0800463 break;
464 default:
465 break;
466 }
msaretteed039b2015-03-18 11:11:19 -0700467 }
468 break;
scroggocc2feb12015-08-14 08:32:46 -0700469 case kRGB_565_SkColorType:
scroggoc5560be2016-02-03 09:42:42 -0800470 proc = &swizzle_mask24_to_565;
scroggocc2feb12015-08-14 08:32:46 -0700471 break;
msaretteed039b2015-03-18 11:11:19 -0700472 default:
473 break;
msarett74114382015-03-16 11:55:18 -0700474 }
475 break;
476 case 32:
msarett5406d6f2015-08-31 06:55:13 -0700477 switch (dstInfo.colorType()) {
msarett34e0ec42016-04-22 16:27:24 -0700478 case kRGBA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400479 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700480 proc = &swizzle_mask32_to_rgba_opaque;
msarettebf44082016-02-03 13:12:38 -0800481 } else {
482 switch (dstInfo.alphaType()) {
483 case kUnpremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700484 proc = &swizzle_mask32_to_rgba_unpremul;
msarettebf44082016-02-03 13:12:38 -0800485 break;
486 case kPremul_SkAlphaType:
msarett34e0ec42016-04-22 16:27:24 -0700487 proc = &swizzle_mask32_to_rgba_premul;
488 break;
489 default:
490 break;
491 }
492 }
493 break;
494 case kBGRA_8888_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400495 if (srcIsOpaque) {
msarett34e0ec42016-04-22 16:27:24 -0700496 proc = &swizzle_mask32_to_bgra_opaque;
497 } else {
498 switch (dstInfo.alphaType()) {
499 case kUnpremul_SkAlphaType:
500 proc = &swizzle_mask32_to_bgra_unpremul;
501 break;
502 case kPremul_SkAlphaType:
503 proc = &swizzle_mask32_to_bgra_premul;
msarettebf44082016-02-03 13:12:38 -0800504 break;
505 default:
506 break;
507 }
msaretteed039b2015-03-18 11:11:19 -0700508 }
509 break;
scroggocc2feb12015-08-14 08:32:46 -0700510 case kRGB_565_SkColorType:
scroggoc5560be2016-02-03 09:42:42 -0800511 proc = &swizzle_mask32_to_565;
scroggocc2feb12015-08-14 08:32:46 -0700512 break;
msaretteed039b2015-03-18 11:11:19 -0700513 default:
514 break;
msarett74114382015-03-16 11:55:18 -0700515 }
516 break;
517 default:
518 SkASSERT(false);
halcanary96fcdcc2015-08-27 07:41:13 -0700519 return nullptr;
msarett74114382015-03-16 11:55:18 -0700520 }
msarett5406d6f2015-08-31 06:55:13 -0700521
msarettfdb47572015-10-13 12:50:14 -0700522 int srcOffset = 0;
523 int srcWidth = dstInfo.width();
524 if (options.fSubset) {
525 srcOffset = options.fSubset->left();
526 srcWidth = options.fSubset->width();
527 }
528
529 return new SkMaskSwizzler(masks, proc, srcOffset, srcWidth);
msarett74114382015-03-16 11:55:18 -0700530}
531
532/*
533 *
534 * Constructor for mask swizzler
535 *
536 */
msarett80803ff2015-10-16 10:54:12 -0700537SkMaskSwizzler::SkMaskSwizzler(SkMasks* masks, RowProc proc, int srcOffset, int subsetWidth)
scroggoe7fc14b2015-10-02 13:14:46 -0700538 : fMasks(masks)
msarett74114382015-03-16 11:55:18 -0700539 , fRowProc(proc)
msarett80803ff2015-10-16 10:54:12 -0700540 , fSubsetWidth(subsetWidth)
541 , fDstWidth(subsetWidth)
scroggoe7fc14b2015-10-02 13:14:46 -0700542 , fSampleX(1)
msarettfdb47572015-10-13 12:50:14 -0700543 , fSrcOffset(srcOffset)
544 , fX0(srcOffset)
msarett74114382015-03-16 11:55:18 -0700545{}
546
scroggoe7fc14b2015-10-02 13:14:46 -0700547int SkMaskSwizzler::onSetSampleX(int sampleX) {
548 // FIXME: Share this function with SkSwizzler?
549 SkASSERT(sampleX > 0); // Surely there is an upper limit? Should there be
550 // way to report failure?
551 fSampleX = sampleX;
msarettfdb47572015-10-13 12:50:14 -0700552 fX0 = get_start_coord(sampleX) + fSrcOffset;
msarett80803ff2015-10-16 10:54:12 -0700553 fDstWidth = get_scaled_dimension(fSubsetWidth, sampleX);
scroggoe7fc14b2015-10-02 13:14:46 -0700554
msarett80803ff2015-10-16 10:54:12 -0700555 // check that fX0 is valid
556 SkASSERT(fX0 >= 0);
scroggoe7fc14b2015-10-02 13:14:46 -0700557 return fDstWidth;
558}
559
msarett74114382015-03-16 11:55:18 -0700560/*
561 *
msaretteed039b2015-03-18 11:11:19 -0700562 * Swizzle the specified row
msarett74114382015-03-16 11:55:18 -0700563 *
564 */
msaretta4970dc2016-01-11 07:23:23 -0800565void SkMaskSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) {
halcanary96fcdcc2015-08-27 07:41:13 -0700566 SkASSERT(nullptr != dst && nullptr != src);
msaretta4970dc2016-01-11 07:23:23 -0800567 fRowProc(dst, src, fDstWidth, fMasks, fX0, fSampleX);
msarett74114382015-03-16 11:55:18 -0700568}