3 * Copyright 2012 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "gl/SkANGLEGLContext.h"
11 SkANGLEGLContext::AutoContextRestore::AutoContextRestore() {
12 fOldEGLContext = eglGetCurrentContext();
13 fOldDisplay = eglGetCurrentDisplay();
14 fOldSurface = eglGetCurrentSurface(EGL_DRAW);
18 SkANGLEGLContext::AutoContextRestore::~AutoContextRestore() {
20 eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
24 ///////////////////////////////////////////////////////////////////////////////
26 SkANGLEGLContext::SkANGLEGLContext()
27 : fContext(EGL_NO_CONTEXT)
28 , fDisplay(EGL_NO_DISPLAY)
29 , fSurface(EGL_NO_SURFACE) {
32 SkANGLEGLContext::~SkANGLEGLContext() {
33 this->destroyGLContext();
36 void SkANGLEGLContext::destroyGLContext() {
38 eglMakeCurrent(fDisplay, 0, 0, 0);
41 eglDestroyContext(fDisplay, fContext);
42 fContext = EGL_NO_CONTEXT;
46 eglDestroySurface(fDisplay, fSurface);
47 fSurface = EGL_NO_SURFACE;
50 //TODO should we close the display?
51 fDisplay = EGL_NO_DISPLAY;
55 const GrGLInterface* SkANGLEGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
56 if (kGL_GrGLStandard == forcedGpuAPI) {
60 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
64 eglInitialize(fDisplay, &majorVersion, &minorVersion);
67 static const EGLint configAttribs[] = {
68 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
69 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
77 EGLConfig surfaceConfig;
78 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
80 static const EGLint contextAttribs[] = {
81 EGL_CONTEXT_CLIENT_VERSION, 2,
84 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
87 static const EGLint surfaceAttribs[] = {
92 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
94 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
96 const GrGLInterface* interface = GrGLCreateANGLEInterface();
97 if (NULL == interface) {
98 SkDebugf("Could not create ANGLE GL interface!\n");
99 this->destroyGLContext();
106 void SkANGLEGLContext::makeCurrent() const {
107 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
108 SkDebugf("Could not set the context.\n");
112 void SkANGLEGLContext::swapBuffers() const {
113 if (!eglSwapBuffers(fDisplay, fSurface)) {
114 SkDebugf("Could not complete eglSwapBuffers.\n");