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