blob: b77ec4b44740e18b63ac5cf209d03303175ced69 [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
robertphillipsfa7ff472016-04-21 11:27:43 -070087 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
88 // This check can cause problems in Chromium if the context has been asynchronously
89 // abandoned (see skbug.com/5200)
90 GrGLenum status;
91 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
92 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
93 }
egdanielec00d942015-09-14 12:56:10 -070094#endif
egdanielec00d942015-09-14 12:56:10 -070095 return true;
96 } else {
97 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
98 GrGLuint rb = glStencil->renderbufferID();
99
100 gpu->invalidateBoundRenderTarget();
101 gpu->stats()->incRenderTargetBinds();
102 GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID()));
103 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
104 GR_GL_STENCIL_ATTACHMENT,
105 GR_GL_RENDERBUFFER, rb));
106 if (glStencil->format().fPacked) {
107 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
108 GR_GL_DEPTH_ATTACHMENT,
109 GR_GL_RENDERBUFFER, rb));
110 } else {
111 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
112 GR_GL_DEPTH_ATTACHMENT,
113 GR_GL_RENDERBUFFER, 0));
114 }
115
116#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700117 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
118 // This check can cause problems in Chromium if the context has been asynchronously
119 // abandoned (see skbug.com/5200)
120 GrGLenum status;
121 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
122 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
123 }
egdanielec00d942015-09-14 12:56:10 -0700124#endif
125 return true;
126 }
127}
128
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000129void GrGLRenderTarget::onRelease() {
bsalomon6dc6f5f2015-06-18 09:12:16 -0700130 if (kBorrowed_LifeCycle != fRTLifecycle) {
egdanield803f272015-03-18 13:01:52 -0700131 if (fTexFBOID) {
132 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000133 }
egdanield803f272015-03-18 13:01:52 -0700134 if (fRTFBOID && fRTFBOID != fTexFBOID) {
135 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000136 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000137 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000138 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000139 }
140 }
egdanield803f272015-03-18 13:01:52 -0700141 fRTFBOID = 0;
142 fTexFBOID = 0;
143 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000144 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000145}
146
147void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700148 fRTFBOID = 0;
149 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000150 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000151 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000152}
egdanielec00d942015-09-14 12:56:10 -0700153
154GrGLGpu* GrGLRenderTarget::getGLGpu() const {
155 SkASSERT(!this->wasDestroyed());
156 return static_cast<GrGLGpu*>(this->getGpu());
157}
158
ericrk0a5fa482015-09-15 14:16:10 -0700159void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
160 // Don't log the backing texture's contribution to the memory size. This will be handled by the
161 // texture object.
162
163 // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer
164 // (have a fMSColorRenderbufferID).
165 if (fMSColorRenderbufferID) {
166 size_t size = this->msaaSamples() * this->totalBytesPerSample();
167
168 // Due to this resource having both a texture and a renderbuffer component, dump as
169 // skia/gpu_resources/resource_#/renderbuffer
170 SkString dumpName("skia/gpu_resources/resource_");
171 dumpName.appendS32(this->getUniqueID());
172 dumpName.append("/renderbuffer");
173
174 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size);
175
176 if (this->isPurgeable()) {
177 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size);
178 }
179
180 SkString renderbuffer_id;
181 renderbuffer_id.appendU32(fMSColorRenderbufferID);
182 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer",
183 renderbuffer_id.c_str());
184 }
185}
186
187size_t GrGLRenderTarget::totalBytesPerSample() const {
188 SkASSERT(kUnknown_GrPixelConfig != fDesc.fConfig);
189 SkASSERT(!GrPixelConfigIsCompressed(fDesc.fConfig));
190 size_t colorBytes = GrBytesPerPixel(fDesc.fConfig);
191 SkASSERT(colorBytes > 0);
192
193 return fDesc.fWidth * fDesc.fHeight * colorBytes;
194}
195
196int GrGLRenderTarget::msaaSamples() const {
197 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
198 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
199 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
200 return SkTMax(1, fDesc.fSampleCnt);
201 }
202
203 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
204 // 0 for the sample count.
205 return 0;
206}
207
208int GrGLRenderTarget::totalSamples() const {
209 int total_samples = this->msaaSamples();
210
211 if (fTexFBOID != kUnresolvableFBOID) {
212 // If we own the resolve buffer then that is one more sample per pixel.
213 total_samples += 1;
214 }
215
216 return total_samples;
217}