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 context_attribs[] = {
59 EGL_CONTEXT_CLIENT_VERSION, 2,
63 static const EGLint config_attribs[] = {
68 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
70 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
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 fprintf(stderr, "Need at least EGL 1.3 to function properly\n");
91 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
93 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
95 exitEglError("Failed to bind API");
96 // get an appropriate config
99 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE){
100 exitEglError("Failed to choose config");
103 fprintf(stderr, "Found no matching EGL configuration\n");
110 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
111 return (VisualID)val;
114 void EGLBackend::createContext(Window window)
116 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
117 surface = eglCreateWindowSurface(display, config, window, NULL);
118 // create an EGL context and use it with the surface
119 context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
120 if (context == EGL_NO_CONTEXT)
121 exitEglError("Failed to create context");
122 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
123 exitEglError("Failed to make context current");
124 printf("Using GL version: %s\n", glGetString(GL_VERSION));
125 // initialise GL utilities
126 resolveFunctionPointers(eglGetProcAddress);
129 EGLBackend::~EGLBackend()
131 if (display != EGL_NO_DISPLAY) {
132 if (context != EGL_NO_CONTEXT) {
133 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
134 eglDestroyContext(display, context);
135 eglDestroySurface(display, surface);
137 eglTerminate(display);
142 void EGLBackend::swapBuffers() const
144 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
145 if (eglSwapBuffers(display, surface) == EGL_FALSE)
146 exitEglError("Failed to swap buffers");
149 void EGLBackend::setSwapInterval(int i) const
151 assert(context != EGL_NO_CONTEXT);
152 // check if swap interval value is supported
154 eglGetConfigAttrib(display, config, EGL_MIN_SWAP_INTERVAL, &val);
156 fprintf(stderr, "Cannot set swap interval to %d, minimum supported value is %d\n", i, val);
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");