blob: 8f140739b20eb2040791fb3e9bd9bad0c7add2b1 [file] [log] [blame]
mtklein27c2b092015-05-22 10:54:39 -07001/*
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"
mtkleine721a8e2016-02-06 19:12:23 -080012#include "SkMSAN.h"
mtkleindde03ff2015-08-31 15:26:08 -070013#include "SkNx.h"
Mike Reedce9514c2017-06-11 22:14:14 -040014#include "SkXfermodePriv.h"
mtklein27c2b092015-05-22 10:54:39 -070015
mtklein08f92342015-08-18 12:23:28 -070016namespace {
mtklein27c2b092015-05-22 10:54:39 -070017
mtklein2aab22a2015-06-26 10:46:31 -070018// Most xfermodes can be done most efficiently 4 pixels at a time in 8 or 16-bit fixed point.
mtkleindefa0da2016-01-08 11:45:21 -080019#define XFERMODE(Xfermode) \
20 struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \
mtkleind9c00372016-01-08 12:12:43 -080021 inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const
mtklein27c2b092015-05-22 10:54:39 -070022
mtklein059ac002015-06-22 10:39:38 -070023XFERMODE(Clear) { return Sk4px::DupPMColor(0); }
mtklein27c2b092015-05-22 10:54:39 -070024XFERMODE(Src) { return s; }
25XFERMODE(Dst) { return d; }
mtklein059ac002015-06-22 10:39:38 -070026XFERMODE(SrcIn) { return s.approxMulDiv255(d.alphas() ); }
27XFERMODE(SrcOut) { return s.approxMulDiv255(d.alphas().inv()); }
28XFERMODE(SrcOver) { return s + d.approxMulDiv255(s.alphas().inv()); }
mtkleind9c00372016-01-08 12:12:43 -080029XFERMODE(DstIn) { return SrcIn ()(s,d); }
30XFERMODE(DstOut) { return SrcOut ()(s,d); }
31XFERMODE(DstOver) { return SrcOver()(s,d); }
mtklein27c2b092015-05-22 10:54:39 -070032
33// [ S * Da + (1 - Sa) * D]
mtklein059ac002015-06-22 10:39:38 -070034XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); }
mtkleind9c00372016-01-08 12:12:43 -080035XFERMODE(DstATop) { return SrcATop()(s,d); }
mtklein27c2b092015-05-22 10:54:39 -070036//[ S * (1 - Da) + (1 - Sa) * D ]
mtklein059ac002015-06-22 10:39:38 -070037XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); }
mtklein27c2b092015-05-22 10:54:39 -070038// [S + D ]
39XFERMODE(Plus) { return s.saturatedAdd(d); }
40// [S * D ]
mtklein059ac002015-06-22 10:39:38 -070041XFERMODE(Modulate) { return s.approxMulDiv255(d); }
mtklein27c2b092015-05-22 10:54:39 -070042// [S + D - S * D]
43XFERMODE(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.
mtklein059ac002015-06-22 10:39:38 -070046 return s + d.approxMulDiv255(s.inv());
mtklein27c2b092015-05-22 10:54:39 -070047}
mtklein059ac002015-06-22 10:39:38 -070048XFERMODE(Multiply) { return (s * d.alphas().inv() + d * s.alphas().inv() + s*d).div255(); }
mtklein27c2b092015-05-22 10:54:39 -070049// [ Sa + Da - Sa*Da, Sc + Dc - 2*min(Sc*Da, Dc*Sa) ] (And notice Sa*Da == min(Sa*Da, Da*Sa).)
50XFERMODE(Difference) {
mtklein059ac002015-06-22 10:39:38 -070051 auto m = Sk4px::Wide::Min(s * d.alphas(), d * s.alphas()).div255();
mtklein27c2b092015-05-22 10:54:39 -070052 // 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 ]
56XFERMODE(Exclusion) {
mtklein059ac002015-06-22 10:39:38 -070057 auto p = s.approxMulDiv255(d);
mtklein27c2b092015-05-22 10:54:39 -070058 // 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
mtklein44cf62c2015-07-14 13:38:28 -070062// 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.
mtkleinb5e86112015-06-24 15:18:39 -070068
mtklein44cf62c2015-07-14 13:38:28 -070069XFERMODE(HardLight) {
mtkleinb5e86112015-06-24 15:18:39 -070070 auto sa = s.alphas(),
71 da = d.alphas();
72
mtklein44cf62c2015-07-14 13:38:28 -070073 auto srcover = s + (d * sa.inv()).div255();
74
mtklein4be181e2015-07-14 10:54:19 -070075 auto isLite = ((sa-s) < s).widenLoHi();
mtkleinb5e86112015-06-24 15:18:39 -070076
mtklein44cf62c2015-07-14 13:38:28 -070077 auto lite = sa*da - ((da-d)*(sa-s) << 1),
78 dark = s*d << 1,
mtkleinb5e86112015-06-24 15:18:39 -070079 both = s*da.inv() + d*sa.inv();
80
mtklein44cf62c2015-07-14 13:38:28 -070081 auto alphas = srcover;
mtklein4be181e2015-07-14 10:54:19 -070082 auto colors = (both + isLite.thenElse(lite, dark)).div255();
mtkleinb5e86112015-06-24 15:18:39 -070083 return alphas.zeroColors() + colors.zeroAlphas();
84}
mtkleind9c00372016-01-08 12:12:43 -080085XFERMODE(Overlay) { return HardLight()(s,d); }
mtkleinb5e86112015-06-24 15:18:39 -070086
87XFERMODE(Darken) {
mtklein44cf62c2015-07-14 13:38:28 -070088 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();
mtkleinb5e86112015-06-24 15:18:39 -070096 auto alphas = srcover,
97 colors = (sda < dsa).thenElse(srcover, dstover);
98 return alphas.zeroColors() + colors.zeroAlphas();
99}
100XFERMODE(Lighten) {
mtklein44cf62c2015-07-14 13:38:28 -0700101 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();
mtkleinb5e86112015-06-24 15:18:39 -0700109 auto alphas = srcover,
mtkleinb743c662015-06-29 12:04:10 -0700110 colors = (dsa < sda).thenElse(srcover, dstover);
mtkleinb5e86112015-06-24 15:18:39 -0700111 return alphas.zeroColors() + colors.zeroAlphas();
112}
mtklein2aab22a2015-06-26 10:46:31 -0700113#undef XFERMODE
mtkleinb5e86112015-06-24 15:18:39 -0700114
mtklein29d60e52015-11-19 09:10:33 -0800115// Some xfermodes use math like divide or sqrt that's best done in floats 1 pixel at a time.
mtkleindefa0da2016-01-08 11:45:21 -0800116#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
mtkleindde03ff2015-08-31 15:26:08 -0700119
120static inline Sk4f a_rgb(const Sk4f& a, const Sk4f& rgb) {
mtklein29d60e52015-11-19 09:10:33 -0800121 static_assert(SK_A32_SHIFT == 24, "");
mtkleindde03ff2015-08-31 15:26:08 -0700122 return a * Sk4f(0,0,0,1) + rgb * Sk4f(1,1,1,0);
123}
mtklein29d60e52015-11-19 09:10:33 -0800124static inline Sk4f alphas(const Sk4f& f) {
mtklein7c249e52016-02-21 10:54:19 -0800125 return f[SK_A32_SHIFT/8];
mtkleindde03ff2015-08-31 15:26:08 -0700126}
mtklein2aab22a2015-06-26 10:46:31 -0700127
128XFERMODE(ColorDodge) {
mtkleindde03ff2015-08-31 15:26:08 -0700129 auto sa = alphas(s),
130 da = alphas(d),
mtklein29d60e52015-11-19 09:10:33 -0800131 isa = Sk4f(1)-sa,
132 ida = Sk4f(1)-da;
mtklein2aab22a2015-06-26 10:46:31 -0700133
134 auto srcover = s + d*isa,
135 dstover = d + s*ida,
mtkleinf8f90e42016-03-21 10:04:46 -0700136 otherwise = sa * Sk4f::Min(da, (d*sa)*(sa-s).invert()) + s*ida + d*isa;
mtklein2aab22a2015-06-26 10:46:31 -0700137
138 // Order matters here, preferring d==0 over s==sa.
mtklein29d60e52015-11-19 09:10:33 -0800139 auto colors = (d == Sk4f(0)).thenElse(dstover,
140 (s == sa).thenElse(srcover,
141 otherwise));
mtkleindde03ff2015-08-31 15:26:08 -0700142 return a_rgb(srcover, colors);
mtklein2aab22a2015-06-26 10:46:31 -0700143}
144XFERMODE(ColorBurn) {
mtkleindde03ff2015-08-31 15:26:08 -0700145 auto sa = alphas(s),
146 da = alphas(d),
mtklein29d60e52015-11-19 09:10:33 -0800147 isa = Sk4f(1)-sa,
148 ida = Sk4f(1)-da;
mtklein2aab22a2015-06-26 10:46:31 -0700149
150 auto srcover = s + d*isa,
151 dstover = d + s*ida,
mtkleinf8f90e42016-03-21 10:04:46 -0700152 otherwise = sa*(da-Sk4f::Min(da, (da-d)*sa*s.invert())) + s*ida + d*isa;
mtklein2aab22a2015-06-26 10:46:31 -0700153
154 // Order matters here, preferring d==da over s==0.
mtklein29d60e52015-11-19 09:10:33 -0800155 auto colors = (d == da).thenElse(dstover,
156 (s == Sk4f(0)).thenElse(srcover,
157 otherwise));
mtkleindde03ff2015-08-31 15:26:08 -0700158 return a_rgb(srcover, colors);
mtklein2aab22a2015-06-26 10:46:31 -0700159}
mtklein823b2a72015-06-29 12:16:26 -0700160XFERMODE(SoftLight) {
mtkleindde03ff2015-08-31 15:26:08 -0700161 auto sa = alphas(s),
162 da = alphas(d),
mtklein29d60e52015-11-19 09:10:33 -0800163 isa = Sk4f(1)-sa,
164 ida = Sk4f(1)-da;
mtklein823b2a72015-06-29 12:16:26 -0700165
166 // Some common terms.
mtklein29d60e52015-11-19 09:10:33 -0800167 auto m = (da > Sk4f(0)).thenElse(d / da, Sk4f(0)),
168 s2 = Sk4f(2)*s,
169 m4 = Sk4f(4)*m;
mtklein823b2a72015-06-29 12:16:26 -0700170
171 // The logic forks three ways:
172 // 1. dark src?
173 // 2. light src, dark dst?
174 // 3. light src, light dst?
mtklein29d60e52015-11-19 09:10:33 -0800175 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?
mtklein823b2a72015-06-29 12:16:26 -0700179
180 auto alpha = s + d*isa;
mtklein29d60e52015-11-19 09:10:33 -0800181 auto colors = s*ida + d*isa + (s2 <= sa).thenElse(darkSrc, liteSrc); // Case 1 or 2/3?
mtklein823b2a72015-06-29 12:16:26 -0700182
mtkleindde03ff2015-08-31 15:26:08 -0700183 return a_rgb(alpha, colors);
mtklein823b2a72015-06-29 12:16:26 -0700184}
mtklein27c2b092015-05-22 10:54:39 -0700185#undef XFERMODE
186
187// A reasonable fallback mode for doing AA is to simply apply the transfermode first,
188// then linearly interpolate the AA.
mtkleindefa0da2016-01-08 11:45:21 -0800189template <typename Xfermode>
mtkleind9c00372016-01-08 12:12:43 -0800190static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) {
191 Sk4px bw = Xfermode()(d, s);
mtklein059ac002015-06-22 10:39:38 -0700192 return (bw * aa + d * aa.inv()).div255();
mtklein27c2b092015-05-22 10:54:39 -0700193}
194
195// For some transfermodes we specialize AA, either for correctness or performance.
mtkleindefa0da2016-01-08 11:45:21 -0800196#define XFERMODE_AA(Xfermode) \
mtkleind9c00372016-01-08 12:12:43 -0800197 template <> Sk4px xfer_aa<Xfermode>(const Sk4px& d, const Sk4px& s, const Sk4px& aa)
mtklein27c2b092015-05-22 10:54:39 -0700198
mtkleinb5e86112015-06-24 15:18:39 -0700199// Plus' clamp needs to happen after AA. skia:3852
200XFERMODE_AA(Plus) { // [ clamp( (1-AA)D + (AA)(S+D) ) == clamp(D + AA*S) ]
201 return d.saturatedAdd(s.approxMulDiv255(aa));
202}
mtklein27c2b092015-05-22 10:54:39 -0700203
mtkleinb5e86112015-06-24 15:18:39 -0700204#undef XFERMODE_AA
mtklein27c2b092015-05-22 10:54:39 -0700205
mtkleine721a8e2016-02-06 19:12:23 -0800206// 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.
209template <typename Xfermode> static void mark_dst_initialized_if_safe(void*, void*) {}
210template <> void mark_dst_initialized_if_safe<Src>(void* dst, void* end) {
211 sk_msan_mark_initialized(dst, end, "Src doesn't read dst.");
212}
213template <> 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
mtkleindefa0da2016-01-08 11:45:21 -0800217template <typename Xfermode>
Mike Reedce9514c2017-06-11 22:14:14 -0400218class Sk4pxXfermode : public SkXfermode {
mtklein27c2b092015-05-22 10:54:39 -0700219public:
Mike Reedce9514c2017-06-11 22:14:14 -0400220 Sk4pxXfermode() {}
mtklein27c2b092015-05-22 10:54:39 -0700221
222 void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
mtkleine721a8e2016-02-06 19:12:23 -0800223 mark_dst_initialized_if_safe<Xfermode>(dst, dst+n);
halcanary96fcdcc2015-08-27 07:41:13 -0700224 if (nullptr == aa) {
mtkleind9c00372016-01-08 12:12:43 -0800225 Sk4px::MapDstSrc(n, dst, src, Xfermode());
mtklein27c2b092015-05-22 10:54:39 -0700226 } else {
mtkleind9c00372016-01-08 12:12:43 -0800227 Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>);
mtklein27c2b092015-05-22 10:54:39 -0700228 }
229 }
mtklein2aab22a2015-06-26 10:46:31 -0700230};
231
mtkleindefa0da2016-01-08 11:45:21 -0800232template <typename Xfermode>
Mike Reedce9514c2017-06-11 22:14:14 -0400233class Sk4fXfermode : public SkXfermode {
mtklein2aab22a2015-06-26 10:46:31 -0700234public:
Mike Reedce9514c2017-06-11 22:14:14 -0400235 Sk4fXfermode() {}
mtklein2aab22a2015-06-26 10:46:31 -0700236
237 void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
mtklein29d60e52015-11-19 09:10:33 -0800238 for (int i = 0; i < n; i++) {
mtkleindefa0da2016-01-08 11:45:21 -0800239 dst[i] = Xfer32_1(dst[i], src[i], aa ? aa+i : nullptr);
mtkleinced15852015-07-22 10:52:53 -0700240 }
241 }
242
mtklein2aab22a2015-06-26 10:46:31 -0700243private:
mtkleindefa0da2016-01-08 11:45:21 -0800244 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
mtklein29d60e52015-11-19 09:10:33 -0800255 static Sk4f Load(SkPMColor c) {
mtklein507ef6d2016-01-31 08:02:47 -0800256 return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255);
mtklein29d60e52015-11-19 09:10:33 -0800257 }
mtkleindefa0da2016-01-08 11:45:21 -0800258
mtklein29d60e52015-11-19 09:10:33 -0800259 static SkPMColor Round(const Sk4f& f) {
260 SkPMColor c;
mtklein507ef6d2016-01-31 08:02:47 -0800261 SkNx_cast<uint8_t>(f * Sk4f(255) + Sk4f(0.5f)).store(&c);
mtklein29d60e52015-11-19 09:10:33 -0800262 return c;
263 }
mtklein27c2b092015-05-22 10:54:39 -0700264};
265
mtklein08f92342015-08-18 12:23:28 -0700266} // namespace
267
268namespace SK_OPTS_NS {
269
Mike Kleincd71f112017-08-23 11:11:55 -0400270/*not static*/ inline SkXfermode* create_xfermode(SkBlendMode mode) {
mtklein27c2b092015-05-22 10:54:39 -0700271 switch (mode) {
mtkleindefa0da2016-01-08 11:45:21 -0800272#define CASE(Xfermode) \
Mike Reedce9514c2017-06-11 22:14:14 -0400273 case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>()
mtkleincd1930d2015-07-21 12:39:57 -0700274 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
mtklein2aab22a2015-06-26 10:46:31 -0700297
mtkleindefa0da2016-01-08 11:45:21 -0800298#define CASE(Xfermode) \
Mike Reedce9514c2017-06-11 22:14:14 -0400299 case SkBlendMode::k##Xfermode: return new Sk4fXfermode<Xfermode>()
mtkleincd1930d2015-07-21 12:39:57 -0700300 CASE(ColorDodge);
301 CASE(ColorBurn);
302 CASE(SoftLight);
303 #undef CASE
304
mtklein27c2b092015-05-22 10:54:39 -0700305 default: break;
306 }
mtklein27c2b092015-05-22 10:54:39 -0700307 return nullptr;
308}
309
mtklein08f92342015-08-18 12:23:28 -0700310} // namespace SK_OPTS_NS
mtklein27c2b092015-05-22 10:54:39 -0700311
312#endif//Sk4pxXfermode_DEFINED