blob: bc95596efe3425ddfa0352ad8298397c52ed0944 [file] [log] [blame]
mtkleind2ffd362015-05-12 06:11:21 -07001/*
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"
Cary Clarka4083c92017-09-15 11:59:23 -040013#include "SkColorData.h"
mtkleind2ffd362015-05-12 06:11:21 -070014
mtklein082e3292015-08-12 11:56:43 -070015// 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).
20namespace {
21
mtkleind2ffd362015-05-12 06:11:21 -070022// 1, 2 or 4 SkPMColors, generally vectorized.
23class Sk4px : public Sk16b {
24public:
mtklein059ac002015-06-22 10:39:38 -070025 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
mtklein8a90edc2015-05-13 12:19:42 -070028 Sk4px(const Sk16b& v) : INHERITED(v) {}
29
mtklein2d8d33e2015-05-13 14:08:45 -070030 Sk4px alphas() const; // ARGB argb XYZW xyzw -> AAAA aaaa XXXX xxxx
31
mtklein0135a412015-05-15 10:36:21 -070032 // 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
mtklein2d8d33e2015-05-13 14:08:45 -070036 Sk4px inv() const { return Sk16b(255) - *this; }
mtkleind2ffd362015-05-12 06:11:21 -070037
38 // When loading or storing fewer than 4 SkPMColors, we use the low lanes.
mtklein2d8d33e2015-05-13 14:08:45 -070039 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 ???? ???? ????
mtkleind2ffd362015-05-12 06:11:21 -070042
mtklein8a90edc2015-05-13 12:19:42 -070043 // Ditto for Alphas... Load2Alphas fills the low two lanes of Sk4px.
44 static Sk4px Load4Alphas(const SkAlpha[4]); // AaXx -> AAAA aaaa XXXX xxxx
mtklein2d8d33e2015-05-13 14:08:45 -070045 static Sk4px Load2Alphas(const SkAlpha[2]); // Aa -> AAAA aaaa ???? ????
mtklein8a90edc2015-05-13 12:19:42 -070046
mtkleind2ffd362015-05-12 06:11:21 -070047 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 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;
mtklein6cbf18c2015-05-12 15:48:09 -070059
mtklein059ac002015-06-22 10:39:38 -070060 // Rounds, i.e. (x+127) / 255.
mtkleincbf4fba2015-11-17 14:19:52 -080061 Sk4px div255() const;
mtklein6cbf18c2015-05-12 15:48:09 -070062
mtklein059ac002015-06-22 10:39:38 -070063 // 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); }
69 static Wide Min(const Wide& a, const Wide& b) { return INHERITED::Min(a,b); }
mtklein4be181e2015-07-14 10:54:19 -070070 Wide thenElse(const Wide& t, const Wide& e) const { return INHERITED::thenElse(t,e); }
mtklein059ac002015-06-22 10:39:38 -070071
mtkleind2ffd362015-05-12 06:11:21 -070072 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
mtklein4be181e2015-07-14 10:54:19 -070078 Wide widenLoHi() const; // ARGB -> AA RR GG BB
mtkleind2ffd362015-05-12 06:11:21 -070079 Wide mulWiden(const Sk16b&) const; // 8-bit x 8-bit -> 16-bit components.
80
mtklein059ac002015-06-22 10:39:38 -070081 // 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); }
mtkleinb5e86112015-06-24 15:18:39 -070087 Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
88 Sk4px thenElse(const Sk4px& t, const Sk4px& e) const { return INHERITED::thenElse(t,e); }
mtklein059ac002015-06-22 10:39:38 -070089
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 Sk4px approxMulDiv255(const Sk16b& o) const {
mtklein9b777962015-05-18 07:03:01 -070093 // (x*y + x) / 256 meets these criteria. (As of course does (x*y + y) / 256 by symmetry.)
mtklein6904d1d2015-08-27 12:05:56 -070094 // FYI: (x*y + 255) / 256 also meets these criteria. In my brief testing, it was slower.
mtklein059ac002015-06-22 10:39:38 -070095 return this->widenLo().addNarrowHi(*this * o);
mtklein9b777962015-05-18 07:03:01 -070096 }
97
mtkleind2ffd362015-05-12 06:11:21 -070098 // 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).
mtkleindefa0da2016-01-08 11:45:21 -0800100 template <typename Fn>
101 static void MapSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
mtklein15f49512015-08-13 07:51:29 -0700102 SkASSERT(dst);
103 SkASSERT(src);
mtkleind2ffd362015-05-12 06:11:21 -0700104 // 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.
mtklein059ac002015-06-22 10:39:38 -0700106 while (n > 0) {
107 if (n >= 8) {
mtkleind2ffd362015-05-12 06:11:21 -0700108 Sk4px dst0 = fn(Load4(src+0)),
109 dst4 = fn(Load4(src+4));
110 dst0.store4(dst+0);
111 dst4.store4(dst+4);
mtklein059ac002015-06-22 10:39:38 -0700112 dst += 8; src += 8; n -= 8;
mtkleind2ffd362015-05-12 06:11:21 -0700113 continue; // Keep our stride at 8 pixels as long as possible.
114 }
mtklein059ac002015-06-22 10:39:38 -0700115 SkASSERT(n <= 7);
116 if (n >= 4) {
mtkleind2ffd362015-05-12 06:11:21 -0700117 fn(Load4(src)).store4(dst);
mtklein059ac002015-06-22 10:39:38 -0700118 dst += 4; src += 4; n -= 4;
mtkleind2ffd362015-05-12 06:11:21 -0700119 }
mtklein059ac002015-06-22 10:39:38 -0700120 if (n >= 2) {
mtkleind2ffd362015-05-12 06:11:21 -0700121 fn(Load2(src)).store2(dst);
mtklein059ac002015-06-22 10:39:38 -0700122 dst += 2; src += 2; n -= 2;
mtkleind2ffd362015-05-12 06:11:21 -0700123 }
mtklein059ac002015-06-22 10:39:38 -0700124 if (n >= 1) {
mtkleind2ffd362015-05-12 06:11:21 -0700125 fn(Load1(src)).store1(dst);
126 }
127 break;
128 }
129 }
130
mtklein6cbf18c2015-05-12 15:48:09 -0700131 // As above, but with dst4' = fn(dst4, src4).
mtkleindefa0da2016-01-08 11:45:21 -0800132 template <typename Fn>
133 static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) {
mtklein15f49512015-08-13 07:51:29 -0700134 SkASSERT(dst);
135 SkASSERT(src);
mtklein059ac002015-06-22 10:39:38 -0700136 while (n > 0) {
137 if (n >= 8) {
mtklein6cbf18c2015-05-12 15:48:09 -0700138 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);
mtklein059ac002015-06-22 10:39:38 -0700142 dst += 8; src += 8; n -= 8;
mtklein6cbf18c2015-05-12 15:48:09 -0700143 continue; // Keep our stride at 8 pixels as long as possible.
144 }
mtklein059ac002015-06-22 10:39:38 -0700145 SkASSERT(n <= 7);
146 if (n >= 4) {
mtklein6cbf18c2015-05-12 15:48:09 -0700147 fn(Load4(dst), Load4(src)).store4(dst);
mtklein059ac002015-06-22 10:39:38 -0700148 dst += 4; src += 4; n -= 4;
mtklein6cbf18c2015-05-12 15:48:09 -0700149 }
mtklein059ac002015-06-22 10:39:38 -0700150 if (n >= 2) {
mtklein6cbf18c2015-05-12 15:48:09 -0700151 fn(Load2(dst), Load2(src)).store2(dst);
mtklein059ac002015-06-22 10:39:38 -0700152 dst += 2; src += 2; n -= 2;
mtklein6cbf18c2015-05-12 15:48:09 -0700153 }
mtklein059ac002015-06-22 10:39:38 -0700154 if (n >= 1) {
mtklein6cbf18c2015-05-12 15:48:09 -0700155 fn(Load1(dst), Load1(src)).store1(dst);
156 }
157 break;
158 }
159 }
160
mtklein49779832015-08-10 12:58:17 -0700161 // As above, but with dst4' = fn(dst4, alpha4).
mtkleindefa0da2016-01-08 11:45:21 -0800162 template <typename Fn>
163 static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) {
mtklein15f49512015-08-13 07:51:29 -0700164 SkASSERT(dst);
165 SkASSERT(a);
mtklein49779832015-08-10 12:58:17 -0700166 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
mtklein6cbf18c2015-05-12 15:48:09 -0700191 // As above, but with dst4' = fn(dst4, src4, alpha4).
mtkleindefa0da2016-01-08 11:45:21 -0800192 template <typename Fn>
193 static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a,
mtklein059ac002015-06-22 10:39:38 -0700194 const Fn& fn) {
mtklein15f49512015-08-13 07:51:29 -0700195 SkASSERT(dst);
196 SkASSERT(src);
197 SkASSERT(a);
mtklein059ac002015-06-22 10:39:38 -0700198 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));
mtklein6cbf18c2015-05-12 15:48:09 -0700202 dst0.store4(dst+0);
203 dst4.store4(dst+4);
mtklein059ac002015-06-22 10:39:38 -0700204 dst += 8; src += 8; a += 8; n -= 8;
mtklein6cbf18c2015-05-12 15:48:09 -0700205 continue; // Keep our stride at 8 pixels as long as possible.
206 }
mtklein059ac002015-06-22 10:39:38 -0700207 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;
mtklein6cbf18c2015-05-12 15:48:09 -0700211 }
mtklein059ac002015-06-22 10:39:38 -0700212 if (n >= 2) {
213 fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
214 dst += 2; src += 2; a += 2; n -= 2;
mtklein6cbf18c2015-05-12 15:48:09 -0700215 }
mtklein059ac002015-06-22 10:39:38 -0700216 if (n >= 1) {
217 fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst);
mtklein6cbf18c2015-05-12 15:48:09 -0700218 }
219 break;
220 }
221 }
222
mtkleind2ffd362015-05-12 06:11:21 -0700223private:
224 typedef Sk16b INHERITED;
225};
226
mtklein082e3292015-08-12 11:56:43 -0700227} // namespace
228
mtkleind2ffd362015-05-12 06:11:21 -0700229#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