Line data Source code
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:
25 172 : 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 :
28 37502 : 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 :
36 7328 : 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:
55 51296 : 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 14656 : Wide operator * (const Wide& o) const { return INHERITED::operator*(o); }
65 14656 : Wide operator + (const Wide& o) const { return INHERITED::operator+(o); }
66 0 : Wide operator - (const Wide& o) const { return INHERITED::operator-(o); }
67 14656 : Wide operator >> (int bits) const { return INHERITED::operator>>(bits); }
68 0 : Wide operator << (int bits) const { return INHERITED::operator<<(bits); }
69 0 : static Wide Min(const Wide& a, const Wide& b) { return INHERITED::Min(a,b); }
70 0 : 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 7328 : 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 7328 : Sk4px operator + (const Sk4px& o) const { return INHERITED::operator+(o); }
86 0 : Sk4px operator - (const Sk4px& o) const { return INHERITED::operator-(o); }
87 0 : Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
88 0 : 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.
92 7328 : 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 7328 : 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>
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>
133 0 : static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
134 0 : SkASSERT(dst);
135 0 : SkASSERT(src);
136 0 : while (n > 0) {
137 0 : if (n >= 8) {
138 0 : Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
139 0 : dst4 = fn(Load4(dst+4), Load4(src+4));
140 0 : dst0.store4(dst+0);
141 0 : dst4.store4(dst+4);
142 0 : dst += 8; src += 8; n -= 8;
143 0 : continue; // Keep our stride at 8 pixels as long as possible.
144 : }
145 0 : SkASSERT(n <= 7);
146 0 : if (n >= 4) {
147 0 : fn(Load4(dst), Load4(src)).store4(dst);
148 0 : dst += 4; src += 4; n -= 4;
149 : }
150 0 : if (n >= 2) {
151 0 : fn(Load2(dst), Load2(src)).store2(dst);
152 0 : dst += 2; src += 2; n -= 2;
153 : }
154 0 : if (n >= 1) {
155 0 : fn(Load1(dst), Load1(src)).store1(dst);
156 : }
157 0 : break;
158 : }
159 0 : }
160 :
161 : // As above, but with dst4' = fn(dst4, alpha4).
162 : template <typename Fn>
163 687 : static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) {
164 687 : SkASSERT(dst);
165 687 : SkASSERT(a);
166 3921 : while (n > 0) {
167 1979 : if (n >= 8) {
168 1617 : Sk4px dst0 = fn(Load4(dst+0), Load4Alphas(a+0)),
169 1617 : dst4 = fn(Load4(dst+4), Load4Alphas(a+4));
170 1617 : dst0.store4(dst+0);
171 1617 : dst4.store4(dst+4);
172 1617 : dst += 8; a += 8; n -= 8;
173 1617 : continue; // Keep our stride at 8 pixels as long as possible.
174 : }
175 362 : SkASSERT(n <= 7);
176 362 : if (n >= 4) {
177 106 : fn(Load4(dst), Load4Alphas(a)).store4(dst);
178 106 : dst += 4; a += 4; n -= 4;
179 : }
180 362 : if (n >= 2) {
181 238 : fn(Load2(dst), Load2Alphas(a)).store2(dst);
182 238 : dst += 2; a += 2; n -= 2;
183 : }
184 362 : if (n >= 1) {
185 86 : fn(Load1(dst), DupAlpha(*a)).store1(dst);
186 : }
187 362 : break;
188 : }
189 687 : }
190 :
191 : // As above, but with dst4' = fn(dst4, src4, alpha4).
192 : template <typename Fn>
193 0 : static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a,
194 : const Fn& fn) {
195 0 : SkASSERT(dst);
196 0 : SkASSERT(src);
197 0 : SkASSERT(a);
198 0 : while (n > 0) {
199 0 : if (n >= 8) {
200 0 : Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
201 0 : dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
202 0 : dst0.store4(dst+0);
203 0 : dst4.store4(dst+4);
204 0 : dst += 8; src += 8; a += 8; n -= 8;
205 0 : continue; // Keep our stride at 8 pixels as long as possible.
206 : }
207 0 : SkASSERT(n <= 7);
208 0 : if (n >= 4) {
209 0 : fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
210 0 : dst += 4; src += 4; a += 4; n -= 4;
211 : }
212 0 : if (n >= 2) {
213 0 : fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
214 0 : dst += 2; src += 2; a += 2; n -= 2;
215 : }
216 0 : if (n >= 1) {
217 0 : fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst);
218 : }
219 0 : break;
220 : }
221 0 : }
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
|