blob: 2178041bc552ec6a1795518adf232348713cab93 [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
Mike Klein3c9fbae2018-10-22 09:48:01 -040016#ifdef SK_FORCE_RASTER_PIPELINE_BLITTER
17
18namespace SK_OPTS_NS {
19 /*not static*/ inline SkXfermode* create_xfermode(SkBlendMode) { return nullptr; }
20}
21
22#else
23
Mike Kleinc33d6142018-12-12 08:47:54 -050024namespace { // NOLINT(google-build-namespaces)
mtklein27c2b092015-05-22 10:54:39 -070025
mtklein2aab22a2015-06-26 10:46:31 -070026// 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 -080027#define XFERMODE(Xfermode) \
28 struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \
mtkleind9c00372016-01-08 12:12:43 -080029 inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const
mtklein27c2b092015-05-22 10:54:39 -070030
mtklein059ac002015-06-22 10:39:38 -070031XFERMODE(Clear) { return Sk4px::DupPMColor(0); }
mtklein27c2b092015-05-22 10:54:39 -070032XFERMODE(Src) { return s; }
33XFERMODE(Dst) { return d; }
mtklein059ac002015-06-22 10:39:38 -070034XFERMODE(SrcIn) { return s.approxMulDiv255(d.alphas() ); }
35XFERMODE(SrcOut) { return s.approxMulDiv255(d.alphas().inv()); }
36XFERMODE(SrcOver) { return s + d.approxMulDiv255(s.alphas().inv()); }
mtkleind9c00372016-01-08 12:12:43 -080037XFERMODE(DstIn) { return SrcIn ()(s,d); }
38XFERMODE(DstOut) { return SrcOut ()(s,d); }
39XFERMODE(DstOver) { return SrcOver()(s,d); }
mtklein27c2b092015-05-22 10:54:39 -070040
41// [ S * Da + (1 - Sa) * D]
mtklein059ac002015-06-22 10:39:38 -070042XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); }
mtkleind9c00372016-01-08 12:12:43 -080043XFERMODE(DstATop) { return SrcATop()(s,d); }
mtklein27c2b092015-05-22 10:54:39 -070044//[ S * (1 - Da) + (1 - Sa) * D ]
mtklein059ac002015-06-22 10:39:38 -070045XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); }
mtklein27c2b092015-05-22 10:54:39 -070046// [S + D ]
47XFERMODE(Plus) { return s.saturatedAdd(d); }
48// [S * D ]
mtklein059ac002015-06-22 10:39:38 -070049XFERMODE(Modulate) { return s.approxMulDiv255(d); }
mtklein27c2b092015-05-22 10:54:39 -070050// [S + D - S * D]
51XFERMODE(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.
mtklein059ac002015-06-22 10:39:38 -070054 return s + d.approxMulDiv255(s.inv());
mtklein27c2b092015-05-22 10:54:39 -070055}
Mike Klein722a9f22018-10-18 14:59:52 -040056
mtklein2aab22a2015-06-26 10:46:31 -070057#undef XFERMODE
mtkleinb5e86112015-06-24 15:18:39 -070058
mtklein27c2b092015-05-22 10:54:39 -070059// A reasonable fallback mode for doing AA is to simply apply the transfermode first,
60// then linearly interpolate the AA.
mtkleindefa0da2016-01-08 11:45:21 -080061template <typename Xfermode>
mtkleind9c00372016-01-08 12:12:43 -080062static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) {
63 Sk4px bw = Xfermode()(d, s);
mtklein059ac002015-06-22 10:39:38 -070064 return (bw * aa + d * aa.inv()).div255();
mtklein27c2b092015-05-22 10:54:39 -070065}
66
67// For some transfermodes we specialize AA, either for correctness or performance.
mtkleindefa0da2016-01-08 11:45:21 -080068#define XFERMODE_AA(Xfermode) \
mtkleind9c00372016-01-08 12:12:43 -080069 template <> Sk4px xfer_aa<Xfermode>(const Sk4px& d, const Sk4px& s, const Sk4px& aa)
mtklein27c2b092015-05-22 10:54:39 -070070
mtkleinb5e86112015-06-24 15:18:39 -070071// Plus' clamp needs to happen after AA. skia:3852
72XFERMODE_AA(Plus) { // [ clamp( (1-AA)D + (AA)(S+D) ) == clamp(D + AA*S) ]
73 return d.saturatedAdd(s.approxMulDiv255(aa));
74}
mtklein27c2b092015-05-22 10:54:39 -070075
mtkleinb5e86112015-06-24 15:18:39 -070076#undef XFERMODE_AA
mtklein27c2b092015-05-22 10:54:39 -070077
mtkleine721a8e2016-02-06 19:12:23 -080078// 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.
81template <typename Xfermode> static void mark_dst_initialized_if_safe(void*, void*) {}
82template <> void mark_dst_initialized_if_safe<Src>(void* dst, void* end) {
83 sk_msan_mark_initialized(dst, end, "Src doesn't read dst.");
84}
85template <> 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
mtkleindefa0da2016-01-08 11:45:21 -080089template <typename Xfermode>
Mike Reedce9514c2017-06-11 22:14:14 -040090class Sk4pxXfermode : public SkXfermode {
mtklein27c2b092015-05-22 10:54:39 -070091public:
Mike Reedce9514c2017-06-11 22:14:14 -040092 Sk4pxXfermode() {}
mtklein27c2b092015-05-22 10:54:39 -070093
94 void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
mtkleine721a8e2016-02-06 19:12:23 -080095 mark_dst_initialized_if_safe<Xfermode>(dst, dst+n);
halcanary96fcdcc2015-08-27 07:41:13 -070096 if (nullptr == aa) {
mtkleind9c00372016-01-08 12:12:43 -080097 Sk4px::MapDstSrc(n, dst, src, Xfermode());
mtklein27c2b092015-05-22 10:54:39 -070098 } else {
mtkleind9c00372016-01-08 12:12:43 -080099 Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>);
mtklein27c2b092015-05-22 10:54:39 -0700100 }
101 }
mtklein2aab22a2015-06-26 10:46:31 -0700102};
103
mtklein08f92342015-08-18 12:23:28 -0700104} // namespace
105
106namespace SK_OPTS_NS {
107
Mike Kleincd71f112017-08-23 11:11:55 -0400108/*not static*/ inline SkXfermode* create_xfermode(SkBlendMode mode) {
mtklein27c2b092015-05-22 10:54:39 -0700109 switch (mode) {
mtkleindefa0da2016-01-08 11:45:21 -0800110#define CASE(Xfermode) \
Mike Reedce9514c2017-06-11 22:14:14 -0400111 case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>()
mtkleincd1930d2015-07-21 12:39:57 -0700112 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);
mtkleincd1930d2015-07-21 12:39:57 -0700127 #undef CASE
mtklein2aab22a2015-06-26 10:46:31 -0700128
mtklein27c2b092015-05-22 10:54:39 -0700129 default: break;
130 }
mtklein27c2b092015-05-22 10:54:39 -0700131 return nullptr;
132}
133
mtklein08f92342015-08-18 12:23:28 -0700134} // namespace SK_OPTS_NS
mtklein27c2b092015-05-22 10:54:39 -0700135
Mike Klein3c9fbae2018-10-22 09:48:01 -0400136#endif // #ifdef SK_FORCE_RASTER_PIPELINE_BLITTER
137
mtklein27c2b092015-05-22 10:54:39 -0700138#endif//Sk4pxXfermode_DEFINED