SkXfermode.h revision 86490573b5cba554a27637e22485455a7b133de7
1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#ifndef SkXfermode_DEFINED
11#define SkXfermode_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkColor.h"
15
16class GrContext;
17class GrEffectRef;
18class GrTexture;
19class SkString;
20
21/** \class SkXfermode
22
23    SkXfermode is the base class for objects that are called to implement custom
24    "transfer-modes" in the drawing pipeline. The static function Create(Modes)
25    can be called to return an instance of any of the predefined subclasses as
26    specified in the Modes enum. When an SkXfermode is assigned to an SkPaint,
27    then objects drawn with that paint have the xfermode applied.
28*/
29class SK_API SkXfermode : public SkFlattenable {
30public:
31    SK_DECLARE_INST_COUNT(SkXfermode)
32
33    SkXfermode() {}
34
35    virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
36                        const SkAlpha aa[]) const;
37    virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
38                        const SkAlpha aa[]) const;
39    virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
40                        const SkAlpha aa[]) const;
41
42    /** Enum of possible coefficients to describe some xfermodes
43     */
44    enum Coeff {
45        kZero_Coeff,    /** 0 */
46        kOne_Coeff,     /** 1 */
47        kSC_Coeff,      /** src color */
48        kISC_Coeff,     /** inverse src color (i.e. 1 - sc) */
49        kDC_Coeff,      /** dst color */
50        kIDC_Coeff,     /** inverse dst color (i.e. 1 - dc) */
51        kSA_Coeff,      /** src alpha */
52        kISA_Coeff,     /** inverse src alpha (i.e. 1 - sa) */
53        kDA_Coeff,      /** dst alpha */
54        kIDA_Coeff,     /** inverse dst alpha (i.e. 1 - da) */
55
56        kCoeffCount
57    };
58
59    /** If the xfermode can be expressed as an equation using the coefficients
60        in Coeff, then asCoeff() returns true, and sets (if not null) src and
61        dst accordingly.
62
63            result = src_coeff * src_color + dst_coeff * dst_color;
64
65        As examples, here are some of the porterduff coefficients
66
67        MODE        SRC_COEFF       DST_COEFF
68        clear       zero            zero
69        src         one             zero
70        dst         zero            one
71        srcover     one             isa
72        dstover     ida             one
73     */
74    virtual bool asCoeff(Coeff* src, Coeff* dst) const;
75
76    /**
77     *  The same as calling xfermode->asCoeff(..), except that this also checks
78     *  if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
79     */
80    static bool AsCoeff(const SkXfermode*, Coeff* src, Coeff* dst);
81
82    /** List of predefined xfermodes.
83        The algebra for the modes uses the following symbols:
84        Sa, Sc  - source alpha and color
85        Da, Dc - destination alpha and color (before compositing)
86        [a, c] - Resulting (alpha, color) values
87        For these equations, the colors are in premultiplied state.
88        If no xfermode is specified, kSrcOver is assumed.
89        The modes are ordered by those that can be expressed as a pair of Coeffs, followed by those
90        that aren't Coeffs but have separable r,g,b computations, and finally
91        those that are not separable.
92     */
93    enum Mode {
94        kClear_Mode,    //!< [0, 0]
95        kSrc_Mode,      //!< [Sa, Sc]
96        kDst_Mode,      //!< [Da, Dc]
97        kSrcOver_Mode,  //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc]
98        kDstOver_Mode,  //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc]
99        kSrcIn_Mode,    //!< [Sa * Da, Sc * Da]
100        kDstIn_Mode,    //!< [Sa * Da, Sa * Dc]
101        kSrcOut_Mode,   //!< [Sa * (1 - Da), Sc * (1 - Da)]
102        kDstOut_Mode,   //!< [Da * (1 - Sa), Dc * (1 - Sa)]
103        kSrcATop_Mode,  //!< [Da, Sc * Da + (1 - Sa) * Dc]
104        kDstATop_Mode,  //!< [Sa, Sa * Dc + Sc * (1 - Da)]
105        kXor_Mode,      //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc]
106        kPlus_Mode,     //!< [Sa + Da, Sc + Dc]
107        kModulate_Mode, // multiplies all components (= alpha and color)
108
109        // Following blend modes are defined in the CSS Compositing standard:
110        // https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending
111        kScreen_Mode,
112        kLastCoeffMode = kScreen_Mode,
113
114        kOverlay_Mode,
115        kDarken_Mode,
116        kLighten_Mode,
117        kColorDodge_Mode,
118        kColorBurn_Mode,
119        kHardLight_Mode,
120        kSoftLight_Mode,
121        kDifference_Mode,
122        kExclusion_Mode,
123        kMultiply_Mode,
124        kLastSeparableMode = kMultiply_Mode,
125
126        kHue_Mode,
127        kSaturation_Mode,
128        kColor_Mode,
129        kLuminosity_Mode,
130        kLastMode = kLuminosity_Mode
131    };
132
133    /**
134     * Gets the name of the Mode as a string.
135     */
136    static const char* ModeName(Mode);
137
138    /**
139     *  If the xfermode is one of the modes in the Mode enum, then asMode()
140     *  returns true and sets (if not null) mode accordingly. Otherwise it
141     *  returns false and ignores the mode parameter.
142     */
143    virtual bool asMode(Mode* mode) const;
144
145    /**
146     *  The same as calling xfermode->asMode(mode), except that this also checks
147     *  if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
148     */
149    static bool AsMode(const SkXfermode*, Mode* mode);
150
151    /**
152     *  Returns true if the xfermode claims to be the specified Mode. This works
153     *  correctly even if the xfermode is NULL (which equates to kSrcOver.) Thus
154     *  you can say this without checking for a null...
155     *
156     *  If (SkXfermode::IsMode(paint.getXfermode(),
157     *                         SkXfermode::kDstOver_Mode)) {
158     *      ...
159     *  }
160     */
161    static bool IsMode(const SkXfermode* xfer, Mode mode);
162
163    /** Return an SkXfermode object for the specified mode.
164     */
165    static SkXfermode* Create(Mode mode);
166
167    /** Return a function pointer to a routine that applies the specified
168        porter-duff transfer mode.
169     */
170    static SkXfermodeProc GetProc(Mode mode);
171
172    /** Return a function pointer to a routine that applies the specified
173        porter-duff transfer mode and srcColor to a 16bit device color. Note,
174        if the mode+srcColor might return a non-opaque color, then there is not
175        16bit proc, and this will return NULL.
176      */
177    static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
178
179    /**
180     *  If the specified mode can be represented by a pair of Coeff, then return
181     *  true and set (if not NULL) the corresponding coeffs. If the mode is
182     *  not representable as a pair of Coeffs, return false and ignore the
183     *  src and dst parameters.
184     */
185    static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst);
186
187    // DEPRECATED: call AsMode(...)
188    static bool IsMode(const SkXfermode* xfer, Mode* mode) {
189        return AsMode(xfer, mode);
190    }
191
192    /** A subclass may implement this factory function to work with the GPU backend. It is legal
193        to call this with all but the context param NULL to simply test the return value. effect,
194        src, and dst must all be NULL or all non-NULL. If effect is non-NULL then the xfermode may
195        optionally allocate an effect to return and the caller as *effect. The caller will install
196        it and own a ref to it. Since the xfermode may or may not assign *effect, the caller should
197        set *effect to NULL beforehand. If the function returns true and *effect is NULL then the
198        src and dst coeffs will be applied to the draw. When *effect is non-NULL the coeffs are
199        ignored. background specifies the texture to use as the background for compositing, and
200        should be accessed in the effect's fragment shader. If NULL, the effect should request
201        access to destination color (setWillReadDstColor()), and use that in the fragment shader
202        (builder->dstColor()).
203     */
204    virtual bool asNewEffectOrCoeff(GrContext*,
205                                    GrEffectRef** effect,
206                                    Coeff* src,
207                                    Coeff* dst,
208                                    GrTexture* background = NULL) const;
209
210    /**
211     *  The same as calling xfermode->asNewEffect(...), except that this also checks if the xfermode
212     *  is NULL, and if so, treats it as kSrcOver_Mode.
213     */
214    static bool AsNewEffectOrCoeff(SkXfermode*,
215                                   GrContext*,
216                                   GrEffectRef** effect,
217                                   Coeff* src,
218                                   Coeff* dst,
219                                   GrTexture* background = NULL);
220
221    SkDEVCODE(virtual void toString(SkString* str) const = 0;)
222    SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
223protected:
224    SkXfermode(SkFlattenableReadBuffer& rb) : SkFlattenable(rb) {}
225
226    /** The default implementation of xfer32/xfer16/xferA8 in turn call this
227        method, 1 color at a time (upscaled to a SkPMColor). The default
228        implmentation of this method just returns dst. If performance is
229        important, your subclass should override xfer32/xfer16/xferA8 directly.
230
231        This method will not be called directly by the client, so it need not
232        be implemented if your subclass has overridden xfer32/xfer16/xferA8
233    */
234    virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const;
235
236private:
237    enum {
238        kModeCount = kLastMode + 1
239    };
240
241    friend class SkGraphics;
242    static void Term();
243
244    typedef SkFlattenable INHERITED;
245};
246
247///////////////////////////////////////////////////////////////////////////////
248
249/** \class SkProcXfermode
250
251    SkProcXfermode is a xfermode that applies the specified proc to its colors.
252    This class is not exported to java.
253*/
254class SkProcXfermode : public SkXfermode {
255public:
256    SkProcXfermode(SkXfermodeProc proc) : fProc(proc) {}
257
258    // overrides from SkXfermode
259    virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
260                        const SkAlpha aa[]) const SK_OVERRIDE;
261    virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
262                        const SkAlpha aa[]) const SK_OVERRIDE;
263    virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
264                        const SkAlpha aa[]) const SK_OVERRIDE;
265
266    SK_DEVELOPER_TO_STRING()
267    SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkProcXfermode)
268
269protected:
270    SkProcXfermode(SkFlattenableReadBuffer&);
271    virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE;
272
273    // allow subclasses to update this after we unflatten
274    void setProc(SkXfermodeProc proc) {
275        fProc = proc;
276    }
277
278private:
279    SkXfermodeProc  fProc;
280
281    typedef SkXfermode INHERITED;
282};
283
284#endif
285