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 **************************************************************************/
40 static Display *display = NULL;
41 static EGLDisplay eglDisplay = EGL_NO_DISPLAY;
42 static int screen = 0;
45 class EglVisual : public Visual
62 static void describeEvent(const XEvent &event) {
66 std::cerr << "ConfigureNotify";
69 std::cerr << "Expose";
72 std::cerr << "KeyPress";
75 std::cerr << "MapNotify";
78 std::cerr << "ReparentNotify";
81 std::cerr << "Event " << event.type;
83 std::cerr << " " << event.xany.window << "\n";
87 class EglDrawable : public Drawable
94 EglDrawable(const Visual *vis, int w, int h) :
95 Drawable(vis, w, h), api(EGL_OPENGL_ES_API)
97 XVisualInfo *visinfo = dynamic_cast<const EglVisual *>(visual)->visinfo;
99 Window root = RootWindow(display, screen);
101 /* window attributes */
102 XSetWindowAttributes attr;
103 attr.background_pixel = 0;
104 attr.border_pixel = 0;
105 attr.colormap = XCreateColormap(display, root, visinfo->visual, AllocNone);
106 attr.event_mask = StructureNotifyMask;
109 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
113 window = XCreateWindow(
123 XSizeHints sizehints;
126 sizehints.width = width;
127 sizehints.height = height;
128 sizehints.flags = USSize | USPosition;
129 XSetNormalHints(display, window, &sizehints);
131 const char *name = "glretrace";
132 XSetStandardProperties(
133 display, window, name, name,
134 None, (char **)NULL, 0, &sizehints);
136 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
138 EGLConfig config = dynamic_cast<const EglVisual *>(visual)->config;
139 surface = eglCreateWindowSurface(eglDisplay, config, window, NULL);
142 void waitForEvent(int type) {
145 XWindowEvent(display, window, StructureNotifyMask, &event);
146 describeEvent(event);
147 } while (event.type != type);
151 eglDestroySurface(eglDisplay, surface);
152 XDestroyWindow(display, window);
156 resize(int w, int h) {
157 if (w == width && h == height) {
163 // We need to ensure that pending events are processed here, and XSync
164 // with discard = True guarantees that, but it appears the limited
165 // event processing we do so far is sufficient
166 //XSync(display, True);
168 Drawable::resize(w, h);
170 XResizeWindow(display, window, w, h);
172 // Tell the window manager to respect the requested size
173 XSizeHints size_hints;
174 size_hints.max_width = size_hints.min_width = w;
175 size_hints.max_height = size_hints.min_height = h;
176 size_hints.flags = PMinSize | PMaxSize;
177 XSetWMNormalHints(display, window, &size_hints);
179 waitForEvent(ConfigureNotify);
181 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
191 XMapWindow(display, window);
193 waitForEvent(MapNotify);
195 eglWaitNative(EGL_CORE_NATIVE_ENGINE);
200 void swapBuffers(void) {
202 eglSwapBuffers(eglDisplay, surface);
207 class EglContext : public Context
212 EglContext(const Visual *vis, Profile prof, EGLContext ctx) :
218 eglDestroyContext(eglDisplay, context);
224 display = XOpenDisplay(NULL);
226 std::cerr << "error: unable to open display " << XDisplayName(NULL) << "\n";
230 screen = DefaultScreen(display);
232 eglDisplay = eglGetDisplay(display);
233 if (eglDisplay == EGL_NO_DISPLAY) {
234 std::cerr << "error: unable to get EGL display\n";
235 XCloseDisplay(display);
240 if (!eglInitialize(eglDisplay, &major, &minor)) {
241 std::cerr << "error: unable to initialize EGL display\n";
242 XCloseDisplay(display);
250 eglTerminate(eglDisplay);
251 XCloseDisplay(display);
257 createVisual(bool doubleBuffer) {
258 EglVisual *visual = new EglVisual();
259 // possible combinations
260 const EGLint api_bits[7] = {
261 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
262 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT,
263 EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT,
264 EGL_OPENGL_BIT | EGL_OPENGL_ES2_BIT,
270 for (int i = 0; i < 7; i++) {
271 Attributes<EGLint> attribs;
273 attribs.add(EGL_SURFACE_TYPE, EGL_WINDOW_BIT);
274 attribs.add(EGL_RED_SIZE, 1);
275 attribs.add(EGL_GREEN_SIZE, 1);
276 attribs.add(EGL_BLUE_SIZE, 1);
277 attribs.add(EGL_ALPHA_SIZE, 1);
278 attribs.add(EGL_DEPTH_SIZE, 1);
279 attribs.add(EGL_STENCIL_SIZE, 1);
280 attribs.add(EGL_RENDERABLE_TYPE, api_bits[i]);
281 attribs.end(EGL_NONE);
283 EGLint num_configs, vid;
284 if (eglChooseConfig(eglDisplay, attribs, &visual->config, 1, &num_configs) &&
286 eglGetConfigAttrib(eglDisplay, visual->config, EGL_NATIVE_VISUAL_ID, &vid)) {
290 templ.visualid = vid;
291 visual->visinfo = XGetVisualInfo(display, VisualIDMask, &templ, &num_visuals);
296 assert(visual->visinfo);
302 createDrawable(const Visual *visual, int width, int height)
304 return new EglDrawable(visual, width, height);
308 createContext(const Visual *_visual, Context *shareContext, Profile profile)
310 const EglVisual *visual = dynamic_cast<const EglVisual *>(_visual);
311 EGLContext share_context = EGL_NO_CONTEXT;
313 Attributes<EGLint> attribs;
316 share_context = dynamic_cast<EglContext*>(shareContext)->context;
319 EGLint api = eglQueryAPI();
323 eglBindAPI(EGL_OPENGL_API);
326 eglBindAPI(EGL_OPENGL_ES_API);
329 eglBindAPI(EGL_OPENGL_ES_API);
330 attribs.add(EGL_CONTEXT_CLIENT_VERSION, 2);
334 attribs.end(EGL_NONE);
336 context = eglCreateContext(eglDisplay, visual->config, share_context, attribs);
342 return new EglContext(visual, profile, context);
346 makeCurrent(Drawable *drawable, Context *context)
348 if (!drawable || !context) {
349 return eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
351 EglDrawable *eglDrawable = dynamic_cast<EglDrawable *>(drawable);
352 EglContext *eglContext = dynamic_cast<EglContext *>(context);
355 ok = eglMakeCurrent(eglDisplay, eglDrawable->surface,
356 eglDrawable->surface, eglContext->context);
361 eglQueryContext(eglDisplay, eglContext->context,
362 EGL_CONTEXT_CLIENT_TYPE, &api);
364 eglDrawable->api = api;
372 processEvents(void) {
373 while (XPending(display) > 0) {
375 XNextEvent(display, &event);
376 describeEvent(event);
382 } /* namespace glws */