1 /* 2 * Copyright 2011 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 9 #ifndef GrGLRenderTarget_DEFINED 10 #define GrGLRenderTarget_DEFINED 11 12 #include "include/core/SkScalar.h" 13 #include "include/gpu/GrBackendSurface.h" 14 #include "src/gpu/ganesh/GrRenderTarget.h" 15 #include "src/gpu/ganesh/gl/GrGLDefines_impl.h" 16 17 class GrGLCaps; 18 class GrGLGpu; 19 class GrGLAttachment; 20 21 class GrGLRenderTarget : public GrRenderTarget { 22 public: 23 using GrSurface::glRTFBOIDis0; alwaysClearStencil()24 bool alwaysClearStencil() const override { return this->glRTFBOIDis0(); } 25 26 // set fSingleSampleFBOID to this value to indicate that it is multisampled but 27 // Gr doesn't know how to resolve it. 28 enum { kUnresolvableFBOID = 0 }; 29 30 struct IDs { 31 GrGLuint fMultisampleFBOID; 32 GrBackendObjectOwnership fRTFBOOwnership; 33 GrGLuint fSingleSampleFBOID; 34 GrGLuint fMSColorRenderbufferID; 35 int fTotalMemorySamplesPerPixel; 36 }; 37 38 static sk_sp<GrGLRenderTarget> MakeWrapped(GrGLGpu*, 39 const SkISize&, 40 GrGLFormat, 41 int sampleCount, 42 const IDs&, 43 int stencilBits, 44 std::string_view label); 45 isFBO0(bool multisample)46 bool isFBO0(bool multisample) const { 47 return (multisample ? fMultisampleFBOID : fSingleSampleFBOID) == 0; 48 } 49 isMultisampledRenderToTexture()50 bool isMultisampledRenderToTexture() const { 51 return fMultisampleFBOID != 0 && fMultisampleFBOID == fSingleSampleFBOID; 52 } 53 54 GrBackendRenderTarget getBackendRenderTarget() const override; 55 56 GrBackendFormat backendFormat() const override; 57 58 bool canAttemptStencilAttachment(bool useMultisampleFBO) const override; 59 60 // GrGLRenderTarget overrides dumpMemoryStatistics so it can log its texture and renderbuffer 61 // components separately. 62 void dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const override; 63 format()64 GrGLFormat format() const { return fRTFormat; } 65 hasDynamicMSAAAttachment()66 bool hasDynamicMSAAAttachment() const { return SkToBool(fDynamicMSAAAttachment); } 67 bool ensureDynamicMSAAAttachment(); 68 69 // Binds the render target to GL_FRAMEBUFFER for rendering. bind(bool useMultisampleFBO)70 void bind(bool useMultisampleFBO) { 71 this->bindInternal(GR_GL_FRAMEBUFFER, useMultisampleFBO); 72 } 73 74 // Must be rebound even if this is already the currently bound render target. mustRebind(bool useMultisampleFBO)75 bool mustRebind(bool useMultisampleFBO) const { 76 return fNeedsStencilAttachmentBind[useMultisampleFBO]; 77 } 78 79 // Binds the render target for copying, reading, or clearing pixel values. If we are an MSAA 80 // render target with a separate resolve texture, we bind the multisampled FBO. Otherwise we 81 // bind the single sample FBO. bindForPixelOps(GrGLenum fboTarget)82 void bindForPixelOps(GrGLenum fboTarget) { 83 this->bindInternal(fboTarget, 84 this->numSamples() > 1 && !this->isMultisampledRenderToTexture()); 85 } 86 87 enum class ResolveDirection : bool { 88 kSingleToMSAA, // glCaps.canResolveSingleToMSAA() must be true. 89 kMSAAToSingle 90 }; 91 92 // Binds the multisampled and single sample FBOs, one to GL_DRAW_FRAMEBUFFER and the other to 93 // GL_READ_FRAMEBUFFER, depending on ResolveDirection. 94 void bindForResolve(ResolveDirection); 95 96 protected: 97 // Constructor for subclasses. 98 GrGLRenderTarget(GrGLGpu*, 99 const SkISize&, 100 GrGLFormat, 101 int sampleCount, 102 const IDs&, 103 std::string_view label); 104 105 void init(GrGLFormat, const IDs&); 106 107 // Binds the render target to the given target and ensures its stencil attachment is valid. 108 void bindInternal(GrGLenum fboTarget, bool useMultisampleFBO); 109 110 void onAbandon() override; 111 void onRelease() override; 112 totalMemorySamplesPerPixel()113 int totalMemorySamplesPerPixel() const { return fTotalMemorySamplesPerPixel; } 114 115 private: 116 // Constructor for instances wrapping backend objects. 117 GrGLRenderTarget(GrGLGpu*, 118 const SkISize&, 119 GrGLFormat, 120 int sampleCount, 121 const IDs&, 122 sk_sp<GrGLAttachment> stencil, 123 std::string_view label); 124 125 void setFlags(const GrGLCaps&, const IDs&); 126 127 GrGLGpu* getGLGpu() const; 128 bool completeStencilAttachment(GrAttachment* stencil, bool useMultisampleFBO) override; 129 130 size_t onGpuMemorySize() const override; 131 132 void onSetLabel() override; 133 134 sk_sp<GrGLAttachment> fDynamicMSAAAttachment; 135 136 GrGLuint fMultisampleFBOID; 137 GrGLuint fSingleSampleFBOID; 138 GrGLuint fMSColorRenderbufferID; 139 GrGLFormat fRTFormat; 140 bool fNeedsStencilAttachmentBind[2] = {false, false}; 141 bool fDMSAARenderToTextureFBOIsMultisample = false; 142 143 GrBackendObjectOwnership fRTFBOOwnership; 144 145 // The RenderTarget needs to be able to report its VRAM footprint even after abandon and 146 // release have potentially zeroed out the IDs (e.g., so the cache can reset itself). Since 147 // the IDs are just required for the computation in totalSamples we cache that result here. 148 int fTotalMemorySamplesPerPixel; 149 150 using INHERITED = GrRenderTarget; 151 }; 152 153 #endif 154