479733e0d29fbf565f135660eba277076fcaf24a
[profile/ivi/navit.git] / navit / navit / graphics / opengl / window.c
1 #define USE_OPENGLES2 1
2
3 #if USE_OPENGLES2
4 #include <GLES2/gl2.h>
5 #include <EGL/egl.h>
6 #else
7 #include <GLES/gl.h>
8 #include <GLES/egl.h>
9 #endif
10 #include <X11/Xlib.h>
11 #include <X11/Xutil.h>
12 #include "debug.h"
13
14 EGLSurface eglwindow;
15 EGLDisplay egldisplay;
16 Display *dpy;
17 Window window;
18
19 #if USE_OPENGLES2
20 static EGLint attributeList[] = {
21         EGL_RED_SIZE, 8,
22         EGL_GREEN_SIZE, 8,
23         EGL_BLUE_SIZE, 8,
24         EGL_DEPTH_SIZE, 16,
25         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
26         EGL_NONE
27 };
28
29 #else
30 static EGLint attributeList[] = {
31         EGL_RED_SIZE, 1,
32         EGL_DEPTH_SIZE, 1,
33         EGL_NONE
34 };
35 #endif
36
37
38 int
39 createEGLWindow(int width, int height, char *name)
40 {
41         EGLConfig config[4];
42         EGLContext cx;
43         int nconfig;
44         XSizeHints sizehints;
45         XSetWindowAttributes swa;
46         XVisualInfo *vi, tmp;
47         int vid, n;
48
49 #if USE_OPENGLES2
50         EGLint aEGLContextAttributes[] = {
51                 EGL_CONTEXT_CLIENT_VERSION, 2,
52                 EGL_NONE
53         };
54 #endif
55
56
57         egldisplay = eglGetDisplay(dpy = XOpenDisplay(NULL));
58         eglInitialize(egldisplay, 0, 0);
59         if (!eglChooseConfig
60             (egldisplay, attributeList, config,
61              sizeof config / sizeof config[0], &nconfig)) {
62                 dbg(0, "can't find requested config\n");
63                 return 0;
64         }
65 #if USE_OPENGLES2
66         cx = eglCreateContext(egldisplay, config[0], EGL_NO_CONTEXT, aEGLContextAttributes);
67 #else
68         cx = eglCreateContext(egldisplay, config[0], EGL_NO_CONTEXT, NULL);
69 #endif
70
71         eglGetConfigAttrib(egldisplay, config[0], EGL_NATIVE_VISUAL_ID,
72                            &vid);
73         tmp.visualid = vid;
74         vi = XGetVisualInfo(dpy, VisualIDMask, &tmp, &n);
75         swa.colormap =
76             XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
77                             AllocNone);
78         sizehints.flags = 0;
79         sizehints.flags = PMinSize | PMaxSize;
80         sizehints.min_width = sizehints.max_width = width;
81         sizehints.min_height = sizehints.max_height = height;
82
83
84         swa.border_pixel = 0;
85         swa.event_mask =
86             ExposureMask | StructureNotifyMask | KeyPressMask |
87             ButtonPressMask | ButtonReleaseMask;
88         window =
89             XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width,
90                           height, 0, vi->depth, InputOutput, vi->visual,
91                           CWBorderPixel | CWColormap | CWEventMask, &swa);
92         XMapWindow(dpy, window);
93         XSetStandardProperties(dpy, window, name, name, None, (void *) 0, 0, &sizehints);
94
95         eglwindow = eglCreateWindowSurface(egldisplay, config[0], (NativeWindowType) window, 0);
96         eglMakeCurrent(egldisplay, eglwindow, eglwindow, cx);
97         return 1;
98 }