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 static const char *eglErrorToString(EGLint e)
29 #define CASE(name) case name: return #name
32 CASE(EGL_NOT_INITIALIZED);
35 CASE(EGL_BAD_ATTRIBUTE);
36 CASE(EGL_BAD_CONTEXT);
38 CASE(EGL_BAD_CURRENT_SURFACE);
39 CASE(EGL_BAD_DISPLAY);
40 default: return "<unknown>";
45 static void exitEglError(const char *what)
47 EGLint e = eglGetError();
48 fprintf(stderr, "EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
52 static const EGLint context_attribs[] = {
54 EGL_CONTEXT_CLIENT_VERSION, 2,
58 static const EGLint config_attribs[] = {
62 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
66 VisualID EGLBackend::initialize(Display *xDisplay)
68 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
69 // get connection and bind API
70 EGLint eglMajor, eglMinor;
71 display = eglGetDisplay(xDisplay);
72 if (display == EGL_NO_DISPLAY)
73 exitEglError("Failed to get EGL display");
74 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
75 exitEglError("Failed to initialize EGL");
76 printf("Using EGL version %d.%d\n", eglMajor, eglMinor);
78 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
80 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
82 exitEglError("Failed to bind API");
83 // get an appropriate config
86 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE || count == 0)
87 exitEglError("Failed to choose config");
92 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
96 void EGLBackend::createContext(Window window)
98 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
99 surface = eglCreateWindowSurface(display, config, window, NULL);
100 // create an EGL context and use it with the surface
101 context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
102 if (context == EGL_NO_CONTEXT)
103 exitEglError("Failed to create context");
104 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
105 exitEglError("Failed to make context current");
108 EGLBackend::~EGLBackend()
110 if (display != EGL_NO_DISPLAY) {
111 if (context != EGL_NO_CONTEXT) {
112 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
113 eglDestroyContext(display, context);
114 eglDestroySurface(display, surface);
116 eglTerminate(display);
121 void EGLBackend::swapBuffers() const
123 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
124 if (eglSwapBuffers(display, surface) == EGL_FALSE)
125 exitEglError("Failed to swap buffers");
128 void EGLBackend::setSwapInterval(int i) const
130 assert(context != EGL_NO_CONTEXT);
131 // check if swap interval value is supported
133 eglGetConfigAttrib(display, config, EGL_MIN_SWAP_INTERVAL, &val);
135 fprintf(stderr, "Cannot set swap interval to %d, minimum supported value is %d\n", i, val);
138 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
140 fprintf(stderr, "Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
144 if (eglSwapInterval(display, i) == EGL_FALSE)
145 exitEglError("Failed to set swap interval");