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 #include "src/gpu/ganesh/gl/GrGLRenderTarget.h"
9
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/ganesh/GrBackendUtils.h"
13 #include "src/gpu/ganesh/GrDirectContextPriv.h"
14 #include "src/gpu/ganesh/GrGpuResourcePriv.h"
15 #include "src/gpu/ganesh/GrResourceProvider.h"
16 #include "src/gpu/ganesh/gl/GrGLGpu.h"
17 #include "src/gpu/ganesh/gl/GrGLUtil.h"
18
19 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
20 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
21 #define GL_CALL_RET(RET, X) GR_GL_CALL_RET(GPUGL->glInterface(), RET, X)
22
23 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
24 // Constructor for wrapped render targets.
GrGLRenderTarget(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & ids,sk_sp<GrGLAttachment> stencil,std::string_view label)25 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
26 const SkISize& dimensions,
27 GrGLFormat format,
28 int sampleCount,
29 const IDs& ids,
30 sk_sp<GrGLAttachment> stencil,
31 std::string_view label)
32 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
33 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo, label, std::move(stencil)) {
34 this->init(format, ids);
35 this->setFlags(gpu->glCaps(), ids);
36 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
37 }
38
GrGLRenderTarget(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & ids,std::string_view label)39 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
40 const SkISize& dimensions,
41 GrGLFormat format,
42 int sampleCount,
43 const IDs& ids,
44 std::string_view label)
45 : GrSurface(gpu, dimensions, GrProtected::kNo, label)
46 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo, label) {
47 this->init(format, ids);
48 this->setFlags(gpu->glCaps(), ids);
49 }
50
setFlags(const GrGLCaps & glCaps,const IDs & idDesc)51 inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDs& idDesc) {
52 if ((fMultisampleFBOID | fSingleSampleFBOID) == 0) {
53 this->setGLRTFBOIDIs0();
54 }
55 }
56
init(GrGLFormat format,const IDs & idDesc)57 void GrGLRenderTarget::init(GrGLFormat format, const IDs& idDesc) {
58 fMultisampleFBOID = idDesc.fMultisampleFBOID;
59 fSingleSampleFBOID = idDesc.fSingleSampleFBOID;
60 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
61 fRTFBOOwnership = idDesc.fRTFBOOwnership;
62 fRTFormat = format;
63 fTotalMemorySamplesPerPixel = idDesc.fTotalMemorySamplesPerPixel;
64 }
65
stencil_bits_to_format(int stencilBits)66 GrGLFormat stencil_bits_to_format(int stencilBits) {
67 SkASSERT(stencilBits);
68 switch (stencilBits) {
69 case 8:
70 // We pick the packed format here so when we query total size we are at least not
71 // underestimating the total size of the stencil buffer. However, in reality this
72 // rarely matters since we usually don't care about the size of wrapped objects.
73 return GrGLFormat::kDEPTH24_STENCIL8;
74 case 16:
75 return GrGLFormat::kSTENCIL_INDEX16;
76 default:
77 SkASSERT(false);
78 return GrGLFormat::kUnknown;
79 }
80 }
81
MakeWrapped(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & idDesc,int stencilBits,std::string_view label)82 sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
83 const SkISize& dimensions,
84 GrGLFormat format,
85 int sampleCount,
86 const IDs& idDesc,
87 int stencilBits,
88 std::string_view label) {
89 sk_sp<GrGLAttachment> sb;
90 if (stencilBits) {
91 // We pick a "fake" actual format that matches the number of stencil bits. When wrapping
92 // an FBO with some number of stencil bits all we care about in the future is that we have
93 // a format with the same number of stencil bits. We don't even directly use the format or
94 // any other properties. Thus it is fine for us to just assign an arbitrary format that
95 // matches the stencil bit count.
96 GrGLFormat sFmt = stencil_bits_to_format(stencilBits);
97
98 // We don't have the actual renderbufferID but we need to make an attachment for the stencil
99 // so we just set it to an invalid value of 0 to make sure we don't explicitly use it or try
100 // and delete it.
101 sb = GrGLAttachment::MakeWrappedRenderBuffer(gpu,
102 /*renderbufferID=*/0,
103 dimensions,
104 GrAttachment::UsageFlags::kStencilAttachment,
105 sampleCount,
106 sFmt);
107 }
108 return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(
109 gpu, dimensions, format, sampleCount, idDesc, std::move(sb), label));
110 }
111
getBackendRenderTarget() const112 GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
113 bool useMultisampleFBO = (this->numSamples() > 1);
114 GrGLFramebufferInfo fbi;
115 fbi.fFBOID = (useMultisampleFBO) ? fMultisampleFBOID : fSingleSampleFBOID;
116 fbi.fFormat = GrGLFormatToEnum(this->format());
117 int numStencilBits = 0;
118 if (GrAttachment* stencil = this->getStencilAttachment(useMultisampleFBO)) {
119 numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat());
120 }
121
122 return GrBackendRenderTarget(
123 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
124 }
125
backendFormat() const126 GrBackendFormat GrGLRenderTarget::backendFormat() const {
127 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
128 // texture 2D.
129 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D);
130 }
131
onGpuMemorySize() const132 size_t GrGLRenderTarget::onGpuMemorySize() const {
133 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
134 fTotalMemorySamplesPerPixel, GrMipmapped::kNo);
135 }
136
onSetLabel()137 void GrGLRenderTarget::onSetLabel() {
138 SkASSERT(fMSColorRenderbufferID);
139 SkASSERT(fRTFBOOwnership == GrBackendObjectOwnership::kOwned);
140 }
141
completeStencilAttachment(GrAttachment * stencil,bool useMultisampleFBO)142 bool GrGLRenderTarget::completeStencilAttachment(GrAttachment* stencil, bool useMultisampleFBO) {
143 // We defer attaching the new stencil buffer until the next time our framebuffer is bound.
144 if (this->getStencilAttachment(useMultisampleFBO) != stencil) {
145 fNeedsStencilAttachmentBind[useMultisampleFBO] = true;
146 }
147 return true;
148 }
149
ensureDynamicMSAAAttachment()150 bool GrGLRenderTarget::ensureDynamicMSAAAttachment() {
151 SkASSERT(this->numSamples() == 1);
152 if (fMultisampleFBOID) {
153 return true;
154 }
155 SkASSERT(!fDynamicMSAAAttachment);
156
157 GrResourceProvider* resourceProvider = this->getContext()->priv().resourceProvider();
158 const GrCaps& caps = *this->getGpu()->caps();
159
160 int internalSampleCount = caps.internalMultisampleCount(this->backendFormat());
161 if (internalSampleCount <= 1) {
162 return false;
163 }
164
165 if (resourceProvider->caps()->msaaResolvesAutomatically() && this->asTexture()) {
166 // We can use EXT_multisampled_render_to_texture for MSAA. We will configure the FBO as MSAA
167 // or not during bindFBO().
168 fMultisampleFBOID = fSingleSampleFBOID;
169 return true;
170 }
171
172 GL_CALL(GenFramebuffers(1, &fMultisampleFBOID));
173 if (!fMultisampleFBOID) {
174 return false;
175 }
176
177 this->getGLGpu()->bindFramebuffer(GR_GL_FRAMEBUFFER, fMultisampleFBOID);
178
179 fDynamicMSAAAttachment.reset(
180 static_cast<GrGLAttachment*>(resourceProvider->getDiscardableMSAAAttachment(
181 this->dimensions(), this->backendFormat(), internalSampleCount,
182 GrProtected(this->isProtected()), GrMemoryless::kNo).release()));
183 if (!fDynamicMSAAAttachment) {
184 return false;
185 }
186
187 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER,
188 fDynamicMSAAAttachment->renderbufferID()));
189 return true;
190 }
191
bindInternal(GrGLenum fboTarget,bool useMultisampleFBO)192 void GrGLRenderTarget::bindInternal(GrGLenum fboTarget, bool useMultisampleFBO) {
193 GrGLuint fboId = useMultisampleFBO ? fMultisampleFBOID : fSingleSampleFBOID;
194 this->getGLGpu()->bindFramebuffer(fboTarget, fboId);
195
196 if (fSingleSampleFBOID != 0 &&
197 fSingleSampleFBOID == fMultisampleFBOID &&
198 useMultisampleFBO != fDMSAARenderToTextureFBOIsMultisample) {
199 auto* glTex = static_cast<GrGLTexture*>(this->asTexture());
200 if (this->getGLGpu()->glCaps().bindTexture0WhenChangingTextureFBOMultisampleCount()) {
201 GL_CALL(FramebufferTexture2D(fboTarget,
202 GR_GL_COLOR_ATTACHMENT0,
203 GR_GL_TEXTURE_2D,
204 0 /*texture*/,
205 0 /*mipMapLevel*/));
206 }
207 if (useMultisampleFBO) {
208 int sampleCount = this->numSamples() > 1 ?
209 this->numSamples() :
210 this->getGpu()->caps()->internalMultisampleCount(this->backendFormat());
211 GL_CALL(FramebufferTexture2DMultisample(fboTarget,
212 GR_GL_COLOR_ATTACHMENT0,
213 glTex->target(),
214 glTex->textureID(),
215 0 /*mipMapLevel*/,
216 sampleCount));
217 } else {
218 GL_CALL(FramebufferTexture2D(fboTarget,
219 GR_GL_COLOR_ATTACHMENT0,
220 glTex->target(),
221 glTex->textureID(),
222 0 /*mipMapLevel*/));
223 }
224 fDMSAARenderToTextureFBOIsMultisample = useMultisampleFBO;
225 fNeedsStencilAttachmentBind[useMultisampleFBO] = true;
226 }
227
228 // Make sure the stencil attachment is valid. Even though a color buffer op doesn't use stencil,
229 // our FBO still needs to be "framebuffer complete".
230 if (fNeedsStencilAttachmentBind[useMultisampleFBO]) {
231 if (auto stencil = this->getStencilAttachment(useMultisampleFBO)) {
232 const GrGLAttachment* glStencil = static_cast<const GrGLAttachment*>(stencil);
233 GL_CALL(FramebufferRenderbuffer(fboTarget,
234 GR_GL_STENCIL_ATTACHMENT,
235 GR_GL_RENDERBUFFER,
236 glStencil->renderbufferID()));
237 if (GrGLFormatIsPackedDepthStencil(glStencil->format())) {
238 GL_CALL(FramebufferRenderbuffer(fboTarget,
239 GR_GL_DEPTH_ATTACHMENT,
240 GR_GL_RENDERBUFFER,
241 glStencil->renderbufferID()));
242 } else {
243 GL_CALL(FramebufferRenderbuffer(fboTarget,
244 GR_GL_DEPTH_ATTACHMENT,
245 GR_GL_RENDERBUFFER,
246 0));
247 }
248 } else {
249 GL_CALL(FramebufferRenderbuffer(fboTarget,
250 GR_GL_STENCIL_ATTACHMENT,
251 GR_GL_RENDERBUFFER,
252 0));
253 GL_CALL(FramebufferRenderbuffer(fboTarget,
254 GR_GL_DEPTH_ATTACHMENT,
255 GR_GL_RENDERBUFFER,
256 0));
257 }
258 #ifdef SK_DEBUG
259 if (!this->getGLGpu()->glCaps().skipErrorChecks() &&
260 !this->getGLGpu()->glCaps().rebindColorAttachmentAfterCheckFramebufferStatus()) {
261 GrGLenum status;
262 GL_CALL_RET(status, CheckFramebufferStatus(fboTarget));
263 if (status != GR_GL_FRAMEBUFFER_COMPLETE) {
264 // This can fail if the context has been asynchronously abandoned (see
265 // skbug.com/5200).
266 SkDebugf("WARNING: failed to attach stencil.\n");
267 }
268 }
269 #endif
270 fNeedsStencilAttachmentBind[useMultisampleFBO] = false;
271 }
272 }
273
bindForResolve(GrGLGpu::ResolveDirection resolveDirection)274 void GrGLRenderTarget::bindForResolve(GrGLGpu::ResolveDirection resolveDirection) {
275 // If the multisample FBO is nonzero, it means we always have something to resolve (even if the
276 // single sample buffer is FBO 0). If it's zero, then there's nothing to resolve.
277 SkASSERT(fMultisampleFBOID != 0);
278
279 // In the EXT_multisampled_render_to_texture case, we shouldn't be resolving anything.
280 SkASSERT(!this->isMultisampledRenderToTexture());
281
282 if (resolveDirection == GrGLGpu::ResolveDirection::kMSAAToSingle) {
283 this->bindInternal(GR_GL_READ_FRAMEBUFFER, true);
284 this->bindInternal(GR_GL_DRAW_FRAMEBUFFER, false);
285 } else {
286 SkASSERT(resolveDirection == GrGLGpu::ResolveDirection::kSingleToMSAA);
287 SkASSERT(this->getGLGpu()->glCaps().canResolveSingleToMSAA());
288 this->bindInternal(GR_GL_READ_FRAMEBUFFER, false);
289 this->bindInternal(GR_GL_DRAW_FRAMEBUFFER, true);
290 }
291 }
292
onRelease()293 void GrGLRenderTarget::onRelease() {
294 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
295 GrGLGpu* gpu = this->getGLGpu();
296 if (fSingleSampleFBOID) {
297 gpu->deleteFramebuffer(fSingleSampleFBOID);
298 }
299 if (fMultisampleFBOID && fMultisampleFBOID != fSingleSampleFBOID) {
300 gpu->deleteFramebuffer(fMultisampleFBOID);
301 }
302 if (fMSColorRenderbufferID) {
303 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
304 }
305 }
306 fMultisampleFBOID = 0;
307 fSingleSampleFBOID = 0;
308 fMSColorRenderbufferID = 0;
309 INHERITED::onRelease();
310 }
311
onAbandon()312 void GrGLRenderTarget::onAbandon() {
313 fMultisampleFBOID = 0;
314 fSingleSampleFBOID = 0;
315 fMSColorRenderbufferID = 0;
316 INHERITED::onAbandon();
317 }
318
getGLGpu() const319 GrGLGpu* GrGLRenderTarget::getGLGpu() const {
320 SkASSERT(!this->wasDestroyed());
321 return static_cast<GrGLGpu*>(this->getGpu());
322 }
323
canAttemptStencilAttachment(bool useMultisampleFBO) const324 bool GrGLRenderTarget::canAttemptStencilAttachment(bool useMultisampleFBO) const {
325 // This cap should have been handled at a higher level.
326 SkASSERT(!this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers());
327 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
328 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
329 // Skia created it.
330 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned ||
331 // The dmsaa attachment is always owned and always supports adding stencil.
332 (this->numSamples() == 1 && useMultisampleFBO);
333 }
334
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const335 void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
336 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
337 // which is multiply inherited from both ourselves and a texture. In these cases, one part
338 // (texture, rt) may be wrapped, while the other is owned by Skia.
339 bool refsWrappedRenderTargetObjects =
340 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
341 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
342 return;
343 }
344
345 int numSamplesNotInTexture = fTotalMemorySamplesPerPixel;
346 if (this->asTexture()) {
347 --numSamplesNotInTexture; // GrGLTexture::dumpMemoryStatistics accounts for 1 sample.
348 }
349 if (numSamplesNotInTexture >= 1) {
350 size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
351 numSamplesNotInTexture, GrMipmapped::kNo);
352
353 // Due to this resource having both a texture and a renderbuffer component, dump as
354 // skia/gpu_resources/resource_#/renderbuffer
355 SkString resourceName = this->getResourceName();
356 resourceName.append("/renderbuffer");
357
358 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
359
360 SkString renderbuffer_id;
361 renderbuffer_id.appendU32(fMSColorRenderbufferID);
362 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
363 renderbuffer_id.c_str());
364 }
365 }
366