2 * OpenGL ES 2.0 memory performance estimator
3 * Copyright (C) 2009 Nokia
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * \author Sami Kyöstilä <sami.kyostila@nokia.com>
21 * Native windowing implementation for X11
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
32 EGLBoolean nativeCreateDisplay(EGLNativeDisplayType *pNativeDisplay)
34 *pNativeDisplay = XOpenDisplay(NULL);
38 fprintf(stderr, "XOpenDisplay failed\n");
45 void nativeDestroyDisplay(EGLNativeDisplayType nativeDisplay)
47 XCloseDisplay(nativeDisplay);
50 static int runningOnFremantle(void)
52 /* Somewhat hacky way of detecting fremantle */
53 FILE* f = popen("pgrep hildon-desktop", "r");
59 if (fscanf(f, "%d", &pid) == 1)
68 EGLBoolean nativeCreateWindow(EGLNativeDisplayType nativeDisplay, EGLDisplay dpy, EGLConfig config,
69 const char *title, int width, int height, EGLNativeWindowType *nativeWindow)
71 Window rootWindow = DefaultRootWindow(nativeDisplay);
75 XTextProperty windowTitle;
76 XSetWindowAttributes winAttrs;
77 XWindowAttributes rootAttrs;
78 XVisualInfo visualInfo;
81 if (eglGetConfigAttrib(dpy, config, EGL_NATIVE_VISUAL_ID, &visualId) != EGL_TRUE)
83 fprintf(stderr, "eglGetConfigAttrib failed: %x\n", eglGetError());
87 visualInfo.visualid = visualId;
88 visualInfo.screen = DefaultScreen(nativeDisplay);
89 visual = XGetVisualInfo(nativeDisplay, VisualIDMask | VisualScreenMask, &visualInfo, &visualCount);
93 fprintf(stderr, "XGetVisualInfo failed\n");
97 XGetWindowAttributes(nativeDisplay, rootWindow, &rootAttrs);
99 winAttrs.background_pixmap = None;
100 winAttrs.border_pixel = 0;
101 winAttrs.colormap = XCreateColormap(nativeDisplay, rootWindow, visual->visual, AllocNone);
103 window = XCreateWindow(nativeDisplay, rootWindow, 0, 0,
104 width, height, 0, visual->depth,
105 InputOutput, visual->visual,
106 CWBackPixmap | CWBorderPixel | CWColormap,
111 fprintf(stderr, "XCreateSimpleWindow failed\n");
115 /* Enable non-composited mode if possible */
116 if (XVisualIDFromVisual(rootAttrs.visual) == visualId)
118 Atom nonCompWindow = XInternAtom(nativeDisplay, "_HILDON_NON_COMPOSITED_WINDOW", False);
119 Atom windowType = XInternAtom(nativeDisplay, "_NET_WM_WINDOW_TYPE", False);
120 Atom windowTypeOverride = XInternAtom(nativeDisplay, "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE", False);
123 XChangeProperty(nativeDisplay, window, nonCompWindow, XA_INTEGER, 32, PropModeReplace, (unsigned char*)&one, 1);
125 if (!runningOnFremantle())
127 XChangeProperty(nativeDisplay, window, windowType, XA_ATOM, 32, PropModeReplace, (unsigned char*)&windowTypeOverride, 1);
132 printf("Warning: using a composited window\n");
135 windowTitle.value = (unsigned char*)title;
136 windowTitle.encoding = XA_STRING;
137 windowTitle.format = 8;
138 windowTitle.nitems = strlen(title);
139 XSetWMName(nativeDisplay, window, &windowTitle);
141 XMapWindow(nativeDisplay, window);
142 XFlush(nativeDisplay);
144 /* Set window to fullscreen mode if it matches the screen size */
145 if (rootAttrs.width == width && rootAttrs.height == height)
148 Atom wmState = XInternAtom(nativeDisplay, "_NET_WM_STATE", False);
149 Atom wmStateFullscreen = XInternAtom(nativeDisplay, "_NET_WM_STATE_FULLSCREEN", False);
151 memset(&xev, 0, sizeof(XEvent));
152 xev.type = ClientMessage;
153 xev.xclient.window = window;
154 xev.xclient.message_type = wmState;
155 xev.xclient.format = 32;
156 xev.xclient.data.l[0] = 1;
157 xev.xclient.data.l[1] = wmStateFullscreen;
158 xev.xclient.data.l[2] = 0;
159 XSendEvent(nativeDisplay, rootWindow, False, SubstructureNotifyMask, &xev);
162 *nativeWindow = window;
167 void nativeDestroyWindow(EGLNativeDisplayType nativeDisplay, EGLNativeWindowType nativeWindow)
169 XDestroyWindow(nativeDisplay, nativeWindow);
172 EGLBoolean nativeCreatePixmap(EGLNativeDisplayType nativeDisplay, EGLDisplay dpy, EGLConfig config,
173 int width, int height, EGLNativePixmapType *nativePixmap)
175 Window rootWindow = DefaultRootWindow(nativeDisplay);
179 if (eglGetConfigAttrib(dpy, config, EGL_BUFFER_SIZE, &depth) != EGL_TRUE)
181 fprintf(stderr, "eglGetConfigAttrib failed: %x\n", eglGetError());
185 pixmap = XCreatePixmap(nativeDisplay, rootWindow, width, height, depth);
189 fprintf(stderr, "XCreatePixmap failed\n");
193 XFlush(nativeDisplay);
195 *nativePixmap = pixmap;
200 void nativeDestroyPixmap(EGLNativeDisplayType nativeDisplay, EGLNativePixmapType nativePixmap)
202 XFreePixmap(nativeDisplay, nativePixmap);