blob: e1a4eb0bae0fa6fd426c24062f671c67db9d23dc [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
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00008#include "GrGLRenderTarget.h"
9
Eric Karl5c779752017-05-08 12:02:07 -070010#include "GrContext.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "GrGLGpu.h"
egdanielec00d942015-09-14 12:56:10 -070012#include "GrGLUtil.h"
kkinnunen2e6055b2016-04-22 01:48:29 -070013#include "GrGpuResourcePriv.h"
14#include "GrRenderTargetPriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070015#include "SkTraceMemoryDump.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,
24 const IDDesc& idDesc,
25 GrGLStencilAttachment* stencil)
kkinnunen2e6055b2016-04-22 01:48:29 -070026 : GrSurface(gpu, desc)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040027 , INHERITED(gpu, desc, stencil) {
28 this->setFlags(gpu->glCaps(), idDesc);
bsalomon37dd3312014-11-03 08:47:23 -080029 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070030 this->registerWithCacheWrapped();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000031}
32
kkinnunen2e6055b2016-04-22 01:48:29 -070033GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc,
34 const IDDesc& idDesc)
35 : GrSurface(gpu, desc)
Robert Phillipsfe0253f2018-03-16 16:47:25 -040036 , INHERITED(gpu, desc) {
37 this->setFlags(gpu->glCaps(), idDesc);
bsalomon37dd3312014-11-03 08:47:23 -080038 this->init(desc, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000039}
40
Robert Phillipsfe0253f2018-03-16 16:47:25 -040041inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDDesc& idDesc) {
csmartdaltonf9635992016-08-10 11:09:07 -070042 if (idDesc.fIsMixedSampled) {
43 SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled.
Robert Phillipsfe0253f2018-03-16 16:47:25 -040044 this->setHasMixedSamples();
csmartdaltonf9635992016-08-10 11:09:07 -070045 }
Jim Van Verth6a40abc2017-11-02 16:56:09 +000046 if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -040047 this->setSupportsWindowRects();
csmartdaltonf9635992016-08-10 11:09:07 -070048 }
csmartdaltonf9635992016-08-10 11:09:07 -070049}
50
bsalomon37dd3312014-11-03 08:47:23 -080051void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080052 fRTFBOID = idDesc.fRTFBOID;
53 fTexFBOID = idDesc.fTexFBOID;
54 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070055 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080056
57 fViewport.fLeft = 0;
58 fViewport.fBottom = 0;
59 fViewport.fWidth = desc.fWidth;
60 fViewport.fHeight = desc.fHeight;
61
Robert Phillips29e52f12016-11-03 10:19:14 -040062 fNumSamplesOwnedPerPixel = this->totalSamples();
senorblancod2981212015-04-30 12:06:10 -070063}
64
bungeman6bd52842016-10-27 09:30:08 -070065sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
66 const GrSurfaceDesc& desc,
67 const IDDesc& idDesc,
68 int stencilBits) {
egdanielec00d942015-09-14 12:56:10 -070069 GrGLStencilAttachment* sb = nullptr;
70 if (stencilBits) {
71 GrGLStencilAttachment::IDDesc sbDesc;
72 GrGLStencilAttachment::Format format;
73 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
74 format.fPacked = false;
75 format.fStencilBits = stencilBits;
76 format.fTotalBits = stencilBits;
Robert Phillipscb2e2352017-08-30 16:44:40 -040077 // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
egdanielec00d942015-09-14 12:56:10 -070078 sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
79 desc.fSampleCnt, format);
80 }
bungeman6bd52842016-10-27 09:30:08 -070081 return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070082}
83
Greg Danielfaa095e2017-12-19 13:15:02 -050084GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
85 GrGLFramebufferInfo fbi;
86 fbi.fFBOID = fRTFBOID;
87 fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
Brian Salomon0c51eea2018-03-09 17:02:09 -050088 int numStencilBits = 0;
89 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
90 numStencilBits = stencil->bits();
91 }
Greg Danielfaa095e2017-12-19 13:15:02 -050092
Greg Daniel8a3f55c2018-03-14 17:32:12 +000093 return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
94 numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050095}
96
senorblancod2981212015-04-30 12:06:10 -070097size_t GrGLRenderTarget::onGpuMemorySize() const {
Brian Salomonbb5711a2017-05-17 13:49:59 -040098 return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -040099 fNumSamplesOwnedPerPixel, GrMipMapped::kNo);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +0000100}
101
egdanielec00d942015-09-14 12:56:10 -0700102bool GrGLRenderTarget::completeStencilAttachment() {
103 GrGLGpu* gpu = this->getGLGpu();
104 const GrGLInterface* interface = gpu->glInterface();
105 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
106 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -0700107 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
108 GR_GL_STENCIL_ATTACHMENT,
109 GR_GL_RENDERBUFFER, 0));
110 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
111 GR_GL_DEPTH_ATTACHMENT,
112 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700113#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700114 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
115 // This check can cause problems in Chromium if the context has been asynchronously
116 // abandoned (see skbug.com/5200)
117 GrGLenum status;
118 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
119 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
120 }
egdanielec00d942015-09-14 12:56:10 -0700121#endif
egdanielec00d942015-09-14 12:56:10 -0700122 return true;
123 } else {
124 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
125 GrGLuint rb = glStencil->renderbufferID();
126
127 gpu->invalidateBoundRenderTarget();
128 gpu->stats()->incRenderTargetBinds();
129 GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID()));
130 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
143#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700144 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
145 // This check can cause problems in Chromium if the context has been asynchronously
146 // abandoned (see skbug.com/5200)
147 GrGLenum status;
148 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
149 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
150 }
egdanielec00d942015-09-14 12:56:10 -0700151#endif
152 return true;
153 }
154}
155
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000156void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700157 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
egdanield803f272015-03-18 13:01:52 -0700158 if (fTexFBOID) {
159 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000160 }
egdanield803f272015-03-18 13:01:52 -0700161 if (fRTFBOID && fRTFBOID != fTexFBOID) {
162 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000163 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000164 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000165 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000166 }
167 }
egdanield803f272015-03-18 13:01:52 -0700168 fRTFBOID = 0;
169 fTexFBOID = 0;
170 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000171 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000172}
173
174void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700175 fRTFBOID = 0;
176 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000177 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000178 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000179}
egdanielec00d942015-09-14 12:56:10 -0700180
181GrGLGpu* GrGLRenderTarget::getGLGpu() const {
182 SkASSERT(!this->wasDestroyed());
183 return static_cast<GrGLGpu*>(this->getGpu());
184}
185
kkinnunen2e6055b2016-04-22 01:48:29 -0700186bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Eric Karl5c779752017-05-08 12:02:07 -0700187 if (this->getGpu()->getContext()->caps()->avoidStencilBuffers()) {
188 return false;
189 }
190
ericrkc4025182016-05-04 12:01:58 -0700191 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
192 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
193 // Skia created it.
194 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700195}
196
ericrk0a5fa482015-09-15 14:16:10 -0700197void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
198 // Don't log the backing texture's contribution to the memory size. This will be handled by the
199 // texture object.
200
201 // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer
202 // (have a fMSColorRenderbufferID).
203 if (fMSColorRenderbufferID) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400204 size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400205 this->msaaSamples(), GrMipMapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700206
207 // Due to this resource having both a texture and a renderbuffer component, dump as
208 // skia/gpu_resources/resource_#/renderbuffer
209 SkString dumpName("skia/gpu_resources/resource_");
Robert Phillips294870f2016-11-11 12:38:40 -0500210 dumpName.appendU32(this->uniqueID().asUInt());
ericrk0a5fa482015-09-15 14:16:10 -0700211 dumpName.append("/renderbuffer");
212
213 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size);
214
215 if (this->isPurgeable()) {
216 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size);
217 }
218
219 SkString renderbuffer_id;
220 renderbuffer_id.appendU32(fMSColorRenderbufferID);
221 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer",
222 renderbuffer_id.c_str());
223 }
224}
225
ericrk0a5fa482015-09-15 14:16:10 -0700226int GrGLRenderTarget::msaaSamples() const {
227 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
228 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
229 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500230 return this->numStencilSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700231 }
232
233 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
234 // 0 for the sample count.
235 return 0;
236}
237
238int GrGLRenderTarget::totalSamples() const {
239 int total_samples = this->msaaSamples();
240
241 if (fTexFBOID != kUnresolvableFBOID) {
242 // If we own the resolve buffer then that is one more sample per pixel.
243 total_samples += 1;
244 }
245
246 return total_samples;
247}