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) |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 27 | , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc), stencil) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 28 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 29 | this->registerWithCacheWrapped(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 30 | } |
| 31 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 32 | GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, |
| 33 | const IDDesc& idDesc) |
| 34 | : GrSurface(gpu, desc) |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 35 | , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc)) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 36 | this->init(desc, idDesc); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Robert Phillips | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 39 | inline GrRenderTargetFlags GrGLRenderTarget::ComputeFlags(const GrGLCaps& glCaps, |
| 40 | const IDDesc& idDesc) { |
| 41 | GrRenderTargetFlags flags = GrRenderTargetFlags::kNone; |
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 | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 44 | flags |= GrRenderTargetFlags::kMixedSampled; |
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 | c4f0a82 | 2017-06-13 08:11:36 -0400 | [diff] [blame] | 47 | flags |= GrRenderTargetFlags::kWindowRectsSupport; |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 48 | } |
| 49 | return flags; |
| 50 | } |
| 51 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 52 | void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) { |
cdalton | d472792 | 2015-11-10 12:49:06 -0800 | [diff] [blame] | 53 | fRTFBOID = idDesc.fRTFBOID; |
| 54 | fTexFBOID = idDesc.fTexFBOID; |
| 55 | fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 56 | fRTFBOOwnership = idDesc.fRTFBOOwnership; |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 57 | |
| 58 | fViewport.fLeft = 0; |
| 59 | fViewport.fBottom = 0; |
| 60 | fViewport.fWidth = desc.fWidth; |
| 61 | fViewport.fHeight = desc.fHeight; |
| 62 | |
Robert Phillips | 29e52f1 | 2016-11-03 10:19:14 -0400 | [diff] [blame] | 63 | fNumSamplesOwnedPerPixel = this->totalSamples(); |
senorblanco | d298121 | 2015-04-30 12:06:10 -0700 | [diff] [blame] | 64 | } |
| 65 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 66 | sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu, |
| 67 | const GrSurfaceDesc& desc, |
| 68 | const IDDesc& idDesc, |
| 69 | int stencilBits) { |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 70 | 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 Phillips | cb2e235 | 2017-08-30 16:44:40 -0400 | [diff] [blame] | 78 | // 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] | 79 | sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight, |
| 80 | desc.fSampleCnt, format); |
| 81 | } |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 82 | return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 85 | GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const { |
| 86 | GrGLFramebufferInfo fbi; |
| 87 | fbi.fFBOID = fRTFBOID; |
| 88 | fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config()); |
| 89 | |
| 90 | return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(), |
| 91 | this->numStencilSamples(), fbi); |
| 92 | } |
| 93 | |
senorblanco | d298121 | 2015-04-30 12:06:10 -0700 | [diff] [blame] | 94 | size_t GrGLRenderTarget::onGpuMemorySize() const { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 95 | return GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 96 | fNumSamplesOwnedPerPixel, GrMipMapped::kNo); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 97 | } |
| 98 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 99 | bool GrGLRenderTarget::completeStencilAttachment() { |
| 100 | GrGLGpu* gpu = this->getGLGpu(); |
| 101 | const GrGLInterface* interface = gpu->glInterface(); |
| 102 | GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 103 | if (nullptr == stencil) { |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 104 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 105 | GR_GL_STENCIL_ATTACHMENT, |
| 106 | GR_GL_RENDERBUFFER, 0)); |
| 107 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 108 | GR_GL_DEPTH_ATTACHMENT, |
| 109 | GR_GL_RENDERBUFFER, 0)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 110 | #ifdef SK_DEBUG |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 111 | if (kChromium_GrGLDriver != gpu->glContext().driver()) { |
| 112 | // This check can cause problems in Chromium if the context has been asynchronously |
| 113 | // abandoned (see skbug.com/5200) |
| 114 | GrGLenum status; |
| 115 | GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 116 | SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 117 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 118 | #endif |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 119 | return true; |
| 120 | } else { |
| 121 | const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil); |
| 122 | GrGLuint rb = glStencil->renderbufferID(); |
| 123 | |
| 124 | gpu->invalidateBoundRenderTarget(); |
| 125 | gpu->stats()->incRenderTargetBinds(); |
| 126 | GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID())); |
| 127 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 128 | GR_GL_STENCIL_ATTACHMENT, |
| 129 | GR_GL_RENDERBUFFER, rb)); |
| 130 | if (glStencil->format().fPacked) { |
| 131 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 132 | GR_GL_DEPTH_ATTACHMENT, |
| 133 | GR_GL_RENDERBUFFER, rb)); |
| 134 | } else { |
| 135 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 136 | GR_GL_DEPTH_ATTACHMENT, |
| 137 | GR_GL_RENDERBUFFER, 0)); |
| 138 | } |
| 139 | |
| 140 | #ifdef SK_DEBUG |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 141 | if (kChromium_GrGLDriver != gpu->glContext().driver()) { |
| 142 | // This check can cause problems in Chromium if the context has been asynchronously |
| 143 | // abandoned (see skbug.com/5200) |
| 144 | GrGLenum status; |
| 145 | GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 146 | SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 147 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 148 | #endif |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 153 | void GrGLRenderTarget::onRelease() { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 154 | if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 155 | if (fTexFBOID) { |
| 156 | GL_CALL(DeleteFramebuffers(1, &fTexFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 157 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 158 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
| 159 | GL_CALL(DeleteFramebuffers(1, &fRTFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 160 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 161 | if (fMSColorRenderbufferID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 162 | GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 165 | fRTFBOID = 0; |
| 166 | fTexFBOID = 0; |
| 167 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 168 | INHERITED::onRelease(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void GrGLRenderTarget::onAbandon() { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 172 | fRTFBOID = 0; |
| 173 | fTexFBOID = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 174 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 175 | INHERITED::onAbandon(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 176 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 177 | |
| 178 | GrGLGpu* GrGLRenderTarget::getGLGpu() const { |
| 179 | SkASSERT(!this->wasDestroyed()); |
| 180 | return static_cast<GrGLGpu*>(this->getGpu()); |
| 181 | } |
| 182 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 183 | bool GrGLRenderTarget::canAttemptStencilAttachment() const { |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 184 | if (this->getGpu()->getContext()->caps()->avoidStencilBuffers()) { |
| 185 | return false; |
| 186 | } |
| 187 | |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 188 | // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently |
| 189 | // allow for borrowed FBO ownership, so we can safely assume that if an object is owned, |
| 190 | // Skia created it. |
| 191 | return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 192 | } |
| 193 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 194 | void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 195 | // Don't log the backing texture's contribution to the memory size. This will be handled by the |
| 196 | // texture object. |
| 197 | |
| 198 | // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer |
| 199 | // (have a fMSColorRenderbufferID). |
| 200 | if (fMSColorRenderbufferID) { |
Brian Salomon | bb5711a | 2017-05-17 13:49:59 -0400 | [diff] [blame] | 201 | size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(), |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 202 | this->msaaSamples(), GrMipMapped::kNo); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 203 | |
| 204 | // Due to this resource having both a texture and a renderbuffer component, dump as |
| 205 | // skia/gpu_resources/resource_#/renderbuffer |
| 206 | SkString dumpName("skia/gpu_resources/resource_"); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 207 | dumpName.appendU32(this->uniqueID().asUInt()); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 208 | dumpName.append("/renderbuffer"); |
| 209 | |
| 210 | traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size); |
| 211 | |
| 212 | if (this->isPurgeable()) { |
| 213 | traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size); |
| 214 | } |
| 215 | |
| 216 | SkString renderbuffer_id; |
| 217 | renderbuffer_id.appendU32(fMSColorRenderbufferID); |
| 218 | traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer", |
| 219 | renderbuffer_id.c_str()); |
| 220 | } |
| 221 | } |
| 222 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 223 | int GrGLRenderTarget::msaaSamples() const { |
| 224 | if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) { |
| 225 | // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own |
| 226 | // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count. |
Brian Salomon | 18c52a7 | 2018-02-02 06:53:26 -0500 | [diff] [blame^] | 227 | return SkTMax(1, this->numStencilSamples()); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use |
| 231 | // 0 for the sample count. |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | int GrGLRenderTarget::totalSamples() const { |
| 236 | int total_samples = this->msaaSamples(); |
| 237 | |
| 238 | if (fTexFBOID != kUnresolvableFBOID) { |
| 239 | // If we own the resolve buffer then that is one more sample per pixel. |
| 240 | total_samples += 1; |
| 241 | } |
| 242 | |
| 243 | return total_samples; |
| 244 | } |