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