1 2 /* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkXfermode_DEFINED 11 #define SkXfermode_DEFINED 12 13 #include "SkFlattenable.h" 14 #include "SkColor.h" 15 16 class SkString; 17 18 /** \class SkXfermode 19 20 SkXfermode is the base class for objects that are called to implement custom 21 "transfer-modes" in the drawing pipeline. The static function Create(Modes) 22 can be called to return an instance of any of the predefined subclasses as 23 specified in the Modes enum. When an SkXfermode is assigned to an SkPaint, 24 then objects drawn with that paint have the xfermode applied. 25 */ 26 class SK_API SkXfermode : public SkFlattenable { 27 public: SK_DECLARE_INST_COUNT(SkXfermode)28 SK_DECLARE_INST_COUNT(SkXfermode) 29 30 SkXfermode() {} 31 32 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, 33 const SkAlpha aa[]) const; 34 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, 35 const SkAlpha aa[]) const; 36 virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count, 37 const SkAlpha aa[]) const; 38 virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count, 39 const SkAlpha aa[]) const; 40 41 /** Enum of possible coefficients to describe some xfermodes 42 */ 43 enum Coeff { 44 kZero_Coeff, /** 0 */ 45 kOne_Coeff, /** 1 */ 46 kSC_Coeff, /** src color */ 47 kISC_Coeff, /** inverse src color (i.e. 1 - sc) */ 48 kDC_Coeff, /** dst color */ 49 kIDC_Coeff, /** inverse dst color (i.e. 1 - dc) */ 50 kSA_Coeff, /** src alpha */ 51 kISA_Coeff, /** inverse src alpha (i.e. 1 - sa) */ 52 kDA_Coeff, /** dst alpha */ 53 kIDA_Coeff, /** inverse dst alpha (i.e. 1 - da) */ 54 55 kCoeffCount 56 }; 57 58 /** If the xfermode can be expressed as an equation using the coefficients 59 in Coeff, then asCoeff() returns true, and sets (if not null) src and 60 dst accordingly. 61 62 result = src_coeff * src_color + dst_coeff * dst_color; 63 64 As examples, here are some of the porterduff coefficients 65 66 MODE SRC_COEFF DST_COEFF 67 clear zero zero 68 src one zero 69 dst zero one 70 srcover one isa 71 dstover ida one 72 */ 73 virtual bool asCoeff(Coeff* src, Coeff* dst) const; 74 75 /** 76 * The same as calling xfermode->asCoeff(..), except that this also checks 77 * if the xfermode is NULL, and if so, treats its as kSrcOver_Mode. 78 */ 79 static bool AsCoeff(const SkXfermode*, Coeff* src, Coeff* dst); 80 81 /** List of predefined xfermodes. 82 The algebra for the modes uses the following symbols: 83 Sa, Sc - source alpha and color 84 Da, Dc - destination alpha and color (before compositing) 85 [a, c] - Resulting (alpha, color) values 86 For these equations, the colors are in premultiplied state. 87 If no xfermode is specified, kSrcOver is assumed. 88 */ 89 enum Mode { 90 kClear_Mode, //!< [0, 0] 91 kSrc_Mode, //!< [Sa, Sc] 92 kDst_Mode, //!< [Da, Dc] 93 kSrcOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Sc + (1 - Sa)*Dc] 94 kDstOver_Mode, //!< [Sa + Da - Sa*Da, Rc = Dc + (1 - Da)*Sc] 95 kSrcIn_Mode, //!< [Sa * Da, Sc * Da] 96 kDstIn_Mode, //!< [Sa * Da, Sa * Dc] 97 kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)] 98 kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)] 99 kSrcATop_Mode, //!< [Da, Sc * Da + (1 - Sa) * Dc] 100 kDstATop_Mode, //!< [Sa, Sa * Dc + Sc * (1 - Da)] 101 kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + (1 - Sa) * Dc] 102 103 // all remaining modes are defined in the SVG Compositing standard 104 // http://www.w3.org/TR/2009/WD-SVGCompositing-20090430/ 105 kPlus_Mode, 106 kModulate_Mode, // multiplies all components (= alpha and color) 107 108 // all above modes can be expressed as pair of src/dst Coeffs 109 kCoeffModesCnt, 110 111 kScreen_Mode = kCoeffModesCnt, 112 kOverlay_Mode, 113 kDarken_Mode, 114 kLighten_Mode, 115 kColorDodge_Mode, 116 kColorBurn_Mode, 117 kHardLight_Mode, 118 kSoftLight_Mode, 119 kDifference_Mode, 120 kExclusion_Mode, 121 122 kLastMode = kExclusion_Mode 123 }; 124 125 /** 126 * If the xfermode is one of the modes in the Mode enum, then asMode() 127 * returns true and sets (if not null) mode accordingly. Otherwise it 128 * returns false and ignores the mode parameter. 129 */ 130 virtual bool asMode(Mode* mode) const; 131 132 /** 133 * The same as calling xfermode->asMode(mode), except that this also checks 134 * if the xfermode is NULL, and if so, treats its as kSrcOver_Mode. 135 */ 136 static bool AsMode(const SkXfermode*, Mode* mode); 137 138 /** 139 * Returns true if the xfermode claims to be the specified Mode. This works 140 * correctly even if the xfermode is NULL (which equates to kSrcOver.) Thus 141 * you can say this without checking for a null... 142 * 143 * If (SkXfermode::IsMode(paint.getXfermode(), 144 * SkXfermode::kDstOver_Mode)) { 145 * ... 146 * } 147 */ 148 static bool IsMode(const SkXfermode* xfer, Mode mode); 149 150 /** Return an SkXfermode object for the specified mode. 151 */ 152 static SkXfermode* Create(Mode mode); 153 154 /** Return a function pointer to a routine that applies the specified 155 porter-duff transfer mode. 156 */ 157 static SkXfermodeProc GetProc(Mode mode); 158 159 /** Return a function pointer to a routine that applies the specified 160 porter-duff transfer mode and srcColor to a 16bit device color. Note, 161 if the mode+srcColor might return a non-opaque color, then there is not 162 16bit proc, and this will return NULL. 163 */ 164 static SkXfermodeProc16 GetProc16(Mode mode, SkColor srcColor); 165 166 /** 167 * If the specified mode can be represented by a pair of Coeff, then return 168 * true and set (if not NULL) the corresponding coeffs. If the mode is 169 * not representable as a pair of Coeffs, return false and ignore the 170 * src and dst parameters. 171 */ 172 static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst); 173 174 // DEPRECATED: call AsMode(...) IsMode(const SkXfermode * xfer,Mode * mode)175 static bool IsMode(const SkXfermode* xfer, Mode* mode) { 176 return AsMode(xfer, mode); 177 } 178 179 SkDEVCODE(virtual void toString(SkString* str) const = 0;) SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP()180 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 181 protected: 182 SkXfermode(SkFlattenableReadBuffer& rb) : SkFlattenable(rb) {} 183 184 /** The default implementation of xfer32/xfer16/xferA8 in turn call this 185 method, 1 color at a time (upscaled to a SkPMColor). The default 186 implmentation of this method just returns dst. If performance is 187 important, your subclass should override xfer32/xfer16/xferA8 directly. 188 189 This method will not be called directly by the client, so it need not 190 be implemented if your subclass has overridden xfer32/xfer16/xferA8 191 */ 192 virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const; 193 194 private: 195 enum { 196 kModeCount = kLastMode + 1 197 }; 198 typedef SkFlattenable INHERITED; 199 }; 200 201 /////////////////////////////////////////////////////////////////////////////// 202 203 /** \class SkProcXfermode 204 205 SkProcXfermode is a xfermode that applies the specified proc to its colors. 206 This class is not exported to java. 207 */ 208 class SkProcXfermode : public SkXfermode { 209 public: SkProcXfermode(SkXfermodeProc proc)210 SkProcXfermode(SkXfermodeProc proc) : fProc(proc) {} 211 212 // overrides from SkXfermode 213 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, 214 const SkAlpha aa[]) const SK_OVERRIDE; 215 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, 216 const SkAlpha aa[]) const SK_OVERRIDE; 217 virtual void xfer4444(uint16_t dst[], const SkPMColor src[], int count, 218 const SkAlpha aa[]) const SK_OVERRIDE; 219 virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count, 220 const SkAlpha aa[]) const SK_OVERRIDE; 221 222 SK_DEVELOPER_TO_STRING() 223 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkProcXfermode) 224 225 protected: 226 SkProcXfermode(SkFlattenableReadBuffer&); 227 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; 228 229 // allow subclasses to update this after we unflatten setProc(SkXfermodeProc proc)230 void setProc(SkXfermodeProc proc) { 231 fProc = proc; 232 } 233 234 private: 235 SkXfermodeProc fProc; 236 237 typedef SkXfermode INHERITED; 238 }; 239 240 #endif 241