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