bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 1 | /* |
| 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.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 8 | #include "GrGLRenderTarget.h" |
| 9 | |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 10 | #include "GrContext.h" |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 11 | #include "GrGLGpu.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 12 | #include "GrGLUtil.h" |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
| 14 | #include "GrRenderTargetPriv.h" |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 15 | #include "SkTraceMemoryDump.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 16 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 17 | #define GPUGL static_cast<GrGLGpu*>(this->getGpu()) |
| 18 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 19 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 20 | // Because this class is virtually derived from GrSurface we must explicitly call its constructor. |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 21 | // Constructor for wrapped render targets. |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 22 | GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, |
| 23 | const GrSurfaceDesc& desc, |
| 24 | const IDDesc& idDesc, |
| 25 | GrGLStencilAttachment* stencil) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 26 | : GrSurface(gpu, desc) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame^] | 27 | , INHERITED(gpu, desc, stencil) { |
| 28 | this->setFlags(gpu->glCaps(), idDesc); |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 29 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 30 | this->registerWithCacheWrapped(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 31 | } |
| 32 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 33 | GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, |
| 34 | const IDDesc& idDesc) |
| 35 | : GrSurface(gpu, desc) |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame^] | 36 | , INHERITED(gpu, desc) { |
| 37 | this->setFlags(gpu->glCaps(), idDesc); |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 38 | this->init(desc, idDesc); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame^] | 41 | inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDDesc& idDesc) { |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 42 | if (idDesc.fIsMixedSampled) { |
| 43 | SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled. |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame^] | 44 | this->setHasMixedSamples(); |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 45 | } |
Jim Van Verth | 6a40abc | 2017-11-02 16:56:09 +0000 | [diff] [blame] | 46 | if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) { |
Robert Phillips | fe0253f | 2018-03-16 16:47:25 -0400 | [diff] [blame^] | 47 | this->setSupportsWindowRects(); |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 48 | } |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 49 | } |
| 50 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 51 | void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) { |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 52 | fRTFBOID = idDesc.fRTFBOID; |
| 53 | fTexFBOID = idDesc.fTexFBOID; |
| 54 | fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 55 | fRTFBOOwnership = idDesc.fRTFBOOwnership; |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 56 | |
| 57 | fViewport.fLeft = 0; |
| 58 | fViewport.fBottom = 0; |
| 59 | fViewport.fWidth = desc.fWidth; |
| 60 | fViewport.fHeight = desc.fHeight; |
| 61 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 62 | fNumSamplesOwnedPerPixel = this->totalSamples(); |
senorblanco | d298121 | 2015-04-30 12:06:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 65 | sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu, |
| 66 | const GrSurfaceDesc& desc, |
| 67 | const IDDesc& idDesc, |
| 68 | int stencilBits) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 69 | 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 Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 77 | // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 78 | sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight, |
| 79 | desc.fSampleCnt, format); |
| 80 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 81 | return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 84 | GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const { |
| 85 | GrGLFramebufferInfo fbi; |
| 86 | fbi.fFBOID = fRTFBOID; |
| 87 | fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config()); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 88 | int numStencilBits = 0; |
| 89 | if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) { |
| 90 | numStencilBits = stencil->bits(); |
| 91 | } |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 92 | |
Greg Daniel | 8a3f55c | 2018-03-14 17:32:12 +0000 | [diff] [blame] | 93 | return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(), |
| 94 | numStencilBits, fbi); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 95 | } |
| 96 | |
senorblanco | d298121 | 2015-04-30 12:06:10 -0700 | [diff] [blame] | 97 | size_t GrGLRenderTarget::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 98 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 99 | fNumSamplesOwnedPerPixel, GrMipMapped::kNo); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 100 | } |
| 101 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 102 | bool GrGLRenderTarget::completeStencilAttachment() { |
| 103 | GrGLGpu* gpu = this->getGLGpu(); |
| 104 | const GrGLInterface* interface = gpu->glInterface(); |
| 105 | GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 106 | if (nullptr == stencil) { |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 107 | 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)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 113 | #ifdef SK_DEBUG |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 114 | 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 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 121 | #endif |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 122 | 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 |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 144 | 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 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 151 | #endif |
| 152 | return true; |
| 153 | } |
| 154 | } |
| 155 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 156 | void GrGLRenderTarget::onRelease() { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 157 | if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 158 | if (fTexFBOID) { |
| 159 | GL_CALL(DeleteFramebuffers(1, &fTexFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 160 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 161 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
| 162 | GL_CALL(DeleteFramebuffers(1, &fRTFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 163 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 164 | if (fMSColorRenderbufferID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 165 | GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 168 | fRTFBOID = 0; |
| 169 | fTexFBOID = 0; |
| 170 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 171 | INHERITED::onRelease(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void GrGLRenderTarget::onAbandon() { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 175 | fRTFBOID = 0; |
| 176 | fTexFBOID = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 177 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 178 | INHERITED::onAbandon(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 179 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 180 | |
| 181 | GrGLGpu* GrGLRenderTarget::getGLGpu() const { |
| 182 | SkASSERT(!this->wasDestroyed()); |
| 183 | return static_cast<GrGLGpu*>(this->getGpu()); |
| 184 | } |
| 185 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 186 | bool GrGLRenderTarget::canAttemptStencilAttachment() const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 187 | if (this->getGpu()->getContext()->caps()->avoidStencilBuffers()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 191 | // 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; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 195 | } |
| 196 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 197 | void 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 Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 204 | size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 205 | this->msaaSamples(), GrMipMapped::kNo); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 206 | |
| 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 Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 210 | dumpName.appendU32(this->uniqueID().asUInt()); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 211 | 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 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 226 | int 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 Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 230 | return this->numStencilSamples(); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 231 | } |
| 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 | |
| 238 | int 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 | } |