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 "src/gpu/gl/GrGLRenderTarget.h"
9
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "include/gpu/GrDirectContext.h"
12 #include "src/gpu/GrBackendUtils.h"
13 #include "src/gpu/GrDirectContextPriv.h"
14 #include "src/gpu/GrGpuResourcePriv.h"
15 #include "src/gpu/gl/GrGLGpu.h"
16 #include "src/gpu/gl/GrGLUtil.h"
17
18 #define GPUGL static_cast<GrGLGpu*>(this->getGpu())
19 #define GL_CALL(X) GR_GL_CALL(GPUGL->glInterface(), X)
20
21 // Because this class is virtually derived from GrSurface we must explicitly call its constructor.
22 // Constructor for wrapped render targets.
GrGLRenderTarget(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & ids,GrGLAttachment * stencil)23 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
24 const SkISize& dimensions,
25 GrGLFormat format,
26 int sampleCount,
27 const IDs& ids,
28 GrGLAttachment* stencil)
29 : GrSurface(gpu, dimensions, GrProtected::kNo)
30 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo, stencil) {
31 this->init(format, ids);
32 this->setFlags(gpu->glCaps(), ids);
33 this->registerWithCacheWrapped(GrWrapCacheable::kNo);
34 }
35
GrGLRenderTarget(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & ids)36 GrGLRenderTarget::GrGLRenderTarget(GrGLGpu* gpu,
37 const SkISize& dimensions,
38 GrGLFormat format,
39 int sampleCount,
40 const IDs& ids)
41 : GrSurface(gpu, dimensions, GrProtected::kNo)
42 , INHERITED(gpu, dimensions, sampleCount, GrProtected::kNo) {
43 this->init(format, ids);
44 this->setFlags(gpu->glCaps(), ids);
45 }
46
setFlags(const GrGLCaps & glCaps,const IDs & idDesc)47 inline void GrGLRenderTarget::setFlags(const GrGLCaps& glCaps, const IDs& idDesc) {
48 if ((fMultisampleFBOID | fSingleSampleFBOID) == 0) {
49 this->setGLRTFBOIDIs0();
50 }
51 }
52
init(GrGLFormat format,const IDs & idDesc)53 void GrGLRenderTarget::init(GrGLFormat format, const IDs& idDesc) {
54 fMultisampleFBOID = idDesc.fMultisampleFBOID;
55 fSingleSampleFBOID = idDesc.fSingleSampleFBOID;
56 fMSColorRenderbufferID = idDesc.fMSColorRenderbufferID;
57 fRTFBOOwnership = idDesc.fRTFBOOwnership;
58 fRTFormat = format;
59 fTotalMemorySamplesPerPixel = idDesc.fTotalMemorySamplesPerPixel;
60 }
61
stencil_bits_to_format(int stencilBits)62 GrGLFormat stencil_bits_to_format(int stencilBits) {
63 SkASSERT(stencilBits);
64 switch (stencilBits) {
65 case 8:
66 // We pick the packed format here so when we query total size we are at least not
67 // underestimating the total size of the stencil buffer. However, in reality this
68 // rarely matters since we usually don't care about the size of wrapped objects.
69 return GrGLFormat::kDEPTH24_STENCIL8;
70 case 16:
71 return GrGLFormat::kSTENCIL_INDEX16;
72 default:
73 SkASSERT(false);
74 return GrGLFormat::kUnknown;
75 }
76 }
77
MakeWrapped(GrGLGpu * gpu,const SkISize & dimensions,GrGLFormat format,int sampleCount,const IDs & idDesc,int stencilBits)78 sk_sp<GrGLRenderTarget> GrGLRenderTarget::MakeWrapped(GrGLGpu* gpu,
79 const SkISize& dimensions,
80 GrGLFormat format,
81 int sampleCount,
82 const IDs& idDesc,
83 int stencilBits) {
84 GrGLAttachment* sb = nullptr;
85 if (stencilBits) {
86 GrGLAttachment::IDDesc sbDesc;
87 // We pick a "fake" actual format that matches the number of stencil bits. When wrapping
88 // an FBO with some number of stencil bits all we care about in the future is that we have
89 // a format with the same number of stencil bits. We don't even directly use the format or
90 // any other properties. Thus it is fine for us to just assign an arbitrary format that
91 // matches the stencil bit count.
92 GrGLFormat sFmt = stencil_bits_to_format(stencilBits);
93
94 // Ownership of sb is passed to the GrRenderTarget so doesn't need to be deleted
95 sb = new GrGLAttachment(gpu, sbDesc, dimensions,
96 GrAttachment::UsageFlags::kStencilAttachment, sampleCount, sFmt);
97 }
98 return sk_sp<GrGLRenderTarget>(
99 new GrGLRenderTarget(gpu, dimensions, format, sampleCount, idDesc, sb));
100 }
101
getBackendRenderTarget() const102 GrBackendRenderTarget GrGLRenderTarget::getBackendRenderTarget() const {
103 bool useMultisampleFBO = (this->numSamples() > 1);
104 GrGLFramebufferInfo fbi;
105 fbi.fFBOID = (useMultisampleFBO) ? fMultisampleFBOID : fSingleSampleFBOID;
106 fbi.fFormat = GrGLFormatToEnum(this->format());
107 int numStencilBits = 0;
108 if (GrAttachment* stencil = this->getStencilAttachment(useMultisampleFBO)) {
109 numStencilBits = GrBackendFormatStencilBits(stencil->backendFormat());
110 }
111
112 return GrBackendRenderTarget(
113 this->width(), this->height(), this->numSamples(), numStencilBits, fbi);
114 }
115
backendFormat() const116 GrBackendFormat GrGLRenderTarget::backendFormat() const {
117 // We should never have a GrGLRenderTarget (even a textureable one with a target that is not
118 // texture 2D.
119 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fRTFormat), GR_GL_TEXTURE_2D);
120 }
121
onGpuMemorySize() const122 size_t GrGLRenderTarget::onGpuMemorySize() const {
123 return GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
124 fTotalMemorySamplesPerPixel, GrMipmapped::kNo);
125 }
126
completeStencilAttachment(GrAttachment * stencil,bool useMultisampleFBO)127 bool GrGLRenderTarget::completeStencilAttachment(GrAttachment* stencil, bool useMultisampleFBO) {
128 GrGLGpu* gpu = this->getGLGpu();
129 const GrGLInterface* interface = gpu->glInterface();
130
131 if (this->numSamples() == 1 && useMultisampleFBO) {
132 // We will be rendering to the dynamic msaa fbo. Make sure to initialize it first.
133 if (!this->ensureDynamicMSAAAttachment()) {
134 return false;
135 }
136 }
137
138 GrGLuint stencilFBOID = (useMultisampleFBO) ? fMultisampleFBOID : fSingleSampleFBOID;
139 gpu->bindFramebuffer(GR_GL_FRAMEBUFFER, stencilFBOID);
140
141 if (nullptr == stencil) {
142 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
143 GR_GL_STENCIL_ATTACHMENT,
144 GR_GL_RENDERBUFFER, 0));
145 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
146 GR_GL_DEPTH_ATTACHMENT,
147 GR_GL_RENDERBUFFER, 0));
148 } else {
149 const GrGLAttachment* glStencil = static_cast<const GrGLAttachment*>(stencil);
150 GrGLuint rb = glStencil->renderbufferID();
151 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
152 GR_GL_STENCIL_ATTACHMENT,
153 GR_GL_RENDERBUFFER, rb));
154 if (GrGLFormatIsPackedDepthStencil(glStencil->format())) {
155 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
156 GR_GL_DEPTH_ATTACHMENT,
157 GR_GL_RENDERBUFFER, rb));
158 } else {
159 GR_GL_CALL(interface, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER,
160 GR_GL_DEPTH_ATTACHMENT,
161 GR_GL_RENDERBUFFER, 0));
162 }
163 }
164
165 #ifdef SK_DEBUG
166 if (!gpu->glCaps().skipErrorChecks()) {
167 // This check can cause problems in Chromium if the context has been asynchronously
168 // abandoned (see skbug.com/5200)
169 GrGLenum status;
170 GR_GL_CALL_RET(interface, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER));
171 SkASSERT(GR_GL_FRAMEBUFFER_COMPLETE == status);
172 }
173 #endif
174
175 return true;
176 }
177
ensureDynamicMSAAAttachment()178 bool GrGLRenderTarget::ensureDynamicMSAAAttachment() {
179 SkASSERT(this->numSamples() == 1);
180 if (fMultisampleFBOID) {
181 return true;
182 }
183 SkASSERT(!fDynamicMSAAAttachment);
184
185 GrResourceProvider* resourceProvider = this->getContext()->priv().resourceProvider();
186 const GrCaps& caps = *this->getGpu()->caps();
187
188 int internalSampleCount = caps.internalMultisampleCount(this->backendFormat());
189 if (internalSampleCount <= 1) {
190 return false;
191 }
192
193 GL_CALL(GenFramebuffers(1, &fMultisampleFBOID));
194 if (!fMultisampleFBOID) {
195 return false;
196 }
197
198 this->getGLGpu()->bindFramebuffer(GR_GL_FRAMEBUFFER, fMultisampleFBOID);
199
200 if (resourceProvider->caps()->msaaResolvesAutomatically()) {
201 if (GrGLTexture* glTex = static_cast<GrGLTexture*>(this->asTexture())) {
202 GL_CALL(FramebufferTexture2DMultisample(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0,
203 glTex->target(), glTex->textureID(),
204 0 /*mipMapLevel*/, internalSampleCount));
205 return true;
206 }
207 }
208
209 fDynamicMSAAAttachment.reset(static_cast<GrGLAttachment*>(resourceProvider->makeMSAAAttachment(
210 this->dimensions(), this->backendFormat(), internalSampleCount,
211 GrProtected(this->isProtected())).release()));
212 if (!fDynamicMSAAAttachment) {
213 return false;
214 }
215
216 GL_CALL(FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, GR_GL_COLOR_ATTACHMENT0, GR_GL_RENDERBUFFER,
217 fDynamicMSAAAttachment->renderbufferID()));
218 return true;
219 }
220
onRelease()221 void GrGLRenderTarget::onRelease() {
222 if (GrBackendObjectOwnership::kBorrowed != fRTFBOOwnership) {
223 GrGLGpu* gpu = this->getGLGpu();
224 if (fSingleSampleFBOID) {
225 SkASSERT(fSingleSampleFBOID != fMultisampleFBOID);
226 gpu->deleteFramebuffer(fSingleSampleFBOID);
227 }
228 if (fMultisampleFBOID) {
229 SkASSERT(fMultisampleFBOID != fSingleSampleFBOID);
230 gpu->deleteFramebuffer(fMultisampleFBOID);
231 }
232 if (fMSColorRenderbufferID) {
233 GL_CALL(DeleteRenderbuffers(1, &fMSColorRenderbufferID));
234 }
235 }
236 fMultisampleFBOID = 0;
237 fSingleSampleFBOID = 0;
238 fMSColorRenderbufferID = 0;
239 INHERITED::onRelease();
240 }
241
onAbandon()242 void GrGLRenderTarget::onAbandon() {
243 fMultisampleFBOID = 0;
244 fSingleSampleFBOID = 0;
245 fMSColorRenderbufferID = 0;
246 INHERITED::onAbandon();
247 }
248
getGLGpu() const249 GrGLGpu* GrGLRenderTarget::getGLGpu() const {
250 SkASSERT(!this->wasDestroyed());
251 return static_cast<GrGLGpu*>(this->getGpu());
252 }
253
canAttemptStencilAttachment(bool useMultisampleFBO) const254 bool GrGLRenderTarget::canAttemptStencilAttachment(bool useMultisampleFBO) const {
255 // This cap should have been handled at a higher level.
256 SkASSERT(!this->getGpu()->getContext()->priv().caps()->avoidStencilBuffers());
257 // Only modify the FBO's attachments if we have created the FBO. Public APIs do not currently
258 // allow for borrowed FBO ownership, so we can safely assume that if an object is owned,
259 // Skia created it.
260 return this->fRTFBOOwnership == GrBackendObjectOwnership::kOwned ||
261 // The dmsaa attachment is always owned and always supports adding stencil.
262 (this->numSamples() == 1 && useMultisampleFBO);
263 }
264
dumpMemoryStatistics(SkTraceMemoryDump * traceMemoryDump) const265 void GrGLRenderTarget::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
266 // Don't check this->fRefsWrappedObjects, as we might be the base of a GrGLTextureRenderTarget
267 // which is multiply inherited from both ourselves and a texture. In these cases, one part
268 // (texture, rt) may be wrapped, while the other is owned by Skia.
269 bool refsWrappedRenderTargetObjects =
270 this->fRTFBOOwnership == GrBackendObjectOwnership::kBorrowed;
271 if (refsWrappedRenderTargetObjects && !traceMemoryDump->shouldDumpWrappedObjects()) {
272 return;
273 }
274
275 int numSamplesNotInTexture = fTotalMemorySamplesPerPixel;
276 if (this->asTexture()) {
277 --numSamplesNotInTexture; // GrGLTexture::dumpMemoryStatistics accounts for 1 sample.
278 }
279 if (numSamplesNotInTexture >= 1) {
280 size_t size = GrSurface::ComputeSize(this->backendFormat(), this->dimensions(),
281 numSamplesNotInTexture, GrMipmapped::kNo);
282
283 // Due to this resource having both a texture and a renderbuffer component, dump as
284 // skia/gpu_resources/resource_#/renderbuffer
285 SkString resourceName = this->getResourceName();
286 resourceName.append("/renderbuffer");
287
288 this->dumpMemoryStatisticsPriv(traceMemoryDump, resourceName, "RenderTarget", size);
289
290 SkString renderbuffer_id;
291 renderbuffer_id.appendU32(fMSColorRenderbufferID);
292 traceMemoryDump->setMemoryBacking(resourceName.c_str(), "gl_renderbuffer",
293 renderbuffer_id.c_str());
294 }
295 }
296