michael@0: michael@0: /* michael@0: * Copyright 2012 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: michael@0: #include "gl/SkANGLEGLContext.h" michael@0: michael@0: SkANGLEGLContext::AutoContextRestore::AutoContextRestore() { michael@0: fOldEGLContext = eglGetCurrentContext(); michael@0: fOldDisplay = eglGetCurrentDisplay(); michael@0: fOldSurface = eglGetCurrentSurface(EGL_DRAW); michael@0: michael@0: } michael@0: michael@0: SkANGLEGLContext::AutoContextRestore::~AutoContextRestore() { michael@0: if (NULL != fOldDisplay) { michael@0: eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkANGLEGLContext::SkANGLEGLContext() michael@0: : fContext(EGL_NO_CONTEXT) michael@0: , fDisplay(EGL_NO_DISPLAY) michael@0: , fSurface(EGL_NO_SURFACE) { michael@0: } michael@0: michael@0: SkANGLEGLContext::~SkANGLEGLContext() { michael@0: this->destroyGLContext(); michael@0: } michael@0: michael@0: void SkANGLEGLContext::destroyGLContext() { michael@0: if (fDisplay) { michael@0: eglMakeCurrent(fDisplay, 0, 0, 0); michael@0: michael@0: if (fContext) { michael@0: eglDestroyContext(fDisplay, fContext); michael@0: fContext = EGL_NO_CONTEXT; michael@0: } michael@0: michael@0: if (fSurface) { michael@0: eglDestroySurface(fDisplay, fSurface); michael@0: fSurface = EGL_NO_SURFACE; michael@0: } michael@0: michael@0: //TODO should we close the display? michael@0: fDisplay = EGL_NO_DISPLAY; michael@0: } michael@0: } michael@0: michael@0: const GrGLInterface* SkANGLEGLContext::createGLContext() { michael@0: michael@0: fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); michael@0: michael@0: EGLint majorVersion; michael@0: EGLint minorVersion; michael@0: eglInitialize(fDisplay, &majorVersion, &minorVersion); michael@0: michael@0: EGLint numConfigs; michael@0: static const EGLint configAttribs[] = { michael@0: EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, michael@0: EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, michael@0: EGL_RED_SIZE, 8, michael@0: EGL_GREEN_SIZE, 8, michael@0: EGL_BLUE_SIZE, 8, michael@0: EGL_ALPHA_SIZE, 8, michael@0: EGL_NONE michael@0: }; michael@0: michael@0: EGLConfig surfaceConfig; michael@0: eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); michael@0: michael@0: static const EGLint contextAttribs[] = { michael@0: EGL_CONTEXT_CLIENT_VERSION, 2, michael@0: EGL_NONE michael@0: }; michael@0: fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs); michael@0: michael@0: michael@0: static const EGLint surfaceAttribs[] = { michael@0: EGL_WIDTH, 1, michael@0: EGL_HEIGHT, 1, michael@0: EGL_NONE michael@0: }; michael@0: fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); michael@0: michael@0: eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); michael@0: michael@0: const GrGLInterface* interface = GrGLCreateANGLEInterface(); michael@0: if (NULL == interface) { michael@0: SkDebugf("Could not create ANGLE GL interface!\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: return interface; michael@0: } michael@0: michael@0: void SkANGLEGLContext::makeCurrent() const { michael@0: if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { michael@0: SkDebugf("Could not set the context.\n"); michael@0: } michael@0: } michael@0: michael@0: void SkANGLEGLContext::swapBuffers() const { michael@0: if (!eglSwapBuffers(fDisplay, fSurface)) { michael@0: SkDebugf("Could not complete eglSwapBuffers.\n"); michael@0: } michael@0: }