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