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::SkANGLEGLContext()
12 : fContext(EGL_NO_CONTEXT)
13 , fDisplay(EGL_NO_DISPLAY)
14 , fSurface(EGL_NO_SURFACE) {
15 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
19 eglInitialize(fDisplay, &majorVersion, &minorVersion);
22 static const EGLint configAttribs[] = {
23 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
24 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
32 EGLConfig surfaceConfig;
33 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
35 static const EGLint contextAttribs[] = {
36 EGL_CONTEXT_CLIENT_VERSION, 2,
39 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
42 static const EGLint surfaceAttribs[] = {
47 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
49 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
51 fGL.reset(GrGLCreateANGLEInterface());
52 if (NULL == fGL.get()) {
53 SkDebugf("Could not create ANGLE GL interface!\n");
54 this->destroyGLContext();
57 if (!fGL->validate()) {
58 SkDebugf("Could not validate ANGLE GL interface!\n");
59 this->destroyGLContext();
64 SkANGLEGLContext::~SkANGLEGLContext() {
65 this->destroyGLContext();
68 void SkANGLEGLContext::destroyGLContext() {
71 eglMakeCurrent(fDisplay, 0, 0, 0);
74 eglDestroyContext(fDisplay, fContext);
75 fContext = EGL_NO_CONTEXT;
79 eglDestroySurface(fDisplay, fSurface);
80 fSurface = EGL_NO_SURFACE;
83 //TODO should we close the display?
84 fDisplay = EGL_NO_DISPLAY;
88 void SkANGLEGLContext::makeCurrent() const {
89 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
90 SkDebugf("Could not set the context.\n");
94 void SkANGLEGLContext::swapBuffers() const {
95 if (!eglSwapBuffers(fDisplay, fSurface)) {
96 SkDebugf("Could not complete eglSwapBuffers.\n");