SkXfermode.h revision 7ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkXfermode_DEFINED
18#define SkXfermode_DEFINED
19
20#include "SkFlattenable.h"
21#include "SkColor.h"
22
23/** \class SkXfermode
24
25    SkXfermode is the base class for objects that are called to implement custom
26    "transfer-modes" in the drawing pipeline. The static function Create(Modes)
27    can be called to return an instance of any of the predefined subclasses as
28    specified in the Modes enum. When an SkXfermode is assigned to an SkPaint,
29    then objects drawn with that paint have the xfermode applied.
30*/
31class SK_API SkXfermode : public SkFlattenable {
32public:
33    SkXfermode() {}
34
35    virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
36                        const SkAlpha aa[]);
37    virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
38                        const SkAlpha aa[]);
39    virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count,
40                          const SkAlpha aa[]);
41    virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
42                        const SkAlpha aa[]);
43
44    /** Enum of possible coefficients to describe some xfermodes
45     */
46    enum Coeff {
47        kZero_Coeff,    /** 0 */
48        kOne_Coeff,     /** 1 */
49        kSC_Coeff,      /** src color */
50        kISC_Coeff,     /** inverse src color (i.e. 1 - sc) */
51        kDC_Coeff,      /** dst color */
52        kIDC_Coeff,     /** inverse dst color (i.e. 1 - dc) */
53        kSA_Coeff,      /** src alpha */
54        kISA_Coeff,     /** inverse src alpha (i.e. 1 - sa) */
55        kDA_Coeff,      /** dst alpha */
56        kIDA_Coeff,     /** inverse dst alpha (i.e. 1 - da) */
57
58        kCoeffCount
59    };
60
61    /** If the xfermode can be expressed as an equation using the coefficients
62        in Coeff, then asCoeff() returns true, and sets (if not null) src and
63        dst accordingly.
64
65            result = src_coeff * src_color + dst_coeff * dst_color;
66
67        As examples, here are some of the porterduff coefficients
68
69        MODE        SRC_COEFF       DST_COEFF
70        clear       zero            zero
71        src         one             zero
72        dst         zero            one
73        srcover     one             isa
74        dstover     ida             one
75     */
76    virtual bool asCoeff(Coeff* src, Coeff* dst);
77
78    /** List of predefined xfermodes.
79        The algebra for the modes uses the following symbols:
80        Sa, Sc  - source alpha and color
81        Da, Dc - destination alpha and color (before compositing)
82        [a, c] - Resulting (alpha, color) values
83        For these equations, the colors are in premultiplied state.
84        If no xfermode is specified, kSrcOver is assumed.
85     */
86    enum Mode {
87        kClear_Mode,    //!< [0, 0]
88        kSrc_Mode,      //!< [Sa, Sc]
89        kDst_Mode,      //!< [Da, Dc]
90        kSrcOver_Mode,  //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc]
91        kDstOver_Mode,  //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc]
92        kSrcIn_Mode,    //!< [Sa * Da, Sc * Da]
93        kDstIn_Mode,    //!< [Sa * Da, Sa * Dc]
94        kSrcOut_Mode,   //!< [Sa * (1 - Da), Sc * (1 - Da)]
95        kDstOut_Mode,   //!< [Da * (1 - Sa), Dc * (1 - Sa)]
96        kSrcATop_Mode,  //!< [Da, Sc * Da + (1 - Sa) * Dc]
97        kDstATop_Mode,  //!< [Sa, Sa * Dc + Sc * (1 - Da)]
98        kXor_Mode,      //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc]
99
100        // these modes are defined in the SVG Compositing standard
101        // http://www.w3.org/TR/2009/WD-SVGCompositing-20090430/
102        kPlus_Mode,
103        kMultiply_Mode,
104        kScreen_Mode,
105        kOverlay_Mode,
106        kDarken_Mode,
107        kLighten_Mode,
108        kColorDodge_Mode,
109        kColorBurn_Mode,
110        kHardLight_Mode,
111        kSoftLight_Mode,
112        kDifference_Mode,
113        kExclusion_Mode,
114
115        kLastMode = kExclusion_Mode
116    };
117
118    /** If the xfermode is one of the modes in the Mode enum, then asMode()
119        returns true and sets (if not null) mode accordingly.
120        This is a better version of IsMode(), which is only able to report
121        about modes that are expressible as coefficients.
122     */
123    virtual bool asMode(Mode* mode);
124
125    /** Return an SkXfermode object for the specified mode.
126     */
127    static SkXfermode* Create(Mode mode);
128
129    /** Return a function pointer to a routine that applies the specified
130        porter-duff transfer mode.
131     */
132    static SkXfermodeProc GetProc(Mode mode);
133
134    /** Return a function pointer to a routine that applies the specified
135        porter-duff transfer mode and srcColor to a 16bit device color. Note,
136        if the mode+srcColor might return a non-opaque color, then there is not
137        16bit proc, and this will return NULL.
138      */
139    static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
140
141    /** If the specified xfermode advertises itself as one of the porterduff
142        modes (via SkXfermode::Coeff), return true and if not null, set mode
143        to the corresponding porterduff mode. If it is not recognized as a one,
144        return false and ignore the mode parameter.
145     */
146    static bool IsMode(SkXfermode*, Mode* mode);
147
148protected:
149    SkXfermode(SkFlattenableReadBuffer& rb) : SkFlattenable(rb) {}
150
151    /** The default implementation of xfer32/xfer16/xferA8 in turn call this
152        method, 1 color at a time (upscaled to a SkPMColor). The default
153        implmentation of this method just returns dst. If performance is
154        important, your subclass should override xfer32/xfer16/xferA8 directly.
155
156        This method will not be called directly by the client, so it need not
157        be implemented if your subclass has overridden xfer32/xfer16/xferA8
158    */
159    virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst);
160
161private:
162    enum {
163        kModeCount = kLastMode + 1
164    };
165    typedef SkFlattenable INHERITED;
166};
167
168///////////////////////////////////////////////////////////////////////////////
169
170/** \class SkProcXfermode
171
172    SkProcXfermode is a xfermode that applies the specified proc to its colors.
173    This class is not exported to java.
174*/
175class SkProcXfermode : public SkXfermode {
176public:
177    SkProcXfermode(SkXfermodeProc proc) : fProc(proc) {}
178
179    // overrides from SkXfermode
180    virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
181                        const SkAlpha aa[]);
182    virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
183                        const SkAlpha aa[]);
184    virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count,
185                          const SkAlpha aa[]);
186    virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
187                        const SkAlpha aa[]);
188
189    // overrides from SkFlattenable
190    virtual Factory getFactory() { return CreateProc; }
191    virtual void    flatten(SkFlattenableWriteBuffer&);
192    virtual bool asMode(SkXfermode::Mode* mode);
193
194protected:
195    SkProcXfermode(SkFlattenableReadBuffer&);
196
197private:
198    SkXfermodeProc  fProc;
199
200    static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
201        return SkNEW_ARGS(SkProcXfermode, (buffer)); }
202
203    typedef SkXfermode INHERITED;
204};
205
206#endif
207
208