• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
31 class SkXfermode : public SkFlattenable {
32 public:
SkXfermode()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     /** Return an SkXfermode object for the specified mode.
119      */
120     static SkXfermode* Create(Mode mode);
121 
122     /** Return a function pointer to a routine that applies the specified
123         porter-duff transfer mode.
124      */
125     static SkXfermodeProc GetProc(Mode mode);
126 
127     /** Return a function pointer to a routine that applies the specified
128         porter-duff transfer mode and srcColor to a 16bit device color. Note,
129         if the mode+srcColor might return a non-opaque color, then there is not
130         16bit proc, and this will return NULL.
131       */
132     static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
133 
134     /** If the specified xfermode advertises itself as one of the porterduff
135         modes (via SkXfermode::Coeff), return true and if not null, set mode
136         to the corresponding porterduff mode. If it is not recognized as a one,
137         return false and ignore the mode parameter.
138      */
139     static bool IsMode(SkXfermode*, Mode* mode);
140 
141 protected:
SkXfermode(SkFlattenableReadBuffer & rb)142     SkXfermode(SkFlattenableReadBuffer& rb) : SkFlattenable(rb) {}
143 
144     /** The default implementation of xfer32/xfer16/xferA8 in turn call this
145         method, 1 color at a time (upscaled to a SkPMColor). The default
146         implmentation of this method just returns dst. If performance is
147         important, your subclass should override xfer32/xfer16/xferA8 directly.
148 
149         This method will not be called directly by the client, so it need not
150         be implemented if your subclass has overridden xfer32/xfer16/xferA8
151     */
152     virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst);
153 
154 private:
155     enum {
156         kModeCount = kLastMode + 1
157     };
158     typedef SkFlattenable INHERITED;
159 };
160 
161 ///////////////////////////////////////////////////////////////////////////////
162 
163 /** \class SkProcXfermode
164 
165     SkProcXfermode is a xfermode that applies the specified proc to its colors.
166     This class is not exported to java.
167 */
168 class SkProcXfermode : public SkXfermode {
169 public:
SkProcXfermode(SkXfermodeProc proc)170     SkProcXfermode(SkXfermodeProc proc) : fProc(proc) {}
171 
172     // overrides from SkXfermode
173     virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
174                         const SkAlpha aa[]);
175     virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
176                         const SkAlpha aa[]);
177     virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count,
178                           const SkAlpha aa[]);
179     virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
180                         const SkAlpha aa[]);
181 
182     // overrides from SkFlattenable
getFactory()183     virtual Factory getFactory() { return CreateProc; }
184     virtual void    flatten(SkFlattenableWriteBuffer&);
185 
186 protected:
187     SkProcXfermode(SkFlattenableReadBuffer&);
188 
189 private:
190     SkXfermodeProc  fProc;
191 
CreateProc(SkFlattenableReadBuffer & buffer)192     static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer) {
193         return SkNEW_ARGS(SkProcXfermode, (buffer)); }
194 
195     typedef SkXfermode INHERITED;
196 };
197 
198 #endif
199 
200