blob: 9c56b60495507b740b9612d8f880c6f7fa580598 [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,
Greg Daniel4065d452018-11-16 15:43:41 -050024 GrGLenum format,
egdanielec00d942015-09-14 12:56:10 -070025 const IDDesc& idDesc,
26 GrGLStencilAttachment* stencil)
kkinnunen2e6055b2016-04-22 01:48:29 -070027 : GrSurface(gpu, desc)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040028 , INHERITED(gpu, desc, stencil) {
29 this->setFlags(gpu->glCaps(), idDesc);
Greg Daniel4065d452018-11-16 15:43:41 -050030 this->init(desc, format, idDesc);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050031 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000032}
33
Greg Daniel4065d452018-11-16 15:43:41 -050034GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, GrGLenum format,
kkinnunen2e6055b2016-04-22 01:48:29 -070035 const IDDesc& idDesc)
36 : GrSurface(gpu, desc)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040037 , INHERITED(gpu, desc) {
38 this->setFlags(gpu->glCaps(), idDesc);
Greg Daniel4065d452018-11-16 15:43:41 -050039 this->init(desc, format, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000040}
41
Robert Phillipsfe0253f2018-03-16 16:47:25 -040042inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDDesc& idDesc) {
Greg Daniela070ed72018-04-26 16:31:38 -040043 if (!idDesc.fRTFBOID) {
44 this->setGLRTFBOIDIs0();
45 }
csmartdaltonf9635992016-08-10 11:09:07 -070046}
47
Greg Daniel4065d452018-11-16 15:43:41 -050048void GrGLRenderTarget::init(const GrSurfaceDesc& desc, GrGLenum format, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080049 fRTFBOID = idDesc.fRTFBOID;
50 fTexFBOID = idDesc.fTexFBOID;
51 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070052 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080053
Greg Daniel4065d452018-11-16 15:43:41 -050054 fRTFormat = format;
55
Robert Phillips29e52f12016-11-03 10:19:14 -040056 fNumSamplesOwnedPerPixel = this->totalSamples();
senorblancod2981212015-04-30 12:06:10 -070057}
58
bungeman6bd52842016-10-27 09:30:08 -070059sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
60 const GrSurfaceDesc& desc,
Greg Daniel4065d452018-11-16 15:43:41 -050061 GrGLenum format,
bungeman6bd52842016-10-27 09:30:08 -070062 const IDDesc& idDesc,
63 int stencilBits) {
egdanielec00d942015-09-14 12:56:10 -070064 GrGLStencilAttachment* sb = nullptr;
65 if (stencilBits) {
66 GrGLStencilAttachment::IDDesc sbDesc;
67 GrGLStencilAttachment::Format format;
68 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
69 format.fPacked = false;
70 format.fStencilBits = stencilBits;
71 format.fTotalBits = stencilBits;
Robert Phillipscb2e2352017-08-30 16:44:40 -040072 // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
egdanielec00d942015-09-14 12:56:10 -070073 sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
74 desc.fSampleCnt, format);
75 }
Greg Daniel4065d452018-11-16 15:43:41 -050076 return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, format, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070077}
78
Greg Danielfaa095e2017-12-19 13:15:02 -050079GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
80 GrGLFramebufferInfo fbi;
81 fbi.fFBOID = fRTFBOID;
82 fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
Brian Salomon0c51eea2018-03-09 17:02:09 -050083 int numStencilBits = 0;
84 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
85 numStencilBits = stencil->bits();
86 }
Greg Danielfaa095e2017-12-19 13:15:02 -050087
Chris Dalton6ce447a2019-06-23 18:07:38 -060088 return GrBackendRenderTarget(
89 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050090}
91
Greg Daniel4065d452018-11-16 15:43:41 -050092GrBackendFormat GrGLRenderTarget::backendFormat() const {
93 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
94 // texture 2D.
95 return GrBackendFormat::MakeGL(fRTFormat, GR_GL_TEXTURE_2D);
96}
97
senorblancod2981212015-04-30 12:06:10 -070098size_t GrGLRenderTarget::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040099 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400100 fNumSamplesOwnedPerPixel, GrMipMapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000101}
102
egdanielec00d942015-09-14 12:56:10 -0700103bool GrGLRenderTarget::completeStencilAttachment() {
104 GrGLGpu* gpu = this->getGLGpu();
105 const GrGLInterface* interface = gpu->glInterface();
106 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
107 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700108 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
109 GR_GL_STENCIL_ATTACHMENT,
110 GR_GL_RENDERBUFFER, 0));
111 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
112 GR_GL_DEPTH_ATTACHMENT,
113 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700114#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700115 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
116 // This check can cause problems in Chromium if the context has been asynchronously
117 // abandoned (see skbug.com/5200)
118 GrGLenum status;
119 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
120 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
121 }
egdanielec00d942015-09-14 12:56:10 -0700122#endif
egdanielec00d942015-09-14 12:56:10 -0700123 return true;
124 } else {
125 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
126 GrGLuint rb = glStencil->renderbufferID();
127
128 gpu->invalidateBoundRenderTarget();
Adrienne Walker4ee88512018-05-17 11:37:14 -0700129 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
egdanielec00d942015-09-14 12:56:10 -0700130 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
131 GR_GL_STENCIL_ATTACHMENT,
132 GR_GL_RENDERBUFFER, rb));
133 if (glStencil->format().fPacked) {
134 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
135 GR_GL_DEPTH_ATTACHMENT,
136 GR_GL_RENDERBUFFER, rb));
137 } else {
138 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
139 GR_GL_DEPTH_ATTACHMENT,
140 GR_GL_RENDERBUFFER, 0));
141 }
142
Adrienne Walker3ed33992018-05-15 11:44:34 -0700143
egdanielec00d942015-09-14 12:56:10 -0700144#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700145 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
146 // This check can cause problems in Chromium if the context has been asynchronously
147 // abandoned (see skbug.com/5200)
148 GrGLenum status;
149 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
150 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
151 }
egdanielec00d942015-09-14 12:56:10 -0700152#endif
153 return true;
154 }
155}
156
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000157void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700158 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700159 GrGLGpu* gpu = this->getGLGpu();
egdanield803f272015-03-18 13:01:52 -0700160 if (fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700161 gpu->deleteFramebuffer(fTexFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000162 }
egdanield803f272015-03-18 13:01:52 -0700163 if (fRTFBOID && fRTFBOID != fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700164 gpu->deleteFramebuffer(fRTFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000165 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000166 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000167 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000168 }
169 }
egdanield803f272015-03-18 13:01:52 -0700170 fRTFBOID = 0;
171 fTexFBOID = 0;
172 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000173 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000174}
175
176void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700177 fRTFBOID = 0;
178 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000180 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000181}
egdanielec00d942015-09-14 12:56:10 -0700182
183GrGLGpu* GrGLRenderTarget::getGLGpu() const {
184 SkASSERT(!this->wasDestroyed());
185 return static_cast<GrGLGpu*>(this->getGpu());
186}
187
kkinnunen2e6055b2016-04-22 01:48:29 -0700188bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500189 if (this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700190 return false;
191 }
192
ericrkc4025182016-05-04 12:01:58 -0700193 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
194 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
195 // Skia created it.
196 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700197}
198
ericrk0a5fa482015-09-15 14:16:10 -0700199void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -0700200 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
201 // which is multiply inherited from both ourselves and a texture. In these cases, one part
202 // (texture, rt) may be wrapped, while the other is owned by Skia.
203 bool refsWrappedRenderTargetObjects =
204 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
205 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
206 return;
207 }
ericrk0a5fa482015-09-15 14:16:10 -0700208
Eric Karlaf770022018-03-19 13:04:03 -0700209 // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
210 // memory usage. It is always a wrapper around either:
211 // - a texture, which is owned elsewhere, and will be dumped there
212 // - a renderbuffer, which will be dumped below.
213
214 // Log any renderbuffer's contribution to memory.
ericrk0a5fa482015-09-15 14:16:10 -0700215 if (fMSColorRenderbufferID) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400216 size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400217 this->msaaSamples(), GrMipMapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700218
219 // Due to this resource having both a texture and a renderbuffer component, dump as
220 // skia/gpu_resources/resource_#/renderbuffer
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400221 SkString resourceName = this->getResourceName();
222 resourceName.append("/renderbuffer");
ericrk0a5fa482015-09-15 14:16:10 -0700223
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400224 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
ericrk0a5fa482015-09-15 14:16:10 -0700225
226 SkString renderbuffer_id;
227 renderbuffer_id.appendU32(fMSColorRenderbufferID);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400228 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
ericrk0a5fa482015-09-15 14:16:10 -0700229 renderbuffer_id.c_str());
230 }
231}
232
ericrk0a5fa482015-09-15 14:16:10 -0700233int GrGLRenderTarget::msaaSamples() const {
234 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
235 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
236 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600237 return this->numSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700238 }
239
240 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
241 // 0 for the sample count.
242 return 0;
243}
244
245int GrGLRenderTarget::totalSamples() const {
246 int total_samples = this->msaaSamples();
247
248 if (fTexFBOID != kUnresolvableFBOID) {
249 // If we own the resolve buffer then that is one more sample per pixel.
250 total_samples += 1;
251 }
252
253 return total_samples;
254}