• 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     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     static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
142         SkASSERT(dst);
143         SkASSERT(src);
144         while (n > 0) {
145             if (n >= 8) {
146                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
147                       dst4 = fn(Load4(dst+4), Load4(src+4));
148                 dst0.store4(dst+0);
149                 dst4.store4(dst+4);
150                 dst += 8; src += 8; n -= 8;
151                 continue;  // Keep our stride at 8 pixels as long as possible.
152             }
153             SkASSERT(n <= 7);
154             if (n >= 4) {
155                 fn(Load4(dst), Load4(src)).store4(dst);
156                 dst += 4; src += 4; n -= 4;
157             }
158             if (n >= 2) {
159                 fn(Load2(dst), Load2(src)).store2(dst);
160                 dst += 2; src += 2; n -= 2;
161             }
162             if (n >= 1) {
163                 fn(Load1(dst), Load1(src)).store1(dst);
164             }
165             break;
166         }
167     }
168 
169     // As above, but with dst4' = fn(dst4, alpha4).
170     template <typename Fn>
MapDstAlpha(int n,SkPMColor * dst,const SkAlpha * a,const Fn & fn)171     static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) {
172         SkASSERT(dst);
173         SkASSERT(a);
174         while (n > 0) {
175             if (n >= 8) {
176                 Sk4px dst0 = fn(Load4(dst+0), Load4Alphas(a+0)),
177                       dst4 = fn(Load4(dst+4), Load4Alphas(a+4));
178                 dst0.store4(dst+0);
179                 dst4.store4(dst+4);
180                 dst += 8; a += 8; n -= 8;
181                 continue;  // Keep our stride at 8 pixels as long as possible.
182             }
183             SkASSERT(n <= 7);
184             if (n >= 4) {
185                 fn(Load4(dst), Load4Alphas(a)).store4(dst);
186                 dst += 4; a += 4; n -= 4;
187             }
188             if (n >= 2) {
189                 fn(Load2(dst), Load2Alphas(a)).store2(dst);
190                 dst += 2; a += 2; n -= 2;
191             }
192             if (n >= 1) {
193                 fn(Load1(dst), Sk16b(*a)).store1(dst);
194             }
195             break;
196         }
197     }
198 
199     // As above, but with dst4' = fn(dst4, src4, alpha4).
200     template <typename Fn>
MapDstSrcAlpha(int n,SkPMColor * dst,const SkPMColor * src,const SkAlpha * a,const Fn & fn)201     static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a,
202                                const Fn& fn) {
203         SkASSERT(dst);
204         SkASSERT(src);
205         SkASSERT(a);
206         while (n > 0) {
207             if (n >= 8) {
208                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
209                       dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
210                 dst0.store4(dst+0);
211                 dst4.store4(dst+4);
212                 dst += 8; src += 8; a += 8; n -= 8;
213                 continue;  // Keep our stride at 8 pixels as long as possible.
214             }
215             SkASSERT(n <= 7);
216             if (n >= 4) {
217                 fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
218                 dst += 4; src += 4; a += 4; n -= 4;
219             }
220             if (n >= 2) {
221                 fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
222                 dst += 2; src += 2; a += 2; n -= 2;
223             }
224             if (n >= 1) {
225                 fn(Load1(dst), Load1(src), Sk16b(*a)).store1(dst);
226             }
227             break;
228         }
229     }
230 
231 private:
232     Sk4px() = default;
233 
234     using INHERITED = Sk16b;
235 };
236 
237 static_assert(sizeof(Sk4px) == sizeof(Sk16b));
238 static_assert(sizeof(Sk4px) == 16);
239 
240 }  // namespace
241 
242 #ifdef SKNX_NO_SIMD
243     #include "src/opts/Sk4px_none.h"
244 #else
245     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
246         #include "src/opts/Sk4px_SSE2.h"
247     #elif defined(SK_ARM_HAS_NEON)
248         #include "src/opts/Sk4px_NEON.h"
249     #else
250         #include "src/opts/Sk4px_none.h"
251     #endif
252 #endif
253 
254 #endif//Sk4px_DEFINED
255