• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef Sk4px_DEFINED
9 #define Sk4px_DEFINED
10 
11 #include "include/core/SkColor.h"
12 #include "include/private/SkColorData.h"
13 #include "include/private/SkNx.h"
14 
15 // This file may be included multiple times by .cpp files with different flags, leading
16 // to different definitions.  Usually that doesn't matter because it's all inlined, but
17 // in Debug modes the compilers may not inline everything.  So wrap everything in an
18 // anonymous namespace to give each includer their own silo of this code (or the linker
19 // will probably pick one randomly for us, which is rarely correct).
20 namespace {  // NOLINT(google-build-namespaces)
21 
22 // 1, 2 or 4 SkPMColors, generally vectorized.
23 class Sk4px : public Sk16b {
24 public:
Sk4px(const Sk16b & v)25     Sk4px(const Sk16b& v) : INHERITED(v) {}
26 
DupPMColor(SkPMColor c)27     static Sk4px DupPMColor(SkPMColor c) {
28         Sk4u splat(c);
29 
30         Sk4px v;
31         memcpy((void*)&v, &splat, 16);
32         return v;
33     }
34 
35     Sk4px alphas() const;  // ARGB argb XYZW xyzw -> AAAA aaaa XXXX xxxx
inv()36     Sk4px inv() const { return Sk16b(255) - *this; }
37 
38     // When loading or storing fewer than 4 SkPMColors, we use the low lanes.
Load4(const SkPMColor px[4])39     static Sk4px Load4(const SkPMColor px[4]) {
40         Sk4px v;
41         memcpy((void*)&v, px, 16);
42         return v;
43     }
Load2(const SkPMColor px[2])44     static Sk4px Load2(const SkPMColor px[2]) {
45         Sk4px v;
46         memcpy((void*)&v, px, 8);
47         return v;
48     }
Load1(const SkPMColor px[1])49     static Sk4px Load1(const SkPMColor px[1]) {
50         Sk4px v;
51         memcpy((void*)&v, px, 4);
52         return v;
53     }
54 
55     // Ditto for Alphas... Load2Alphas fills the low two lanes of Sk4px.
56     static Sk4px Load4Alphas(const SkAlpha[4]);  // AaXx -> AAAA aaaa XXXX xxxx
57     static Sk4px Load2Alphas(const SkAlpha[2]);  // Aa   -> AAAA aaaa ???? ????
58 
store4(SkPMColor px[4])59     void store4(SkPMColor px[4]) const { memcpy(px, this, 16); }
store2(SkPMColor px[2])60     void store2(SkPMColor px[2]) const { memcpy(px, this,  8); }
store1(SkPMColor px[1])61     void store1(SkPMColor px[1]) const { memcpy(px, this,  4); }
62 
63     // 1, 2, or 4 SkPMColors with 16-bit components.
64     // This is most useful as the result of a multiply, e.g. from mulWiden().
65     class Wide : public Sk16h {
66     public:
Wide(const Sk16h & v)67         Wide(const Sk16h& v) : Sk16h(v) {}
68 
69         // Add, then pack the top byte of each component back down into 4 SkPMColors.
70         Sk4px addNarrowHi(const Sk16h&) const;
71 
72         // Rounds, i.e. (x+127) / 255.
73         Sk4px div255() const;
74 
75         // These just keep the types as Wide so the user doesn't have to keep casting.
76         Wide operator * (const Wide& o) const { return INHERITED::operator*(o); }
77         Wide operator + (const Wide& o) const { return INHERITED::operator+(o); }
78         Wide operator - (const Wide& o) const { return INHERITED::operator-(o); }
79         Wide operator >> (int bits) const { return INHERITED::operator>>(bits); }
80         Wide operator << (int bits) const { return INHERITED::operator<<(bits); }
81 
82     private:
83         using INHERITED = Sk16h;
84     };
85 
86     Wide widen() const;               // Widen 8-bit values to low 8-bits of 16-bit lanes.
87     Wide mulWiden(const Sk16b&) const;  // 8-bit x 8-bit -> 16-bit components.
88 
89     // The only 8-bit multiply we use is 8-bit x 8-bit -> 16-bit.  Might as well make it pithy.
90     Wide operator * (const Sk4px& o) const { return this->mulWiden(o); }
91 
92     // These just keep the types as Sk4px so the user doesn't have to keep casting.
93     Sk4px operator + (const Sk4px& o) const { return INHERITED::operator+(o); }
94     Sk4px operator - (const Sk4px& o) const { return INHERITED::operator-(o); }
95     Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
thenElse(const Sk4px & t,const Sk4px & e)96     Sk4px thenElse(const Sk4px& t, const Sk4px& e) const { return INHERITED::thenElse(t,e); }
97 
98     // Generally faster than (*this * o).div255().
99     // May be incorrect by +-1, but is always exactly correct when *this or o is 0 or 255.
approxMulDiv255(const Sk16b & o)100     Sk4px approxMulDiv255(const Sk16b& o) const {
101         // (x*y + x) / 256 meets these criteria.  (As of course does (x*y + y) / 256 by symmetry.)
102         // FYI: (x*y + 255) / 256 also meets these criteria.  In my brief testing, it was slower.
103         return this->widen().addNarrowHi(*this * o);
104     }
105 
106     // A generic driver that maps fn over a src array into a dst array.
107     // fn should take an Sk4px (4 src pixels) and return an Sk4px (4 dst pixels).
108     template <typename Fn>
MapSrc(int n,SkPMColor * dst,const SkPMColor * src,const Fn & fn)109     [[maybe_unused]] static void MapSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
110         SkASSERT(dst);
111         SkASSERT(src);
112         // This looks a bit odd, but it helps loop-invariant hoisting across different calls to fn.
113         // Basically, we need to make sure we keep things inside a single loop.
114         while (n > 0) {
115             if (n >= 8) {
116                 Sk4px dst0 = fn(Load4(src+0)),
117                       dst4 = fn(Load4(src+4));
118                 dst0.store4(dst+0);
119                 dst4.store4(dst+4);
120                 dst += 8; src += 8; n -= 8;
121                 continue;  // Keep our stride at 8 pixels as long as possible.
122             }
123             SkASSERT(n <= 7);
124             if (n >= 4) {
125                 fn(Load4(src)).store4(dst);
126                 dst += 4; src += 4; n -= 4;
127             }
128             if (n >= 2) {
129                 fn(Load2(src)).store2(dst);
130                 dst += 2; src += 2; n -= 2;
131             }
132             if (n >= 1) {
133                 fn(Load1(src)).store1(dst);
134             }
135             break;
136         }
137     }
138 
139     // As above, but with dst4' = fn(dst4, src4).
140     template <typename Fn>
MapDstSrc(int n,SkPMColor * dst,const SkPMColor * src,const Fn & fn)141     [[maybe_unused]] static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src,
142                                            const Fn& fn) {
143         SkASSERT(dst);
144         SkASSERT(src);
145         while (n > 0) {
146             if (n >= 8) {
147                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
148                       dst4 = fn(Load4(dst+4), Load4(src+4));
149                 dst0.store4(dst+0);
150                 dst4.store4(dst+4);
151                 dst += 8; src += 8; n -= 8;
152                 continue;  // Keep our stride at 8 pixels as long as possible.
153             }
154             SkASSERT(n <= 7);
155             if (n >= 4) {
156                 fn(Load4(dst), Load4(src)).store4(dst);
157                 dst += 4; src += 4; n -= 4;
158             }
159             if (n >= 2) {
160                 fn(Load2(dst), Load2(src)).store2(dst);
161                 dst += 2; src += 2; n -= 2;
162             }
163             if (n >= 1) {
164                 fn(Load1(dst), Load1(src)).store1(dst);
165             }
166             break;
167         }
168     }
169 
170     // As above, but with dst4' = fn(dst4, alpha4).
171     template <typename Fn>
MapDstAlpha(int n,SkPMColor * dst,const SkAlpha * a,const Fn & fn)172     [[maybe_unused]] static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a,
173                                              const Fn& fn) {
174         SkASSERT(dst);
175         SkASSERT(a);
176         while (n > 0) {
177             if (n >= 8) {
178                 Sk4px dst0 = fn(Load4(dst+0), Load4Alphas(a+0)),
179                       dst4 = fn(Load4(dst+4), Load4Alphas(a+4));
180                 dst0.store4(dst+0);
181                 dst4.store4(dst+4);
182                 dst += 8; a += 8; n -= 8;
183                 continue;  // Keep our stride at 8 pixels as long as possible.
184             }
185             SkASSERT(n <= 7);
186             if (n >= 4) {
187                 fn(Load4(dst), Load4Alphas(a)).store4(dst);
188                 dst += 4; a += 4; n -= 4;
189             }
190             if (n >= 2) {
191                 fn(Load2(dst), Load2Alphas(a)).store2(dst);
192                 dst += 2; a += 2; n -= 2;
193             }
194             if (n >= 1) {
195                 fn(Load1(dst), Sk16b(*a)).store1(dst);
196             }
197             break;
198         }
199     }
200 
201     // As above, but with dst4' = fn(dst4, src4, alpha4).
202     template <typename Fn>
MapDstSrcAlpha(int n,SkPMColor * dst,const SkPMColor * src,const SkAlpha * a,const Fn & fn)203     [[maybe_unused]] static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src,
204                                                 const SkAlpha* a, const Fn& fn) {
205         SkASSERT(dst);
206         SkASSERT(src);
207         SkASSERT(a);
208         while (n > 0) {
209             if (n >= 8) {
210                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
211                       dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
212                 dst0.store4(dst+0);
213                 dst4.store4(dst+4);
214                 dst += 8; src += 8; a += 8; n -= 8;
215                 continue;  // Keep our stride at 8 pixels as long as possible.
216             }
217             SkASSERT(n <= 7);
218             if (n >= 4) {
219                 fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
220                 dst += 4; src += 4; a += 4; n -= 4;
221             }
222             if (n >= 2) {
223                 fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
224                 dst += 2; src += 2; a += 2; n -= 2;
225             }
226             if (n >= 1) {
227                 fn(Load1(dst), Load1(src), Sk16b(*a)).store1(dst);
228             }
229             break;
230         }
231     }
232 
233 private:
234     Sk4px() = default;
235 
236     using INHERITED = Sk16b;
237 };
238 
239 static_assert(sizeof(Sk4px) == sizeof(Sk16b));
240 static_assert(sizeof(Sk4px) == 16);
241 
242 }  // namespace
243 
244 #ifdef SKNX_NO_SIMD
245     #include "src/opts/Sk4px_none.h"
246 #else
247     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
248         #include "src/opts/Sk4px_SSE2.h"
249     #elif defined(SK_ARM_HAS_NEON)
250         #include "src/opts/Sk4px_NEON.h"
251     #else
252         #include "src/opts/Sk4px_none.h"
253     #endif
254 #endif
255 
256 #endif//Sk4px_DEFINED
257