1 /* gltest - small OpenGL tearing test program
2 * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "eglbackend.h"
25 #include <EGL/eglext.h>
27 #if !defined(CON_GL1) && !defined(CON_GLES2)
28 #error "Valid GL contexts for EGL are: GL1, GLES2"
31 static const char *eglErrorToString(EGLint e)
33 #define CASE(name) case name: return #name
36 CASE(EGL_NOT_INITIALIZED);
39 CASE(EGL_BAD_ATTRIBUTE);
40 CASE(EGL_BAD_CONTEXT);
42 CASE(EGL_BAD_CURRENT_SURFACE);
43 CASE(EGL_BAD_DISPLAY);
44 default: return "<unknown>";
49 static void dieEgl(const char *what)
51 EGLint e = eglGetError();
52 die("EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
55 static const EGLint configAttribs[] = {
60 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
62 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
66 static const EGLint contextAttribs[] = {
68 EGL_CONTEXT_CLIENT_VERSION, 2,
73 VisualID EGLBackend::initialize(Display *xDisplay)
75 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
76 // get connection and bind API
77 EGLint eglMajor, eglMinor;
78 display = eglGetDisplay(xDisplay);
79 if (display == EGL_NO_DISPLAY)
80 dieEgl("Failed to get EGL display");
81 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
82 dieEgl("Failed to initialize EGL");
83 printf("Using EGL version: %d.%d\n", eglMajor, eglMinor);
84 if (eglMajor == 1 && eglMinor < 3) {
85 // Choosing the GL context version requires EGL 1.3
86 die("Need at least EGL 1.3 to function properly\n");
89 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
91 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
93 dieEgl("Failed to bind API");
94 // get an appropriate config
97 if (eglChooseConfig(display, configAttribs, configs, 1, &count) == EGL_FALSE){
98 dieEgl("Failed to choose framebuffer configuration");
101 die("Found no matching framebuffer configuration\n");
107 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
108 return (VisualID)val;
111 void EGLBackend::createContext(Window window)
113 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
114 surface = eglCreateWindowSurface(display, config, window, NULL);
115 // create an EGL context and use it with the surface
116 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
117 if (context == EGL_NO_CONTEXT)
118 dieEgl("Failed to create context");
119 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
120 dieEgl("Failed to make context current");
121 printf("Using GL version: %s\n", glGetString(GL_VERSION));
122 // initialise GL utilities
123 resolveFunctionPointers(eglGetProcAddress);
126 EGLBackend::~EGLBackend()
128 if (display != EGL_NO_DISPLAY) {
129 if (context != EGL_NO_CONTEXT) {
130 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
131 eglDestroyContext(display, context);
132 eglDestroySurface(display, surface);
134 eglTerminate(display);
139 void EGLBackend::swapBuffers() const
141 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
142 if (eglSwapBuffers(display, surface) == EGL_FALSE)
143 dieEgl("Failed to swap buffers");
146 void EGLBackend::setSwapInterval(int i) const
148 assert(context != EGL_NO_CONTEXT);
149 // check if swap interval value is supported
151 die("Cannot set swap interval to %d, must not be negative\n", i);
154 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
156 die("Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
159 if (eglSwapInterval(display, i) == EGL_FALSE)
160 dieEgl("Failed to set swap interval");