blob: 5dc7e04b457cea2c8317d8a2f3d5850fd6f5ce4b [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,
Brian Salomonea4ad302019-08-07 13:04:55 -040023 const SkISize& size,
24 GrGLFormat format,
25 GrPixelConfig config,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040026 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -040027 const IDs& ids,
egdanielec00d942015-09-14 12:56:10 -070028 GrGLStencilAttachment* stencil)
Brian Salomonea4ad302019-08-07 13:04:55 -040029 : GrSurface(gpu, size, config, GrProtected::kNo)
30 , INHERITED(gpu, size, config, sampleCount, GrProtected::kNo, stencil) {
31 this->setFlags(gpu->glCaps(), ids);
32 this->init(format, ids);
Brian Salomonaa6ca0a2019-01-24 16:03:07 -050033 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000034}
35
Brian Salomonea4ad302019-08-07 13:04:55 -040036GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
37 const SkISize& size,
38 GrGLFormat format,
39 GrPixelConfig config,
40 int sampleCount,
41 const IDs& ids)
42 : GrSurface(gpu, size, config, GrProtected::kNo)
43 , INHERITED(gpu, size, config, sampleCount, GrProtected::kNo) {
44 this->setFlags(gpu->glCaps(), ids);
45 this->init(format, ids);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000046}
47
Brian Salomonea4ad302019-08-07 13:04:55 -040048inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDs& idDesc) {
Greg Daniela070ed72018-04-26 16:31:38 -040049 if (!idDesc.fRTFBOID) {
50 this->setGLRTFBOIDIs0();
51 }
csmartdaltonf9635992016-08-10 11:09:07 -070052}
53
Brian Salomonea4ad302019-08-07 13:04:55 -040054void GrGLRenderTarget::init(GrGLFormat format, const IDs& idDesc) {
55 fRTFBOID = idDesc.fRTFBOID;
56 fTexFBOID = idDesc.fTexFBOID;
57 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
58 fRTFBOOwnership = idDesc.fRTFBOOwnership;
59 fRTFormat = format;
Robert Phillips29e52f12016-11-03 10:19:14 -040060 fNumSamplesOwnedPerPixel = this->totalSamples();
senorblancod2981212015-04-30 12:06:10 -070061}
62
bungeman6bd52842016-10-27 09:30:08 -070063sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
Brian Salomonea4ad302019-08-07 13:04:55 -040064 const SkISize& size,
65 GrGLFormat format,
66 GrPixelConfig config,
Brian Salomon27b4d8d2019-07-22 14:23:45 -040067 int sampleCount,
Brian Salomonea4ad302019-08-07 13:04:55 -040068 const IDs& idDesc,
bungeman6bd52842016-10-27 09:30:08 -070069 int stencilBits) {
egdanielec00d942015-09-14 12:56:10 -070070 GrGLStencilAttachment* sb = nullptr;
71 if (stencilBits) {
72 GrGLStencilAttachment::IDDesc sbDesc;
73 GrGLStencilAttachment::Format format;
74 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
75 format.fPacked = false;
76 format.fStencilBits = stencilBits;
77 format.fTotalBits = stencilBits;
Robert Phillipscb2e2352017-08-30 16:44:40 -040078 // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
Brian Salomonea4ad302019-08-07 13:04:55 -040079 sb = new GrGLStencilAttachment(gpu, sbDesc, size.width(), size.height(), sampleCount,
80 format);
egdanielec00d942015-09-14 12:56:10 -070081 }
Brian Salomon27b4d8d2019-07-22 14:23:45 -040082 return sk_sp<GrGLRenderTarget>(
Brian Salomonea4ad302019-08-07 13:04:55 -040083 new GrGLRenderTarget(gpu, size, format, config, sampleCount, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070084}
85
Greg Danielfaa095e2017-12-19 13:15:02 -050086GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
87 GrGLFramebufferInfo fbi;
88 fbi.fFBOID = fRTFBOID;
Brian Salomon1c53a9f2019-08-12 14:10:12 -040089 fbi.fFormat = GrGLFormatToEnum(this->format());
Brian Salomon0c51eea2018-03-09 17:02:09 -050090 int numStencilBits = 0;
91 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
92 numStencilBits = stencil->bits();
93 }
Greg Danielfaa095e2017-12-19 13:15:02 -050094
Chris Dalton6ce447a2019-06-23 18:07:38 -060095 return GrBackendRenderTarget(
96 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050097}
98
Greg Daniel4065d452018-11-16 15:43:41 -050099GrBackendFormat GrGLRenderTarget::backendFormat() const {
100 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
101 // texture 2D.
Brian Salomonea4ad302019-08-07 13:04:55 -0400102 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D);
Greg Daniel4065d452018-11-16 15:43:41 -0500103}
104
senorblancod2981212015-04-30 12:06:10 -0700105size_t GrGLRenderTarget::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400106 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400107 fNumSamplesOwnedPerPixel, GrMipMapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000108}
109
egdanielec00d942015-09-14 12:56:10 -0700110bool GrGLRenderTarget::completeStencilAttachment() {
111 GrGLGpu* gpu = this->getGLGpu();
112 const GrGLInterface* interface = gpu->glInterface();
113 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
114 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700115 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
116 GR_GL_STENCIL_ATTACHMENT,
117 GR_GL_RENDERBUFFER, 0));
118 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
119 GR_GL_DEPTH_ATTACHMENT,
120 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700121#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700122 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
123 // This check can cause problems in Chromium if the context has been asynchronously
124 // abandoned (see skbug.com/5200)
125 GrGLenum status;
126 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
127 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
128 }
egdanielec00d942015-09-14 12:56:10 -0700129#endif
egdanielec00d942015-09-14 12:56:10 -0700130 return true;
131 } else {
132 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
133 GrGLuint rb = glStencil->renderbufferID();
134
135 gpu->invalidateBoundRenderTarget();
Adrienne Walker4ee88512018-05-17 11:37:14 -0700136 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
egdanielec00d942015-09-14 12:56:10 -0700137 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
138 GR_GL_STENCIL_ATTACHMENT,
139 GR_GL_RENDERBUFFER, rb));
140 if (glStencil->format().fPacked) {
141 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
142 GR_GL_DEPTH_ATTACHMENT,
143 GR_GL_RENDERBUFFER, rb));
144 } else {
145 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
146 GR_GL_DEPTH_ATTACHMENT,
147 GR_GL_RENDERBUFFER, 0));
148 }
149
Adrienne Walker3ed33992018-05-15 11:44:34 -0700150
egdanielec00d942015-09-14 12:56:10 -0700151#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700152 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
153 // This check can cause problems in Chromium if the context has been asynchronously
154 // abandoned (see skbug.com/5200)
155 GrGLenum status;
156 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
157 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
158 }
egdanielec00d942015-09-14 12:56:10 -0700159#endif
160 return true;
161 }
162}
163
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000164void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700165 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700166 GrGLGpu* gpu = this->getGLGpu();
egdanield803f272015-03-18 13:01:52 -0700167 if (fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700168 gpu->deleteFramebuffer(fTexFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000169 }
egdanield803f272015-03-18 13:01:52 -0700170 if (fRTFBOID && fRTFBOID != fTexFBOID) {
Adrienne Walker4ee88512018-05-17 11:37:14 -0700171 gpu->deleteFramebuffer(fRTFBOID);
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000173 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000174 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000175 }
176 }
egdanield803f272015-03-18 13:01:52 -0700177 fRTFBOID = 0;
178 fTexFBOID = 0;
179 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000180 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000181}
182
183void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700184 fRTFBOID = 0;
185 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000186 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000187 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000188}
egdanielec00d942015-09-14 12:56:10 -0700189
190GrGLGpu* GrGLRenderTarget::getGLGpu() const {
191 SkASSERT(!this->wasDestroyed());
192 return static_cast<GrGLGpu*>(this->getGpu());
193}
194
kkinnunen2e6055b2016-04-22 01:48:29 -0700195bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500196 if (this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers()) {
Eric Karl5c779752017-05-08 12:02:07 -0700197 return false;
198 }
199
ericrkc4025182016-05-04 12:01:58 -0700200 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
201 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
202 // Skia created it.
203 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700204}
205
ericrk0a5fa482015-09-15 14:16:10 -0700206void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
Eric Karlaf770022018-03-19 13:04:03 -0700207 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
208 // which is multiply inherited from both ourselves and a texture. In these cases, one part
209 // (texture, rt) may be wrapped, while the other is owned by Skia.
210 bool refsWrappedRenderTargetObjects =
211 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
212 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
213 return;
214 }
ericrk0a5fa482015-09-15 14:16:10 -0700215
Eric Karlaf770022018-03-19 13:04:03 -0700216 // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
217 // memory usage. It is always a wrapper around either:
218 // - a texture, which is owned elsewhere, and will be dumped there
219 // - a renderbuffer, which will be dumped below.
220
221 // Log any renderbuffer's contribution to memory.
ericrk0a5fa482015-09-15 14:16:10 -0700222 if (fMSColorRenderbufferID) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400223 size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400224 this->msaaSamples(), GrMipMapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700225
226 // Due to this resource having both a texture and a renderbuffer component, dump as
227 // skia/gpu_resources/resource_#/renderbuffer
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400228 SkString resourceName = this->getResourceName();
229 resourceName.append("/renderbuffer");
ericrk0a5fa482015-09-15 14:16:10 -0700230
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400231 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
ericrk0a5fa482015-09-15 14:16:10 -0700232
233 SkString renderbuffer_id;
234 renderbuffer_id.appendU32(fMSColorRenderbufferID);
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -0400235 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
ericrk0a5fa482015-09-15 14:16:10 -0700236 renderbuffer_id.c_str());
237 }
238}
239
ericrk0a5fa482015-09-15 14:16:10 -0700240int GrGLRenderTarget::msaaSamples() const {
241 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
242 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
243 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600244 return this->numSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700245 }
246
247 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
248 // 0 for the sample count.
249 return 0;
250}
251
252int GrGLRenderTarget::totalSamples() const {
253 int total_samples = this->msaaSamples();
254
255 if (fTexFBOID != kUnresolvableFBOID) {
256 // If we own the resolve buffer then that is one more sample per pixel.
257 total_samples += 1;
258 }
259
260 return total_samples;
261}