blob: 7485874a7b1a916f4173050e4dbe7d5d1cd4b5ee [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"
Adlai Holler3d0359a2020-07-09 15:35:55 -04009#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040023 const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -040024 GrGLFormat format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040025 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -040026 const IDs& ids,
egdanielec00d942015-09-14 12:56:10 -070027 GrGLStencilAttachment* stencil)
Greg Danield51fa2f2020-01-22 16:53:38 -050028 : GrSurface(gpu, dimensions, GrProtected::kNo)
29 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo, stencil) {
Brian Salomonea4ad302019-08-07 13:04:55 -040030 this->setFlags(gpu->glCaps(), ids);
31 this->init(format, ids);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050032 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000033}
34
Brian Salomonea4ad302019-08-07 13:04:55 -040035GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040036 const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -040037 GrGLFormat format,
Brian Salomonea4ad302019-08-07 13:04:55 -040038 int sampleCount,
39 const IDs& ids)
Greg Danield51fa2f2020-01-22 16:53:38 -050040 : GrSurface(gpu, dimensions, GrProtected::kNo)
41 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo) {
Brian Salomonea4ad302019-08-07 13:04:55 -040042 this->setFlags(gpu->glCaps(), ids);
43 this->init(format, ids);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000044}
45
Brian Salomonea4ad302019-08-07 13:04:55 -040046inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDs& idDesc) {
Greg Daniela070ed72018-04-26 16:31:38 -040047 if (!idDesc.fRTFBOID) {
Jim Van Verth5fab9092019-11-25 21:23:53 +000048 this->setGLRTFBOIDIs0();
Greg Daniela070ed72018-04-26 16:31:38 -040049 }
csmartdaltonf9635992016-08-10 11:09:07 -070050}
51
Brian Salomonea4ad302019-08-07 13:04:55 -040052void GrGLRenderTarget::init(GrGLFormat format, const IDs& idDesc) {
53 fRTFBOID = idDesc.fRTFBOID;
54 fTexFBOID = idDesc.fTexFBOID;
55 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
56 fRTFBOOwnership = idDesc.fRTFBOOwnership;
57 fRTFormat = format;
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,
Brian Salomon9f2b86c2019-10-22 10:37:46 -040062 const SkISize& dimensions,
Brian Salomonea4ad302019-08-07 13:04:55 -040063 GrGLFormat format,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040064 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -040065 const IDs& idDesc,
bungeman6bd52842016-10-27 09:30:08 -070066 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 Salomon9f2b86c2019-10-22 10:37:46 -040076 sb = new GrGLStencilAttachment(gpu, sbDesc, dimensions.width(), dimensions.height(),
77 sampleCount, format);
egdanielec00d942015-09-14 12:56:10 -070078 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -040079 return sk_sp<GrGLRenderTarget>(
Greg Danield51fa2f2020-01-22 16:53:38 -050080 new GrGLRenderTarget(gpu, dimensions, format, sampleCount, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070081}
82
Greg Danielfaa095e2017-12-19 13:15:02 -050083GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
84 GrGLFramebufferInfo fbi;
85 fbi.fFBOID = fRTFBOID;
Brian Salomon1c53a9f2019-08-12 14:10:12 -040086 fbi.fFormat = GrGLFormatToEnum(this->format());
Brian Salomon0c51eea2018-03-09 17:02:09 -050087 int numStencilBits = 0;
88 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
89 numStencilBits = stencil->bits();
90 }
Greg Danielfaa095e2017-12-19 13:15:02 -050091
Chris Dalton6ce447a2019-06-23 18:07:38 -060092 return GrBackendRenderTarget(
93 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050094}
95
Greg Daniel4065d452018-11-16 15:43:41 -050096GrBackendFormat GrGLRenderTarget::backendFormat() const {
97 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
98 // texture 2D.
Brian Salomonea4ad302019-08-07 13:04:55 -040099 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D);
Greg Daniel4065d452018-11-16 15:43:41 -0500100}
101
senorblancod2981212015-04-30 12:06:10 -0700102size_t GrGLRenderTarget::onGpuMemorySize() const {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400103 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400104 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400105 fNumSamplesOwnedPerPixel, GrMipmapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000106}
107
egdanielec00d942015-09-14 12:56:10 -0700108bool GrGLRenderTarget::completeStencilAttachment() {
109 GrGLGpu* gpu = this->getGLGpu();
110 const GrGLInterface* interface = gpu->glInterface();
111 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
112 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700113 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
114 GR_GL_STENCIL_ATTACHMENT,
115 GR_GL_RENDERBUFFER, 0));
116 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
117 GR_GL_DEPTH_ATTACHMENT,
118 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700119#ifdef SK_DEBUG
Brian Salomond8575452020-02-25 12:13:29 -0500120 if (!gpu->glCaps().skipErrorChecks()) {
robertphillipsfa7ff472016-04-21 11:27:43 -0700121 // This check can cause problems in Chromium if the context has been asynchronously
122 // abandoned (see skbug.com/5200)
123 GrGLenum status;
124 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
125 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
126 }
egdanielec00d942015-09-14 12:56:10 -0700127#endif
egdanielec00d942015-09-14 12:56:10 -0700128 return true;
129 } else {
130 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
131 GrGLuint rb = glStencil->renderbufferID();
132
133 gpu->invalidateBoundRenderTarget();
Adrienne Walker4ee88512018-05-17 11:37:14 -0700134 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
egdanielec00d942015-09-14 12:56:10 -0700135 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
136 GR_GL_STENCIL_ATTACHMENT,
137 GR_GL_RENDERBUFFER, rb));
138 if (glStencil->format().fPacked) {
139 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
140 GR_GL_DEPTH_ATTACHMENT,
141 GR_GL_RENDERBUFFER, rb));
142 } else {
143 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
144 GR_GL_DEPTH_ATTACHMENT,
145 GR_GL_RENDERBUFFER, 0));
146 }
147
Adrienne Walker3ed33992018-05-15 11:44:34 -0700148
egdanielec00d942015-09-14 12:56:10 -0700149#ifdef SK_DEBUG
Brian Salomond8575452020-02-25 12:13:29 -0500150 if (!gpu->glCaps().skipErrorChecks()) {
robertphillipsfa7ff472016-04-21 11:27:43 -0700151 // This check can cause problems in Chromium if the context has been asynchronously
152 // abandoned (see skbug.com/5200)
153 GrGLenum status;
154 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
155 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
156 }
egdanielec00d942015-09-14 12:56:10 -0700157#endif
158 return true;
159 }
160}
161
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000162void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700163 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700164 GrGLGpu* gpu = this->getGLGpu();
egdanield803f272015-03-18 13:01:52 -0700165 if (fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700166 gpu->deleteFramebuffer(fTexFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000167 }
egdanield803f272015-03-18 13:01:52 -0700168 if (fRTFBOID && fRTFBOID != fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700169 gpu->deleteFramebuffer(fRTFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000170 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000171 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000172 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000173 }
174 }
egdanield803f272015-03-18 13:01:52 -0700175 fRTFBOID = 0;
176 fTexFBOID = 0;
177 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000178 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179}
180
181void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700182 fRTFBOID = 0;
183 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000184 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000185 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000186}
egdanielec00d942015-09-14 12:56:10 -0700187
188GrGLGpu* GrGLRenderTarget::getGLGpu() const {
189 SkASSERT(!this->wasDestroyed());
190 return static_cast<GrGLGpu*>(this->getGpu());
191}
192
kkinnunen2e6055b2016-04-22 01:48:29 -0700193bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500194 if (this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700195 return false;
196 }
197
ericrkc4025182016-05-04 12:01:58 -0700198 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
199 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
200 // Skia created it.
201 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700202}
203
ericrk0a5fa482015-09-15 14:16:10 -0700204void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -0700205 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
206 // which is multiply inherited from both ourselves and a texture. In these cases, one part
207 // (texture, rt) may be wrapped, while the other is owned by Skia.
208 bool refsWrappedRenderTargetObjects =
209 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
210 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
211 return;
212 }
ericrk0a5fa482015-09-15 14:16:10 -0700213
Eric Karlaf770022018-03-19 13:04:03 -0700214 // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
215 // memory usage. It is always a wrapper around either:
216 // - a texture, which is owned elsewhere, and will be dumped there
217 // - a renderbuffer, which will be dumped below.
218
219 // Log any renderbuffer's contribution to memory.
ericrk0a5fa482015-09-15 14:16:10 -0700220 if (fMSColorRenderbufferID) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400221 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400222 size_t size = GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400223 this->msaaSamples(), GrMipmapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700224
225 // Due to this resource having both a texture and a renderbuffer component, dump as
226 // skia/gpu_resources/resource_#/renderbuffer
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400227 SkString resourceName = this->getResourceName();
228 resourceName.append("/renderbuffer");
ericrk0a5fa482015-09-15 14:16:10 -0700229
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400230 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
ericrk0a5fa482015-09-15 14:16:10 -0700231
232 SkString renderbuffer_id;
233 renderbuffer_id.appendU32(fMSColorRenderbufferID);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400234 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
ericrk0a5fa482015-09-15 14:16:10 -0700235 renderbuffer_id.c_str());
236 }
237}
238
ericrk0a5fa482015-09-15 14:16:10 -0700239int GrGLRenderTarget::msaaSamples() const {
240 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
241 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
242 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600243 return this->numSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700244 }
245
246 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
247 // 0 for the sample count.
248 return 0;
249}
250
251int GrGLRenderTarget::totalSamples() const {
252 int total_samples = this->msaaSamples();
253
254 if (fTexFBOID != kUnresolvableFBOID) {
255 // If we own the resolve buffer then that is one more sample per pixel.
256 total_samples += 1;
257 }
258
259 return total_samples;
260}