blob: 5efb44fffec5b8855a4d3edf54b30fdedf963025 [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
Brian Salomonf7f54332020-07-28 09:23:35 -04008#include "src/gpu/gl/GrGLRenderTarget.h"
9
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkTraceMemoryDump.h"
Adlai Holler3d0359a2020-07-09 15:35:55 -040011#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrGpuResourcePriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/gl/GrGLGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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
Greg Daniele77162e2020-09-21 15:32:11 -040076 sb = new GrGLStencilAttachment(gpu, sbDesc, dimensions, sampleCount, format);
egdanielec00d942015-09-14 12:56:10 -070077 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -040078 return sk_sp<GrGLRenderTarget>(
Greg Danield51fa2f2020-01-22 16:53:38 -050079 new GrGLRenderTarget(gpu, dimensions, format, sampleCount, 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;
Brian Salomon1c53a9f2019-08-12 14:10:12 -040085 fbi.fFormat = GrGLFormatToEnum(this->format());
Brian Salomon0c51eea2018-03-09 17:02:09 -050086 int numStencilBits = 0;
Brian Salomonf7f54332020-07-28 09:23:35 -040087 if (GrStencilAttachment* stencil = this->getStencilAttachment()) {
Brian Salomon0c51eea2018-03-09 17:02:09 -050088 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.
Brian Salomonea4ad302019-08-07 13:04:55 -040098 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D);
Greg Daniel4065d452018-11-16 15:43:41 -050099}
100
senorblancod2981212015-04-30 12:06:10 -0700101size_t GrGLRenderTarget::onGpuMemorySize() const {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400102 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400103 return GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400104 fNumSamplesOwnedPerPixel, GrMipmapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000105}
106
egdanielec00d942015-09-14 12:56:10 -0700107bool GrGLRenderTarget::completeStencilAttachment() {
108 GrGLGpu* gpu = this->getGLGpu();
109 const GrGLInterface* interface = gpu->glInterface();
Brian Salomonf7f54332020-07-28 09:23:35 -0400110 GrStencilAttachment* stencil = this->getStencilAttachment();
egdanielec00d942015-09-14 12:56:10 -0700111 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700112 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
113 GR_GL_STENCIL_ATTACHMENT,
114 GR_GL_RENDERBUFFER, 0));
115 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
116 GR_GL_DEPTH_ATTACHMENT,
117 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700118#ifdef SK_DEBUG
Brian Salomond8575452020-02-25 12:13:29 -0500119 if (!gpu->glCaps().skipErrorChecks()) {
robertphillipsfa7ff472016-04-21 11:27:43 -0700120 // This check can cause problems in Chromium if the context has been asynchronously
121 // abandoned (see skbug.com/5200)
122 GrGLenum status;
123 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
124 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
125 }
egdanielec00d942015-09-14 12:56:10 -0700126#endif
egdanielec00d942015-09-14 12:56:10 -0700127 return true;
128 } else {
129 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
130 GrGLuint rb = glStencil->renderbufferID();
131
132 gpu->invalidateBoundRenderTarget();
Adrienne Walker4ee88512018-05-17 11:37:14 -0700133 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
egdanielec00d942015-09-14 12:56:10 -0700134 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
135 GR_GL_STENCIL_ATTACHMENT,
136 GR_GL_RENDERBUFFER, rb));
137 if (glStencil->format().fPacked) {
138 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
139 GR_GL_DEPTH_ATTACHMENT,
140 GR_GL_RENDERBUFFER, rb));
141 } else {
142 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
143 GR_GL_DEPTH_ATTACHMENT,
144 GR_GL_RENDERBUFFER, 0));
145 }
146
Adrienne Walker3ed33992018-05-15 11:44:34 -0700147
egdanielec00d942015-09-14 12:56:10 -0700148#ifdef SK_DEBUG
Brian Salomond8575452020-02-25 12:13:29 -0500149 if (!gpu->glCaps().skipErrorChecks()) {
robertphillipsfa7ff472016-04-21 11:27:43 -0700150 // This check can cause problems in Chromium if the context has been asynchronously
151 // abandoned (see skbug.com/5200)
152 GrGLenum status;
153 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
154 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
155 }
egdanielec00d942015-09-14 12:56:10 -0700156#endif
157 return true;
158 }
159}
160
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000161void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700162 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700163 GrGLGpu* gpu = this->getGLGpu();
egdanield803f272015-03-18 13:01:52 -0700164 if (fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700165 gpu->deleteFramebuffer(fTexFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000166 }
egdanield803f272015-03-18 13:01:52 -0700167 if (fRTFBOID && fRTFBOID != fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700168 gpu->deleteFramebuffer(fRTFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000170 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000171 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172 }
173 }
egdanield803f272015-03-18 13:01:52 -0700174 fRTFBOID = 0;
175 fTexFBOID = 0;
176 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000177 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000178}
179
180void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700181 fRTFBOID = 0;
182 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000183 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000184 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000185}
egdanielec00d942015-09-14 12:56:10 -0700186
187GrGLGpu* GrGLRenderTarget::getGLGpu() const {
188 SkASSERT(!this->wasDestroyed());
189 return static_cast<GrGLGpu*>(this->getGpu());
190}
191
kkinnunen2e6055b2016-04-22 01:48:29 -0700192bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500193 if (this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700194 return false;
195 }
196
ericrkc4025182016-05-04 12:01:58 -0700197 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
198 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
199 // Skia created it.
200 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700201}
202
ericrk0a5fa482015-09-15 14:16:10 -0700203void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -0700204 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
205 // which is multiply inherited from both ourselves and a texture. In these cases, one part
206 // (texture, rt) may be wrapped, while the other is owned by Skia.
207 bool refsWrappedRenderTargetObjects =
208 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
209 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
210 return;
211 }
ericrk0a5fa482015-09-15 14:16:10 -0700212
Eric Karlaf770022018-03-19 13:04:03 -0700213 // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
214 // memory usage. It is always a wrapper around either:
215 // - a texture, which is owned elsewhere, and will be dumped there
216 // - a renderbuffer, which will be dumped below.
217
218 // Log any renderbuffer's contribution to memory.
ericrk0a5fa482015-09-15 14:16:10 -0700219 if (fMSColorRenderbufferID) {
Greg Daniel7fd7a8a2019-10-10 16:10:31 -0400220 const GrCaps& caps = *this->getGpu()->caps();
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400221 size_t size = GrSurface::ComputeSize(caps, this->backendFormat(), this->dimensions(),
Brian Salomon7e67dca2020-07-21 09:27:25 -0400222 this->msaaSamples(), GrMipmapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700223
224 // Due to this resource having both a texture and a renderbuffer component, dump as
225 // skia/gpu_resources/resource_#/renderbuffer
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400226 SkString resourceName = this->getResourceName();
227 resourceName.append("/renderbuffer");
ericrk0a5fa482015-09-15 14:16:10 -0700228
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400229 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
ericrk0a5fa482015-09-15 14:16:10 -0700230
231 SkString renderbuffer_id;
232 renderbuffer_id.appendU32(fMSColorRenderbufferID);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400233 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
ericrk0a5fa482015-09-15 14:16:10 -0700234 renderbuffer_id.c_str());
235 }
236}
237
ericrk0a5fa482015-09-15 14:16:10 -0700238int GrGLRenderTarget::msaaSamples() const {
239 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
240 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
241 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600242 return this->numSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700243 }
244
245 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
246 // 0 for the sample count.
247 return 0;
248}
249
250int GrGLRenderTarget::totalSamples() const {
251 int total_samples = this->msaaSamples();
252
253 if (fTexFBOID != kUnresolvableFBOID) {
254 // If we own the resolve buffer then that is one more sample per pixel.
255 total_samples += 1;
256 }
257
258 return total_samples;
259}