blob: a7784e9c82f26a7d5904fbd9846de6949bccc2cd [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTraceMemoryDump.h"
9#include "include/gpu/GrContext.h"
10#include "src/gpu/GrContextPriv.h"
11#include "src/gpu/GrGpuResourcePriv.h"
12#include "src/gpu/GrRenderTargetPriv.h"
13#include "src/gpu/gl/GrGLGpu.h"
14#include "src/gpu/gl/GrGLRenderTarget.h"
15#include "src/gpu/gl/GrGLUtil.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000016
egdanield803f272015-03-18 13:01:52 -070017#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
18#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000019
bsalomon37dd3312014-11-03 08:47:23 -080020// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070021// Constructor for wrapped render targets.
egdanielec00d942015-09-14 12:56:10 -070022GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
23 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040024 int sampleCount,
Greg Daniel4065d452018-11-16 15:43:41 -050025 GrGLenum format,
egdanielec00d942015-09-14 12:56:10 -070026 const IDDesc& idDesc,
27 GrGLStencilAttachment* stencil)
Brian Salomona9c22572019-08-05 12:57:09 -040028 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, GrProtected::kNo)
29 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCount, GrProtected::kNo,
30 stencil) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040031 this->setFlags(gpu->glCaps(), idDesc);
Greg Daniel4065d452018-11-16 15:43:41 -050032 this->init(desc, format, idDesc);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050033 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000034}
35
Brian Salomon27b4d8d2019-07-22 14:23:45 -040036GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, int sampleCount,
37 GrGLenum format, const IDDesc& idDesc)
Brian Salomona9c22572019-08-05 12:57:09 -040038 : GrSurface(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, GrProtected::kNo)
39 , INHERITED(gpu, {desc.fWidth, desc.fHeight}, desc.fConfig, sampleCount, GrProtected::kNo) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040040 this->setFlags(gpu->glCaps(), idDesc);
Greg Daniel4065d452018-11-16 15:43:41 -050041 this->init(desc, format, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000042}
43
Robert Phillipsfe0253f2018-03-16 16:47:25 -040044inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDDesc& idDesc) {
Greg Daniela070ed72018-04-26 16:31:38 -040045 if (!idDesc.fRTFBOID) {
46 this->setGLRTFBOIDIs0();
47 }
csmartdaltonf9635992016-08-10 11:09:07 -070048}
49
Greg Daniel4065d452018-11-16 15:43:41 -050050void GrGLRenderTarget::init(const GrSurfaceDesc& desc, GrGLenum format, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080051 fRTFBOID = idDesc.fRTFBOID;
52 fTexFBOID = idDesc.fTexFBOID;
53 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070054 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080055
Greg Daniel4065d452018-11-16 15:43:41 -050056 fRTFormat = format;
57
Robert Phillips29e52f12016-11-03 10:19:14 -040058 fNumSamplesOwnedPerPixel = this->totalSamples();
senorblancod2981212015-04-30 12:06:10 -070059}
60
bungeman6bd52842016-10-27 09:30:08 -070061sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
62 const GrSurfaceDesc& desc,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040063 int sampleCount,
Greg Daniel4065d452018-11-16 15:43:41 -050064 GrGLenum format,
bungeman6bd52842016-10-27 09:30:08 -070065 const IDDesc& idDesc,
66 int stencilBits) {
egdanielec00d942015-09-14 12:56:10 -070067 GrGLStencilAttachment* sb = nullptr;
68 if (stencilBits) {
69 GrGLStencilAttachment::IDDesc sbDesc;
70 GrGLStencilAttachment::Format format;
71 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
72 format.fPacked = false;
73 format.fStencilBits = stencilBits;
74 format.fTotalBits = stencilBits;
Robert Phillipscb2e2352017-08-30 16:44:40 -040075 // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
Brian Salomon27b4d8d2019-07-22 14:23:45 -040076 sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight, sampleCount, format);
egdanielec00d942015-09-14 12:56:10 -070077 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -040078 return sk_sp<GrGLRenderTarget>(
79 new GrGLRenderTarget(gpu, desc, sampleCount, format, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070080}
81
Greg Danielfaa095e2017-12-19 13:15:02 -050082GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
83 GrGLFramebufferInfo fbi;
84 fbi.fFBOID = fRTFBOID;
85 fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
Brian Salomon0c51eea2018-03-09 17:02:09 -050086 int numStencilBits = 0;
87 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
88 numStencilBits = stencil->bits();
89 }
Greg Danielfaa095e2017-12-19 13:15:02 -050090
Chris Dalton6ce447a2019-06-23 18:07:38 -060091 return GrBackendRenderTarget(
92 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050093}
94
Greg Daniel4065d452018-11-16 15:43:41 -050095GrBackendFormat GrGLRenderTarget::backendFormat() const {
96 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
97 // texture 2D.
98 return GrBackendFormat::MakeGL(fRTFormat, GR_GL_TEXTURE_2D);
99}
100
senorblancod2981212015-04-30 12:06:10 -0700101size_t GrGLRenderTarget::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400102 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400103 fNumSamplesOwnedPerPixel, GrMipMapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000104}
105
egdanielec00d942015-09-14 12:56:10 -0700106bool GrGLRenderTarget::completeStencilAttachment() {
107 GrGLGpu* gpu = this->getGLGpu();
108 const GrGLInterface* interface = gpu->glInterface();
109 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
110 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700111 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
112 GR_GL_STENCIL_ATTACHMENT,
113 GR_GL_RENDERBUFFER, 0));
114 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
115 GR_GL_DEPTH_ATTACHMENT,
116 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700117#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700118 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
119 // This check can cause problems in Chromium if the context has been asynchronously
120 // abandoned (see skbug.com/5200)
121 GrGLenum status;
122 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
123 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
124 }
egdanielec00d942015-09-14 12:56:10 -0700125#endif
egdanielec00d942015-09-14 12:56:10 -0700126 return true;
127 } else {
128 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
129 GrGLuint rb = glStencil->renderbufferID();
130
131 gpu->invalidateBoundRenderTarget();
Adrienne Walker4ee88512018-05-17 11:37:14 -0700132 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
egdanielec00d942015-09-14 12:56:10 -0700133 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
134 GR_GL_STENCIL_ATTACHMENT,
135 GR_GL_RENDERBUFFER, rb));
136 if (glStencil->format().fPacked) {
137 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
138 GR_GL_DEPTH_ATTACHMENT,
139 GR_GL_RENDERBUFFER, rb));
140 } else {
141 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
142 GR_GL_DEPTH_ATTACHMENT,
143 GR_GL_RENDERBUFFER, 0));
144 }
145
Adrienne Walker3ed33992018-05-15 11:44:34 -0700146
egdanielec00d942015-09-14 12:56:10 -0700147#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700148 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
149 // This check can cause problems in Chromium if the context has been asynchronously
150 // abandoned (see skbug.com/5200)
151 GrGLenum status;
152 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
153 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
154 }
egdanielec00d942015-09-14 12:56:10 -0700155#endif
156 return true;
157 }
158}
159
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000160void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700161 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700162 GrGLGpu* gpu = this->getGLGpu();
egdanield803f272015-03-18 13:01:52 -0700163 if (fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700164 gpu->deleteFramebuffer(fTexFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000165 }
egdanield803f272015-03-18 13:01:52 -0700166 if (fRTFBOID && fRTFBOID != fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700167 gpu->deleteFramebuffer(fRTFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000168 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000170 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000171 }
172 }
egdanield803f272015-03-18 13:01:52 -0700173 fRTFBOID = 0;
174 fTexFBOID = 0;
175 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000176 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000177}
178
179void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700180 fRTFBOID = 0;
181 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000182 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000183 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000184}
egdanielec00d942015-09-14 12:56:10 -0700185
186GrGLGpu* GrGLRenderTarget::getGLGpu() const {
187 SkASSERT(!this->wasDestroyed());
188 return static_cast<GrGLGpu*>(this->getGpu());
189}
190
kkinnunen2e6055b2016-04-22 01:48:29 -0700191bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500192 if (this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700193 return false;
194 }
195
ericrkc4025182016-05-04 12:01:58 -0700196 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
197 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
198 // Skia created it.
199 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700200}
201
ericrk0a5fa482015-09-15 14:16:10 -0700202void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -0700203 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
204 // which is multiply inherited from both ourselves and a texture. In these cases, one part
205 // (texture, rt) may be wrapped, while the other is owned by Skia.
206 bool refsWrappedRenderTargetObjects =
207 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
208 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
209 return;
210 }
ericrk0a5fa482015-09-15 14:16:10 -0700211
Eric Karlaf770022018-03-19 13:04:03 -0700212 // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
213 // memory usage. It is always a wrapper around either:
214 // - a texture, which is owned elsewhere, and will be dumped there
215 // - a renderbuffer, which will be dumped below.
216
217 // Log any renderbuffer's contribution to memory.
ericrk0a5fa482015-09-15 14:16:10 -0700218 if (fMSColorRenderbufferID) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400219 size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400220 this->msaaSamples(), GrMipMapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700221
222 // Due to this resource having both a texture and a renderbuffer component, dump as
223 // skia/gpu_resources/resource_#/renderbuffer
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400224 SkString resourceName = this->getResourceName();
225 resourceName.append("/renderbuffer");
ericrk0a5fa482015-09-15 14:16:10 -0700226
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400227 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
ericrk0a5fa482015-09-15 14:16:10 -0700228
229 SkString renderbuffer_id;
230 renderbuffer_id.appendU32(fMSColorRenderbufferID);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400231 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
ericrk0a5fa482015-09-15 14:16:10 -0700232 renderbuffer_id.c_str());
233 }
234}
235
ericrk0a5fa482015-09-15 14:16:10 -0700236int GrGLRenderTarget::msaaSamples() const {
237 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
238 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
239 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600240 return this->numSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700241 }
242
243 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
244 // 0 for the sample count.
245 return 0;
246}
247
248int GrGLRenderTarget::totalSamples() const {
249 int total_samples = this->msaaSamples();
250
251 if (fTexFBOID != kUnresolvableFBOID) {
252 // If we own the resolve buffer then that is one more sample per pixel.
253 total_samples += 1;
254 }
255
256 return total_samples;
257}