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 | |
cdalton | ee0175f | 2015-06-12 08:21:26 -0700 | [diff] [blame] | 11 | #include "GrBlend.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 12 | #include "GrColor.h" |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 13 | #include "GrProcessor.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 14 | #include "GrTexture.h" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 15 | #include "GrTypes.h" |
| 16 | #include "SkXfermode.h" |
| 17 | |
jvanverth | e9c0fc6 | 2015-04-29 11:18:05 -0700 | [diff] [blame] | 18 | class GrShaderCaps; |
| 19 | class GrGLSLCaps; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 20 | class GrGLXferProcessor; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 21 | class GrProcOptInfo; |
| 22 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 23 | /** |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 24 | * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes |
| 25 | * required after a pixel has been written, before it can be safely read again. |
| 26 | */ |
| 27 | enum GrXferBarrierType { |
| 28 | kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture. |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 29 | kBlend_GrXferBarrierType, //<! Required by certain blend extensions. |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /** |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 33 | * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 34 | * color, and for applying any coverage. It does this by emitting fragment shader code and |
| 35 | * controlling the fixed-function blend state. When dual-source blending is available, it may also |
| 36 | * write a seconday fragment shader output color. GrXferProcessor has two modes of operation: |
| 37 | * |
| 38 | * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the |
| 39 | * GrXferProcessor may read the destination color. While operating in this mode, the subclass only |
| 40 | * provides shader code that blends the src and dst colors, and the base class applies coverage. |
| 41 | * |
| 42 | * No dst read: When not performing a dst read, the subclass is given full control of the fixed- |
| 43 | * function blend state and/or secondary output, and is responsible to apply coverage on its own. |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 44 | * |
| 45 | * A GrXferProcessor is never installed directly into our draw state, but instead is created from a |
| 46 | * GrXPFactory once we have finalized the state of our draw. |
| 47 | */ |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 48 | class GrXferProcessor : public GrProcessor { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 49 | public: |
| 50 | /** |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 51 | * A texture that contains the dst pixel values and an integer coord offset from device space |
| 52 | * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a |
| 53 | * GrXferProcessor for blending in the fragment shader. |
| 54 | */ |
| 55 | class DstTexture { |
| 56 | public: |
| 57 | DstTexture() { fOffset.set(0, 0); } |
| 58 | |
| 59 | DstTexture(const DstTexture& other) { |
| 60 | *this = other; |
| 61 | } |
| 62 | |
| 63 | DstTexture(GrTexture* texture, const SkIPoint& offset) |
| 64 | : fTexture(SkSafeRef(texture)) |
| 65 | , fOffset(offset) { |
| 66 | } |
| 67 | |
| 68 | DstTexture& operator=(const DstTexture& other) { |
| 69 | fTexture.reset(SkSafeRef(other.fTexture.get())); |
| 70 | fOffset = other.fOffset; |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | const SkIPoint& offset() const { return fOffset; } |
| 75 | |
| 76 | void setOffset(const SkIPoint& offset) { fOffset = offset; } |
| 77 | void setOffset(int ox, int oy) { fOffset.set(ox, oy); } |
| 78 | |
| 79 | GrTexture* texture() const { return fTexture.get(); } |
| 80 | |
| 81 | GrTexture* setTexture(GrTexture* texture) { |
| 82 | fTexture.reset(SkSafeRef(texture)); |
| 83 | return texture; |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | SkAutoTUnref<GrTexture> fTexture; |
| 88 | SkIPoint fOffset; |
| 89 | }; |
| 90 | |
| 91 | /** |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 92 | * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLProcessorKey(...) to get the |
| 93 | * specific subclass's key. |
| 94 | */ |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 95 | void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 96 | |
| 97 | /** Returns a new instance of the appropriate *GL* implementation class |
| 98 | for the given GrXferProcessor; caller is responsible for deleting |
| 99 | the object. */ |
| 100 | virtual GrGLXferProcessor* createGLInstance() const = 0; |
| 101 | |
| 102 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 103 | * Optimizations for blending / coverage that an OptDrawState should apply to itself. |
| 104 | */ |
| 105 | enum OptFlags { |
| 106 | /** |
bsalomon | d07a279 | 2015-07-08 10:20:21 -0700 | [diff] [blame^] | 107 | * No optimizations needed |
| 108 | */ |
| 109 | kNone_Opt = 0, |
| 110 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 111 | * The draw can be skipped completely. |
| 112 | */ |
| 113 | kSkipDraw_OptFlag = 0x1, |
| 114 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 115 | * GrXferProcessor will ignore color, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 116 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 117 | kIgnoreColor_OptFlag = 0x2, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 118 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 119 | * GrXferProcessor will ignore coverage, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 120 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 121 | kIgnoreCoverage_OptFlag = 0x4, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 122 | /** |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 123 | * Clear color stages and override input color to that returned by getOptimizations |
| 124 | */ |
| 125 | kOverrideColor_OptFlag = 0x8, |
| 126 | /** |
egdaniel | f7c2d55 | 2015-02-13 12:11:00 -0800 | [diff] [blame] | 127 | * Can tweak alpha for coverage. Currently this flag should only be used by a batch |
| 128 | */ |
| 129 | kCanTweakAlphaForCoverage_OptFlag = 0x20, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags); |
| 133 | |
| 134 | /** |
| 135 | * Determines which optimizations (as described by the ptFlags above) can be performed by |
| 136 | * 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] | 137 | * its state to reflected the given blend optimizations. If the XP needs to see a specific input |
| 138 | * color to blend correctly, it will set the OverrideColor flag and the output parameter |
| 139 | * overrideColor will be the required value that should be passed into the XP. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 140 | * 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] | 141 | * and color values for its draw. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 142 | */ |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 143 | OptFlags getOptimizations(const GrProcOptInfo& colorPOI, |
| 144 | const GrProcOptInfo& coveragePOI, |
| 145 | bool doesStencilWrite, |
| 146 | GrColor* overrideColor, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 147 | const GrCaps& caps); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 148 | |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Returns whether this XP will require an Xfer barrier on the given rt. If true, outBarrierType |
| 151 | * is updated to contain the type of barrier needed. |
| 152 | */ |
| 153 | bool willNeedXferBarrier(const GrRenderTarget* rt, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 154 | const GrCaps& caps, |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 155 | GrXferBarrierType* outBarrierType) const; |
| 156 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 157 | struct BlendInfo { |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 158 | void reset() { |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 159 | fEquation = kAdd_GrBlendEquation; |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 160 | fSrcBlend = kOne_GrBlendCoeff; |
| 161 | fDstBlend = kZero_GrBlendCoeff; |
| 162 | fBlendConstant = 0; |
| 163 | fWriteColor = true; |
| 164 | } |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 165 | |
bsalomon | f7cc877 | 2015-05-11 11:21:14 -0700 | [diff] [blame] | 166 | SkDEBUGCODE(SkString dump() const;) |
| 167 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 168 | GrBlendEquation fEquation; |
| 169 | GrBlendCoeff fSrcBlend; |
| 170 | GrBlendCoeff fDstBlend; |
| 171 | GrColor fBlendConstant; |
| 172 | bool fWriteColor; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 175 | void getBlendInfo(BlendInfo* blendInfo) const; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 176 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 177 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 178 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Returns the texture to be used as the destination when reading the dst in the fragment |
| 181 | * shader. If the returned texture is NULL then the XP is either not reading the dst or we have |
| 182 | * extentions that support framebuffer fetching and thus don't need a copy of the dst texture. |
| 183 | */ |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 184 | const GrTexture* getDstTexture() const { return fDstTexture.getTexture(); } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 187 | * Returns the offset in device coords to use when accessing the dst texture to get the dst |
| 188 | * pixel color in the shader. This value is only valid if getDstTexture() != NULL. |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 189 | */ |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 190 | const SkIPoint& dstTextureOffset() const { |
| 191 | SkASSERT(this->getDstTexture()); |
| 192 | return fDstTextureOffset; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 193 | } |
| 194 | |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 195 | /** |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 196 | * If we are performing a dst read, returns whether the base class will use mixed samples to |
| 197 | * antialias the shader's final output. If not doing a dst read, the subclass is responsible |
| 198 | * for antialiasing and this returns false. |
| 199 | */ |
| 200 | bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; } |
| 201 | |
| 202 | /** |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 203 | * Returns whether or not the XP will look at coverage when doing its blending. |
| 204 | */ |
| 205 | bool readsCoverage() const { return fReadsCoverage; } |
| 206 | |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 207 | /** |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 208 | * Returns whether or not this xferProcossor will set a secondary output to be used with dual |
| 209 | * source blending. |
| 210 | */ |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 211 | bool hasSecondaryOutput() const; |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 212 | |
| 213 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 214 | true when the two processor are of the same subclass (i.e. they return the same object from |
| 215 | from getFactory()). |
| 216 | |
| 217 | A return value of true from isEqual() should not be used to test whether the processor would |
| 218 | generate the same shader code. To test for identical code generation use getGLProcessorKey*/ |
| 219 | |
| 220 | bool isEqual(const GrXferProcessor& that) const { |
| 221 | if (this->classID() != that.classID()) { |
| 222 | return false; |
| 223 | } |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 224 | if (this->fWillReadDstColor != that.fWillReadDstColor) { |
| 225 | return false; |
| 226 | } |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 227 | if (this->fReadsCoverage != that.fReadsCoverage) { |
| 228 | return false; |
| 229 | } |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 230 | if (this->fDstTexture.getTexture() != that.fDstTexture.getTexture()) { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 233 | if (this->fDstTextureOffset != that.fDstTextureOffset) { |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 234 | return false; |
| 235 | } |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 236 | if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) { |
| 237 | return false; |
| 238 | } |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 239 | return this->onIsEqual(that); |
| 240 | } |
| 241 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 242 | protected: |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 243 | GrXferProcessor(); |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 244 | GrXferProcessor(const DstTexture*, bool willReadDstColor, bool hasMixedSamples); |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 245 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 246 | private: |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 247 | virtual OptFlags onGetOptimizations(const GrProcOptInfo& colorPOI, |
| 248 | const GrProcOptInfo& coveragePOI, |
| 249 | bool doesStencilWrite, |
| 250 | GrColor* overrideColor, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 251 | const GrCaps& caps) = 0; |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 252 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 253 | /** |
| 254 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer |
| 255 | * processor's GL backend implementation. |
| 256 | */ |
jvanverth | cfc1886 | 2015-04-28 08:48:20 -0700 | [diff] [blame] | 257 | virtual void onGetGLProcessorKey(const GrGLSLCaps& caps, |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 258 | GrProcessorKeyBuilder* b) const = 0; |
| 259 | |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 260 | /** |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 261 | * If not using a texture barrier, retrieves whether the subclass will require a different type |
| 262 | * of barrier. |
| 263 | */ |
| 264 | virtual bool onWillNeedXferBarrier(const GrRenderTarget*, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 265 | const GrCaps&, |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 266 | GrXferBarrierType* outBarrierType SK_UNUSED) const { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | /** |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 271 | * If we are not performing a dst read, returns whether the subclass will set a secondary |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 272 | * output. When using dst reads, the base class controls the secondary output and this method |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 273 | * will not be called. |
| 274 | */ |
| 275 | virtual bool onHasSecondaryOutput() const { return false; } |
| 276 | |
| 277 | /** |
| 278 | * If we are not performing a dst read, retrieves the fixed-function blend state required by the |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 279 | * subclass. When using dst reads, the base class controls the fixed-function blend state and |
| 280 | * this method will not be called. The BlendInfo struct comes initialized to "no blending". |
cdalton | f4f2b44 | 2015-04-23 09:40:23 -0700 | [diff] [blame] | 281 | */ |
| 282 | virtual void onGetBlendInfo(BlendInfo*) const {} |
| 283 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 284 | virtual bool onIsEqual(const GrXferProcessor&) const = 0; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 285 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 286 | bool fWillReadDstColor; |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 287 | bool fDstReadUsesMixedSamples; |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 288 | bool fReadsCoverage; |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 289 | SkIPoint fDstTextureOffset; |
| 290 | GrTextureAccess fDstTexture; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 291 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 292 | typedef GrFragmentProcessor INHERITED; |
| 293 | }; |
| 294 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 295 | GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags); |
| 296 | |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 297 | /////////////////////////////////////////////////////////////////////////////// |
| 298 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 299 | /** |
| 300 | * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is |
| 301 | * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the |
| 302 | * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the |
| 303 | * draw information to create a GrXferProcessor (XP) which can implement the desired blending for |
| 304 | * the draw. |
| 305 | * |
| 306 | * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it |
| 307 | * creates will have. For example, can it create an XP that supports RGB coverage or will the XP |
| 308 | * blend with the destination color. |
| 309 | */ |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 310 | class GrXPFactory : public SkRefCnt { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 311 | public: |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 312 | typedef GrXferProcessor::DstTexture DstTexture; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 313 | GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI, |
| 314 | const GrProcOptInfo& coveragePOI, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 315 | bool hasMixedSamples, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 316 | const DstTexture*, |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 317 | const GrCaps& caps) const; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * This function returns true if the GrXferProcessor generated from this factory will be able to |
| 321 | * correctly blend when using RGB coverage. The knownColor and knownColorFlags represent the |
| 322 | * final computed color from the color stages. |
| 323 | */ |
| 324 | virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const = 0; |
| 325 | |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Known color information after blending, but before accounting for any coverage. |
| 328 | */ |
| 329 | struct InvariantBlendedColor { |
| 330 | bool fWillBlendWithDst; |
| 331 | GrColor fKnownColor; |
| 332 | GrColorComponentFlags fKnownColorFlags; |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | /** |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 336 | * Returns information about the output color, produced by XPs from this factory, that will be |
| 337 | * known after blending. Note that we can conflate coverage and color, so the actual values |
| 338 | * written to pixels with partial coverage may not always seem consistent with the invariant |
| 339 | * information returned by this function. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 340 | */ |
cdalton | 1fa4572 | 2015-06-02 10:43:39 -0700 | [diff] [blame] | 341 | virtual void getInvariantBlendedColor(const GrProcOptInfo& colorPOI, |
| 342 | InvariantBlendedColor*) const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 343 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 344 | bool willNeedDstTexture(const GrCaps& caps, const GrProcOptInfo& colorPOI, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 345 | const GrProcOptInfo& coveragePOI, bool hasMixedSamples) const; |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 346 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 347 | bool isEqual(const GrXPFactory& that) const { |
| 348 | if (this->classID() != that.classID()) { |
| 349 | return false; |
| 350 | } |
| 351 | return this->onIsEqual(that); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Helper for down-casting to a GrXPFactory subclass |
| 356 | */ |
| 357 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 358 | |
| 359 | uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; } |
| 360 | |
| 361 | protected: |
| 362 | GrXPFactory() : fClassID(kIllegalXPFClassID) {} |
| 363 | |
| 364 | template <typename XPF_SUBCLASS> void initClassID() { |
| 365 | static uint32_t kClassID = GenClassID(); |
| 366 | fClassID = kClassID; |
| 367 | } |
| 368 | |
| 369 | uint32_t fClassID; |
| 370 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 371 | private: |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 372 | virtual GrXferProcessor* onCreateXferProcessor(const GrCaps& caps, |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame] | 373 | const GrProcOptInfo& colorPOI, |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 374 | const GrProcOptInfo& coveragePOI, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 375 | bool hasMixedSamples, |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 376 | const DstTexture*) const = 0; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 377 | /** |
| 378 | * Returns true if the XP generated by this factory will explicitly read dst in the fragment |
| 379 | * shader. |
| 380 | */ |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 381 | virtual bool willReadDstColor(const GrCaps& caps, |
egdaniel | 3ad6570 | 2015-02-17 11:15:47 -0800 | [diff] [blame] | 382 | const GrProcOptInfo& colorPOI, |
cdalton | 86ae0a9 | 2015-06-08 15:11:04 -0700 | [diff] [blame] | 383 | const GrProcOptInfo& coveragePOI, |
| 384 | bool hasMixedSamples) const = 0; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 385 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 386 | virtual bool onIsEqual(const GrXPFactory&) const = 0; |
| 387 | |
| 388 | static uint32_t GenClassID() { |
| 389 | // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The |
| 390 | // atomic inc returns the old value not the incremented value. So we add |
| 391 | // 1 to the returned value. |
| 392 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1; |
| 393 | if (!id) { |
| 394 | SkFAIL("This should never wrap as it should only be called once for each GrXPFactory " |
| 395 | "subclass."); |
| 396 | } |
| 397 | return id; |
| 398 | } |
| 399 | |
| 400 | enum { |
| 401 | kIllegalXPFClassID = 0, |
| 402 | }; |
| 403 | static int32_t gCurrXPFClassID; |
| 404 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 405 | typedef GrProgramElement INHERITED; |
| 406 | }; |
| 407 | |
| 408 | #endif |
| 409 | |