1 /* 2 * Copyright 2006 The Android Open Source Project 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkXfermode_DEFINED 9 #define SkXfermode_DEFINED 10 11 #include "SkBlendMode.h" 12 #include "SkColor.h" 13 #include "SkFlattenable.h" 14 15 class GrFragmentProcessor; 16 class GrTexture; 17 class GrXPFactory; 18 class SkRasterPipeline; 19 class SkString; 20 21 struct SkArithmeticParams; 22 23 struct SkPM4f; 24 typedef SkPM4f (*SkXfermodeProc4f)(const SkPM4f& src, const SkPM4f& dst); 25 26 /** \class SkXfermode 27 * 28 * SkXfermode is the base class for objects that are called to implement custom 29 * "transfer-modes" in the drawing pipeline. The static function Create(Modes) 30 * can be called to return an instance of any of the predefined subclasses as 31 * specified in the Modes enum. When an SkXfermode is assigned to an SkPaint, 32 * then objects drawn with that paint have the xfermode applied. 33 * 34 * All subclasses are required to be reentrant-safe : it must be legal to share 35 * the same instance between several threads. 36 */ 37 class SK_API SkXfermode : public SkFlattenable { 38 public: 39 virtual void xfer32(SkPMColor dst[], const SkPMColor src[], int count, 40 const SkAlpha aa[]) const; 41 virtual void xfer16(uint16_t dst[], const SkPMColor src[], int count, 42 const SkAlpha aa[]) const; 43 virtual void xferA8(SkAlpha dst[], const SkPMColor src[], int count, 44 const SkAlpha aa[]) const; 45 46 /** Enum of possible coefficients to describe some xfermodes 47 */ 48 enum Coeff { 49 kZero_Coeff, /** 0 */ 50 kOne_Coeff, /** 1 */ 51 kSC_Coeff, /** src color */ 52 kISC_Coeff, /** inverse src color (i.e. 1 - sc) */ 53 kDC_Coeff, /** dst color */ 54 kIDC_Coeff, /** inverse dst color (i.e. 1 - dc) */ 55 kSA_Coeff, /** src alpha */ 56 kISA_Coeff, /** inverse src alpha (i.e. 1 - sa) */ 57 kDA_Coeff, /** dst alpha */ 58 kIDA_Coeff, /** inverse dst alpha (i.e. 1 - da) */ 59 60 kCoeffCount 61 }; 62 63 /** List of predefined xfermodes. 64 The algebra for the modes uses the following symbols: 65 Sa, Sc - source alpha and color 66 Da, Dc - destination alpha and color (before compositing) 67 [a, c] - Resulting (alpha, color) values 68 For these equations, the colors are in premultiplied state. 69 If no xfermode is specified, kSrcOver is assumed. 70 The modes are ordered by those that can be expressed as a pair of Coeffs, followed by those 71 that aren't Coeffs but have separable r,g,b computations, and finally 72 those that are not separable. 73 */ 74 enum Mode { 75 kClear_Mode, //!< [0, 0] 76 kSrc_Mode, //!< [Sa, Sc] 77 kDst_Mode, //!< [Da, Dc] 78 kSrcOver_Mode, //!< [Sa + Da * (1 - Sa), Sc + Dc * (1 - Sa)] 79 kDstOver_Mode, //!< [Da + Sa * (1 - Da), Dc + Sc * (1 - Da)] 80 kSrcIn_Mode, //!< [Sa * Da, Sc * Da] 81 kDstIn_Mode, //!< [Da * Sa, Dc * Sa] 82 kSrcOut_Mode, //!< [Sa * (1 - Da), Sc * (1 - Da)] 83 kDstOut_Mode, //!< [Da * (1 - Sa), Dc * (1 - Sa)] 84 kSrcATop_Mode, //!< [Da, Sc * Da + Dc * (1 - Sa)] 85 kDstATop_Mode, //!< [Sa, Dc * Sa + Sc * (1 - Da)] 86 kXor_Mode, //!< [Sa + Da - 2 * Sa * Da, Sc * (1 - Da) + Dc * (1 - Sa)] 87 kPlus_Mode, //!< [Sa + Da, Sc + Dc] 88 kModulate_Mode, // multiplies all components (= alpha and color) 89 90 // Following blend modes are defined in the CSS Compositing standard: 91 // https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blending 92 kScreen_Mode, 93 kLastCoeffMode = kScreen_Mode, 94 95 kOverlay_Mode, 96 kDarken_Mode, 97 kLighten_Mode, 98 kColorDodge_Mode, 99 kColorBurn_Mode, 100 kHardLight_Mode, 101 kSoftLight_Mode, 102 kDifference_Mode, 103 kExclusion_Mode, 104 kMultiply_Mode, 105 kLastSeparableMode = kMultiply_Mode, 106 107 kHue_Mode, 108 kSaturation_Mode, 109 kColor_Mode, 110 kLuminosity_Mode, 111 kLastMode = kLuminosity_Mode 112 }; 113 114 /** 115 * Gets the name of the Mode as a string. 116 */ 117 static const char* ModeName(Mode); ModeName(SkBlendMode mode)118 static const char* ModeName(SkBlendMode mode) { 119 return ModeName(Mode(mode)); 120 } 121 122 /** 123 * If the xfermode is one of the modes in the Mode enum, then asMode() 124 * returns true and sets (if not null) mode accordingly. Otherwise it 125 * returns false and ignores the mode parameter. 126 */ 127 virtual bool asMode(Mode* mode) const; 128 129 /** 130 * The same as calling xfermode->asMode(mode), except that this also checks 131 * if the xfermode is NULL, and if so, treats it as kSrcOver_Mode. 132 */ 133 static bool AsMode(const SkXfermode*, Mode* mode); AsMode(const sk_sp<SkXfermode> & xfer,Mode * mode)134 static bool AsMode(const sk_sp<SkXfermode>& xfer, Mode* mode) { 135 return AsMode(xfer.get(), mode); 136 } 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); IsMode(const sk_sp<SkXfermode> & xfer,Mode mode)149 static bool IsMode(const sk_sp<SkXfermode>& xfer, Mode mode) { 150 return IsMode(xfer.get(), mode); 151 } 152 153 /** Return an SkXfermode object for the specified mode. 154 */ 155 static sk_sp<SkXfermode> Make(Mode); 156 #ifdef SK_SUPPORT_LEGACY_XFERMODE_PTR Create(Mode mode)157 static SkXfermode* Create(Mode mode) { 158 return Make(mode).release(); 159 } 160 SK_ATTR_DEPRECATED("use AsMode(...)") IsMode(const SkXfermode * xfer,Mode * mode)161 static bool IsMode(const SkXfermode* xfer, Mode* mode) { 162 return AsMode(xfer, mode); 163 } 164 #endif 165 166 /** 167 * Skia maintains global xfermode objects corresponding to each BlendMode. This returns a 168 * ptr to that global xfermode (or null if the mode is srcover). Thus the caller may use 169 * the returned ptr, but it should leave its refcnt untouched. 170 */ Peek(SkBlendMode mode)171 static SkXfermode* Peek(SkBlendMode mode) { 172 sk_sp<SkXfermode> xfer = Make(mode); 173 if (!xfer) { 174 SkASSERT(SkBlendMode::kSrcOver == mode); 175 return nullptr; 176 } 177 SkASSERT(!xfer->unique()); 178 return xfer.get(); 179 } 180 Make(SkBlendMode bm)181 static sk_sp<SkXfermode> Make(SkBlendMode bm) { 182 return Make((Mode)bm); 183 } 184 blend()185 SkBlendMode blend() const { 186 Mode mode; 187 SkAssertResult(this->asMode(&mode)); 188 return (SkBlendMode)mode; 189 } 190 191 /** Return a function pointer to a routine that applies the specified 192 porter-duff transfer mode. 193 */ 194 static SkXfermodeProc GetProc(Mode mode); 195 static SkXfermodeProc4f GetProc4f(Mode); 196 197 virtual SkXfermodeProc4f getProc4f() const; 198 199 bool appendStages(SkRasterPipeline*) const; 200 201 /** 202 * If the specified mode can be represented by a pair of Coeff, then return 203 * true and set (if not NULL) the corresponding coeffs. If the mode is 204 * not representable as a pair of Coeffs, return false and ignore the 205 * src and dst parameters. 206 */ 207 static bool ModeAsCoeff(Mode mode, Coeff* src, Coeff* dst); 208 209 /** 210 * Returns whether or not the xfer mode can support treating coverage as alpha 211 */ 212 virtual bool supportsCoverageAsAlpha() const; 213 214 /** 215 * The same as calling xfermode->supportsCoverageAsAlpha(), except that this also checks if 216 * the xfermode is NULL, and if so, treats it as kSrcOver_Mode. 217 */ 218 static bool SupportsCoverageAsAlpha(const SkXfermode* xfer); SupportsCoverageAsAlpha(const sk_sp<SkXfermode> & xfer)219 static bool SupportsCoverageAsAlpha(const sk_sp<SkXfermode>& xfer) { 220 return SupportsCoverageAsAlpha(xfer.get()); 221 } 222 223 enum SrcColorOpacity { 224 // The src color is known to be opaque (alpha == 255) 225 kOpaque_SrcColorOpacity = 0, 226 // The src color is known to be fully transparent (color == 0) 227 kTransparentBlack_SrcColorOpacity = 1, 228 // The src alpha is known to be fully transparent (alpha == 0) 229 kTransparentAlpha_SrcColorOpacity = 2, 230 // The src color opacity is unknown 231 kUnknown_SrcColorOpacity = 3 232 }; 233 234 /** 235 * Returns whether or not the result of the draw with the xfer mode will be opaque or not. The 236 * input to this call is an enum describing known information about the opacity of the src color 237 * that will be given to the xfer mode. 238 */ 239 virtual bool isOpaque(SrcColorOpacity opacityType) const; 240 241 /** 242 * The same as calling xfermode->isOpaque(...), except that this also checks if 243 * the xfermode is NULL, and if so, treats it as kSrcOver_Mode. 244 */ 245 static bool IsOpaque(const SkXfermode* xfer, SrcColorOpacity opacityType); IsOpaque(const sk_sp<SkXfermode> & xfer,SrcColorOpacity opacityType)246 static bool IsOpaque(const sk_sp<SkXfermode>& xfer, SrcColorOpacity opacityType) { 247 return IsOpaque(xfer.get(), opacityType); 248 } 249 static bool IsOpaque(SkBlendMode, SrcColorOpacity); 250 251 #if SK_SUPPORT_GPU 252 /** Used by the SkXfermodeImageFilter to blend two colors via a GrFragmentProcessor. 253 The input to the returned FP is the src color. The dst color is 254 provided by the dst param which becomes a child FP of the returned FP. 255 It is legal for the function to return a null output. This indicates that 256 the output of the blend is simply the src color. 257 */ 258 virtual sk_sp<GrFragmentProcessor> makeFragmentProcessorForImageFilter( 259 sk_sp<GrFragmentProcessor> dst) const; 260 261 /** A subclass must implement this factory function to work with the GPU backend. 262 The xfermode will return a factory for which the caller will get a ref. It is up 263 to the caller to install it. XferProcessors cannot use a background texture. 264 */ 265 virtual sk_sp<GrXPFactory> asXPFactory() const; 266 #endif 267 268 SK_TO_STRING_PUREVIRT() 269 SK_DECLARE_FLATTENABLE_REGISTRAR_GROUP() 270 SK_DEFINE_FLATTENABLE_TYPE(SkXfermode) 271 272 enum D32Flags { 273 kSrcIsOpaque_D32Flag = 1 << 0, 274 kSrcIsSingle_D32Flag = 1 << 1, 275 kDstIsSRGB_D32Flag = 1 << 2, 276 }; 277 typedef void (*D32Proc)(const SkXfermode*, uint32_t dst[], const SkPM4f src[], 278 int count, const SkAlpha coverage[]); 279 static D32Proc GetD32Proc(SkXfermode*, uint32_t flags); GetD32Proc(const sk_sp<SkXfermode> & xfer,uint32_t flags)280 static D32Proc GetD32Proc(const sk_sp<SkXfermode>& xfer, uint32_t flags) { 281 return GetD32Proc(xfer.get(), flags); 282 } 283 284 enum F16Flags { 285 kSrcIsOpaque_F16Flag = 1 << 0, 286 kSrcIsSingle_F16Flag = 1 << 1, 287 }; 288 typedef void (*F16Proc)(const SkXfermode*, uint64_t dst[], const SkPM4f src[], int count, 289 const SkAlpha coverage[]); 290 static F16Proc GetF16Proc(SkXfermode*, uint32_t flags); GetF16Proc(const sk_sp<SkXfermode> & xfer,uint32_t flags)291 static F16Proc GetF16Proc(const sk_sp<SkXfermode>& xfer, uint32_t flags) { 292 return GetF16Proc(xfer.get(), flags); 293 } 294 295 enum LCDFlags { 296 kSrcIsOpaque_LCDFlag = 1 << 0, // else src(s) may have alpha < 1 297 kSrcIsSingle_LCDFlag = 1 << 1, // else src[count] 298 kDstIsSRGB_LCDFlag = 1 << 2, // else l32 or f16 299 }; 300 typedef void (*LCD32Proc)(uint32_t* dst, const SkPM4f* src, int count, const uint16_t lcd[]); 301 typedef void (*LCDF16Proc)(uint64_t* dst, const SkPM4f* src, int count, const uint16_t lcd[]); 302 static LCD32Proc GetLCD32Proc(uint32_t flags); GetLCDF16Proc(uint32_t)303 static LCDF16Proc GetLCDF16Proc(uint32_t) { return nullptr; } 304 isArithmetic(SkArithmeticParams *)305 virtual bool isArithmetic(SkArithmeticParams*) const { return false; } 306 307 protected: SkXfermode()308 SkXfermode() {} 309 /** The default implementation of xfer32/xfer16/xferA8 in turn call this 310 method, 1 color at a time (upscaled to a SkPMColor). The default 311 implementation of this method just returns dst. If performance is 312 important, your subclass should override xfer32/xfer16/xferA8 directly. 313 314 This method will not be called directly by the client, so it need not 315 be implemented if your subclass has overridden xfer32/xfer16/xferA8 316 */ 317 virtual SkPMColor xferColor(SkPMColor src, SkPMColor dst) const; 318 319 virtual D32Proc onGetD32Proc(uint32_t flags) const; 320 virtual F16Proc onGetF16Proc(uint32_t flags) const; 321 virtual bool onAppendStages(SkRasterPipeline*) const; 322 323 private: 324 enum { 325 kModeCount = kLastMode + 1 326 }; 327 328 typedef SkFlattenable INHERITED; 329 }; 330 331 #endif 332