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