/
EGLX11Display.cpp
227 lines (189 loc) · 6.38 KB
/
EGLX11Display.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* EGLBenchmark
*
* Author: Jarkko Vatjus-Anttila <jvatjusanttila@gmail.com>
*
* For conditions of distribution and use, see copyright notice in license.txt
*/
#include "EGLX11Display.h"
#include "DebugLog.h"
#include "GLWrapper.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <sys/stat.h>
EGLX11Display::EGLX11Display() :
egl_context(0),
win(0),
x_display(NULL),
egl_display(0),
egl_surface(0)
{
}
EGLX11Display::~EGLX11Display()
{
destroyEGLDisplay();
}
/******************************************************************************
* Display and context creation helpers
*/
int EGLX11Display::createEGLDisplay(int width, int height, bool fullscreen)
{
Window root;
XSetWindowAttributes swa;
XSetWindowAttributes xattr;
Atom wm_state, a_fullscreen;
XWMHints hints;
XEvent xev;
EGLConfig ecfg;
EGLint num_config;
w_width = width;
w_height = height;
w_fullscreen = fullscreen;
/*
* X11 native display initialization
*/
DebugLog::Instance()->MESSAGE(3, "Connecting to X server\n");
x_display = XOpenDisplay(NULL);
if ( x_display == NULL )
{
DebugLog::Instance()->MESSAGE(1, "Error: Unable to connect to X Server\n");
return -1;
}
DebugLog::Instance()->MESSAGE(3, "Querying X root window\n");
root = DefaultRootWindow(x_display);
DebugLog::Instance()->MESSAGE(3, "Creating X11 window\n");
swa.event_mask = ExposureMask | PointerMotionMask | KeyPressMask;
win = XCreateWindow(
x_display, root,
0, 0, width, height, 0,
CopyFromParent, InputOutput,
CopyFromParent, CWEventMask,
&swa );
DebugLog::Instance()->MESSAGE(3, "Updating window attributes\n");
xattr.override_redirect = false;
XChangeWindowAttributes ( x_display, win, CWOverrideRedirect, &xattr );
DebugLog::Instance()->MESSAGE(3, "Setting Window manager hints\n");
hints.input = true;
hints.flags = InputHint;
XSetWMHints(x_display, win, &hints);
// make the window visible on the screen
DebugLog::Instance()->MESSAGE(3, "Making window visible\n");
XMapWindow (x_display, win);
XStoreName (x_display, win, "EGLX11Display");
// get identifiers for the provided atom name strings
wm_state = XInternAtom (x_display, "_NET_WM_STATE", false);
a_fullscreen = false;
if (w_fullscreen == true)
a_fullscreen = XInternAtom (x_display, "_NET_WM_STATE_FULLSCREEN", w_fullscreen);
DebugLog::Instance()->MESSAGE(3, "Updating window event masks\n");
memset ( &xev, 0, sizeof(xev) );
xev.type = ClientMessage;
xev.xclient.window = win;
xev.xclient.message_type = wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1;
xev.xclient.data.l[1] = a_fullscreen;
XSendEvent (
x_display,
DefaultRootWindow ( x_display ),
false,
SubstructureNotifyMask,
&xev );
DebugLog::Instance()->MESSAGE(2, "X11 native display init done!\n");
/*
* Now that the native window is up, we shall initialize EGL
*/
DebugLog::Instance()->MESSAGE(3, "EGL: eglGetDisplay()\n");
egl_display = eglGetDisplay( (EGLNativeDisplayType) x_display );
GLWrapper::Instance()->flushEGLErrors();
if ( egl_display == EGL_NO_DISPLAY ) {
DebugLog::Instance()->MESSAGE(1, "EGL: eglGetDisplay() failed!\n");
return -1;
}
DebugLog::Instance()->MESSAGE(3, "EGL: eglInitialize()\n");
if ( !eglInitialize( egl_display, NULL, NULL ) ) {
GLWrapper::Instance()->flushEGLErrors();
DebugLog::Instance()->MESSAGE(1, "EGL: eglInitialize() failed!\n");
return -1;
}
GLWrapper::Instance()->flushEGLErrors();
DebugLog::Instance()->MESSAGE(3, "EGL: eglChooseConfig()\n");
if ( !eglChooseConfig( egl_display, attr, &ecfg, 1, &num_config ) ) {
GLWrapper::Instance()->flushEGLErrors();
DebugLog::Instance()->MESSAGE(1, "EGL: eglChooseConfig() failed!\n");
return -1;
}
GLWrapper::Instance()->flushEGLErrors();
DebugLog::Instance()->MESSAGE(3, "EGL: EGL configs available?\n");
if ( num_config == 0 ) {
DebugLog::Instance()->MESSAGE(1, "EGL: eglGetDisplay() no configs found!\n");
return -1;
}
DebugLog::Instance()->MESSAGE(3, "EGL: eglCreateWindowSurface()\n");
egl_surface = eglCreateWindowSurface ( egl_display, ecfg, win, NULL );
GLWrapper::Instance()->flushEGLErrors();
if ( egl_surface == EGL_NO_SURFACE ) {
DebugLog::Instance()->MESSAGE(5, "EGL: eglCreateWindowSurface() failed!\n");
return -1;
}
DebugLog::Instance()->MESSAGE(3, "EGL: eglCreateContext()\n");
egl_context = eglCreateContext ( egl_display, ecfg, EGL_NO_CONTEXT, ctxattr );
GLWrapper::Instance()->flushEGLErrors();
if ( egl_context == EGL_NO_CONTEXT ) {
DebugLog::Instance()->MESSAGE(1, "EGL: eglCreateContext() failed!\n");
return -1;
}
DebugLog::Instance()->MESSAGE(3, "EGL: eglMakeCurrent()\n");
eglMakeCurrent( egl_display, egl_surface, egl_surface, egl_context );
GLWrapper::Instance()->flushEGLErrors();
GLWrapper::Instance()->flushGLErrors();
DebugLog::Instance()->MESSAGE(2, "EGL initialization completed!\n");
return 0;
}
int EGLX11Display::destroyEGLDisplay(void)
{
DebugLog::Instance()->MESSAGE(3, "Starting EGL/X11 destruction\n");
if (egl_display != 0)
{
if (egl_context != 0)
eglDestroyContext (egl_display, egl_context);
if (egl_surface != 0)
eglDestroySurface (egl_display, egl_surface);
eglTerminate (egl_display);
}
if (x_display != NULL)
{
if (win != 0)
XDestroyWindow(x_display, win);
XCloseDisplay(x_display);
}
egl_display = 0;
egl_context = 0;
egl_surface = 0;
win = 0;
x_display = NULL;
DebugLog::Instance()->MESSAGE(3, "EGL/X11 Destruction done\n");
return 0;
}
Display * EGLX11Display::getXDisplay(void)
{
return x_display;
}
EGLDisplay EGLX11Display::getEGLDisplay(void)
{
return egl_display;
}
EGLSurface EGLX11Display::getEGLSurface(void)
{
return egl_surface;
}
unsigned int EGLX11Display::getDisplayWidth(void)
{
DebugLog::Instance()->MESSAGE(4, "EGLX11Display: requesting display width %d\n", w_width);
return w_width;
}
unsigned int EGLX11Display::getDisplayHeight(void)
{
DebugLog::Instance()->MESSAGE(4, "EGLX11Display: requesting display height %d\n", w_height);
return w_height;
}