epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkXfermode_DEFINED |
| 11 | #define SkXfermode_DEFINED |
| 12 | |
| 13 | #include "SkFlattenable.h" |
| 14 | #include "SkColor.h" |
| 15 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 16 | class GrFragmentProcessor; |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 17 | class GrTexture; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 18 | class GrXPFactory; |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 19 | class SkString; |
| 20 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | /** \class SkXfermode |
reed@google.com | fb6deed | 2013-10-10 17:35:58 +0000 | [diff] [blame] | 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 | * |
| 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.org | 7ffb1b2 | 2011-03-15 21:27:08 +0000 | [diff] [blame] | 32 | class SK_API SkXfermode : public SkFlattenable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | public: |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 34 | SK_DECLARE_INST_COUNT(SkXfermode) |
| 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 37 | const SkAlpha aa[]) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 39 | const SkAlpha aa[]) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count, |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 41 | const SkAlpha aa[]) const; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 42 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 43 | /** Enum of possible coefficients to describe some xfermodes |
| 44 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | enum Coeff { |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 46 | 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.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 56 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | kCoeffCount |
| 58 | }; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 59 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 60 | /** 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.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 63 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 64 | result = src_coeff * src_color + dst_coeff * dst_color; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 65 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 66 | As examples, here are some of the porterduff coefficients |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 67 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 68 | 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.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 75 | virtual bool asCoeff(Coeff* src, Coeff* dst) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 77 | /** |
| 78 | * The same as calling xfermode->asCoeff(..), except that this also checks |
bsalomon@google.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 79 | * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode. |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 80 | */ |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 81 | static bool AsCoeff(const SkXfermode*, Coeff* src, Coeff* dst); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 82 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 83 | /** 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.com | 8da9bc7 | 2013-04-19 15:03:21 +0000 | [diff] [blame] | 90 | 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.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 93 | */ |
| 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.org | b24f893 | 2013-03-05 16:23:59 +0000 | [diff] [blame] | 107 | kPlus_Mode, //!< [Sa + Da, Sc + Dc] |
reed@google.com | 8d3cd7a | 2013-01-30 21:36:11 +0000 | [diff] [blame] | 108 | kModulate_Mode, // multiplies all components (= alpha and color) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 109 | |
skia.committer@gmail.com | 6433435 | 2013-03-06 07:01:46 +0000 | [diff] [blame] | 110 | // Following blend modes are defined in the CSS Compositing standard: |
commit-bot@chromium.org | b24f893 | 2013-03-05 16:23:59 +0000 | [diff] [blame] | 111 | // https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending |
bsalomon@google.com | b0091b8 | 2013-04-15 15:16:47 +0000 | [diff] [blame] | 112 | kScreen_Mode, |
bsalomon@google.com | 8da9bc7 | 2013-04-19 15:03:21 +0000 | [diff] [blame] | 113 | kLastCoeffMode = kScreen_Mode, |
| 114 | |
| 115 | kOverlay_Mode, |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 116 | 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.com | 25cfa69 | 2013-02-04 20:06:00 +0000 | [diff] [blame] | 124 | kMultiply_Mode, |
bsalomon@google.com | 8da9bc7 | 2013-04-19 15:03:21 +0000 | [diff] [blame] | 125 | kLastSeparableMode = kMultiply_Mode, |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 126 | |
commit-bot@chromium.org | b24f893 | 2013-03-05 16:23:59 +0000 | [diff] [blame] | 127 | kHue_Mode, |
| 128 | kSaturation_Mode, |
| 129 | kColor_Mode, |
| 130 | kLuminosity_Mode, |
commit-bot@chromium.org | b24f893 | 2013-03-05 16:23:59 +0000 | [diff] [blame] | 131 | kLastMode = kLuminosity_Mode |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 132 | }; |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 133 | |
commit-bot@chromium.org | d7aaf60 | 2013-04-01 12:51:34 +0000 | [diff] [blame] | 134 | /** |
| 135 | * Gets the name of the Mode as a string. |
| 136 | */ |
| 137 | static const char* ModeName(Mode); |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 138 | |
reed@google.com | c0d4aa2 | 2011-04-13 21:12:04 +0000 | [diff] [blame] | 139 | /** |
| 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.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 143 | */ |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 144 | virtual bool asMode(Mode* mode) const; |
vandebo@chromium.org | 4854327 | 2011-02-08 19:28:07 +0000 | [diff] [blame] | 145 | |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 146 | /** |
| 147 | * The same as calling xfermode->asMode(mode), except that this also checks |
bsalomon@google.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 148 | * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode. |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 149 | */ |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 150 | static bool AsMode(const SkXfermode*, Mode* mode); |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 151 | |
mike@reedtribe.org | e303fcf | 2011-11-17 02:16:43 +0000 | [diff] [blame] | 152 | /** |
| 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.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 162 | static bool IsMode(const SkXfermode* xfer, Mode mode); |
mike@reedtribe.org | e303fcf | 2011-11-17 02:16:43 +0000 | [diff] [blame] | 163 | |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 164 | /** 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.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 179 | |
reed@google.com | c0d4aa2 | 2011-04-13 21:12:04 +0000 | [diff] [blame] | 180 | /** |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 181 | * 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.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 185 | */ |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 186 | static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst); |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 187 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 188 | SK_ATTR_DEPRECATED("use AsMode(...)") |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 189 | static bool IsMode(const SkXfermode* xfer, Mode* mode) { |
reed@google.com | 43c50c8 | 2011-04-14 15:50:52 +0000 | [diff] [blame] | 190 | return AsMode(xfer, mode); |
| 191 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 192 | |
egdaniel | 38cd055 | 2015-01-14 08:05:11 -0800 | [diff] [blame] | 193 | /** 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.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 199 | */ |
egdaniel | 38cd055 | 2015-01-14 08:05:11 -0800 | [diff] [blame] | 200 | virtual bool asFragmentProcessor(GrFragmentProcessor**, GrTexture* background) const; |
bsalomon@google.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 201 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 202 | /** 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 |
egdaniel | 38cd055 | 2015-01-14 08:05:11 -0800 | [diff] [blame] | 206 | caller should set *xpf to NULL beforehand. XferProcessors cannot use a background texture. |
bsalomon@google.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 207 | */ |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 208 | virtual bool asXPFactory(GrXPFactory** xpf) const; |
| 209 | |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame^] | 210 | /** 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. |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 213 | */ |
egdaniel | 5813616 | 2015-01-20 10:19:22 -0800 | [diff] [blame^] | 214 | static bool AsXPFactory(SkXfermode*, GrXPFactory**); |
bsalomon@google.com | f51c013 | 2013-03-27 18:31:15 +0000 | [diff] [blame] | 215 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 216 | SK_TO_STRING_PUREVIRT() |
djsollen@google.com | a2ca41e | 2012-03-23 19:00:34 +0000 | [diff] [blame] | 217 | SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 218 | SK_DEFINE_FLATTENABLE_TYPE(SkXfermode) |
| 219 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | protected: |
commit-bot@chromium.org | bd0be25 | 2014-05-15 15:40:41 +0000 | [diff] [blame] | 221 | SkXfermode() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 222 | /** 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.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 226 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | 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.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 230 | virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | |
| 232 | private: |
reed@android.com | a0f5d15 | 2009-06-22 17:38:10 +0000 | [diff] [blame] | 233 | enum { |
| 234 | kModeCount = kLastMode + 1 |
| 235 | }; |
commit-bot@chromium.org | 8649057 | 2013-10-04 16:52:55 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | typedef SkFlattenable INHERITED; |
| 238 | }; |
| 239 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 240 | #endif |