blob: f6ad3ba611af439ebf4a7403db40579aecc35768 [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)
csmartdaltonf9635992016-08-10 11:09:07 -070026 , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc), 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)
csmartdaltonf9635992016-08-10 11:09:07 -070034 , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc)) {
bsalomon37dd3312014-11-03 08:47:23 -080035 this->init(desc, idDesc);
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000036}
37
csmartdaltonf9635992016-08-10 11:09:07 -070038inline GrRenderTarget::Flags GrGLRenderTarget::ComputeFlags(const GrGLCaps& glCaps,
39 const IDDesc& idDesc) {
40 Flags flags = Flags::kNone;
41 if (idDesc.fIsMixedSampled) {
42 SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled.
43 flags |= Flags::kMixedSampled;
44 }
45 if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) {
46 flags |= Flags::kWindowRectsSupport;
47 }
48 return flags;
49}
50
bsalomon37dd3312014-11-03 08:47:23 -080051void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
cdaltond4727922015-11-10 12:49:06 -080052 fRTFBOID = idDesc.fRTFBOID;
53 fTexFBOID = idDesc.fTexFBOID;
54 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
kkinnunen2e6055b2016-04-22 01:48:29 -070055 fRTFBOOwnership = idDesc.fRTFBOOwnership;
bsalomon37dd3312014-11-03 08:47:23 -080056
57 fViewport.fLeft = 0;
58 fViewport.fBottom = 0;
59 fViewport.fWidth = desc.fWidth;
60 fViewport.fHeight = desc.fHeight;
61
ericrk0a5fa482015-09-15 14:16:10 -070062 fGpuMemorySize = this->totalSamples() * this->totalBytesPerSample();
robertphillips6e83ac72015-08-13 05:19:14 -070063
robertphillipsf1c6cd72016-08-18 07:11:13 -070064 SkASSERT(fGpuMemorySize <= WorstCaseSize(desc));
senorblancod2981212015-04-30 12:06:10 -070065}
66
egdanielec00d942015-09-14 12:56:10 -070067GrGLRenderTarget* GrGLRenderTarget::CreateWrapped(GrGLGpu* gpu,
68 const GrSurfaceDesc& desc,
69 const IDDesc& idDesc,
70 int stencilBits) {
71 GrGLStencilAttachment* sb = nullptr;
72 if (stencilBits) {
73 GrGLStencilAttachment::IDDesc sbDesc;
74 GrGLStencilAttachment::Format format;
75 format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
76 format.fPacked = false;
77 format.fStencilBits = stencilBits;
78 format.fTotalBits = stencilBits;
79 // Owndership of sb is passed to the GrRenderTarget so doesn't need to be deleted
80 sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
81 desc.fSampleCnt, format);
82 }
83 return (new GrGLRenderTarget(gpu, desc, idDesc, sb));
84}
85
senorblancod2981212015-04-30 12:06:10 -070086size_t GrGLRenderTarget::onGpuMemorySize() const {
87 return fGpuMemorySize;
bsalomon@google.com5bfc2172011-07-29 20:29:05 +000088}
89
egdanielec00d942015-09-14 12:56:10 -070090bool GrGLRenderTarget::completeStencilAttachment() {
91 GrGLGpu* gpu = this->getGLGpu();
92 const GrGLInterface* interface = gpu->glInterface();
93 GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
94 if (nullptr == stencil) {
egdaniel79bd2ae2015-09-15 08:46:13 -070095 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
96 GR_GL_STENCIL_ATTACHMENT,
97 GR_GL_RENDERBUFFER, 0));
98 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
99 GR_GL_DEPTH_ATTACHMENT,
100 GR_GL_RENDERBUFFER, 0));
egdanielec00d942015-09-14 12:56:10 -0700101#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700102 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
103 // This check can cause problems in Chromium if the context has been asynchronously
104 // abandoned (see skbug.com/5200)
105 GrGLenum status;
106 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
107 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
108 }
egdanielec00d942015-09-14 12:56:10 -0700109#endif
egdanielec00d942015-09-14 12:56:10 -0700110 return true;
111 } else {
112 const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
113 GrGLuint rb = glStencil->renderbufferID();
114
115 gpu->invalidateBoundRenderTarget();
116 gpu->stats()->incRenderTargetBinds();
117 GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID()));
118 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
119 GR_GL_STENCIL_ATTACHMENT,
120 GR_GL_RENDERBUFFER, rb));
121 if (glStencil->format().fPacked) {
122 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
123 GR_GL_DEPTH_ATTACHMENT,
124 GR_GL_RENDERBUFFER, rb));
125 } else {
126 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
127 GR_GL_DEPTH_ATTACHMENT,
128 GR_GL_RENDERBUFFER, 0));
129 }
130
131#ifdef SK_DEBUG
robertphillipsfa7ff472016-04-21 11:27:43 -0700132 if (kChromium_GrGLDriver != gpu->glContext().driver()) {
133 // This check can cause problems in Chromium if the context has been asynchronously
134 // abandoned (see skbug.com/5200)
135 GrGLenum status;
136 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
137 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
138 }
egdanielec00d942015-09-14 12:56:10 -0700139#endif
140 return true;
141 }
142}
143
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000144void GrGLRenderTarget::onRelease() {
kkinnunen2e6055b2016-04-22 01:48:29 -0700145 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
egdanield803f272015-03-18 13:01:52 -0700146 if (fTexFBOID) {
147 GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000148 }
egdanield803f272015-03-18 13:01:52 -0700149 if (fRTFBOID && fRTFBOID != fTexFBOID) {
150 GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000151 }
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000152 if (fMSColorRenderbufferID) {
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000153 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000154 }
155 }
egdanield803f272015-03-18 13:01:52 -0700156 fRTFBOID = 0;
157 fTexFBOID = 0;
158 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000159 INHERITED::onRelease();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000160}
161
162void GrGLRenderTarget::onAbandon() {
egdanield803f272015-03-18 13:01:52 -0700163 fRTFBOID = 0;
164 fTexFBOID = 0;
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000165 fMSColorRenderbufferID = 0;
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000166 INHERITED::onAbandon();
bsalomon@google.comaa5b6732011-07-29 15:13:20 +0000167}
egdanielec00d942015-09-14 12:56:10 -0700168
169GrGLGpu* GrGLRenderTarget::getGLGpu() const {
170 SkASSERT(!this->wasDestroyed());
171 return static_cast<GrGLGpu*>(this->getGpu());
172}
173
kkinnunen2e6055b2016-04-22 01:48:29 -0700174bool GrGLRenderTarget::canAttemptStencilAttachment() const {
ericrkc4025182016-05-04 12:01:58 -0700175 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
176 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
177 // Skia created it.
178 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
kkinnunen2e6055b2016-04-22 01:48:29 -0700179}
180
ericrk0a5fa482015-09-15 14:16:10 -0700181void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
182 // Don't log the backing texture's contribution to the memory size. This will be handled by the
183 // texture object.
184
185 // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer
186 // (have a fMSColorRenderbufferID).
187 if (fMSColorRenderbufferID) {
188 size_t size = this->msaaSamples() * this->totalBytesPerSample();
189
190 // Due to this resource having both a texture and a renderbuffer component, dump as
191 // skia/gpu_resources/resource_#/renderbuffer
192 SkString dumpName("skia/gpu_resources/resource_");
robertphillips8abb3702016-08-31 14:04:06 -0700193 dumpName.appendS32(this->uniqueID());
ericrk0a5fa482015-09-15 14:16:10 -0700194 dumpName.append("/renderbuffer");
195
196 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size);
197
198 if (this->isPurgeable()) {
199 traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size);
200 }
201
202 SkString renderbuffer_id;
203 renderbuffer_id.appendU32(fMSColorRenderbufferID);
204 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer",
205 renderbuffer_id.c_str());
206 }
207}
208
209size_t GrGLRenderTarget::totalBytesPerSample() const {
210 SkASSERT(kUnknown_GrPixelConfig != fDesc.fConfig);
211 SkASSERT(!GrPixelConfigIsCompressed(fDesc.fConfig));
212 size_t colorBytes = GrBytesPerPixel(fDesc.fConfig);
213 SkASSERT(colorBytes > 0);
214
215 return fDesc.fWidth * fDesc.fHeight * colorBytes;
216}
217
218int GrGLRenderTarget::msaaSamples() const {
219 if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
220 // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
221 // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
222 return SkTMax(1, fDesc.fSampleCnt);
223 }
224
225 // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
226 // 0 for the sample count.
227 return 0;
228}
229
230int GrGLRenderTarget::totalSamples() const {
231 int total_samples = this->msaaSamples();
232
233 if (fTexFBOID != kUnresolvableFBOID) {
234 // If we own the resolve buffer then that is one more sample per pixel.
235 total_samples += 1;
236 }
237
238 return total_samples;
239}