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" |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 13 | #include "GrTypes.h" |
| 14 | #include "SkXfermode.h" |
| 15 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 16 | class GrDrawTargetCaps; |
| 17 | class GrGLCaps; |
| 18 | class GrGLXferProcessor; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 19 | class GrProcOptInfo; |
| 20 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 21 | /** |
| 22 | * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst |
| 23 | * color. It does this by emitting fragment shader code and controlling the fixed-function blend |
| 24 | * state. The inputs to its shader code are the final computed src color and fractional pixel |
| 25 | * coverage. The GrXferProcessor's shader code writes the fragment shader output color that goes |
| 26 | * into the fixed-function blend. When dual-source blending is available, it may also write a |
| 27 | * seconday fragment shader output color. When allowed by the backend API, the GrXferProcessor may |
| 28 | * read the destination color. The GrXferProcessor is responsible for setting the blend coefficients |
| 29 | * and blend constant color. |
| 30 | * |
| 31 | * A GrXferProcessor is never installed directly into our draw state, but instead is created from a |
| 32 | * GrXPFactory once we have finalized the state of our draw. |
| 33 | */ |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 34 | class GrXferProcessor : public GrProcessor { |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 35 | public: |
| 36 | /** |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 37 | * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer |
| 38 | * processor's GL backend implementation. |
| 39 | */ |
| 40 | virtual void getGLProcessorKey(const GrGLCaps& caps, |
| 41 | GrProcessorKeyBuilder* b) const = 0; |
| 42 | |
| 43 | /** Returns a new instance of the appropriate *GL* implementation class |
| 44 | for the given GrXferProcessor; caller is responsible for deleting |
| 45 | the object. */ |
| 46 | virtual GrGLXferProcessor* createGLInstance() const = 0; |
| 47 | |
| 48 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 49 | * Optimizations for blending / coverage that an OptDrawState should apply to itself. |
| 50 | */ |
| 51 | enum OptFlags { |
| 52 | /** |
| 53 | * No optimizations needed |
| 54 | */ |
| 55 | kNone_Opt = 0, |
| 56 | /** |
| 57 | * The draw can be skipped completely. |
| 58 | */ |
| 59 | kSkipDraw_OptFlag = 0x1, |
| 60 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 61 | * GrXferProcessor will ignore color, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 62 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 63 | kIgnoreColor_OptFlag = 0x2, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 64 | /** |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 65 | * GrXferProcessor will ignore coverage, thus no need to provide |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 66 | */ |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 67 | kIgnoreCoverage_OptFlag = 0x4, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 68 | /** |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 69 | * Clear color stages and override input color to that returned by getOptimizations |
| 70 | */ |
| 71 | kOverrideColor_OptFlag = 0x8, |
| 72 | /** |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 73 | * Set CoverageDrawing_StateBit |
| 74 | */ |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 75 | kSetCoverageDrawing_OptFlag = 0x10, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | GR_DECL_BITFIELD_OPS_FRIENDS(OptFlags); |
| 79 | |
| 80 | /** |
| 81 | * Determines which optimizations (as described by the ptFlags above) can be performed by |
| 82 | * 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] | 83 | * its state to reflected the given blend optimizations. If the XP needs to see a specific input |
| 84 | * color to blend correctly, it will set the OverrideColor flag and the output parameter |
| 85 | * overrideColor will be the required value that should be passed into the XP. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 86 | * 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] | 87 | * and color values for its draw. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 88 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 89 | // TODO: remove need for colorWriteDisabled once colorWriteDisabled is its own XP. |
| 90 | virtual OptFlags getOptimizations(const GrProcOptInfo& colorPOI, |
| 91 | const GrProcOptInfo& coveragePOI, |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 92 | bool colorWriteDisabled, |
| 93 | bool doesStencilWrite, |
egdaniel | 54160f3 | 2014-12-15 12:38:53 -0800 | [diff] [blame] | 94 | GrColor* overrideColor, |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 95 | const GrDrawTargetCaps& caps) = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 96 | |
| 97 | struct BlendInfo { |
| 98 | GrBlendCoeff fSrcBlend; |
| 99 | GrBlendCoeff fDstBlend; |
| 100 | GrColor fBlendConstant; |
| 101 | }; |
| 102 | |
| 103 | virtual void getBlendInfo(BlendInfo* blendInfo) const = 0; |
| 104 | |
| 105 | /** Will this prceossor read the destination pixel value? */ |
| 106 | bool willReadDstColor() const { return fWillReadDstColor; } |
| 107 | |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Returns whether or not this xferProcossor will set a secondary output to be used with dual |
| 110 | * source blending. |
| 111 | */ |
| 112 | virtual bool hasSecondaryOutput() const { return false; } |
| 113 | |
| 114 | /** Returns true if this and other processor conservatively draw identically. It can only return |
| 115 | true when the two processor are of the same subclass (i.e. they return the same object from |
| 116 | from getFactory()). |
| 117 | |
| 118 | A return value of true from isEqual() should not be used to test whether the processor would |
| 119 | generate the same shader code. To test for identical code generation use getGLProcessorKey*/ |
| 120 | |
| 121 | bool isEqual(const GrXferProcessor& that) const { |
| 122 | if (this->classID() != that.classID()) { |
| 123 | return false; |
| 124 | } |
| 125 | return this->onIsEqual(that); |
| 126 | } |
| 127 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 128 | protected: |
| 129 | GrXferProcessor() : fWillReadDstColor(false) {} |
| 130 | |
| 131 | /** |
| 132 | * If the prceossor subclass will read the destination pixel value then it must call this |
| 133 | * function from its constructor. Otherwise, when its generated backend-specific prceossor class |
| 134 | * attempts to generate code that reads the destination pixel it will fail. |
| 135 | */ |
| 136 | void setWillReadDstColor() { fWillReadDstColor = true; } |
| 137 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 138 | private: |
egdaniel | c230414 | 2014-12-11 13:15:13 -0800 | [diff] [blame] | 139 | virtual bool onIsEqual(const GrXferProcessor&) const = 0; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 140 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 141 | bool fWillReadDstColor; |
| 142 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 143 | typedef GrFragmentProcessor INHERITED; |
| 144 | }; |
| 145 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 146 | GR_MAKE_BITFIELD_OPS(GrXferProcessor::OptFlags); |
| 147 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 148 | /** |
| 149 | * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is |
| 150 | * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the |
| 151 | * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the |
| 152 | * draw information to create a GrXferProcessor (XP) which can implement the desired blending for |
| 153 | * the draw. |
| 154 | * |
| 155 | * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it |
| 156 | * creates will have. For example, can it create an XP that supports RGB coverage or will the XP |
| 157 | * blend with the destination color. |
| 158 | */ |
egdaniel | c016fb8 | 2014-12-03 11:41:54 -0800 | [diff] [blame] | 159 | class GrXPFactory : public SkRefCnt { |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 160 | public: |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 161 | virtual GrXferProcessor* createXferProcessor(const GrProcOptInfo& colorPOI, |
| 162 | const GrProcOptInfo& coveragePOI) const = 0; |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * This function returns true if the GrXferProcessor generated from this factory will be able to |
| 166 | * correctly blend when using RGB coverage. The knownColor and knownColorFlags represent the |
| 167 | * final computed color from the color stages. |
| 168 | */ |
| 169 | virtual bool supportsRGBCoverage(GrColor knownColor, uint32_t knownColorFlags) const = 0; |
| 170 | |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Depending on color blend mode requested it may or may not be possible to correctly blend with |
| 173 | * fractional pixel coverage generated by the fragment shader. |
| 174 | * |
| 175 | * 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] | 176 | * certain state information (colorWriteDisabled) to determine whether |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 177 | * coverage can be handled correctly. |
| 178 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 179 | // TODO: remove need for colorWriteDisabled once colorWriteDisabled is its own XP. |
| 180 | virtual bool canApplyCoverage(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI, |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 181 | bool colorWriteDisabled) const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 182 | |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame^] | 183 | |
| 184 | struct InvariantOutput { |
| 185 | bool fWillBlendWithDst; |
| 186 | GrColor fBlendedColor; |
| 187 | uint32_t fBlendedColorFlags; |
| 188 | }; |
| 189 | |
| 190 | /** |
| 191 | * This function returns known information about the output of the xfer processor produced by |
| 192 | * this xp factory. The invariant color information returned by this function refers to the |
| 193 | * final color produced after all blending. |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 194 | */ |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 195 | // TODO: remove need for colorWriteDisabled once only XP can read dst. |
egdaniel | 9e4ecdc | 2014-12-18 12:44:55 -0800 | [diff] [blame^] | 196 | virtual void getInvariantOutput(const GrProcOptInfo& colorPOI, const GrProcOptInfo& coveragePOI, |
| 197 | bool colorWriteDisabled, InvariantOutput*) const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Determines whether multiplying the computed per-pixel color by the pixel's fractional |
| 201 | * coverage before the blend will give the correct final destination color. In general it |
| 202 | * will not as coverage is applied after blending. |
| 203 | */ |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 204 | virtual bool canTweakAlphaForCoverage() const = 0; |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 205 | |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 206 | bool isEqual(const GrXPFactory& that) const { |
| 207 | if (this->classID() != that.classID()) { |
| 208 | return false; |
| 209 | } |
| 210 | return this->onIsEqual(that); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Helper for down-casting to a GrXPFactory subclass |
| 215 | */ |
| 216 | template <typename T> const T& cast() const { return *static_cast<const T*>(this); } |
| 217 | |
| 218 | uint32_t classID() const { SkASSERT(kIllegalXPFClassID != fClassID); return fClassID; } |
| 219 | |
| 220 | protected: |
| 221 | GrXPFactory() : fClassID(kIllegalXPFClassID) {} |
| 222 | |
| 223 | template <typename XPF_SUBCLASS> void initClassID() { |
| 224 | static uint32_t kClassID = GenClassID(); |
| 225 | fClassID = kClassID; |
| 226 | } |
| 227 | |
| 228 | uint32_t fClassID; |
| 229 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 230 | private: |
egdaniel | 915187b | 2014-12-05 12:58:28 -0800 | [diff] [blame] | 231 | virtual bool onIsEqual(const GrXPFactory&) const = 0; |
| 232 | |
| 233 | static uint32_t GenClassID() { |
| 234 | // fCurrXPFactoryID has been initialized to kIllegalXPFactoryID. The |
| 235 | // atomic inc returns the old value not the incremented value. So we add |
| 236 | // 1 to the returned value. |
| 237 | uint32_t id = static_cast<uint32_t>(sk_atomic_inc(&gCurrXPFClassID)) + 1; |
| 238 | if (!id) { |
| 239 | SkFAIL("This should never wrap as it should only be called once for each GrXPFactory " |
| 240 | "subclass."); |
| 241 | } |
| 242 | return id; |
| 243 | } |
| 244 | |
| 245 | enum { |
| 246 | kIllegalXPFClassID = 0, |
| 247 | }; |
| 248 | static int32_t gCurrXPFClassID; |
| 249 | |
egdaniel | 378092f | 2014-12-03 10:40:13 -0800 | [diff] [blame] | 250 | typedef GrProgramElement INHERITED; |
| 251 | }; |
| 252 | |
| 253 | #endif |
| 254 | |