mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 13 | #include "SkColorData.h" |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 14 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 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 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 22 | // 1, 2 or 4 SkPMColors, generally vectorized. |
| 23 | class Sk4px : public Sk16b { |
| 24 | public: |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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 | |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 28 | Sk4px(const Sk16b& v) : INHERITED(v) {} |
| 29 | |
mtklein | 2d8d33e | 2015-05-13 14:08:45 -0700 | [diff] [blame] | 30 | Sk4px alphas() const; // ARGB argb XYZW xyzw -> AAAA aaaa XXXX xxxx |
| 31 | |
mtklein | 0135a41 | 2015-05-15 10:36:21 -0700 | [diff] [blame] | 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 | |
mtklein | 2d8d33e | 2015-05-13 14:08:45 -0700 | [diff] [blame] | 36 | Sk4px inv() const { return Sk16b(255) - *this; } |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 37 | |
| 38 | // When loading or storing fewer than 4 SkPMColors, we use the low lanes. |
mtklein | 2d8d33e | 2015-05-13 14:08:45 -0700 | [diff] [blame] | 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 ???? ???? ???? |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 42 | |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 43 | // Ditto for Alphas... Load2Alphas fills the low two lanes of Sk4px. |
| 44 | static Sk4px Load4Alphas(const SkAlpha[4]); // AaXx -> AAAA aaaa XXXX xxxx |
mtklein | 2d8d33e | 2015-05-13 14:08:45 -0700 | [diff] [blame] | 45 | static Sk4px Load2Alphas(const SkAlpha[2]); // Aa -> AAAA aaaa ???? ???? |
mtklein | 8a90edc | 2015-05-13 12:19:42 -0700 | [diff] [blame] | 46 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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 | 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; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 59 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 60 | // Rounds, i.e. (x+127) / 255. |
mtklein | cbf4fba | 2015-11-17 14:19:52 -0800 | [diff] [blame] | 61 | Sk4px div255() const; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 62 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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); } |
| 69 | static Wide Min(const Wide& a, const Wide& b) { return INHERITED::Min(a,b); } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 70 | Wide thenElse(const Wide& t, const Wide& e) const { return INHERITED::thenElse(t,e); } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 71 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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 |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 78 | Wide widenLoHi() const; // ARGB -> AA RR GG BB |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 79 | Wide mulWiden(const Sk16b&) const; // 8-bit x 8-bit -> 16-bit components. |
| 80 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 87 | 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); } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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 | Sk4px approxMulDiv255(const Sk16b& o) const { |
mtklein | 9b77796 | 2015-05-18 07:03:01 -0700 | [diff] [blame] | 93 | // (x*y + x) / 256 meets these criteria. (As of course does (x*y + y) / 256 by symmetry.) |
mtklein | 6904d1d | 2015-08-27 12:05:56 -0700 | [diff] [blame] | 94 | // FYI: (x*y + 255) / 256 also meets these criteria. In my brief testing, it was slower. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 95 | return this->widenLo().addNarrowHi(*this * o); |
mtklein | 9b77796 | 2015-05-18 07:03:01 -0700 | [diff] [blame] | 96 | } |
| 97 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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). |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 100 | template <typename Fn> |
| 101 | static void MapSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) { |
mtklein | 15f4951 | 2015-08-13 07:51:29 -0700 | [diff] [blame] | 102 | SkASSERT(dst); |
| 103 | SkASSERT(src); |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 106 | while (n > 0) { |
| 107 | if (n >= 8) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 108 | Sk4px dst0 = fn(Load4(src+0)), |
| 109 | dst4 = fn(Load4(src+4)); |
| 110 | dst0.store4(dst+0); |
| 111 | dst4.store4(dst+4); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 112 | dst += 8; src += 8; n -= 8; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 113 | continue; // Keep our stride at 8 pixels as long as possible. |
| 114 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 115 | SkASSERT(n <= 7); |
| 116 | if (n >= 4) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 117 | fn(Load4(src)).store4(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 118 | dst += 4; src += 4; n -= 4; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 119 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 120 | if (n >= 2) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 121 | fn(Load2(src)).store2(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 122 | dst += 2; src += 2; n -= 2; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 123 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 124 | if (n >= 1) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 125 | fn(Load1(src)).store1(dst); |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 131 | // As above, but with dst4' = fn(dst4, src4). |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 132 | template <typename Fn> |
| 133 | static void MapDstSrc(int n, SkPMColor* dst, const SkPMColor* src, const Fn& fn) { |
mtklein | 15f4951 | 2015-08-13 07:51:29 -0700 | [diff] [blame] | 134 | SkASSERT(dst); |
| 135 | SkASSERT(src); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 136 | while (n > 0) { |
| 137 | if (n >= 8) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 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); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 142 | dst += 8; src += 8; n -= 8; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 143 | continue; // Keep our stride at 8 pixels as long as possible. |
| 144 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 145 | SkASSERT(n <= 7); |
| 146 | if (n >= 4) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 147 | fn(Load4(dst), Load4(src)).store4(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 148 | dst += 4; src += 4; n -= 4; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 149 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 150 | if (n >= 2) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 151 | fn(Load2(dst), Load2(src)).store2(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 152 | dst += 2; src += 2; n -= 2; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 153 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 154 | if (n >= 1) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 155 | fn(Load1(dst), Load1(src)).store1(dst); |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 161 | // As above, but with dst4' = fn(dst4, alpha4). |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 162 | template <typename Fn> |
| 163 | static void MapDstAlpha(int n, SkPMColor* dst, const SkAlpha* a, const Fn& fn) { |
mtklein | 15f4951 | 2015-08-13 07:51:29 -0700 | [diff] [blame] | 164 | SkASSERT(dst); |
| 165 | SkASSERT(a); |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame] | 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 | |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 191 | // As above, but with dst4' = fn(dst4, src4, alpha4). |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 192 | template <typename Fn> |
| 193 | static void MapDstSrcAlpha(int n, SkPMColor* dst, const SkPMColor* src, const SkAlpha* a, |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 194 | const Fn& fn) { |
mtklein | 15f4951 | 2015-08-13 07:51:29 -0700 | [diff] [blame] | 195 | SkASSERT(dst); |
| 196 | SkASSERT(src); |
| 197 | SkASSERT(a); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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)); |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 202 | dst0.store4(dst+0); |
| 203 | dst4.store4(dst+4); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 204 | dst += 8; src += 8; a += 8; n -= 8; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 205 | continue; // Keep our stride at 8 pixels as long as possible. |
| 206 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 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; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 211 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 212 | if (n >= 2) { |
| 213 | fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst); |
| 214 | dst += 2; src += 2; a += 2; n -= 2; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 215 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 216 | if (n >= 1) { |
| 217 | fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst); |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 223 | private: |
| 224 | typedef Sk16b INHERITED; |
| 225 | }; |
| 226 | |
mtklein | 082e329 | 2015-08-12 11:56:43 -0700 | [diff] [blame] | 227 | } // namespace |
| 228 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 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 |