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 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 10 | #include "GrGLGpu.h" |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 11 | #include "GrGLUtil.h" |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
| 13 | #include "GrRenderTargetPriv.h" |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 14 | #include "SkTraceMemoryDump.h" |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 15 | |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 16 | #define GPUGL static_cast<GrGLGpu*>(this->getGpu()) |
| 17 | #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X) |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 18 | |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 19 | // 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] | 20 | // Constructor for wrapped render targets. |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 21 | GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, |
| 22 | const GrSurfaceDesc& desc, |
| 23 | const IDDesc& idDesc, |
| 24 | GrGLStencilAttachment* stencil) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 25 | : GrSurface(gpu, desc) |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 26 | , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc), stencil) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 27 | this->init(desc, idDesc); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 28 | this->registerWithCacheWrapped(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 29 | } |
| 30 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 31 | GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, |
| 32 | const IDDesc& idDesc) |
| 33 | : GrSurface(gpu, desc) |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 34 | , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc)) { |
bsalomon | 37dd331 | 2014-11-03 08:47:23 -0800 | [diff] [blame] | 35 | this->init(desc, idDesc); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 36 | } |
| 37 | |
csmartdalton | f963599 | 2016-08-10 11:09:07 -0700 | [diff] [blame] | 38 | inline GrRenderTarget::Flags GrGLRenderTarget::ComputeFlags(const GrGLCaps& glCaps, |
| 39 | const IDDesc& idDesc) { |
| 40 | Flags flags = Flags::kNone; |
| 41 | if (idDesc.fIsMixedSampled) { |
| 42 | SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled. |
| 43 | flags |= Flags::kMixedSampled; |
| 44 | } |
| 45 | if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) { |
| 46 | flags |= Flags::kWindowRectsSupport; |
| 47 | } |
| 48 | return flags; |
| 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; |
| 77 | // Owndership of sb is passed to the GrRenderTarget so doesn't need to be deleted |
| 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 | |
senorblanco | d298121 | 2015-04-30 12:06:10 -0700 | [diff] [blame] | 84 | size_t GrGLRenderTarget::onGpuMemorySize() const { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 85 | return GrSurface::ComputeSize(fDesc, fNumSamplesOwnedPerPixel, false); |
bsalomon@google.com | 5bfc217 | 2011-07-29 20:29:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 88 | bool GrGLRenderTarget::completeStencilAttachment() { |
| 89 | GrGLGpu* gpu = this->getGLGpu(); |
| 90 | const GrGLInterface* interface = gpu->glInterface(); |
| 91 | GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment(); |
| 92 | if (nullptr == stencil) { |
egdaniel | 79bd2ae | 2015-09-15 08:46:13 -0700 | [diff] [blame] | 93 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 94 | GR_GL_STENCIL_ATTACHMENT, |
| 95 | GR_GL_RENDERBUFFER, 0)); |
| 96 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 97 | GR_GL_DEPTH_ATTACHMENT, |
| 98 | GR_GL_RENDERBUFFER, 0)); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 99 | #ifdef SK_DEBUG |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 100 | if (kChromium_GrGLDriver != gpu->glContext().driver()) { |
| 101 | // This check can cause problems in Chromium if the context has been asynchronously |
| 102 | // abandoned (see skbug.com/5200) |
| 103 | GrGLenum status; |
| 104 | GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 105 | SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 106 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 107 | #endif |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 108 | return true; |
| 109 | } else { |
| 110 | const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil); |
| 111 | GrGLuint rb = glStencil->renderbufferID(); |
| 112 | |
| 113 | gpu->invalidateBoundRenderTarget(); |
| 114 | gpu->stats()->incRenderTargetBinds(); |
| 115 | GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID())); |
| 116 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 117 | GR_GL_STENCIL_ATTACHMENT, |
| 118 | GR_GL_RENDERBUFFER, rb)); |
| 119 | if (glStencil->format().fPacked) { |
| 120 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 121 | GR_GL_DEPTH_ATTACHMENT, |
| 122 | GR_GL_RENDERBUFFER, rb)); |
| 123 | } else { |
| 124 | GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, |
| 125 | GR_GL_DEPTH_ATTACHMENT, |
| 126 | GR_GL_RENDERBUFFER, 0)); |
| 127 | } |
| 128 | |
| 129 | #ifdef SK_DEBUG |
robertphillips | fa7ff47 | 2016-04-21 11:27:43 -0700 | [diff] [blame] | 130 | if (kChromium_GrGLDriver != gpu->glContext().driver()) { |
| 131 | // This check can cause problems in Chromium if the context has been asynchronously |
| 132 | // abandoned (see skbug.com/5200) |
| 133 | GrGLenum status; |
| 134 | GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); |
| 135 | SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status); |
| 136 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 137 | #endif |
| 138 | return true; |
| 139 | } |
| 140 | } |
| 141 | |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 142 | void GrGLRenderTarget::onRelease() { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 143 | if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 144 | if (fTexFBOID) { |
| 145 | GL_CALL(DeleteFramebuffers(1, &fTexFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 146 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 147 | if (fRTFBOID && fRTFBOID != fTexFBOID) { |
| 148 | GL_CALL(DeleteFramebuffers(1, &fRTFBOID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 149 | } |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 150 | if (fMSColorRenderbufferID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 151 | GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID)); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 154 | fRTFBOID = 0; |
| 155 | fTexFBOID = 0; |
| 156 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 157 | INHERITED::onRelease(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void GrGLRenderTarget::onAbandon() { |
egdaniel | d803f27 | 2015-03-18 13:01:52 -0700 | [diff] [blame] | 161 | fRTFBOID = 0; |
| 162 | fTexFBOID = 0; |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 163 | fMSColorRenderbufferID = 0; |
robertphillips@google.com | d6bbbf8 | 2012-09-05 15:46:34 +0000 | [diff] [blame] | 164 | INHERITED::onAbandon(); |
bsalomon@google.com | aa5b673 | 2011-07-29 15:13:20 +0000 | [diff] [blame] | 165 | } |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 166 | |
| 167 | GrGLGpu* GrGLRenderTarget::getGLGpu() const { |
| 168 | SkASSERT(!this->wasDestroyed()); |
| 169 | return static_cast<GrGLGpu*>(this->getGpu()); |
| 170 | } |
| 171 | |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 172 | bool GrGLRenderTarget::canAttemptStencilAttachment() const { |
ericrk | c402518 | 2016-05-04 12:01:58 -0700 | [diff] [blame] | 173 | // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently |
| 174 | // allow for borrowed FBO ownership, so we can safely assume that if an object is owned, |
| 175 | // Skia created it. |
| 176 | return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 177 | } |
| 178 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 179 | void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const { |
| 180 | // Don't log the backing texture's contribution to the memory size. This will be handled by the |
| 181 | // texture object. |
| 182 | |
| 183 | // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer |
| 184 | // (have a fMSColorRenderbufferID). |
| 185 | if (fMSColorRenderbufferID) { |
Robert Phillips | d6214d4 | 2016-11-07 08:23:48 -0500 | [diff] [blame] | 186 | size_t size = GrSurface::ComputeSize(fDesc, this->msaaSamples(), false); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 187 | |
| 188 | // Due to this resource having both a texture and a renderbuffer component, dump as |
| 189 | // skia/gpu_resources/resource_#/renderbuffer |
| 190 | SkString dumpName("skia/gpu_resources/resource_"); |
Robert Phillips | 294870f | 2016-11-11 12:38:40 -0500 | [diff] [blame] | 191 | dumpName.appendU32(this->uniqueID().asUInt()); |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 192 | dumpName.append("/renderbuffer"); |
| 193 | |
| 194 | traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size); |
| 195 | |
| 196 | if (this->isPurgeable()) { |
| 197 | traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size); |
| 198 | } |
| 199 | |
| 200 | SkString renderbuffer_id; |
| 201 | renderbuffer_id.appendU32(fMSColorRenderbufferID); |
| 202 | traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer", |
| 203 | renderbuffer_id.c_str()); |
| 204 | } |
| 205 | } |
| 206 | |
ericrk | 0a5fa48 | 2015-09-15 14:16:10 -0700 | [diff] [blame] | 207 | int GrGLRenderTarget::msaaSamples() const { |
| 208 | if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) { |
| 209 | // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own |
| 210 | // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count. |
| 211 | return SkTMax(1, fDesc.fSampleCnt); |
| 212 | } |
| 213 | |
| 214 | // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use |
| 215 | // 0 for the sample count. |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | int GrGLRenderTarget::totalSamples() const { |
| 220 | int total_samples = this->msaaSamples(); |
| 221 | |
| 222 | if (fTexFBOID != kUnresolvableFBOID) { |
| 223 | // If we own the resolve buffer then that is one more sample per pixel. |
| 224 | total_samples += 1; |
| 225 | } |
| 226 | |
| 227 | return total_samples; |
| 228 | } |