2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/gpu/ganesh/gl/GrGLAttachment.h"
10 #include "include/core/SkTraceMemoryDump.h"
11 #include "src/gpu/ganesh/gl/GrGLGpu.h"
13 #define GL_CALL(X) GR_GL_CALL(gpu->glInterface(), X)
15 #define GL_ALLOC_CALL(call) \
17 if (gpu->glCaps().skipErrorChecks()) { \
18 GR_GL_CALL(gpu->glInterface(), call); \
19 return static_cast<GrGLenum>(GR_GL_NO_ERROR); \
21 gpu->clearErrorsAndCheckForOOM(); \
22 GR_GL_CALL_NOERRCHECK(gpu->glInterface(), call); \
23 return gpu->getErrorAndCheckForOOM(); \
27 static bool renderbuffer_storage_msaa(GrGLGpu* gpu,
32 SkASSERT(GrGLCaps::kNone_MSFBOType != gpu->glCaps().msFBOType());
34 switch (gpu->glCaps().msFBOType()) {
35 case GrGLCaps::kStandard_MSFBOType:
36 error = GL_ALLOC_CALL(RenderbufferStorageMultisample(
37 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
39 case GrGLCaps::kES_Apple_MSFBOType:
40 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2APPLE(
41 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
43 case GrGLCaps::kES_EXT_MsToTexture_MSFBOType:
44 case GrGLCaps::kES_IMG_MsToTexture_MSFBOType:
45 error = GL_ALLOC_CALL(RenderbufferStorageMultisampleES2EXT(
46 GR_GL_RENDERBUFFER, sampleCount, format, width, height));
48 case GrGLCaps::kNone_MSFBOType:
52 return error == GR_GL_NO_ERROR;
55 sk_sp<GrGLAttachment> GrGLAttachment::MakeStencil(GrGLGpu* gpu,
61 GL_CALL(GenRenderbuffers(1, &rbID));
65 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbID));
66 GrGLenum glFmt = GrGLFormatToEnum(format);
67 // we do this "if" so that we don't call the multisample
68 // version on a GL that doesn't have an MSAA extension.
70 if (!renderbuffer_storage_msaa(gpu, sampleCnt, glFmt, dimensions.width(),
71 dimensions.height())) {
72 GL_CALL(DeleteRenderbuffers(1, &rbID));
76 GrGLenum error = GL_ALLOC_CALL(RenderbufferStorage(
77 GR_GL_RENDERBUFFER, glFmt, dimensions.width(), dimensions.height()));
78 if (error != GR_GL_NO_ERROR) {
79 GL_CALL(DeleteRenderbuffers(1, &rbID));
84 return sk_sp<GrGLAttachment>(new GrGLAttachment(gpu,
87 GrAttachment::UsageFlags::kStencilAttachment,
93 sk_sp<GrGLAttachment> GrGLAttachment::MakeMSAA(GrGLGpu* gpu,
99 GL_CALL(GenRenderbuffers(1, &rbID));
103 GL_CALL(BindRenderbuffer(GR_GL_RENDERBUFFER, rbID));
104 GrGLenum glFmt = gpu->glCaps().getRenderbufferInternalFormat(format);
105 if (!renderbuffer_storage_msaa(
106 gpu, sampleCnt, glFmt, dimensions.width(), dimensions.height())) {
107 GL_CALL(DeleteRenderbuffers(1, &rbID));
111 return sk_sp<GrGLAttachment>(new GrGLAttachment(gpu,
114 GrAttachment::UsageFlags::kColorAttachment,
121 void GrGLAttachment::onRelease() {
122 if (0 != fRenderbufferID) {
123 GrGLGpu* gpuGL = (GrGLGpu*)this->getGpu();
124 const GrGLInterface* gl = gpuGL->glInterface();
125 GR_GL_CALL(gl, DeleteRenderbuffers(1, &fRenderbufferID));
129 INHERITED::onRelease();
132 void GrGLAttachment::onAbandon() {
135 INHERITED::onAbandon();
138 GrBackendFormat GrGLAttachment::backendFormat() const {
139 return GrBackendFormat::MakeGL(GrGLFormatToEnum(fFormat), GR_GL_TEXTURE_NONE);
142 void GrGLAttachment::setMemoryBacking(SkTraceMemoryDump* traceMemoryDump,
143 const SkString& dumpName) const {
144 SkString renderbuffer_id;
145 renderbuffer_id.appendU32(this->renderbufferID());
146 traceMemoryDump->setMemoryBacking(dumpName.c_str(), "gl_renderbuffer", renderbuffer_id.c_str());