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