• 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 "SkNx.h"
12 #include "SkColor.h"
13 #include "SkColorPriv.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 {
21 
22 // 1, 2 or 4 SkPMColors, generally vectorized.
23 class Sk4px : public Sk16b {
24 public:
DupAlpha(SkAlpha a)25     static Sk4px DupAlpha(SkAlpha a) { return Sk16b(a); }  //    a -> aaaa aaaa aaaa aaaa
26     static Sk4px DupPMColor(SkPMColor c);                  // argb -> argb argb argb argb
27 
Sk4px(const Sk16b & v)28     Sk4px(const Sk16b& v) : INHERITED(v) {}
29 
30     Sk4px alphas() const;  // ARGB argb XYZW xyzw -> AAAA aaaa XXXX xxxx
31 
32     // Mask away color or alpha lanes.
33     Sk4px zeroColors() const;  // ARGB argb XYZW xyzw -> A000 a000 X000 x000
34     Sk4px zeroAlphas() const;  // ARGB argb XYZW xyzw -> 0RGB 0rgb 0YZW 0yzw
35 
inv()36     Sk4px inv() const { return Sk16b(255) - *this; }
37 
38     // When loading or storing fewer than 4 SkPMColors, we use the low lanes.
39     static Sk4px Load4(const SkPMColor[4]);  // PMColor[4] -> ARGB argb XYZW xyzw
40     static Sk4px Load2(const SkPMColor[2]);  // PMColor[2] -> ARGB argb ???? ????
41     static Sk4px Load1(const SkPMColor[1]);  // PMColor[1] -> ARGB ???? ???? ????
42 
43     // Ditto for Alphas... Load2Alphas fills the low two lanes of Sk4px.
44     static Sk4px Load4Alphas(const SkAlpha[4]);  // AaXx -> AAAA aaaa XXXX xxxx
45     static Sk4px Load2Alphas(const SkAlpha[2]);  // Aa   -> AAAA aaaa ???? ????
46 
47     void store4(SkPMColor[4]) const;
48     void store2(SkPMColor[2]) const;
49     void store1(SkPMColor[1]) const;
50 
51     // 1, 2, or 4 SkPMColors with 16-bit components.
52     // This is most useful as the result of a multiply, e.g. from mulWiden().
53     class Wide : public Sk16h {
54     public:
Wide(const Sk16h & v)55         Wide(const Sk16h& v) : Sk16h(v) {}
56 
57         // Pack the top byte of each component back down into 4 SkPMColors.
58         Sk4px addNarrowHi(const Sk16h&) const;
59 
60         // Rounds, i.e. (x+127) / 255.
61         Sk4px div255() const;
62 
63         // These just keep the types as Wide so the user doesn't have to keep casting.
64         Wide operator * (const Wide& o) const { return INHERITED::operator*(o); }
65         Wide operator + (const Wide& o) const { return INHERITED::operator+(o); }
66         Wide operator - (const Wide& o) const { return INHERITED::operator-(o); }
67         Wide operator >> (int bits) const { return INHERITED::operator>>(bits); }
68         Wide operator << (int bits) const { return INHERITED::operator<<(bits); }
Min(const Wide & a,const Wide & b)69         static Wide Min(const Wide& a, const Wide& b) { return INHERITED::Min(a,b); }
thenElse(const Wide & t,const Wide & e)70         Wide thenElse(const Wide& t, const Wide& e) const { return INHERITED::thenElse(t,e); }
71 
72     private:
73         typedef Sk16h INHERITED;
74     };
75 
76     Wide widenLo() const;               // ARGB -> 0A 0R 0G 0B
77     Wide widenHi() const;               // ARGB -> A0 R0 G0 B0
78     Wide widenLoHi() const;             // ARGB -> AA RR GG BB
79     Wide mulWiden(const Sk16b&) const;  // 8-bit x 8-bit -> 16-bit components.
80 
81     // The only 8-bit multiply we use is 8-bit x 8-bit -> 16-bit.  Might as well make it pithy.
82     Wide operator * (const Sk4px& o) const { return this->mulWiden(o); }
83 
84     // These just keep the types as Sk4px so the user doesn't have to keep casting.
85     Sk4px operator + (const Sk4px& o) const { return INHERITED::operator+(o); }
86     Sk4px operator - (const Sk4px& o) const { return INHERITED::operator-(o); }
87     Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
thenElse(const Sk4px & t,const Sk4px & e)88     Sk4px thenElse(const Sk4px& t, const Sk4px& e) const { return INHERITED::thenElse(t,e); }
89 
90     // Generally faster than (*this * o).div255().
91     // May be incorrect by +-1, but is always exactly correct when *this or o is 0 or 255.
approxMulDiv255(const Sk16b & o)92     Sk4px approxMulDiv255(const Sk16b& o) const {
93         // (x*y + x) / 256 meets these criteria.  (As of course does (x*y + y) / 256 by symmetry.)
94         // FYI: (x*y + 255) / 256 also meets these criteria.  In my brief testing, it was slower.
95         return this->widenLo().addNarrowHi(*this * o);
96     }
97 
98     // A generic driver that maps fn over a src array into a dst array.
99     // fn should take an Sk4px (4 src pixels) and return an Sk4px (4 dst pixels).
100     template <typename Fn>
MapSrc(int n,SkPMColor * dst,const SkPMColor * src,const Fn & fn)101     static void MapSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
102         SkASSERT(dst);
103         SkASSERT(src);
104         // This looks a bit odd, but it helps loop-invariant hoisting across different calls to fn.
105         // Basically, we need to make sure we keep things inside a single loop.
106         while (n > 0) {
107             if (n >= 8) {
108                 Sk4px dst0 = fn(Load4(src+0)),
109                       dst4 = fn(Load4(src+4));
110                 dst0.store4(dst+0);
111                 dst4.store4(dst+4);
112                 dst += 8; src += 8; n -= 8;
113                 continue;  // Keep our stride at 8 pixels as long as possible.
114             }
115             SkASSERT(n <= 7);
116             if (n >= 4) {
117                 fn(Load4(src)).store4(dst);
118                 dst += 4; src += 4; n -= 4;
119             }
120             if (n >= 2) {
121                 fn(Load2(src)).store2(dst);
122                 dst += 2; src += 2; n -= 2;
123             }
124             if (n >= 1) {
125                 fn(Load1(src)).store1(dst);
126             }
127             break;
128         }
129     }
130 
131     // As above, but with dst4' = fn(dst4, src4).
132     template <typename Fn>
MapDstSrc(int n,SkPMColor * dst,const SkPMColor * src,const Fn & fn)133     static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
134         SkASSERT(dst);
135         SkASSERT(src);
136         while (n > 0) {
137             if (n >= 8) {
138                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
139                       dst4 = fn(Load4(dst+4), Load4(src+4));
140                 dst0.store4(dst+0);
141                 dst4.store4(dst+4);
142                 dst += 8; src += 8; n -= 8;
143                 continue;  // Keep our stride at 8 pixels as long as possible.
144             }
145             SkASSERT(n <= 7);
146             if (n >= 4) {
147                 fn(Load4(dst), Load4(src)).store4(dst);
148                 dst += 4; src += 4; n -= 4;
149             }
150             if (n >= 2) {
151                 fn(Load2(dst), Load2(src)).store2(dst);
152                 dst += 2; src += 2; n -= 2;
153             }
154             if (n >= 1) {
155                 fn(Load1(dst), Load1(src)).store1(dst);
156             }
157             break;
158         }
159     }
160 
161     // As above, but with dst4' = fn(dst4, alpha4).
162     template <typename Fn>
MapDstAlpha(int n,SkPMColor * dst,const SkAlpha * a,const Fn & fn)163     static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) {
164         SkASSERT(dst);
165         SkASSERT(a);
166         while (n > 0) {
167             if (n >= 8) {
168                 Sk4px dst0 = fn(Load4(dst+0), Load4Alphas(a+0)),
169                       dst4 = fn(Load4(dst+4), Load4Alphas(a+4));
170                 dst0.store4(dst+0);
171                 dst4.store4(dst+4);
172                 dst += 8; a += 8; n -= 8;
173                 continue;  // Keep our stride at 8 pixels as long as possible.
174             }
175             SkASSERT(n <= 7);
176             if (n >= 4) {
177                 fn(Load4(dst), Load4Alphas(a)).store4(dst);
178                 dst += 4; a += 4; n -= 4;
179             }
180             if (n >= 2) {
181                 fn(Load2(dst), Load2Alphas(a)).store2(dst);
182                 dst += 2; a += 2; n -= 2;
183             }
184             if (n >= 1) {
185                 fn(Load1(dst), DupAlpha(*a)).store1(dst);
186             }
187             break;
188         }
189     }
190 
191     // As above, but with dst4' = fn(dst4, src4, alpha4).
192     template <typename Fn>
MapDstSrcAlpha(int n,SkPMColor * dst,const SkPMColor * src,const SkAlpha * a,const Fn & fn)193     static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a,
194                                const Fn& fn) {
195         SkASSERT(dst);
196         SkASSERT(src);
197         SkASSERT(a);
198         while (n > 0) {
199             if (n >= 8) {
200                 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
201                       dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
202                 dst0.store4(dst+0);
203                 dst4.store4(dst+4);
204                 dst += 8; src += 8; a += 8; n -= 8;
205                 continue;  // Keep our stride at 8 pixels as long as possible.
206             }
207             SkASSERT(n <= 7);
208             if (n >= 4) {
209                 fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
210                 dst += 4; src += 4; a += 4; n -= 4;
211             }
212             if (n >= 2) {
213                 fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
214                 dst += 2; src += 2; a += 2; n -= 2;
215             }
216             if (n >= 1) {
217                 fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst);
218             }
219             break;
220         }
221     }
222 
223 private:
224     typedef Sk16b INHERITED;
225 };
226 
227 }  // namespace
228 
229 #ifdef SKNX_NO_SIMD
230     #include "../opts/Sk4px_none.h"
231 #else
232     #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
233         #include "../opts/Sk4px_SSE2.h"
234     #elif defined(SK_ARM_HAS_NEON)
235         #include "../opts/Sk4px_NEON.h"
236     #else
237         #include "../opts/Sk4px_none.h"
238     #endif
239 #endif
240 
241 #endif//Sk4px_DEFINED
242