Remove some trailing spaces
[gltest.git] / eglbackend.cpp
1 /* gltest - small OpenGL tearing test program
2  * Copyright (C) 2012-2013 Ralf Jung <post@ralfj.de>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "eglbackend.h"
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24
25 #include <EGL/eglext.h>
26
27 static const char *eglErrorToString(EGLint e)
28 {
29 #define CASE(name) case name: return #name
30         switch (e) {
31                 CASE(EGL_SUCCESS);
32                 CASE(EGL_NOT_INITIALIZED);
33                 CASE(EGL_BAD_ACCESS);
34                 CASE(EGL_BAD_ALLOC);
35                 CASE(EGL_BAD_ATTRIBUTE);
36                 CASE(EGL_BAD_CONTEXT);
37                 CASE(EGL_BAD_CONFIG);
38                 CASE(EGL_BAD_CURRENT_SURFACE);
39                 CASE(EGL_BAD_DISPLAY);
40                 default: return "<unknown>";
41         }
42 #undef CASE
43 }
44
45 static void exitEglError(const char *what)
46 {
47         EGLint e = eglGetError();
48         fprintf(stderr, "EGL error %d (%s): %s\n", e, eglErrorToString(e), what);
49         exit(1);
50 }
51
52 // attributes for a  visual in RGBA format with at least 4 bits per color
53 static const EGLint config_attribs[] = {
54         EGL_RED_SIZE,             4,
55         EGL_GREEN_SIZE,           4,
56         EGL_BLUE_SIZE,            4,
57         EGL_RENDERABLE_TYPE,      EGL_OPENGL_BIT,
58         EGL_NONE,
59 };
60
61 VisualID EGLBackend::initialize(Display *xDisplay)
62 {
63         if (display == EGL_NO_DISPLAY) { // this implies that the context is also unitialized
64                 // get connection and bind API
65                 EGLint eglMajor, eglMinor;
66                 display = eglGetDisplay(xDisplay);
67                 if (display == EGL_NO_DISPLAY)
68                         exitEglError("Failed to get EGL display");
69                 if (eglInitialize(display, &eglMajor, &eglMinor) == EGL_FALSE)
70                         exitEglError("Failed to initialize EGL");
71                 printf("Using EGL version %d.%d\n", eglMajor, eglMinor);
72                 eglBindAPI(EGL_OPENGL_API);
73                 // get an appropriate config
74                 EGLConfig configs[1];
75                 EGLint count;
76                 if (eglChooseConfig(display, config_attribs, configs, 1, &count) == EGL_FALSE || count == 0)
77                         exitEglError("Failed to choose config");
78                 config = configs[0];
79         }
80         // return visual ID
81         EGLint val;
82         eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &val);
83         return (VisualID)val;
84 }
85
86 void EGLBackend::createContext(Window window)
87 {
88         assert(display != EGL_NO_DISPLAY && context == EGL_NO_CONTEXT);
89         surface = eglCreateWindowSurface(display, config, window, NULL);
90         // create an EGL context and use it with the surface
91         context = eglCreateContext(display, config, EGL_NO_CONTEXT, NULL);
92         if (context == EGL_NO_CONTEXT)
93                 exitEglError("Failed to create context");
94         if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
95                 exitEglError("Failed to make context current");
96 }
97
98 EGLBackend::~EGLBackend()
99 {
100         if (display != EGL_NO_DISPLAY) {
101                 if (context != EGL_NO_CONTEXT) {
102                         eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
103                         eglDestroyContext(display, context);
104                         eglDestroySurface(display, surface);
105                 }
106                 eglTerminate(display);
107                 eglReleaseThread();
108         }
109 }
110
111 void EGLBackend::swapBuffers() const
112 {
113         assert(context != EGL_NO_CONTEXT); // this implies the display is also initialized
114         if (eglSwapBuffers(display, surface) == EGL_FALSE)
115                 exitEglError("Failed to swap buffers");
116 }
117
118 void EGLBackend::setSwapInterval(int i) const
119 {
120         assert(context != EGL_NO_CONTEXT);
121         // check if swap interval value is supported
122         EGLint val;
123         eglGetConfigAttrib(display, config, EGL_MIN_SWAP_INTERVAL, &val);
124         if (i < val) {
125                 fprintf(stderr, "Cannot set swap interval to %d, minimum supported value is %d\n", i, val);
126                 exit(1);
127         }
128         eglGetConfigAttrib(display, config, EGL_MAX_SWAP_INTERVAL, &val);
129         if (i > val) {
130                 fprintf(stderr, "Cannot set swap interval to %d, maximum supported value is %d\n", i, val);
131                 exit(1);
132         }
133         // use it
134         if (eglSwapInterval(display, i) == EGL_FALSE)
135                 exitEglError("Failed to set swap interval");
136 }