blob: de7399bd92153949309add7bdb8821b52a9bcf6d [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)
csmartdaltonf9635992016-08-10 11:09:07 -070027 , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc), stencil) {
bsalomon37dd3312014-11-03 08:47:23 -080028 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070029 this->registerWithCacheWrapped();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000030}
31
kkinnunen2e6055b2016-04-22 01:48:29 -070032GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc,
33 const IDDesc& idDesc)
34 : GrSurface(gpu, desc)
csmartdaltonf9635992016-08-10 11:09:07 -070035 , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc)) {
bsalomon37dd3312014-11-03 08:47:23 -080036 this->init(desc, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000037}
38
Robert Phillipsc4f0a822017-06-13 08:11:36 -040039inline GrRenderTargetFlags GrGLRenderTarget::ComputeFlags(const GrGLCaps& glCaps,
40 const IDDesc& idDesc) {
41 GrRenderTargetFlags flags = GrRenderTargetFlags::kNone;
csmartdaltonf9635992016-08-10 11:09:07 -070042 if (idDesc.fIsMixedSampled) {
43 SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled.
Robert Phillipsc4f0a822017-06-13 08:11:36 -040044 flags |= GrRenderTargetFlags::kMixedSampled;
csmartdaltonf9635992016-08-10 11:09:07 -070045 }
Jim Van Verth6a40abc2017-11-02 16:56:09 +000046 if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) {
Robert Phillipsc4f0a822017-06-13 08:11:36 -040047 flags |= GrRenderTargetFlags::kWindowRectsSupport;
csmartdaltonf9635992016-08-10 11:09:07 -070048 }
49 return flags;
50}
51
bsalomon37dd3312014-11-03 08:47:23 -080052void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080053 fRTFBOID = idDesc.fRTFBOID;
54 fTexFBOID = idDesc.fTexFBOID;
55 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070056 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080057
58 fViewport.fLeft = 0;
59 fViewport.fBottom = 0;
60 fViewport.fWidth = desc.fWidth;
61 fViewport.fHeight = desc.fHeight;
62
Robert Phillips29e52f12016-11-03 10:19:14 -040063 fNumSamplesOwnedPerPixel = this->totalSamples();
senorblancod2981212015-04-30 12:06:10 -070064}
65
bungeman6bd52842016-10-27 09:30:08 -070066sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
67 const GrSurfaceDesc& desc,
68 const IDDesc& idDesc,
69 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
egdanielec00d942015-09-14 12:56:10 -070079 sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
80 desc.fSampleCnt, format);
81 }
bungeman6bd52842016-10-27 09:30:08 -070082 return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb));
egdanielec00d942015-09-14 12:56:10 -070083}
84
Greg Danielfaa095e2017-12-19 13:15:02 -050085GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
86 GrGLFramebufferInfo fbi;
87 fbi.fFBOID = fRTFBOID;
88 fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
Brian Salomon0c51eea2018-03-09 17:02:09 -050089 int numStencilBits = 0;
90 if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
91 numStencilBits = stencil->bits();
92 }
Greg Danielfaa095e2017-12-19 13:15:02 -050093
Greg Daniel8a3f55c2018-03-14 17:32:12 +000094 return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
95 numStencilBits, fbi);
Greg Danielfaa095e2017-12-19 13:15:02 -050096}
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();
129 gpu->stats()->incRenderTargetBinds();
130 GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID()));
131 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
132 GR_GL_STENCIL_ATTACHMENT,
133 GR_GL_RENDERBUFFER, rb));
134 if (glStencil->format().fPacked) {
135 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
136 GR_GL_DEPTH_ATTACHMENT,
137 GR_GL_RENDERBUFFER, rb));
138 } else {
139 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
140 GR_GL_DEPTH_ATTACHMENT,
141 GR_GL_RENDERBUFFER, 0));
142 }
143
144#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) {
egdanield803f272015-03-18 13:01:52 -0700159 if (fTexFBOID) {
160 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000161 }
egdanield803f272015-03-18 13:01:52 -0700162 if (fRTFBOID && fRTFBOID != fTexFBOID) {
163 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000164 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000165 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000166 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000167 }
168 }
egdanield803f272015-03-18 13:01:52 -0700169 fRTFBOID = 0;
170 fTexFBOID = 0;
171 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000172 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000173}
174
175void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700176 fRTFBOID = 0;
177 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000178 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000179 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000180}
egdanielec00d942015-09-14 12:56:10 -0700181
182GrGLGpu* GrGLRenderTarget::getGLGpu() const {
183 SkASSERT(!this->wasDestroyed());
184 return static_cast<GrGLGpu*>(this->getGpu());
185}
186
kkinnunen2e6055b2016-04-22 01:48:29 -0700187bool GrGLRenderTarget::canAttemptStencilAttachment() const {
Eric Karl5c779752017-05-08 12:02:07 -0700188 if (this->getGpu()->getContext()->caps()->avoidStencilBuffers()) {
189 return false;
190 }
191
ericrkc4025182016-05-04 12:01:58 -0700192 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
193 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
194 // Skia created it.
195 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700196}
197
ericrk0a5fa482015-09-15 14:16:10 -0700198void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
199 // Don't log the backing texture's contribution to the memory size. This will be handled by the
200 // texture object.
201
202 // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer
203 // (have a fMSColorRenderbufferID).
204 if (fMSColorRenderbufferID) {
Brian Salomonbb5711a2017-05-17 13:49:59 -0400205 size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
Greg Daniele252f082017-10-23 16:05:23 -0400206 this->msaaSamples(), GrMipMapped::kNo);
ericrk0a5fa482015-09-15 14:16:10 -0700207
208 // Due to this resource having both a texture and a renderbuffer component, dump as
209 // skia/gpu_resources/resource_#/renderbuffer
210 SkString dumpName("skia/gpu_resources/resource_");
Robert Phillips294870f2016-11-11 12:38:40 -0500211 dumpName.appendU32(this->uniqueID().asUInt());
ericrk0a5fa482015-09-15 14:16:10 -0700212 dumpName.append("/renderbuffer");
213
214 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size);
215
216 if (this->isPurgeable()) {
217 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size);
218 }
219
220 SkString renderbuffer_id;
221 renderbuffer_id.appendU32(fMSColorRenderbufferID);
222 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer",
223 renderbuffer_id.c_str());
224 }
225}
226
ericrk0a5fa482015-09-15 14:16:10 -0700227int GrGLRenderTarget::msaaSamples() const {
228 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
229 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
230 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
Brian Salomonbdecacf2018-02-02 20:32:49 -0500231 return this->numStencilSamples();
ericrk0a5fa482015-09-15 14:16:10 -0700232 }
233
234 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
235 // 0 for the sample count.
236 return 0;
237}
238
239int GrGLRenderTarget::totalSamples() const {
240 int total_samples = this->msaaSamples();
241
242 if (fTexFBOID != kUnresolvableFBOID) {
243 // If we own the resolve buffer then that is one more sample per pixel.
244 total_samples += 1;
245 }
246
247 return total_samples;
248}