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 | |
Mike Klein | 3c9fbae | 2018-10-22 09:48:01 -0400 | [diff] [blame] | 16 | #ifdef SK_FORCE_RASTER_PIPELINE_BLITTER |
| 17 | |
| 18 | namespace SK_OPTS_NS { |
| 19 | /*not static*/ inline SkXfermode* create_xfermode(SkBlendMode) { return nullptr; } |
| 20 | } |
| 21 | |
| 22 | #else |
| 23 | |
Mike Klein | c33d614 | 2018-12-12 08:47:54 -0500 | [diff] [blame] | 24 | namespace { // NOLINT(google-build-namespaces) |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 25 | |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 26 | // 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] | 27 | #define XFERMODE(Xfermode) \ |
| 28 | struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \ |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 29 | inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 30 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 31 | XFERMODE(Clear) { return Sk4px::DupPMColor(0); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 32 | XFERMODE(Src) { return s; } |
| 33 | XFERMODE(Dst) { return d; } |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 34 | XFERMODE(SrcIn) { return s.approxMulDiv255(d.alphas() ); } |
| 35 | XFERMODE(SrcOut) { return s.approxMulDiv255(d.alphas().inv()); } |
| 36 | XFERMODE(SrcOver) { return s + d.approxMulDiv255(s.alphas().inv()); } |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 37 | XFERMODE(DstIn) { return SrcIn ()(s,d); } |
| 38 | XFERMODE(DstOut) { return SrcOut ()(s,d); } |
| 39 | XFERMODE(DstOver) { return SrcOver()(s,d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 40 | |
| 41 | // [ S * Da + (1 - Sa) * D] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 42 | XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); } |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 43 | XFERMODE(DstATop) { return SrcATop()(s,d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 44 | //[ S * (1 - Da) + (1 - Sa) * D ] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 45 | XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 46 | // [S + D ] |
| 47 | XFERMODE(Plus) { return s.saturatedAdd(d); } |
| 48 | // [S * D ] |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 49 | XFERMODE(Modulate) { return s.approxMulDiv255(d); } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 50 | // [S + D - S * D] |
| 51 | XFERMODE(Screen) { |
| 52 | // Doing the math as S + (1-S)*D or S + (D - S*D) means the add and subtract can be done |
| 53 | // 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] | 54 | return s + d.approxMulDiv255(s.inv()); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 55 | } |
Mike Klein | 722a9f2 | 2018-10-18 14:59:52 -0400 | [diff] [blame] | 56 | |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 57 | #undef XFERMODE |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 58 | |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 59 | // A reasonable fallback mode for doing AA is to simply apply the transfermode first, |
| 60 | // then linearly interpolate the AA. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 61 | template <typename Xfermode> |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 62 | static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) { |
| 63 | Sk4px bw = Xfermode()(d, s); |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 64 | return (bw * aa + d * aa.inv()).div255(); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // For some transfermodes we specialize AA, either for correctness or performance. |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 68 | #define XFERMODE_AA(Xfermode) \ |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 69 | 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] | 70 | |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 71 | // Plus' clamp needs to happen after AA. skia:3852 |
| 72 | XFERMODE_AA(Plus) { // [ clamp( (1-AA)D + (AA)(S+D) ) == clamp(D + AA*S) ] |
| 73 | return d.saturatedAdd(s.approxMulDiv255(aa)); |
| 74 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 75 | |
mtklein | b5e8611 | 2015-06-24 15:18:39 -0700 | [diff] [blame] | 76 | #undef XFERMODE_AA |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 77 | |
mtklein | e721a8e | 2016-02-06 19:12:23 -0800 | [diff] [blame] | 78 | // Src and Clear modes are safe to use with unitialized dst buffers, |
| 79 | // even if the implementation branches based on bytes from dst (e.g. asserts in Debug mode). |
| 80 | // For those modes, just lie to MSAN that dst is always intialized. |
| 81 | template <typename Xfermode> static void mark_dst_initialized_if_safe(void*, void*) {} |
| 82 | template <> void mark_dst_initialized_if_safe<Src>(void* dst, void* end) { |
| 83 | sk_msan_mark_initialized(dst, end, "Src doesn't read dst."); |
| 84 | } |
| 85 | template <> void mark_dst_initialized_if_safe<Clear>(void* dst, void* end) { |
| 86 | sk_msan_mark_initialized(dst, end, "Clear doesn't read dst."); |
| 87 | } |
| 88 | |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 89 | template <typename Xfermode> |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 90 | class Sk4pxXfermode : public SkXfermode { |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 91 | public: |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 92 | Sk4pxXfermode() {} |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 93 | |
| 94 | 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] | 95 | mark_dst_initialized_if_safe<Xfermode>(dst, dst+n); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | if (nullptr == aa) { |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 97 | Sk4px::MapDstSrc(n, dst, src, Xfermode()); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 98 | } else { |
mtklein | d9c0037 | 2016-01-08 12:12:43 -0800 | [diff] [blame] | 99 | Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>); |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
mtklein | 08f9234 | 2015-08-18 12:23:28 -0700 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | namespace SK_OPTS_NS { |
| 107 | |
Mike Klein | cd71f11 | 2017-08-23 11:11:55 -0400 | [diff] [blame] | 108 | /*not static*/ inline SkXfermode* create_xfermode(SkBlendMode mode) { |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 109 | switch (mode) { |
mtklein | defa0da | 2016-01-08 11:45:21 -0800 | [diff] [blame] | 110 | #define CASE(Xfermode) \ |
Mike Reed | ce9514c | 2017-06-11 22:14:14 -0400 | [diff] [blame] | 111 | case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>() |
mtklein | cd1930d | 2015-07-21 12:39:57 -0700 | [diff] [blame] | 112 | CASE(Clear); |
| 113 | CASE(Src); |
| 114 | CASE(Dst); |
| 115 | CASE(SrcOver); |
| 116 | CASE(DstOver); |
| 117 | CASE(SrcIn); |
| 118 | CASE(DstIn); |
| 119 | CASE(SrcOut); |
| 120 | CASE(DstOut); |
| 121 | CASE(SrcATop); |
| 122 | CASE(DstATop); |
| 123 | CASE(Xor); |
| 124 | CASE(Plus); |
| 125 | CASE(Modulate); |
| 126 | CASE(Screen); |
mtklein | cd1930d | 2015-07-21 12:39:57 -0700 | [diff] [blame] | 127 | #undef CASE |
mtklein | 2aab22a | 2015-06-26 10:46:31 -0700 | [diff] [blame] | 128 | |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 129 | default: break; |
| 130 | } |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 131 | return nullptr; |
| 132 | } |
| 133 | |
mtklein | 08f9234 | 2015-08-18 12:23:28 -0700 | [diff] [blame] | 134 | } // namespace SK_OPTS_NS |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 135 | |
Mike Klein | 3c9fbae | 2018-10-22 09:48:01 -0400 | [diff] [blame] | 136 | #endif // #ifdef SK_FORCE_RASTER_PIPELINE_BLITTER |
| 137 | |
mtklein | 27c2b09 | 2015-05-22 10:54:39 -0700 | [diff] [blame] | 138 | #endif//Sk4pxXfermode_DEFINED |