From c58b126623d68a64e225fc8612dfbd6a7e56e97b Mon Sep 17 00:00:00 2001 From: "bsalomon@google.com" Date: Mon, 1 Oct 2012 19:06:57 +0000 Subject: Move android gl files from src/gpu/android to src/gpu/gl/android. R=robertphillips@google.com Review URL: https://codereview.appspot.com/6590051 git-svn-id: http://skia.googlecode.com/svn/trunk@5757 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/gpu/gl/android/SkNativeGLContext_android.cpp | 104 +++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/gpu/gl/android/SkNativeGLContext_android.cpp (limited to 'src/gpu/gl/android/SkNativeGLContext_android.cpp') diff --git a/src/gpu/gl/android/SkNativeGLContext_android.cpp b/src/gpu/gl/android/SkNativeGLContext_android.cpp new file mode 100644 index 0000000000..f21eed8666 --- /dev/null +++ b/src/gpu/gl/android/SkNativeGLContext_android.cpp @@ -0,0 +1,104 @@ + +/* + * Copyright 2011 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#include "gl/SkNativeGLContext.h" + +SkNativeGLContext::AutoContextRestore::AutoContextRestore() { + fOldEGLContext = eglGetCurrentContext(); + fOldDisplay = eglGetCurrentDisplay(); + fOldSurface = eglGetCurrentSurface(EGL_DRAW); + +} + +SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { + if (NULL != fOldDisplay) { + eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext); + } +} + +/////////////////////////////////////////////////////////////////////////////// + +SkNativeGLContext::SkNativeGLContext() + : fContext(EGL_NO_CONTEXT) + , fDisplay(EGL_NO_DISPLAY) + , fSurface(EGL_NO_SURFACE) { +} + +SkNativeGLContext::~SkNativeGLContext() { + this->destroyGLContext(); +} + +void SkNativeGLContext::destroyGLContext() { + if (fDisplay) { + eglMakeCurrent(fDisplay, 0, 0, 0); + + if (fContext) { + eglDestroyContext(fDisplay, fContext); + fContext = EGL_NO_CONTEXT; + } + + if (fSurface) { + eglDestroySurface(fDisplay, fSurface); + fSurface = EGL_NO_SURFACE; + } + + //TODO should we close the display? + fDisplay = EGL_NO_DISPLAY; + } +} + +const GrGLInterface* SkNativeGLContext::createGLContext() { + fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); + + EGLint majorVersion; + EGLint minorVersion; + eglInitialize(fDisplay, &majorVersion, &minorVersion); + + EGLint numConfigs; + static const EGLint configAttribs[] = { + EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_RED_SIZE, 8, + EGL_GREEN_SIZE, 8, + EGL_BLUE_SIZE, 8, + EGL_ALPHA_SIZE, 8, + EGL_NONE + }; + + EGLConfig surfaceConfig; + eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs); + + static const EGLint contextAttribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs); + + + static const EGLint surfaceAttribs[] = { + EGL_WIDTH, 1, + EGL_HEIGHT, 1, + EGL_NONE + }; + fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs); + + eglMakeCurrent(fDisplay, fSurface, fSurface, fContext); + + const GrGLInterface* interface = GrGLCreateNativeInterface(); + if (!interface) { + SkDebugf("Failed to create gl interface"); + this->destroyGLContext(); + return NULL; + } + return interface; +} + +void SkNativeGLContext::makeCurrent() const { + if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) { + SkDebugf("Could not set the context.\n"); + } +} -- cgit v1.2.3