blob: 27713b2dd26010adc187eb452adc4bd5a027620b [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
jvanverth39edf762014-12-22 11:44:19 -080010#include "GrGLGpu.h"
egdanielec00d942015-09-14 12:56:10 -070011#include "GrGLUtil.h"
kkinnunen2e6055b2016-04-22 01:48:29 -070012#include "GrGpuResourcePriv.h"
13#include "GrRenderTargetPriv.h"
ericrk0a5fa482015-09-15 14:16:10 -070014#include "SkTraceMemoryDump.h"
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000015
egdanield803f272015-03-18 13:01:52 -070016#define GPUGL static_cast<GrGLGpu*>(this->getGpu())
17#define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
bsalomon@google.com0b77d682011-08-19 13:28:54 +000018
bsalomon37dd3312014-11-03 08:47:23 -080019// Because this class is virtually derived from GrSurface we must explicitly call its constructor.
kkinnunen2e6055b2016-04-22 01:48:29 -070020// Constructor for wrapped render targets.
egdanielec00d942015-09-14 12:56:10 -070021GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
22 const GrSurfaceDesc& desc,
23 const IDDesc& idDesc,
24 GrGLStencilAttachment* stencil)
kkinnunen2e6055b2016-04-22 01:48:29 -070025 : GrSurface(gpu, desc)
26 , INHERITED(gpu, desc, idDesc.fSampleConfig, stencil) {
bsalomon37dd3312014-11-03 08:47:23 -080027 this->init(desc, idDesc);
kkinnunen2e6055b2016-04-22 01:48:29 -070028 this->registerWithCacheWrapped();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +000029}
30
kkinnunen2e6055b2016-04-22 01:48:29 -070031GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc,
32 const IDDesc& idDesc)
33 : GrSurface(gpu, desc)
34 , INHERITED(gpu, desc, idDesc.fSampleConfig) {
bsalomon37dd3312014-11-03 08:47:23 -080035 this->init(desc, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000036}
37
bsalomon37dd3312014-11-03 08:47:23 -080038void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080039 fRTFBOID = idDesc.fRTFBOID;
40 fTexFBOID = idDesc.fTexFBOID;
41 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070042 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080043
44 fViewport.fLeft = 0;
45 fViewport.fBottom = 0;
46 fViewport.fWidth = desc.fWidth;
47 fViewport.fHeight = desc.fHeight;
48
ericrk0a5fa482015-09-15 14:16:10 -070049 fGpuMemorySize = this->totalSamples() * this->totalBytesPerSample();
robertphillips6e83ac72015-08-13 05:19:14 -070050
51 SkASSERT(fGpuMemorySize <= WorseCaseSize(desc));
senorblancod2981212015-04-30 12:06:10 -070052}
53
egdanielec00d942015-09-14 12:56:10 -070054GrGLRenderTarget* 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
senorblancod2981212015-04-30 12:06:10 -070073size_t GrGLRenderTarget::onGpuMemorySize() const {
74 return fGpuMemorySize;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000075}
76
egdanielec00d942015-09-14 12:56:10 -070077bool GrGLRenderTarget::completeStencilAttachment() {
78 GrGLGpu* gpu = this->getGLGpu();
79 const GrGLInterface* interface = gpu->glInterface();
80 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
81 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -070082 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));
egdanielec00d942015-09-14 12:56:10 -070088#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -070089 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 }
egdanielec00d942015-09-14 12:56:10 -070096#endif
egdanielec00d942015-09-14 12:56:10 -070097 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
robertphillipsfa7ff472016-04-21 11:27:43 -0700119 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 }
egdanielec00d942015-09-14 12:56:10 -0700126#endif
127 return true;
128 }
129}
130
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000131void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700132 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
egdanield803f272015-03-18 13:01:52 -0700133 if (fTexFBOID) {
134 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000135 }
egdanield803f272015-03-18 13:01:52 -0700136 if (fRTFBOID && fRTFBOID != fTexFBOID) {
137 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000138 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000139 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000140 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000141 }
142 }
egdanield803f272015-03-18 13:01:52 -0700143 fRTFBOID = 0;
144 fTexFBOID = 0;
145 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000146 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000147}
148
149void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700150 fRTFBOID = 0;
151 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000152 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000153 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000154}
egdanielec00d942015-09-14 12:56:10 -0700155
156GrGLGpu* GrGLRenderTarget::getGLGpu() const {
157 SkASSERT(!this->wasDestroyed());
158 return static_cast<GrGLGpu*>(this->getGpu());
159}
160
kkinnunen2e6055b2016-04-22 01:48:29 -0700161bool GrGLRenderTarget::canAttemptStencilAttachment() const {
ericrkc4025182016-05-04 12:01:58 -0700162 // 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;
kkinnunen2e6055b2016-04-22 01:48:29 -0700166}
167
ericrk0a5fa482015-09-15 14:16:10 -0700168void 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
196size_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
205int 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
217int 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}