Debian Codegraph @

src/core/SkBlitter_ARGB32.cpp

raw
1 /*
2 * Copyright 2006 The Android Open Source Project
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 "include/core/SkColor.h"
9 #include "include/core/SkColorType.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPixmap.h"
12 #include "include/core/SkRect.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkAlign.h"
15 #include "include/private/base/SkCPUTypes.h"
16 #include "include/private/base/SkDebug.h"
17 #include "include/private/base/SkMalloc.h"
18 #include "include/private/base/SkTo.h"
19 #include "src/base/SkUtils.h"
20 #include "src/base/SkVx.h"
21 #include "src/core/SkBlitMask.h"
22 #include "src/core/SkBlitRow.h"
23 #include "src/core/SkBlitter.h"
24 #include "src/core/SkColorData.h"
25 #include "src/core/SkColorPriv.h"
26 #include "src/core/SkCoreBlitters.h"
27 #include "src/core/SkMask.h"
28 #include "src/core/SkMemset.h"
29 #include "src/shaders/SkShaderBase.h"
30
31 #include <algorithm>
32 #include <cstddef>
33 #include <cstdint>
34 #include <optional>
35
36 static inline int upscale_31_to_32(int value) {
37 SkASSERT((unsigned)value <= 31);
38 return value + (value >> 4);
39 }
40
41 static inline int blend_32(int src, int dst, int scale) {
42 SkASSERT((unsigned)src <= 0xFF);
43 SkASSERT((unsigned)dst <= 0xFF);
44 SkASSERT((unsigned)scale <= 32);
45 return dst + ((src - dst) * scale >> 5);
46 }
47
48 static inline SkPMColor blend_lcd16(int srcA, int srcR, int srcG, int srcB,
49 SkPMColor dst, uint16_t mask) {
50 if (mask == 0) {
51 return dst;
52 }
53
54 /* We want all of these in 5bits, hence the shifts in case one of them
55 * (green) is 6bits.
56 */
57 int maskR = SkGetPackedR16(mask) >> (SK_R16_BITS - 5);
58 int maskG = SkGetPackedG16(mask) >> (SK_G16_BITS - 5);
59 int maskB = SkGetPackedB16(mask) >> (SK_B16_BITS - 5);
60
61 // Now upscale them to 0..32, so we can use blend32
62 maskR = upscale_31_to_32(maskR);
63 maskG = upscale_31_to_32(maskG);
64 maskB = upscale_31_to_32(maskB);
65
66 // srcA has been upscaled to 256 before passed into this function
67 maskR = maskR * srcA >> 8;
68 maskG = maskG * srcA >> 8;
69 maskB = maskB * srcA >> 8;
70
71 int dstA = SkGetPackedA32(dst);
72 int dstR = SkGetPackedR32(dst);
73 int dstG = SkGetPackedG32(dst);
74 int dstB = SkGetPackedB32(dst);
75
76 // Subtract 1 from srcA to bring it back to [0-255] to compare against dstA, alpha needs to
77 // use either the min or the max of the LCD coverages. See https:/skbug.com/40037823
78 int maskA = (srcA-1) < dstA ? std::min(maskR, std::min(maskG, maskB))
79 : std::max(maskR, std::max(maskG, maskB));
80
81 return SkPackARGB32(blend_32(0xFF, dstA, maskA),
82 blend_32(srcR, dstR, maskR),
83 blend_32(srcG, dstG, maskG),
84 blend_32(srcB, dstB, maskB));
85 }
86
87 static inline SkPMColor blend_lcd16_opaque(int srcR, int srcG, int srcB,
88 SkPMColor dst, uint16_t mask,
89 SkPMColor opaqueDst) {
90 if (mask == 0) {
91 return dst;
92 }
93
94 if (0xFFFF == mask) {
95 return opaqueDst;
96 }
97
98 /* We want all of these in 5bits, hence the shifts in case one of them
99 * (green) is 6bits.
100 */
101 int maskR = SkGetPackedR16(mask) >> (SK_R16_BITS - 5);
102 int maskG = SkGetPackedG16(mask) >> (SK_G16_BITS - 5);
103 int maskB = SkGetPackedB16(mask) >> (SK_B16_BITS - 5);
104
105 // Now upscale them to 0..32, so we can use blend32
106 maskR = upscale_31_to_32(maskR);
107 maskG = upscale_31_to_32(maskG);
108 maskB = upscale_31_to_32(maskB);
109
110 int dstA = SkGetPackedA32(dst);
111 int dstR = SkGetPackedR32(dst);
112 int dstG = SkGetPackedG32(dst);
113 int dstB = SkGetPackedB32(dst);
114
115 // Opaque src alpha always uses the max of the LCD coverages.
116 int maskA = std::max(maskR, std::max(maskG, maskB));
117
118 // LCD blitting is only supported if the dst is known/required
119 // to be opaque
120 return SkPackARGB32(blend_32(0xFF, dstA, maskA),
121 blend_32(srcR, dstR, maskR),
122 blend_32(srcG, dstG, maskG),
123 blend_32(srcB, dstB, maskB));
124 }
125
126
127 // TODO: rewrite at least the SSE code here. It's miserable.
128
129 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
130 #include <emmintrin.h>
131
132 // The following (left) shifts cause the top 5 bits of the mask components to
133 // line up with the corresponding components in an SkPMColor.
134 // Note that the mask's RGB16 order may differ from the SkPMColor order.
135 #define SK_R16x5_R32x5_SHIFT (SK_R32_SHIFT - SK_R16_SHIFT - SK_R16_BITS + 5)
136 #define SK_G16x5_G32x5_SHIFT (SK_G32_SHIFT - SK_G16_SHIFT - SK_G16_BITS + 5)
137 #define SK_B16x5_B32x5_SHIFT (SK_B32_SHIFT - SK_B16_SHIFT - SK_B16_BITS + 5)
138
139 #if SK_R16x5_R32x5_SHIFT == 0
140 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (x)
141 #elif SK_R16x5_R32x5_SHIFT > 0
142 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (_mm_slli_epi32(x, SK_R16x5_R32x5_SHIFT))
143 #else
144 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (_mm_srli_epi32(x, -SK_R16x5_R32x5_SHIFT))
145 #endif
146
147 #if SK_G16x5_G32x5_SHIFT == 0
148 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (x)
149 #elif SK_G16x5_G32x5_SHIFT > 0
150 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (_mm_slli_epi32(x, SK_G16x5_G32x5_SHIFT))
151 #else
152 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (_mm_srli_epi32(x, -SK_G16x5_G32x5_SHIFT))
153 #endif
154
155 #if SK_B16x5_B32x5_SHIFT == 0
156 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (x)
157 #elif SK_B16x5_B32x5_SHIFT > 0
158 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (_mm_slli_epi32(x, SK_B16x5_B32x5_SHIFT))
159 #else
160 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (_mm_srli_epi32(x, -SK_B16x5_B32x5_SHIFT))
161 #endif
162
163 static __m128i blend_lcd16_sse2(__m128i &src, __m128i &dst, __m128i &mask, __m128i &srcA) {
164 // In the following comments, the components of src, dst and mask are
165 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
166 // by an R, G, B, or A suffix. Components of one of the four pixels that
167 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
168 // example is the blue channel of the second destination pixel. Memory
169 // layout is shown for an ARGB byte order in a color value.
170
171 // src and srcA store 8-bit values interleaved with zeros.
172 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
173 // srcA = (srcA, 0, srcA, 0, srcA, 0, srcA, 0,
174 // srcA, 0, srcA, 0, srcA, 0, srcA, 0)
175 // mask stores 16-bit values (compressed three channels) interleaved with zeros.
176 // Lo and Hi denote the low and high bytes of a 16-bit value, respectively.
177 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
178 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
179
180 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
181 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
182 __m128i r = _mm_and_si128(SkPackedR16x5ToUnmaskedR32x5_SSE2(mask),
183 _mm_set1_epi32(0x1F << SK_R32_SHIFT));
184
185 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
186 __m128i g = _mm_and_si128(SkPackedG16x5ToUnmaskedG32x5_SSE2(mask),
187 _mm_set1_epi32(0x1F << SK_G32_SHIFT));
188
189 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
190 __m128i b = _mm_and_si128(SkPackedB16x5ToUnmaskedB32x5_SSE2(mask),
191 _mm_set1_epi32(0x1F << SK_B32_SHIFT));
192
193 // a needs to be either the min or the max of the LCD coverages, depending on srcA < dstA
194 __m128i aMin = _mm_min_epu8(_mm_slli_epi32(r, SK_A32_SHIFT - SK_R32_SHIFT),
195 _mm_min_epu8(_mm_slli_epi32(g, SK_A32_SHIFT - SK_G32_SHIFT),
196 _mm_slli_epi32(b, SK_A32_SHIFT - SK_B32_SHIFT)));
197 __m128i aMax = _mm_max_epu8(_mm_slli_epi32(r, SK_A32_SHIFT - SK_R32_SHIFT),
198 _mm_max_epu8(_mm_slli_epi32(g, SK_A32_SHIFT - SK_G32_SHIFT),
199 _mm_slli_epi32(b, SK_A32_SHIFT - SK_B32_SHIFT)));
200 // srcA has been biased to [0-256], so compare srcA against (dstA+1)
201 __m128i a = _mm_cmplt_epi32(srcA,
202 _mm_and_si128(
203 _mm_add_epi32(dst, _mm_set1_epi32(1 << SK_A32_SHIFT)),
204 _mm_set1_epi32(SK_A32_MASK)));
205 // a = if_then_else(a, aMin, aMax) == (aMin & a) | (aMax & ~a)
206 a = _mm_or_si128(_mm_and_si128(a, aMin), _mm_andnot_si128(a, aMax));
207
208 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
209 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
210 // 8-bit position
211 // mask = (m0A, m0R, m0G, m0B, m1A, m1R, m1G, m1B,
212 // m2A, m2R, m2G, m2B, m3A, m3R, m3G, m3B)
213 mask = _mm_or_si128(_mm_or_si128(a, r), _mm_or_si128(g, b));
214
215 // Interleave R,G,B into the lower byte of word.
216 // i.e. split the sixteen 8-bit values from mask into two sets of eight
217 // 16-bit values, padded by zero.
218 __m128i maskLo, maskHi;
219 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0)
220 maskLo = _mm_unpacklo_epi8(mask, _mm_setzero_si128());
221 // maskHi = (m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
222 maskHi = _mm_unpackhi_epi8(mask, _mm_setzero_si128());
223
224 // Upscale from 0..31 to 0..32
225 // (allows to replace division by left-shift further down)
226 // Left-shift each component by 4 and add the result back to that component,
227 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
228 maskLo = _mm_add_epi16(maskLo, _mm_srli_epi16(maskLo, 4));
229 maskHi = _mm_add_epi16(maskHi, _mm_srli_epi16(maskHi, 4));
230
231 // Multiply each component of maskLo and maskHi by srcA
232 maskLo = _mm_mullo_epi16(maskLo, srcA);
233 maskHi = _mm_mullo_epi16(maskHi, srcA);
234
235 // Left shift mask components by 8 (divide by 256)
236 maskLo = _mm_srli_epi16(maskLo, 8);
237 maskHi = _mm_srli_epi16(maskHi, 8);
238
239 // Interleave R,G,B into the lower byte of the word
240 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0)
241 __m128i dstLo = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
242 // dstLo = (d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
243 __m128i dstHi = _mm_unpackhi_epi8(dst, _mm_setzero_si128());
244
245 // mask = (src - dst) * mask
246 maskLo = _mm_mullo_epi16(maskLo, _mm_sub_epi16(src, dstLo));
247 maskHi = _mm_mullo_epi16(maskHi, _mm_sub_epi16(src, dstHi));
248
249 // mask = (src - dst) * mask >> 5
250 maskLo = _mm_srai_epi16(maskLo, 5);
251 maskHi = _mm_srai_epi16(maskHi, 5);
252
253 // Add two pixels into result.
254 // result = dst + ((src - dst) * mask >> 5)
255 __m128i resultLo = _mm_add_epi16(dstLo, maskLo);
256 __m128i resultHi = _mm_add_epi16(dstHi, maskHi);
257
258 // Pack into 4 32bit dst pixels.
259 // resultLo and resultHi contain eight 16-bit components (two pixels) each.
260 // Merge into one SSE regsiter with sixteen 8-bit values (four pixels),
261 // clamping to 255 if necessary.
262 return _mm_packus_epi16(resultLo, resultHi);
263 }
264
265 static __m128i blend_lcd16_opaque_sse2(__m128i &src, __m128i &dst, __m128i &mask) {
266 // In the following comments, the components of src, dst and mask are
267 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
268 // by an R, G, B, or A suffix. Components of one of the four pixels that
269 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
270 // example is the blue channel of the second destination pixel. Memory
271 // layout is shown for an ARGB byte order in a color value.
272
273 // src and srcA store 8-bit values interleaved with zeros.
274 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
275 // mask stores 16-bit values (shown as high and low bytes) interleaved with
276 // zeros
277 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
278 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
279
280 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
281 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
282 __m128i r = _mm_and_si128(SkPackedR16x5ToUnmaskedR32x5_SSE2(mask),
283 _mm_set1_epi32(0x1F << SK_R32_SHIFT));
284
285 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
286 __m128i g = _mm_and_si128(SkPackedG16x5ToUnmaskedG32x5_SSE2(mask),
287 _mm_set1_epi32(0x1F << SK_G32_SHIFT));
288
289 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
290 __m128i b = _mm_and_si128(SkPackedB16x5ToUnmaskedB32x5_SSE2(mask),
291 _mm_set1_epi32(0x1F << SK_B32_SHIFT));
292
293 // a = max(r, g, b) since opaque src alpha uses max of LCD coverages
294 __m128i a = _mm_max_epu8(_mm_slli_epi32(r, SK_A32_SHIFT - SK_R32_SHIFT),
295 _mm_max_epu8(_mm_slli_epi32(g, SK_A32_SHIFT - SK_G32_SHIFT),
296 _mm_slli_epi32(b, SK_A32_SHIFT - SK_B32_SHIFT)));
297
298 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
299 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
300 // 8-bit position
301 // mask = (m0A, m0R, m0G, m0B, m1A, m1R, m1G, m1B,
302 // m2A, m2R, m2G, m2B, m3A, m3R, m3G, m3B)
303 mask = _mm_or_si128(_mm_or_si128(a, r), _mm_or_si128(g, b));
304
305 // Interleave R,G,B into the lower byte of word.
306 // i.e. split the sixteen 8-bit values from mask into two sets of eight
307 // 16-bit values, padded by zero.
308 __m128i maskLo, maskHi;
309 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0)
310 maskLo = _mm_unpacklo_epi8(mask, _mm_setzero_si128());
311 // maskHi = (m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
312 maskHi = _mm_unpackhi_epi8(mask, _mm_setzero_si128());
313
314 // Upscale from 0..31 to 0..32
315 // (allows to replace division by left-shift further down)
316 // Left-shift each component by 4 and add the result back to that component,
317 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
318 maskLo = _mm_add_epi16(maskLo, _mm_srli_epi16(maskLo, 4));
319 maskHi = _mm_add_epi16(maskHi, _mm_srli_epi16(maskHi, 4));
320
321 // Interleave R,G,B into the lower byte of the word
322 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0)
323 __m128i dstLo = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
324 // dstLo = (d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
325 __m128i dstHi = _mm_unpackhi_epi8(dst, _mm_setzero_si128());
326
327 // mask = (src - dst) * mask
328 maskLo = _mm_mullo_epi16(maskLo, _mm_sub_epi16(src, dstLo));
329 maskHi = _mm_mullo_epi16(maskHi, _mm_sub_epi16(src, dstHi));
330
331 // mask = (src - dst) * mask >> 5
332 maskLo = _mm_srai_epi16(maskLo, 5);
333 maskHi = _mm_srai_epi16(maskHi, 5);
334
335 // Add two pixels into result.
336 // result = dst + ((src - dst) * mask >> 5)
337 __m128i resultLo = _mm_add_epi16(dstLo, maskLo);
338 __m128i resultHi = _mm_add_epi16(dstHi, maskHi);
339
340 // Merge into one SSE regsiter with sixteen 8-bit values (four pixels),
341 // clamping to 255 if necessary.
342 return _mm_packus_epi16(resultLo, resultHi);
343 }
344
345 void blit_row_lcd16(SkPMColor dst[], const uint16_t mask[], SkColor src, int width, SkPMColor) {
346 if (width <= 0) {
347 return;
348 }
349
350 int srcA = SkColorGetA(src);
351 int srcR = SkColorGetR(src);
352 int srcG = SkColorGetG(src);
353 int srcB = SkColorGetB(src);
354
355 srcA = SkAlpha255To256(srcA);
356
357 if (width >= 4) {
358 SkASSERT(SkIsAlign4((uintptr_t) dst));
359 while (!SkIsAlign16((uintptr_t) dst)) {
360 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
361 mask++;
362 dst++;
363 width--;
364 }
365
366 __m128i *d = reinterpret_cast<__m128i*>(dst);
367 // Set alpha to 0xFF and replicate source four times in SSE register.
368 __m128i src_sse = _mm_set1_epi32(SkPackARGB32(0xFF, srcR, srcG, srcB));
369 // Interleave with zeros to get two sets of four 16-bit values.
370 src_sse = _mm_unpacklo_epi8(src_sse, _mm_setzero_si128());
371 // Set srcA_sse to contain eight copies of srcA, padded with zero.
372 // src_sse=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
373 __m128i srcA_sse = _mm_set1_epi16(srcA);
374 while (width >= 4) {
375 // Load four destination pixels into dst_sse.
376 __m128i dst_sse = _mm_load_si128(d);
377 // Load four 16-bit masks into lower half of mask_sse.
378 // mask does *not* actually need to be 16 byte alligned to use this command
379 __m128i mask_sse = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(mask));
380
381 // Check whether masks are equal to 0 and get the highest bit
382 // of each byte of result, if masks are all zero, we will get
383 // pack_cmp to 0xFFFF
384 int pack_cmp = _mm_movemask_epi8(_mm_cmpeq_epi16(mask_sse,
385 _mm_setzero_si128()));
386
387 // if mask pixels are not all zero, we will blend the dst pixels
388 if (pack_cmp != 0xFFFF) {
389 // Unpack 4 16bit mask pixels to
390 // mask_sse = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
391 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
392 mask_sse = _mm_unpacklo_epi16(mask_sse,
393 _mm_setzero_si128());
394
395 // Process 4 32bit dst pixels
396 __m128i result = blend_lcd16_sse2(src_sse, dst_sse, mask_sse, srcA_sse);
397 _mm_store_si128(d, result);
398 }
399
400 d++;
401 mask += 4;
402 width -= 4;
403 }
404
405 dst = reinterpret_cast<SkPMColor*>(d);
406 }
407
408 while (width > 0) {
409 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
410 mask++;
411 dst++;
412 width--;
413 }
414 }
415
416 void blit_row_lcd16_opaque(SkPMColor dst[], const uint16_t mask[],
417 SkColor src, int width, SkPMColor opaqueDst) {
418 if (width <= 0) {
419 return;
420 }
421
422 int srcR = SkColorGetR(src);
423 int srcG = SkColorGetG(src);
424 int srcB = SkColorGetB(src);
425
426 if (width >= 4) {
427 SkASSERT(SkIsAlign4((uintptr_t) dst));
428 while (!SkIsAlign16((uintptr_t) dst)) {
429 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
430 mask++;
431 dst++;
432 width--;
433 }
434
435 __m128i *d = reinterpret_cast<__m128i*>(dst);
436 // Set alpha to 0xFF and replicate source four times in SSE register.
437 __m128i src_sse = _mm_set1_epi32(SkPackARGB32(0xFF, srcR, srcG, srcB));
438 // Set srcA_sse to contain eight copies of srcA, padded with zero.
439 // src_sse=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
440 src_sse = _mm_unpacklo_epi8(src_sse, _mm_setzero_si128());
441 while (width >= 4) {
442 // Load four destination pixels into dst_sse.
443 __m128i dst_sse = _mm_load_si128(d);
444 // Load four 16-bit masks into lower half of mask_sse.
445 // mask does *not* actually need to be 16 byte alligned to use this command
446 __m128i mask_sse = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(mask));
447
448 // Check whether masks are equal to 0 and get the highest bit
449 // of each byte of result, if masks are all zero, we will get
450 // pack_cmp to 0xFFFF
451 int pack_cmp = _mm_movemask_epi8(_mm_cmpeq_epi16(mask_sse,
452 _mm_setzero_si128()));
453
454 // if mask pixels are not all zero, we will blend the dst pixels
455 if (pack_cmp != 0xFFFF) {
456 // Unpack 4 16bit mask pixels to
457 // mask_sse = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
458 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
459 mask_sse = _mm_unpacklo_epi16(mask_sse,
460 _mm_setzero_si128());
461
462 // Process 4 32bit dst pixels
463 __m128i result = blend_lcd16_opaque_sse2(src_sse, dst_sse, mask_sse);
464 _mm_store_si128(d, result);
465 }
466
467 d++;
468 mask += 4;
469 width -= 4;
470 }
471
472 dst = reinterpret_cast<SkPMColor*>(d);
473 }
474
475 while (width > 0) {
476 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
477 mask++;
478 dst++;
479 width--;
480 }
481 }
482
483 #elif defined(SK_ARM_HAS_NEON)
484 #include <arm_neon.h>
485
486 #define NEON_A (SK_A32_SHIFT / 8)
487 #define NEON_R (SK_R32_SHIFT / 8)
488 #define NEON_G (SK_G32_SHIFT / 8)
489 #define NEON_B (SK_B32_SHIFT / 8)
490
491 static inline uint8x8_t blend_32_neon(uint8x8_t src, uint8x8_t dst, uint16x8_t scale) {
492 int16x8_t src_wide, dst_wide;
493
494 src_wide = vreinterpretq_s16_u16(vmovl_u8(src));
495 dst_wide = vreinterpretq_s16_u16(vmovl_u8(dst));
496
497 src_wide = (src_wide - dst_wide) * vreinterpretq_s16_u16(scale);
498
499 dst_wide += vshrq_n_s16(src_wide, 5);
500
501 return vmovn_u16(vreinterpretq_u16_s16(dst_wide));
502 }
503
504 void blit_row_lcd16_opaque(SkPMColor dst[], const uint16_t src[],
505 SkColor color, int width,
506 SkPMColor opaqueDst) {
507 int colR = SkColorGetR(color);
508 int colG = SkColorGetG(color);
509 int colB = SkColorGetB(color);
510
511 uint8x8_t vcolA = vdup_n_u8(0xFF);
512 uint8x8_t vcolR = vdup_n_u8(colR);
513 uint8x8_t vcolG = vdup_n_u8(colG);
514 uint8x8_t vcolB = vdup_n_u8(colB);
515
516 while (width >= 8) {
517 uint8x8x4_t vdst;
518 uint16x8_t vmask;
519 uint16x8_t vmaskR, vmaskG, vmaskB, vmaskA;
520
521 vdst = vld4_u8((uint8_t*)dst);
522 vmask = vld1q_u16(src);
523
524 // Get all the color masks on 5 bits
525 vmaskR = vshrq_n_u16(vmask, SK_R16_SHIFT);
526 vmaskG = vshrq_n_u16(vshlq_n_u16(vmask, SK_R16_BITS),
527 SK_B16_BITS + SK_R16_BITS + 1);
528 vmaskB = vmask & vdupq_n_u16(SK_B16_MASK);
529
530 // Upscale to 0..32
531 vmaskR = vmaskR + vshrq_n_u16(vmaskR, 4);
532 vmaskG = vmaskG + vshrq_n_u16(vmaskG, 4);
533 vmaskB = vmaskB + vshrq_n_u16(vmaskB, 4);
534 // Opaque srcAlpha always uses the max of the 3 LCD coverage values
535 vmaskA = vmaxq_u16(vmaskR, vmaxq_u16(vmaskG, vmaskB));
536
537 vdst.val[NEON_R] = blend_32_neon(vcolR, vdst.val[NEON_R], vmaskR);
538 vdst.val[NEON_G] = blend_32_neon(vcolG, vdst.val[NEON_G], vmaskG);
539 vdst.val[NEON_B] = blend_32_neon(vcolB, vdst.val[NEON_B], vmaskB);
540 vdst.val[NEON_A] = blend_32_neon(vcolA, vdst.val[NEON_A], vmaskA);
541
542 vst4_u8((uint8_t*)dst, vdst);
543
544 dst += 8;
545 src += 8;
546 width -= 8;
547 }
548
549 // Leftovers
550 for (int i = 0; i < width; i++) {
551 dst[i] = blend_lcd16_opaque(colR, colG, colB, dst[i], src[i], opaqueDst);
552 }
553 }
554
555 void blit_row_lcd16(SkPMColor dst[], const uint16_t src[],
556 SkColor color, int width, SkPMColor) {
557 int colA = SkColorGetA(color);
558 int colR = SkColorGetR(color);
559 int colG = SkColorGetG(color);
560 int colB = SkColorGetB(color);
561
562 // srcA in [0-255] to compare vs dstA
563 uint16x8_t vcolACmp = vdupq_n_u16(colA);
564 colA = SkAlpha255To256(colA);
565
566 uint16x8_t vcolA = vdupq_n_u16(colA); // srcA in [0-256] to combine with coverage
567 uint8x8_t vcolR = vdup_n_u8(colR);
568 uint8x8_t vcolG = vdup_n_u8(colG);
569 uint8x8_t vcolB = vdup_n_u8(colB);
570
571 while (width >= 8) {
572 uint8x8x4_t vdst;
573 uint16x8_t vmask;
574 uint16x8_t vmaskR, vmaskG, vmaskB, vmaskA;
575
576 vdst = vld4_u8((uint8_t*)dst);
577 vmask = vld1q_u16(src);
578
579 // Get all the color masks on 5 bits
580 vmaskR = vshrq_n_u16(vmask, SK_R16_SHIFT);
581 vmaskG = vshrq_n_u16(vshlq_n_u16(vmask, SK_R16_BITS),
582 SK_B16_BITS + SK_R16_BITS + 1);
583 vmaskB = vmask & vdupq_n_u16(SK_B16_MASK);
584
585 // Upscale to 0..32
586 vmaskR = vmaskR + vshrq_n_u16(vmaskR, 4);
587 vmaskG = vmaskG + vshrq_n_u16(vmaskG, 4);
588 vmaskB = vmaskB + vshrq_n_u16(vmaskB, 4);
589
590 vmaskR = vshrq_n_u16(vmaskR * vcolA, 8);
591 vmaskG = vshrq_n_u16(vmaskG * vcolA, 8);
592 vmaskB = vshrq_n_u16(vmaskB * vcolA, 8);
593
594 // Select either the min or the max of the RGB mask values, depending on if the src
595 // alpha is less than the dst alpha.
596 vmaskA = vbslq_u16(vcleq_u16(vcolACmp, vmovl_u8(vdst.val[NEON_A])), // srcA < dstA
597 vminq_u16(vmaskR, vminq_u16(vmaskG, vmaskB)), // ? min(r,g,b)
598 vmaxq_u16(vmaskR, vmaxq_u16(vmaskG, vmaskB))); // : max(r,g,b)
599
600 vdst.val[NEON_R] = blend_32_neon(vcolR, vdst.val[NEON_R], vmaskR);
601 vdst.val[NEON_G] = blend_32_neon(vcolG, vdst.val[NEON_G], vmaskG);
602 vdst.val[NEON_B] = blend_32_neon(vcolB, vdst.val[NEON_B], vmaskB);
603 // vmaskA already includes vcolA so blend against 0xFF
604 vdst.val[NEON_A] = blend_32_neon(vdup_n_u8(0xFF), vdst.val[NEON_A], vmaskA);
605 vst4_u8((uint8_t*)dst, vdst);
606
607 dst += 8;
608 src += 8;
609 width -= 8;
610 }
611
612 for (int i = 0; i < width; i++) {
613 dst[i] = blend_lcd16(colA, colR, colG, colB, dst[i], src[i]);
614 }
615 }
616
617 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LASX
618
619 // The following (left) shifts cause the top 5 bits of the mask components to
620 // line up with the corresponding components in an SkPMColor.
621 // Note that the mask's RGB16 order may differ from the SkPMColor order.
622 #define SK_R16x5_R32x5_SHIFT (SK_R32_SHIFT - SK_R16_SHIFT - SK_R16_BITS + 5)
623 #define SK_G16x5_G32x5_SHIFT (SK_G32_SHIFT - SK_G16_SHIFT - SK_G16_BITS + 5)
624 #define SK_B16x5_B32x5_SHIFT (SK_B32_SHIFT - SK_B16_SHIFT - SK_B16_BITS + 5)
625
626 #if SK_R16x5_R32x5_SHIFT == 0
627 #define SkPackedR16x5ToUnmaskedR32x5_LASX(x) (x)
628 #elif SK_R16x5_R32x5_SHIFT > 0
629 #define SkPackedR16x5ToUnmaskedR32x5_LASX(x) (__lasx_xvslli_w(x, SK_R16x5_R32x5_SHIFT))
630 #else
631 #define SkPackedR16x5ToUnmaskedR32x5_LASX(x) (__lasx_xvsrli_w(x, -SK_R16x5_R32x5_SHIFT))
632 #endif
633
634 #if SK_G16x5_G32x5_SHIFT == 0
635 #define SkPackedG16x5ToUnmaskedG32x5_LASX(x) (x)
636 #elif SK_G16x5_G32x5_SHIFT > 0
637 #define SkPackedG16x5ToUnmaskedG32x5_LASX(x) (__lasx_xvslli_w(x, SK_G16x5_G32x5_SHIFT))
638 #else
639 #define SkPackedG16x5ToUnmaskedG32x5_LASX(x) (__lasx_xvsrli_w(x, -SK_G16x5_G32x5_SHIFT))
640 #endif
641
642 #if SK_B16x5_B32x5_SHIFT == 0
643 #define SkPackedB16x5ToUnmaskedB32x5_LASX(x) (x)
644 #elif SK_B16x5_B32x5_SHIFT > 0
645 #define SkPackedB16x5ToUnmaskedB32x5_LASX(x) (__lasx_xvslli_w(x, SK_B16x5_B32x5_SHIFT))
646 #else
647 #define SkPackedB16x5ToUnmaskedB32x5_LASX(x) (__lasx_xvsrli_w(x, -SK_B16x5_B32x5_SHIFT))
648 #endif
649
650 static __m256i blend_lcd16_lasx(__m256i &src, __m256i &dst, __m256i &mask, __m256i &srcA) {
651 // In the following comments, the components of src, dst and mask are
652 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
653 // by an R, G, B, or A suffix. Components of one of the four pixels that
654 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
655 // example is the blue channel of the second destination pixel. Memory
656 // layout is shown for an ARGB byte order in a color value.
657
658 // src and srcA store 8-bit values interleaved with zeros.
659 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0,
660 // 0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
661 // srcA = (srcA, 0, srcA, 0, srcA, 0, srcA, 0,
662 // srcA, 0, srcA, 0, srcA, 0, srcA, 0,
663 // srcA, 0, srcA, 0, srcA, 0, srcA, 0,
664 // srcA, 0, srcA, 0, srcA, 0, srcA, 0)
665 // mask stores 16-bit values (compressed three channels) interleaved with zeros.
666 // Lo and Hi denote the low and high bytes of a 16-bit value, respectively.
667 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
668 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0,
669 // m4RGBLo, m4RGBHi, 0, 0, m5RGBLo, m5RGBHi, 0, 0,
670 // m6RGBLo, m6RGBHi, 0, 0, m7RGBLo, m7RGBHi, 0, 0)
671
672 __m256i xv_zero = __lasx_xvldi(0);
673
674 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
675 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0,
676 // 0, m4R, 0, 0, 0, m5R, 0, 0, 0, m6R, 0, 0, 0, m7R, 0, 0)
677 __m256i r = __lasx_xvand_v(SkPackedR16x5ToUnmaskedR32x5_LASX(mask),
678 __lasx_xvreplgr2vr_w(0x1F << SK_R32_SHIFT));
679
680 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
681 // 0, 0, m4G, 0, 0, 0, m5G, 0, 0, 0, m6G, 0, 0, 0, m7R, 0)
682 __m256i g = __lasx_xvand_v(SkPackedG16x5ToUnmaskedG32x5_LASX(mask),
683 __lasx_xvreplgr2vr_w(0x1F << SK_G32_SHIFT));
684
685 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
686 // 0, 0, 0, m4B, 0, 0, 0, m5B, 0, 0, 0, m6B, 0, 0, 0, m7B)
687 __m256i b = __lasx_xvand_v(SkPackedB16x5ToUnmaskedB32x5_LASX(mask),
688 __lasx_xvreplgr2vr_w(0x1F << SK_B32_SHIFT));
689
690 // a needs to be either the min or the max of the LCD coverages, depending on srcA < dstA
691 __m256i aMin = __lasx_xvmin_b(__lasx_xvslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
692 __lasx_xvmin_b(__lasx_xvslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
693 __lasx_xvslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
694 __m256i aMax = __lasx_xvmax_b(__lasx_xvslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
695 __lasx_xvmax_b(__lasx_xvslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
696 __lasx_xvslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
697 // srcA has been biased to [0-256], so compare srcA against (dstA+1)
698 __m256i a = __lasx_xvmskltz_w(srcA -
699 __lasx_xvand_v(
700 __lasx_xvadd_w(dst,
701 __lasx_xvreplgr2vr_w(1 << SK_A32_SHIFT)),
702 __lasx_xvreplgr2vr_w(SK_A32_MASK)));
703 // a = if_then_else(a, aMin, aMax) == (aMin & a) | (aMax & ~a)
704 a = __lasx_xvor_v(__lasx_xvand_v(a, aMin), __lasx_xvandn_v(a, aMax));
705
706 // Pack the 8 16bit mask pixels into 8 32bit pixels, (p0, p1, p2, p3)
707 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
708 // 8-bit position
709 // mask = (m0A, m0R, m0G, m0B, m1R, m1R, m1G, m1B,
710 // m2A, m2R, m2G, m2B, m3R, m3R, m3G, m3B,
711 // m4A, m4R, m4G, m4B, m5R, m5R, m5G, m5B,
712 // m6A, m6R, m6G, m6B, m7R, m7R, m7G, m7B)
713 mask = __lasx_xvor_v(__lasx_xvor_v(a, r), __lasx_xvor_v(g, b));
714
715 // Interleave R,G,B into the lower byte of word.
716 // i.e. split the sixteen 8-bit values from mask into two sets of sixteen
717 // 16-bit values, padded by zero.
718 __m256i maskLo, maskHi;
719 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0,
720 // m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
721 maskLo = __lasx_xvilvl_b(xv_zero, mask);
722 // maskHi = (m4A, 0, m4R, 0, m4G, 0, m4B, 0, m5A, 0, m5R, 0, m5G, 0, m5B, 0,
723 // m6A, 0, m6R, 0, m6G, 0, m6B, 0, m7A, 0, m7R, 0, m7G, 0, m7B, 0)
724 maskHi = __lasx_xvilvh_b(xv_zero, mask);
725
726 // Upscale from 0..31 to 0..32
727 // (allows to replace division by left-shift further down)
728 // Left-shift each component by 4 and add the result back to that component,
729 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
730 maskLo = __lasx_xvadd_h(maskLo, __lasx_xvsrli_h(maskLo, 4));
731 maskHi = __lasx_xvadd_h(maskHi, __lasx_xvsrli_h(maskHi, 4));
732
733 // Multiply each component of maskLo and maskHi by srcA
734 maskLo = __lasx_xvmul_h(maskLo, srcA);
735 maskHi = __lasx_xvmul_h(maskHi, srcA);
736
737 // Left shift mask components by 8 (divide by 256)
738 maskLo = __lasx_xvsrli_h(maskLo, 8);
739 maskHi = __lasx_xvsrli_h(maskHi, 8);
740
741 // Interleave R,G,B into the lower byte of the word
742 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0)
743 // d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
744 __m256i dstLo = __lasx_xvilvl_b(xv_zero, dst);
745 // dstLo = (d4A, 0, d4R, 0, d4G, 0, d4B, 0, d5A, 0, d5R, 0, d5G, 0, d5B, 0)
746 // d6A, 0, d6R, 0, d6G, 0, d6B, 0, d7A, 0, d7R, 0, d7G, 0, d7B, 0)
747 __m256i dstHi = __lasx_xvilvh_b(xv_zero, dst);
748
749 // mask = (src - dst) * mask
750 maskLo = __lasx_xvmul_h(maskLo, __lasx_xvsub_h(src, dstLo));
751 maskHi = __lasx_xvmul_h(maskHi, __lasx_xvsub_h(src, dstHi));
752
753 // mask = (src - dst) * mask >> 5
754 maskLo = __lasx_xvsrai_h(maskLo, 5);
755 maskHi = __lasx_xvsrai_h(maskHi, 5);
756
757 // Add two pixels into result.
758 // result = dst + ((src - dst) * mask >> 5)
759 __m256i resultLo = __lasx_xvadd_h(dstLo, maskLo);
760 __m256i resultHi = __lasx_xvadd_h(dstHi, maskHi);
761
762 // Pack into 8 32bit dst pixels.
763 // resultLo and resultHi contain sixteen 16-bit components (four pixels) each.
764 // Merge into one LASX regsiter with 32 8-bit values (eight pixels),
765 // clamping to 255 if necessary.
766 __m256i tmpl = __lasx_xvsat_hu(resultLo, 7);
767 __m256i tmph = __lasx_xvsat_hu(resultHi, 7);
768 return __lasx_xvpickev_b(tmph, tmpl);
769 }
770
771 static __m256i blend_lcd16_opaque_lasx(__m256i &src, __m256i &dst, __m256i &mask) {
772 // In the following comments, the components of src, dst and mask are
773 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
774 // by an R, G, B, or A suffix. Components of one of the four pixels that
775 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
776 // example is the blue channel of the second destination pixel. Memory
777 // layout is shown for an ARGB byte order in a color value.
778
779 // src and srcA store 8-bit values interleaved with zeros.
780 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0,
781 // 0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
782 // mask stores 16-bit values (shown as high and low bytes) interleaved with
783 // zeros
784 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
785 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0,
786 // m4RGBLo, m4RGBHi, 0, 0, m5RGBLo, m5RGBHi, 0, 0,
787 // m6RGBLo, m6RGBHi, 0, 0, m7RGBLo, m7RGBHi, 0, 0)
788
789 __m256i xv_zero = __lasx_xvldi(0);
790
791 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
792 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0,
793 // 0, m4R, 0, 0, 0, m5R, 0, 0, 0, m6R, 0, 0, 0, m7R, 0, 0)
794 __m256i r = __lasx_xvand_v(SkPackedR16x5ToUnmaskedR32x5_LASX(mask),
795 __lasx_xvreplgr2vr_w(0x1F << SK_R32_SHIFT));
796
797 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0,
798 // 0, 0, m4G, 0, 0, 0, m5G, 0, 0, 0, m6G, 0, 0, 0, m7G, 0)
799 __m256i g = __lasx_xvand_v(SkPackedG16x5ToUnmaskedG32x5_LASX(mask),
800 __lasx_xvreplgr2vr_w(0x1F << SK_G32_SHIFT));
801
802 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B,
803 // 0, 0, 0, m4B, 0, 0, 0, m5B, 0, 0, 0, m6B, 0, 0, 0, m7B)
804 __m256i b = __lasx_xvand_v(SkPackedB16x5ToUnmaskedB32x5_LASX(mask),
805 __lasx_xvreplgr2vr_w(0x1F << SK_B32_SHIFT));
806
807 // a = max(r, g, b) since opaque src alpha uses max of LCD coverages
808 __m256i a = __lasx_xvmax_b(__lasx_xvslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
809 __lasx_xvmax_b(__lasx_xvslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
810 __lasx_xvslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
811
812 // Pack the 8 16bit mask pixels into 8 32bit pixels, (p0, p1, p2, p3,
813 // p4, p5, p6, p7)
814 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
815 // 8-bit position
816 // mask = (m0A, m0R, m0G, m0B, m1A, m1R, m1G, m1B,
817 // m2A, m2R, m2G, m2B, m3A, m3R, m3G, m3B,
818 // m4A, m4R, m4G, m4B, m5A, m5R, m5G, m5B,
819 // m6A, m6R, m6G, m6B, m7A, m7R, m7G, m7B)
820 mask = __lasx_xvor_v(__lasx_xvor_v(a, r), __lasx_xvor_v(g, b));
821
822 // Interleave R,G,B into the lower byte of word.
823 // i.e. split the 32 8-bit values from mask into two sets of sixteen
824 // 16-bit values, padded by zero.
825 __m256i maskLo, maskHi;
826 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0,
827 // m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
828 maskLo = __lasx_xvilvl_b(xv_zero, mask);
829 // maskHi = (m4A, 0, m4R, 0, m4G, 0, m4B, 0, m5A, 0, m5R, 0, m5G, 0, m5B, 0,
830 // m6A, 0, m6R, 0, m6G, 0, m6B, 0, m7A, 0, m7R, 0, m7G, 0, m7B, 0)
831 maskHi = __lasx_xvilvh_b(xv_zero, mask);
832
833 // Upscale from 0..31 to 0..32
834 // (allows to replace division by left-shift further down)
835 // Left-shift each component by 4 and add the result back to that component,
836 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
837 maskLo = __lasx_xvadd_h(maskLo, __lasx_xvsrli_h(maskLo, 4));
838 maskHi = __lasx_xvadd_h(maskHi, __lasx_xvsrli_h(maskHi, 4));
839
840 // Interleave R,G,B into the lower byte of the word
841 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0,
842 // d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
843 __m256i dstLo = __lasx_xvilvl_b(xv_zero, dst);
844 // dstLo = (d4A, 0, d4R, 0, d4G, 0, d4B, 0, d5A, 0, d5R, 0, d5G, 0, d5B, 0,
845 // dstLo = (d6A, 0, d6R, 0, d6G, 0, d6B, 0, d7A, 0, d7R, 0, d7G, 0, d7B, 0)
846 __m256i dstHi = __lasx_xvilvh_b(xv_zero, dst);
847
848 // mask = (src - dst) * mask
849 maskLo = __lasx_xvmul_h(maskLo, __lasx_xvsub_h(src, dstLo));
850 maskHi = __lasx_xvmul_h(maskHi, __lasx_xvsub_h(src, dstHi));
851
852 // mask = (src - dst) * mask >> 5
853 maskLo = __lasx_xvsrai_h(maskLo, 5);
854 maskHi = __lasx_xvsrai_h(maskHi, 5);
855
856 // Add two pixels into result.
857 // result = dst + ((src - dst) * mask >> 5)
858 __m256i resultLo = __lasx_xvadd_h(dstLo, maskLo);
859 __m256i resultHi = __lasx_xvadd_h(dstHi, maskHi);
860
861 // Merge into one SSE regsiter with 32 8-bit values (eight pixels),
862 // clamping to 255 if necessary.
863 __m256i tmpl = __lasx_xvsat_hu(resultLo, 7);
864 __m256i tmph = __lasx_xvsat_hu(resultHi, 7);
865
866 return __lasx_xvpickev_b(tmph, tmpl);
867 }
868
869 void blit_row_lcd16(SkPMColor dst[], const uint16_t mask[], SkColor src, int width, SkPMColor) {
870 if (width <= 0) {
871 return;
872 }
873
874 int srcA = SkColorGetA(src);
875 int srcR = SkColorGetR(src);
876 int srcG = SkColorGetG(src);
877 int srcB = SkColorGetB(src);
878 __m256i xv_zero = __lasx_xvldi(0);
879
880 srcA = SkAlpha255To256(srcA);
881 if (width >= 8) {
882 SkASSERT(SkIsAlign4((uintptr_t) dst));
883 while (!SkIsAlign16((uintptr_t) dst)) {
884 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
885 mask++;
886 dst++;
887 width--;
888 }
889
890 __m256i *d = reinterpret_cast<__m256i*>(dst);
891 // Set alpha to 0xFF and replicate source eight times in LASX register.
892 unsigned int skpackargb32 = SkPackARGB32(0xFF, srcR, srcG, srcB);
893 __m256i src_lasx = __lasx_xvreplgr2vr_w(skpackargb32);
894 // Interleave with zeros to get two sets of eight 16-bit values.
895 src_lasx = __lasx_xvilvl_b(xv_zero, src_lasx);
896 // Set srcA_lasx to contain sixteen copies of srcA, padded with zero.
897 // src_lasx=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0,
898 // 0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
899 __m256i srcA_lasx = __lasx_xvreplgr2vr_h(srcA);
900
901 while (width >= 8) {
902 // Load eight destination pixels into dst_lasx.
903 __m256i dst_lasx = __lasx_xvld(d, 0);
904 // Load eight 16-bit masks into lower half of mask_lasx.
905 __m256i mask_lasx = __lasx_xvld(mask, 0);
906 mask_lasx = (__m256i){mask_lasx[0], 0, mask_lasx[1], 0};
907
908 int pack_cmp = __lasx_xbz_v(mask_lasx);
909 // if mask pixels are not all zero, we will blend the dst pixels
910 if (pack_cmp != 1) {
911 // Unpack 8 16bit mask pixels to
912 // mask_lasx = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
913 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0,
914 // m4RGBLo, m4RGBHi, 0, 0, m5RGBLo, m5RGBHi, 0, 0,
915 // m6RGBLo, m6RGBHi, 0, 0, m7RGBLo, m7RGBHi, 0, 0)
916 mask_lasx = __lasx_xvilvl_h(xv_zero, mask_lasx);
917
918 // Process 8 32bit dst pixels
919 __m256i result = blend_lcd16_lasx(src_lasx, dst_lasx, mask_lasx, srcA_lasx);
920 __lasx_xvst(result, d, 0);
921 }
922 d++;
923 mask += 8;
924 width -= 8;
925 }
926 dst = reinterpret_cast<SkPMColor*>(d);
927 }
928
929 while (width > 0) {
930 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
931 mask++;
932 dst++;
933 width--;
934 }
935 }
936
937 void blit_row_lcd16_opaque(SkPMColor dst[], const uint16_t mask[],
938 SkColor src, int width, SkPMColor opaqueDst) {
939 if (width <= 0) {
940 return;
941 }
942
943 int srcR = SkColorGetR(src);
944 int srcG = SkColorGetG(src);
945 int srcB = SkColorGetB(src);
946 __m256i xv_zero = __lasx_xvldi(0);
947
948 if (width >= 8) {
949 SkASSERT(SkIsAlign4((uintptr_t) dst));
950 while (!SkIsAlign16((uintptr_t) dst)) {
951 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
952 mask++;
953 dst++;
954 width--;
955 }
956
957 __m256i *d = reinterpret_cast<__m256i*>(dst);
958 // Set alpha to 0xFF and replicate source four times in LASX register.
959 unsigned int sk_pack_argb32 = SkPackARGB32(0xFF, srcR, srcG, srcB);
960 __m256i src_lasx = __lasx_xvreplgr2vr_w(sk_pack_argb32);
961 // Set srcA_lasx to contain sixteen copies of srcA, padded with zero.
962 // src_lasx=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0,
963 // 0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
964 src_lasx = __lasx_xvilvl_b(xv_zero, src_lasx);
965
966 while (width >= 8) {
967 // Load eight destination pixels into dst_lasx.
968 __m256i dst_lasx = __lasx_xvld(d, 0);
969 // Load eight 16-bit masks into lower half of mask_lasx.
970 __m256i mask_lasx = __lasx_xvld(mask, 0);
971 mask_lasx = (__m256i){mask_lasx[0], 0, mask_lasx[1], 0};
972
973 int32_t pack_cmp = __lasx_xbz_v(mask_lasx);
974 // if mask pixels are not all zero, we will blend the dst pixels
975 if (pack_cmp != 1) {
976 // Unpack 8 16bit mask pixels to
977 // mask_lasx = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
978 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0,
979 // m4RGBLo, m4RGBHi, 0, 0, m5RGBLo, m5RGBHi, 0, 0,
980 // m6RGBLo, m6RGBHi, 0, 0, m7RGBLo, m7RGBHi, 0, 0)
981 mask_lasx = __lasx_xvilvl_h(xv_zero, mask_lasx);
982 // Process 8 32bit dst pixels
983 __m256i result = blend_lcd16_opaque_lasx(src_lasx, dst_lasx, mask_lasx);
984 __lasx_xvst(result, d, 0);
985 }
986 d++;
987 mask += 8;
988 width -= 8;
989 }
990
991 dst = reinterpret_cast<SkPMColor*>(d);
992 }
993
994 while (width > 0) {
995 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
996 mask++;
997 dst++;
998 width--;
999 }
1000 }
1001
1002 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LSX
1003
1004 // The following (left) shifts cause the top 5 bits of the mask components to
1005 // line up with the corresponding components in an SkPMColor.
1006 // Note that the mask's RGB16 order may differ from the SkPMColor order.
1007 #define SK_R16x5_R32x5_SHIFT (SK_R32_SHIFT - SK_R16_SHIFT - SK_R16_BITS + 5)
1008 #define SK_G16x5_G32x5_SHIFT (SK_G32_SHIFT - SK_G16_SHIFT - SK_G16_BITS + 5)
1009 #define SK_B16x5_B32x5_SHIFT (SK_B32_SHIFT - SK_B16_SHIFT - SK_B16_BITS + 5)
1010
1011 #if SK_R16x5_R32x5_SHIFT == 0
1012 #define SkPackedR16x5ToUnmaskedR32x5_LSX(x) (x)
1013 #elif SK_R16x5_R32x5_SHIFT > 0
1014 #define SkPackedR16x5ToUnmaskedR32x5_LSX(x) (__lsx_vslli_w(x, SK_R16x5_R32x5_SHIFT))
1015 #else
1016 #define SkPackedR16x5ToUnmaskedR32x5_LSX(x) (__lsx_vsrli_w(x, -SK_R16x5_R32x5_SHIFT))
1017 #endif
1018
1019 #if SK_G16x5_G32x5_SHIFT == 0
1020 #define SkPackedG16x5ToUnmaskedG32x5_LSX(x) (x)
1021 #elif SK_G16x5_G32x5_SHIFT > 0
1022 #define SkPackedG16x5ToUnmaskedG32x5_LSX(x) (__lsx_vslli_w(x, SK_G16x5_G32x5_SHIFT))
1023 #else
1024 #define SkPackedG16x5ToUnmaskedG32x5_LSX(x) (__lsx_vsrli_w(x, -SK_G16x5_G32x5_SHIFT))
1025 #endif
1026
1027 #if SK_B16x5_B32x5_SHIFT == 0
1028 #define SkPackedB16x5ToUnmaskedB32x5_LSX(x) (x)
1029 #elif SK_B16x5_B32x5_SHIFT > 0
1030 #define SkPackedB16x5ToUnmaskedB32x5_LSX(x) (__lsx_vslli_w(x, SK_B16x5_B32x5_SHIFT))
1031 #else
1032 #define SkPackedB16x5ToUnmaskedB32x5_LSX(x) (__lsx_vsrli_w(x, -SK_B16x5_B32x5_SHIFT))
1033 #endif
1034
1035 static __m128i blend_lcd16_lsx(__m128i &src, __m128i &dst, __m128i &mask, __m128i &srcA) {
1036 // In the following comments, the components of src, dst and mask are
1037 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
1038 // by an R, G, B, or A suffix. Components of one of the four pixels that
1039 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
1040 // example is the blue channel of the second destination pixel. Memory
1041 // layout is shown for an ARGB byte order in a color value.
1042
1043 // src and srcA store 8-bit values interleaved with zeros.
1044 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
1045 // srcA = (srcA, 0, srcA, 0, srcA, 0, srcA, 0,
1046 // srcA, 0, srcA, 0, srcA, 0, srcA, 0)
1047 // mask stores 16-bit values (compressed three channels) interleaved with zeros.
1048 // Lo and Hi denote the low and high bytes of a 16-bit value, respectively.
1049 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
1050 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
1051
1052 __m128i v_zero = __lsx_vldi(0);
1053
1054 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
1055 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
1056 __m128i r = __lsx_vand_v(SkPackedR16x5ToUnmaskedR32x5_LSX(mask),
1057 __lsx_vreplgr2vr_w(0x1F << SK_R32_SHIFT));
1058
1059 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
1060 __m128i g = __lsx_vand_v(SkPackedG16x5ToUnmaskedG32x5_LSX(mask),
1061 __lsx_vreplgr2vr_w(0x1F << SK_G32_SHIFT));
1062
1063 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
1064 __m128i b = __lsx_vand_v(SkPackedB16x5ToUnmaskedB32x5_LSX(mask),
1065 __lsx_vreplgr2vr_w(0x1F << SK_B32_SHIFT));
1066
1067 // a needs to be either the min or the max of the LCD coverages, depending on srcA < dstA
1068 __m128i aMin = __lsx_vmin_b(__lsx_vslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
1069 __lsx_vmin_b(__lsx_vslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
1070 __lsx_vslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
1071 __m128i aMax = __lsx_vmax_b(__lsx_vslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
1072 __lsx_vmax_b(__lsx_vslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
1073 __lsx_vslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
1074 // srcA has been biased to [0-256], so compare srcA against (dstA+1)
1075 __m128i a = __lsx_vmskltz_w(srcA -
1076 __lsx_vand_v(
1077 __lsx_vadd_w(dst,
1078 __lsx_vreplgr2vr_w(1 << SK_A32_SHIFT)),
1079 __lsx_vreplgr2vr_w(SK_A32_MASK)));
1080 // a = if_then_else(a, aMin, aMax) == (aMin & a) | (aMax & ~a)
1081 a = __lsx_vor_v(__lsx_vand_v(a, aMin), __lsx_vandn_v(a, aMax));
1082
1083 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
1084 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
1085 // 8-bit position
1086 // mask = (m0A, m0R, m0G, m0B, m1A, m1R, m1G, m1B,
1087 // m2A, m2R, m2G, m2B, m3A, m3R, m3G, m3B)
1088 mask = __lsx_vor_v(__lsx_vor_v(a, r), __lsx_vor_v(g, b));
1089
1090 // Interleave R,G,B into the lower byte of word.
1091 // i.e. split the sixteen 8-bit values from mask into two sets of eight
1092 // 16-bit values, padded by zero.
1093 __m128i maskLo, maskHi;
1094 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0)
1095 maskLo = __lsx_vilvl_b(v_zero, mask);
1096 // maskHi = (m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
1097 maskHi = __lsx_vilvh_b(v_zero, mask);
1098
1099 // Upscale from 0..31 to 0..32
1100 // (allows to replace division by left-shift further down)
1101 // Left-shift each component by 4 and add the result back to that component,
1102 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
1103 maskLo = __lsx_vadd_h(maskLo, __lsx_vsrli_h(maskLo, 4));
1104 maskHi = __lsx_vadd_h(maskHi, __lsx_vsrli_h(maskHi, 4));
1105
1106 // Multiply each component of maskLo and maskHi by srcA
1107 maskLo = __lsx_vmul_h(maskLo, srcA);
1108 maskHi = __lsx_vmul_h(maskHi, srcA);
1109
1110 // Left shift mask components by 8 (divide by 256)
1111 maskLo = __lsx_vsrli_h(maskLo, 8);
1112 maskHi = __lsx_vsrli_h(maskHi, 8);
1113
1114 // Interleave R,G,B into the lower byte of the word
1115 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0)
1116 __m128i dstLo = __lsx_vilvl_b(v_zero, dst);
1117 // dstLo = (d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
1118 __m128i dstHi = __lsx_vilvh_b(v_zero, dst);
1119
1120 // mask = (src - dst) * mask
1121 maskLo = __lsx_vmul_h(maskLo, __lsx_vsub_h(src, dstLo));
1122 maskHi = __lsx_vmul_h(maskHi, __lsx_vsub_h(src, dstHi));
1123
1124 // mask = (src - dst) * mask >> 5
1125 maskLo = __lsx_vsrai_h(maskLo, 5);
1126 maskHi = __lsx_vsrai_h(maskHi, 5);
1127
1128 // Add two pixels into result.
1129 // result = dst + ((src - dst) * mask >> 5)
1130 __m128i resultLo = __lsx_vadd_h(dstLo, maskLo);
1131 __m128i resultHi = __lsx_vadd_h(dstHi, maskHi);
1132
1133 // Pack into 4 32bit dst pixels.
1134 // resultLo and resultHi contain eight 16-bit components (two pixels) each.
1135 // Merge into one LSX regsiter with sixteen 8-bit values (four pixels),
1136 // clamping to 255 if necessary.
1137 __m128i tmpl = __lsx_vsat_hu(resultLo, 7);
1138 __m128i tmph = __lsx_vsat_hu(resultHi, 7);
1139 return __lsx_vpickev_b(tmph, tmpl);
1140 }
1141
1142 static __m128i blend_lcd16_opaque_lsx(__m128i &src, __m128i &dst, __m128i &mask) {
1143 // In the following comments, the components of src, dst and mask are
1144 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
1145 // by an R, G, B, or A suffix. Components of one of the four pixels that
1146 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
1147 // example is the blue channel of the second destination pixel. Memory
1148 // layout is shown for an ARGB byte order in a color value.
1149
1150 // src and srcA store 8-bit values interleaved with zeros.
1151 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
1152 // mask stores 16-bit values (shown as high and low bytes) interleaved with
1153 // zeros
1154 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
1155 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
1156
1157 __m128i v_zero = __lsx_vldi(0);
1158
1159 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
1160 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
1161 __m128i r = __lsx_vand_v(SkPackedR16x5ToUnmaskedR32x5_LSX(mask),
1162 __lsx_vreplgr2vr_w(0x1F << SK_R32_SHIFT));
1163
1164 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
1165 __m128i g = __lsx_vand_v(SkPackedG16x5ToUnmaskedG32x5_LSX(mask),
1166 __lsx_vreplgr2vr_w(0x1F << SK_G32_SHIFT));
1167
1168 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
1169 __m128i b = __lsx_vand_v(SkPackedB16x5ToUnmaskedB32x5_LSX(mask),
1170 __lsx_vreplgr2vr_w(0x1F << SK_B32_SHIFT));
1171
1172 // a = max(r, g, b) since opaque src alpha uses max of LCD coverages
1173 __m128i a = __lsx_vmax_b(__lsx_vslli_w(r, SK_A32_SHIFT - SK_R32_SHIFT),
1174 __lsx_vmax_b(__lsx_vslli_w(g, SK_A32_SHIFT - SK_G32_SHIFT),
1175 __lsx_vslli_w(b, SK_A32_SHIFT - SK_B32_SHIFT)));
1176
1177 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
1178 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
1179 // 8-bit position
1180 // mask = (m0A, m0R, m0G, m0B, m1A, m1R, m1G, m1B,
1181 // m2A, m2R, m2G, m2B, m3A, m3R, m3G, m3B)
1182 mask = __lsx_vor_v(__lsx_vor_v(a, r), __lsx_vor_v(g, b));
1183
1184 // Interleave R,G,B into the lower byte of word.
1185 // i.e. split the sixteen 8-bit values from mask into two sets of eight
1186 // 16-bit values, padded by zero.
1187 __m128i maskLo, maskHi;
1188 // maskLo = (m0A, 0, m0R, 0, m0G, 0, m0B, 0, m1A, 0, m1R, 0, m1G, 0, m1B, 0)
1189 maskLo = __lsx_vilvl_b(v_zero, mask);
1190 // maskHi = (m2A, 0, m2R, 0, m2G, 0, m2B, 0, m3A, 0, m3R, 0, m3G, 0, m3B, 0)
1191 maskHi = __lsx_vilvh_b(v_zero, mask);
1192
1193 // Upscale from 0..31 to 0..32
1194 // (allows to replace division by left-shift further down)
1195 // Left-shift each component by 4 and add the result back to that component,
1196 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
1197 maskLo = __lsx_vadd_h(maskLo, __lsx_vsrli_h(maskLo, 4));
1198 maskHi = __lsx_vadd_h(maskHi, __lsx_vsrli_h(maskHi, 4));
1199
1200 // Interleave R,G,B into the lower byte of the word
1201 // dstLo = (d0A, 0, d0R, 0, d0G, 0, d0B, 0, d1A, 0, d1R, 0, d1G, 0, d1B, 0)
1202 __m128i dstLo = __lsx_vilvl_b(v_zero, dst);
1203 // dstLo = (d2A, 0, d2R, 0, d2G, 0, d2B, 0, d3A, 0, d3R, 0, d3G, 0, d3B, 0)
1204 __m128i dstHi = __lsx_vilvh_b(v_zero, dst);
1205
1206 // mask = (src - dst) * mask
1207 maskLo = __lsx_vmul_h(maskLo, __lsx_vsub_h(src, dstLo));
1208 maskHi = __lsx_vmul_h(maskHi, __lsx_vsub_h(src, dstHi));
1209
1210 // mask = (src - dst) * mask >> 5
1211 maskLo = __lsx_vsrai_h(maskLo, 5);
1212 maskHi = __lsx_vsrai_h(maskHi, 5);
1213
1214 // Add two pixels into result.
1215 // result = dst + ((src - dst) * mask >> 5)
1216 __m128i resultLo = __lsx_vadd_h(dstLo, maskLo);
1217 __m128i resultHi = __lsx_vadd_h(dstHi, maskHi);
1218
1219 // Merge into one LSX regsiter with sixteen 8-bit values (four pixels),
1220 // clamping to 255 if necessary.
1221 __m128i tmpl = __lsx_vsat_hu(resultLo, 7);
1222 __m128i tmph = __lsx_vsat_hu(resultHi, 7);
1223 return __lsx_vpickev_b(tmph, tmpl);
1224 }
1225
1226 void blit_row_lcd16(SkPMColor dst[], const uint16_t mask[], SkColor src, int width, SkPMColor) {
1227 if (width <= 0) {
1228 return;
1229 }
1230
1231 int srcA = SkColorGetA(src);
1232 int srcR = SkColorGetR(src);
1233 int srcG = SkColorGetG(src);
1234 int srcB = SkColorGetB(src);
1235 __m128i v_zero = __lsx_vldi(0);
1236
1237 srcA = SkAlpha255To256(srcA);
1238 if (width >= 4) {
1239 SkASSERT(SkIsAlign4((uintptr_t) dst));
1240 while (!SkIsAlign16((uintptr_t) dst)) {
1241 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
1242 mask++;
1243 dst++;
1244 width--;
1245 }
1246
1247 __m128i *d = reinterpret_cast<__m128i*>(dst);
1248 // Set alpha to 0xFF and replicate source eight times in LSX register.
1249 unsigned int skpackargb32 = SkPackARGB32(0xFF, srcR, srcG, srcB);
1250 __m128i src_lsx = __lsx_vreplgr2vr_w(skpackargb32);
1251 // Interleave with zeros to get two sets of eight 16-bit values.
1252 src_lsx = __lsx_vilvl_b(v_zero, src_lsx);
1253 // Set srcA_lsx to contain eight copies of srcA, padded with zero.
1254 // src_lsx=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
1255 __m128i srcA_lsx = __lsx_vreplgr2vr_h(srcA);
1256
1257 while (width >= 4) {
1258 // Load eight destination pixels into dst_lsx.
1259 __m128i dst_lsx = __lsx_vld(d, 0);
1260 // Load four 16-bit masks into lower half of mask_lsx.
1261 __m128i mask_lsx = __lsx_vldrepl_d((void *)mask, 0);
1262 mask_lsx = __lsx_vilvl_d(v_zero, mask_lsx);
1263
1264 int pack_cmp = __lsx_bz_v(mask_lsx);
1265 // if mask pixels are not all zero, we will blend the dst pixels
1266 if (pack_cmp != 1) {
1267 // Unpack 4 16bit mask pixels to
1268 // mask_lsx = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
1269 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
1270 mask_lsx = __lsx_vilvl_h(v_zero, mask_lsx);
1271
1272 // Process 8 32bit dst pixels
1273 __m128i result = blend_lcd16_lsx(src_lsx, dst_lsx, mask_lsx, srcA_lsx);
1274 __lsx_vst(result, d, 0);
1275 }
1276
1277 d++;
1278 mask += 4;
1279 width -= 4;
1280 }
1281
1282 dst = reinterpret_cast<SkPMColor*>(d);
1283 }
1284
1285 while (width > 0) {
1286 *dst = blend_lcd16(srcA, srcR, srcG, srcB, *dst, *mask);
1287 mask++;
1288 dst++;
1289 width--;
1290 }
1291 }
1292
1293 void blit_row_lcd16_opaque(SkPMColor dst[], const uint16_t mask[],
1294 SkColor src, int width, SkPMColor opaqueDst) {
1295 if (width <= 0) {
1296 return;
1297 }
1298
1299 int srcR = SkColorGetR(src);
1300 int srcG = SkColorGetG(src);
1301 int srcB = SkColorGetB(src);
1302 __m128i v_zero = __lsx_vldi(0);
1303
1304 if (width >= 4) {
1305 SkASSERT(SkIsAlign4((uintptr_t) dst));
1306 while (!SkIsAlign16((uintptr_t) dst)) {
1307 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
1308 mask++;
1309 dst++;
1310 width--;
1311 }
1312
1313 __m128i *d = reinterpret_cast<__m128i*>(dst);
1314 // Set alpha to 0xFF and replicate source four times in LSX register.
1315 unsigned int sk_pack_argb32 = SkPackARGB32(0xFF, srcR, srcG, srcB);
1316 __m128i src_lsx = __lsx_vreplgr2vr_w(sk_pack_argb32);
1317 // Set srcA_lsx to contain eight copies of srcA, padded with zero.
1318 // src_lsx=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
1319 src_lsx = __lsx_vilvl_b(v_zero, src_lsx);
1320
1321 while (width >= 4) {
1322 // Load four destination pixels into dst_lsx.
1323 __m128i dst_lsx = __lsx_vld(d, 0);
1324 // Load four 16-bit masks into lower half of mask_lsx.
1325 __m128i mask_lsx = __lsx_vldrepl_d((void *)(mask), 0);
1326 mask_lsx = __lsx_vilvl_d(v_zero, mask_lsx);
1327
1328 int pack_cmp = __lsx_bz_v(mask_lsx);
1329 // if mask pixels are not all zero, we will blend the dst pixels
1330 if (pack_cmp != 1) {
1331 // Unpack 4 16bit mask pixels to
1332 mask_lsx = __lsx_vilvl_h(v_zero, mask_lsx);
1333
1334 // Process 8 32bit dst pixels
1335 __m128i result = blend_lcd16_opaque_lsx(src_lsx, dst_lsx, mask_lsx);
1336 __lsx_vst(result, d, 0);
1337 }
1338 d++;
1339 mask += 4;
1340 width -= 4;
1341 }
1342
1343 dst = reinterpret_cast<SkPMColor*>(d);
1344 }
1345
1346 while (width > 0) {
1347 *dst = blend_lcd16_opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
1348 mask++;
1349 dst++;
1350 width--;
1351 }
1352 }
1353
1354 #else
1355
1356 static inline void blit_row_lcd16(SkPMColor dst[], const uint16_t mask[],
1357 SkColor src, int width, SkPMColor) {
1358 int srcA = SkColorGetA(src);
1359 int srcR = SkColorGetR(src);
1360 int srcG = SkColorGetG(src);
1361 int srcB = SkColorGetB(src);
1362
1363 srcA = SkAlpha255To256(srcA);
1364
1365 for (int i = 0; i < width; i++) {
1366 dst[i] = blend_lcd16(srcA, srcR, srcG, srcB, dst[i], mask[i]);
1367 }
1368 }
1369
1370 static inline void blit_row_lcd16_opaque(SkPMColor dst[], const uint16_t mask[],
1371 SkColor src, int width,
1372 SkPMColor opaqueDst) {
1373 int srcR = SkColorGetR(src);
1374 int srcG = SkColorGetG(src);
1375 int srcB = SkColorGetB(src);
1376
1377 for (int i = 0; i < width; i++) {
1378 dst[i] = blend_lcd16_opaque(srcR, srcG, srcB, dst[i], mask[i], opaqueDst);
1379 }
1380 }
1381
1382 #endif
1383
1384 static bool blit_color(const SkPixmap& device,
1385 const SkMask& mask,
1386 const SkIRect& clip,
1387 SkColor color) {
1388 int x = clip.fLeft,
1389 y = clip.fTop;
1390
1391 if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kA8_Format) {
1392 SkOpts::blit_mask_d32_a8(device.writable_addr32(x,y), device.rowBytes(),
1393 (const SkAlpha*)mask.getAddr(x,y), mask.fRowBytes,
1394 color, clip.width(), clip.height());
1395 return true;
1396 }
1397
1398 if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kLCD16_Format) {
1399 auto dstRow = device.writable_addr32(x,y);
1400 auto maskRow = (const uint16_t*)mask.getAddr(x,y);
1401
1402 auto blit_row = blit_row_lcd16;
1403 SkPMColor opaqueDst = 0; // ignored unless opaque
1404
1405 if (0xff == SkColorGetA(color)) {
1406 blit_row = blit_row_lcd16_opaque;
1407 opaqueDst = SkPreMultiplyColor(color);
1408 }
1409
1410 for (int height = clip.height(); height --> 0; ) {
1411 blit_row(dstRow, maskRow, color, clip.width(), opaqueDst);
1412
1413 dstRow = (SkPMColor*) (( char*) dstRow + device.rowBytes());
1414 maskRow = (const uint16_t*)((const char*)maskRow + mask.fRowBytes);
1415 }
1416 return true;
1417 }
1418
1419 return false;
1420 }
1421
1422 ///////////////////////////////////////////////////////////////////////////////
1423
1424 static void SkARGB32_Blit32(const SkPixmap& device, const SkMask& mask,
1425 const SkIRect& clip, SkPMColor srcColor) {
1426 U8CPU alpha = SkGetPackedA32(srcColor);
1427 unsigned flags = SkBlitRow::kSrcPixelAlpha_Flag32;
1428 if (alpha != 255) {
1429 flags |= SkBlitRow::kGlobalAlpha_Flag32;
1430 }
1431 SkBlitRow::Proc32 proc = SkBlitRow::Factory32(flags);
1432
1433 int x = clip.fLeft;
1434 int y = clip.fTop;
1435 int width = clip.width();
1436 int height = clip.height();
1437
1438 SkPMColor* dstRow = device.writable_addr32(x, y);
1439 const SkPMColor* srcRow = reinterpret_cast<const SkPMColor*>(mask.getAddr8(x, y));
1440
1441 do {
1442 proc(dstRow, srcRow, width, alpha);
1443 dstRow = (SkPMColor*)((char*)dstRow + device.rowBytes());
1444 srcRow = (const SkPMColor*)((const char*)srcRow + mask.fRowBytes);
1445 } while (--height != 0);
1446 }
1447
1448 //////////////////////////////////////////////////////////////////////////////////////
1449
1450 SkARGB32_Blitter::SkARGB32_Blitter(const SkPixmap& device, const SkPaint& paint)
1451 : SkRasterBlitter(device) {
1452 SkColor color = paint.getColor();
1453 fColor = color;
1454 fSrcA = SkColorGetA(color);
1455 fPMColor = SkPreMultiplyColor(fColor);
1456 }
1457
1458 #if defined _WIN32 // disable warning : local variable used without having been initialized
1459 #pragma warning ( push )
1460 #pragma warning ( disable : 4701 )
1461 #endif
1462
1463 void SkARGB32_Blitter::blitH(int x, int y, int width) {
1464 SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width());
1465
1466 uint32_t* device = fDevice.writable_addr32(x, y);
1467 SkBlitRow::Color32(device, width, fPMColor);
1468 }
1469
1470 void SkARGB32_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
1471 const int16_t runs[]) {
1472 SkASSERT(fSrcA != 0xFF); // There is an opaque specialization
1473 if (fSrcA == 0) {
1474 return;
1475 }
1476
1477 uint32_t* device = fDevice.writable_addr32(x, y);
1478
1479 for (;;) {
1480 int count = runs[0];
1481 SkASSERT(count >= 0);
1482 if (count <= 0) {
1483 return;
1484 }
1485 SkAlpha aa = antialias[0];
1486 if (aa) {
1487 SkPMColor sc = SkAlphaMulQ(fPMColor, SkAlpha255To256(aa));
1488 SkBlitRow::Color32(device, count, sc);
1489 }
1490 runs += count;
1491 antialias += count;
1492 device += count;
1493 }
1494 }
1495
1496 void SkARGB32_Blitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
1497 uint32_t* device = fDevice.writable_addr32(x, y);
1498 SkDEBUGCODE((void)fDevice.writable_addr32(x + 1, y);)
1499
1500 device[0] = SkBlendARGB32(fPMColor, device[0], a0);
1501 device[1] = SkBlendARGB32(fPMColor, device[1], a1);
1502 }
1503
1504 void SkARGB32_Blitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
1505 uint32_t* device = fDevice.writable_addr32(x, y);
1506 SkDEBUGCODE((void)fDevice.writable_addr32(x, y + 1);)
1507
1508 device[0] = SkBlendARGB32(fPMColor, device[0], a0);
1509 device = (uint32_t*)((char*)device + fDevice.rowBytes());
1510 device[0] = SkBlendARGB32(fPMColor, device[0], a1);
1511 }
1512
1513 //////////////////////////////////////////////////////////////////////////////////////
1514
1515 #define solid_8_pixels(mask, dst, color) \
1516 do { \
1517 if (mask & 0x80) dst[0] = color; \
1518 if (mask & 0x40) dst[1] = color; \
1519 if (mask & 0x20) dst[2] = color; \
1520 if (mask & 0x10) dst[3] = color; \
1521 if (mask & 0x08) dst[4] = color; \
1522 if (mask & 0x04) dst[5] = color; \
1523 if (mask & 0x02) dst[6] = color; \
1524 if (mask & 0x01) dst[7] = color; \
1525 } while (0)
1526
1527 #define SK_BLITBWMASK_NAME SkARGB32_BlitBW
1528 #define SK_BLITBWMASK_ARGS , SkPMColor color
1529 #define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst, color)
1530 #define SK_BLITBWMASK_GETADDR writable_addr32
1531 #define SK_BLITBWMASK_DEVTYPE uint32_t
1532 #include "src/core/SkBlitBWMaskTemplate.h"
1533
1534 #define blend_8_pixels(mask, dst, sc, dst_scale) \
1535 do { \
1536 if (mask & 0x80) { dst[0] = sc + SkAlphaMulQ(dst[0], dst_scale); } \
1537 if (mask & 0x40) { dst[1] = sc + SkAlphaMulQ(dst[1], dst_scale); } \
1538 if (mask & 0x20) { dst[2] = sc + SkAlphaMulQ(dst[2], dst_scale); } \
1539 if (mask & 0x10) { dst[3] = sc + SkAlphaMulQ(dst[3], dst_scale); } \
1540 if (mask & 0x08) { dst[4] = sc + SkAlphaMulQ(dst[4], dst_scale); } \
1541 if (mask & 0x04) { dst[5] = sc + SkAlphaMulQ(dst[5], dst_scale); } \
1542 if (mask & 0x02) { dst[6] = sc + SkAlphaMulQ(dst[6], dst_scale); } \
1543 if (mask & 0x01) { dst[7] = sc + SkAlphaMulQ(dst[7], dst_scale); } \
1544 } while (0)
1545
1546 #define SK_BLITBWMASK_NAME SkARGB32_BlendBW
1547 #define SK_BLITBWMASK_ARGS , uint32_t sc, unsigned dst_scale
1548 #define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, sc, dst_scale)
1549 #define SK_BLITBWMASK_GETADDR writable_addr32
1550 #define SK_BLITBWMASK_DEVTYPE uint32_t
1551 #include "src/core/SkBlitBWMaskTemplate.h"
1552
1553 void SkARGB32_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) {
1554 SkASSERT(mask.fBounds.contains(clip));
1555 SkASSERT(fSrcA != 0xFF);
1556
1557 if (fSrcA == 0) {
1558 return;
1559 }
1560
1561 if (blit_color(fDevice, mask, clip, fColor)) {
1562 return;
1563 }
1564
1565 switch (mask.fFormat) {
1566 case SkMask::kBW_Format:
1567 SkARGB32_BlendBW(fDevice, mask, clip, fPMColor, SkAlpha255To256(255 - fSrcA));
1568 break;
1569 case SkMask::kARGB32_Format:
1570 SkARGB32_Blit32(fDevice, mask, clip, fPMColor);
1571 break;
1572 default:
1573 SK_ABORT("Mask format not handled.");
1574 }
1575 }
1576
1577 void SkARGB32_Opaque_Blitter::blitMask(const SkMask& mask,
1578 const SkIRect& clip) {
1579 SkASSERT(fSrcA == 0xFF);
1580 SkASSERT(mask.fBounds.contains(clip));
1581
1582 if (blit_color(fDevice, mask, clip, fColor)) {
1583 return;
1584 }
1585
1586 switch (mask.fFormat) {
1587 case SkMask::kBW_Format:
1588 SkARGB32_BlitBW(fDevice, mask, clip, fPMColor);
1589 break;
1590 case SkMask::kARGB32_Format:
1591 SkARGB32_Blit32(fDevice, mask, clip, fPMColor);
1592 break;
1593 default:
1594 SK_ABORT("Mask format not handled.");
1595 }
1596 }
1597
1598 void SkARGB32_Opaque_Blitter::blitAntiH(int x,
1599 int y,
1600 const SkAlpha antialias[],
1601 const int16_t runs[]) {
1602 SkASSERT(fSrcA == 0xFF);
1603
1604 uint32_t* device = fDevice.writable_addr32(x, y);
1605 for (;;) {
1606 int count = runs[0];
1607 SkASSERT(count >= 0);
1608 if (count <= 0) {
1609 return;
1610 }
1611 SkAlpha aa = antialias[0];
1612 if (aa == 255) {
1613 SkOpts::memset32(device, fPMColor, count);
1614 } else if (aa > 0) {
1615 SkPMColor sc = SkAlphaMulQ(fPMColor, SkAlpha255To256(aa));
1616 SkBlitRow::Color32(device, count, sc);
1617 }
1618 runs += count;
1619 antialias += count;
1620 device += count;
1621 }
1622 }
1623
1624 void SkARGB32_Opaque_Blitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
1625 uint32_t* device = fDevice.writable_addr32(x, y);
1626 SkDEBUGCODE((void)fDevice.writable_addr32(x + 1, y);)
1627
1628 device[0] = SkFastFourByteInterp(fPMColor, device[0], a0);
1629 device[1] = SkFastFourByteInterp(fPMColor, device[1], a1);
1630 }
1631
1632 void SkARGB32_Opaque_Blitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
1633 uint32_t* device = fDevice.writable_addr32(x, y);
1634 SkDEBUGCODE((void)fDevice.writable_addr32(x, y + 1);)
1635
1636 device[0] = SkFastFourByteInterp(fPMColor, device[0], a0);
1637 device = (uint32_t*)((char*)device + fDevice.rowBytes());
1638 device[0] = SkFastFourByteInterp(fPMColor, device[0], a1);
1639 }
1640
1641 std::optional<SkBlitter::DirectBlit> SkARGB32_Opaque_Blitter::canDirectBlit() {
1642 return {{ fDevice, fPMColor }};
1643 }
1644
1645 ///////////////////////////////////////////////////////////////////////////////
1646
1647 void SkARGB32_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
1648 if (alpha == 0 || fSrcA == 0) {
1649 return;
1650 }
1651
1652 uint32_t* device = fDevice.writable_addr32(x, y);
1653 SkPMColor color = fPMColor;
1654
1655 if (alpha != 255) {
1656 color = SkAlphaMulQ(color, SkAlpha255To256(alpha));
1657 }
1658
1659 const unsigned dst_scale = SkAlpha255To256(255 - SkGetPackedA32(color));
1660 const size_t rowBytes = fDevice.rowBytes();
1661 while (--height >= 0) {
1662 device[0] = color + SkAlphaMulQ(device[0], dst_scale);
1663 device = (uint32_t*)((char*)device + rowBytes);
1664 }
1665 }
1666
1667 void SkARGB32_Blitter::blitRect(int x, int y, int width, int height) {
1668 SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width() && y + height <= fDevice.height());
1669
1670 if (fSrcA == 0) {
1671 return;
1672 }
1673
1674 uint32_t* device = fDevice.writable_addr32(x, y);
1675 const size_t rowBytes = fDevice.rowBytes();
1676
1677 if (SkGetPackedA32(fPMColor) == 0xFF) {
1678 SkOpts::rect_memset32(device, fPMColor, width, rowBytes, height);
1679 } else {
1680 while (height --> 0) {
1681 SkBlitRow::Color32(device, width, fPMColor);
1682 device = (uint32_t*)((char*)device + rowBytes);
1683 }
1684 }
1685 }
1686
1687 #if defined _WIN32
1688 #pragma warning ( pop )
1689 #endif
1690
1691 ///////////////////////////////////////////////////////////////////////
1692
1693 void SkARGB32_Black_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
1694 const int16_t runs[]) {
1695 uint32_t* device = fDevice.writable_addr32(x, y);
1696 static constexpr SkPMColor kBlack = (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT);
1697
1698 for (;;) {
1699 int count = runs[0];
1700 SkASSERT(count >= 0);
1701 if (count <= 0) {
1702 return;
1703 }
1704 unsigned aa = antialias[0];
1705 if (aa) {
1706 if (aa == 255) {
1707 SkOpts::memset32(device, kBlack, count);
1708 } else {
1709 const SkPMColor src = aa << SK_A32_SHIFT;
1710 const unsigned dst_scale = SkAlpha255To256(255 - aa);
1711 int n = count;
1712 do {
1713 --n;
1714 device[n] = src + SkAlphaMulQ(device[n], dst_scale);
1715 } while (n > 0);
1716 }
1717 }
1718 runs += count;
1719 antialias += count;
1720 device += count;
1721 }
1722 }
1723
1724 void SkARGB32_Black_Blitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) {
1725 uint32_t* device = fDevice.writable_addr32(x, y);
1726 SkDEBUGCODE((void)fDevice.writable_addr32(x + 1, y);)
1727
1728 device[0] = (a0 << SK_A32_SHIFT) + SkAlphaMulQ(device[0], 256 - a0);
1729 device[1] = (a1 << SK_A32_SHIFT) + SkAlphaMulQ(device[1], 256 - a1);
1730 }
1731
1732 void SkARGB32_Black_Blitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) {
1733 uint32_t* device = fDevice.writable_addr32(x, y);
1734 SkDEBUGCODE((void)fDevice.writable_addr32(x, y + 1);)
1735
1736 device[0] = (a0 << SK_A32_SHIFT) + SkAlphaMulQ(device[0], 256 - a0);
1737 device = (uint32_t*)((char*)device + fDevice.rowBytes());
1738 device[0] = (a1 << SK_A32_SHIFT) + SkAlphaMulQ(device[0], 256 - a1);
1739 }
1740
1741 ///////////////////////////////////////////////////////////////////////////////
1742
1743 SkARGB32_Shader_Blitter::SkARGB32_Shader_Blitter(const SkPixmap& device,
1744 const SkPaint& paint,
1745 SkShaderBase::Context* shaderContext)
1746 : SkShaderBlitter(device, paint, shaderContext) {
1747 fBuffer = (SkPMColor*)sk_malloc_throw(device.width() * (sizeof(SkPMColor)));
1748
1749 SkASSERT(paint.isSrcOver());
1750
1751 int flags = 0;
1752 if (!(shaderContext->getFlags() & SkShaderBase::kOpaqueAlpha_Flag)) {
1753 flags |= SkBlitRow::kSrcPixelAlpha_Flag32;
1754 }
1755 // we call this on the output from the shader
1756 fProc32 = SkBlitRow::Factory32(flags);
1757 // we call this on the output from the shader + alpha from the aa buffer
1758 fProc32Blend = SkBlitRow::Factory32(flags | SkBlitRow::kGlobalAlpha_Flag32);
1759
1760 fShadeDirectlyIntoDevice =
1761 SkToBool(shaderContext->getFlags() & SkShaderBase::kOpaqueAlpha_Flag);
1762 }
1763
1764 SkARGB32_Shader_Blitter::~SkARGB32_Shader_Blitter() {
1765 sk_free(fBuffer);
1766 }
1767
1768 void SkARGB32_Shader_Blitter::blitH(int x, int y, int width) {
1769 SkASSERT(x >= 0 && y >= 0 && x + width <= fDevice.width());
1770
1771 uint32_t* device = fDevice.writable_addr32(x, y);
1772
1773 if (fShadeDirectlyIntoDevice) {
1774 fShaderContext->shadeSpan(x, y, device, width);
1775 } else {
1776 SkPMColor* span = fBuffer;
1777 fShaderContext->shadeSpan(x, y, span, width);
1778 fProc32(device, span, width, 255);
1779 }
1780 }
1781
1782 void SkARGB32_Shader_Blitter::blitRect(int x, int y, int width, int height) {
1783 SkASSERT(x >= 0 && y >= 0 &&
1784 x + width <= fDevice.width() && y + height <= fDevice.height());
1785
1786 uint32_t* device = fDevice.writable_addr32(x, y);
1787 const size_t deviceRB = fDevice.rowBytes();
1788 auto* shaderContext = fShaderContext;
1789 SkPMColor* span = fBuffer;
1790
1791 if (fShadeDirectlyIntoDevice) {
1792 do {
1793 shaderContext->shadeSpan(x, y, device, width);
1794 y += 1;
1795 device = (uint32_t*)((char*)device + deviceRB);
1796 } while (--height > 0);
1797 } else {
1798 SkBlitRow::Proc32 proc = fProc32;
1799 do {
1800 shaderContext->shadeSpan(x, y, span, width);
1801 proc(device, span, width, 255);
1802 y += 1;
1803 device = (uint32_t*)((char*)device + deviceRB);
1804 } while (--height > 0);
1805 }
1806 }
1807
1808 void SkARGB32_Shader_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[],
1809 const int16_t runs[]) {
1810 SkPMColor* span = fBuffer;
1811 uint32_t* device = fDevice.writable_addr32(x, y);
1812 auto* shaderContext = fShaderContext;
1813
1814 if (fShadeDirectlyIntoDevice || (shaderContext->getFlags() & SkShaderBase::kOpaqueAlpha_Flag)) {
1815 for (;;) {
1816 int count = *runs;
1817 if (count <= 0) {
1818 break;
1819 }
1820 int aa = *antialias;
1821 if (aa) {
1822 if (aa == 255) {
1823 // cool, have the shader draw right into the device
1824 shaderContext->shadeSpan(x, y, device, count);
1825 } else {
1826 shaderContext->shadeSpan(x, y, span, count);
1827 fProc32Blend(device, span, count, aa);
1828 }
1829 }
1830 device += count;
1831 runs += count;
1832 antialias += count;
1833 x += count;
1834 }
1835 } else {
1836 for (;;) {
1837 int count = *runs;
1838 if (count <= 0) {
1839 break;
1840 }
1841 int aa = *antialias;
1842 if (aa) {
1843 shaderContext->shadeSpan(x, y, span, count);
1844 if (aa == 255) {
1845 fProc32(device, span, count, 255);
1846 } else {
1847 fProc32Blend(device, span, count, aa);
1848 }
1849 }
1850 device += count;
1851 runs += count;
1852 antialias += count;
1853 x += count;
1854 }
1855 }
1856 }
1857
1858 using U32 = skvx::Vec< 4, uint32_t>;
1859 using U8x4 = skvx::Vec<16, uint8_t>;
1860 using U8 = skvx::Vec< 4, uint8_t>;
1861
1862 static void drive(SkPMColor* dst, const SkPMColor* src, const uint8_t* cov, int n,
1863 U8x4 (*kernel)(U8x4,U8x4,U8x4)) {
1864
1865 auto apply = [kernel](U32 dst, U32 src, U8 cov) -> U32 {
1866 U8x4 cov_splat = skvx::shuffle<0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3>(cov);
1867 return sk_bit_cast<U32>(kernel(sk_bit_cast<U8x4>(dst),
1868 sk_bit_cast<U8x4>(src),
1869 cov_splat));
1870 };
1871 while (n >= 4) {
1872 apply(U32::Load(dst), U32::Load(src), U8::Load(cov)).store(dst);
1873 dst += 4;
1874 src += 4;
1875 cov += 4;
1876 n -= 4;
1877 }
1878 while (n --> 0) {
1879 *dst = apply(U32{*dst}, U32{*src}, U8{*cov})[0];
1880 dst++;
1881 src++;
1882 cov++;
1883 }
1884 }
1885
1886 static void blend_row_A8(SkPMColor* dst, const void* mask, const SkPMColor* src, int n) {
1887 auto cov = (const uint8_t*)mask;
1888 drive(dst, src, cov, n, [](U8x4 d, U8x4 s, U8x4 c) {
1889 U8x4 s_aa = skvx::approx_scale(s, c),
1890 alpha = skvx::shuffle<3,3,3,3, 7,7,7,7, 11,11,11,11, 15,15,15,15>(s_aa);
1891 return s_aa + skvx::approx_scale(d, 255 - alpha);
1892 });
1893 }
1894
1895 static void blend_row_A8_opaque(SkPMColor* dst, const void* mask, const SkPMColor* src, int n) {
1896 auto cov = (const uint8_t*)mask;
1897 drive(dst, src, cov, n, [](U8x4 d, U8x4 s, U8x4 c) {
1898 return skvx::div255( skvx::cast<uint16_t>(s) * skvx::cast<uint16_t>( c )
1899 + skvx::cast<uint16_t>(d) * skvx::cast<uint16_t>(255-c));
1900 });
1901 }
1902
1903 static void blend_row_lcd16(SkPMColor* dst, const void* vmask, const SkPMColor* src, int n) {
1904 auto src_alpha_blend = [](int s, int d, int sa, int m) {
1905 return d + SkAlphaMul(s - SkAlphaMul(sa, d), m);
1906 };
1907
1908 auto upscale_31_to_255 = [](int v) {
1909 return (v << 3) | (v >> 2);
1910 };
1911
1912 auto mask = (const uint16_t*)vmask;
1913 for (int i = 0; i < n; ++i) {
1914 uint16_t m = mask[i];
1915 if (0 == m) {
1916 continue;
1917 }
1918
1919 SkPMColor s = src[i];
1920 SkPMColor d = dst[i];
1921
1922 int srcA = SkGetPackedA32(s);
1923 int srcR = SkGetPackedR32(s);
1924 int srcG = SkGetPackedG32(s);
1925 int srcB = SkGetPackedB32(s);
1926
1927 srcA += srcA >> 7;
1928
1929 // We're ignoring the least significant bit of the green coverage channel here.
1930 int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5);
1931 int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5);
1932 int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5);
1933
1934 // Scale up to 8-bit coverage to work with SkAlphaMul() in src_alpha_blend().
1935 maskR = upscale_31_to_255(maskR);
1936 maskG = upscale_31_to_255(maskG);
1937 maskB = upscale_31_to_255(maskB);
1938
1939 // This LCD blit routine only works if the destination is opaque.
1940 dst[i] = SkPackARGB32(0xFF,
1941 src_alpha_blend(srcR, SkGetPackedR32(d), srcA, maskR),
1942 src_alpha_blend(srcG, SkGetPackedG32(d), srcA, maskG),
1943 src_alpha_blend(srcB, SkGetPackedB32(d), srcA, maskB));
1944 }
1945 }
1946
1947 static void blend_row_LCD16_opaque(SkPMColor* dst, const void* vmask, const SkPMColor* src, int n) {
1948 auto mask = (const uint16_t*)vmask;
1949
1950 for (int i = 0; i < n; ++i) {
1951 uint16_t m = mask[i];
1952 if (0 == m) {
1953 continue;
1954 }
1955
1956 SkPMColor s = src[i];
1957 SkPMColor d = dst[i];
1958
1959 int srcR = SkGetPackedR32(s);
1960 int srcG = SkGetPackedG32(s);
1961 int srcB = SkGetPackedB32(s);
1962
1963 // We're ignoring the least significant bit of the green coverage channel here.
1964 int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5);
1965 int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5);
1966 int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5);
1967
1968 // Now upscale them to 0..32, so we can use blend_32.
1969 maskR = upscale_31_to_32(maskR);
1970 maskG = upscale_31_to_32(maskG);
1971 maskB = upscale_31_to_32(maskB);
1972
1973 // This LCD blit routine only works if the destination is opaque.
1974 dst[i] = SkPackARGB32(0xFF,
1975 blend_32(srcR, SkGetPackedR32(d), maskR),
1976 blend_32(srcG, SkGetPackedG32(d), maskG),
1977 blend_32(srcB, SkGetPackedB32(d), maskB));
1978 }
1979 }
1980
1981 void SkARGB32_Shader_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) {
1982 SkASSERT(mask.fBounds.contains(clip));
1983
1984 void (*blend_row)(SkPMColor*, const void* mask, const SkPMColor*, int) = nullptr;
1985
1986 bool opaque = (fShaderContext->getFlags() & SkShaderBase::kOpaqueAlpha_Flag);
1987
1988 if (mask.fFormat == SkMask::kA8_Format && opaque) {
1989 blend_row = blend_row_A8_opaque;
1990 } else if (mask.fFormat == SkMask::kA8_Format) {
1991 blend_row = blend_row_A8;
1992 } else if (mask.fFormat == SkMask::kLCD16_Format && opaque) {
1993 blend_row = blend_row_LCD16_opaque;
1994 } else if (mask.fFormat == SkMask::kLCD16_Format) {
1995 blend_row = blend_row_lcd16;
1996 } else {
1997 this->SkShaderBlitter::blitMask(mask, clip);
1998 return;
1999 }
2000
2001 const int x = clip.fLeft;
2002 const int width = clip.width();
2003 int y = clip.fTop;
2004 int height = clip.height();
2005
2006 char* dstRow = (char*)fDevice.writable_addr32(x, y);
2007 const size_t dstRB = fDevice.rowBytes();
2008 const uint8_t* maskRow = (const uint8_t*)mask.getAddr(x, y);
2009 const size_t maskRB = mask.fRowBytes;
2010
2011 SkPMColor* span = fBuffer;
2012 SkASSERT(blend_row);
2013 do {
2014 fShaderContext->shadeSpan(x, y, span, width);
2015 blend_row(reinterpret_cast<SkPMColor*>(dstRow), maskRow, span, width);
2016 dstRow += dstRB;
2017 maskRow += maskRB;
2018 y += 1;
2019 } while (--height > 0);
2020 }
2021
2022 void SkARGB32_Shader_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
2023 SkASSERT(x >= 0 && y >= 0 && y + height <= fDevice.height());
2024
2025 uint32_t* device = fDevice.writable_addr32(x, y);
2026 const size_t deviceRB = fDevice.rowBytes();
2027
2028 if (fShadeDirectlyIntoDevice) {
2029 if (255 == alpha) {
2030 do {
2031 fShaderContext->shadeSpan(x, y, device, 1);
2032 y += 1;
2033 device = (uint32_t*)((char*)device + deviceRB);
2034 } while (--height > 0);
2035 } else {
2036 do {
2037 SkPMColor c;
2038 fShaderContext->shadeSpan(x, y, &c, 1);
2039 *device = SkFourByteInterp(c, *device, alpha);
2040 y += 1;
2041 device = (uint32_t*)((char*)device + deviceRB);
2042 } while (--height > 0);
2043 }
2044 } else {
2045 SkPMColor* span = fBuffer;
2046 SkBlitRow::Proc32 proc = (255 == alpha) ? fProc32 : fProc32Blend;
2047 do {
2048 fShaderContext->shadeSpan(x, y, span, 1);
2049 proc(device, span, 1, alpha);
2050 y += 1;
2051 device = (uint32_t*)((char*)device + deviceRB);
2052 } while (--height > 0);
2053 }
2054 }