michael@0: michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "gl/SkGLContextHelper.h" michael@0: #include "GrGLUtil.h" michael@0: michael@0: SkGLContextHelper::SkGLContextHelper() michael@0: : fFBO(0) michael@0: , fColorBufferID(0) michael@0: , fDepthStencilBufferID(0) michael@0: , fGL(NULL) { michael@0: } michael@0: michael@0: SkGLContextHelper::~SkGLContextHelper() { michael@0: michael@0: if (fGL) { michael@0: // TODO: determine why DeleteFramebuffers is generating a GL error in tests michael@0: SK_GL_NOERRCHECK(*this, DeleteFramebuffers(1, &fFBO)); michael@0: SK_GL_NOERRCHECK(*this, DeleteRenderbuffers(1, &fColorBufferID)); michael@0: SK_GL_NOERRCHECK(*this, DeleteRenderbuffers(1, &fDepthStencilBufferID)); michael@0: } michael@0: michael@0: SkSafeUnref(fGL); michael@0: } michael@0: michael@0: bool SkGLContextHelper::init(int width, int height) { michael@0: if (fGL) { michael@0: fGL->unref(); michael@0: this->destroyGLContext(); michael@0: } michael@0: michael@0: fGL = this->createGLContext(); michael@0: if (fGL) { michael@0: const GrGLubyte* temp; michael@0: michael@0: if (!fGL->validate()) { michael@0: fGL = NULL; michael@0: this->destroyGLContext(); michael@0: return false; michael@0: } michael@0: michael@0: SK_GL_RET(*this, temp, GetString(GR_GL_VERSION)); michael@0: const char* versionStr = reinterpret_cast(temp); michael@0: GrGLVersion version = GrGLGetVersionFromString(versionStr); michael@0: michael@0: // clear any existing GL erorrs michael@0: GrGLenum error; michael@0: do { michael@0: SK_GL_RET(*this, error, GetError()); michael@0: } while (GR_GL_NO_ERROR != error); michael@0: michael@0: SK_GL(*this, GenFramebuffers(1, &fFBO)); michael@0: SK_GL(*this, BindFramebuffer(GR_GL_FRAMEBUFFER, fFBO)); michael@0: SK_GL(*this, GenRenderbuffers(1, &fColorBufferID)); michael@0: SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fColorBufferID)); michael@0: if (kGLES_GrGLStandard == this->gl()->fStandard) { michael@0: SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, michael@0: GR_GL_RGBA8, michael@0: width, height)); michael@0: } else { michael@0: SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, michael@0: GR_GL_RGBA, michael@0: width, height)); michael@0: } michael@0: SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, michael@0: GR_GL_COLOR_ATTACHMENT0, michael@0: GR_GL_RENDERBUFFER, michael@0: fColorBufferID)); michael@0: SK_GL(*this, GenRenderbuffers(1, &fDepthStencilBufferID)); michael@0: SK_GL(*this, BindRenderbuffer(GR_GL_RENDERBUFFER, fDepthStencilBufferID)); michael@0: michael@0: // Some drivers that support packed depth stencil will only succeed michael@0: // in binding a packed format an FBO. However, we can't rely on packed michael@0: // depth stencil being available. michael@0: bool supportsPackedDepthStencil; michael@0: if (kGLES_GrGLStandard == this->gl()->fStandard) { michael@0: supportsPackedDepthStencil = version >= GR_GL_VER(3,0) || michael@0: this->hasExtension("GL_OES_packed_depth_stencil"); michael@0: } else { michael@0: supportsPackedDepthStencil = version >= GR_GL_VER(3,0) || michael@0: this->hasExtension("GL_EXT_packed_depth_stencil") || michael@0: this->hasExtension("GL_ARB_framebuffer_object"); michael@0: } michael@0: michael@0: if (supportsPackedDepthStencil) { michael@0: // ES2 requires sized internal formats for RenderbufferStorage michael@0: // On Desktop we let the driver decide. michael@0: GrGLenum format = kGLES_GrGLStandard == this->gl()->fStandard ? michael@0: GR_GL_DEPTH24_STENCIL8 : michael@0: GR_GL_DEPTH_STENCIL; michael@0: SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, michael@0: format, michael@0: width, height)); michael@0: SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, michael@0: GR_GL_DEPTH_ATTACHMENT, michael@0: GR_GL_RENDERBUFFER, michael@0: fDepthStencilBufferID)); michael@0: } else { michael@0: GrGLenum format = kGLES_GrGLStandard == this->gl()->fStandard ? GR_GL_STENCIL_INDEX8 : michael@0: GR_GL_STENCIL_INDEX; michael@0: SK_GL(*this, RenderbufferStorage(GR_GL_RENDERBUFFER, michael@0: format, michael@0: width, height)); michael@0: } michael@0: SK_GL(*this, FramebufferRenderbuffer(GR_GL_FRAMEBUFFER, michael@0: GR_GL_STENCIL_ATTACHMENT, michael@0: GR_GL_RENDERBUFFER, michael@0: fDepthStencilBufferID)); michael@0: SK_GL(*this, Viewport(0, 0, width, height)); michael@0: SK_GL(*this, ClearStencil(0)); michael@0: SK_GL(*this, Clear(GR_GL_STENCIL_BUFFER_BIT)); michael@0: michael@0: SK_GL_RET(*this, error, GetError()); michael@0: GrGLenum status; michael@0: SK_GL_RET(*this, status, CheckFramebufferStatus(GR_GL_FRAMEBUFFER)); michael@0: michael@0: if (GR_GL_FRAMEBUFFER_COMPLETE != status || michael@0: GR_GL_NO_ERROR != error) { michael@0: fFBO = 0; michael@0: fColorBufferID = 0; michael@0: fDepthStencilBufferID = 0; michael@0: fGL->unref(); michael@0: fGL = NULL; michael@0: this->destroyGLContext(); michael@0: return false; michael@0: } else { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: }