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