1 /* 2 * Copyright 2016 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 GrRenderTargetProxy_DEFINED 9 #define GrRenderTargetProxy_DEFINED 10 11 #include "GrSurfaceProxy.h" 12 #include "GrTypesPriv.h" 13 14 class GrResourceProvider; 15 class GrRenderTargetProxyPriv; 16 17 // This class delays the acquisition of RenderTargets until they are actually 18 // required 19 // Beware: the uniqueID of the RenderTargetProxy will usually be different than 20 // the uniqueID of the RenderTarget it represents! 21 class GrRenderTargetProxy : virtual public GrSurfaceProxy { 22 public: asRenderTargetProxy()23 GrRenderTargetProxy* asRenderTargetProxy() override { return this; } asRenderTargetProxy()24 const GrRenderTargetProxy* asRenderTargetProxy() const override { return this; } 25 26 // Actually instantiate the backing rendertarget, if necessary. 27 bool instantiate(GrResourceProvider*) override; 28 fsaaType()29 GrFSAAType fsaaType() const { 30 if (fSampleCnt <= 1) { 31 SkASSERT(!this->hasMixedSamples()); 32 return GrFSAAType::kNone; 33 } 34 return this->hasMixedSamples() ? GrFSAAType::kMixedSamples : GrFSAAType::kUnifiedMSAA; 35 } 36 37 /* 38 * When instantiated does this proxy require a stencil buffer? 39 */ setNeedsStencil()40 void setNeedsStencil() { fNeedsStencil = true; } needsStencil()41 bool needsStencil() const { return fNeedsStencil; } 42 43 /** 44 * Returns the number of samples/pixel in the stencil buffer (One if non-MSAA). 45 */ numStencilSamples()46 int numStencilSamples() const { return fSampleCnt; } 47 48 /** 49 * Returns the number of samples/pixel in the color buffer (One if non-MSAA or mixed sampled). 50 */ numColorSamples()51 int numColorSamples() const { 52 return GrFSAAType::kMixedSamples == this->fsaaType() ? 1 : fSampleCnt; 53 } 54 55 int maxWindowRectangles(const GrCaps& caps) const; 56 wrapsVkSecondaryCB()57 bool wrapsVkSecondaryCB() const { return fWrapsVkSecondaryCB == WrapsVkSecondaryCB::kYes; } 58 59 // TODO: move this to a priv class! 60 bool refsWrappedObjects() const; 61 62 // Provides access to special purpose functions. 63 GrRenderTargetProxyPriv rtPriv(); 64 const GrRenderTargetProxyPriv rtPriv() const; 65 66 protected: 67 friend class GrProxyProvider; // for ctors 68 friend class GrRenderTargetProxyPriv; 69 70 // Deferred version 71 GrRenderTargetProxy(const GrCaps&, const GrBackendFormat&, const GrSurfaceDesc&, 72 GrSurfaceOrigin, SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); 73 74 // Lazy-callback version 75 // There are two main use cases for lazily-instantiated proxies: 76 // basic knowledge - width, height, config, samples, origin are known 77 // minimal knowledge - only config is known. 78 // 79 // The basic knowledge version is used for DDL where we know the type of proxy we are going to 80 // use, but we don't have access to the GPU yet to instantiate it. 81 // 82 // The minimal knowledge version is used for CCPR where we are generating an atlas but we do not 83 // know the final size until flush time. 84 GrRenderTargetProxy(LazyInstantiateCallback&&, LazyInstantiationType lazyType, 85 const GrBackendFormat&, const GrSurfaceDesc&, GrSurfaceOrigin, 86 SkBackingFit, SkBudgeted, GrInternalSurfaceFlags); 87 88 // Wrapped version 89 enum class WrapsVkSecondaryCB : bool { kNo = false, kYes = true }; 90 GrRenderTargetProxy(sk_sp<GrSurface>, GrSurfaceOrigin, 91 WrapsVkSecondaryCB wrapsVkSecondaryCB = WrapsVkSecondaryCB::kNo); 92 93 sk_sp<GrSurface> createSurface(GrResourceProvider*) const override; 94 95 private: setHasMixedSamples()96 void setHasMixedSamples() { 97 fSurfaceFlags |= GrInternalSurfaceFlags::kMixedSampled; 98 } hasMixedSamples()99 bool hasMixedSamples() const { return fSurfaceFlags & GrInternalSurfaceFlags::kMixedSampled; } 100 setGLRTFBOIDIs0()101 void setGLRTFBOIDIs0() { 102 fSurfaceFlags |= GrInternalSurfaceFlags::kGLRTFBOIDIs0; 103 } glRTFBOIDIs0()104 bool glRTFBOIDIs0() const { 105 return fSurfaceFlags & GrInternalSurfaceFlags::kGLRTFBOIDIs0; 106 } 107 108 109 size_t onUninstantiatedGpuMemorySize() const override; 110 SkDEBUGCODE(void onValidateSurface(const GrSurface*) override;) 111 112 // WARNING: Be careful when adding or removing fields here. ASAN is likely to trigger warnings 113 // when instantiating GrTextureRenderTargetProxy. The std::function in GrSurfaceProxy makes 114 // each class in the diamond require 16 byte alignment. Clang appears to layout the fields for 115 // each class to achieve the necessary alignment. However, ASAN checks the alignment of 'this' 116 // in the constructors, and always looks for the full 16 byte alignment, even if the fields in 117 // that particular class don't require it. Changing the size of this object can move the start 118 // address of other types, leading to this problem. 119 120 int fSampleCnt; 121 bool fNeedsStencil; 122 WrapsVkSecondaryCB fWrapsVkSecondaryCB; 123 124 // For wrapped render targets the actual GrRenderTarget is stored in the GrIORefProxy class. 125 // For deferred proxies that pointer is filled in when we need to instantiate the 126 // deferred resource. 127 128 typedef GrSurfaceProxy INHERITED; 129 }; 130 131 #endif 132