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