Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: src/opts/SkColor_opts_SSE2.h

Issue 754733002: Add SkBlendARGB32_SSE2() to clean up code (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add fast path for SkBlendARGB32_SSE2 with constant alpha factor Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/opts/SkBlitRow_opts_SSE2.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The Android Open Source Project 2 * Copyright 2014 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkColor_opts_SSE2_DEFINED 8 #ifndef SkColor_opts_SSE2_DEFINED
9 #define SkColor_opts_SSE2_DEFINED 9 #define SkColor_opts_SSE2_DEFINED
10 10
(...skipping 24 matching lines...) Expand all
35 static inline __m128i SkAlphaMulAlpha_SSE2(const __m128i& a, 35 static inline __m128i SkAlphaMulAlpha_SSE2(const __m128i& a,
36 const __m128i& b) { 36 const __m128i& b) {
37 __m128i prod = _mm_mullo_epi16(a, b); 37 __m128i prod = _mm_mullo_epi16(a, b);
38 prod = _mm_add_epi32(prod, _mm_set1_epi32(128)); 38 prod = _mm_add_epi32(prod, _mm_set1_epi32(128));
39 prod = _mm_add_epi32(prod, _mm_srli_epi32(prod, 8)); 39 prod = _mm_add_epi32(prod, _mm_srli_epi32(prod, 8));
40 prod = _mm_srli_epi32(prod, 8); 40 prod = _mm_srli_epi32(prod, 8);
41 41
42 return prod; 42 return prod;
43 } 43 }
44 44
45 static const __m128i rb_mask = _mm_set1_epi32(0x00FF00FF);
46 static const __m128i ag_mask = _mm_set1_epi32(0xFF00FF00);
47
45 // Portable version SkAlphaMulQ is in SkColorPriv.h. 48 // Portable version SkAlphaMulQ is in SkColorPriv.h.
46 static inline __m128i SkAlphaMulQ_SSE2(const __m128i& c, const __m128i& scale) { 49 static inline __m128i SkAlphaMulQ_SSE2(const __m128i& c, const __m128i& scale) {
47 __m128i mask = _mm_set1_epi32(0xFF00FF);
48 __m128i s = _mm_or_si128(_mm_slli_epi32(scale, 16), scale); 50 __m128i s = _mm_or_si128(_mm_slli_epi32(scale, 16), scale);
49 51
50 // uint32_t rb = ((c & mask) * scale) >> 8 52 // uint32_t rb = ((c & mask) * scale) >> 8
51 __m128i rb = _mm_and_si128(mask, c); 53 __m128i rb = _mm_and_si128(rb_mask, c);
52 rb = _mm_mullo_epi16(rb, s); 54 rb = _mm_mullo_epi16(rb, s);
53 rb = _mm_srli_epi16(rb, 8); 55 rb = _mm_srli_epi16(rb, 8);
54 56
55 // uint32_t ag = ((c >> 8) & mask) * scale 57 // uint32_t ag = ((c >> 8) & mask) * scale
56 __m128i ag = _mm_srli_epi16(c, 8); 58 __m128i ag = _mm_srli_epi16(c, 8);
57 ASSERT_EQ(ag, _mm_and_si128(mask, ag)); // ag = _mm_srli_epi16(c, 8) did th is for us. 59 ASSERT_EQ(ag, _mm_and_si128(rb_mask, ag)); // ag = _mm_srli_epi16(c, 8) did this for us.
58 ag = _mm_mullo_epi16(ag, s); 60 ag = _mm_mullo_epi16(ag, s);
59 61
60 // (rb & mask) | (ag & ~mask) 62 // (rb & mask) | (ag & ~mask)
61 ASSERT_EQ(rb, _mm_and_si128(mask, rb)); // rb = _mm_srli_epi16(rb, 8) did t his for us. 63 ASSERT_EQ(rb, _mm_and_si128(rb_mask, rb)); // rb = _mm_srli_epi16(rb, 8) di d this for us.
62 ag = _mm_andnot_si128(mask, ag); 64 ag = _mm_and_si128(ag_mask, ag);
63 return _mm_or_si128(rb, ag); 65 return _mm_or_si128(rb, ag);
64 } 66 }
65 67
68 // Fast path for SkAlphaMulQ_SSE2 with a constant scale factor.
69 static inline __m128i SkAlphaMulQ_SSE2(const __m128i& c, const unsigned scale) {
70 __m128i s = _mm_set1_epi16(scale << 8); // Move scale factor to upper byte o f word.
71
72 // With mulhi, red and blue values are already in the right place and
73 // don't need to be divided by 256.
74 __m128i rb = _mm_and_si128(rb_mask, c);
75 rb = _mm_mulhi_epu16(rb, s);
76
77 __m128i ag = _mm_and_si128(ag_mask, c);
78 ag = _mm_mulhi_epu16(ag, s); // Alpha and green values are in the higher byte of each word.
79 ag = _mm_and_si128(ag_mask, ag);
80
81 return _mm_or_si128(rb, ag);
82 }
83
66 static inline __m128i SkGetPackedA32_SSE2(const __m128i& src) { 84 static inline __m128i SkGetPackedA32_SSE2(const __m128i& src) {
67 __m128i a = _mm_slli_epi32(src, (24 - SK_A32_SHIFT)); 85 __m128i a = _mm_slli_epi32(src, (24 - SK_A32_SHIFT));
68 return _mm_srli_epi32(a, 24); 86 return _mm_srli_epi32(a, 24);
69 } 87 }
70 88
71 static inline __m128i SkGetPackedR32_SSE2(const __m128i& src) { 89 static inline __m128i SkGetPackedR32_SSE2(const __m128i& src) {
72 __m128i r = _mm_slli_epi32(src, (24 - SK_R32_SHIFT)); 90 __m128i r = _mm_slli_epi32(src, (24 - SK_R32_SHIFT));
73 return _mm_srli_epi32(r, 24); 91 return _mm_srli_epi32(r, 24);
74 } 92 }
75 93
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 SK_B32_SHIFT + (8 - SK_B16_BITS)); 196 SK_B32_SHIFT + (8 - SK_B16_BITS));
179 b2 = _mm_and_si128(b2, _mm_set1_epi32(SK_B16_MASK)); 197 b2 = _mm_and_si128(b2, _mm_set1_epi32(SK_B16_MASK));
180 __m128i b = _mm_packs_epi32(b1, b2); 198 __m128i b = _mm_packs_epi32(b1, b2);
181 199
182 // Store 8 16-bit colors in dst. 200 // Store 8 16-bit colors in dst.
183 __m128i d_pixel = SkPackRGB16_SSE2(r, g, b); 201 __m128i d_pixel = SkPackRGB16_SSE2(r, g, b);
184 202
185 return d_pixel; 203 return d_pixel;
186 } 204 }
187 205
206 // Portable version SkBlendARGB32 is in SkColorPriv.h.
207 static inline __m128i SkBlendARGB32_SSE2(const __m128i& src, const __m128i& dst,
208 const __m128i& aa) {
209 __m128i src_scale = SkAlpha255To256_SSE2(aa);
210 // SkAlpha255To256(255 - SkAlphaMul(SkGetPackedA32(src), src_scale))
211 __m128i dst_scale = SkGetPackedA32_SSE2(src);
212 dst_scale = _mm_mullo_epi16(dst_scale, src_scale);
213 dst_scale = _mm_srli_epi16(dst_scale, 8);
214 dst_scale = _mm_sub_epi32(_mm_set1_epi32(256), dst_scale);
215
216 __m128i result = SkAlphaMulQ_SSE2(src, src_scale);
217 return _mm_add_epi8(result, SkAlphaMulQ_SSE2(dst, dst_scale));
218 }
219
220 // Fast path for SkBlendARGB32_SSE2 with a constant alpha factor.
221 static inline __m128i SkBlendARGB32_SSE2(const __m128i& src, const __m128i& dst,
222 const unsigned aa) {
223 unsigned alpha = SkAlpha255To256(aa);
224 __m128i src_scale = _mm_set1_epi32(alpha);
225 // SkAlpha255To256(255 - SkAlphaMul(SkGetPackedA32(src), src_scale))
226 __m128i dst_scale = SkGetPackedA32_SSE2(src);
227 dst_scale = _mm_mullo_epi16(dst_scale, src_scale);
228 dst_scale = _mm_srli_epi16(dst_scale, 8);
229 dst_scale = _mm_sub_epi32(_mm_set1_epi32(256), dst_scale);
230
231 __m128i result = SkAlphaMulQ_SSE2(src, alpha);
232 return _mm_add_epi8(result, SkAlphaMulQ_SSE2(dst, dst_scale));
233 }
234
188 #undef ASSERT_EQ 235 #undef ASSERT_EQ
189 #endif // SkColor_opts_SSE2_DEFINED 236 #endif // SkColor_opts_SSE2_DEFINED
OLDNEW
« no previous file with comments | « src/opts/SkBlitRow_opts_SSE2.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698