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