]> git.cworth.org Git - apitrace/blob - glws_egl_xlib.cpp
Use libGLESv2.so.2 as the OpenGL ES 2.0 library SONAME.
[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, 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(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) {
274     EglVisual *visual = new EglVisual();
275     // possible combinations
276     const EGLint api_bits[7] = {
277         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
278         EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
279         EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
280         EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
281         EGL_OPENGL_ES_BIT,
282         EGL_OPENGL_ES2_BIT,
283         EGL_OPENGL_BIT,
284     };
285
286     for (int i = 0; i < 7; i++) {
287         Attributes<EGLint> attribs;
288
289         attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
290         attribs.add(EGL_RED_SIZE, 1);
291         attribs.add(EGL_GREEN_SIZE, 1);
292         attribs.add(EGL_BLUE_SIZE, 1);
293         attribs.add(EGL_ALPHA_SIZE, 1);
294         attribs.add(EGL_DEPTH_SIZE, 1);
295         attribs.add(EGL_STENCIL_SIZE, 1);
296         attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
297         attribs.end(EGL_NONE);
298
299         EGLint num_configs, vid;
300         if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
301             num_configs == 1 &&
302             eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
303             XVisualInfo templ;
304             int num_visuals;
305
306             templ.visualid = vid;
307             visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
308             break;
309         }
310     }
311
312     assert(visual->visinfo);
313
314     return visual;
315 }
316
317 Drawable *
318 createDrawable(const Visual *visual, int width, int height)
319 {
320     return new EglDrawable(visual, width, height);
321 }
322
323 Context *
324 createContext(const Visual *_visual, Context *shareContext, Profile profile)
325 {
326     const EglVisual *visual = static_cast<const EglVisual *>(_visual);
327     EGLContext share_context = EGL_NO_CONTEXT;
328     EGLContext context;
329     Attributes<EGLint> attribs;
330
331     if (shareContext) {
332         share_context = static_cast<EglContext*>(shareContext)->context;
333     }
334
335     EGLint api = eglQueryAPI();
336
337     switch (profile) {
338     case PROFILE_COMPAT:
339         load("libGL.so.1");
340         eglBindAPI(EGL_OPENGL_API);
341         break;
342     case PROFILE_ES1:
343         load("libGLESv1_CM.so.1");
344         eglBindAPI(EGL_OPENGL_ES_API);
345         break;
346     case PROFILE_ES2:
347         load("libGLESv2.so.2");
348         eglBindAPI(EGL_OPENGL_ES_API);
349         attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
350         break;
351     }
352
353     attribs.end(EGL_NONE);
354
355     context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
356     if (!context)
357         return NULL;
358
359     eglBindAPI(api);
360
361     return new EglContext(visual, profile, context);
362 }
363
364 bool
365 makeCurrent(Drawable *drawable, Context *context)
366 {
367     if (!drawable || !context) {
368         return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
369     } else {
370         EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
371         EglContext *eglContext = static_cast<EglContext *>(context);
372         EGLBoolean ok;
373
374         ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
375                             eglDrawable->surface, eglContext->context);
376
377         if (ok) {
378             EGLint api;
379
380             eglQueryContext(eglDisplay, eglContext->context,
381                             EGL_CONTEXT_CLIENT_TYPE, &api);
382
383             eglDrawable->api = api;
384         }
385
386         return ok;
387     }
388 }
389
390 bool
391 processEvents(void) {
392     while (XPending(display) > 0) {
393         XEvent event;
394         XNextEvent(display, &event);
395         describeEvent(event);
396     }
397     return true;
398 }
399
400
401 } /* namespace glws */