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 "GrNonAtomicRef.h" 13 #include "GrProcessor.h" 14 #include "GrProcessorAnalysis.h" 15 #include "GrTypes.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: 58 DstProxy() { fOffset.set(0, 0); } 59 60 DstProxy(const DstProxy& other) { 61 *this = other; 62 } 63 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 84 const SkIPoint& offset() const { return fOffset; } 85 86 void setOffset(const SkIPoint& offset) { fOffset = offset; } 87 void setOffset(int ox, int oy) { fOffset.set(ox, oy); } 88 89 GrTextureProxy* proxy() const { return fProxy.get(); } 90 91 void setProxy(sk_sp<GrTextureProxy> proxy) { 92 fProxy = std::move(proxy); 93 if (!fProxy) { 94 fOffset = {0, 0}; 95 } 96 } 97 98 bool instantiate(GrResourceProvider* resourceProvider) { 99 return SkToBool(fProxy->instantiate(resourceProvider)); 100 } 101 102 private: 103 sk_sp<GrTextureProxy> fProxy; 104 SkIPoint fOffset; 105 }; 106 107 /** 108 * Sets a unique key on the GrProcessorKeyBuilder calls onGetGLSLProcessorKey(...) to get the 109 * specific subclass's key. 110 */ 111 void getGLSLProcessorKey(const GrShaderCaps&, 112 GrProcessorKeyBuilder*, 113 const GrSurfaceOrigin* originIfDstTexture) const; 114 115 /** Returns a new instance of the appropriate *GL* implementation class 116 for the given GrXferProcessor; caller is responsible for deleting 117 the object. */ 118 virtual GrGLSLXferProcessor* createGLSLInstance() const = 0; 119 120 /** 121 * Returns the barrier type, if any, that this XP will require. Note that the possibility 122 * that a kTexture type barrier is required is handled by the GrPipeline and need not be 123 * considered by subclass overrides of this function. 124 */ 125 virtual GrXferBarrierType xferBarrierType(const GrCaps& caps) const { 126 return kNone_GrXferBarrierType; 127 } 128 129 struct BlendInfo { 130 void reset() { 131 fEquation = kAdd_GrBlendEquation; 132 fSrcBlend = kOne_GrBlendCoeff; 133 fDstBlend = kZero_GrBlendCoeff; 134 fBlendConstant = SK_PMColor4fTRANSPARENT; 135 fWriteColor = true; 136 } 137 138 SkDEBUGCODE(SkString dump() const;) 139 140 GrBlendEquation fEquation; 141 GrBlendCoeff fSrcBlend; 142 GrBlendCoeff fDstBlend; 143 SkPMColor4f fBlendConstant; 144 bool fWriteColor; 145 }; 146 147 void getBlendInfo(BlendInfo* blendInfo) const; 148 149 bool willReadDstColor() const { return fWillReadDstColor; } 150 151 /** 152 * If we are performing a dst read, returns whether the base class will use mixed samples to 153 * antialias the shader's final output. If not doing a dst read, the subclass is responsible 154 * for antialiasing and this returns false. 155 */ 156 bool dstReadUsesMixedSamples() const { return fDstReadUsesMixedSamples; } 157 158 /** 159 * Returns whether or not this xferProcossor will set a secondary output to be used with dual 160 * source blending. 161 */ 162 bool hasSecondaryOutput() const; 163 164 bool isLCD() const { return fIsLCD; } 165 166 /** Returns true if this and other processor conservatively draw identically. It can only return 167 true when the two processor are of the same subclass (i.e. they return the same object from 168 from getFactory()). 169 170 A return value of true from isEqual() should not be used to test whether the processor would 171 generate the same shader code. To test for identical code generation use getGLSLProcessorKey 172 */ 173 174 bool isEqual(const GrXferProcessor& that) const { 175 if (this->classID() != that.classID()) { 176 return false; 177 } 178 if (this->fWillReadDstColor != that.fWillReadDstColor) { 179 return false; 180 } 181 if (this->fDstReadUsesMixedSamples != that.fDstReadUsesMixedSamples) { 182 return false; 183 } 184 if (fIsLCD != that.fIsLCD) { 185 return false; 186 } 187 return this->onIsEqual(that); 188 } 189 190 protected: 191 GrXferProcessor(ClassID classID); 192 GrXferProcessor(ClassID classID, bool willReadDstColor, bool hasMixedSamples, 193 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 */ 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 */ 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 GrProcessor 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__) 247 #pragma GCC diagnostic push 248 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor" 249 #endif 250 #if defined(__clang__) 251 #pragma clang diagnostic push 252 #pragma clang diagnostic ignored "-Wnon-virtual-dtor" 253 #endif 254 class GrXPFactory { 255 public: 256 typedef GrXferProcessor::DstProxy DstProxy; 257 258 enum class AnalysisProperties : unsigned { 259 kNone = 0x0, 260 /** 261 * The fragment shader will require the destination color. 262 */ 263 kReadsDstInShader = 0x1, 264 /** 265 * The op may apply coverage as alpha and still blend correctly. 266 */ 267 kCompatibleWithAlphaAsCoverage = 0x2, 268 /** 269 * The color input to the GrXferProcessor will be ignored. 270 */ 271 kIgnoresInputColor = 0x4, 272 /** 273 * The destination color will be provided to the fragment processor using a texture. This is 274 * additional information about the implementation of kReadsDstInShader. 275 */ 276 kRequiresDstTexture = 0x10, 277 /** 278 * If set, each pixel can only be touched once during a draw (e.g., because we have a dst 279 * texture or because we need an xfer barrier). 280 */ 281 kRequiresNonOverlappingDraws = 0x20, 282 }; 283 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AnalysisProperties); 284 285 static sk_sp<const GrXferProcessor> MakeXferProcessor(const GrXPFactory*, 286 const GrProcessorAnalysisColor&, 287 GrProcessorAnalysisCoverage, 288 bool hasMixedSamples, 289 const GrCaps& caps); 290 291 static AnalysisProperties GetAnalysisProperties(const GrXPFactory*, 292 const GrProcessorAnalysisColor&, 293 const GrProcessorAnalysisCoverage&, 294 const GrCaps&); 295 296 protected: 297 constexpr GrXPFactory() {} 298 299 private: 300 virtual sk_sp<const GrXferProcessor> makeXferProcessor(const GrProcessorAnalysisColor&, 301 GrProcessorAnalysisCoverage, 302 bool hasMixedSamples, 303 const GrCaps&) const = 0; 304 305 /** 306 * Subclass analysis implementation. This should not return kNeedsDstInTexture as that will be 307 * inferred by the base class based on kReadsDstInShader and the caps. 308 */ 309 virtual AnalysisProperties analysisProperties(const GrProcessorAnalysisColor&, 310 const GrProcessorAnalysisCoverage&, 311 const GrCaps&) 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