1 /**************************************************************************
3 * Copyright 2011 LunarG, Inc.
4 * Copyright 2011 Jose Fonseca
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
41 static Display *display = NULL;
42 static EGLDisplay eglDisplay = EGL_NO_DISPLAY;
43 static int screen = 0;
46 class EglVisual : public Visual
63 static void describeEvent(const XEvent &event) {
67 std::cerr << "ConfigureNotify";
70 std::cerr << "Expose";
73 std::cerr << "KeyPress";
76 std::cerr << "MapNotify";
79 std::cerr << "ReparentNotify";
82 std::cerr << "Event " << event.type;
84 std::cerr << " " << event.xany.window << "\n";
88 class EglDrawable : public Drawable
95 EglDrawable(const Visual *vis, int w, int h) :
96 Drawable(vis, w, h), api(EGL_OPENGL_ES_API)
98 XVisualInfo *visinfo = static_cast<const EglVisual *>(visual)->visinfo;
100 Window root = RootWindow(display, screen);
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;
110 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
114 window = XCreateWindow(
124 XSizeHints sizehints;
127 sizehints.width = width;
128 sizehints.height = height;
129 sizehints.flags = USSize | USPosition;
130 XSetNormalHints(display, window, &sizehints);
132 const char *name = "glretrace";
133 XSetStandardProperties(
134 display, window, name, name,
135 None, (char **)NULL, 0, &sizehints);
137 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
139 EGLConfig config = static_cast<const EglVisual *>(visual)->config;
140 surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
143 void waitForEvent(int type) {
146 XWindowEvent(display, window, StructureNotifyMask, &event);
147 describeEvent(event);
148 } while (event.type != type);
152 eglDestroySurface(eglDisplay, surface);
153 XDestroyWindow(display, window);
157 resize(int w, int h) {
158 if (w == width && h == height) {
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);
169 Drawable::resize(w, h);
171 XResizeWindow(display, window, w, h);
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);
180 waitForEvent(ConfigureNotify);
182 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
192 XMapWindow(display, window);
194 waitForEvent(MapNotify);
196 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
201 void swapBuffers(void) {
203 eglSwapBuffers(eglDisplay, surface);
208 class EglContext : public Context
213 EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
219 eglDestroyContext(eglDisplay, context);
224 * Load the symbols from the specified shared object into global namespace, so
225 * that they can be later found by dlsym(RTLD_NEXT, ...);
228 load(const char *filename)
230 if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
231 std::cerr << "error: unable to open " << filename << "\n";
240 display = XOpenDisplay(NULL);
242 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
246 screen = DefaultScreen(display);
248 eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
249 if (eglDisplay == EGL_NO_DISPLAY) {
250 std::cerr << "error: unable to get EGL display\n";
251 XCloseDisplay(display);
256 if (!eglInitialize(eglDisplay, &major, &minor)) {
257 std::cerr << "error: unable to initialize EGL display\n";
258 XCloseDisplay(display);
266 eglTerminate(eglDisplay);
267 XCloseDisplay(display);
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,
281 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
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,
290 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
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,
299 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
303 const EGLint *api_bits;
307 api_bits = api_bits_gl;
310 api_bits = api_bits_gles1;
313 api_bits = api_bits_gles2;
319 for (int i = 0; i < 7; i++) {
320 Attributes<EGLint> attribs;
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);
332 EGLint num_configs, vid;
333 if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
335 eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
339 templ.visualid = vid;
340 visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
345 assert(visual->visinfo);
351 createDrawable(const Visual *visual, int width, int height)
353 return new EglDrawable(visual, width, height);
357 createContext(const Visual *_visual, Context *shareContext, Profile profile)
359 const EglVisual *visual = static_cast<const EglVisual *>(_visual);
360 EGLContext share_context = EGL_NO_CONTEXT;
362 Attributes<EGLint> attribs;
365 share_context = static_cast<EglContext*>(shareContext)->context;
368 EGLint api = eglQueryAPI();
373 eglBindAPI(EGL_OPENGL_API);
379 load("libGLESv1_CM.so.1");
380 eglBindAPI(EGL_OPENGL_ES_API);
383 load("libGLESv2.so.2");
384 eglBindAPI(EGL_OPENGL_ES_API);
385 attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
391 attribs.end(EGL_NONE);
393 context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
399 return new EglContext(visual, profile, context);
403 makeCurrent(Drawable *drawable, Context *context)
405 if (!drawable || !context) {
406 return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
408 EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
409 EglContext *eglContext = static_cast<EglContext *>(context);
412 ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
413 eglDrawable->surface, eglContext->context);
418 eglQueryContext(eglDisplay, eglContext->context,
419 EGL_CONTEXT_CLIENT_TYPE, &api);
421 eglDrawable->api = api;
429 processEvents(void) {
430 while (XPending(display) > 0) {
432 XNextEvent(display, &event);
433 describeEvent(event);
439 } /* namespace glws */