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