blob: d4585f6402f926441d3893b632c1c1ebcaafe005 [file] [log] [blame]
bsalomon@google.comaa5b6732011-07-29 15:13:20 +00001/*
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.comaa5b6732011-07-29 15:13:20 +00008#include "GrGLRenderTarget.h"
9
egdanielec00d942015-09-14 12:56:10 -070010#include "GrRenderTargetPriv.h"
jvanverth39edf762014-12-22 11:44:19 -080011#include "GrGLGpu.h"
egdanielec00d942015-09-14 12:56:10 -070012#include "GrGLUtil.h"
ericrk0a5fa482015-09-15 14:16:10 -070013#include "SkTraceMemoryDump.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000014
egdanield803f272015-03-18 13:01:52 -070015#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
16#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000017
bsalomon37dd3312014-11-03 08:47:23 -080018// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
egdanielec00d942015-09-14 12:56:10 -070019GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
20 const GrSurfaceDesc& desc,
21 const IDDesc& idDesc,
22 GrGLStencilAttachment* stencil)
bsalomon5236cf42015-01-14 10:42:08 -080023 : GrSurface(gpu, idDesc.fLifeCycle, desc)
egdanielec00d942015-09-14 12:56:10 -070024 , INHERITED(gpu, idDesc.fLifeCycle, desc, idDesc.fSampleConfig, stencil) {
bsalomon37dd3312014-11-03 08:47:23 -080025 this->init(desc, idDesc);
bsalomon16961262014-08-26 14:01:07 -070026 this->registerWithCache();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000027}
28
bsalomon861e1032014-12-16 07:33:49 -080029GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, const IDDesc& idDesc,
bsalomon37dd3312014-11-03 08:47:23 -080030 Derived)
bsalomon5236cf42015-01-14 10:42:08 -080031 : GrSurface(gpu, idDesc.fLifeCycle, desc)
vbuzinovdded6962015-06-12 08:59:45 -070032 , INHERITED(gpu, idDesc.fLifeCycle, desc, idDesc.fSampleConfig) {
bsalomon37dd3312014-11-03 08:47:23 -080033 this->init(desc, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000034}
35
bsalomon37dd3312014-11-03 08:47:23 -080036void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080037 fRTFBOID = idDesc.fRTFBOID;
38 fTexFBOID = idDesc.fTexFBOID;
39 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
40 fRTLifecycle = idDesc.fLifeCycle;
bsalomon37dd3312014-11-03 08:47:23 -080041
42 fViewport.fLeft = 0;
43 fViewport.fBottom = 0;
44 fViewport.fWidth = desc.fWidth;
45 fViewport.fHeight = desc.fHeight;
46
ericrk0a5fa482015-09-15 14:16:10 -070047 fGpuMemorySize = this->totalSamples() * this->totalBytesPerSample();
robertphillips6e83ac72015-08-13 05:19:14 -070048
49 SkASSERT(fGpuMemorySize <= WorseCaseSize(desc));
senorblancod2981212015-04-30 12:06:10 -070050}
51
egdanielec00d942015-09-14 12:56:10 -070052GrGLRenderTarget* 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
senorblancod2981212015-04-30 12:06:10 -070071size_t GrGLRenderTarget::onGpuMemorySize() const {
72 return fGpuMemorySize;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000073}
74
egdanielec00d942015-09-14 12:56:10 -070075bool GrGLRenderTarget::completeStencilAttachment() {
76 GrGLGpu* gpu = this->getGLGpu();
77 const GrGLInterface* interface = gpu->glInterface();
78 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
79 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -070080 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));
egdanielec00d942015-09-14 12:56:10 -070086#ifdef SK_DEBUG
egdaniel79bd2ae2015-09-15 08:46:13 -070087 GrGLenum status;
88 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
89 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
egdanielec00d942015-09-14 12:56:10 -070090#endif
egdanielec00d942015-09-14 12:56:10 -070091 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.comaa5b6732011-07-29 15:13:20 +0000121void GrGLRenderTarget::onRelease() {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700122 if (kBorrowed_LifeCycle != fRTLifecycle) {
egdanield803f272015-03-18 13:01:52 -0700123 if (fTexFBOID) {
124 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000125 }
egdanield803f272015-03-18 13:01:52 -0700126 if (fRTFBOID && fRTFBOID != fTexFBOID) {
127 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000128 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000129 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000130 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000131 }
132 }
egdanield803f272015-03-18 13:01:52 -0700133 fRTFBOID = 0;
134 fTexFBOID = 0;
135 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000136 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000137}
138
139void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700140 fRTFBOID = 0;
141 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000142 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000143 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000144}
egdanielec00d942015-09-14 12:56:10 -0700145
146GrGLGpu* GrGLRenderTarget::getGLGpu() const {
147 SkASSERT(!this->wasDestroyed());
148 return static_cast<GrGLGpu*>(this->getGpu());
149}
150
ericrk0a5fa482015-09-15 14:16:10 -0700151void 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
179size_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
188int 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
200int 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}