• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "GrGLRenderTarget.h"
9 #include "GrContext.h"
10 #include "GrContextPriv.h"
11 #include "GrGLGpu.h"
12 #include "GrGLUtil.h"
13 #include "GrGpuResourcePriv.h"
14 #include "GrRenderTargetPriv.h"
15 #include "SkTraceMemoryDump.h"
16 
17 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
18 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
19 
20 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
21 // Constructor for wrapped render targets.
GrGLRenderTarget(GrGLGpu * gpu,const GrSurfaceDesc & desc,GrGLenum format,const IDDesc & idDesc,GrGLStencilAttachment * stencil)22 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
23                                    const GrSurfaceDesc& desc,
24                                    GrGLenum format,
25                                    const IDDesc& idDesc,
26                                    GrGLStencilAttachment* stencil)
27     : GrSurface(gpu, desc)
28     , INHERITED(gpu, desc, stencil) {
29     this->setFlags(gpu->glCaps(), idDesc);
30     this->init(desc, format, idDesc);
31     this->registerWithCacheWrapped(GrWrapCacheable::kNo);
32 }
33 
GrGLRenderTarget(GrGLGpu * gpu,const GrSurfaceDesc & desc,GrGLenum format,const IDDesc & idDesc)34 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc, GrGLenum format,
35                                    const IDDesc& idDesc)
36     : GrSurface(gpu, desc)
37     , INHERITED(gpu, desc) {
38     this->setFlags(gpu->glCaps(), idDesc);
39     this->init(desc, format, idDesc);
40 }
41 
setFlags(const GrGLCaps & glCaps,const IDDesc & idDesc)42 inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDDesc& idDesc) {
43     if (idDesc.fIsMixedSampled) {
44         SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled.
45         this->setHasMixedSamples();
46     }
47     if (!idDesc.fRTFBOID) {
48         this->setGLRTFBOIDIs0();
49     }
50 }
51 
init(const GrSurfaceDesc & desc,GrGLenum format,const IDDesc & idDesc)52 void GrGLRenderTarget::init(const GrSurfaceDesc& desc, GrGLenum format, const IDDesc& idDesc) {
53     fRTFBOID                = idDesc.fRTFBOID;
54     fTexFBOID               = idDesc.fTexFBOID;
55     fMSColorRenderbufferID  = idDesc.fMSColorRenderbufferID;
56     fRTFBOOwnership         = idDesc.fRTFBOOwnership;
57 
58     fRTFormat               = format;
59 
60     fViewport.fLeft   = 0;
61     fViewport.fBottom = 0;
62     fViewport.fWidth  = desc.fWidth;
63     fViewport.fHeight = desc.fHeight;
64 
65     fNumSamplesOwnedPerPixel = this->totalSamples();
66 }
67 
MakeWrapped(GrGLGpu * gpu,const GrSurfaceDesc & desc,GrGLenum format,const IDDesc & idDesc,int stencilBits)68 sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
69                                                       const GrSurfaceDesc& desc,
70                                                       GrGLenum format,
71                                                       const IDDesc& idDesc,
72                                                       int stencilBits) {
73     GrGLStencilAttachment* sb = nullptr;
74     if (stencilBits) {
75         GrGLStencilAttachment::IDDesc sbDesc;
76         GrGLStencilAttachment::Format format;
77         format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
78         format.fPacked = false;
79         format.fStencilBits = stencilBits;
80         format.fTotalBits = stencilBits;
81         // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
82         sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
83                                        desc.fSampleCnt, format);
84     }
85     return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, format, idDesc, sb));
86 }
87 
getBackendRenderTarget() const88 GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
89     GrGLFramebufferInfo fbi;
90     fbi.fFBOID = fRTFBOID;
91     fbi.fFormat = this->getGLGpu()->glCaps().configSizedInternalFormat(this->config());
92     int numStencilBits = 0;
93     if (GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment()) {
94         numStencilBits = stencil->bits();
95     }
96 
97     return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
98                                  numStencilBits, fbi);
99 }
100 
backendFormat() const101 GrBackendFormat GrGLRenderTarget::backendFormat() const {
102     // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
103     // texture 2D.
104     return GrBackendFormat::MakeGL(fRTFormat, GR_GL_TEXTURE_2D);
105 }
106 
onGpuMemorySize() const107 size_t GrGLRenderTarget::onGpuMemorySize() const {
108     return GrSurface::ComputeSize(this->config(), this->width(), this->height(),
109                                   fNumSamplesOwnedPerPixel, GrMipMapped::kNo);
110 }
111 
completeStencilAttachment()112 bool GrGLRenderTarget::completeStencilAttachment() {
113     GrGLGpu* gpu = this->getGLGpu();
114     const GrGLInterface* interface = gpu->glInterface();
115     GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
116     if (nullptr == stencil) {
117         GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
118                                                       GR_GL_STENCIL_ATTACHMENT,
119                                                       GR_GL_RENDERBUFFER, 0));
120         GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
121                                                       GR_GL_DEPTH_ATTACHMENT,
122                                                       GR_GL_RENDERBUFFER, 0));
123 #ifdef SK_DEBUG
124         if (kChromium_GrGLDriver != gpu->glContext().driver()) {
125             // This check can cause problems in Chromium if the context has been asynchronously
126             // abandoned (see skbug.com/5200)
127             GrGLenum status;
128             GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
129             SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
130         }
131 #endif
132         return true;
133     } else {
134         const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
135         GrGLuint rb = glStencil->renderbufferID();
136 
137         gpu->invalidateBoundRenderTarget();
138         gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID());
139         GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
140                                                       GR_GL_STENCIL_ATTACHMENT,
141                                                       GR_GL_RENDERBUFFER, rb));
142         if (glStencil->format().fPacked) {
143             GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
144                                                           GR_GL_DEPTH_ATTACHMENT,
145                                                           GR_GL_RENDERBUFFER, rb));
146         } else {
147             GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
148                                                           GR_GL_DEPTH_ATTACHMENT,
149                                                           GR_GL_RENDERBUFFER, 0));
150         }
151 
152 
153 #ifdef SK_DEBUG
154         if (kChromium_GrGLDriver != gpu->glContext().driver()) {
155             // This check can cause problems in Chromium if the context has been asynchronously
156             // abandoned (see skbug.com/5200)
157             GrGLenum status;
158             GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
159             SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
160         }
161 #endif
162         return true;
163     }
164 }
165 
onRelease()166 void GrGLRenderTarget::onRelease() {
167     if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
168         GrGLGpu* gpu = this->getGLGpu();
169         if (fTexFBOID) {
170             gpu->deleteFramebuffer(fTexFBOID);
171         }
172         if (fRTFBOID && fRTFBOID != fTexFBOID) {
173             gpu->deleteFramebuffer(fRTFBOID);
174         }
175         if (fMSColorRenderbufferID) {
176             GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
177         }
178     }
179     fRTFBOID                = 0;
180     fTexFBOID               = 0;
181     fMSColorRenderbufferID  = 0;
182     INHERITED::onRelease();
183 }
184 
onAbandon()185 void GrGLRenderTarget::onAbandon() {
186     fRTFBOID                = 0;
187     fTexFBOID               = 0;
188     fMSColorRenderbufferID  = 0;
189     INHERITED::onAbandon();
190 }
191 
getGLGpu() const192 GrGLGpu* GrGLRenderTarget::getGLGpu() const {
193     SkASSERT(!this->wasDestroyed());
194     return static_cast<GrGLGpu*>(this->getGpu());
195 }
196 
canAttemptStencilAttachment() const197 bool GrGLRenderTarget::canAttemptStencilAttachment() const {
198     if (this->getGpu()->getContext()->contextPriv().caps()->avoidStencilBuffers()) {
199         return false;
200     }
201 
202     // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
203     // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
204     // Skia created it.
205     return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
206 }
207 
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const208 void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
209     // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
210     // which is multiply inherited from both ourselves and a texture. In these cases, one part
211     // (texture, rt) may be wrapped, while the other is owned by Skia.
212     bool refsWrappedRenderTargetObjects =
213             this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
214     if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
215         return;
216     }
217 
218     // Don't log the framebuffer, as the framebuffer itself doesn't contribute to meaningful
219     // memory usage. It is always a wrapper around either:
220     // - a texture, which is owned elsewhere, and will be dumped there
221     // - a renderbuffer, which will be dumped below.
222 
223     // Log any renderbuffer's contribution to memory.
224     if (fMSColorRenderbufferID) {
225         size_t size = GrSurface::ComputeSize(this->config(), this->width(), this->height(),
226                                              this->msaaSamples(), GrMipMapped::kNo);
227 
228         // Due to this resource having both a texture and a renderbuffer component, dump as
229         // skia/gpu_resources/resource_#/renderbuffer
230         SkString resourceName = this->getResourceName();
231         resourceName.append("/renderbuffer");
232 
233         this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
234 
235         SkString renderbuffer_id;
236         renderbuffer_id.appendU32(fMSColorRenderbufferID);
237         traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
238                                           renderbuffer_id.c_str());
239     }
240 }
241 
msaaSamples() const242 int GrGLRenderTarget::msaaSamples() const {
243     if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
244         // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
245         // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
246         return this->numStencilSamples();
247     }
248 
249     // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
250     // 0 for the sample count.
251     return 0;
252 }
253 
totalSamples() const254 int GrGLRenderTarget::totalSamples() const {
255   int total_samples = this->msaaSamples();
256 
257   if (fTexFBOID != kUnresolvableFBOID) {
258       // If we own the resolve buffer then that is one more sample per pixel.
259       total_samples += 1;
260   }
261 
262   return total_samples;
263 }
264