3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "gl/SkNativeGLContext.h"
10 SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
11 fOldEGLContext = eglGetCurrentContext();
12 fOldDisplay = eglGetCurrentDisplay();
13 fOldSurface = eglGetCurrentSurface(EGL_DRAW);
17 SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
19 eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
23 ///////////////////////////////////////////////////////////////////////////////
25 SkNativeGLContext::SkNativeGLContext()
26 : fContext(EGL_NO_CONTEXT)
27 , fDisplay(EGL_NO_DISPLAY)
28 , fSurface(EGL_NO_SURFACE) {
31 SkNativeGLContext::~SkNativeGLContext() {
32 this->destroyGLContext();
35 void SkNativeGLContext::destroyGLContext() {
37 eglMakeCurrent(fDisplay, 0, 0, 0);
40 eglDestroyContext(fDisplay, fContext);
41 fContext = EGL_NO_CONTEXT;
45 eglDestroySurface(fDisplay, fSurface);
46 fSurface = EGL_NO_SURFACE;
49 //TODO should we close the display?
50 fDisplay = EGL_NO_DISPLAY;
54 const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
55 static const EGLint kEGLContextAttribsForOpenGL[] = {
59 static const EGLint kEGLContextAttribsForOpenGLES[] = {
60 EGL_CONTEXT_CLIENT_VERSION, 2,
65 const EGLint* fContextAttribs;
67 EGLint fRenderableTypeBit;
68 GrGLStandard fStandard;
71 kEGLContextAttribsForOpenGL,
76 { // OpenGL ES. This seems to work for both ES2 and 3 (when available).
77 kEGLContextAttribsForOpenGLES,
84 size_t apiLimit = SK_ARRAY_COUNT(kAPIs);
86 if (forcedGpuAPI == kGL_GrGLStandard) {
88 } else if (forcedGpuAPI == kGLES_GrGLStandard) {
91 SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == forcedGpuAPI);
93 const GrGLInterface* interface = NULL;
95 for (; NULL == interface && api < apiLimit; ++api) {
96 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
100 eglInitialize(fDisplay, &majorVersion, &minorVersion);
103 SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
104 SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
105 SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
106 SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
109 if (!eglBindAPI(kAPIs[api].fAPI)) {
114 const EGLint configAttribs[] = {
115 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
116 EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
124 EGLConfig surfaceConfig;
125 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
126 SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
130 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs);
131 if (EGL_NO_CONTEXT == fContext) {
132 SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError());
136 static const EGLint kSurfaceAttribs[] = {
142 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
143 if (EGL_NO_SURFACE == fSurface) {
144 SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
145 this->destroyGLContext();
149 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
150 SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError());
151 this->destroyGLContext();
155 interface = GrGLCreateNativeInterface();
156 if (NULL == interface) {
157 SkDebugf("Failed to create gl interface.\n");
158 this->destroyGLContext();
162 if (!interface->validate()) {
165 this->destroyGLContext();
172 void SkNativeGLContext::makeCurrent() const {
173 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
174 SkDebugf("Could not set the context.\n");
178 void SkNativeGLContext::swapBuffers() const {
179 if (!eglSwapBuffers(fDisplay, fSurface)) {
180 SkDebugf("Could not complete eglSwapBuffers.\n");