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 // Binds the render target for copying, reading, or clearing pixel values. If we are an MSAA 74 // render target with a separate resolve texture, we bind the multisampled FBO. Otherwise we 75 // bind the single sample FBO. bindForPixelOps(GrGLenum fboTarget)76 void bindForPixelOps(GrGLenum fboTarget) { 77 this->bindInternal(fboTarget, 78 this->numSamples() > 1 && !this->isMultisampledRenderToTexture()); 79 } 80 81 enum class ResolveDirection : bool { 82 kSingleToMSAA, // glCaps.canResolveSingleToMSAA() must be true. 83 kMSAAToSingle 84 }; 85 86 // Binds the multisampled and single sample FBOs, one to GL_DRAW_FRAMEBUFFER and the other to 87 // GL_READ_FRAMEBUFFER, depending on ResolveDirection. 88 void bindForResolve(ResolveDirection); 89 90 protected: 91 // Constructor for subclasses. 92 GrGLRenderTarget(GrGLGpu*, 93 const SkISize&, 94 GrGLFormat, 95 int sampleCount, 96 const IDs&); 97 98 void init(GrGLFormat, const IDs&); 99 100 // Binds the render target to the given target and ensures its stencil attachment is valid. 101 void bindInternal(GrGLenum fboTarget, bool useMultisampleFBO); 102 103 void onAbandon() override; 104 void onRelease() override; 105 totalMemorySamplesPerPixel()106 int totalMemorySamplesPerPixel() const { return fTotalMemorySamplesPerPixel; } 107 108 private: 109 // Constructor for instances wrapping backend objects. 110 GrGLRenderTarget( 111 GrGLGpu*, const SkISize&, GrGLFormat, int sampleCount, const IDs&, 112 sk_sp<GrGLAttachment> stencil); 113 114 void setFlags(const GrGLCaps&, const IDs&); 115 116 GrGLGpu* getGLGpu() const; 117 bool completeStencilAttachment(GrAttachment* stencil, bool useMultisampleFBO) override; 118 119 size_t onGpuMemorySize() const override; 120 121 sk_sp<GrGLAttachment> fDynamicMSAAAttachment; 122 123 GrGLuint fMultisampleFBOID; 124 GrGLuint fSingleSampleFBOID; 125 GrGLuint fMSColorRenderbufferID; 126 GrGLFormat fRTFormat; 127 bool fNeedsStencilAttachmentBind[2] = {false, false}; 128 bool fDMSAARenderToTextureFBOIsMultisample = false; 129 130 GrBackendObjectOwnership fRTFBOOwnership; 131 132 // The RenderTarget needs to be able to report its VRAM footprint even after abandon and 133 // release have potentially zeroed out the IDs (e.g., so the cache can reset itself). Since 134 // the IDs are just required for the computation in totalSamples we cache that result here. 135 int fTotalMemorySamplesPerPixel; 136 137 using INHERITED = GrRenderTarget; 138 }; 139 140 #endif 141