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"
26 #include <EGL/eglext.h>
28 #if !defined(CON_GL1) && !defined(CON_GLES2)
29 #error "Valid GL contexts for EGL are: GL1, GLES2"
32 static const char *eglErrorToString(EGLint e)
34 #define CASE(name) case name: return #name
37 CASE(EGL_NOT_INITIALIZED);
40 CASE(EGL_BAD_ATTRIBUTE);
41 CASE(EGL_BAD_CONTEXT);
43 CASE(EGL_BAD_CURRENT_SURFACE);
44 CASE(EGL_BAD_DISPLAY);
45 default: return "<unknown>";
50 static void exitEglError(const char *what)
52 EGLint e = eglGetError();
53 fprintf(stderr, "EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
57 static const EGLint configAttribs[] = {
62 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
64 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
68 static const EGLint contextAttribs[] = {
70 EGL_CONTEXT_CLIENT_VERSION, 2,
75 VisualID EGLBackend::initialize(Display *xDisplay)
77 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
78 // get connection and bind API
79 EGLint eglMajor, eglMinor;
80 display = eglGetDisplay(xDisplay);
81 if (display == EGL_NO_DISPLAY)
82 exitEglError("Failed to get EGL display");
83 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
84 exitEglError("Failed to initialize EGL");
85 printf("Using EGL version: %d.%d\n", eglMajor, eglMinor);
86 if (eglMajor == 1 && eglMinor < 3) {
87 // Choosing the GL context version requires EGL 1.3
88 fprintf(stderr, "Need at least EGL 1.3 to function properly\n");
92 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
94 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
96 exitEglError("Failed to bind API");
97 // get an appropriate config
100 if (eglChooseConfig(display, configAttribs, configs, 1, &count) == EGL_FALSE){
101 exitEglError("Failed to choose framebuffer configuration");
104 fprintf(stderr, "Found no matching framebuffer configuration\n");
111 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
112 return (VisualID)val;
115 void EGLBackend::createContext(Window window)
117 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
118 surface = eglCreateWindowSurface(display, config, window, NULL);
119 // create an EGL context and use it with the surface
120 context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
121 if (context == EGL_NO_CONTEXT)
122 exitEglError("Failed to create context");
123 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
124 exitEglError("Failed to make context current");
125 printf("Using GL version: %s\n", glGetString(GL_VERSION));
126 // initialise GL utilities
127 resolveFunctionPointers(eglGetProcAddress);
130 EGLBackend::~EGLBackend()
132 if (display != EGL_NO_DISPLAY) {
133 if (context != EGL_NO_CONTEXT) {
134 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
135 eglDestroyContext(display, context);
136 eglDestroySurface(display, surface);
138 eglTerminate(display);
143 void EGLBackend::swapBuffers() const
145 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
146 if (eglSwapBuffers(display, surface) == EGL_FALSE)
147 exitEglError("Failed to swap buffers");
150 void EGLBackend::setSwapInterval(int i) const
152 assert(context != EGL_NO_CONTEXT);
153 // check if swap interval value is supported
155 fprintf(stderr, "Cannot set swap interval to %d, must not be negative\n", i);
159 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
161 fprintf(stderr, "Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
165 if (eglSwapInterval(display, i) == EGL_FALSE)
166 exitEglError("Failed to set swap interval");