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_GL3) && !defined(CON_GLES2)
28 #error "Valid GL contexts for EGL are: GL1, GL3, 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,
70 EGL_CONTEXT_MAJOR_VERSION_KHR, 3,
71 EGL_CONTEXT_MINOR_VERSION_KHR, 0,
72 EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR,
77 VisualID EGLBackend::initialize(Display *xDisplay)
79 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
80 // get connection and bind API
81 EGLint eglMajor, eglMinor;
82 display = eglGetDisplay(xDisplay);
83 if (display == EGL_NO_DISPLAY)
84 dieEgl("Failed to get EGL display");
85 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
86 dieEgl("Failed to initialize EGL");
87 printf("Using EGL version: %d.%d\n", eglMajor, eglMinor);
88 if (eglMajor == 1 && eglMinor < 4) {
89 // Choosing the GL context version requires EGL 1.3, creating an OpenGL 3 context requires EGL 1.4
90 die("Need at least EGL 1.4 to function properly\n");
93 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
95 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
97 dieEgl("Failed to bind API");
98 // check for the extension we need
100 if (!haveEGLExtension("EGL_KHR_create_context")) {
101 die("Required EGL extension EGL_KHR_create_context is not supported\n");
104 // get an appropriate config
105 EGLConfig configs[1];
107 if (eglChooseConfig(display, configAttribs, configs, 1, &count) == EGL_FALSE){
108 dieEgl("Failed to choose framebuffer configuration");
111 die("Found no matching framebuffer configuration\n");
117 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
118 return (VisualID)val;
121 bool EGLBackend::haveEGLExtension(const std::string &name)
123 assert(display != EGL_NO_DISPLAY);
124 std::string extensions = eglQueryString(display, EGL_EXTENSIONS);
125 return (std::string(" "+extensions+" ").find(" "+name+" ") != std::string::npos);
128 void EGLBackend::createContext(Window window)
130 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
131 surface = eglCreateWindowSurface(display, config, window, NULL);
132 // create an EGL context and use it with the surface
133 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
134 if (context == EGL_NO_CONTEXT)
135 dieEgl("Failed to create context");
136 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
137 dieEgl("Failed to make context current");
138 printf("Using GL version: %s\n", glGetString(GL_VERSION));
139 // initialise GL utilities
140 resolveFunctionPointers(eglGetProcAddress);
143 EGLBackend::~EGLBackend()
145 if (display != EGL_NO_DISPLAY) {
146 if (context != EGL_NO_CONTEXT) {
147 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
148 eglDestroyContext(display, context);
149 eglDestroySurface(display, surface);
151 eglTerminate(display);
156 void EGLBackend::swapBuffers() const
158 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
159 if (eglSwapBuffers(display, surface) == EGL_FALSE)
160 dieEgl("Failed to swap buffers");
163 void EGLBackend::setSwapInterval(int i) const
165 assert(context != EGL_NO_CONTEXT);
166 // check if swap interval value is supported
168 die("Cannot set swap interval to %d, must not be negative\n", i);
171 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
173 die("Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
176 if (eglSwapInterval(display, i) == EGL_FALSE)
177 dieEgl("Failed to set swap interval");