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 exitEglError(const char *what)
51 EGLint e = eglGetError();
52 fprintf(stderr, "EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
56 static const EGLint context_attribs[] = {
58 EGL_CONTEXT_CLIENT_VERSION, 2,
62 static const EGLint config_attribs[] = {
67 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
69 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
74 VisualID EGLBackend::initialize(Display *xDisplay)
76 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
77 // get connection and bind API
78 EGLint eglMajor, eglMinor;
79 display = eglGetDisplay(xDisplay);
80 if (display == EGL_NO_DISPLAY)
81 exitEglError("Failed to get EGL display");
82 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
83 exitEglError("Failed to initialize EGL");
84 printf("Using EGL version %d.%d\n", eglMajor, eglMinor);
85 if (eglMajor == 1 && eglMinor < 3) {
86 fprintf(stderr, "Need at least EGL 1.3 to function properly\n");
90 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
92 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
94 exitEglError("Failed to bind API");
95 // get an appropriate config
98 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE){
99 exitEglError("Failed to choose config");
102 fprintf(stderr, "Found no matching EGL configuration\n");
109 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
110 return (VisualID)val;
113 void EGLBackend::createContext(Window window)
115 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
116 surface = eglCreateWindowSurface(display, config, window, NULL);
117 // create an EGL context and use it with the surface
118 context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
119 if (context == EGL_NO_CONTEXT)
120 exitEglError("Failed to create context");
121 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
122 exitEglError("Failed to make context current");
125 EGLBackend::~EGLBackend()
127 if (display != EGL_NO_DISPLAY) {
128 if (context != EGL_NO_CONTEXT) {
129 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
130 eglDestroyContext(display, context);
131 eglDestroySurface(display, surface);
133 eglTerminate(display);
138 void EGLBackend::swapBuffers() const
140 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
141 if (eglSwapBuffers(display, surface) == EGL_FALSE)
142 exitEglError("Failed to swap buffers");
145 void EGLBackend::setSwapInterval(int i) const
147 assert(context != EGL_NO_CONTEXT);
148 // check if swap interval value is supported
150 eglGetConfigAttrib(display, config, EGL_MIN_SWAP_INTERVAL, &val);
152 fprintf(stderr, "Cannot set swap interval to %d, minimum supported value is %d\n", i, val);
155 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
157 fprintf(stderr, "Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
161 if (eglSwapInterval(display, i) == EGL_FALSE)
162 exitEglError("Failed to set swap interval");