mtklein | 27c2b09 | 2015-05-22 10:54:39 -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 Sk4pxXfermode_DEFINED |
| 9 | #define Sk4pxXfermode_DEFINED |
| 10 | |
| 11 | #include "Sk4px.h" |
mtklein | e721a8e | 2016-02-06 19:12:23 -0800 | [diff] [blame] | 12 | #include "SkMSAN.h" |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 13 | #include "SkNx.h" |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 14 | #include "SkXfermodePriv.h" |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 15 | |
mtklein | 08f9234 | 2015-08-18 12:23:28 -0700 | [diff] [blame] | 16 | namespace { |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 17 | |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 18 | // Most xfermodes can be done most efficiently 4 pixels at a time in 8 or 16-bit fixed point. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 19 | #define XFERMODE(Xfermode) \ |
| 20 | struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \ |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 21 | inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 22 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 23 | XFERMODE(Clear) { return Sk4px::DupPMColor(0); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 24 | XFERMODE(Src) { return s; } |
| 25 | XFERMODE(Dst) { return d; } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 26 | XFERMODE(SrcIn) { return s.approxMulDiv255(d.alphas() ); } |
| 27 | XFERMODE(SrcOut) { return s.approxMulDiv255(d.alphas().inv()); } |
| 28 | XFERMODE(SrcOver) { return s + d.approxMulDiv255(s.alphas().inv()); } |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 29 | XFERMODE(DstIn) { return SrcIn ()(s,d); } |
| 30 | XFERMODE(DstOut) { return SrcOut ()(s,d); } |
| 31 | XFERMODE(DstOver) { return SrcOver()(s,d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 32 | |
| 33 | // [ S * Da + (1 - Sa) * D] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 34 | XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); } |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 35 | XFERMODE(DstATop) { return SrcATop()(s,d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 36 | //[ S * (1 - Da) + (1 - Sa) * D ] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 37 | XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 38 | // [S + D ] |
| 39 | XFERMODE(Plus) { return s.saturatedAdd(d); } |
| 40 | // [S * D ] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 41 | XFERMODE(Modulate) { return s.approxMulDiv255(d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 42 | // [S + D - S * D] |
| 43 | XFERMODE(Screen) { |
| 44 | // Doing the math as S + (1-S)*D or S + (D - S*D) means the add and subtract can be done |
| 45 | // in 8-bit space without overflow. S + (1-S)*D is a touch faster because inv() is cheap. |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 46 | return s + d.approxMulDiv255(s.inv()); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 47 | } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 48 | XFERMODE(Multiply) { return (s * d.alphas().inv() + d * s.alphas().inv() + s*d).div255(); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 49 | // [ Sa + Da - Sa*Da, Sc + Dc - 2*min(Sc*Da, Dc*Sa) ] (And notice Sa*Da == min(Sa*Da, Da*Sa).) |
| 50 | XFERMODE(Difference) { |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 51 | auto m = Sk4px::Wide::Min(s * d.alphas(), d * s.alphas()).div255(); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 52 | // There's no chance of underflow, and if we subtract m before adding s+d, no overflow. |
| 53 | return (s - m) + (d - m.zeroAlphas()); |
| 54 | } |
| 55 | // [ Sa + Da - Sa*Da, Sc + Dc - 2*Sc*Dc ] |
| 56 | XFERMODE(Exclusion) { |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 57 | auto p = s.approxMulDiv255(d); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 58 | // There's no chance of underflow, and if we subtract p before adding src+dst, no overflow. |
| 59 | return (s - p) + (d - p.zeroAlphas()); |
| 60 | } |
| 61 | |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 62 | // We take care to use exact math for these next few modes where alphas |
| 63 | // and colors are calculated using significantly different math. We need |
| 64 | // to preserve premul invariants, and exact math makes this easier. |
| 65 | // |
| 66 | // TODO: Some of these implementations might be able to be sped up a bit |
| 67 | // while maintaining exact math, but let's follow up with that. |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 68 | |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 69 | XFERMODE(HardLight) { |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 70 | auto sa = s.alphas(), |
| 71 | da = d.alphas(); |
| 72 | |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 73 | auto srcover = s + (d * sa.inv()).div255(); |
| 74 | |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 75 | auto isLite = ((sa-s) < s).widenLoHi(); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 76 | |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 77 | auto lite = sa*da - ((da-d)*(sa-s) << 1), |
| 78 | dark = s*d << 1, |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 79 | both = s*da.inv() + d*sa.inv(); |
| 80 | |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 81 | auto alphas = srcover; |
mtklein | 4be181e | 2015-07-14 10:54:19 -0700 | [diff] [blame] | 82 | auto colors = (both + isLite.thenElse(lite, dark)).div255(); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 83 | return alphas.zeroColors() + colors.zeroAlphas(); |
| 84 | } |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 85 | XFERMODE(Overlay) { return HardLight()(s,d); } |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 86 | |
| 87 | XFERMODE(Darken) { |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 88 | auto sa = s.alphas(), |
| 89 | da = d.alphas(); |
| 90 | |
| 91 | auto sda = (s*da).div255(), |
| 92 | dsa = (d*sa).div255(); |
| 93 | |
| 94 | auto srcover = s + (d * sa.inv()).div255(), |
| 95 | dstover = d + (s * da.inv()).div255(); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 96 | auto alphas = srcover, |
| 97 | colors = (sda < dsa).thenElse(srcover, dstover); |
| 98 | return alphas.zeroColors() + colors.zeroAlphas(); |
| 99 | } |
| 100 | XFERMODE(Lighten) { |
mtklein | 44cf62c | 2015-07-14 13:38:28 -0700 | [diff] [blame] | 101 | auto sa = s.alphas(), |
| 102 | da = d.alphas(); |
| 103 | |
| 104 | auto sda = (s*da).div255(), |
| 105 | dsa = (d*sa).div255(); |
| 106 | |
| 107 | auto srcover = s + (d * sa.inv()).div255(), |
| 108 | dstover = d + (s * da.inv()).div255(); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 109 | auto alphas = srcover, |
mtklein | b743c66 | 2015-06-29 12:04:10 -0700 | [diff] [blame] | 110 | colors = (dsa < sda).thenElse(srcover, dstover); |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 111 | return alphas.zeroColors() + colors.zeroAlphas(); |
| 112 | } |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 113 | #undef XFERMODE |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 114 | |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 115 | // Some xfermodes use math like divide or sqrt that's best done in floats 1 pixel at a time. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 116 | #define XFERMODE(Xfermode) \ |
| 117 | struct Xfermode { Sk4f operator()(const Sk4f&, const Sk4f&) const; }; \ |
| 118 | inline Sk4f Xfermode::operator()(const Sk4f& d, const Sk4f& s) const |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 119 | |
| 120 | static inline Sk4f a_rgb(const Sk4f& a, const Sk4f& rgb) { |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 121 | static_assert(SK_A32_SHIFT == 24, ""); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 122 | return a * Sk4f(0,0,0,1) + rgb * Sk4f(1,1,1,0); |
| 123 | } |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 124 | static inline Sk4f alphas(const Sk4f& f) { |
mtklein | 7c249e5 | 2016-02-21 10:54:19 -0800 | [diff] [blame] | 125 | return f[SK_A32_SHIFT/8]; |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 126 | } |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 127 | |
| 128 | XFERMODE(ColorDodge) { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 129 | auto sa = alphas(s), |
| 130 | da = alphas(d), |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 131 | isa = Sk4f(1)-sa, |
| 132 | ida = Sk4f(1)-da; |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 133 | |
| 134 | auto srcover = s + d*isa, |
| 135 | dstover = d + s*ida, |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 136 | otherwise = sa * Sk4f::Min(da, (d*sa)*(sa-s).invert()) + s*ida + d*isa; |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 137 | |
| 138 | // Order matters here, preferring d==0 over s==sa. |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 139 | auto colors = (d == Sk4f(0)).thenElse(dstover, |
| 140 | (s == sa).thenElse(srcover, |
| 141 | otherwise)); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 142 | return a_rgb(srcover, colors); |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 143 | } |
| 144 | XFERMODE(ColorBurn) { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 145 | auto sa = alphas(s), |
| 146 | da = alphas(d), |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 147 | isa = Sk4f(1)-sa, |
| 148 | ida = Sk4f(1)-da; |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 149 | |
| 150 | auto srcover = s + d*isa, |
| 151 | dstover = d + s*ida, |
mtklein | f8f90e4 | 2016-03-21 10:04:46 -0700 | [diff] [blame] | 152 | otherwise = sa*(da-Sk4f::Min(da, (da-d)*sa*s.invert())) + s*ida + d*isa; |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 153 | |
| 154 | // Order matters here, preferring d==da over s==0. |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 155 | auto colors = (d == da).thenElse(dstover, |
| 156 | (s == Sk4f(0)).thenElse(srcover, |
| 157 | otherwise)); |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 158 | return a_rgb(srcover, colors); |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 159 | } |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 160 | XFERMODE(SoftLight) { |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 161 | auto sa = alphas(s), |
| 162 | da = alphas(d), |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 163 | isa = Sk4f(1)-sa, |
| 164 | ida = Sk4f(1)-da; |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 165 | |
| 166 | // Some common terms. |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 167 | auto m = (da > Sk4f(0)).thenElse(d / da, Sk4f(0)), |
| 168 | s2 = Sk4f(2)*s, |
| 169 | m4 = Sk4f(4)*m; |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 170 | |
| 171 | // The logic forks three ways: |
| 172 | // 1. dark src? |
| 173 | // 2. light src, dark dst? |
| 174 | // 3. light src, light dst? |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 175 | auto darkSrc = d*(sa + (s2 - sa)*(Sk4f(1) - m)), // Used in case 1. |
| 176 | darkDst = (m4*m4 + m4)*(m - Sk4f(1)) + Sk4f(7)*m, // Used in case 2. |
| 177 | liteDst = m.sqrt() - m, // Used in case 3. |
| 178 | liteSrc = d*sa + da*(s2-sa)*(Sk4f(4)*d <= da).thenElse(darkDst, liteDst); // Case 2 or 3? |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 179 | |
| 180 | auto alpha = s + d*isa; |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 181 | auto colors = s*ida + d*isa + (s2 <= sa).thenElse(darkSrc, liteSrc); // Case 1 or 2/3? |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 182 | |
mtklein | dde03ff | 2015-08-31 15:26:08 -0700 | [diff] [blame] | 183 | return a_rgb(alpha, colors); |
mtklein | 823b2a7 | 2015-06-29 12:16:26 -0700 | [diff] [blame] | 184 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 185 | #undef XFERMODE |
| 186 | |
| 187 | // A reasonable fallback mode for doing AA is to simply apply the transfermode first, |
| 188 | // then linearly interpolate the AA. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 189 | template <typename Xfermode> |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 190 | static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) { |
| 191 | Sk4px bw = Xfermode()(d, s); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 192 | return (bw * aa + d * aa.inv()).div255(); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // For some transfermodes we specialize AA, either for correctness or performance. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 196 | #define XFERMODE_AA(Xfermode) \ |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 197 | template <> Sk4px xfer_aa<Xfermode>(const Sk4px& d, const Sk4px& s, const Sk4px& aa) |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 198 | |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 199 | // Plus' clamp needs to happen after AA. skia:3852 |
| 200 | XFERMODE_AA(Plus) { // [ clamp( (1-AA)D + (AA)(S+D) ) == clamp(D + AA*S) ] |
| 201 | return d.saturatedAdd(s.approxMulDiv255(aa)); |
| 202 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 203 | |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 204 | #undef XFERMODE_AA |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 205 | |
mtklein | e721a8e | 2016-02-06 19:12:23 -0800 | [diff] [blame] | 206 | // Src and Clear modes are safe to use with unitialized dst buffers, |
| 207 | // even if the implementation branches based on bytes from dst (e.g. asserts in Debug mode). |
| 208 | // For those modes, just lie to MSAN that dst is always intialized. |
| 209 | template <typename Xfermode> static void mark_dst_initialized_if_safe(void*, void*) {} |
| 210 | template <> void mark_dst_initialized_if_safe<Src>(void* dst, void* end) { |
| 211 | sk_msan_mark_initialized(dst, end, "Src doesn't read dst."); |
| 212 | } |
| 213 | template <> void mark_dst_initialized_if_safe<Clear>(void* dst, void* end) { |
| 214 | sk_msan_mark_initialized(dst, end, "Clear doesn't read dst."); |
| 215 | } |
| 216 | |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 217 | template <typename Xfermode> |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 218 | class Sk4pxXfermode : public SkXfermode { |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 219 | public: |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 220 | Sk4pxXfermode() {} |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 221 | |
| 222 | void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override { |
mtklein | e721a8e | 2016-02-06 19:12:23 -0800 | [diff] [blame] | 223 | mark_dst_initialized_if_safe<Xfermode>(dst, dst+n); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 224 | if (nullptr == aa) { |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 225 | Sk4px::MapDstSrc(n, dst, src, Xfermode()); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 226 | } else { |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 227 | Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 232 | template <typename Xfermode> |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 233 | class Sk4fXfermode : public SkXfermode { |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 234 | public: |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 235 | Sk4fXfermode() {} |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 236 | |
| 237 | void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override { |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 238 | for (int i = 0; i < n; i++) { |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 239 | dst[i] = Xfer32_1(dst[i], src[i], aa ? aa+i : nullptr); |
mtklein | ced1585 | 2015-07-22 10:52:53 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 243 | private: |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 244 | static SkPMColor Xfer32_1(SkPMColor dst, const SkPMColor src, const SkAlpha* aa) { |
| 245 | Sk4f d = Load(dst), |
| 246 | s = Load(src), |
| 247 | b = Xfermode()(d, s); |
| 248 | if (aa) { |
| 249 | Sk4f a = Sk4f(*aa) * Sk4f(1.0f/255); |
| 250 | b = b*a + d*(Sk4f(1)-a); |
| 251 | } |
| 252 | return Round(b); |
| 253 | } |
| 254 | |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 255 | static Sk4f Load(SkPMColor c) { |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 256 | return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255); |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 257 | } |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 258 | |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 259 | static SkPMColor Round(const Sk4f& f) { |
| 260 | SkPMColor c; |
mtklein | 507ef6d | 2016-01-31 08:02:47 -0800 | [diff] [blame] | 261 | SkNx_cast<uint8_t>(f * Sk4f(255) + Sk4f(0.5f)).store(&c); |
mtklein | 29d60e5 | 2015-11-19 09:10:33 -0800 | [diff] [blame] | 262 | return c; |
| 263 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
mtklein | 08f9234 | 2015-08-18 12:23:28 -0700 | [diff] [blame] | 266 | } // namespace |
| 267 | |
| 268 | namespace SK_OPTS_NS { |
| 269 | |
Mike Klein | cd71f11 | 2017-08-23 11:11:55 -0400 | [diff] [blame] | 270 | /*not static*/ inline SkXfermode* create_xfermode(SkBlendMode mode) { |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 271 | switch (mode) { |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 272 | #define CASE(Xfermode) \ |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 273 | case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>() |
mtklein | cd1930d | 2015-07-21 12:39:57 -0700 | [diff] [blame] | 274 | CASE(Clear); |
| 275 | CASE(Src); |
| 276 | CASE(Dst); |
| 277 | CASE(SrcOver); |
| 278 | CASE(DstOver); |
| 279 | CASE(SrcIn); |
| 280 | CASE(DstIn); |
| 281 | CASE(SrcOut); |
| 282 | CASE(DstOut); |
| 283 | CASE(SrcATop); |
| 284 | CASE(DstATop); |
| 285 | CASE(Xor); |
| 286 | CASE(Plus); |
| 287 | CASE(Modulate); |
| 288 | CASE(Screen); |
| 289 | CASE(Multiply); |
| 290 | CASE(Difference); |
| 291 | CASE(Exclusion); |
| 292 | CASE(HardLight); |
| 293 | CASE(Overlay); |
| 294 | CASE(Darken); |
| 295 | CASE(Lighten); |
| 296 | #undef CASE |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 297 | |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 298 | #define CASE(Xfermode) \ |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 299 | case SkBlendMode::k##Xfermode: return new Sk4fXfermode<Xfermode>() |
mtklein | cd1930d | 2015-07-21 12:39:57 -0700 | [diff] [blame] | 300 | CASE(ColorDodge); |
| 301 | CASE(ColorBurn); |
| 302 | CASE(SoftLight); |
| 303 | #undef CASE |
| 304 | |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 305 | default: break; |
| 306 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 307 | return nullptr; |
| 308 | } |
| 309 | |
mtklein | 08f9234 | 2015-08-18 12:23:28 -0700 | [diff] [blame] | 310 | } // namespace SK_OPTS_NS |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 311 | |
| 312 | #endif//Sk4pxXfermode_DEFINED |