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, bool pbuffer) :
96 Drawable(vis, w, h, pbuffer),
97 api(EGL_OPENGL_ES_API)
99 XVisualInfo *visinfo = static_cast<const EglVisual *>(visual)->visinfo;
101 Window root = RootWindow(display, screen);
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;
111 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
115 window = XCreateWindow(
125 XSizeHints sizehints;
128 sizehints.width = width;
129 sizehints.height = height;
130 sizehints.flags = USSize | USPosition;
131 XSetNormalHints(display, window, &sizehints);
133 const char *name = "glretrace";
134 XSetStandardProperties(
135 display, window, name, name,
136 None, (char **)NULL, 0, &sizehints);
138 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
140 EGLConfig config = static_cast<const EglVisual *>(visual)->config;
141 surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
144 void waitForEvent(int type) {
147 XWindowEvent(display, window, StructureNotifyMask, &event);
148 describeEvent(event);
149 } while (event.type != type);
153 eglDestroySurface(eglDisplay, surface);
155 XDestroyWindow(display, window);
156 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
161 EGLContext currentContext = eglGetCurrentContext();
162 EGLSurface currentDrawSurface = eglGetCurrentSurface(EGL_DRAW);
163 EGLSurface currentReadSurface = eglGetCurrentSurface(EGL_READ);
164 bool rebindDrawSurface = currentDrawSurface == surface;
165 bool rebindReadSurface = currentReadSurface == surface;
167 if (rebindDrawSurface || rebindReadSurface) {
168 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
171 eglDestroySurface(eglDisplay, surface);
173 EGLConfig config = static_cast<const EglVisual *>(visual)->config;
174 surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
176 if (rebindDrawSurface || rebindReadSurface) {
177 eglMakeCurrent(eglDisplay, surface, surface, currentContext);
182 resize(int w, int h) {
183 if (w == width && h == height) {
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);
194 Drawable::resize(w, h);
196 XResizeWindow(display, window, w, h);
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);
205 waitForEvent(ConfigureNotify);
207 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
210 * Some implementations won't update the backbuffer unless we recreate
217 eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
218 eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
220 if (eglWidth != width || eglHeight != height) {
223 eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
224 eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
227 assert(eglWidth == width);
228 assert(eglHeight == height);
238 XMapWindow(display, window);
240 waitForEvent(MapNotify);
242 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
247 void swapBuffers(void) {
249 eglSwapBuffers(eglDisplay, surface);
254 class EglContext : public Context
259 EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
265 eglDestroyContext(eglDisplay, context);
270 * Load the symbols from the specified shared object into global namespace, so
271 * that they can be later found by dlsym(RTLD_NEXT, ...);
274 load(const char *filename)
276 if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
277 std::cerr << "error: unable to open " << filename << "\n";
286 display = XOpenDisplay(NULL);
288 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
292 screen = DefaultScreen(display);
294 eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
295 if (eglDisplay == EGL_NO_DISPLAY) {
296 std::cerr << "error: unable to get EGL display\n";
297 XCloseDisplay(display);
302 if (!eglInitialize(eglDisplay, &major, &minor)) {
303 std::cerr << "error: unable to initialize EGL display\n";
304 XCloseDisplay(display);
312 eglTerminate(eglDisplay);
313 XCloseDisplay(display);
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,
327 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
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,
336 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
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,
345 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
349 const EGLint *api_bits;
353 api_bits = api_bits_gl;
356 api_bits = api_bits_gles1;
359 api_bits = api_bits_gles2;
365 for (int i = 0; i < 7; i++) {
366 Attributes<EGLint> attribs;
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);
378 EGLint num_configs, vid;
379 if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
381 eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
385 templ.visualid = vid;
386 visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
391 assert(visual->visinfo);
397 createDrawable(const Visual *visual, int width, int height, bool pbuffer)
399 return new EglDrawable(visual, width, height, pbuffer);
403 createContext(const Visual *_visual, Context *shareContext, Profile profile, bool debug)
405 const EglVisual *visual = static_cast<const EglVisual *>(_visual);
406 EGLContext share_context = EGL_NO_CONTEXT;
408 Attributes<EGLint> attribs;
411 share_context = static_cast<EglContext*>(shareContext)->context;
414 EGLint api = eglQueryAPI();
419 eglBindAPI(EGL_OPENGL_API);
425 load("libGLESv1_CM.so.1");
426 eglBindAPI(EGL_OPENGL_ES_API);
429 load("libGLESv2.so.2");
430 eglBindAPI(EGL_OPENGL_ES_API);
431 attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
437 attribs.end(EGL_NONE);
439 context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
445 return new EglContext(visual, profile, context);
449 makeCurrent(Drawable *drawable, Context *context)
451 if (!drawable || !context) {
452 return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
454 EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
455 EglContext *eglContext = static_cast<EglContext *>(context);
458 ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
459 eglDrawable->surface, eglContext->context);
464 eglQueryContext(eglDisplay, eglContext->context,
465 EGL_CONTEXT_CLIENT_TYPE, &api);
467 eglDrawable->api = api;
475 processEvents(void) {
476 while (XPending(display) > 0) {
478 XNextEvent(display, &event);
479 describeEvent(event);
485 } /* namespace glws */