2 * Copyright 2012 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapProcState_opts_SSE2.h"
10 #include "SkBlitRow_opts_SSE2.h"
11 #include "SkColorPriv.h"
12 #include "SkColor_opts_SSE2.h"
16 /* SSE2 version of S32_Blend_BlitRow32()
17 * portable version is in core/SkBlitRow_D32.cpp
19 void S32_Blend_BlitRow32_SSE2(SkPMColor* SK_RESTRICT dst,
20 const SkPMColor* SK_RESTRICT src,
21 int count, U8CPU alpha) {
22 SkASSERT(alpha <= 255);
27 uint32_t src_scale = SkAlpha255To256(alpha);
28 uint32_t dst_scale = 256 - src_scale;
31 SkASSERT(((size_t)dst & 0x03) == 0);
32 while (((size_t)dst & 0x0F) != 0) {
33 *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale);
39 const __m128i *s = reinterpret_cast<const __m128i*>(src);
40 __m128i *d = reinterpret_cast<__m128i*>(dst);
41 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
42 __m128i ag_mask = _mm_set1_epi32(0xFF00FF00);
44 // Move scale factors to upper byte of word
45 __m128i src_scale_wide = _mm_set1_epi16(src_scale << 8);
46 __m128i dst_scale_wide = _mm_set1_epi16(dst_scale << 8);
48 // Load 4 pixels each of src and dest.
49 __m128i src_pixel = _mm_loadu_si128(s);
50 __m128i dst_pixel = _mm_load_si128(d);
52 // Interleave Atom port 0/1 operations based on the execution port
53 // constraints that multiply can only be executed on port 0 (while
54 // boolean operations can be executed on either port 0 or port 1)
55 // because GCC currently doesn't do a good job scheduling
56 // instructions based on these constraints.
58 // Get red and blue pixels into lower byte of each word.
59 // (0, r, 0, b, 0, r, 0, b, 0, r, 0, b, 0, r, 0, b)
60 __m128i src_rb = _mm_and_si128(rb_mask, src_pixel);
63 // (4 x (0, rs.h, 0, bs.h))
64 // where rs.h stands for the higher byte of r * scale, and
65 // bs.h the higher byte of b * scale.
66 src_rb = _mm_mulhi_epu16(src_rb, src_scale_wide);
68 // Get alpha and green pixels into higher byte of each word.
69 // (a, 0, g, 0, a, 0, g, 0, a, 0, g, 0, a, 0, g, 0)
70 __m128i src_ag = _mm_and_si128(ag_mask, src_pixel);
73 // (4 x (as.h, as.l, gs.h, gs.l))
74 src_ag = _mm_mulhi_epu16(src_ag, src_scale_wide);
76 // Clear the lower byte of the a*scale and g*scale results
77 // (4 x (as.h, 0, gs.h, 0))
78 src_ag = _mm_and_si128(src_ag, ag_mask);
80 // Operations the destination pixels are the same as on the
81 // source pixels. See the comments above.
82 __m128i dst_rb = _mm_and_si128(rb_mask, dst_pixel);
83 dst_rb = _mm_mulhi_epu16(dst_rb, dst_scale_wide);
84 __m128i dst_ag = _mm_and_si128(ag_mask, dst_pixel);
85 dst_ag = _mm_mulhi_epu16(dst_ag, dst_scale_wide);
86 dst_ag = _mm_and_si128(dst_ag, ag_mask);
88 // Combine back into RGBA.
89 // (4 x (as.h, rs.h, gs.h, bs.h))
90 src_pixel = _mm_or_si128(src_rb, src_ag);
91 dst_pixel = _mm_or_si128(dst_rb, dst_ag);
94 __m128i result = _mm_add_epi8(src_pixel, dst_pixel);
95 _mm_store_si128(d, result);
100 src = reinterpret_cast<const SkPMColor*>(s);
101 dst = reinterpret_cast<SkPMColor*>(d);
105 *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale);
112 void S32A_Opaque_BlitRow32_SSE2(SkPMColor* SK_RESTRICT dst,
113 const SkPMColor* SK_RESTRICT src,
114 int count, U8CPU alpha) {
115 SkASSERT(alpha == 255);
121 SkASSERT(((size_t)dst & 0x03) == 0);
122 while (((size_t)dst & 0x0F) != 0) {
123 *dst = SkPMSrcOver(*src, *dst);
129 const __m128i *s = reinterpret_cast<const __m128i*>(src);
130 __m128i *d = reinterpret_cast<__m128i*>(dst);
131 #ifdef SK_USE_ACCURATE_BLENDING
132 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
133 __m128i c_128 = _mm_set1_epi16(128); // 8 copies of 128 (16-bit)
134 __m128i c_255 = _mm_set1_epi16(255); // 8 copies of 255 (16-bit)
137 __m128i src_pixel = _mm_loadu_si128(s);
138 __m128i dst_pixel = _mm_load_si128(d);
140 __m128i dst_rb = _mm_and_si128(rb_mask, dst_pixel);
141 __m128i dst_ag = _mm_srli_epi16(dst_pixel, 8);
142 // Shift alphas down to lower 8 bits of each quad.
143 __m128i alpha = _mm_srli_epi32(src_pixel, 24);
145 // Copy alpha to upper 3rd byte of each quad
146 alpha = _mm_or_si128(alpha, _mm_slli_epi32(alpha, 16));
148 // Subtract alphas from 255, to get 0..255
149 alpha = _mm_sub_epi16(c_255, alpha);
151 // Multiply by red and blue by src alpha.
152 dst_rb = _mm_mullo_epi16(dst_rb, alpha);
153 // Multiply by alpha and green by src alpha.
154 dst_ag = _mm_mullo_epi16(dst_ag, alpha);
156 // dst_rb_low = (dst_rb >> 8)
157 __m128i dst_rb_low = _mm_srli_epi16(dst_rb, 8);
158 __m128i dst_ag_low = _mm_srli_epi16(dst_ag, 8);
160 // dst_rb = (dst_rb + dst_rb_low + 128) >> 8
161 dst_rb = _mm_add_epi16(dst_rb, dst_rb_low);
162 dst_rb = _mm_add_epi16(dst_rb, c_128);
163 dst_rb = _mm_srli_epi16(dst_rb, 8);
165 // dst_ag = (dst_ag + dst_ag_low + 128) & ag_mask
166 dst_ag = _mm_add_epi16(dst_ag, dst_ag_low);
167 dst_ag = _mm_add_epi16(dst_ag, c_128);
168 dst_ag = _mm_andnot_si128(rb_mask, dst_ag);
170 // Combine back into RGBA.
171 dst_pixel = _mm_or_si128(dst_rb, dst_ag);
174 __m128i result = _mm_add_epi8(src_pixel, dst_pixel);
175 _mm_store_si128(d, result);
181 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
182 __m128i c_256 = _mm_set1_epi16(0x0100); // 8 copies of 256 (16-bit)
185 __m128i src_pixel = _mm_loadu_si128(s);
186 __m128i dst_pixel = _mm_load_si128(d);
188 __m128i dst_rb = _mm_and_si128(rb_mask, dst_pixel);
189 __m128i dst_ag = _mm_srli_epi16(dst_pixel, 8);
191 // (a0, g0, a1, g1, a2, g2, a3, g3) (low byte of each word)
192 __m128i alpha = _mm_srli_epi16(src_pixel, 8);
194 // (a0, a0, a1, a1, a2, g2, a3, g3)
195 alpha = _mm_shufflehi_epi16(alpha, 0xF5);
197 // (a0, a0, a1, a1, a2, a2, a3, a3)
198 alpha = _mm_shufflelo_epi16(alpha, 0xF5);
200 // Subtract alphas from 256, to get 1..256
201 alpha = _mm_sub_epi16(c_256, alpha);
203 // Multiply by red and blue by src alpha.
204 dst_rb = _mm_mullo_epi16(dst_rb, alpha);
205 // Multiply by alpha and green by src alpha.
206 dst_ag = _mm_mullo_epi16(dst_ag, alpha);
209 dst_rb = _mm_srli_epi16(dst_rb, 8);
211 // Mask out high bits (already in the right place)
212 dst_ag = _mm_andnot_si128(rb_mask, dst_ag);
214 // Combine back into RGBA.
215 dst_pixel = _mm_or_si128(dst_rb, dst_ag);
218 __m128i result = _mm_add_epi8(src_pixel, dst_pixel);
219 _mm_store_si128(d, result);
225 src = reinterpret_cast<const SkPMColor*>(s);
226 dst = reinterpret_cast<SkPMColor*>(d);
230 *dst = SkPMSrcOver(*src, *dst);
237 void S32A_Blend_BlitRow32_SSE2(SkPMColor* SK_RESTRICT dst,
238 const SkPMColor* SK_RESTRICT src,
239 int count, U8CPU alpha) {
240 SkASSERT(alpha <= 255);
246 while (((size_t)dst & 0x0F) != 0) {
247 *dst = SkBlendARGB32(*src, *dst, alpha);
253 uint32_t src_scale = SkAlpha255To256(alpha);
255 const __m128i *s = reinterpret_cast<const __m128i*>(src);
256 __m128i *d = reinterpret_cast<__m128i*>(dst);
257 __m128i src_scale_wide = _mm_set1_epi16(src_scale << 8);
258 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
259 __m128i c_256 = _mm_set1_epi16(256); // 8 copies of 256 (16-bit)
261 // Load 4 pixels each of src and dest.
262 __m128i src_pixel = _mm_loadu_si128(s);
263 __m128i dst_pixel = _mm_load_si128(d);
265 // Get red and blue pixels into lower byte of each word.
266 __m128i dst_rb = _mm_and_si128(rb_mask, dst_pixel);
267 __m128i src_rb = _mm_and_si128(rb_mask, src_pixel);
269 // Get alpha and green into lower byte of each word.
270 __m128i dst_ag = _mm_srli_epi16(dst_pixel, 8);
271 __m128i src_ag = _mm_srli_epi16(src_pixel, 8);
273 // Put per-pixel alpha in low byte of each word.
274 // After the following two statements, the dst_alpha looks like
275 // (0, a0, 0, a0, 0, a1, 0, a1, 0, a2, 0, a2, 0, a3, 0, a3)
276 __m128i dst_alpha = _mm_shufflehi_epi16(src_ag, 0xF5);
277 dst_alpha = _mm_shufflelo_epi16(dst_alpha, 0xF5);
279 // dst_alpha = dst_alpha * src_scale
280 // Because src_scales are in the higher byte of each word and
281 // we use mulhi here, the resulting alpha values are already
282 // in the right place and don't need to be divided by 256.
283 // (0, sa0, 0, sa0, 0, sa1, 0, sa1, 0, sa2, 0, sa2, 0, sa3, 0, sa3)
284 dst_alpha = _mm_mulhi_epu16(dst_alpha, src_scale_wide);
286 // Subtract alphas from 256, to get 1..256
287 dst_alpha = _mm_sub_epi16(c_256, dst_alpha);
289 // Multiply red and blue by dst pixel alpha.
290 dst_rb = _mm_mullo_epi16(dst_rb, dst_alpha);
291 // Multiply alpha and green by dst pixel alpha.
292 dst_ag = _mm_mullo_epi16(dst_ag, dst_alpha);
294 // Multiply red and blue by global alpha.
295 // (4 x (0, rs.h, 0, bs.h))
296 // where rs.h stands for the higher byte of r * src_scale,
297 // and bs.h the higher byte of b * src_scale.
298 // Again, because we use mulhi, the resuling red and blue
299 // values are already in the right place and don't need to
300 // be divided by 256.
301 src_rb = _mm_mulhi_epu16(src_rb, src_scale_wide);
302 // Multiply alpha and green by global alpha.
303 // (4 x (0, as.h, 0, gs.h))
304 src_ag = _mm_mulhi_epu16(src_ag, src_scale_wide);
307 dst_rb = _mm_srli_epi16(dst_rb, 8);
309 // Mask out low bits (goodies already in the right place; no need to divide)
310 dst_ag = _mm_andnot_si128(rb_mask, dst_ag);
311 // Shift alpha and green to higher byte of each word.
312 // (4 x (as.h, 0, gs.h, 0))
313 src_ag = _mm_slli_epi16(src_ag, 8);
315 // Combine back into RGBA.
316 dst_pixel = _mm_or_si128(dst_rb, dst_ag);
317 src_pixel = _mm_or_si128(src_rb, src_ag);
319 // Add two pixels into result.
320 __m128i result = _mm_add_epi8(src_pixel, dst_pixel);
321 _mm_store_si128(d, result);
326 src = reinterpret_cast<const SkPMColor*>(s);
327 dst = reinterpret_cast<SkPMColor*>(d);
331 *dst = SkBlendARGB32(*src, *dst, alpha);
338 /* SSE2 version of Color32()
339 * portable version is in core/SkBlitRow_D32.cpp
341 void Color32_SSE2(SkPMColor dst[], const SkPMColor src[], int count,
349 memcpy(dst, src, count * sizeof(SkPMColor));
354 unsigned colorA = SkGetPackedA32(color);
356 sk_memset32(dst, color, count);
358 unsigned scale = 256 - SkAlpha255To256(colorA);
361 SkASSERT(((size_t)dst & 0x03) == 0);
362 while (((size_t)dst & 0x0F) != 0) {
363 *dst = color + SkAlphaMulQ(*src, scale);
369 const __m128i *s = reinterpret_cast<const __m128i*>(src);
370 __m128i *d = reinterpret_cast<__m128i*>(dst);
371 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
372 __m128i src_scale_wide = _mm_set1_epi16(scale);
373 __m128i color_wide = _mm_set1_epi32(color);
375 // Load 4 pixels each of src and dest.
376 __m128i src_pixel = _mm_loadu_si128(s);
378 // Get red and blue pixels into lower byte of each word.
379 __m128i src_rb = _mm_and_si128(rb_mask, src_pixel);
381 // Get alpha and green into lower byte of each word.
382 __m128i src_ag = _mm_srli_epi16(src_pixel, 8);
384 // Multiply by scale.
385 src_rb = _mm_mullo_epi16(src_rb, src_scale_wide);
386 src_ag = _mm_mullo_epi16(src_ag, src_scale_wide);
389 src_rb = _mm_srli_epi16(src_rb, 8);
390 src_ag = _mm_andnot_si128(rb_mask, src_ag);
392 // Combine back into RGBA.
393 src_pixel = _mm_or_si128(src_rb, src_ag);
395 // Add color to result.
396 __m128i result = _mm_add_epi8(color_wide, src_pixel);
399 _mm_store_si128(d, result);
404 src = reinterpret_cast<const SkPMColor*>(s);
405 dst = reinterpret_cast<SkPMColor*>(d);
409 *dst = color + SkAlphaMulQ(*src, scale);
417 void SkARGB32_A8_BlitMask_SSE2(void* device, size_t dstRB, const void* maskPtr,
418 size_t maskRB, SkColor origColor,
419 int width, int height) {
420 SkPMColor color = SkPreMultiplyColor(origColor);
421 size_t dstOffset = dstRB - (width << 2);
422 size_t maskOffset = maskRB - width;
423 SkPMColor* dst = (SkPMColor *)device;
424 const uint8_t* mask = (const uint8_t*)maskPtr;
428 while (((size_t)dst & 0x0F) != 0 && (count > 0)) {
429 *dst = SkBlendARGB32(color, *dst, *mask);
434 __m128i *d = reinterpret_cast<__m128i*>(dst);
435 __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
436 __m128i c_256 = _mm_set1_epi16(256);
437 __m128i c_1 = _mm_set1_epi16(1);
438 __m128i src_pixel = _mm_set1_epi32(color);
440 // Load 4 pixels each of src and dest.
441 __m128i dst_pixel = _mm_load_si128(d);
443 //set the aphla value
444 __m128i src_scale_wide = _mm_set_epi8(0, *(mask+3),\
446 *(mask+2),0, *(mask+2),\
447 0,*(mask+1), 0,*(mask+1),\
450 //call SkAlpha255To256()
451 src_scale_wide = _mm_add_epi16(src_scale_wide, c_1);
453 // Get red and blue pixels into lower byte of each word.
454 __m128i dst_rb = _mm_and_si128(rb_mask, dst_pixel);
455 __m128i src_rb = _mm_and_si128(rb_mask, src_pixel);
457 // Get alpha and green into lower byte of each word.
458 __m128i dst_ag = _mm_srli_epi16(dst_pixel, 8);
459 __m128i src_ag = _mm_srli_epi16(src_pixel, 8);
461 // Put per-pixel alpha in low byte of each word.
462 __m128i dst_alpha = _mm_shufflehi_epi16(src_ag, 0xF5);
463 dst_alpha = _mm_shufflelo_epi16(dst_alpha, 0xF5);
465 // dst_alpha = dst_alpha * src_scale
466 dst_alpha = _mm_mullo_epi16(dst_alpha, src_scale_wide);
469 dst_alpha = _mm_srli_epi16(dst_alpha, 8);
471 // Subtract alphas from 256, to get 1..256
472 dst_alpha = _mm_sub_epi16(c_256, dst_alpha);
473 // Multiply red and blue by dst pixel alpha.
474 dst_rb = _mm_mullo_epi16(dst_rb, dst_alpha);
475 // Multiply alpha and green by dst pixel alpha.
476 dst_ag = _mm_mullo_epi16(dst_ag, dst_alpha);
478 // Multiply red and blue by global alpha.
479 src_rb = _mm_mullo_epi16(src_rb, src_scale_wide);
480 // Multiply alpha and green by global alpha.
481 src_ag = _mm_mullo_epi16(src_ag, src_scale_wide);
483 dst_rb = _mm_srli_epi16(dst_rb, 8);
484 src_rb = _mm_srli_epi16(src_rb, 8);
486 // Mask out low bits (goodies already in the right place; no need to divide)
487 dst_ag = _mm_andnot_si128(rb_mask, dst_ag);
488 src_ag = _mm_andnot_si128(rb_mask, src_ag);
490 // Combine back into RGBA.
491 dst_pixel = _mm_or_si128(dst_rb, dst_ag);
492 __m128i tmp_src_pixel = _mm_or_si128(src_rb, src_ag);
494 // Add two pixels into result.
495 __m128i result = _mm_add_epi8(tmp_src_pixel, dst_pixel);
496 _mm_store_si128(d, result);
497 // load the next 4 pixel
502 dst = reinterpret_cast<SkPMColor *>(d);
505 *dst= SkBlendARGB32(color, *dst, *mask);
510 dst = (SkPMColor *)((char*)dst + dstOffset);
512 } while (--height != 0);
515 // The following (left) shifts cause the top 5 bits of the mask components to
516 // line up with the corresponding components in an SkPMColor.
517 // Note that the mask's RGB16 order may differ from the SkPMColor order.
518 #define SK_R16x5_R32x5_SHIFT (SK_R32_SHIFT - SK_R16_SHIFT - SK_R16_BITS + 5)
519 #define SK_G16x5_G32x5_SHIFT (SK_G32_SHIFT - SK_G16_SHIFT - SK_G16_BITS + 5)
520 #define SK_B16x5_B32x5_SHIFT (SK_B32_SHIFT - SK_B16_SHIFT - SK_B16_BITS + 5)
522 #if SK_R16x5_R32x5_SHIFT == 0
523 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (x)
524 #elif SK_R16x5_R32x5_SHIFT > 0
525 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (_mm_slli_epi32(x, SK_R16x5_R32x5_SHIFT))
527 #define SkPackedR16x5ToUnmaskedR32x5_SSE2(x) (_mm_srli_epi32(x, -SK_R16x5_R32x5_SHIFT))
530 #if SK_G16x5_G32x5_SHIFT == 0
531 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (x)
532 #elif SK_G16x5_G32x5_SHIFT > 0
533 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (_mm_slli_epi32(x, SK_G16x5_G32x5_SHIFT))
535 #define SkPackedG16x5ToUnmaskedG32x5_SSE2(x) (_mm_srli_epi32(x, -SK_G16x5_G32x5_SHIFT))
538 #if SK_B16x5_B32x5_SHIFT == 0
539 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (x)
540 #elif SK_B16x5_B32x5_SHIFT > 0
541 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (_mm_slli_epi32(x, SK_B16x5_B32x5_SHIFT))
543 #define SkPackedB16x5ToUnmaskedB32x5_SSE2(x) (_mm_srli_epi32(x, -SK_B16x5_B32x5_SHIFT))
546 static __m128i SkBlendLCD16_SSE2(__m128i &src, __m128i &dst,
547 __m128i &mask, __m128i &srcA) {
548 // In the following comments, the components of src, dst and mask are
549 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
550 // by an R, G, B, or A suffix. Components of one of the four pixels that
551 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
552 // example is the blue channel of the second destination pixel. Memory
553 // layout is shown for an ARGB byte order in a color value.
555 // src and srcA store 8-bit values interleaved with zeros.
556 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
557 // srcA = (srcA, 0, srcA, 0, srcA, 0, srcA, 0,
558 // srcA, 0, srcA, 0, srcA, 0, srcA, 0)
559 // mask stores 16-bit values (compressed three channels) interleaved with zeros.
560 // Lo and Hi denote the low and high bytes of a 16-bit value, respectively.
561 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
562 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
564 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
565 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
566 __m128i r = _mm_and_si128(SkPackedR16x5ToUnmaskedR32x5_SSE2(mask),
567 _mm_set1_epi32(0x1F << SK_R32_SHIFT));
569 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
570 __m128i g = _mm_and_si128(SkPackedG16x5ToUnmaskedG32x5_SSE2(mask),
571 _mm_set1_epi32(0x1F << SK_G32_SHIFT));
573 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
574 __m128i b = _mm_and_si128(SkPackedB16x5ToUnmaskedB32x5_SSE2(mask),
575 _mm_set1_epi32(0x1F << SK_B32_SHIFT));
577 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
578 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
580 // mask = (0, m0R, m0G, m0B, 0, m1R, m1G, m1B,
581 // 0, m2R, m2G, m2B, 0, m3R, m3G, m3B)
582 mask = _mm_or_si128(_mm_or_si128(r, g), b);
584 // Interleave R,G,B into the lower byte of word.
585 // i.e. split the sixteen 8-bit values from mask into two sets of eight
586 // 16-bit values, padded by zero.
587 __m128i maskLo, maskHi;
588 // maskLo = (0, 0, m0R, 0, m0G, 0, m0B, 0, 0, 0, m1R, 0, m1G, 0, m1B, 0)
589 maskLo = _mm_unpacklo_epi8(mask, _mm_setzero_si128());
590 // maskHi = (0, 0, m2R, 0, m2G, 0, m2B, 0, 0, 0, m3R, 0, m3G, 0, m3B, 0)
591 maskHi = _mm_unpackhi_epi8(mask, _mm_setzero_si128());
593 // Upscale from 0..31 to 0..32
594 // (allows to replace division by left-shift further down)
595 // Left-shift each component by 4 and add the result back to that component,
596 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
597 maskLo = _mm_add_epi16(maskLo, _mm_srli_epi16(maskLo, 4));
598 maskHi = _mm_add_epi16(maskHi, _mm_srli_epi16(maskHi, 4));
600 // Multiply each component of maskLo and maskHi by srcA
601 maskLo = _mm_mullo_epi16(maskLo, srcA);
602 maskHi = _mm_mullo_epi16(maskHi, srcA);
604 // Left shift mask components by 8 (divide by 256)
605 maskLo = _mm_srli_epi16(maskLo, 8);
606 maskHi = _mm_srli_epi16(maskHi, 8);
608 // Interleave R,G,B into the lower byte of the word
609 // dstLo = (0, 0, d0R, 0, d0G, 0, d0B, 0, 0, 0, d1R, 0, d1G, 0, d1B, 0)
610 __m128i dstLo = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
611 // dstLo = (0, 0, d2R, 0, d2G, 0, d2B, 0, 0, 0, d3R, 0, d3G, 0, d3B, 0)
612 __m128i dstHi = _mm_unpackhi_epi8(dst, _mm_setzero_si128());
614 // mask = (src - dst) * mask
615 maskLo = _mm_mullo_epi16(maskLo, _mm_sub_epi16(src, dstLo));
616 maskHi = _mm_mullo_epi16(maskHi, _mm_sub_epi16(src, dstHi));
618 // mask = (src - dst) * mask >> 5
619 maskLo = _mm_srai_epi16(maskLo, 5);
620 maskHi = _mm_srai_epi16(maskHi, 5);
622 // Add two pixels into result.
623 // result = dst + ((src - dst) * mask >> 5)
624 __m128i resultLo = _mm_add_epi16(dstLo, maskLo);
625 __m128i resultHi = _mm_add_epi16(dstHi, maskHi);
627 // Pack into 4 32bit dst pixels.
628 // resultLo and resultHi contain eight 16-bit components (two pixels) each.
629 // Merge into one SSE regsiter with sixteen 8-bit values (four pixels),
630 // clamping to 255 if necessary.
631 return _mm_packus_epi16(resultLo, resultHi);
634 static __m128i SkBlendLCD16Opaque_SSE2(__m128i &src, __m128i &dst,
636 // In the following comments, the components of src, dst and mask are
637 // abbreviated as (s)rc, (d)st, and (m)ask. Color components are marked
638 // by an R, G, B, or A suffix. Components of one of the four pixels that
639 // are processed in parallel are marked with 0, 1, 2, and 3. "d1B", for
640 // example is the blue channel of the second destination pixel. Memory
641 // layout is shown for an ARGB byte order in a color value.
643 // src and srcA store 8-bit values interleaved with zeros.
644 // src = (0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
645 // mask stores 16-bit values (shown as high and low bytes) interleaved with
647 // mask = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
648 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
650 // Get the R,G,B of each 16bit mask pixel, we want all of them in 5 bits.
651 // r = (0, m0R, 0, 0, 0, m1R, 0, 0, 0, m2R, 0, 0, 0, m3R, 0, 0)
652 __m128i r = _mm_and_si128(SkPackedR16x5ToUnmaskedR32x5_SSE2(mask),
653 _mm_set1_epi32(0x1F << SK_R32_SHIFT));
655 // g = (0, 0, m0G, 0, 0, 0, m1G, 0, 0, 0, m2G, 0, 0, 0, m3G, 0)
656 __m128i g = _mm_and_si128(SkPackedG16x5ToUnmaskedG32x5_SSE2(mask),
657 _mm_set1_epi32(0x1F << SK_G32_SHIFT));
659 // b = (0, 0, 0, m0B, 0, 0, 0, m1B, 0, 0, 0, m2B, 0, 0, 0, m3B)
660 __m128i b = _mm_and_si128(SkPackedB16x5ToUnmaskedB32x5_SSE2(mask),
661 _mm_set1_epi32(0x1F << SK_B32_SHIFT));
663 // Pack the 4 16bit mask pixels into 4 32bit pixels, (p0, p1, p2, p3)
664 // Each component (m0R, m0G, etc.) is then a 5-bit value aligned to an
666 // mask = (0, m0R, m0G, m0B, 0, m1R, m1G, m1B,
667 // 0, m2R, m2G, m2B, 0, m3R, m3G, m3B)
668 mask = _mm_or_si128(_mm_or_si128(r, g), b);
670 // Interleave R,G,B into the lower byte of word.
671 // i.e. split the sixteen 8-bit values from mask into two sets of eight
672 // 16-bit values, padded by zero.
673 __m128i maskLo, maskHi;
674 // maskLo = (0, 0, m0R, 0, m0G, 0, m0B, 0, 0, 0, m1R, 0, m1G, 0, m1B, 0)
675 maskLo = _mm_unpacklo_epi8(mask, _mm_setzero_si128());
676 // maskHi = (0, 0, m2R, 0, m2G, 0, m2B, 0, 0, 0, m3R, 0, m3G, 0, m3B, 0)
677 maskHi = _mm_unpackhi_epi8(mask, _mm_setzero_si128());
679 // Upscale from 0..31 to 0..32
680 // (allows to replace division by left-shift further down)
681 // Left-shift each component by 4 and add the result back to that component,
682 // mapping numbers in the range 0..15 to 0..15, and 16..31 to 17..32
683 maskLo = _mm_add_epi16(maskLo, _mm_srli_epi16(maskLo, 4));
684 maskHi = _mm_add_epi16(maskHi, _mm_srli_epi16(maskHi, 4));
686 // Interleave R,G,B into the lower byte of the word
687 // dstLo = (0, 0, d0R, 0, d0G, 0, d0B, 0, 0, 0, d1R, 0, d1G, 0, d1B, 0)
688 __m128i dstLo = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
689 // dstLo = (0, 0, d2R, 0, d2G, 0, d2B, 0, 0, 0, d3R, 0, d3G, 0, d3B, 0)
690 __m128i dstHi = _mm_unpackhi_epi8(dst, _mm_setzero_si128());
692 // mask = (src - dst) * mask
693 maskLo = _mm_mullo_epi16(maskLo, _mm_sub_epi16(src, dstLo));
694 maskHi = _mm_mullo_epi16(maskHi, _mm_sub_epi16(src, dstHi));
696 // mask = (src - dst) * mask >> 5
697 maskLo = _mm_srai_epi16(maskLo, 5);
698 maskHi = _mm_srai_epi16(maskHi, 5);
700 // Add two pixels into result.
701 // result = dst + ((src - dst) * mask >> 5)
702 __m128i resultLo = _mm_add_epi16(dstLo, maskLo);
703 __m128i resultHi = _mm_add_epi16(dstHi, maskHi);
705 // Pack into 4 32bit dst pixels and force opaque.
706 // resultLo and resultHi contain eight 16-bit components (two pixels) each.
707 // Merge into one SSE regsiter with sixteen 8-bit values (four pixels),
708 // clamping to 255 if necessary. Set alpha components to 0xFF.
709 return _mm_or_si128(_mm_packus_epi16(resultLo, resultHi),
710 _mm_set1_epi32(SK_A32_MASK << SK_A32_SHIFT));
713 void SkBlitLCD16Row_SSE2(SkPMColor dst[], const uint16_t mask[],
714 SkColor src, int width, SkPMColor) {
719 int srcA = SkColorGetA(src);
720 int srcR = SkColorGetR(src);
721 int srcG = SkColorGetG(src);
722 int srcB = SkColorGetB(src);
724 srcA = SkAlpha255To256(srcA);
727 SkASSERT(((size_t)dst & 0x03) == 0);
728 while (((size_t)dst & 0x0F) != 0) {
729 *dst = SkBlendLCD16(srcA, srcR, srcG, srcB, *dst, *mask);
735 __m128i *d = reinterpret_cast<__m128i*>(dst);
736 // Set alpha to 0xFF and replicate source four times in SSE register.
737 __m128i src_sse = _mm_set1_epi32(SkPackARGB32(0xFF, srcR, srcG, srcB));
738 // Interleave with zeros to get two sets of four 16-bit values.
739 src_sse = _mm_unpacklo_epi8(src_sse, _mm_setzero_si128());
740 // Set srcA_sse to contain eight copies of srcA, padded with zero.
741 // src_sse=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
742 __m128i srcA_sse = _mm_set1_epi16(srcA);
744 // Load four destination pixels into dst_sse.
745 __m128i dst_sse = _mm_load_si128(d);
746 // Load four 16-bit masks into lower half of mask_sse.
747 __m128i mask_sse = _mm_loadl_epi64(
748 reinterpret_cast<const __m128i*>(mask));
750 // Check whether masks are equal to 0 and get the highest bit
751 // of each byte of result, if masks are all zero, we will get
752 // pack_cmp to 0xFFFF
753 int pack_cmp = _mm_movemask_epi8(_mm_cmpeq_epi16(mask_sse,
754 _mm_setzero_si128()));
756 // if mask pixels are not all zero, we will blend the dst pixels
757 if (pack_cmp != 0xFFFF) {
758 // Unpack 4 16bit mask pixels to
759 // mask_sse = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
760 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
761 mask_sse = _mm_unpacklo_epi16(mask_sse,
762 _mm_setzero_si128());
764 // Process 4 32bit dst pixels
765 __m128i result = SkBlendLCD16_SSE2(src_sse, dst_sse,
767 _mm_store_si128(d, result);
775 dst = reinterpret_cast<SkPMColor*>(d);
779 *dst = SkBlendLCD16(srcA, srcR, srcG, srcB, *dst, *mask);
786 void SkBlitLCD16OpaqueRow_SSE2(SkPMColor dst[], const uint16_t mask[],
787 SkColor src, int width, SkPMColor opaqueDst) {
792 int srcR = SkColorGetR(src);
793 int srcG = SkColorGetG(src);
794 int srcB = SkColorGetB(src);
797 SkASSERT(((size_t)dst & 0x03) == 0);
798 while (((size_t)dst & 0x0F) != 0) {
799 *dst = SkBlendLCD16Opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
805 __m128i *d = reinterpret_cast<__m128i*>(dst);
806 // Set alpha to 0xFF and replicate source four times in SSE register.
807 __m128i src_sse = _mm_set1_epi32(SkPackARGB32(0xFF, srcR, srcG, srcB));
808 // Set srcA_sse to contain eight copies of srcA, padded with zero.
809 // src_sse=(0xFF, 0, sR, 0, sG, 0, sB, 0, 0xFF, 0, sR, 0, sG, 0, sB, 0)
810 src_sse = _mm_unpacklo_epi8(src_sse, _mm_setzero_si128());
812 // Load four destination pixels into dst_sse.
813 __m128i dst_sse = _mm_load_si128(d);
814 // Load four 16-bit masks into lower half of mask_sse.
815 __m128i mask_sse = _mm_loadl_epi64(
816 reinterpret_cast<const __m128i*>(mask));
818 // Check whether masks are equal to 0 and get the highest bit
819 // of each byte of result, if masks are all zero, we will get
820 // pack_cmp to 0xFFFF
821 int pack_cmp = _mm_movemask_epi8(_mm_cmpeq_epi16(mask_sse,
822 _mm_setzero_si128()));
824 // if mask pixels are not all zero, we will blend the dst pixels
825 if (pack_cmp != 0xFFFF) {
826 // Unpack 4 16bit mask pixels to
827 // mask_sse = (m0RGBLo, m0RGBHi, 0, 0, m1RGBLo, m1RGBHi, 0, 0,
828 // m2RGBLo, m2RGBHi, 0, 0, m3RGBLo, m3RGBHi, 0, 0)
829 mask_sse = _mm_unpacklo_epi16(mask_sse,
830 _mm_setzero_si128());
832 // Process 4 32bit dst pixels
833 __m128i result = SkBlendLCD16Opaque_SSE2(src_sse, dst_sse,
835 _mm_store_si128(d, result);
843 dst = reinterpret_cast<SkPMColor*>(d);
847 *dst = SkBlendLCD16Opaque(srcR, srcG, srcB, *dst, *mask, opaqueDst);
854 /* SSE2 version of S32_D565_Opaque()
855 * portable version is in core/SkBlitRow_D16.cpp
857 void S32_D565_Opaque_SSE2(uint16_t* SK_RESTRICT dst,
858 const SkPMColor* SK_RESTRICT src, int count,
859 U8CPU alpha, int /*x*/, int /*y*/) {
860 SkASSERT(255 == alpha);
867 while (((size_t)dst & 0x0F) != 0) {
868 SkPMColor c = *src++;
871 *dst++ = SkPixel32ToPixel16_ToU16(c);
875 const __m128i* s = reinterpret_cast<const __m128i*>(src);
876 __m128i* d = reinterpret_cast<__m128i*>(dst);
877 __m128i r16_mask = _mm_set1_epi32(SK_R16_MASK);
878 __m128i g16_mask = _mm_set1_epi32(SK_G16_MASK);
879 __m128i b16_mask = _mm_set1_epi32(SK_B16_MASK);
882 // Load 8 pixels of src.
883 __m128i src_pixel1 = _mm_loadu_si128(s++);
884 __m128i src_pixel2 = _mm_loadu_si128(s++);
886 // Calculate result r.
887 __m128i r1 = _mm_srli_epi32(src_pixel1,
888 SK_R32_SHIFT + (8 - SK_R16_BITS));
889 r1 = _mm_and_si128(r1, r16_mask);
890 __m128i r2 = _mm_srli_epi32(src_pixel2,
891 SK_R32_SHIFT + (8 - SK_R16_BITS));
892 r2 = _mm_and_si128(r2, r16_mask);
893 __m128i r = _mm_packs_epi32(r1, r2);
895 // Calculate result g.
896 __m128i g1 = _mm_srli_epi32(src_pixel1,
897 SK_G32_SHIFT + (8 - SK_G16_BITS));
898 g1 = _mm_and_si128(g1, g16_mask);
899 __m128i g2 = _mm_srli_epi32(src_pixel2,
900 SK_G32_SHIFT + (8 - SK_G16_BITS));
901 g2 = _mm_and_si128(g2, g16_mask);
902 __m128i g = _mm_packs_epi32(g1, g2);
904 // Calculate result b.
905 __m128i b1 = _mm_srli_epi32(src_pixel1,
906 SK_B32_SHIFT + (8 - SK_B16_BITS));
907 b1 = _mm_and_si128(b1, b16_mask);
908 __m128i b2 = _mm_srli_epi32(src_pixel2,
909 SK_B32_SHIFT + (8 - SK_B16_BITS));
910 b2 = _mm_and_si128(b2, b16_mask);
911 __m128i b = _mm_packs_epi32(b1, b2);
913 // Store 8 16-bit colors in dst.
914 __m128i d_pixel = SkPackRGB16_SSE2(r, g, b);
915 _mm_store_si128(d++, d_pixel);
918 src = reinterpret_cast<const SkPMColor*>(s);
919 dst = reinterpret_cast<uint16_t*>(d);
924 SkPMColor c = *src++;
926 *dst++ = SkPixel32ToPixel16_ToU16(c);
927 } while (--count != 0);
931 /* SSE2 version of S32A_D565_Opaque()
932 * portable version is in core/SkBlitRow_D16.cpp
934 void S32A_D565_Opaque_SSE2(uint16_t* SK_RESTRICT dst,
935 const SkPMColor* SK_RESTRICT src,
936 int count, U8CPU alpha, int /*x*/, int /*y*/) {
937 SkASSERT(255 == alpha);
944 // Make dst 16 bytes alignment
945 while (((size_t)dst & 0x0F) != 0) {
946 SkPMColor c = *src++;
948 *dst = SkSrcOver32To16(c, *dst);
954 const __m128i* s = reinterpret_cast<const __m128i*>(src);
955 __m128i* d = reinterpret_cast<__m128i*>(dst);
956 __m128i var255 = _mm_set1_epi16(255);
957 __m128i r16_mask = _mm_set1_epi16(SK_R16_MASK);
958 __m128i g16_mask = _mm_set1_epi16(SK_G16_MASK);
959 __m128i b16_mask = _mm_set1_epi16(SK_B16_MASK);
962 // Load 8 pixels of src.
963 __m128i src_pixel1 = _mm_loadu_si128(s++);
964 __m128i src_pixel2 = _mm_loadu_si128(s++);
966 // Check whether src pixels are equal to 0 and get the highest bit
967 // of each byte of result, if src pixels are all zero, src_cmp1 and
968 // src_cmp2 will be 0xFFFF.
969 int src_cmp1 = _mm_movemask_epi8(_mm_cmpeq_epi16(src_pixel1,
970 _mm_setzero_si128()));
971 int src_cmp2 = _mm_movemask_epi8(_mm_cmpeq_epi16(src_pixel2,
972 _mm_setzero_si128()));
973 if (src_cmp1 == 0xFFFF && src_cmp2 == 0xFFFF) {
979 // Load 8 pixels of dst.
980 __m128i dst_pixel = _mm_load_si128(d);
982 // Extract A from src.
983 __m128i sa1 = _mm_slli_epi32(src_pixel1, (24 - SK_A32_SHIFT));
984 sa1 = _mm_srli_epi32(sa1, 24);
985 __m128i sa2 = _mm_slli_epi32(src_pixel2, (24 - SK_A32_SHIFT));
986 sa2 = _mm_srli_epi32(sa2, 24);
987 __m128i sa = _mm_packs_epi32(sa1, sa2);
989 // Extract R from src.
990 __m128i sr1 = _mm_slli_epi32(src_pixel1, (24 - SK_R32_SHIFT));
991 sr1 = _mm_srli_epi32(sr1, 24);
992 __m128i sr2 = _mm_slli_epi32(src_pixel2, (24 - SK_R32_SHIFT));
993 sr2 = _mm_srli_epi32(sr2, 24);
994 __m128i sr = _mm_packs_epi32(sr1, sr2);
996 // Extract G from src.
997 __m128i sg1 = _mm_slli_epi32(src_pixel1, (24 - SK_G32_SHIFT));
998 sg1 = _mm_srli_epi32(sg1, 24);
999 __m128i sg2 = _mm_slli_epi32(src_pixel2, (24 - SK_G32_SHIFT));
1000 sg2 = _mm_srli_epi32(sg2, 24);
1001 __m128i sg = _mm_packs_epi32(sg1, sg2);
1003 // Extract B from src.
1004 __m128i sb1 = _mm_slli_epi32(src_pixel1, (24 - SK_B32_SHIFT));
1005 sb1 = _mm_srli_epi32(sb1, 24);
1006 __m128i sb2 = _mm_slli_epi32(src_pixel2, (24 - SK_B32_SHIFT));
1007 sb2 = _mm_srli_epi32(sb2, 24);
1008 __m128i sb = _mm_packs_epi32(sb1, sb2);
1010 // Extract R G B from dst.
1011 __m128i dr = _mm_srli_epi16(dst_pixel, SK_R16_SHIFT);
1012 dr = _mm_and_si128(dr, r16_mask);
1013 __m128i dg = _mm_srli_epi16(dst_pixel, SK_G16_SHIFT);
1014 dg = _mm_and_si128(dg, g16_mask);
1015 __m128i db = _mm_srli_epi16(dst_pixel, SK_B16_SHIFT);
1016 db = _mm_and_si128(db, b16_mask);
1018 __m128i isa = _mm_sub_epi16(var255, sa); // 255 -sa
1020 // Calculate R G B of result.
1021 // Original algorithm is in SkSrcOver32To16().
1022 dr = _mm_add_epi16(sr, SkMul16ShiftRound_SSE2(dr, isa, SK_R16_BITS));
1023 dr = _mm_srli_epi16(dr, 8 - SK_R16_BITS);
1024 dg = _mm_add_epi16(sg, SkMul16ShiftRound_SSE2(dg, isa, SK_G16_BITS));
1025 dg = _mm_srli_epi16(dg, 8 - SK_G16_BITS);
1026 db = _mm_add_epi16(sb, SkMul16ShiftRound_SSE2(db, isa, SK_B16_BITS));
1027 db = _mm_srli_epi16(db, 8 - SK_B16_BITS);
1029 // Pack R G B into 16-bit color.
1030 __m128i d_pixel = SkPackRGB16_SSE2(dr, dg, db);
1032 // Store 8 16-bit colors in dst.
1033 _mm_store_si128(d++, d_pixel);
1037 src = reinterpret_cast<const SkPMColor*>(s);
1038 dst = reinterpret_cast<uint16_t*>(d);
1043 SkPMColor c = *src++;
1046 *dst = SkSrcOver32To16(c, *dst);
1049 } while (--count != 0);
1053 void S32_D565_Opaque_Dither_SSE2(uint16_t* SK_RESTRICT dst,
1054 const SkPMColor* SK_RESTRICT src,
1055 int count, U8CPU alpha, int x, int y) {
1056 SkASSERT(255 == alpha);
1063 while (((size_t)dst & 0x0F) != 0) {
1065 SkPMColor c = *src++;
1068 unsigned dither = DITHER_VALUE(x);
1069 *dst++ = SkDitherRGB32To565(c, dither);
1074 unsigned short dither_value[8];
1076 #ifdef ENABLE_DITHER_MATRIX_4X4
1077 const uint8_t* dither_scan = gDitherMatrix_3Bit_4X4[(y) & 3];
1078 dither_value[0] = dither_value[4] = dither_scan[(x) & 3];
1079 dither_value[1] = dither_value[5] = dither_scan[(x + 1) & 3];
1080 dither_value[2] = dither_value[6] = dither_scan[(x + 2) & 3];
1081 dither_value[3] = dither_value[7] = dither_scan[(x + 3) & 3];
1083 const uint16_t dither_scan = gDitherMatrix_3Bit_16[(y) & 3];
1084 dither_value[0] = dither_value[4] = (dither_scan
1085 >> (((x) & 3) << 2)) & 0xF;
1086 dither_value[1] = dither_value[5] = (dither_scan
1087 >> (((x + 1) & 3) << 2)) & 0xF;
1088 dither_value[2] = dither_value[6] = (dither_scan
1089 >> (((x + 2) & 3) << 2)) & 0xF;
1090 dither_value[3] = dither_value[7] = (dither_scan
1091 >> (((x + 3) & 3) << 2)) & 0xF;
1093 dither = _mm_loadu_si128((__m128i*) dither_value);
1095 const __m128i* s = reinterpret_cast<const __m128i*>(src);
1096 __m128i* d = reinterpret_cast<__m128i*>(dst);
1098 while (count >= 8) {
1099 // Load 8 pixels of src.
1100 __m128i src_pixel1 = _mm_loadu_si128(s++);
1101 __m128i src_pixel2 = _mm_loadu_si128(s++);
1103 // Extract R from src.
1104 __m128i sr1 = _mm_slli_epi32(src_pixel1, (24 - SK_R32_SHIFT));
1105 sr1 = _mm_srli_epi32(sr1, 24);
1106 __m128i sr2 = _mm_slli_epi32(src_pixel2, (24 - SK_R32_SHIFT));
1107 sr2 = _mm_srli_epi32(sr2, 24);
1108 __m128i sr = _mm_packs_epi32(sr1, sr2);
1110 // SkDITHER_R32To565(sr, dither)
1111 __m128i sr_offset = _mm_srli_epi16(sr, 5);
1112 sr = _mm_add_epi16(sr, dither);
1113 sr = _mm_sub_epi16(sr, sr_offset);
1114 sr = _mm_srli_epi16(sr, SK_R32_BITS - SK_R16_BITS);
1116 // Extract G from src.
1117 __m128i sg1 = _mm_slli_epi32(src_pixel1, (24 - SK_G32_SHIFT));
1118 sg1 = _mm_srli_epi32(sg1, 24);
1119 __m128i sg2 = _mm_slli_epi32(src_pixel2, (24 - SK_G32_SHIFT));
1120 sg2 = _mm_srli_epi32(sg2, 24);
1121 __m128i sg = _mm_packs_epi32(sg1, sg2);
1123 // SkDITHER_R32To565(sg, dither)
1124 __m128i sg_offset = _mm_srli_epi16(sg, 6);
1125 sg = _mm_add_epi16(sg, _mm_srli_epi16(dither, 1));
1126 sg = _mm_sub_epi16(sg, sg_offset);
1127 sg = _mm_srli_epi16(sg, SK_G32_BITS - SK_G16_BITS);
1129 // Extract B from src.
1130 __m128i sb1 = _mm_slli_epi32(src_pixel1, (24 - SK_B32_SHIFT));
1131 sb1 = _mm_srli_epi32(sb1, 24);
1132 __m128i sb2 = _mm_slli_epi32(src_pixel2, (24 - SK_B32_SHIFT));
1133 sb2 = _mm_srli_epi32(sb2, 24);
1134 __m128i sb = _mm_packs_epi32(sb1, sb2);
1136 // SkDITHER_R32To565(sb, dither)
1137 __m128i sb_offset = _mm_srli_epi16(sb, 5);
1138 sb = _mm_add_epi16(sb, dither);
1139 sb = _mm_sub_epi16(sb, sb_offset);
1140 sb = _mm_srli_epi16(sb, SK_B32_BITS - SK_B16_BITS);
1142 // Pack and store 16-bit dst pixel.
1143 __m128i d_pixel = SkPackRGB16_SSE2(sr, sg, sb);
1144 _mm_store_si128(d++, d_pixel);
1150 src = reinterpret_cast<const SkPMColor*>(s);
1151 dst = reinterpret_cast<uint16_t*>(d);
1157 SkPMColor c = *src++;
1160 unsigned dither = DITHER_VALUE(x);
1161 *dst++ = SkDitherRGB32To565(c, dither);
1163 } while (--count != 0);
1167 /* SSE2 version of S32A_D565_Opaque_Dither()
1168 * portable version is in core/SkBlitRow_D16.cpp
1170 void S32A_D565_Opaque_Dither_SSE2(uint16_t* SK_RESTRICT dst,
1171 const SkPMColor* SK_RESTRICT src,
1172 int count, U8CPU alpha, int x, int y) {
1173 SkASSERT(255 == alpha);
1180 while (((size_t)dst & 0x0F) != 0) {
1182 SkPMColor c = *src++;
1185 unsigned a = SkGetPackedA32(c);
1187 int d = SkAlphaMul(DITHER_VALUE(x), SkAlpha255To256(a));
1189 unsigned sr = SkGetPackedR32(c);
1190 unsigned sg = SkGetPackedG32(c);
1191 unsigned sb = SkGetPackedB32(c);
1192 sr = SkDITHER_R32_FOR_565(sr, d);
1193 sg = SkDITHER_G32_FOR_565(sg, d);
1194 sb = SkDITHER_B32_FOR_565(sb, d);
1196 uint32_t src_expanded = (sg << 24) | (sr << 13) | (sb << 2);
1197 uint32_t dst_expanded = SkExpand_rgb_16(*dst);
1198 dst_expanded = dst_expanded * (SkAlpha255To256(255 - a) >> 3);
1199 // now src and dst expanded are in g:11 r:10 x:1 b:10
1200 *dst = SkCompact_rgb_16((src_expanded + dst_expanded) >> 5);
1207 unsigned short dither_value[8];
1208 __m128i dither, dither_cur;
1209 #ifdef ENABLE_DITHER_MATRIX_4X4
1210 const uint8_t* dither_scan = gDitherMatrix_3Bit_4X4[(y) & 3];
1211 dither_value[0] = dither_value[4] = dither_scan[(x) & 3];
1212 dither_value[1] = dither_value[5] = dither_scan[(x + 1) & 3];
1213 dither_value[2] = dither_value[6] = dither_scan[(x + 2) & 3];
1214 dither_value[3] = dither_value[7] = dither_scan[(x + 3) & 3];
1216 const uint16_t dither_scan = gDitherMatrix_3Bit_16[(y) & 3];
1217 dither_value[0] = dither_value[4] = (dither_scan
1218 >> (((x) & 3) << 2)) & 0xF;
1219 dither_value[1] = dither_value[5] = (dither_scan
1220 >> (((x + 1) & 3) << 2)) & 0xF;
1221 dither_value[2] = dither_value[6] = (dither_scan
1222 >> (((x + 2) & 3) << 2)) & 0xF;
1223 dither_value[3] = dither_value[7] = (dither_scan
1224 >> (((x + 3) & 3) << 2)) & 0xF;
1226 dither = _mm_loadu_si128((__m128i*) dither_value);
1228 const __m128i* s = reinterpret_cast<const __m128i*>(src);
1229 __m128i* d = reinterpret_cast<__m128i*>(dst);
1230 __m128i var256 = _mm_set1_epi16(256);
1231 __m128i r16_mask = _mm_set1_epi16(SK_R16_MASK);
1232 __m128i g16_mask = _mm_set1_epi16(SK_G16_MASK);
1233 __m128i b16_mask = _mm_set1_epi16(SK_B16_MASK);
1235 while (count >= 8) {
1236 // Load 8 pixels of src and dst.
1237 __m128i src_pixel1 = _mm_loadu_si128(s++);
1238 __m128i src_pixel2 = _mm_loadu_si128(s++);
1239 __m128i dst_pixel = _mm_load_si128(d);
1241 // Extract A from src.
1242 __m128i sa1 = _mm_slli_epi32(src_pixel1, (24 - SK_A32_SHIFT));
1243 sa1 = _mm_srli_epi32(sa1, 24);
1244 __m128i sa2 = _mm_slli_epi32(src_pixel2, (24 - SK_A32_SHIFT));
1245 sa2 = _mm_srli_epi32(sa2, 24);
1246 __m128i sa = _mm_packs_epi32(sa1, sa2);
1248 // Calculate current dither value.
1249 dither_cur = _mm_mullo_epi16(dither,
1250 _mm_add_epi16(sa, _mm_set1_epi16(1)));
1251 dither_cur = _mm_srli_epi16(dither_cur, 8);
1253 // Extract R from src.
1254 __m128i sr1 = _mm_slli_epi32(src_pixel1, (24 - SK_R32_SHIFT));
1255 sr1 = _mm_srli_epi32(sr1, 24);
1256 __m128i sr2 = _mm_slli_epi32(src_pixel2, (24 - SK_R32_SHIFT));
1257 sr2 = _mm_srli_epi32(sr2, 24);
1258 __m128i sr = _mm_packs_epi32(sr1, sr2);
1260 // SkDITHER_R32_FOR_565(sr, d)
1261 __m128i sr_offset = _mm_srli_epi16(sr, 5);
1262 sr = _mm_add_epi16(sr, dither_cur);
1263 sr = _mm_sub_epi16(sr, sr_offset);
1266 sr = _mm_slli_epi16(sr, 2);
1268 // Extract G from src.
1269 __m128i sg1 = _mm_slli_epi32(src_pixel1, (24 - SK_G32_SHIFT));
1270 sg1 = _mm_srli_epi32(sg1, 24);
1271 __m128i sg2 = _mm_slli_epi32(src_pixel2, (24 - SK_G32_SHIFT));
1272 sg2 = _mm_srli_epi32(sg2, 24);
1273 __m128i sg = _mm_packs_epi32(sg1, sg2);
1275 // sg = SkDITHER_G32_FOR_565(sg, d).
1276 __m128i sg_offset = _mm_srli_epi16(sg, 6);
1277 sg = _mm_add_epi16(sg, _mm_srli_epi16(dither_cur, 1));
1278 sg = _mm_sub_epi16(sg, sg_offset);
1281 sg = _mm_slli_epi16(sg, 3);
1283 // Extract B from src.
1284 __m128i sb1 = _mm_slli_epi32(src_pixel1, (24 - SK_B32_SHIFT));
1285 sb1 = _mm_srli_epi32(sb1, 24);
1286 __m128i sb2 = _mm_slli_epi32(src_pixel2, (24 - SK_B32_SHIFT));
1287 sb2 = _mm_srli_epi32(sb2, 24);
1288 __m128i sb = _mm_packs_epi32(sb1, sb2);
1290 // sb = SkDITHER_B32_FOR_565(sb, d).
1291 __m128i sb_offset = _mm_srli_epi16(sb, 5);
1292 sb = _mm_add_epi16(sb, dither_cur);
1293 sb = _mm_sub_epi16(sb, sb_offset);
1296 sb = _mm_slli_epi16(sb, 2);
1298 // Extract R G B from dst.
1299 __m128i dr = _mm_srli_epi16(dst_pixel, SK_R16_SHIFT);
1300 dr = _mm_and_si128(dr, r16_mask);
1301 __m128i dg = _mm_srli_epi16(dst_pixel, SK_G16_SHIFT);
1302 dg = _mm_and_si128(dg, g16_mask);
1303 __m128i db = _mm_srli_epi16(dst_pixel, SK_B16_SHIFT);
1304 db = _mm_and_si128(db, b16_mask);
1306 // SkAlpha255To256(255 - a) >> 3
1307 __m128i isa = _mm_sub_epi16(var256, sa);
1308 isa = _mm_srli_epi16(isa, 3);
1310 dr = _mm_mullo_epi16(dr, isa);
1311 dr = _mm_add_epi16(dr, sr);
1312 dr = _mm_srli_epi16(dr, 5);
1314 dg = _mm_mullo_epi16(dg, isa);
1315 dg = _mm_add_epi16(dg, sg);
1316 dg = _mm_srli_epi16(dg, 5);
1318 db = _mm_mullo_epi16(db, isa);
1319 db = _mm_add_epi16(db, sb);
1320 db = _mm_srli_epi16(db, 5);
1322 // Package and store dst pixel.
1323 __m128i d_pixel = SkPackRGB16_SSE2(dr, dg, db);
1324 _mm_store_si128(d++, d_pixel);
1330 src = reinterpret_cast<const SkPMColor*>(s);
1331 dst = reinterpret_cast<uint16_t*>(d);
1337 SkPMColor c = *src++;
1340 unsigned a = SkGetPackedA32(c);
1342 int d = SkAlphaMul(DITHER_VALUE(x), SkAlpha255To256(a));
1344 unsigned sr = SkGetPackedR32(c);
1345 unsigned sg = SkGetPackedG32(c);
1346 unsigned sb = SkGetPackedB32(c);
1347 sr = SkDITHER_R32_FOR_565(sr, d);
1348 sg = SkDITHER_G32_FOR_565(sg, d);
1349 sb = SkDITHER_B32_FOR_565(sb, d);
1351 uint32_t src_expanded = (sg << 24) | (sr << 13) | (sb << 2);
1352 uint32_t dst_expanded = SkExpand_rgb_16(*dst);
1353 dst_expanded = dst_expanded * (SkAlpha255To256(255 - a) >> 3);
1354 // now src and dst expanded are in g:11 r:10 x:1 b:10
1355 *dst = SkCompact_rgb_16((src_expanded + dst_expanded) >> 5);
1359 } while (--count != 0);