• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
12 #include "SkMSAN.h"
13 #include "SkNx.h"
14 #include "SkXfermodePriv.h"
15 
16 namespace {
17 
18 // Most xfermodes can be done most efficiently 4 pixels at a time in 8 or 16-bit fixed point.
19 #define XFERMODE(Xfermode) \
20     struct Xfermode { Sk4px operator()(const Sk4px&, const Sk4px&) const; }; \
21     inline Sk4px Xfermode::operator()(const Sk4px& d, const Sk4px& s) const
22 
XFERMODE(Clear)23 XFERMODE(Clear) { return Sk4px::DupPMColor(0); }
XFERMODE(Src)24 XFERMODE(Src)   { return s; }
XFERMODE(Dst)25 XFERMODE(Dst)   { return d; }
XFERMODE(SrcIn)26 XFERMODE(SrcIn)   { return     s.approxMulDiv255(d.alphas()      ); }
XFERMODE(SrcOut)27 XFERMODE(SrcOut)  { return     s.approxMulDiv255(d.alphas().inv()); }
XFERMODE(SrcOver)28 XFERMODE(SrcOver) { return s + d.approxMulDiv255(s.alphas().inv()); }
XFERMODE(DstIn)29 XFERMODE(DstIn)   { return SrcIn  ()(s,d); }
XFERMODE(DstOut)30 XFERMODE(DstOut)  { return SrcOut ()(s,d); }
XFERMODE(DstOver)31 XFERMODE(DstOver) { return SrcOver()(s,d); }
32 
33 // [ S * Da + (1 - Sa) * D]
XFERMODE(SrcATop)34 XFERMODE(SrcATop) { return (s * d.alphas() + d * s.alphas().inv()).div255(); }
XFERMODE(DstATop)35 XFERMODE(DstATop) { return SrcATop()(s,d); }
36 //[ S * (1 - Da) + (1 - Sa) * D ]
XFERMODE(Xor)37 XFERMODE(Xor) { return (s * d.alphas().inv() + d * s.alphas().inv()).div255(); }
38 // [S + D ]
XFERMODE(Plus)39 XFERMODE(Plus) { return s.saturatedAdd(d); }
40 // [S * D ]
XFERMODE(Modulate)41 XFERMODE(Modulate) { return s.approxMulDiv255(d); }
42 // [S + D - S * D]
XFERMODE(Screen)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.
46     return s + d.approxMulDiv255(s.inv());
47 }
XFERMODE(Multiply)48 XFERMODE(Multiply) { return (s * d.alphas().inv() + d * s.alphas().inv() + s*d).div255(); }
49 // [ Sa + Da - Sa*Da, Sc + Dc - 2*min(Sc*Da, Dc*Sa) ]  (And notice Sa*Da == min(Sa*Da, Da*Sa).)
XFERMODE(Difference)50 XFERMODE(Difference) {
51     auto m = Sk4px::Wide::Min(s * d.alphas(), d * s.alphas()).div255();
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 ]
XFERMODE(Exclusion)56 XFERMODE(Exclusion) {
57     auto p = s.approxMulDiv255(d);
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 
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.
68 
XFERMODE(HardLight)69 XFERMODE(HardLight) {
70     auto sa = s.alphas(),
71          da = d.alphas();
72 
73     auto srcover = s + (d * sa.inv()).div255();
74 
75     auto isLite = ((sa-s) < s).widenLoHi();
76 
77     auto lite = sa*da - ((da-d)*(sa-s) << 1),
78          dark = s*d << 1,
79          both = s*da.inv() + d*sa.inv();
80 
81     auto alphas = srcover;
82     auto colors = (both + isLite.thenElse(lite, dark)).div255();
83     return alphas.zeroColors() + colors.zeroAlphas();
84 }
XFERMODE(Overlay)85 XFERMODE(Overlay) { return HardLight()(s,d); }
86 
XFERMODE(Darken)87 XFERMODE(Darken) {
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();
96     auto alphas = srcover,
97          colors = (sda < dsa).thenElse(srcover, dstover);
98     return alphas.zeroColors() + colors.zeroAlphas();
99 }
XFERMODE(Lighten)100 XFERMODE(Lighten) {
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();
109     auto alphas = srcover,
110          colors = (dsa < sda).thenElse(srcover, dstover);
111     return alphas.zeroColors() + colors.zeroAlphas();
112 }
113 #undef XFERMODE
114 
115 // Some xfermodes use math like divide or sqrt that's best done in floats 1 pixel at a time.
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
119 
a_rgb(const Sk4f & a,const Sk4f & rgb)120 static inline Sk4f a_rgb(const Sk4f& a, const Sk4f& rgb) {
121     static_assert(SK_A32_SHIFT == 24, "");
122     return a * Sk4f(0,0,0,1) + rgb * Sk4f(1,1,1,0);
123 }
alphas(const Sk4f & f)124 static inline Sk4f alphas(const Sk4f& f) {
125     return f[SK_A32_SHIFT/8];
126 }
127 
XFERMODE(ColorDodge)128 XFERMODE(ColorDodge) {
129     auto sa = alphas(s),
130          da = alphas(d),
131          isa = Sk4f(1)-sa,
132          ida = Sk4f(1)-da;
133 
134     auto srcover = s + d*isa,
135          dstover = d + s*ida,
136          otherwise = sa * Sk4f::Min(da, (d*sa)*(sa-s).invert()) + s*ida + d*isa;
137 
138     // Order matters here, preferring d==0 over s==sa.
139     auto colors = (d == Sk4f(0)).thenElse(dstover,
140                   (s ==      sa).thenElse(srcover,
141                                           otherwise));
142     return a_rgb(srcover, colors);
143 }
XFERMODE(ColorBurn)144 XFERMODE(ColorBurn) {
145     auto sa = alphas(s),
146          da = alphas(d),
147          isa = Sk4f(1)-sa,
148          ida = Sk4f(1)-da;
149 
150     auto srcover = s + d*isa,
151          dstover = d + s*ida,
152          otherwise = sa*(da-Sk4f::Min(da, (da-d)*sa*s.invert())) + s*ida + d*isa;
153 
154     // Order matters here, preferring d==da over s==0.
155     auto colors = (d ==      da).thenElse(dstover,
156                   (s == Sk4f(0)).thenElse(srcover,
157                                           otherwise));
158     return a_rgb(srcover, colors);
159 }
XFERMODE(SoftLight)160 XFERMODE(SoftLight) {
161     auto sa = alphas(s),
162          da = alphas(d),
163          isa = Sk4f(1)-sa,
164          ida = Sk4f(1)-da;
165 
166     // Some common terms.
167     auto m  = (da > Sk4f(0)).thenElse(d / da, Sk4f(0)),
168          s2 = Sk4f(2)*s,
169          m4 = Sk4f(4)*m;
170 
171     // The logic forks three ways:
172     //    1. dark src?
173     //    2. light src, dark dst?
174     //    3. light src, light dst?
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?
179 
180     auto alpha  = s + d*isa;
181     auto colors = s*ida + d*isa + (s2 <= sa).thenElse(darkSrc, liteSrc);           // Case 1 or 2/3?
182 
183     return a_rgb(alpha, colors);
184 }
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.
189 template <typename Xfermode>
xfer_aa(const Sk4px & d,const Sk4px & s,const Sk4px & aa)190 static Sk4px xfer_aa(const Sk4px& d, const Sk4px& s, const Sk4px& aa) {
191     Sk4px bw = Xfermode()(d, s);
192     return (bw * aa + d * aa.inv()).div255();
193 }
194 
195 // For some transfermodes we specialize AA, either for correctness or performance.
196 #define XFERMODE_AA(Xfermode) \
197     template <> Sk4px xfer_aa<Xfermode>(const Sk4px& d, const Sk4px& s, const Sk4px& aa)
198 
199 // Plus' clamp needs to happen after AA.  skia:3852
XFERMODE_AA(Plus)200 XFERMODE_AA(Plus) {  // [ clamp( (1-AA)D + (AA)(S+D) ) == clamp(D + AA*S) ]
201     return d.saturatedAdd(s.approxMulDiv255(aa));
202 }
203 
204 #undef XFERMODE_AA
205 
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.
mark_dst_initialized_if_safe(void *,void *)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 
217 template <typename Xfermode>
218 class Sk4pxXfermode : public SkXfermode {
219 public:
Sk4pxXfermode()220     Sk4pxXfermode() {}
221 
xfer32(SkPMColor dst[],const SkPMColor src[],int n,const SkAlpha aa[])222     void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
223         mark_dst_initialized_if_safe<Xfermode>(dst, dst+n);
224         if (nullptr == aa) {
225             Sk4px::MapDstSrc(n, dst, src, Xfermode());
226         } else {
227             Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>);
228         }
229     }
230 };
231 
232 template <typename Xfermode>
233 class Sk4fXfermode : public SkXfermode {
234 public:
Sk4fXfermode()235     Sk4fXfermode() {}
236 
xfer32(SkPMColor dst[],const SkPMColor src[],int n,const SkAlpha aa[])237     void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
238         for (int i = 0; i < n; i++) {
239             dst[i] = Xfer32_1(dst[i], src[i], aa ? aa+i : nullptr);
240         }
241     }
242 
243 private:
Xfer32_1(SkPMColor dst,const SkPMColor src,const SkAlpha * aa)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 
Load(SkPMColor c)255     static Sk4f Load(SkPMColor c) {
256         return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255);
257     }
258 
Round(const Sk4f & f)259     static SkPMColor Round(const Sk4f& f) {
260         SkPMColor c;
261         SkNx_cast<uint8_t>(f * Sk4f(255) + Sk4f(0.5f)).store(&c);
262         return c;
263     }
264 };
265 
266 } // namespace
267 
268 namespace SK_OPTS_NS {
269 
create_xfermode(SkBlendMode mode)270 static SkXfermode* create_xfermode(SkBlendMode mode) {
271     switch (mode) {
272 #define CASE(Xfermode) \
273     case SkBlendMode::k##Xfermode: return new Sk4pxXfermode<Xfermode>()
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
297 
298 #define CASE(Xfermode) \
299     case SkBlendMode::k##Xfermode: return new Sk4fXfermode<Xfermode>()
300         CASE(ColorDodge);
301         CASE(ColorBurn);
302         CASE(SoftLight);
303     #undef CASE
304 
305         default: break;
306     }
307     return nullptr;
308 }
309 
310 } // namespace SK_OPTS_NS
311 
312 #endif//Sk4pxXfermode_DEFINED
313