blob: fc9ae21ac45445b9445285ba697c16686661b418 [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.comd252db02009-04-01 18:31:44 +000060 /** If the xfermode can be expressed as an equation using the coefficients
61 in Coeff, then asCoeff() returns true, and sets (if not null) src and
62 dst accordingly.
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000063
reed@android.comd252db02009-04-01 18:31:44 +000064 result = src_coeff * src_color + dst_coeff * dst_color;
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000065
reed@android.comd252db02009-04-01 18:31:44 +000066 As examples, here are some of the porterduff coefficients
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000067
reed@android.comd252db02009-04-01 18:31:44 +000068 MODE SRC_COEFF DST_COEFF
69 clear zero zero
70 src one zero
71 dst zero one
72 srcover one isa
73 dstover ida one
74 */
reed@google.com30da7452012-12-17 19:55:24 +000075 virtual bool asCoeff(Coeff* src, Coeff* dst) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000076
reed@google.com43c50c82011-04-14 15:50:52 +000077 /**
78 * The same as calling xfermode->asCoeff(..), except that this also checks
bsalomon@google.comf51c0132013-03-27 18:31:15 +000079 * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
reed@google.com43c50c82011-04-14 15:50:52 +000080 */
reed@google.com30da7452012-12-17 19:55:24 +000081 static bool AsCoeff(const SkXfermode*, Coeff* src, Coeff* dst);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +000082
reed@android.coma0f5d152009-06-22 17:38:10 +000083 /** List of predefined xfermodes.
84 The algebra for the modes uses the following symbols:
85 Sa, Sc - source alpha and color
86 Da, Dc - destination alpha and color (before compositing)
87 [a, c] - Resulting (alpha, color) values
88 For these equations, the colors are in premultiplied state.
89 If no xfermode is specified, kSrcOver is assumed.
bsalomon@google.com8da9bc72013-04-19 15:03:21 +000090 The modes are ordered by those that can be expressed as a pair of Coeffs, followed by those
91 that aren't Coeffs but have separable r,g,b computations, and finally
92 those that are not separable.
reed@android.coma0f5d152009-06-22 17:38:10 +000093 */
94 enum Mode {
95 kClear_Mode, //!< [0, 0]
96 kSrc_Mode, //!< [Sa, Sc]
97 kDst_Mode, //!< [Da, Dc]
98 kSrcOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc]
99 kDstOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc]
100 kSrcIn_Mode, //!< [Sa * Da, Sc * Da]
101 kDstIn_Mode, //!< [Sa * Da, Sa * Dc]
102 kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)]
103 kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)]
104 kSrcATop_Mode, //!< [Da, Sc * Da + (1 - Sa) * Dc]
105 kDstATop_Mode, //!< [Sa, Sa * Dc + Sc * (1 - Da)]
106 kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc]
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000107 kPlus_Mode, //!< [Sa + Da, Sc + Dc]
reed@google.com8d3cd7a2013-01-30 21:36:11 +0000108 kModulate_Mode, // multiplies all components (= alpha and color)
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000109
skia.committer@gmail.com64334352013-03-06 07:01:46 +0000110 // Following blend modes are defined in the CSS Compositing standard:
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000111 // https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending
bsalomon@google.comb0091b82013-04-15 15:16:47 +0000112 kScreen_Mode,
bsalomon@google.com8da9bc72013-04-19 15:03:21 +0000113 kLastCoeffMode = kScreen_Mode,
114
115 kOverlay_Mode,
reed@android.coma0f5d152009-06-22 17:38:10 +0000116 kDarken_Mode,
117 kLighten_Mode,
118 kColorDodge_Mode,
119 kColorBurn_Mode,
120 kHardLight_Mode,
121 kSoftLight_Mode,
122 kDifference_Mode,
123 kExclusion_Mode,
reed@google.com25cfa692013-02-04 20:06:00 +0000124 kMultiply_Mode,
bsalomon@google.com8da9bc72013-04-19 15:03:21 +0000125 kLastSeparableMode = kMultiply_Mode,
reed@android.coma0f5d152009-06-22 17:38:10 +0000126
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000127 kHue_Mode,
128 kSaturation_Mode,
129 kColor_Mode,
130 kLuminosity_Mode,
commit-bot@chromium.orgb24f8932013-03-05 16:23:59 +0000131 kLastMode = kLuminosity_Mode
reed@android.coma0f5d152009-06-22 17:38:10 +0000132 };
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000133
commit-bot@chromium.orgd7aaf602013-04-01 12:51:34 +0000134 /**
135 * Gets the name of the Mode as a string.
136 */
137 static const char* ModeName(Mode);
reed@android.coma0f5d152009-06-22 17:38:10 +0000138
reed@google.comc0d4aa22011-04-13 21:12:04 +0000139 /**
140 * If the xfermode is one of the modes in the Mode enum, then asMode()
141 * returns true and sets (if not null) mode accordingly. Otherwise it
142 * returns false and ignores the mode parameter.
vandebo@chromium.org48543272011-02-08 19:28:07 +0000143 */
reed@google.com30da7452012-12-17 19:55:24 +0000144 virtual bool asMode(Mode* mode) const;
vandebo@chromium.org48543272011-02-08 19:28:07 +0000145
reed@google.com43c50c82011-04-14 15:50:52 +0000146 /**
147 * The same as calling xfermode->asMode(mode), except that this also checks
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000148 * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode.
reed@google.com43c50c82011-04-14 15:50:52 +0000149 */
reed@google.com30da7452012-12-17 19:55:24 +0000150 static bool AsMode(const SkXfermode*, Mode* mode);
reed@google.com43c50c82011-04-14 15:50:52 +0000151
mike@reedtribe.orge303fcf2011-11-17 02:16:43 +0000152 /**
153 * Returns true if the xfermode claims to be the specified Mode. This works
154 * correctly even if the xfermode is NULL (which equates to kSrcOver.) Thus
155 * you can say this without checking for a null...
156 *
157 * If (SkXfermode::IsMode(paint.getXfermode(),
158 * SkXfermode::kDstOver_Mode)) {
159 * ...
160 * }
161 */
reed@google.com30da7452012-12-17 19:55:24 +0000162 static bool IsMode(const SkXfermode* xfer, Mode mode);
mike@reedtribe.orge303fcf2011-11-17 02:16:43 +0000163
reed@android.coma0f5d152009-06-22 17:38:10 +0000164 /** Return an SkXfermode object for the specified mode.
165 */
166 static SkXfermode* Create(Mode mode);
167
168 /** Return a function pointer to a routine that applies the specified
169 porter-duff transfer mode.
170 */
171 static SkXfermodeProc GetProc(Mode mode);
172
173 /** Return a function pointer to a routine that applies the specified
174 porter-duff transfer mode and srcColor to a 16bit device color. Note,
175 if the mode+srcColor might return a non-opaque color, then there is not
176 16bit proc, and this will return NULL.
177 */
178 static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor);
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000179
reed@google.comc0d4aa22011-04-13 21:12:04 +0000180 /**
reed@google.com43c50c82011-04-14 15:50:52 +0000181 * If the specified mode can be represented by a pair of Coeff, then return
182 * true and set (if not NULL) the corresponding coeffs. If the mode is
183 * not representable as a pair of Coeffs, return false and ignore the
184 * src and dst parameters.
reed@android.coma0f5d152009-06-22 17:38:10 +0000185 */
reed@google.com43c50c82011-04-14 15:50:52 +0000186 static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst);
reed@android.coma0f5d152009-06-22 17:38:10 +0000187
reed@google.com44699382013-10-31 17:28:30 +0000188 SK_ATTR_DEPRECATED("use AsMode(...)")
reed@google.com30da7452012-12-17 19:55:24 +0000189 static bool IsMode(const SkXfermode* xfer, Mode* mode) {
reed@google.com43c50c82011-04-14 15:50:52 +0000190 return AsMode(xfer, mode);
191 }
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000192
egdaniel38cd0552015-01-14 08:05:11 -0800193 /** Implemented by a subclass to support use as an image filter in the GPU backend. When used as
194 an image filter the xfer mode blends the source color against a background texture rather
195 than the destination. It is implemented as a fragment processor. This can be called with
196 both params set to NULL to query whether it would succeed. Otherwise, both params are
197 required. Upon success the function returns true and the caller owns a ref to the fragment
198 parameter. Upon failure false is returned and the processor param is not written to.
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000199 */
egdaniel38cd0552015-01-14 08:05:11 -0800200 virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture* background) const;
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000201
egdaniel378092f2014-12-03 10:40:13 -0800202 /** A subclass may implement this factory function to work with the GPU backend. It is legal
203 to call this with xpf NULL to simply test the return value. If xpf is non-NULL then the
204 xfermode may optionally allocate a factory to return to the caller as *xpf. The caller
205 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 -0800206 caller should set *xpf to NULL beforehand. XferProcessors cannot use a background texture.
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000207 */
egdaniel378092f2014-12-03 10:40:13 -0800208 virtual bool asXPFactory(GrXPFactory** xpf) const;
209
egdaniel58136162015-01-20 10:19:22 -0800210 /** Returns true if the xfermode can be expressed as an xfer processor factory (xpFactory).
211 This helper calls the asXPFactory() virtual. If the xfermode is NULL, it is treated as
212 kSrcOver_Mode. It is legal to call this with xpf param NULL to simply test the return value.
egdaniel378092f2014-12-03 10:40:13 -0800213 */
egdaniel58136162015-01-20 10:19:22 -0800214 static bool AsXPFactory(SkXfermode*, GrXPFactory**);
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000215
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000216 SK_TO_STRING_PUREVIRT()
djsollen@google.coma2ca41e2012-03-23 19:00:34 +0000217 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()
commit-bot@chromium.orgc0b7e102013-10-23 17:06:21 +0000218 SK_DEFINE_FLATTENABLE_TYPE(SkXfermode)
219
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220protected:
commit-bot@chromium.orgbd0be252014-05-15 15:40:41 +0000221 SkXfermode() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000222 /** The default implementation of xfer32/xfer16/xferA8 in turn call this
223 method, 1 color at a time (upscaled to a SkPMColor). The default
224 implmentation of this method just returns dst. If performance is
225 important, your subclass should override xfer32/xfer16/xferA8 directly.
tomhudson@google.com1447c6f2011-04-27 14:09:52 +0000226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 This method will not be called directly by the client, so it need not
228 be implemented if your subclass has overridden xfer32/xfer16/xferA8
229 */
reed@google.com30da7452012-12-17 19:55:24 +0000230 virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231
232private:
reed@android.coma0f5d152009-06-22 17:38:10 +0000233 enum {
234 kModeCount = kLastMode + 1
235 };
commit-bot@chromium.org86490572013-10-04 16:52:55 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 typedef SkFlattenable INHERITED;
238};
239
reed@android.com8a1c16f2008-12-17 15:59:43 +0000240#endif