1 #include "eglbackend.h"
7 #include <EGL/eglext.h>
9 static const char *eglErrorToString(EGLint e)
11 #define CASE(name) case name: return #name
14 CASE(EGL_NOT_INITIALIZED);
17 CASE(EGL_BAD_ATTRIBUTE);
18 CASE(EGL_BAD_CONTEXT);
20 CASE(EGL_BAD_CURRENT_SURFACE);
21 CASE(EGL_BAD_DISPLAY);
22 default: return "<unknown>";
27 static void exitEglError(const char *what)
29 EGLint e = eglGetError();
30 fprintf(stderr, "EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
34 /* attributes for a double buffered visual in RGBA format with at least
36 static const EGLint config_attribs[] = {
40 EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
44 VisualID EGLBackend::initialize(Display *xDisplay)
46 if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
47 // get connection and bind API
48 EGLint eglMajor, eglMinor;
49 display = eglGetDisplay(xDisplay);
50 if (display == EGL_NO_DISPLAY)
51 exitEglError("Failed to get EGL display");
52 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
53 exitEglError("Failed to initialize EGL");
54 printf("Using EGL version %d.%d\n", eglMajor, eglMinor);
55 eglBindAPI(EGL_OPENGL_API);
56 // get an appropriate config
59 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE || count == 0)
60 exitEglError("Failed to choose config");
65 eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
69 void EGLBackend::createContext(Window window)
71 assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
72 surface = eglCreateWindowSurface(display, config, window, NULL);
73 /* create an EGL context and use it with the surface */
74 context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
75 if (context == EGL_NO_CONTEXT)
76 exitEglError("Failed to create context");
77 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
78 exitEglError("Failed to make context current");
81 EGLBackend::~EGLBackend()
83 if (display != EGL_NO_DISPLAY) {
84 if (context != EGL_NO_CONTEXT) {
85 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
86 eglDestroyContext(display, context);
87 eglDestroySurface(display, surface);
89 eglTerminate(display);
94 void EGLBackend::swapBuffers() const
96 assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
97 eglSwapBuffers(display, surface);
100 void EGLBackend::setSwapInterval(int i) const
102 assert(context != EGL_NO_CONTEXT);
103 eglSwapInterval(display, i);