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" |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame^] | 13 | #include "SkColorPriv.h" |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 14 | |
mtklein | aa999cb | 2015-05-22 17:18:21 -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 | |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame^] | 51 | // 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 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 59 | // 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; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 67 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 68 | // 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); |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 73 | } |
| 74 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 75 | // 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); } |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 82 | 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] | 83 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 84 | 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 |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 90 | Wide widenLoHi() const; // ARGB -> AA RR GG BB |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 91 | Wide mulWiden(const Sk16b&) const; // 8-bit x 8-bit -> 16-bit components. |
| 92 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 93 | // 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); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 99 | 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); } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 101 | |
| 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 { |
mtklein | 9b77796 | 2015-05-18 07:03:01 -0700 | [diff] [blame] | 105 | // (x*y + x) / 256 meets these criteria. (As of course does (x*y + y) / 256 by symmetry.) |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 106 | return this->widenLo().addNarrowHi(*this * o); |
mtklein | 9b77796 | 2015-05-18 07:03:01 -0700 | [diff] [blame] | 107 | } |
| 108 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 109 | // 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). |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame^] | 111 | template <typename Fn, typename Dst> |
| 112 | static void MapSrc(int n, Dst* dst, const SkPMColor* src, const Fn& fn) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 113 | // 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. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 115 | while (n > 0) { |
| 116 | if (n >= 8) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 117 | Sk4px dst0 = fn(Load4(src+0)), |
| 118 | dst4 = fn(Load4(src+4)); |
| 119 | dst0.store4(dst+0); |
| 120 | dst4.store4(dst+4); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 121 | dst += 8; src += 8; n -= 8; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 122 | continue; // Keep our stride at 8 pixels as long as possible. |
| 123 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 124 | SkASSERT(n <= 7); |
| 125 | if (n >= 4) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 126 | fn(Load4(src)).store4(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 127 | dst += 4; src += 4; n -= 4; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 128 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 129 | if (n >= 2) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 130 | fn(Load2(src)).store2(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 131 | dst += 2; src += 2; n -= 2; |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 132 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 133 | if (n >= 1) { |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 134 | fn(Load1(src)).store1(dst); |
| 135 | } |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 140 | // As above, but with dst4' = fn(dst4, src4). |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame^] | 141 | template <typename Fn, typename Dst> |
| 142 | static void MapDstSrc(int n, Dst* dst, const SkPMColor* src, const Fn& fn) { |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 143 | while (n > 0) { |
| 144 | if (n >= 8) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 145 | 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); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 149 | dst += 8; src += 8; n -= 8; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 150 | continue; // Keep our stride at 8 pixels as long as possible. |
| 151 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 152 | SkASSERT(n <= 7); |
| 153 | if (n >= 4) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 154 | fn(Load4(dst), Load4(src)).store4(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 155 | dst += 4; src += 4; n -= 4; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 156 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 157 | if (n >= 2) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 158 | fn(Load2(dst), Load2(src)).store2(dst); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 159 | dst += 2; src += 2; n -= 2; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 160 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 161 | if (n >= 1) { |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 162 | fn(Load1(dst), Load1(src)).store1(dst); |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // As above, but with dst4' = fn(dst4, src4, alpha4). |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame^] | 169 | template <typename Fn, typename Dst> |
| 170 | static void MapDstSrcAlpha(int n, Dst* dst, const SkPMColor* src, const SkAlpha* a, |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 171 | 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)); |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 176 | dst0.store4(dst+0); |
| 177 | dst4.store4(dst+4); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 178 | dst += 8; src += 8; a += 8; n -= 8; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 179 | continue; // Keep our stride at 8 pixels as long as possible. |
| 180 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 181 | 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; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 185 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 186 | if (n >= 2) { |
| 187 | fn(Load2(dst), Load2(src), Load2Alphas(a)).store2(dst); |
| 188 | dst += 2; src += 2; a += 2; n -= 2; |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 189 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 190 | if (n >= 1) { |
| 191 | fn(Load1(dst), Load1(src), DupAlpha(*a)).store1(dst); |
mtklein | 6cbf18c | 2015-05-12 15:48:09 -0700 | [diff] [blame] | 192 | } |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 197 | private: |
| 198 | typedef Sk16b INHERITED; |
| 199 | }; |
| 200 | |
mtklein | aa999cb | 2015-05-22 17:18:21 -0700 | [diff] [blame] | 201 | } // namespace |
| 202 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 203 | #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 |