]> git.cworth.org Git - apitrace/blob - glws_egl_xlib.cpp
Merge branch 'trace-threads'
[apitrace] / 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) :
96         Drawable(vis, w, h), api(EGL_OPENGL_ES_API)
97     {
98         XVisualInfo *visinfo = static_cast<const EglVisual *>(visual)->visinfo;
99
100         Window root = RootWindow(display, screen);
101
102         /* window attributes */
103         XSetWindowAttributes attr;
104         attr.background_pixel = 0;
105         attr.border_pixel = 0;
106         attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
107         attr.event_mask = StructureNotifyMask;
108
109         unsigned long mask;
110         mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
111
112         int x = 0, y = 0;
113
114         window = XCreateWindow(
115             display, root,
116             x, y, width, height,
117             0,
118             visinfo->depth,
119             InputOutput,
120             visinfo->visual,
121             mask,
122             &attr);
123
124         XSizeHints sizehints;
125         sizehints.x = x;
126         sizehints.y = y;
127         sizehints.width  = width;
128         sizehints.height = height;
129         sizehints.flags = USSize | USPosition;
130         XSetNormalHints(display, window, &sizehints);
131
132         const char *name = "glretrace";
133         XSetStandardProperties(
134             display, window, name, name,
135             None, (char **)NULL, 0, &sizehints);
136
137         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
138
139         EGLConfig config = static_cast<const EglVisual *>(visual)->config;
140         surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
141     }
142
143     void waitForEvent(int type) {
144         XEvent event;
145         do {
146             XWindowEvent(display, window, StructureNotifyMask, &event);
147             describeEvent(event);
148         } while (event.type != type);
149     }
150
151     ~EglDrawable() {
152         eglDestroySurface(eglDisplay, surface);
153         XDestroyWindow(display, window);
154     }
155
156     void
157     resize(int w, int h) {
158         if (w == width && h == height) {
159             return;
160         }
161
162         eglWaitClient();
163
164         // We need to ensure that pending events are processed here, and XSync
165         // with discard = True guarantees that, but it appears the limited
166         // event processing we do so far is sufficient
167         //XSync(display, True);
168
169         Drawable::resize(w, h);
170
171         XResizeWindow(display, window, w, h);
172
173         // Tell the window manager to respect the requested size
174         XSizeHints size_hints;
175         size_hints.max_width  = size_hints.min_width  = w;
176         size_hints.max_height = size_hints.min_height = h;
177         size_hints.flags = PMinSize | PMaxSize;
178         XSetWMNormalHints(display, window, &size_hints);
179
180         waitForEvent(ConfigureNotify);
181
182         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
183     }
184
185     void show(void) {
186         if (visible) {
187             return;
188         }
189
190         eglWaitClient();
191
192         XMapWindow(display, window);
193
194         waitForEvent(MapNotify);
195
196         eglWaitNative(EGL_CORE_NATIVE_ENGINE);
197
198         Drawable::show();
199     }
200
201     void swapBuffers(void) {
202         eglBindAPI(api);
203         eglSwapBuffers(eglDisplay, surface);
204     }
205 };
206
207
208 class EglContext : public Context
209 {
210 public:
211     EGLContext context;
212
213     EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
214         Context(vis, prof),
215         context(ctx)
216     {}
217
218     ~EglContext() {
219         eglDestroyContext(eglDisplay, context);
220     }
221 };
222
223 /**
224  * Load the symbols from the specified shared object into global namespace, so
225  * that they can be later found by dlsym(RTLD_NEXT, ...);
226  */
227 static void
228 load(const char *filename)
229 {
230     if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
231         std::cerr << "error: unable to open " << filename << "\n";
232         exit(1);
233     }
234 }
235
236 void
237 init(void) {
238     load("libEGL.so.1");
239
240     display = XOpenDisplay(NULL);
241     if (!display) {
242         std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
243         exit(1);
244     }
245
246     screen = DefaultScreen(display);
247
248     eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
249     if (eglDisplay == EGL_NO_DISPLAY) {
250         std::cerr << "error: unable to get EGL display\n";
251         XCloseDisplay(display);
252         exit(1);
253     }
254
255     EGLint major, minor;
256     if (!eglInitialize(eglDisplay, &major, &minor)) {
257         std::cerr << "error: unable to initialize EGL display\n";
258         XCloseDisplay(display);
259         exit(1);
260     }
261 }
262
263 void
264 cleanup(void) {
265     if (display) {
266         eglTerminate(eglDisplay);
267         XCloseDisplay(display);
268         display = NULL;
269     }
270 }
271
272 Visual *
273 createVisual(bool doubleBuffer, Profile profile) {
274     EglVisual *visual = new EglVisual();
275     // possible combinations
276     const EGLint api_bits_gl[7] = {
277         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
278         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
279         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
280         EGL_OPENGL_BIT,
281         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
282         EGL_OPENGL_ES2_BIT,
283         EGL_OPENGL_ES_BIT,
284     };
285     const EGLint api_bits_gles1[7] = {
286         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
287         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
288         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
289         EGL_OPENGL_ES_BIT,
290         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
291         EGL_OPENGL_BIT,
292         EGL_OPENGL_ES2_BIT,
293     };
294     const EGLint api_bits_gles2[7] = {
295         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
296         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
297         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
298         EGL_OPENGL_ES2_BIT,
299         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
300         EGL_OPENGL_BIT,
301         EGL_OPENGL_ES_BIT,
302     };
303     const EGLint *api_bits;
304
305     switch(profile) {
306     case PROFILE_COMPAT:
307         api_bits = api_bits_gl;
308         break;
309     case PROFILE_ES1:
310         api_bits = api_bits_gles1;
311         break;
312     case PROFILE_ES2:
313         api_bits = api_bits_gles2;
314         break;
315     default:
316         return NULL;
317     };
318
319     for (int i = 0; i < 7; i++) {
320         Attributes<EGLint> attribs;
321
322         attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
323         attribs.add(EGL_RED_SIZE, 1);
324         attribs.add(EGL_GREEN_SIZE, 1);
325         attribs.add(EGL_BLUE_SIZE, 1);
326         attribs.add(EGL_ALPHA_SIZE, 1);
327         attribs.add(EGL_DEPTH_SIZE, 1);
328         attribs.add(EGL_STENCIL_SIZE, 1);
329         attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
330         attribs.end(EGL_NONE);
331
332         EGLint num_configs, vid;
333         if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
334             num_configs == 1 &&
335             eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
336             XVisualInfo templ;
337             int num_visuals;
338
339             templ.visualid = vid;
340             visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
341             break;
342         }
343     }
344
345     assert(visual->visinfo);
346
347     return visual;
348 }
349
350 Drawable *
351 createDrawable(const Visual *visual, int width, int height)
352 {
353     return new EglDrawable(visual, width, height);
354 }
355
356 Context *
357 createContext(const Visual *_visual, Context *shareContext, Profile profile)
358 {
359     const EglVisual *visual = static_cast<const EglVisual *>(_visual);
360     EGLContext share_context = EGL_NO_CONTEXT;
361     EGLContext context;
362     Attributes<EGLint> attribs;
363
364     if (shareContext) {
365         share_context = static_cast<EglContext*>(shareContext)->context;
366     }
367
368     EGLint api = eglQueryAPI();
369
370     switch (profile) {
371     case PROFILE_COMPAT:
372         load("libGL.so.1");
373         eglBindAPI(EGL_OPENGL_API);
374         break;
375     case PROFILE_CORE:
376         assert(0);
377         return NULL;
378     case PROFILE_ES1:
379         load("libGLESv1_CM.so.1");
380         eglBindAPI(EGL_OPENGL_ES_API);
381         break;
382     case PROFILE_ES2:
383         load("libGLESv2.so.2");
384         eglBindAPI(EGL_OPENGL_ES_API);
385         attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
386         break;
387     default:
388         return NULL;
389     }
390
391     attribs.end(EGL_NONE);
392
393     context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
394     if (!context)
395         return NULL;
396
397     eglBindAPI(api);
398
399     return new EglContext(visual, profile, context);
400 }
401
402 bool
403 makeCurrent(Drawable *drawable, Context *context)
404 {
405     if (!drawable || !context) {
406         return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
407     } else {
408         EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
409         EglContext *eglContext = static_cast<EglContext *>(context);
410         EGLBoolean ok;
411
412         ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
413                             eglDrawable->surface, eglContext->context);
414
415         if (ok) {
416             EGLint api;
417
418             eglQueryContext(eglDisplay, eglContext->context,
419                             EGL_CONTEXT_CLIENT_TYPE, &api);
420
421             eglDrawable->api = api;
422         }
423
424         return ok;
425     }
426 }
427
428 bool
429 processEvents(void) {
430     while (XPending(display) > 0) {
431         XEvent event;
432         XNextEvent(display, &event);
433         describeEvent(event);
434     }
435     return true;
436 }
437
438
439 } /* namespace glws */