• 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 "SkXfermode_proccoeff.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).approxInvert()) + 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.approxInvert())) + 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 SkProcCoeffXfermode {
219 public:
Sk4pxXfermode(const ProcCoeff & rec,SkXfermode::Mode mode)220     Sk4pxXfermode(const ProcCoeff& rec, SkXfermode::Mode mode)
221         : INHERITED(rec, mode) {}
222 
xfer32(SkPMColor dst[],const SkPMColor src[],int n,const SkAlpha aa[])223     void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
224         mark_dst_initialized_if_safe<Xfermode>(dst, dst+n);
225         if (nullptr == aa) {
226             Sk4px::MapDstSrc(n, dst, src, Xfermode());
227         } else {
228             Sk4px::MapDstSrcAlpha(n, dst, src, aa, xfer_aa<Xfermode>);
229         }
230     }
231 
xfer16(uint16_t dst[],const SkPMColor src[],int n,const SkAlpha aa[])232     void xfer16(uint16_t dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
233         mark_dst_initialized_if_safe<Xfermode>(dst, dst+n);
234         SkPMColor dst32[4];
235         while (n >= 4) {
236             dst32[0] = SkPixel16ToPixel32(dst[0]);
237             dst32[1] = SkPixel16ToPixel32(dst[1]);
238             dst32[2] = SkPixel16ToPixel32(dst[2]);
239             dst32[3] = SkPixel16ToPixel32(dst[3]);
240 
241             this->xfer32(dst32, src, 4, aa);
242 
243             dst[0] = SkPixel32ToPixel16(dst32[0]);
244             dst[1] = SkPixel32ToPixel16(dst32[1]);
245             dst[2] = SkPixel32ToPixel16(dst32[2]);
246             dst[3] = SkPixel32ToPixel16(dst32[3]);
247 
248             dst += 4;
249             src += 4;
250             aa  += aa ? 4 : 0;
251             n -= 4;
252         }
253         while (n) {
254             SkPMColor dst32 = SkPixel16ToPixel32(*dst);
255             this->xfer32(&dst32, src, 1, aa);
256             *dst = SkPixel32ToPixel16(dst32);
257 
258             dst += 1;
259             src += 1;
260             aa  += aa ? 1 : 0;
261             n   -= 1;
262         }
263     }
264 
265 private:
266     typedef SkProcCoeffXfermode INHERITED;
267 };
268 
269 template <typename Xfermode>
270 class Sk4fXfermode : public SkProcCoeffXfermode {
271 public:
Sk4fXfermode(const ProcCoeff & rec,SkXfermode::Mode mode)272     Sk4fXfermode(const ProcCoeff& rec, SkXfermode::Mode mode)
273         : INHERITED(rec, mode) {}
274 
xfer32(SkPMColor dst[],const SkPMColor src[],int n,const SkAlpha aa[])275     void xfer32(SkPMColor dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
276         for (int i = 0; i < n; i++) {
277             dst[i] = Xfer32_1(dst[i], src[i], aa ? aa+i : nullptr);
278         }
279     }
280 
xfer16(uint16_t dst[],const SkPMColor src[],int n,const SkAlpha aa[])281     void xfer16(uint16_t dst[], const SkPMColor src[], int n, const SkAlpha aa[]) const override {
282         for (int i = 0; i < n; i++) {
283             SkPMColor dst32 = SkPixel16ToPixel32(dst[i]);
284             dst32 = Xfer32_1(dst32, src[i], aa ? aa+i : nullptr);
285             dst[i] = SkPixel32ToPixel16(dst32);
286         }
287     }
288 
289 private:
Xfer32_1(SkPMColor dst,const SkPMColor src,const SkAlpha * aa)290     static SkPMColor Xfer32_1(SkPMColor dst, const SkPMColor src, const SkAlpha* aa) {
291         Sk4f d = Load(dst),
292              s = Load(src),
293              b = Xfermode()(d, s);
294         if (aa) {
295             Sk4f a = Sk4f(*aa) * Sk4f(1.0f/255);
296             b = b*a + d*(Sk4f(1)-a);
297         }
298         return Round(b);
299     }
300 
Load(SkPMColor c)301     static Sk4f Load(SkPMColor c) {
302         return SkNx_cast<float>(Sk4b::Load(&c)) * Sk4f(1.0f/255);
303     }
304 
Round(const Sk4f & f)305     static SkPMColor Round(const Sk4f& f) {
306         SkPMColor c;
307         SkNx_cast<uint8_t>(f * Sk4f(255) + Sk4f(0.5f)).store(&c);
308         return c;
309     }
310 
311     typedef SkProcCoeffXfermode INHERITED;
312 };
313 
314 } // namespace
315 
316 namespace SK_OPTS_NS {
317 
create_xfermode(const ProcCoeff & rec,SkXfermode::Mode mode)318 static SkXfermode* create_xfermode(const ProcCoeff& rec, SkXfermode::Mode mode) {
319     switch (mode) {
320 #define CASE(Xfermode) \
321     case SkXfermode::k##Xfermode##_Mode: return new Sk4pxXfermode<Xfermode>(rec, mode)
322         CASE(Clear);
323         CASE(Src);
324         CASE(Dst);
325         CASE(SrcOver);
326         CASE(DstOver);
327         CASE(SrcIn);
328         CASE(DstIn);
329         CASE(SrcOut);
330         CASE(DstOut);
331         CASE(SrcATop);
332         CASE(DstATop);
333         CASE(Xor);
334         CASE(Plus);
335         CASE(Modulate);
336         CASE(Screen);
337         CASE(Multiply);
338         CASE(Difference);
339         CASE(Exclusion);
340         CASE(HardLight);
341         CASE(Overlay);
342         CASE(Darken);
343         CASE(Lighten);
344     #undef CASE
345 
346 #define CASE(Xfermode) \
347     case SkXfermode::k##Xfermode##_Mode: return new Sk4fXfermode<Xfermode>(rec, mode)
348         CASE(ColorDodge);
349         CASE(ColorBurn);
350         CASE(SoftLight);
351     #undef CASE
352 
353         default: break;
354     }
355     return nullptr;
356 }
357 
358 } // namespace SK_OPTS_NS
359 
360 #endif//Sk4pxXfermode_DEFINED
361