blob: 24a21c66c1c40ff02a1fada344cf5dd4a713ed01 [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"
mtkleinced15852015-07-22 10:52:53 -070013#include "SkColorPriv.h"
mtkleind2ffd362015-05-12 06:11:21 -070014
mtkleinaa999cb2015-05-22 17:18:21 -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
mtkleinced15852015-07-22 10:52:53 -070051 // Same as above for 565.
52 static Sk4px Load4(const SkPMColor16 src[4]);
53 static Sk4px Load2(const SkPMColor16 src[2]);
54 static Sk4px Load1(const SkPMColor16 src[1]);
55 void store4(SkPMColor16 dst[4]) const;
56 void store2(SkPMColor16 dst[2]) const;
57 void store1(SkPMColor16 dst[1]) const;
58
mtkleind2ffd362015-05-12 06:11:21 -070059 // 1, 2, or 4 SkPMColors with 16-bit components.
60 // This is most useful as the result of a multiply, e.g. from mulWiden().
61 class Wide : public Sk16h {
62 public:
63 Wide(const Sk16h& v) : Sk16h(v) {}
64
65 // Pack the top byte of each component back down into 4 SkPMColors.
66 Sk4px addNarrowHi(const Sk16h&) const;
mtklein6cbf18c2015-05-12 15:48:09 -070067
mtklein059ac002015-06-22 10:39:38 -070068 // Rounds, i.e. (x+127) / 255.
69 Sk4px div255() const {
70 // Calculated as ((x+128) + ((x+128)>>8)) >> 8.
71 auto v = *this + Sk16h(128);
72 return v.addNarrowHi(v >> 8);
mtklein6cbf18c2015-05-12 15:48:09 -070073 }
74
mtklein059ac002015-06-22 10:39:38 -070075 // 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 static Wide Min(const Wide& a, const Wide& b) { return INHERITED::Min(a,b); }
mtklein4be181e2015-07-14 10:54:19 -070082 Wide thenElse(const Wide& t, const Wide& e) const { return INHERITED::thenElse(t,e); }
mtklein059ac002015-06-22 10:39:38 -070083
mtkleind2ffd362015-05-12 06:11:21 -070084 private:
85 typedef Sk16h INHERITED;
86 };
87
88 Wide widenLo() const; // ARGB -> 0A 0R 0G 0B
89 Wide widenHi() const; // ARGB -> A0 R0 G0 B0
mtklein4be181e2015-07-14 10:54:19 -070090 Wide widenLoHi() const; // ARGB -> AA RR GG BB
mtkleind2ffd362015-05-12 06:11:21 -070091 Wide mulWiden(const Sk16b&) const; // 8-bit x 8-bit -> 16-bit components.
92
mtklein059ac002015-06-22 10:39:38 -070093 // The only 8-bit multiply we use is 8-bit x 8-bit -> 16-bit. Might as well make it pithy.
94 Wide operator * (const Sk4px& o) const { return this->mulWiden(o); }
95
96 // These just keep the types as Sk4px so the user doesn't have to keep casting.
97 Sk4px operator + (const Sk4px& o) const { return INHERITED::operator+(o); }
98 Sk4px operator - (const Sk4px& o) const { return INHERITED::operator-(o); }
mtkleinb5e86112015-06-24 15:18:39 -070099 Sk4px operator < (const Sk4px& o) const { return INHERITED::operator<(o); }
100 Sk4px thenElse(const Sk4px& t, const Sk4px& e) const { return INHERITED::thenElse(t,e); }
mtklein059ac002015-06-22 10:39:38 -0700101
102 // Generally faster than (*this * o).div255().
103 // May be incorrect by +-1, but is always exactly correct when *this or o is 0 or 255.
104 Sk4px approxMulDiv255(const Sk16b& o) const {
mtklein9b777962015-05-18 07:03:01 -0700105 // (x*y + x) / 256 meets these criteria. (As of course does (x*y + y) / 256 by symmetry.)
mtklein059ac002015-06-22 10:39:38 -0700106 return this->widenLo().addNarrowHi(*this * o);
mtklein9b777962015-05-18 07:03:01 -0700107 }
108
mtkleind2ffd362015-05-12 06:11:21 -0700109 // A generic driver that maps fn over a src array into a dst array.
110 // fn should take an Sk4px (4 src pixels) and return an Sk4px (4 dst pixels).
mtkleinced15852015-07-22 10:52:53 -0700111 template <typename Fn, typename Dst>
112 static void MapSrc(int n, Dst* dst, const SkPMColor* src, const Fn& fn) {
mtkleind2ffd362015-05-12 06:11:21 -0700113 // This looks a bit odd, but it helps loop-invariant hoisting across different calls to fn.
114 // Basically, we need to make sure we keep things inside a single loop.
mtklein059ac002015-06-22 10:39:38 -0700115 while (n > 0) {
116 if (n >= 8) {
mtkleind2ffd362015-05-12 06:11:21 -0700117 Sk4px dst0 = fn(Load4(src+0)),
118 dst4 = fn(Load4(src+4));
119 dst0.store4(dst+0);
120 dst4.store4(dst+4);
mtklein059ac002015-06-22 10:39:38 -0700121 dst += 8; src += 8; n -= 8;
mtkleind2ffd362015-05-12 06:11:21 -0700122 continue; // Keep our stride at 8 pixels as long as possible.
123 }
mtklein059ac002015-06-22 10:39:38 -0700124 SkASSERT(n <= 7);
125 if (n >= 4) {
mtkleind2ffd362015-05-12 06:11:21 -0700126 fn(Load4(src)).store4(dst);
mtklein059ac002015-06-22 10:39:38 -0700127 dst += 4; src += 4; n -= 4;
mtkleind2ffd362015-05-12 06:11:21 -0700128 }
mtklein059ac002015-06-22 10:39:38 -0700129 if (n >= 2) {
mtkleind2ffd362015-05-12 06:11:21 -0700130 fn(Load2(src)).store2(dst);
mtklein059ac002015-06-22 10:39:38 -0700131 dst += 2; src += 2; n -= 2;
mtkleind2ffd362015-05-12 06:11:21 -0700132 }
mtklein059ac002015-06-22 10:39:38 -0700133 if (n >= 1) {
mtkleind2ffd362015-05-12 06:11:21 -0700134 fn(Load1(src)).store1(dst);
135 }
136 break;
137 }
138 }
139
mtklein6cbf18c2015-05-12 15:48:09 -0700140 // As above, but with dst4' = fn(dst4, src4).
mtkleinced15852015-07-22 10:52:53 -0700141 template <typename Fn, typename Dst>
142 static void MapDstSrc(int n, Dst* dst, const SkPMColor* src, const Fn& fn) {
mtklein059ac002015-06-22 10:39:38 -0700143 while (n > 0) {
144 if (n >= 8) {
mtklein6cbf18c2015-05-12 15:48:09 -0700145 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0)),
146 dst4 = fn(Load4(dst+4), Load4(src+4));
147 dst0.store4(dst+0);
148 dst4.store4(dst+4);
mtklein059ac002015-06-22 10:39:38 -0700149 dst += 8; src += 8; n -= 8;
mtklein6cbf18c2015-05-12 15:48:09 -0700150 continue; // Keep our stride at 8 pixels as long as possible.
151 }
mtklein059ac002015-06-22 10:39:38 -0700152 SkASSERT(n <= 7);
153 if (n >= 4) {
mtklein6cbf18c2015-05-12 15:48:09 -0700154 fn(Load4(dst), Load4(src)).store4(dst);
mtklein059ac002015-06-22 10:39:38 -0700155 dst += 4; src += 4; n -= 4;
mtklein6cbf18c2015-05-12 15:48:09 -0700156 }
mtklein059ac002015-06-22 10:39:38 -0700157 if (n >= 2) {
mtklein6cbf18c2015-05-12 15:48:09 -0700158 fn(Load2(dst), Load2(src)).store2(dst);
mtklein059ac002015-06-22 10:39:38 -0700159 dst += 2; src += 2; n -= 2;
mtklein6cbf18c2015-05-12 15:48:09 -0700160 }
mtklein059ac002015-06-22 10:39:38 -0700161 if (n >= 1) {
mtklein6cbf18c2015-05-12 15:48:09 -0700162 fn(Load1(dst), Load1(src)).store1(dst);
163 }
164 break;
165 }
166 }
167
168 // As above, but with dst4' = fn(dst4, src4, alpha4).
mtkleinced15852015-07-22 10:52:53 -0700169 template <typename Fn, typename Dst>
170 static void MapDstSrcAlpha(int n, Dst* dst, const SkPMColor* src, const SkAlpha* a,
mtklein059ac002015-06-22 10:39:38 -0700171 const Fn& fn) {
172 while (n > 0) {
173 if (n >= 8) {
174 Sk4px dst0 = fn(Load4(dst+0), Load4(src+0), Load4Alphas(a+0)),
175 dst4 = fn(Load4(dst+4), Load4(src+4), Load4Alphas(a+4));
mtklein6cbf18c2015-05-12 15:48:09 -0700176 dst0.store4(dst+0);
177 dst4.store4(dst+4);
mtklein059ac002015-06-22 10:39:38 -0700178 dst += 8; src += 8; a += 8; n -= 8;
mtklein6cbf18c2015-05-12 15:48:09 -0700179 continue; // Keep our stride at 8 pixels as long as possible.
180 }
mtklein059ac002015-06-22 10:39:38 -0700181 SkASSERT(n <= 7);
182 if (n >= 4) {
183 fn(Load4(dst), Load4(src), Load4Alphas(a)).store4(dst);
184 dst += 4; src += 4; a += 4; n -= 4;
mtklein6cbf18c2015-05-12 15:48:09 -0700185 }
mtklein059ac002015-06-22 10:39:38 -0700186 if (n >= 2) {
187 fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst);
188 dst += 2; src += 2; a += 2; n -= 2;
mtklein6cbf18c2015-05-12 15:48:09 -0700189 }
mtklein059ac002015-06-22 10:39:38 -0700190 if (n >= 1) {
191 fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst);
mtklein6cbf18c2015-05-12 15:48:09 -0700192 }
193 break;
194 }
195 }
196
mtkleind2ffd362015-05-12 06:11:21 -0700197private:
198 typedef Sk16b INHERITED;
199};
200
mtkleinaa999cb2015-05-22 17:18:21 -0700201} // namespace
202
mtkleind2ffd362015-05-12 06:11:21 -0700203#ifdef SKNX_NO_SIMD
204 #include "../opts/Sk4px_none.h"
205#else
206 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
207 #include "../opts/Sk4px_SSE2.h"
208 #elif defined(SK_ARM_HAS_NEON)
209 #include "../opts/Sk4px_NEON.h"
210 #else
211 #include "../opts/Sk4px_none.h"
212 #endif
213#endif
214
215#endif//Sk4px_DEFINED