egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 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 GrXferProcessor_DEFINED |
| 9 | #define GrXferProcessor_DEFINED |
| 10 | |
| 11 | #include "GrColor.h" |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 12 | #include "GrProcessor.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 13 | #include "GrTexture.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | #include "SkXfermode.h" |
| 16 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 17 | class GrDrawTargetCaps; |
| 18 | class GrGLCaps; |
| 19 | class GrGLXferProcessor; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 20 | class GrProcOptInfo; |
| 21 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 22 | /** |
egdaniel | b197b8f | 2015-02-17 07:34:43 -0800 | [diff] [blame] | 23 | * Coeffecients for alpha-blending. |
| 24 | */ |
| 25 | enum GrBlendCoeff { |
| 26 | kInvalid_GrBlendCoeff = -1, |
| 27 | |
| 28 | kZero_GrBlendCoeff, //<! 0 |
| 29 | kOne_GrBlendCoeff, //<! 1 |
| 30 | kSC_GrBlendCoeff, //<! src color |
| 31 | kISC_GrBlendCoeff, //<! one minus src color |
| 32 | kDC_GrBlendCoeff, //<! dst color |
| 33 | kIDC_GrBlendCoeff, //<! one minus dst color |
| 34 | kSA_GrBlendCoeff, //<! src alpha |
| 35 | kISA_GrBlendCoeff, //<! one minus src alpha |
| 36 | kDA_GrBlendCoeff, //<! dst alpha |
| 37 | kIDA_GrBlendCoeff, //<! one minus dst alpha |
| 38 | kConstC_GrBlendCoeff, //<! constant color |
| 39 | kIConstC_GrBlendCoeff, //<! one minus constant color |
| 40 | kConstA_GrBlendCoeff, //<! constant color alpha |
| 41 | kIConstA_GrBlendCoeff, //<! one minus constant color alpha |
| 42 | kS2C_GrBlendCoeff, |
| 43 | kIS2C_GrBlendCoeff, |
| 44 | kS2A_GrBlendCoeff, |
| 45 | kIS2A_GrBlendCoeff, |
| 46 | |
| 47 | kTotalGrBlendCoeffCount |
| 48 | }; |
| 49 | |
| 50 | /** |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 51 | * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst |
| 52 | * color. It does this by emitting fragment shader code and controlling the fixed-function blend |
| 53 | * state. The inputs to its shader code are the final computed src color and fractional pixel |
| 54 | * coverage. The GrXferProcessor's shader code writes the fragment shader output color that goes |
| 55 | * into the fixed-function blend. When dual-source blending is available, it may also write a |
| 56 | * seconday fragment shader output color. When allowed by the backend API, the GrXferProcessor may |
| 57 | * read the destination color. The GrXferProcessor is responsible for setting the blend coefficients |
| 58 | * and blend constant color. |
| 59 | * |
| 60 | * A GrXferProcessor is never installed directly into our draw state, but instead is created from a |
| 61 | * GrXPFactory once we have finalized the state of our draw. |
| 62 | */ |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 63 | class GrXferProcessor : public GrProcessor { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 64 | public: |
| 65 | /** |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 66 | * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLProcessorKey(...) to get the |
| 67 | * specific subclass's key. |
| 68 | */ |
| 69 | void getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 70 | |
| 71 | /** Returns a new instance of the appropriate *GL* implementation class |
| 72 | for the given GrXferProcessor; caller is responsible for deleting |
| 73 | the object. */ |
| 74 | virtual GrGLXferProcessor* createGLInstance() const = 0; |
| 75 | |
| 76 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 77 | * Optimizations for blending / coverage that an OptDrawState should apply to itself. |
| 78 | */ |
| 79 | enum OptFlags { |
| 80 | /** |
| 81 | * No optimizations needed |
| 82 | */ |
| 83 | kNone_Opt = 0, |
| 84 | /** |
| 85 | * The draw can be skipped completely. |
| 86 | */ |
| 87 | kSkipDraw_OptFlag = 0x1, |
| 88 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 89 | * GrXferProcessor will ignore color, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 90 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 91 | kIgnoreColor_OptFlag = 0x2, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 92 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 93 | * GrXferProcessor will ignore coverage, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 94 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 95 | kIgnoreCoverage_OptFlag = 0x4, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 96 | /** |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 97 | * Clear color stages and override input color to that returned by getOptimizations |
| 98 | */ |
| 99 | kOverrideColor_OptFlag = 0x8, |
| 100 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 101 | * Set CoverageDrawing_StateBit |
| 102 | */ |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 103 | kSetCoverageDrawing_OptFlag = 0x10, |
egdaniel | f7c2d55 | 2015-02-13 12:11:00 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Can tweak alpha for coverage. Currently this flag should only be used by a batch |
| 106 | */ |
| 107 | kCanTweakAlphaForCoverage_OptFlag = 0x20, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags); |
| 111 | |
| 112 | /** |
| 113 | * Determines which optimizations (as described by the ptFlags above) can be performed by |
| 114 | * the draw with this xfer processor. If this function is called, the xfer processor may change |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 115 | * its state to reflected the given blend optimizations. If the XP needs to see a specific input |
| 116 | * color to blend correctly, it will set the OverrideColor flag and the output parameter |
| 117 | * overrideColor will be the required value that should be passed into the XP. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 118 | * A caller who calls this function on a XP is required to honor the returned OptFlags |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 119 | * and color values for its draw. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 120 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 121 | virtual OptFlags getOptimizations(const GrProcOptInfo& colorPOI, |
| 122 | const GrProcOptInfo& coveragePOI, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 123 | bool doesStencilWrite, |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 124 | GrColor* overrideColor, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 125 | const GrDrawTargetCaps& caps) = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 126 | |
| 127 | struct BlendInfo { |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 128 | BlendInfo() : fWriteColor(true) {} |
| 129 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 130 | GrBlendCoeff fSrcBlend; |
| 131 | GrBlendCoeff fDstBlend; |
| 132 | GrColor fBlendConstant; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 133 | bool fWriteColor; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | virtual void getBlendInfo(BlendInfo* blendInfo) const = 0; |
| 137 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 138 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 139 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Returns the texture to be used as the destination when reading the dst in the fragment |
| 142 | * shader. If the returned texture is NULL then the XP is either not reading the dst or we have |
| 143 | * extentions that support framebuffer fetching and thus don't need a copy of the dst texture. |
| 144 | */ |
| 145 | const GrTexture* getDstCopyTexture() const { return fDstCopy.getTexture(); } |
| 146 | |
| 147 | /** |
| 148 | * Returns the offset into the DstCopyTexture to use when reading it in the shader. This value |
| 149 | * is only valid if getDstCopyTexture() != NULL. |
| 150 | */ |
| 151 | const SkIPoint& dstCopyTextureOffset() const { |
| 152 | SkASSERT(this->getDstCopyTexture()); |
| 153 | return fDstCopyTextureOffset; |
| 154 | } |
| 155 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Returns whether or not this xferProcossor will set a secondary output to be used with dual |
| 158 | * source blending. |
| 159 | */ |
| 160 | virtual bool hasSecondaryOutput() const { return false; } |
| 161 | |
| 162 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 163 | true when the two processor are of the same subclass (i.e. they return the same object from |
| 164 | from getFactory()). |
| 165 | |
| 166 | A return value of true from isEqual() should not be used to test whether the processor would |
| 167 | generate the same shader code. To test for identical code generation use getGLProcessorKey*/ |
| 168 | |
| 169 | bool isEqual(const GrXferProcessor& that) const { |
| 170 | if (this->classID() != that.classID()) { |
| 171 | return false; |
| 172 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 173 | if (this->fWillReadDstColor != that.fWillReadDstColor) { |
| 174 | return false; |
| 175 | } |
| 176 | if (this->fDstCopy.getTexture() != that.fDstCopy.getTexture()) { |
| 177 | return false; |
| 178 | } |
| 179 | if (this->fDstCopyTextureOffset != that.fDstCopyTextureOffset) { |
| 180 | return false; |
| 181 | } |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 182 | return this->onIsEqual(that); |
| 183 | } |
| 184 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 185 | protected: |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 186 | GrXferProcessor(); |
| 187 | GrXferProcessor(const GrDeviceCoordTexture* dstCopy, bool willReadDstColor); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 188 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 189 | private: |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 190 | /** |
| 191 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer |
| 192 | * processor's GL backend implementation. |
| 193 | */ |
| 194 | virtual void onGetGLProcessorKey(const GrGLCaps& caps, |
| 195 | GrProcessorKeyBuilder* b) const = 0; |
| 196 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 197 | virtual bool onIsEqual(const GrXferProcessor&) const = 0; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 198 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 199 | bool fWillReadDstColor; |
| 200 | SkIPoint fDstCopyTextureOffset; |
| 201 | GrTextureAccess fDstCopy; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 202 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 203 | typedef GrFragmentProcessor INHERITED; |
| 204 | }; |
| 205 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 206 | GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags); |
| 207 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 208 | /////////////////////////////////////////////////////////////////////////////// |
| 209 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 210 | /** |
| 211 | * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is |
| 212 | * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the |
| 213 | * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the |
| 214 | * draw information to create a GrXferProcessor (XP) which can implement the desired blending for |
| 215 | * the draw. |
| 216 | * |
| 217 | * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it |
| 218 | * creates will have. For example, can it create an XP that supports RGB coverage or will the XP |
| 219 | * blend with the destination color. |
| 220 | */ |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 221 | class GrXPFactory : public SkRefCnt { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 222 | public: |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 223 | GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI, |
| 224 | const GrProcOptInfo& coveragePOI, |
| 225 | const GrDeviceCoordTexture* dstCopy, |
| 226 | const GrDrawTargetCaps& caps) const; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * This function returns true if the GrXferProcessor generated from this factory will be able to |
| 230 | * correctly blend when using RGB coverage. The knownColor and knownColorFlags represent the |
| 231 | * final computed color from the color stages. |
| 232 | */ |
| 233 | virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const = 0; |
| 234 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 235 | /** |
| 236 | * Depending on color blend mode requested it may or may not be possible to correctly blend with |
| 237 | * fractional pixel coverage generated by the fragment shader. |
| 238 | * |
| 239 | * This function considers the known color and coverage input into the xfer processor and |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 240 | * certain state information (colorWriteDisabled) to determine whether |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 241 | * coverage can be handled correctly. |
| 242 | */ |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 243 | virtual bool canApplyCoverage(const GrProcOptInfo& colorPOI, |
| 244 | const GrProcOptInfo& coveragePOI) const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 245 | |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 246 | |
| 247 | struct InvariantOutput { |
| 248 | bool fWillBlendWithDst; |
| 249 | GrColor fBlendedColor; |
| 250 | uint32_t fBlendedColorFlags; |
| 251 | }; |
| 252 | |
| 253 | /** |
| 254 | * This function returns known information about the output of the xfer processor produced by |
| 255 | * this xp factory. The invariant color information returned by this function refers to the |
| 256 | * final color produced after all blending. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 257 | */ |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 258 | virtual void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI, |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 259 | InvariantOutput*) const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 263 | * coverage before the blend will give the correct final destination color. In general it |
| 264 | * will not as coverage is applied after blending. |
| 265 | */ |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 266 | virtual bool canTweakAlphaForCoverage() const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 267 | |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 268 | bool willNeedDstCopy(const GrDrawTargetCaps& caps, const GrProcOptInfo& colorPOI, |
| 269 | const GrProcOptInfo& coveragePOI) const; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 270 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 271 | bool isEqual(const GrXPFactory& that) const { |
| 272 | if (this->classID() != that.classID()) { |
| 273 | return false; |
| 274 | } |
| 275 | return this->onIsEqual(that); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Helper for down-casting to a GrXPFactory subclass |
| 280 | */ |
| 281 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 282 | |
| 283 | uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; } |
| 284 | |
| 285 | protected: |
| 286 | GrXPFactory() : fClassID(kIllegalXPFClassID) {} |
| 287 | |
| 288 | template <typename XPF_SUBCLASS> void initClassID() { |
| 289 | static uint32_t kClassID = GenClassID(); |
| 290 | fClassID = kClassID; |
| 291 | } |
| 292 | |
| 293 | uint32_t fClassID; |
| 294 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 295 | private: |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame^] | 296 | virtual GrXferProcessor* onCreateXferProcessor(const GrDrawTargetCaps& caps, |
| 297 | const GrProcOptInfo& colorPOI, |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 298 | const GrProcOptInfo& coveragePOI, |
| 299 | const GrDeviceCoordTexture* dstCopy) const = 0; |
| 300 | /** |
| 301 | * Returns true if the XP generated by this factory will explicitly read dst in the fragment |
| 302 | * shader. |
| 303 | */ |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame^] | 304 | virtual bool willReadDstColor(const GrDrawTargetCaps& caps, |
| 305 | const GrProcOptInfo& colorPOI, |
egdaniel | e36914c | 2015-02-13 09:00:33 -0800 | [diff] [blame] | 306 | const GrProcOptInfo& coveragePOI) const = 0; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 307 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 308 | virtual bool onIsEqual(const GrXPFactory&) const = 0; |
| 309 | |
| 310 | static uint32_t GenClassID() { |
| 311 | // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The |
| 312 | // atomic inc returns the old value not the incremented value. So we add |
| 313 | // 1 to the returned value. |
| 314 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1; |
| 315 | if (!id) { |
| 316 | SkFAIL("This should never wrap as it should only be called once for each GrXPFactory " |
| 317 | "subclass."); |
| 318 | } |
| 319 | return id; |
| 320 | } |
| 321 | |
| 322 | enum { |
| 323 | kIllegalXPFClassID = 0, |
| 324 | }; |
| 325 | static int32_t gCurrXPFClassID; |
| 326 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 327 | typedef GrProgramElement INHERITED; |
| 328 | }; |
| 329 | |
| 330 | #endif |
| 331 | |