DRI2: report swap events correctly in direct rendered case
[mesa.git] / progs / openvg / demos / lion.c
1 #include <assert.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8 #include <X11/keysym.h>
9 #include <VG/openvg.h>
10 #include <GLES/egl.h>
11
12 #include "lion-render.h"
13
14 static VGint width, height;
15 struct lion *lion = 0;
16 VGfloat angle = 0;
17
18 static void
19 draw(void)
20 {
21 vgClear(0, 0, width, height);
22
23 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
24 vgLoadIdentity();
25 vgTranslate(width/2, height/2);
26 vgRotate(angle);
27 vgTranslate(-width/2, -height/2);
28
29 lion_render(lion);
30
31 ++angle;
32 }
33
34
35 /* new window size or exposure */
36 static void
37 reshape(int w, int h)
38 {
39 width = w;
40 height = h;
41 }
42
43
44 static void
45 init(void)
46 {
47 float clear_color[4] = {1.0, 1.0, 1.0, 1.0};
48 vgSetfv(VG_CLEAR_COLOR, 4, clear_color);
49
50 lion = lion_create();
51 }
52
53
54 /*
55 * Create an RGB, double-buffered X window.
56 * Return the window and context handles.
57 */
58 static void
59 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
60 const char *name,
61 int x, int y, int width, int height,
62 Window *winRet,
63 EGLContext *ctxRet,
64 EGLSurface *surfRet)
65 {
66 static const EGLint attribs[] = {
67 EGL_RED_SIZE, 1,
68 EGL_GREEN_SIZE, 1,
69 EGL_BLUE_SIZE, 1,
70 EGL_RENDERABLE_TYPE, EGL_OPENVG_BIT,
71 EGL_NONE
72 };
73
74 int scrnum;
75 XSetWindowAttributes attr;
76 unsigned long mask;
77 Window root;
78 Window win;
79 XVisualInfo *visInfo, visTemplate;
80 int num_visuals;
81 EGLContext ctx;
82 EGLConfig config;
83 EGLint num_configs;
84 EGLint vid;
85
86 scrnum = DefaultScreen( x_dpy );
87 root = RootWindow( x_dpy, scrnum );
88
89 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs) ||
90 !num_configs) {
91 printf("Error: couldn't get an EGL visual config\n");
92 exit(1);
93 }
94
95 assert(config);
96
97 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
98 printf("Error: eglGetConfigAttrib() failed\n");
99 exit(1);
100 }
101
102 /* The X window visual must match the EGL config */
103 visTemplate.visualid = vid;
104 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
105 if (!visInfo) {
106 printf("Error: couldn't get X visual\n");
107 exit(1);
108 }
109
110 /* window attributes */
111 attr.background_pixel = 0;
112 attr.border_pixel = 0;
113 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
114 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
115 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
116
117 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
118 0, visInfo->depth, InputOutput,
119 visInfo->visual, mask, &attr );
120
121 /* set hints and properties */
122 {
123 XSizeHints sizehints;
124 sizehints.x = x;
125 sizehints.y = y;
126 sizehints.width = width;
127 sizehints.height = height;
128 sizehints.flags = USSize | USPosition;
129 XSetNormalHints(x_dpy, win, &sizehints);
130 XSetStandardProperties(x_dpy, win, name, name,
131 None, (char **)NULL, 0, &sizehints);
132 }
133
134 eglBindAPI(EGL_OPENVG_API);
135
136 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
137 if (!ctx) {
138 printf("Error: eglCreateContext failed\n");
139 exit(1);
140 }
141
142 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
143
144 if (!*surfRet) {
145 printf("Error: eglCreateWindowSurface failed\n");
146 exit(1);
147 }
148
149 XFree(visInfo);
150
151 *winRet = win;
152 *ctxRet = ctx;
153 }
154
155
156 static void
157 event_loop(Display *dpy, Window win,
158 EGLDisplay egl_dpy, EGLSurface egl_surf)
159 {
160 while (1) {
161 XEvent event;
162
163 while (XPending(dpy) > 0) {
164 XNextEvent(dpy, &event);
165
166 switch (event.type) {
167 case Expose:
168 break;
169 case ConfigureNotify:
170 reshape(event.xconfigure.width, event.xconfigure.height);
171 break;
172 case KeyPress:
173 {
174 char buffer[10];
175 int r, code;
176 code = XLookupKeysym(&event.xkey, 0);
177 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
178 NULL, NULL);
179 if (buffer[0] == 27) {
180 /* escape */
181 return;
182 }
183 }
184 break;
185 default:
186 ; /*no-op*/
187 }
188 }
189
190 draw();
191 eglSwapBuffers(egl_dpy, egl_surf);
192 }
193 }
194
195
196 static void
197 usage(void)
198 {
199 printf("Usage:\n");
200 printf(" -display <displayname> set the display to run on\n");
201 printf(" -info display OpenGL renderer info\n");
202 }
203
204 int
205 main(int argc, char *argv[])
206 {
207 const int winWidth = 350, winHeight = 450;
208 Display *x_dpy;
209 Window win;
210 EGLSurface egl_surf;
211 EGLContext egl_ctx;
212 EGLDisplay egl_dpy;
213 char *dpyName = NULL;
214 GLboolean printInfo = GL_FALSE;
215 EGLint egl_major, egl_minor;
216 int i;
217 const char *s;
218
219 for (i = 1; i < argc; i++) {
220 if (strcmp(argv[i], "-display") == 0) {
221 dpyName = argv[i+1];
222 i++;
223 }
224 else if (strcmp(argv[i], "-info") == 0) {
225 printInfo = GL_TRUE;
226 }
227 else {
228 usage();
229 return -1;
230 }
231 }
232
233 x_dpy = XOpenDisplay(dpyName);
234 if (!x_dpy) {
235 printf("Error: couldn't open display %s\n",
236 dpyName ? dpyName : getenv("DISPLAY"));
237 return -1;
238 }
239
240 egl_dpy = eglGetDisplay(x_dpy);
241 if (!egl_dpy) {
242 printf("Error: eglGetDisplay() failed\n");
243 return -1;
244 }
245
246 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
247 printf("Error: eglInitialize() failed\n");
248 return -1;
249 }
250
251 s = eglQueryString(egl_dpy, EGL_VERSION);
252 printf("EGL_VERSION = %s\n", s);
253
254 make_x_window(x_dpy, egl_dpy,
255 "Lion Example", 0, 0, winWidth, winHeight,
256 &win, &egl_ctx, &egl_surf);
257
258 XMapWindow(x_dpy, win);
259 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
260 printf("Error: eglMakeCurrent() failed\n");
261 return -1;
262 }
263
264 if (printInfo) {
265 printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER));
266 printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION));
267 printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR));
268 }
269
270 init();
271
272 /* Set initial projection/viewing transformation.
273 * We can't be sure we'll get a ConfigureNotify event when the window
274 * first appears.
275 */
276 reshape(winWidth, winHeight);
277
278 event_loop(x_dpy, win, egl_dpy, egl_surf);
279
280 eglDestroyContext(egl_dpy, egl_ctx);
281 eglDestroySurface(egl_dpy, egl_surf);
282 eglTerminate(egl_dpy);
283
284
285 XDestroyWindow(x_dpy, win);
286 XCloseDisplay(x_dpy);
287
288 return 0;
289 }