Line data Source code
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 :
10 : #include "GrGLGpu.h"
11 : #include "GrGLUtil.h"
12 : #include "GrGpuResourcePriv.h"
13 : #include "GrRenderTargetPriv.h"
14 : #include "SkTraceMemoryDump.h"
15 :
16 : #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
17 : #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
18 :
19 : // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
20 : // Constructor for wrapped render targets.
21 0 : GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
22 : const GrSurfaceDesc& desc,
23 : const IDDesc& idDesc,
24 0 : GrGLStencilAttachment* stencil)
25 : : GrSurface(gpu, desc)
26 0 : , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc), stencil) {
27 0 : this->init(desc, idDesc);
28 0 : this->registerWithCacheWrapped();
29 0 : }
30 :
31 0 : GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu, const GrSurfaceDesc& desc,
32 0 : const IDDesc& idDesc)
33 : : GrSurface(gpu, desc)
34 0 : , INHERITED(gpu, desc, ComputeFlags(gpu->glCaps(), idDesc)) {
35 0 : this->init(desc, idDesc);
36 0 : }
37 :
38 0 : inline GrRenderTarget::Flags GrGLRenderTarget::ComputeFlags(const GrGLCaps& glCaps,
39 : const IDDesc& idDesc) {
40 0 : Flags flags = Flags::kNone;
41 0 : if (idDesc.fIsMixedSampled) {
42 0 : SkASSERT(glCaps.usesMixedSamples() && idDesc.fRTFBOID); // FBO 0 can't be mixed sampled.
43 0 : flags |= Flags::kMixedSampled;
44 : }
45 0 : if (glCaps.maxWindowRectangles() > 0 && idDesc.fRTFBOID) {
46 0 : flags |= Flags::kWindowRectsSupport;
47 : }
48 0 : return flags;
49 : }
50 :
51 0 : void GrGLRenderTarget::init(const GrSurfaceDesc& desc, const IDDesc& idDesc) {
52 0 : fRTFBOID = idDesc.fRTFBOID;
53 0 : fTexFBOID = idDesc.fTexFBOID;
54 0 : fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
55 0 : fRTFBOOwnership = idDesc.fRTFBOOwnership;
56 :
57 0 : fViewport.fLeft = 0;
58 0 : fViewport.fBottom = 0;
59 0 : fViewport.fWidth = desc.fWidth;
60 0 : fViewport.fHeight = desc.fHeight;
61 :
62 0 : fNumSamplesOwnedPerPixel = this->totalSamples();
63 0 : }
64 :
65 0 : sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
66 : const GrSurfaceDesc& desc,
67 : const IDDesc& idDesc,
68 : int stencilBits) {
69 0 : GrGLStencilAttachment* sb = nullptr;
70 0 : if (stencilBits) {
71 0 : GrGLStencilAttachment::IDDesc sbDesc;
72 : GrGLStencilAttachment::Format format;
73 0 : format.fInternalFormat = GrGLStencilAttachment::kUnknownInternalFormat;
74 0 : format.fPacked = false;
75 0 : format.fStencilBits = stencilBits;
76 0 : format.fTotalBits = stencilBits;
77 : // Owndership of sb is passed to the GrRenderTarget so doesn't need to be deleted
78 0 : sb = new GrGLStencilAttachment(gpu, sbDesc, desc.fWidth, desc.fHeight,
79 0 : desc.fSampleCnt, format);
80 : }
81 0 : return sk_sp<GrGLRenderTarget>(new GrGLRenderTarget(gpu, desc, idDesc, sb));
82 : }
83 :
84 0 : size_t GrGLRenderTarget::onGpuMemorySize() const {
85 0 : return GrSurface::ComputeSize(fDesc, fNumSamplesOwnedPerPixel, false);
86 : }
87 :
88 0 : bool GrGLRenderTarget::completeStencilAttachment() {
89 0 : GrGLGpu* gpu = this->getGLGpu();
90 0 : const GrGLInterface* interface = gpu->glInterface();
91 0 : GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
92 0 : if (nullptr == stencil) {
93 0 : GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
94 : GR_GL_STENCIL_ATTACHMENT,
95 : GR_GL_RENDERBUFFER, 0));
96 0 : GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
97 : GR_GL_DEPTH_ATTACHMENT,
98 : GR_GL_RENDERBUFFER, 0));
99 : #ifdef SK_DEBUG
100 0 : if (kChromium_GrGLDriver != gpu->glContext().driver()) {
101 : // This check can cause problems in Chromium if the context has been asynchronously
102 : // abandoned (see skbug.com/5200)
103 : GrGLenum status;
104 0 : GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
105 0 : SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
106 : }
107 : #endif
108 0 : return true;
109 : } else {
110 0 : const GrGLStencilAttachment* glStencil = static_cast<const GrGLStencilAttachment*>(stencil);
111 0 : GrGLuint rb = glStencil->renderbufferID();
112 :
113 0 : gpu->invalidateBoundRenderTarget();
114 0 : gpu->stats()->incRenderTargetBinds();
115 0 : GR_GL_CALL(interface, BindFramebuffer(GR_GL_FRAMEBUFFER, this->renderFBOID()));
116 0 : GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
117 : GR_GL_STENCIL_ATTACHMENT,
118 : GR_GL_RENDERBUFFER, rb));
119 0 : if (glStencil->format().fPacked) {
120 0 : GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
121 : GR_GL_DEPTH_ATTACHMENT,
122 : GR_GL_RENDERBUFFER, rb));
123 : } else {
124 0 : GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
125 : GR_GL_DEPTH_ATTACHMENT,
126 : GR_GL_RENDERBUFFER, 0));
127 : }
128 :
129 : #ifdef SK_DEBUG
130 0 : if (kChromium_GrGLDriver != gpu->glContext().driver()) {
131 : // This check can cause problems in Chromium if the context has been asynchronously
132 : // abandoned (see skbug.com/5200)
133 : GrGLenum status;
134 0 : GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
135 0 : SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
136 : }
137 : #endif
138 0 : return true;
139 : }
140 : }
141 :
142 0 : void GrGLRenderTarget::onRelease() {
143 0 : if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
144 0 : if (fTexFBOID) {
145 0 : GL_CALL(DeleteFramebuffers(1, &fTexFBOID));
146 : }
147 0 : if (fRTFBOID && fRTFBOID != fTexFBOID) {
148 0 : GL_CALL(DeleteFramebuffers(1, &fRTFBOID));
149 : }
150 0 : if (fMSColorRenderbufferID) {
151 0 : GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
152 : }
153 : }
154 0 : fRTFBOID = 0;
155 0 : fTexFBOID = 0;
156 0 : fMSColorRenderbufferID = 0;
157 0 : INHERITED::onRelease();
158 0 : }
159 :
160 0 : void GrGLRenderTarget::onAbandon() {
161 0 : fRTFBOID = 0;
162 0 : fTexFBOID = 0;
163 0 : fMSColorRenderbufferID = 0;
164 0 : INHERITED::onAbandon();
165 0 : }
166 :
167 0 : GrGLGpu* GrGLRenderTarget::getGLGpu() const {
168 0 : SkASSERT(!this->wasDestroyed());
169 0 : return static_cast<GrGLGpu*>(this->getGpu());
170 : }
171 :
172 0 : bool GrGLRenderTarget::canAttemptStencilAttachment() const {
173 : // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
174 : // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
175 : // Skia created it.
176 0 : return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned;
177 : }
178 :
179 0 : void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
180 : // Don't log the backing texture's contribution to the memory size. This will be handled by the
181 : // texture object.
182 :
183 : // Log any renderbuffer's contribution to memory. We only do this if we own the renderbuffer
184 : // (have a fMSColorRenderbufferID).
185 0 : if (fMSColorRenderbufferID) {
186 0 : size_t size = GrSurface::ComputeSize(fDesc, this->msaaSamples(), false);
187 :
188 : // Due to this resource having both a texture and a renderbuffer component, dump as
189 : // skia/gpu_resources/resource_#/renderbuffer
190 0 : SkString dumpName("skia/gpu_resources/resource_");
191 0 : dumpName.appendU32(this->uniqueID().asUInt());
192 0 : dumpName.append("/renderbuffer");
193 :
194 0 : traceMemoryDump->dumpNumericValue(dumpName.c_str(), "size", "bytes", size);
195 :
196 0 : if (this->isPurgeable()) {
197 0 : traceMemoryDump->dumpNumericValue(dumpName.c_str(), "purgeable_size", "bytes", size);
198 : }
199 :
200 0 : SkString renderbuffer_id;
201 0 : renderbuffer_id.appendU32(fMSColorRenderbufferID);
202 0 : traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer",
203 0 : renderbuffer_id.c_str());
204 : }
205 0 : }
206 :
207 0 : int GrGLRenderTarget::msaaSamples() const {
208 0 : if (fTexFBOID == kUnresolvableFBOID || fTexFBOID != fRTFBOID) {
209 : // If the render target's FBO is external (fTexFBOID == kUnresolvableFBOID), or if we own
210 : // the render target's FBO (fTexFBOID == fRTFBOID) then we use the provided sample count.
211 0 : return SkTMax(1, fDesc.fSampleCnt);
212 : }
213 :
214 : // When fTexFBOID == fRTFBOID, we either are not using MSAA, or MSAA is auto resolving, so use
215 : // 0 for the sample count.
216 0 : return 0;
217 : }
218 :
219 0 : int GrGLRenderTarget::totalSamples() const {
220 0 : int total_samples = this->msaaSamples();
221 :
222 0 : if (fTexFBOID != kUnresolvableFBOID) {
223 : // If we own the resolve buffer then that is one more sample per pixel.
224 0 : total_samples += 1;
225 : }
226 :
227 0 : return total_samples;
228 : }
|