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);
154 XDestroyWindow(display, window);
155 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
160 EGLContext currentContext = eglGetCurrentContext();
161 EGLSurface currentDrawSurface = eglGetCurrentSurface(EGL_DRAW);
162 EGLSurface currentReadSurface = eglGetCurrentSurface(EGL_DRAW);
163 bool rebindDrawSurface = currentDrawSurface == surface;
164 bool rebindReadSurface = currentReadSurface == surface;
166 if (rebindDrawSurface || rebindReadSurface) {
167 eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
170 eglDestroySurface(eglDisplay, surface);
172 EGLConfig config = static_cast<const EglVisual *>(visual)->config;
173 surface = eglCreateWindowSurface(eglDisplay, config, (EGLNativeWindowType)window, NULL);
175 if (rebindDrawSurface || rebindReadSurface) {
176 eglMakeCurrent(eglDisplay, surface, surface, currentContext);
181 resize(int w, int h) {
182 if (w == width && h == height) {
188 // We need to ensure that pending events are processed here, and XSync
189 // with discard = True guarantees that, but it appears the limited
190 // event processing we do so far is sufficient
191 //XSync(display, True);
193 Drawable::resize(w, h);
195 XResizeWindow(display, window, w, h);
197 // Tell the window manager to respect the requested size
198 XSizeHints size_hints;
199 size_hints.max_width = size_hints.min_width = w;
200 size_hints.max_height = size_hints.min_height = h;
201 size_hints.flags = PMinSize | PMaxSize;
202 XSetWMNormalHints(display, window, &size_hints);
204 waitForEvent(ConfigureNotify);
206 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
209 * Some implementations won't update the backbuffer unless we recreate
216 eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
217 eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
219 if (eglWidth != width || eglHeight != height) {
222 eglQuerySurface(eglDisplay, surface, EGL_WIDTH, &eglWidth);
223 eglQuerySurface(eglDisplay, surface, EGL_HEIGHT, &eglHeight);
226 assert(eglWidth == width);
227 assert(eglHeight == height);
237 XMapWindow(display, window);
239 waitForEvent(MapNotify);
241 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
246 void swapBuffers(void) {
248 eglSwapBuffers(eglDisplay, surface);
253 class EglContext : public Context
258 EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
264 eglDestroyContext(eglDisplay, context);
269 * Load the symbols from the specified shared object into global namespace, so
270 * that they can be later found by dlsym(RTLD_NEXT, ...);
273 load(const char *filename)
275 if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
276 std::cerr << "error: unable to open " << filename << "\n";
285 display = XOpenDisplay(NULL);
287 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
291 screen = DefaultScreen(display);
293 eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
294 if (eglDisplay == EGL_NO_DISPLAY) {
295 std::cerr << "error: unable to get EGL display\n";
296 XCloseDisplay(display);
301 if (!eglInitialize(eglDisplay, &major, &minor)) {
302 std::cerr << "error: unable to initialize EGL display\n";
303 XCloseDisplay(display);
311 eglTerminate(eglDisplay);
312 XCloseDisplay(display);
318 createVisual(bool doubleBuffer, Profile profile) {
319 EglVisual *visual = new EglVisual();
320 // possible combinations
321 const EGLint api_bits_gl[7] = {
322 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
323 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
324 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
326 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
330 const EGLint api_bits_gles1[7] = {
331 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
332 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
333 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
335 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
339 const EGLint api_bits_gles2[7] = {
340 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
341 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
342 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
344 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
348 const EGLint *api_bits;
352 api_bits = api_bits_gl;
355 api_bits = api_bits_gles1;
358 api_bits = api_bits_gles2;
364 for (int i = 0; i < 7; i++) {
365 Attributes<EGLint> attribs;
367 attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
368 attribs.add(EGL_RED_SIZE, 1);
369 attribs.add(EGL_GREEN_SIZE, 1);
370 attribs.add(EGL_BLUE_SIZE, 1);
371 attribs.add(EGL_ALPHA_SIZE, 1);
372 attribs.add(EGL_DEPTH_SIZE, 1);
373 attribs.add(EGL_STENCIL_SIZE, 1);
374 attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
375 attribs.end(EGL_NONE);
377 EGLint num_configs, vid;
378 if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
380 eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
384 templ.visualid = vid;
385 visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
390 assert(visual->visinfo);
396 createDrawable(const Visual *visual, int width, int height)
398 return new EglDrawable(visual, width, height);
402 createContext(const Visual *_visual, Context *shareContext, Profile profile, bool debug)
404 const EglVisual *visual = static_cast<const EglVisual *>(_visual);
405 EGLContext share_context = EGL_NO_CONTEXT;
407 Attributes<EGLint> attribs;
410 share_context = static_cast<EglContext*>(shareContext)->context;
413 EGLint api = eglQueryAPI();
418 eglBindAPI(EGL_OPENGL_API);
424 load("libGLESv1_CM.so.1");
425 eglBindAPI(EGL_OPENGL_ES_API);
428 load("libGLESv2.so.2");
429 eglBindAPI(EGL_OPENGL_ES_API);
430 attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
436 attribs.end(EGL_NONE);
438 context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
444 return new EglContext(visual, profile, context);
448 makeCurrent(Drawable *drawable, Context *context)
450 if (!drawable || !context) {
451 return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
453 EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
454 EglContext *eglContext = static_cast<EglContext *>(context);
457 ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
458 eglDrawable->surface, eglContext->context);
463 eglQueryContext(eglDisplay, eglContext->context,
464 EGL_CONTEXT_CLIENT_TYPE, &api);
466 eglDrawable->api = api;
474 processEvents(void) {
475 while (XPending(display) > 0) {
477 XNextEvent(display, &event);
478 describeEvent(event);
484 } /* namespace glws */