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