blob: 1dd01a2e1d7f0cf526e3c9f078f3ac7aebdd58de [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkXfermode_DEFINED
11#define SkXfermode_DEFINED
12
13#include "SkFlattenable.h"
14#include "SkColor.h"
15
joshualittb0a8a372014-09-23 09:50:21 -070016class GrFragmentProcessor;
senorblanco@chromium.org86fc2662013-05-31 17:49:12 +000017class GrTexture;
egdaniel378092f2014-12-03 10:40:13 -080018class GrXPFactory;
robertphillips@google.comb83b6b42013-01-22 14:32:09 +000019class SkString;
20
reed@android.com8a1c16f2008-12-17 15:59:43 +000021/** \class SkXfermode
reed@google.comfb6deed2013-10-10 17:35:58 +000022 *
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 *
29 * All subclasses are required to be reentrant-safe : it must be legal to share
30 * the same instance between several threads.
31 */
ctguil@chromium.org7ffb1b22011-03-15 21:27:08 +000032class SK_API SkXfermode : public SkFlattenable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033public:
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000034 SK_DECLARE_INST_COUNT(SkXfermode)
35
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +000037 const SkAlpha aa[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +000039 const SkAlpha aa[]) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
reed@google.com30da7452012-12-17 19:55:24 +000041 const SkAlpha aa[]) const;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000042
reed@android.comd252db02009-04-01 18:31:44 +000043 /** Enum of possible coefficients to describe some xfermodes
44 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 enum Coeff {
reed@android.comd252db02009-04-01 18:31:44 +000046 kZero_Coeff, /** 0 */
47 kOne_Coeff, /** 1 */
48 kSC_Coeff, /** src color */
49 kISC_Coeff, /** inverse src color (i.e. 1 - sc) */
50 kDC_Coeff, /** dst color */
51 kIDC_Coeff, /** inverse dst color (i.e. 1 - dc) */
52 kSA_Coeff, /** src alpha */
53 kISA_Coeff, /** inverse src alpha (i.e. 1 - sa) */
54 kDA_Coeff, /** dst alpha */
55 kIDA_Coeff, /** inverse dst alpha (i.e. 1 - da) */
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 kCoeffCount
58 };
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000059
reed@android.coma0f5d152009-06-22 17:38:10 +000060 /** List of predefined xfermodes.
61 The algebra for the modes uses the following symbols:
62 Sa, Sc - source alpha and color
63 Da, Dc - destination alpha and color (before compositing)
64 [a, c] - Resulting (alpha, color) values
65 For these equations, the colors are in premultiplied state.
66 If no xfermode is specified, kSrcOver is assumed.
bsalomon@google.com8da9bc72013-04-19 15:03:21 +000067 The modes are ordered by those that can be expressed as a pair of Coeffs, followed by those
68 that aren't Coeffs but have separable r,g,b computations, and finally
69 those that are not separable.
reed@android.coma0f5d152009-06-22 17:38:10 +000070 */
71 enum Mode {
72 kClear_Mode, //!< [0, 0]
73 kSrc_Mode, //!< [Sa, Sc]
74 kDst_Mode, //!< [Da, Dc]
75 kSrcOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc]
76 kDstOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc]
77 kSrcIn_Mode, //!< [Sa * Da, Sc * Da]
78 kDstIn_Mode, //!< [Sa * Da, Sa * Dc]
79 kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)]
80 kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)]
81 kSrcATop_Mode, //!< [Da, Sc * Da + (1 - Sa) * Dc]
82 kDstATop_Mode, //!< [Sa, Sa * Dc + Sc * (1 - Da)]
83 kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc]
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +000084 kPlus_Mode, //!< [Sa + Da, Sc + Dc]
reed@google.com8d3cd7a2013-01-30 21:36:11 +000085 kModulate_Mode, // multiplies all components (= alpha and color)
rmistry@google.comfbfcd562012-08-23 18:09:54 +000086
skia.committer@gmail.com64334352013-03-06 07:01:46 +000087 // Following blend modes are defined in the CSS Compositing standard:
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +000088 // https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending
bsalomon@google.comb0091b82013-04-15 15:16:47 +000089 kScreen_Mode,
bsalomon@google.com8da9bc72013-04-19 15:03:21 +000090 kLastCoeffMode = kScreen_Mode,
91
92 kOverlay_Mode,
reed@android.coma0f5d152009-06-22 17:38:10 +000093 kDarken_Mode,
94 kLighten_Mode,
95 kColorDodge_Mode,
96 kColorBurn_Mode,
97 kHardLight_Mode,
98 kSoftLight_Mode,
99 kDifference_Mode,
100 kExclusion_Mode,
reed@google.com25cfa692013-02-04 20:06:00 +0000101 kMultiply_Mode,
bsalomon@google.com8da9bc72013-04-19 15:03:21 +0000102 kLastSeparableMode = kMultiply_Mode,
reed@android.coma0f5d152009-06-22 17:38:10 +0000103
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000104 kHue_Mode,
105 kSaturation_Mode,
106 kColor_Mode,
107 kLuminosity_Mode,
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000108 kLastMode = kLuminosity_Mode
reed@android.coma0f5d152009-06-22 17:38:10 +0000109 };
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000110
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +0000111 /**
112 * Gets the name of the Mode as a string.
113 */
114 static const char* ModeName(Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000115
reed@google.comc0d4aa22011-04-13 21:12:04 +0000116 /**
117 * If the xfermode is one of the modes in the Mode enum, then asMode()
118 * returns true and sets (if not null) mode accordingly. Otherwise it
119 * returns false and ignores the mode parameter.
vandebo@chromium.org48543272011-02-08 19:28:07 +0000120 */
reed@google.com30da7452012-12-17 19:55:24 +0000121 virtual bool asMode(Mode* mode) const;
vandebo@chromium.org48543272011-02-08 19:28:07 +0000122
reed@google.com43c50c82011-04-14 15:50:52 +0000123 /**
124 * The same as calling xfermode->asMode(mode), except that this also checks
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000125 * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
reed@google.com43c50c82011-04-14 15:50:52 +0000126 */
reed@google.com30da7452012-12-17 19:55:24 +0000127 static bool AsMode(const SkXfermode*, Mode* mode);
reed@google.com43c50c82011-04-14 15:50:52 +0000128
mike@reedtribe.orge303fcf2011-11-17 02:16:43 +0000129 /**
130 * Returns true if the xfermode claims to be the specified Mode. This works
131 * correctly even if the xfermode is NULL (which equates to kSrcOver.) Thus
132 * you can say this without checking for a null...
133 *
134 * If (SkXfermode::IsMode(paint.getXfermode(),
135 * SkXfermode::kDstOver_Mode)) {
136 * ...
137 * }
138 */
reed@google.com30da7452012-12-17 19:55:24 +0000139 static bool IsMode(const SkXfermode* xfer, Mode mode);
mike@reedtribe.orge303fcf2011-11-17 02:16:43 +0000140
reed@android.coma0f5d152009-06-22 17:38:10 +0000141 /** Return an SkXfermode object for the specified mode.
142 */
143 static SkXfermode* Create(Mode mode);
144
145 /** Return a function pointer to a routine that applies the specified
146 porter-duff transfer mode.
147 */
148 static SkXfermodeProc GetProc(Mode mode);
149
150 /** Return a function pointer to a routine that applies the specified
151 porter-duff transfer mode and srcColor to a 16bit device color. Note,
152 if the mode+srcColor might return a non-opaque color, then there is not
153 16bit proc, and this will return NULL.
154 */
155 static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000156
reed@google.comc0d4aa22011-04-13 21:12:04 +0000157 /**
reed@google.com43c50c82011-04-14 15:50:52 +0000158 * If the specified mode can be represented by a pair of Coeff, then return
159 * true and set (if not NULL) the corresponding coeffs. If the mode is
160 * not representable as a pair of Coeffs, return false and ignore the
161 * src and dst parameters.
reed@android.coma0f5d152009-06-22 17:38:10 +0000162 */
reed@google.com43c50c82011-04-14 15:50:52 +0000163 static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst);
reed@android.coma0f5d152009-06-22 17:38:10 +0000164
reed@google.com44699382013-10-31 17:28:30 +0000165 SK_ATTR_DEPRECATED("use AsMode(...)")
reed@google.com30da7452012-12-17 19:55:24 +0000166 static bool IsMode(const SkXfermode* xfer, Mode* mode) {
reed@google.com43c50c82011-04-14 15:50:52 +0000167 return AsMode(xfer, mode);
168 }
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000169
egdanieldcfb7cf2015-01-22 06:52:29 -0800170 /**
171 * Returns whether or not the xfer mode can support treating coverage as alpha
172 */
173 virtual bool supportsCoverageAsAlpha() const;
174
175 /**
176 * The same as calling xfermode->supportsCoverageAsAlpha(), except that this also checks if
177 * the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
178 */
179 static bool SupportsCoverageAsAlpha(const SkXfermode* xfer);
180
181 enum SrcColorOpacity {
182 // The src color is known to be opaque (alpha == 255)
183 kOpaque_SrcColorOpacity = 0,
184 // The src color is known to be fully transparent (color == 0)
185 kTransparentBlack_SrcColorOpacity = 1,
186 // The src alpha is known to be fully transparent (alpha == 0)
187 kTransparentAlpha_SrcColorOpacity = 2,
188 // The src color opacity is unknown
189 kUnknown_SrcColorOpacity = 3
190 };
191
192 /**
193 * Returns whether or not the result of the draw with the xfer mode will be opaque or not. The
194 * input to this call is an enum describing known information about the opacity of the src color
195 * that will be given to the xfer mode.
196 */
197 virtual bool isOpaque(SrcColorOpacity opacityType) const;
198
199 /**
200 * The same as calling xfermode->isOpaque(...), except that this also checks if
201 * the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
202 */
203 static bool IsOpaque(const SkXfermode* xfer, SrcColorOpacity opacityType);
204
egdaniel38cd0552015-01-14 08:05:11 -0800205 /** Implemented by a subclass to support use as an image filter in the GPU backend. When used as
206 an image filter the xfer mode blends the source color against a background texture rather
207 than the destination. It is implemented as a fragment processor. This can be called with
208 both params set to NULL to query whether it would succeed. Otherwise, both params are
209 required. Upon success the function returns true and the caller owns a ref to the fragment
210 parameter. Upon failure false is returned and the processor param is not written to.
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000211 */
egdaniel38cd0552015-01-14 08:05:11 -0800212 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture* background) const;
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000213
egdaniel378092f2014-12-03 10:40:13 -0800214 /** A subclass may implement this factory function to work with the GPU backend. It is legal
215 to call this with xpf NULL to simply test the return value. If xpf is non-NULL then the
216 xfermode may optionally allocate a factory to return to the caller as *xpf. The caller
217 will install it and own a ref to it. Since the xfermode may or may not assign *xpf, the
egdaniel38cd0552015-01-14 08:05:11 -0800218 caller should set *xpf to NULL beforehand. XferProcessors cannot use a background texture.
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000219 */
egdaniel378092f2014-12-03 10:40:13 -0800220 virtual bool asXPFactory(GrXPFactory** xpf) const;
221
egdaniel58136162015-01-20 10:19:22 -0800222 /** Returns true if the xfermode can be expressed as an xfer processor factory (xpFactory).
223 This helper calls the asXPFactory() virtual. If the xfermode is NULL, it is treated as
224 kSrcOver_Mode. It is legal to call this with xpf param NULL to simply test the return value.
egdaniel378092f2014-12-03 10:40:13 -0800225 */
egdaniel58136162015-01-20 10:19:22 -0800226 static bool AsXPFactory(SkXfermode*, GrXPFactory**);
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000227
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000228 SK_TO_STRING_PUREVIRT()
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000229 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000230 SK_DEFINE_FLATTENABLE_TYPE(SkXfermode)
231
reed@android.com8a1c16f2008-12-17 15:59:43 +0000232protected:
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +0000233 SkXfermode() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000234 /** The default implementation of xfer32/xfer16/xferA8 in turn call this
235 method, 1 color at a time (upscaled to a SkPMColor). The default
236 implmentation of this method just returns dst. If performance is
237 important, your subclass should override xfer32/xfer16/xferA8 directly.
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 This method will not be called directly by the client, so it need not
240 be implemented if your subclass has overridden xfer32/xfer16/xferA8
241 */
reed@google.com30da7452012-12-17 19:55:24 +0000242 virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243
244private:
reed@android.coma0f5d152009-06-22 17:38:10 +0000245 enum {
246 kModeCount = kLastMode + 1
247 };
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000248
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249 typedef SkFlattenable INHERITED;
250};
251
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252#endif