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[] = {
63 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
65 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
70 VisualID EGLBackend::initialize(Display *xDisplay)
72 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
73 // get connection and bind API
74 EGLint eglMajor, eglMinor;
75 display = eglGetDisplay(xDisplay);
76 if (display == EGL_NO_DISPLAY)
77 exitEglError("Failed to get EGL display");
78 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
79 exitEglError("Failed to initialize EGL");
80 printf("Using EGL version %d.%d\n", eglMajor, eglMinor);
81 if (eglMajor == 1 && eglMinor < 3) {
82 fprintf(stderr, "Need at least EGL 1.3 to function properly\n");
86 if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE)
88 if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
90 exitEglError("Failed to bind API");
91 // get an appropriate config
94 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE){
95 exitEglError("Failed to choose config");
98 fprintf(stderr, "Found no matching EGL configuration\n");
105 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
106 return (VisualID)val;
109 void EGLBackend::createContext(Window window)
111 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
112 surface = eglCreateWindowSurface(display, config, window, NULL);
113 // create an EGL context and use it with the surface
114 context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attribs);
115 if (context == EGL_NO_CONTEXT)
116 exitEglError("Failed to create context");
117 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
118 exitEglError("Failed to make context current");
121 EGLBackend::~EGLBackend()
123 if (display != EGL_NO_DISPLAY) {
124 if (context != EGL_NO_CONTEXT) {
125 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
126 eglDestroyContext(display, context);
127 eglDestroySurface(display, surface);
129 eglTerminate(display);
134 void EGLBackend::swapBuffers() const
136 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
137 if (eglSwapBuffers(display, surface) == EGL_FALSE)
138 exitEglError("Failed to swap buffers");
141 void EGLBackend::setSwapInterval(int i) const
143 assert(context != EGL_NO_CONTEXT);
144 // check if swap interval value is supported
146 eglGetConfigAttrib(display, config, EGL_MIN_SWAP_INTERVAL, &val);
148 fprintf(stderr, "Cannot set swap interval to %d, minimum supported value is %d\n", i, val);
151 eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
153 fprintf(stderr, "Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
157 if (eglSwapInterval(display, i) == EGL_FALSE)
158 exitEglError("Failed to set swap interval");