forked from apitrace/apitrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glws_egl_xlib.cpp
401 lines (323 loc) · 10.3 KB
/
glws_egl_xlib.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**************************************************************************
*
* Copyright 2011 LunarG, Inc.
* Copyright 2011 Jose Fonseca
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <dlfcn.h>
#include "glproc.hpp"
#include "glws.hpp"
namespace glws {
static Display *display = NULL;
static EGLDisplay eglDisplay = EGL_NO_DISPLAY;
static int screen = 0;
class EglVisual : public Visual
{
public:
EGLConfig config;
XVisualInfo *visinfo;
EglVisual() :
config(0),
visinfo(0)
{}
~EglVisual() {
XFree(visinfo);
}
};
static void describeEvent(const XEvent &event) {
if (0) {
switch (event.type) {
case ConfigureNotify:
std::cerr << "ConfigureNotify";
break;
case Expose:
std::cerr << "Expose";
break;
case KeyPress:
std::cerr << "KeyPress";
break;
case MapNotify:
std::cerr << "MapNotify";
break;
case ReparentNotify:
std::cerr << "ReparentNotify";
break;
default:
std::cerr << "Event " << event.type;
}
std::cerr << " " << event.xany.window << "\n";
}
}
class EglDrawable : public Drawable
{
public:
Window window;
EGLSurface surface;
EGLint api;
EglDrawable(const Visual *vis, int w, int h) :
Drawable(vis, w, h), api(EGL_OPENGL_ES_API)
{
XVisualInfo *visinfo = static_cast<const EglVisual *>(visual)->visinfo;
Window root = RootWindow(display, screen);
/* window attributes */
XSetWindowAttributes attr;
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask;
unsigned long mask;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
int x = 0, y = 0;
window = XCreateWindow(
display, root,
x, y, width, height,
0,
visinfo->depth,
InputOutput,
visinfo->visual,
mask,
&attr);
XSizeHints sizehints;
sizehints.x = x;
sizehints.y = y;
sizehints.width = width;
sizehints.height = height;
sizehints.flags = USSize | USPosition;
XSetNormalHints(display, window, &sizehints);
const char *name = "glretrace";
XSetStandardProperties(
display, window, name, name,
None, (char **)NULL, 0, &sizehints);
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
EGLConfig config = static_cast<const EglVisual *>(visual)->config;
surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
}
void waitForEvent(int type) {
XEvent event;
do {
XWindowEvent(display, window, StructureNotifyMask, &event);
describeEvent(event);
} while (event.type != type);
}
~EglDrawable() {
eglDestroySurface(eglDisplay, surface);
XDestroyWindow(display, window);
}
void
resize(int w, int h) {
if (w == width && h == height) {
return;
}
eglWaitClient();
// We need to ensure that pending events are processed here, and XSync
// with discard = True guarantees that, but it appears the limited
// event processing we do so far is sufficient
//XSync(display, True);
Drawable::resize(w, h);
XResizeWindow(display, window, w, h);
// Tell the window manager to respect the requested size
XSizeHints size_hints;
size_hints.max_width = size_hints.min_width = w;
size_hints.max_height = size_hints.min_height = h;
size_hints.flags = PMinSize | PMaxSize;
XSetWMNormalHints(display, window, &size_hints);
waitForEvent(ConfigureNotify);
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
}
void show(void) {
if (visible) {
return;
}
eglWaitClient();
XMapWindow(display, window);
waitForEvent(MapNotify);
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
Drawable::show();
}
void swapBuffers(void) {
eglBindAPI(api);
eglSwapBuffers(eglDisplay, surface);
}
};
class EglContext : public Context
{
public:
EGLContext context;
EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
Context(vis, prof),
context(ctx)
{}
~EglContext() {
eglDestroyContext(eglDisplay, context);
}
};
/**
* Load the symbols from the specified shared object into global namespace, so
* that they can be later found by dlsym(RTLD_NEXT, ...);
*/
static void
load(const char *filename)
{
if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
std::cerr << "error: unable to open " << filename << "\n";
exit(1);
}
}
void
init(void) {
load("libEGL.so.1");
display = XOpenDisplay(NULL);
if (!display) {
std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
exit(1);
}
screen = DefaultScreen(display);
eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
if (eglDisplay == EGL_NO_DISPLAY) {
std::cerr << "error: unable to get EGL display\n";
XCloseDisplay(display);
exit(1);
}
EGLint major, minor;
if (!eglInitialize(eglDisplay, &major, &minor)) {
std::cerr << "error: unable to initialize EGL display\n";
XCloseDisplay(display);
exit(1);
}
}
void
cleanup(void) {
if (display) {
eglTerminate(eglDisplay);
XCloseDisplay(display);
display = NULL;
}
}
Visual *
createVisual(bool doubleBuffer) {
EglVisual *visual = new EglVisual();
// possible combinations
const EGLint api_bits[7] = {
EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
EGL_OPENGL_ES_BIT,
EGL_OPENGL_ES2_BIT,
EGL_OPENGL_BIT,
};
for (int i = 0; i < 7; i++) {
Attributes<EGLint> attribs;
attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
attribs.add(EGL_RED_SIZE, 1);
attribs.add(EGL_GREEN_SIZE, 1);
attribs.add(EGL_BLUE_SIZE, 1);
attribs.add(EGL_ALPHA_SIZE, 1);
attribs.add(EGL_DEPTH_SIZE, 1);
attribs.add(EGL_STENCIL_SIZE, 1);
attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
attribs.end(EGL_NONE);
EGLint num_configs, vid;
if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
num_configs == 1 &&
eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
XVisualInfo templ;
int num_visuals;
templ.visualid = vid;
visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
break;
}
}
assert(visual->visinfo);
return visual;
}
Drawable *
createDrawable(const Visual *visual, int width, int height)
{
return new EglDrawable(visual, width, height);
}
Context *
createContext(const Visual *_visual, Context *shareContext, Profile profile)
{
const EglVisual *visual = static_cast<const EglVisual *>(_visual);
EGLContext share_context = EGL_NO_CONTEXT;
EGLContext context;
Attributes<EGLint> attribs;
if (shareContext) {
share_context = static_cast<EglContext*>(shareContext)->context;
}
EGLint api = eglQueryAPI();
switch (profile) {
case PROFILE_COMPAT:
load("libGL.so.1");
eglBindAPI(EGL_OPENGL_API);
break;
case PROFILE_ES1:
load("libGLESv1_CM.so.1");
eglBindAPI(EGL_OPENGL_ES_API);
break;
case PROFILE_ES2:
load("libGLESv2.so.2");
eglBindAPI(EGL_OPENGL_ES_API);
attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
break;
}
attribs.end(EGL_NONE);
context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
if (!context)
return NULL;
eglBindAPI(api);
return new EglContext(visual, profile, context);
}
bool
makeCurrent(Drawable *drawable, Context *context)
{
if (!drawable || !context) {
return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
} else {
EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
EglContext *eglContext = static_cast<EglContext *>(context);
EGLBoolean ok;
ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
eglDrawable->surface, eglContext->context);
if (ok) {
EGLint api;
eglQueryContext(eglDisplay, eglContext->context,
EGL_CONTEXT_CLIENT_TYPE, &api);
eglDrawable->api = api;
}
return ok;
}
}
bool
processEvents(void) {
while (XPending(display) > 0) {
XEvent event;
XNextEvent(display, &event);
describeEvent(event);
}
return true;
}
} /* namespace glws */