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 "GrBlend.h" 12 #include "GrColor.h" 13 #include "GrNonAtomicRef.h" 14 #include "GrProcessor.h" 15 #include "GrProcessorAnalysis.h" 16 #include "GrTypes.h" 17 18 class GrGLSLXferProcessor; 19 class GrProcessorSet; 20 class GrShaderCaps; 21 22 /** 23 * Barriers for blending. When a shader reads the dst directly, an Xfer barrier is sometimes 24 * required after a pixel has been written, before it can be safely read again. 25 */ 26 enum GrXferBarrierType { 27 kNone_GrXferBarrierType = 0, //<! No barrier is required 28 kTexture_GrXferBarrierType, //<! Required when a shader reads and renders to the same texture. 29 kBlend_GrXferBarrierType, //<! Required by certain blend extensions. 30 }; 31 /** Should be able to treat kNone as false in boolean expressions */ 32 GR_STATIC_ASSERT(SkToBool(kNone_GrXferBarrierType) == false); 33 34 /** 35 * GrXferProcessor is responsible for implementing the xfer mode that blends the src color and dst 36 * color, and for applying any coverage. It does this by emitting fragment shader code and 37 * controlling the fixed-function blend state. When dual-source blending is available, it may also 38 * write a seconday fragment shader output color. GrXferProcessor has two modes of operation: 39 * 40 * Dst read: When allowed by the backend API, or when supplied a texture of the destination, the 41 * GrXferProcessor may read the destination color. While operating in this mode, the subclass only 42 * provides shader code that blends the src and dst colors, and the base class applies coverage. 43 * 44 * No dst read: When not performing a dst read, the subclass is given full control of the fixed- 45 * function blend state and/or secondary output, and is responsible to apply coverage on its own. 46 * 47 * A GrXferProcessor is never installed directly into our draw state, but instead is created from a 48 * GrXPFactory once we have finalized the state of our draw. 49 */ 50 class GrXferProcessor : public GrProcessor, public GrNonAtomicRef<GrXferProcessor> { 51 public: 52 /** 53 * A texture that contains the dst pixel values and an integer coord offset from device space 54 * to the space of the texture. Depending on GPU capabilities a DstTexture may be used by a 55 * GrXferProcessor for blending in the fragment shader. 56 */ 57 class DstProxy { 58 public: DstProxy()59 DstProxy() { fOffset.set(0, 0); } 60 DstProxy(const DstProxy & other)61 DstProxy(const DstProxy& other) { 62 *this = other; 63 } 64 DstProxy(sk_sp<GrTextureProxy> proxy,const SkIPoint & offset)65 DstProxy(sk_sp<GrTextureProxy> proxy, const SkIPoint& offset) 66 : fProxy(std::move(proxy)) { 67 if (fProxy) { 68 fOffset = offset; 69 } else { 70 fOffset.set(0, 0); 71 } 72 } 73 74 DstProxy& operator=(const DstProxy& other) { 75 fProxy = other.fProxy; 76 fOffset = other.fOffset; 77 return *this; 78 } 79 80 bool operator==(const DstProxy& that) const { 81 return fProxy == that.fProxy && fOffset == that.fOffset; 82 } 83 bool operator!=(const DstProxy& that) const { return !(*this == that); } 84 offset()85 const SkIPoint& offset() const { return fOffset; } 86 setOffset(const SkIPoint & offset)87 void setOffset(const SkIPoint& offset) { fOffset = offset; } setOffset(int ox,int oy)88 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } 89 proxy()90 GrTextureProxy* proxy() const { return fProxy.get(); } 91 setProxy(sk_sp<GrTextureProxy> proxy)92 void setProxy(sk_sp<GrTextureProxy> proxy) { 93 fProxy = std::move(proxy); 94 if (!fProxy) { 95 fOffset = {0, 0}; 96 } 97 } 98 instantiate(GrResourceProvider * resourceProvider)99 bool instantiate(GrResourceProvider* resourceProvider) { 100 return SkToBool(fProxy->instantiate(resourceProvider)); 101 } 102 103 private: 104 sk_sp<GrTextureProxy> fProxy; 105 SkIPoint fOffset; 106 }; 107 108 /** 109 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the 110 * specific subclass's key. 111 */ 112 void getGLSLProcessorKey(const GrShaderCaps&, 113 GrProcessorKeyBuilder*, 114 const GrSurfaceOrigin* originIfDstTexture) const; 115 116 /** Returns a new instance of the appropriate *GL* implementation class 117 for the given GrXferProcessor; caller is responsible for deleting 118 the object. */ 119 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0; 120 121 /** 122 * Returns the barrier type, if any, that this XP will require. Note that the possibility 123 * that a kTexture type barrier is required is handled by the GrPipeline and need not be 124 * considered by subclass overrides of this function. 125 */ xferBarrierType(const GrCaps & caps)126 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const { 127 return kNone_GrXferBarrierType; 128 } 129 130 struct BlendInfo { resetBlendInfo131 void reset() { 132 fEquation = kAdd_GrBlendEquation; 133 fSrcBlend = kOne_GrBlendCoeff; 134 fDstBlend = kZero_GrBlendCoeff; 135 fBlendConstant = 0; 136 fWriteColor = true; 137 } 138 139 SkDEBUGCODE(SkString dump() const;) 140 141 GrBlendEquation fEquation; 142 GrBlendCoeff fSrcBlend; 143 GrBlendCoeff fDstBlend; 144 GrColor fBlendConstant; 145 bool fWriteColor; 146 }; 147 148 void getBlendInfo(BlendInfo* blendInfo) const; 149 willReadDstColor()150 bool willReadDstColor() const { return fWillReadDstColor; } 151 152 /** 153 * If we are performing a dst read, returns whether the base class will use mixed samples to 154 * antialias the shader's final output. If not doing a dst read, the subclass is responsible 155 * for antialiasing and this returns false. 156 */ dstReadUsesMixedSamples()157 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; } 158 159 /** 160 * Returns whether or not this xferProcossor will set a secondary output to be used with dual 161 * source blending. 162 */ 163 bool hasSecondaryOutput() const; 164 isLCD()165 bool isLCD() const { return fIsLCD; } 166 167 /** Returns true if this and other processor conservatively draw identically. It can only return 168 true when the two processor are of the same subclass (i.e. they return the same object from 169 from getFactory()). 170 171 A return value of true from isEqual() should not be used to test whether the processor would 172 generate the same shader code. To test for identical code generation use getGLSLProcessorKey 173 */ 174 isEqual(const GrXferProcessor & that)175 bool isEqual(const GrXferProcessor& that) const { 176 if (this->classID() != that.classID()) { 177 return false; 178 } 179 if (this->fWillReadDstColor != that.fWillReadDstColor) { 180 return false; 181 } 182 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) { 183 return false; 184 } 185 if (fIsLCD != that.fIsLCD) { 186 return false; 187 } 188 return this->onIsEqual(that); 189 } 190 191 protected: 192 GrXferProcessor(); 193 GrXferProcessor(bool willReadDstColor, bool hasMixedSamples, GrProcessorAnalysisCoverage); 194 195 private: 196 /** 197 * Sets a unique key on the GrProcessorKeyBuilder that is directly associated with this xfer 198 * processor's GL backend implementation. 199 */ 200 virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0; 201 202 /** 203 * If we are not performing a dst read, returns whether the subclass will set a secondary 204 * output. When using dst reads, the base class controls the secondary output and this method 205 * will not be called. 206 */ onHasSecondaryOutput()207 virtual bool onHasSecondaryOutput() const { return false; } 208 209 /** 210 * If we are not performing a dst read, retrieves the fixed-function blend state required by the 211 * subclass. When using dst reads, the base class controls the fixed-function blend state and 212 * this method will not be called. The BlendInfo struct comes initialized to "no blending". 213 */ onGetBlendInfo(BlendInfo *)214 virtual void onGetBlendInfo(BlendInfo*) const {} 215 216 virtual bool onIsEqual(const GrXferProcessor&) const = 0; 217 218 bool fWillReadDstColor; 219 bool fDstReadUsesMixedSamples; 220 bool fIsLCD; 221 222 typedef GrFragmentProcessor INHERITED; 223 }; 224 225 /** 226 * We install a GrXPFactory (XPF) early on in the pipeline before all the final draw information is 227 * known (e.g. whether there is fractional pixel coverage, will coverage be 1 or 4 channel, is the 228 * draw opaque, etc.). Once the state of the draw is finalized, we use the XPF along with all the 229 * draw information to create a GrXferProcessor (XP) which can implement the desired blending for 230 * the draw. 231 * 232 * Before the XP is created, the XPF is able to answer queries about what functionality the XPs it 233 * creates will have. For example, can it create an XP that supports RGB coverage or will the XP 234 * blend with the destination color. 235 * 236 * GrXPFactories are intended to be static immutable objects. We pass them around as raw pointers 237 * and expect the pointers to always be valid and for the factories to be reusable and thread safe. 238 * Equality is tested for using pointer comparison. GrXPFactory destructors must be no-ops. 239 */ 240 241 // In order to construct GrXPFactory subclass instances as constexpr the subclass, and therefore 242 // GrXPFactory, must be a literal type. One requirement is having a trivial destructor. This is ok 243 // since these objects have no need for destructors. However, GCC and clang throw a warning when a 244 // class has virtual functions and a non-virtual destructor. We suppress that warning here and 245 // for the subclasses. 246 #if defined(__GNUC__) || defined(__clang) 247 #pragma GCC diagnostic push 248 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" 249 #endif 250 class GrXPFactory { 251 public: 252 typedef GrXferProcessor::DstProxy DstProxy; 253 254 enum class AnalysisProperties : unsigned { 255 kNone = 0x0, 256 /** 257 * The fragment shader will require the destination color. 258 */ 259 kReadsDstInShader = 0x1, 260 /** 261 * The op may apply coverage as alpha and still blend correctly. 262 */ 263 kCompatibleWithAlphaAsCoverage = 0x2, 264 /** 265 * The color input to the GrXferProcessor will be ignored. 266 */ 267 kIgnoresInputColor = 0x4, 268 /** 269 * If set overlapping stencil and cover operations can be replaced by a combined stencil 270 * followed by a combined cover. 271 */ 272 kCanCombineOverlappedStencilAndCover = 0x8, 273 /** 274 * The destination color will be provided to the fragment processor using a texture. This is 275 * additional information about the implementation of kReadsDstInShader. 276 */ 277 kRequiresDstTexture = 0x10, 278 /** 279 * If set overlapping draws may not be combined because a barrier must be inserted between 280 * them. 281 */ 282 kRequiresBarrierBetweenOverlappingDraws = 0x20, 283 }; 284 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties); 285 286 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*, 287 const GrProcessorAnalysisColor&, 288 GrProcessorAnalysisCoverage, 289 bool hasMixedSamples, 290 const GrCaps& caps); 291 292 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*, 293 const GrProcessorAnalysisColor&, 294 const GrProcessorAnalysisCoverage&, 295 const GrCaps&); 296 297 protected: GrXPFactory()298 constexpr GrXPFactory() {} 299 300 private: 301 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&, 302 GrProcessorAnalysisCoverage, 303 bool hasMixedSamples, 304 const GrCaps&) const = 0; 305 306 /** 307 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be 308 * inferred by the base class based on kReadsDstInShader and the caps. 309 */ 310 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&, 311 const GrProcessorAnalysisCoverage&, 312 const GrCaps&) const = 0; 313 }; 314 #if defined(__GNUC__) || defined(__clang) 315 #pragma GCC diagnostic pop 316 #endif 317 318 GR_MAKE_BITFIELD_CLASS_OPS(GrXPFactory::AnalysisProperties); 319 320 #endif 321