]> git.cworth.org Git - apitrace/blob - retrace/glws_egl_xlib.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / retrace / glws_egl_xlib.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 LunarG, Inc.
4  * Copyright 2011 Jose Fonseca
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 #include <assert.h>
28 #include <stdlib.h>
29
30 #include <iostream>
31
32 #include <dlfcn.h>
33
34 #include "glproc.hpp"
35 #include "glws.hpp"
36
37
38 namespace glws {
39
40
41 static Display *display = NULL;
42 static EGLDisplay eglDisplay = EGL_NO_DISPLAY;
43 static int screen = 0;
44
45
46 class EglVisual : public Visual
47 {
48 public:
49     EGLConfig config;
50     XVisualInfo *visinfo;
51
52     EglVisual() :
53         config(0),
54         visinfo(0)
55     {}
56
57     ~EglVisual() {
58         XFree(visinfo);
59     }
60 };
61
62
63 static void describeEvent(const XEvent &event) {
64     if (0) {
65         switch (event.type) {
66         case ConfigureNotify:
67             std::cerr << "ConfigureNotify";
68             break;
69         case Expose:
70             std::cerr << "Expose";
71             break;
72         case KeyPress:
73             std::cerr << "KeyPress";
74             break;
75         case MapNotify:
76             std::cerr << "MapNotify";
77             break;
78         case ReparentNotify:
79             std::cerr << "ReparentNotify";
80             break;
81         default:
82             std::cerr << "Event " << event.type;
83         }
84         std::cerr << " " << event.xany.window << "\n";
85     }
86 }
87
88 class EglDrawable : public Drawable
89 {
90 public:
91     Window window;
92     EGLSurface surface;
93     EGLint api;
94
95     EglDrawable(const Visual *vis, int w, int h, bool pbuffer) :
96         Drawable(vis, w, h, pbuffer),
97         api(EGL_OPENGL_ES_API)
98     {
99         XVisualInfo *visinfo = static_cast<const EglVisual *>(visual)->visinfo;
100
101         Window root = RootWindow(display, screen);
102
103         /* window attributes */
104         XSetWindowAttributes attr;
105         attr.background_pixel = 0;
106         attr.border_pixel = 0;
107         attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
108         attr.event_mask = StructureNotifyMask;
109
110         unsigned long mask;
111         mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
112
113         int x = 0, y = 0;
114
115         window = XCreateWindow(
116             display, root,
117             x, y, width, height,
118             0,
119             visinfo->depth,
120             InputOutput,
121             visinfo->visual,
122             mask,
123             &attr);
124
125         XSizeHints sizehints;
126         sizehints.x = x;
127         sizehints.y = y;
128         sizehints.width  = width;
129         sizehints.height = height;
130         sizehints.flags = USSize | USPosition;
131         XSetNormalHints(display, window, &sizehints);
132
133         const char *name = "glretrace";
134         XSetStandardProperties(
135             display, window, name, name,
136             None, (char **)NULL, 0, &sizehints);
137
138         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
139
140         EGLConfig config = static_cast<const EglVisual *>(visual)->config;
141         surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
142     }
143
144     void waitForEvent(int type) {
145         XEvent event;
146         do {
147             XWindowEvent(display, window, StructureNotifyMask, &event);
148             describeEvent(event);
149         } while (event.type != type);
150     }
151
152     ~EglDrawable() {
153         eglDestroySurface(eglDisplay, surface);
154         eglWaitClient();
155         XDestroyWindow(display, window);
156         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
157     }
158
159     void
160     recreate(void) {
161         EGLContext currentContext = eglGetCurrentContext();
162         EGLSurface currentDrawSurface = eglGetCurrentSurface(EGL_DRAW);
163         EGLSurface currentReadSurface = eglGetCurrentSurface(EGL_DRAW);
164         bool rebindDrawSurface = currentDrawSurface == surface;
165         bool rebindReadSurface = currentReadSurface == surface;
166
167         if (rebindDrawSurface || rebindReadSurface) {
168             eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
169         }
170
171         eglDestroySurface(eglDisplay, surface);
172
173         EGLConfig config = static_cast<const EglVisual *>(visual)->config;
174         surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
175
176         if (rebindDrawSurface || rebindReadSurface) {
177             eglMakeCurrent(eglDisplay, surface, surface, currentContext);
178         }
179     }
180
181     void
182     resize(int w, int h) {
183         if (w == width && h == height) {
184             return;
185         }
186
187         eglWaitClient();
188
189         // We need to ensure that pending events are processed here, and XSync
190         // with discard = True guarantees that, but it appears the limited
191         // event processing we do so far is sufficient
192         //XSync(display, True);
193
194         Drawable::resize(w, h);
195
196         XResizeWindow(display, window, w, h);
197
198         // Tell the window manager to respect the requested size
199         XSizeHints size_hints;
200         size_hints.max_width  = size_hints.min_width  = w;
201         size_hints.max_height = size_hints.min_height = h;
202         size_hints.flags = PMinSize | PMaxSize;
203         XSetWMNormalHints(display, window, &size_hints);
204
205         waitForEvent(ConfigureNotify);
206
207         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
208
209         /*
210          * Some implementations won't update the backbuffer unless we recreate
211          * the EGL surface.
212          */
213
214         int eglWidth;
215         int eglHeight;
216
217         eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
218         eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
219
220         if (eglWidth != width || eglHeight != height) {
221             recreate();
222
223             eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
224             eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
225         }
226
227         assert(eglWidth == width);
228         assert(eglHeight == height);
229     }
230
231     void show(void) {
232         if (visible) {
233             return;
234         }
235
236         eglWaitClient();
237
238         XMapWindow(display, window);
239
240         waitForEvent(MapNotify);
241
242         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
243
244         Drawable::show();
245     }
246
247     void swapBuffers(void) {
248         eglBindAPI(api);
249         eglSwapBuffers(eglDisplay, surface);
250     }
251 };
252
253
254 class EglContext : public Context
255 {
256 public:
257     EGLContext context;
258
259     EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
260         Context(vis, prof),
261         context(ctx)
262     {}
263
264     ~EglContext() {
265         eglDestroyContext(eglDisplay, context);
266     }
267 };
268
269 /**
270  * Load the symbols from the specified shared object into global namespace, so
271  * that they can be later found by dlsym(RTLD_NEXT, ...);
272  */
273 static void
274 load(const char *filename)
275 {
276     if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
277         std::cerr << "error: unable to open " << filename << "\n";
278         exit(1);
279     }
280 }
281
282 void
283 init(void) {
284     load("libEGL.so.1");
285
286     display = XOpenDisplay(NULL);
287     if (!display) {
288         std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
289         exit(1);
290     }
291
292     screen = DefaultScreen(display);
293
294     eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
295     if (eglDisplay == EGL_NO_DISPLAY) {
296         std::cerr << "error: unable to get EGL display\n";
297         XCloseDisplay(display);
298         exit(1);
299     }
300
301     EGLint major, minor;
302     if (!eglInitialize(eglDisplay, &major, &minor)) {
303         std::cerr << "error: unable to initialize EGL display\n";
304         XCloseDisplay(display);
305         exit(1);
306     }
307 }
308
309 void
310 cleanup(void) {
311     if (display) {
312         eglTerminate(eglDisplay);
313         XCloseDisplay(display);
314         display = NULL;
315     }
316 }
317
318 Visual *
319 createVisual(bool doubleBuffer, Profile profile) {
320     EglVisual *visual = new EglVisual();
321     // possible combinations
322     const EGLint api_bits_gl[7] = {
323         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
324         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
325         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
326         EGL_OPENGL_BIT,
327         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
328         EGL_OPENGL_ES2_BIT,
329         EGL_OPENGL_ES_BIT,
330     };
331     const EGLint api_bits_gles1[7] = {
332         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
333         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
334         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
335         EGL_OPENGL_ES_BIT,
336         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
337         EGL_OPENGL_BIT,
338         EGL_OPENGL_ES2_BIT,
339     };
340     const EGLint api_bits_gles2[7] = {
341         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
342         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
343         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
344         EGL_OPENGL_ES2_BIT,
345         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
346         EGL_OPENGL_BIT,
347         EGL_OPENGL_ES_BIT,
348     };
349     const EGLint *api_bits;
350
351     switch(profile) {
352     case PROFILE_COMPAT:
353         api_bits = api_bits_gl;
354         break;
355     case PROFILE_ES1:
356         api_bits = api_bits_gles1;
357         break;
358     case PROFILE_ES2:
359         api_bits = api_bits_gles2;
360         break;
361     default:
362         return NULL;
363     };
364
365     for (int i = 0; i < 7; i++) {
366         Attributes<EGLint> attribs;
367
368         attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
369         attribs.add(EGL_RED_SIZE, 1);
370         attribs.add(EGL_GREEN_SIZE, 1);
371         attribs.add(EGL_BLUE_SIZE, 1);
372         attribs.add(EGL_ALPHA_SIZE, 1);
373         attribs.add(EGL_DEPTH_SIZE, 1);
374         attribs.add(EGL_STENCIL_SIZE, 1);
375         attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
376         attribs.end(EGL_NONE);
377
378         EGLint num_configs, vid;
379         if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
380             num_configs == 1 &&
381             eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
382             XVisualInfo templ;
383             int num_visuals;
384
385             templ.visualid = vid;
386             visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
387             break;
388         }
389     }
390
391     assert(visual->visinfo);
392
393     return visual;
394 }
395
396 Drawable *
397 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
398 {
399     return new EglDrawable(visual, width, height, pbuffer);
400 }
401
402 Context *
403 createContext(const Visual *_visual, Context *shareContext, Profile profile, bool debug)
404 {
405     const EglVisual *visual = static_cast<const EglVisual *>(_visual);
406     EGLContext share_context = EGL_NO_CONTEXT;
407     EGLContext context;
408     Attributes<EGLint> attribs;
409
410     if (shareContext) {
411         share_context = static_cast<EglContext*>(shareContext)->context;
412     }
413
414     EGLint api = eglQueryAPI();
415
416     switch (profile) {
417     case PROFILE_COMPAT:
418         load("libGL.so.1");
419         eglBindAPI(EGL_OPENGL_API);
420         break;
421     case PROFILE_CORE:
422         assert(0);
423         return NULL;
424     case PROFILE_ES1:
425         load("libGLESv1_CM.so.1");
426         eglBindAPI(EGL_OPENGL_ES_API);
427         break;
428     case PROFILE_ES2:
429         load("libGLESv2.so.2");
430         eglBindAPI(EGL_OPENGL_ES_API);
431         attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
432         break;
433     default:
434         return NULL;
435     }
436
437     attribs.end(EGL_NONE);
438
439     context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
440     if (!context)
441         return NULL;
442
443     eglBindAPI(api);
444
445     return new EglContext(visual, profile, context);
446 }
447
448 bool
449 makeCurrent(Drawable *drawable, Context *context)
450 {
451     if (!drawable || !context) {
452         return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
453     } else {
454         EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
455         EglContext *eglContext = static_cast<EglContext *>(context);
456         EGLBoolean ok;
457
458         ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
459                             eglDrawable->surface, eglContext->context);
460
461         if (ok) {
462             EGLint api;
463
464             eglQueryContext(eglDisplay, eglContext->context,
465                             EGL_CONTEXT_CLIENT_TYPE, &api);
466
467             eglDrawable->api = api;
468         }
469
470         return ok;
471     }
472 }
473
474 bool
475 processEvents(void) {
476     while (XPending(display) > 0) {
477         XEvent event;
478         XNextEvent(display, &event);
479         describeEvent(event);
480     }
481     return true;
482 }
483
484
485 } /* namespace glws */