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

Side by Side Diff: src/opts/SkOpts_sse41.cpp

Issue 1632713002: Revert of AVX 2 SrcOver blits: color32, blitmask. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months 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/SkOpts_avx2.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 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkOpts.h" 8 #include "SkOpts.h"
9 9
10 #define SK_OPTS_NS sk_sse41 10 #define SK_OPTS_NS sk_sse41
11 #include "SkBlurImageFilter_opts.h" 11 #include "SkBlurImageFilter_opts.h"
12 12
13 #ifndef SK_SUPPORT_LEGACY_X86_BLITS 13 #ifndef SK_SUPPORT_LEGACY_X86_BLITS
14 14
15 namespace sk_sse41 { 15 // This file deals mostly with unpacked 8-bit values,
16 // i.e. values between 0 and 255, but in 16-bit lanes with 0 at the top.
16 17
17 // An SSE register holding at most 64 bits of useful data in the low lanes. 18 // So __m128i typically represents 1 or 2 pixels, and m128ix2 represents 4.
18 struct m64i { 19 struct m128ix2 { __m128i lo, hi; };
19 __m128i v;
20 /*implicit*/ m64i(__m128i v) : v(v) {}
21 operator __m128i() const { return v; }
22 };
23 20
24 // Load 4, 2, or 1 constant pixels or coverages (4x replicated). 21 // unpack{lo,hi}() get our raw pixels unpacked, from half of 4 packed pixels to 2 unpacked pixels.
25 static __m128i next4(uint32_t val) { return _mm_set1_epi32(val); } 22 static inline __m128i unpacklo(__m128i x) { return _mm_cvtepu8_epi16(x); }
26 static m64i next2(uint32_t val) { return _mm_set1_epi32(val); } 23 static inline __m128i unpackhi(__m128i x) { return _mm_unpackhi_epi8(x, _mm_setz ero_si128()); }
27 static m64i next1(uint32_t val) { return _mm_set1_epi32(val); }
28 24
29 static __m128i next4(uint8_t val) { return _mm_set1_epi8(val); } 25 // pack() converts back, from 4 unpacked pixels to 4 packed pixels.
30 static m64i next2(uint8_t val) { return _mm_set1_epi8(val); } 26 static inline __m128i pack(__m128i lo, __m128i hi) { return _mm_packus_epi16(lo, hi); }
31 static m64i next1(uint8_t val) { return _mm_set1_epi8(val); }
32 27
33 // Load 4, 2, or 1 variable pixels or coverages (4x replicated), 28 // These nextN() functions abstract over the difference between iterating over
34 // incrementing the pointer past what we read. 29 // an array of values and returning a constant value, for uint8_t and uint32_t.
35 static __m128i next4(const uint32_t*& ptr) { 30 // The nextN() taking pointers increment that pointer past where they read.
36 auto r = _mm_loadu_si128((const __m128i*)ptr); 31 //
32 // nextN() returns N unpacked pixels or 4N unpacked coverage values.
33
34 static inline __m128i next1(uint8_t val) { return _mm_set1_epi16(val); }
35 static inline __m128i next2(uint8_t val) { return _mm_set1_epi16(val); }
36 static inline m128ix2 next4(uint8_t val) { return { next2(val), next2(val) }; }
37
38 static inline __m128i next1(uint32_t val) { return unpacklo(_mm_cvtsi32_si128(va l)); }
39 static inline __m128i next2(uint32_t val) { return unpacklo(_mm_set1_epi32(val)) ; }
40 static inline m128ix2 next4(uint32_t val) { return { next2(val), next2(val) }; }
41
42 static inline __m128i next1(const uint8_t*& ptr) { return _mm_set1_epi16(*ptr++) ; }
43 static inline __m128i next2(const uint8_t*& ptr) {
44 auto r = _mm_cvtsi32_si128(*(const uint16_t*)ptr);
45 ptr += 2;
46 const int _ = ~0;
47 return _mm_shuffle_epi8(r, _mm_setr_epi8(0,_,0,_,0,_,0,_, 1,_,1,_,1,_,1,_));
48 }
49 static inline m128ix2 next4(const uint8_t*& ptr) {
50 auto r = _mm_cvtsi32_si128(*(const uint32_t*)ptr);
37 ptr += 4; 51 ptr += 4;
38 return r; 52 const int _ = ~0;
53 auto lo = _mm_shuffle_epi8(r, _mm_setr_epi8(0,_,0,_,0,_,0,_, 1,_,1,_,1,_,1,_ )),
54 hi = _mm_shuffle_epi8(r, _mm_setr_epi8(2,_,2,_,2,_,2,_, 3,_,3,_,3,_,3,_ ));
55 return { lo, hi };
39 } 56 }
40 static m64i next2(const uint32_t*& ptr) { 57
41 auto r = _mm_loadl_epi64((const __m128i*)ptr); 58 static inline __m128i next1(const uint32_t*& ptr) { return unpacklo(_mm_cvtsi32_ si128(*ptr++)); }
59 static inline __m128i next2(const uint32_t*& ptr) {
60 auto r = unpacklo(_mm_loadl_epi64((const __m128i*)ptr));
42 ptr += 2; 61 ptr += 2;
43 return r; 62 return r;
44 } 63 }
45 static m64i next1(const uint32_t*& ptr) { 64 static inline m128ix2 next4(const uint32_t*& ptr) {
46 auto r = _mm_cvtsi32_si128(*ptr); 65 auto packed = _mm_loadu_si128((const __m128i*)ptr);
47 ptr += 1; 66 ptr += 4;
48 return r; 67 return { unpacklo(packed), unpackhi(packed) };
49 } 68 }
50 69
51 // xyzw -> xxxx yyyy zzzz wwww 70 // Divide by 255 with rounding.
52 static __m128i replicate_coverage(__m128i xyzw) { 71 // (x+127)/255 == ((x+128)*257)>>16.
53 const uint8_t mask[] = { 0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3 }; 72 // Sometimes we can be more efficient by breaking this into two parts.
54 return _mm_shuffle_epi8(xyzw, _mm_load_si128((const __m128i*)mask)); 73 static inline __m128i div255_part1(__m128i x) { return _mm_add_epi16(x, _mm_set1 _epi16(128)); }
74 static inline __m128i div255_part2(__m128i x) { return _mm_mulhi_epu16(x, _mm_se t1_epi16(257)); }
75 static inline __m128i div255(__m128i x) { return div255_part2(div255_part1(x)); }
76
77 // (x*y+127)/255, a byte multiply.
78 static inline __m128i scale(__m128i x, __m128i y) {
79 return div255(_mm_mullo_epi16(x, y));
55 } 80 }
56 81
57 static __m128i next4(const uint8_t*& ptr) { 82 // (255 - x).
58 auto r = replicate_coverage(_mm_cvtsi32_si128(*(const uint32_t*)ptr)); 83 static inline __m128i inv(__m128i x) {
59 ptr += 4; 84 return _mm_xor_si128(_mm_set1_epi16(0x00ff), x); // This seems a bit faster than _mm_sub_epi16.
60 return r;
61 } 85 }
62 static m64i next2(const uint8_t*& ptr) { 86
63 auto r = replicate_coverage(_mm_cvtsi32_si128(*(const uint16_t*)ptr)); 87 // ARGB argb -> AAAA aaaa
64 ptr += 2; 88 static inline __m128i alphas(__m128i px) {
65 return r; 89 const int a = 2 * (SK_A32_SHIFT/8); // SK_A32_SHIFT is typically 24, so thi s is typically 6.
66 } 90 const int _ = ~0;
67 static m64i next1(const uint8_t*& ptr) { 91 return _mm_shuffle_epi8(px, _mm_setr_epi8(a+0,_,a+0,_,a+0,_,a+0,_, a+8,_,a+8 ,_,a+8,_,a+8,_));
68 auto r = replicate_coverage(_mm_cvtsi32_si128(*ptr));
69 ptr += 1;
70 return r;
71 } 92 }
72 93
73 // For i = 0...n, tgt = fn(dst,src,cov), where Dst,Src,and Cov can be constants or arrays. 94 // For i = 0...n, tgt = fn(dst,src,cov), where Dst,Src,and Cov can be constants or arrays.
74 template <typename Dst, typename Src, typename Cov, typename Fn> 95 template <typename Dst, typename Src, typename Cov, typename Fn>
75 static void loop(int n, uint32_t* t, const Dst dst, const Src src, const Cov cov , Fn&& fn) { 96 static inline void loop(int n, uint32_t* t, const Dst dst, const Src src, const Cov cov, Fn&& fn) {
76 // We don't want to muck with the callers' pointers, so we make them const a nd copy here. 97 // We don't want to muck with the callers' pointers, so we make them const a nd copy here.
77 Dst d = dst; 98 Dst d = dst;
78 Src s = src; 99 Src s = src;
79 Cov c = cov; 100 Cov c = cov;
80 101
81 // Writing this as a single while-loop helps hoist loop invariants from fn. 102 // Writing this as a single while-loop helps hoist loop invariants from fn.
82 while (n) { 103 while (n) {
83 if (n >= 4) { 104 if (n >= 4) {
84 _mm_storeu_si128((__m128i*)t, fn(next4(d), next4(s), next4(c))); 105 auto d4 = next4(d),
106 s4 = next4(s),
107 c4 = next4(c);
108 auto lo = fn(d4.lo, s4.lo, c4.lo),
109 hi = fn(d4.hi, s4.hi, c4.hi);
110 _mm_storeu_si128((__m128i*)t, pack(lo,hi));
85 t += 4; 111 t += 4;
86 n -= 4; 112 n -= 4;
87 continue; 113 continue;
88 } 114 }
89 if (n & 2) { 115 if (n & 2) {
90 _mm_storel_epi64((__m128i*)t, fn(next2(d), next2(s), next2(c))); 116 auto r = fn(next2(d), next2(s), next2(c));
117 _mm_storel_epi64((__m128i*)t, pack(r,r));
91 t += 2; 118 t += 2;
92 } 119 }
93 if (n & 1) { 120 if (n & 1) {
94 *t = _mm_cvtsi128_si32(fn(next1(d), next1(s), next1(c))); 121 auto r = fn(next1(d), next1(s), next1(c));
122 *t = _mm_cvtsi128_si32(pack(r,r));
95 } 123 }
96 return; 124 return;
97 } 125 }
98 } 126 }
99 127
100 // packed 128 namespace sk_sse41 {
101 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ //
102 // unpacked
103
104 // Everything on the packed side of the squiggly line deals with densely packed 8-bit data,
105 // e.g. [BGRA bgra ... ] for pixels or [ CCCC cccc ... ] for coverage.
106 //
107 // Everything on the unpacked side of the squiggly line deals with unpacked 8-bi t data,
108 // e.g [B_G_ R_A_ b_g_ r_a_ ] for pixels or [ C_C_ C_C_ c_c_ c_c_ c_c_ ] for cov erage,
109 // where _ is a zero byte.
110 //
111 // Adapt<Fn> / adapt(fn) allow the two sides to interoperate,
112 // by unpacking arguments, calling fn, then packing the results.
113 //
114 // This lets us write most of our code in terms of unpacked inputs (considerably simpler)
115 // and all the packing and unpacking is handled automatically.
116
117 template <typename Fn>
118 struct Adapt {
119 Fn fn;
120
121 __m128i operator()(__m128i d, __m128i s, __m128i c) {
122 auto lo = [](__m128i x) { return _mm_unpacklo_epi8(x, _mm_setzero_si128( )); };
123 auto hi = [](__m128i x) { return _mm_unpackhi_epi8(x, _mm_setzero_si128( )); };
124 return _mm_packus_epi16(fn(lo(d), lo(s), lo(c)),
125 fn(hi(d), hi(s), hi(c)));
126 }
127
128 m64i operator()(const m64i& d, const m64i& s, const m64i& c) {
129 auto lo = [](__m128i x) { return _mm_unpacklo_epi8(x, _mm_setzero_si128( )); };
130 auto r = fn(lo(d), lo(s), lo(c));
131 return _mm_packus_epi16(r, r);
132 }
133 };
134
135 template <typename Fn>
136 static Adapt<Fn> adapt(Fn&& fn) { return { fn }; }
137
138 // These helpers all work exclusively with unpacked 8-bit values,
139 // except div255() with is 16-bit -> unpacked 8-bit, and mul255() which is the r everse.
140
141 // Divide by 255 with rounding.
142 // (x+127)/255 == ((x+128)*257)>>16.
143 // Sometimes we can be more efficient by breaking this into two parts.
144 static __m128i div255_part1(__m128i x) { return _mm_add_epi16(x, _mm_set1_epi16( 128)); }
145 static __m128i div255_part2(__m128i x) { return _mm_mulhi_epu16(x, _mm_set1_epi1 6(257)); }
146 static __m128i div255(__m128i x) { return div255_part2(div255_part1(x)); }
147
148 // (x*y+127)/255, a byte multiply.
149 static __m128i scale(__m128i x, __m128i y) { return div255(_mm_mullo_epi16(x, y) ); }
150
151 // (255 * x).
152 static __m128i mul255(__m128i x) { return _mm_sub_epi16(_mm_slli_epi16(x, 8), x) ; }
153
154 // (255 - x).
155 static __m128i inv(__m128i x) { return _mm_xor_si128(_mm_set1_epi16(0x00ff), x); }
156
157 // ARGB argb -> AAAA aaaa
158 static __m128i alphas(__m128i px) {
159 const int a = 2 * (SK_A32_SHIFT/8); // SK_A32_SHIFT is typically 24, so thi s is typically 6.
160 const int _ = ~0;
161 return _mm_shuffle_epi8(px, _mm_setr_epi8(a+0,_,a+0,_,a+0,_,a+0,_, a+8,_,a+8 ,_,a+8,_,a+8,_));
162 }
163 129
164 // SrcOver, with a constant source and full coverage. 130 // SrcOver, with a constant source and full coverage.
165 static void blit_row_color32(SkPMColor* tgt, const SkPMColor* dst, int n, SkPMCo lor src) { 131 static void blit_row_color32(SkPMColor* tgt, const SkPMColor* dst, int n, SkPMCo lor src) {
166 // We want to calculate s + (d * inv(alphas(s)) + 127)/255. 132 // We want to calculate s + (d * inv(alphas(s)) + 127)/255.
167 // We'd generally do that div255 as s + ((d * inv(alphas(s)) + 128)*257)>>16 . 133 // We'd generally do that div255 as s + ((d * inv(alphas(s)) + 128)*257)>>16 .
168 134
169 // But we can go one step further to ((s*255 + 128 + d*inv(alphas(s)))*257)> >16. 135 // But we can go one step further to ((s*255 + 128 + d*inv(alphas(s)))*257)> >16.
170 // This lets us hoist (s*255+128) and inv(alphas(s)) out of the loop. 136 // This lets us hoist (s*255+128) and inv(alphas(s)) out of the loop.
171 __m128i s = _mm_unpacklo_epi8(_mm_set1_epi32(src), _mm_setzero_si128()), 137 __m128i s = next2(src),
172 s_255_128 = div255_part1(mul255(s)), 138 s_255_128 = div255_part1(_mm_mullo_epi16(s, _mm_set1_epi16(255))),
173 A = inv(alphas(s)); 139 A = inv(alphas(s));
174 140
175 const uint8_t cov = 0xff; 141 const uint8_t cov = 0xff;
176 loop(n, tgt, dst, src, cov, adapt([=](__m128i d, __m128i, __m128i) { 142 loop(n, tgt, dst, src, cov, [=](__m128i d, __m128i, __m128i) {
177 return div255_part2(_mm_add_epi16(s_255_128, _mm_mullo_epi16(d, A))); 143 return div255_part2(_mm_add_epi16(s_255_128, _mm_mullo_epi16(d, A)));
178 })); 144 });
179 } 145 }
180 146
181 // SrcOver, with a constant source and variable coverage. 147 // SrcOver, with a constant source and variable coverage.
182 // If the source is opaque, SrcOver becomes Src. 148 // If the source is opaque, SrcOver becomes Src.
183 static void blit_mask_d32_a8(SkPMColor* dst, size_t dstRB, 149 static void blit_mask_d32_a8(SkPMColor* dst, size_t dstRB,
184 const SkAlpha* cov, size_t covRB, 150 const SkAlpha* cov, size_t covRB,
185 SkColor color, int w, int h) { 151 SkColor color, int w, int h) {
186 if (SkColorGetA(color) == 0xFF) { 152 if (SkColorGetA(color) == 0xFF) {
187 const SkPMColor src = SkSwizzle_BGRA_to_PMColor(color); 153 const SkPMColor src = SkSwizzle_BGRA_to_PMColor(color);
188 while (h --> 0) { 154 while (h --> 0) {
189 loop(w, dst, (const SkPMColor*)dst, src, cov, 155 loop(w, dst, (const SkPMColor*)dst, src, cov, [](__m128i d, __m128i s, __m128i c) {
190 adapt([](__m128i d, __m128i s, __m128i c) {
191 // Src blend mode: a simple lerp from d to s by c. 156 // Src blend mode: a simple lerp from d to s by c.
192 // TODO: try a pmaddubsw version? 157 // TODO: try a pmaddubsw version?
193 return div255(_mm_add_epi16(_mm_mullo_epi16(inv(c),d), 158 return div255(_mm_add_epi16(_mm_mullo_epi16(inv(c),d), _mm_mullo _epi16(c,s)));
194 _mm_mullo_epi16( c ,s))); 159 });
195 }));
196 dst += dstRB / sizeof(*dst); 160 dst += dstRB / sizeof(*dst);
197 cov += covRB / sizeof(*cov); 161 cov += covRB / sizeof(*cov);
198 } 162 }
199 } else { 163 } else {
200 const SkPMColor src = SkPreMultiplyColor(color); 164 const SkPMColor src = SkPreMultiplyColor(color);
201 while (h --> 0) { 165 while (h --> 0) {
202 loop(w, dst, (const SkPMColor*)dst, src, cov, 166 loop(w, dst, (const SkPMColor*)dst, src, cov, [](__m128i d, __m128i s, __m128i c) {
203 adapt([](__m128i d, __m128i s, __m128i c) {
204 // SrcOver blend mode, with coverage folded into source alpha. 167 // SrcOver blend mode, with coverage folded into source alpha.
205 __m128i sc = scale(s,c), 168 __m128i sc = scale(s,c),
206 AC = inv(alphas(sc)); 169 AC = inv(alphas(sc));
207 return _mm_add_epi16(sc, scale(d,AC)); 170 return _mm_add_epi16(sc, scale(d,AC));
208 })); 171 });
209 dst += dstRB / sizeof(*dst); 172 dst += dstRB / sizeof(*dst);
210 cov += covRB / sizeof(*cov); 173 cov += covRB / sizeof(*cov);
211 } 174 }
212 } 175 }
213 } 176 }
214 177
215 } // namespace sk_sse41 178 } // namespace sk_sse41
216
217 #endif 179 #endif
218 180
219 namespace SkOpts { 181 namespace SkOpts {
220 void Init_sse41() { 182 void Init_sse41() {
221 box_blur_xx = sk_sse41::box_blur_xx; 183 box_blur_xx = sk_sse41::box_blur_xx;
222 box_blur_xy = sk_sse41::box_blur_xy; 184 box_blur_xy = sk_sse41::box_blur_xy;
223 box_blur_yx = sk_sse41::box_blur_yx; 185 box_blur_yx = sk_sse41::box_blur_yx;
224 186
225 #ifndef SK_SUPPORT_LEGACY_X86_BLITS 187 #ifndef SK_SUPPORT_LEGACY_X86_BLITS
226 blit_row_color32 = sk_sse41::blit_row_color32; 188 blit_row_color32 = sk_sse41::blit_row_color32;
227 blit_mask_d32_a8 = sk_sse41::blit_mask_d32_a8; 189 blit_mask_d32_a8 = sk_sse41::blit_mask_d32_a8;
228 #endif 190 #endif
229 } 191 }
230 } 192 }
OLDNEW
« no previous file with comments | « src/opts/SkOpts_avx2.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698