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 "include/private/SkNx.h" |
12 | #include "src/core/Sk4px.h" |
13 | #include "src/core/SkMSAN.h" |
14 | #include "src/core/SkXfermodePriv.h" |
15 | |
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 | |
24 | namespace { // NOLINT(google-build-namespaces) |
25 | |
26 | // Most xfermodes can be done most efficiently 4 pixels at a time in 8 or 16-bit fixed point. |
27 | #define XFERMODE(Xfermode) \ |
28 | struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \ |
29 | inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const |
30 | |
31 | XFERMODE(Clear) { return Sk4px::DupPMColor(0); } |
32 | XFERMODE(Src) { return s; } |
33 | XFERMODE(Dst) { return d; } |
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()); } |
37 | XFERMODE(DstIn) { return SrcIn ()(s,d); } |
38 | XFERMODE(DstOut) { return SrcOut ()(s,d); } |
39 | XFERMODE(DstOver) { return SrcOver()(s,d); } |
40 | |
41 | // [ S * Da + (1 - Sa) * D] |
42 | XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); } |
43 | XFERMODE(DstATop) { return SrcATop()(s,d); } |
44 | //[ S * (1 - Da) + (1 - Sa) * D ] |
45 | XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); } |
46 | // [S + D ] |
47 | XFERMODE(Plus) { return s.saturatedAdd(d); } |
48 | // [S * D ] |
49 | XFERMODE(Modulate) { return s.approxMulDiv255(d); } |
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. |
54 | return s + d.approxMulDiv255(s.inv()); |
55 | } |
56 | |
57 | #undef XFERMODE |
58 | |
59 | // A reasonable fallback mode for doing AA is to simply apply the transfermode first, |
60 | // then linearly interpolate the AA. |
61 | template <typename Xfermode> |
62 | static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) { |
63 | Sk4px bw = Xfermode()(d, s); |
64 | return (bw * aa + d * aa.inv()).div255(); |
65 | } |
66 | |
67 | // For some transfermodes we specialize AA, either for correctness or performance. |
68 | #define XFERMODE_AA(Xfermode) \ |
69 | template <> Sk4px xfer_aa<Xfermode>(const Sk4px& d, const Sk4px& s, const Sk4px& aa) |
70 | |
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 | } |
75 | |
76 | #undef XFERMODE_AA |
77 | |
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 | |
89 | template <typename Xfermode> |
90 | class Sk4pxXfermode : public SkXfermode { |
91 | public: |
92 | Sk4pxXfermode() {} |
93 | |
94 | void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override { |
95 | mark_dst_initialized_if_safe<Xfermode>(dst, dst+n); |
96 | if (nullptr == aa) { |
97 | Sk4px::MapDstSrc(n, dst, src, Xfermode()); |
98 | } else { |
99 | Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>); |
100 | } |
101 | } |
102 | }; |
103 | |
104 | } // namespace |
105 | |
106 | namespace SK_OPTS_NS { |
107 | |
108 | /*not static*/ inline SkXfermode* create_xfermode(SkBlendMode mode) { |
109 | switch (mode) { |
110 | #define CASE(Xfermode) \ |
111 | case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>() |
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); |
127 | #undef CASE |
128 | |
129 | default: break; |
130 | } |
131 | return nullptr; |
132 | } |
133 | |
134 | } // namespace SK_OPTS_NS |
135 | |
136 | #endif // #ifdef SK_FORCE_RASTER_PIPELINE_BLITTER |
137 | |
138 | #endif//Sk4pxXfermode_DEFINED |
139 | |