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);
159 resize(int w, int h) {
160 if (w == width && h == height) {
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);
171 Drawable::resize(w, h);
173 XResizeWindow(display, window, w, h);
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);
182 waitForEvent(ConfigureNotify);
184 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
194 XMapWindow(display, window);
196 waitForEvent(MapNotify);
198 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
203 void swapBuffers(void) {
205 eglSwapBuffers(eglDisplay, surface);
210 class EglContext : public Context
215 EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
221 eglDestroyContext(eglDisplay, context);
226 * Load the symbols from the specified shared object into global namespace, so
227 * that they can be later found by dlsym(RTLD_NEXT, ...);
230 load(const char *filename)
232 if (!dlopen(filename, RTLD_GLOBAL | RTLD_LAZY)) {
233 std::cerr << "error: unable to open " << filename << "\n";
242 display = XOpenDisplay(NULL);
244 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
248 screen = DefaultScreen(display);
250 eglDisplay = eglGetDisplay((EGLNativeDisplayType)display);
251 if (eglDisplay == EGL_NO_DISPLAY) {
252 std::cerr << "error: unable to get EGL display\n";
253 XCloseDisplay(display);
258 if (!eglInitialize(eglDisplay, &major, &minor)) {
259 std::cerr << "error: unable to initialize EGL display\n";
260 XCloseDisplay(display);
268 eglTerminate(eglDisplay);
269 XCloseDisplay(display);
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,
283 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
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,
292 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
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,
301 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
305 const EGLint *api_bits;
309 api_bits = api_bits_gl;
312 api_bits = api_bits_gles1;
315 api_bits = api_bits_gles2;
321 for (int i = 0; i < 7; i++) {
322 Attributes<EGLint> attribs;
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);
334 EGLint num_configs, vid;
335 if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
337 eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
341 templ.visualid = vid;
342 visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
347 assert(visual->visinfo);
353 createDrawable(const Visual *visual, int width, int height)
355 return new EglDrawable(visual, width, height);
359 createContext(const Visual *_visual, Context *shareContext, Profile profile)
361 const EglVisual *visual = static_cast<const EglVisual *>(_visual);
362 EGLContext share_context = EGL_NO_CONTEXT;
364 Attributes<EGLint> attribs;
367 share_context = static_cast<EglContext*>(shareContext)->context;
370 EGLint api = eglQueryAPI();
375 eglBindAPI(EGL_OPENGL_API);
381 load("libGLESv1_CM.so.1");
382 eglBindAPI(EGL_OPENGL_ES_API);
385 load("libGLESv2.so.2");
386 eglBindAPI(EGL_OPENGL_ES_API);
387 attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
393 attribs.end(EGL_NONE);
395 context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
401 return new EglContext(visual, profile, context);
405 makeCurrent(Drawable *drawable, Context *context)
407 if (!drawable || !context) {
408 return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
410 EglDrawable *eglDrawable = static_cast<EglDrawable *>(drawable);
411 EglContext *eglContext = static_cast<EglContext *>(context);
414 ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
415 eglDrawable->surface, eglContext->context);
420 eglQueryContext(eglDisplay, eglContext->context,
421 EGL_CONTEXT_CLIENT_TYPE, &api);
423 eglDrawable->api = api;
431 processEvents(void) {
432 while (XPending(display) > 0) {
434 XNextEvent(display, &event);
435 describeEvent(event);
441 } /* namespace glws */