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