Merge commit 'origin/gallium-master-merge'
[mesa.git] / progs / egl / xegl_tri.c
1 /*
2 * Copyright (C) 2008 Brian Paul All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * Draw a triangle with X/EGL.
24 * Brian Paul
25 * 3 June 2008
26 */
27
28
29 #include <assert.h>
30 #include <math.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #include <X11/keysym.h>
37 #include <GL/gl.h> /* using full OpenGL for now */
38 #include <GLES/egl.h>
39
40
41 static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0;
42
43
44 static void
45 draw(void)
46 {
47 static const GLfloat verts[3][2] = {
48 { -1, -1 },
49 { 1, -1 },
50 { 0, 1 }
51 };
52 static const GLfloat colors[3][3] = {
53 { 1, 0, 0 },
54 { 0, 1, 0 },
55 { 0, 0, 1 }
56 };
57
58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59
60 glPushMatrix();
61 glRotatef(view_rotx, 1, 0, 0);
62 glRotatef(view_roty, 0, 1, 0);
63 glRotatef(view_rotz, 0, 0, 1);
64
65 {
66 glVertexPointer(2, GL_FLOAT, 0, verts);
67 glColorPointer(3, GL_FLOAT, 0, colors);
68 glEnableClientState(GL_VERTEX_ARRAY);
69 glEnableClientState(GL_COLOR_ARRAY);
70
71 glDrawArrays(GL_TRIANGLES, 0, 3);
72
73 glDisableClientState(GL_VERTEX_ARRAY);
74 glDisableClientState(GL_COLOR_ARRAY);
75 }
76
77 glPopMatrix();
78 }
79
80
81 /* new window size or exposure */
82 static void
83 reshape(int width, int height)
84 {
85 GLfloat ar = (GLfloat) width / (GLfloat) height;
86
87 glViewport(0, 0, (GLint) width, (GLint) height);
88
89 glMatrixMode(GL_PROJECTION);
90 glLoadIdentity();
91 glFrustum(-ar, ar, -1, 1, 5.0, 60.0);
92
93 glMatrixMode(GL_MODELVIEW);
94 glLoadIdentity();
95 glTranslatef(0.0, 0.0, -10.0);
96 }
97
98
99 static void
100 init(void)
101 {
102 glClearColor(0.4, 0.4, 0.4, 0.0);
103 }
104
105
106 /*
107 * Create an RGB, double-buffered X window.
108 * Return the window and context handles.
109 */
110 static void
111 make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
112 const char *name,
113 int x, int y, int width, int height,
114 Window *winRet,
115 EGLContext *ctxRet,
116 EGLSurface *surfRet)
117 {
118 static const EGLint attribs[] = {
119 EGL_RED_SIZE, 1,
120 EGL_GREEN_SIZE, 1,
121 EGL_BLUE_SIZE, 1,
122 EGL_DEPTH_SIZE, 1,
123 EGL_NONE
124 };
125
126 int scrnum;
127 XSetWindowAttributes attr;
128 unsigned long mask;
129 Window root;
130 Window win;
131 XVisualInfo *visInfo, visTemplate;
132 int num_visuals;
133 EGLContext ctx;
134 EGLConfig config;
135 EGLint num_configs;
136 EGLint vid;
137
138 scrnum = DefaultScreen( x_dpy );
139 root = RootWindow( x_dpy, scrnum );
140
141 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
142 printf("Error: couldn't get an EGL visual config\n");
143 exit(1);
144 }
145
146 assert(config);
147 assert(num_configs > 0);
148
149 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
150 printf("Error: eglGetConfigAttrib() failed\n");
151 exit(1);
152 }
153
154 /* The X window visual must match the EGL config */
155 visTemplate.visualid = vid;
156 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
157 if (!visInfo) {
158 printf("Error: couldn't get X visual\n");
159 exit(1);
160 }
161
162 /* window attributes */
163 attr.background_pixel = 0;
164 attr.border_pixel = 0;
165 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
166 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
167 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
168
169 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
170 0, visInfo->depth, InputOutput,
171 visInfo->visual, mask, &attr );
172
173 /* set hints and properties */
174 {
175 XSizeHints sizehints;
176 sizehints.x = x;
177 sizehints.y = y;
178 sizehints.width = width;
179 sizehints.height = height;
180 sizehints.flags = USSize | USPosition;
181 XSetNormalHints(x_dpy, win, &sizehints);
182 XSetStandardProperties(x_dpy, win, name, name,
183 None, (char **)NULL, 0, &sizehints);
184 }
185
186 eglBindAPI(EGL_OPENGL_API);
187
188 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
189 if (!ctx) {
190 printf("Error: glXCreateContext failed\n");
191 exit(1);
192 }
193
194 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
195
196 if (!*surfRet) {
197 printf("Error: eglCreateWindowSurface failed\n");
198 exit(1);
199 }
200
201 XFree(visInfo);
202
203 *winRet = win;
204 *ctxRet = ctx;
205 }
206
207
208 static void
209 event_loop(Display *dpy, Window win,
210 EGLDisplay egl_dpy, EGLSurface egl_surf)
211 {
212 while (1) {
213 int redraw = 0;
214 XEvent event;
215
216 XNextEvent(dpy, &event);
217
218 switch (event.type) {
219 case Expose:
220 redraw = 1;
221 break;
222 case ConfigureNotify:
223 reshape(event.xconfigure.width, event.xconfigure.height);
224 break;
225 case KeyPress:
226 {
227 char buffer[10];
228 int r, code;
229 code = XLookupKeysym(&event.xkey, 0);
230 if (code == XK_Left) {
231 view_roty += 5.0;
232 }
233 else if (code == XK_Right) {
234 view_roty -= 5.0;
235 }
236 else if (code == XK_Up) {
237 view_rotx += 5.0;
238 }
239 else if (code == XK_Down) {
240 view_rotx -= 5.0;
241 }
242 else {
243 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
244 NULL, NULL);
245 if (buffer[0] == 27) {
246 /* escape */
247 return;
248 }
249 }
250 }
251 redraw = 1;
252 break;
253 default:
254 ; /*no-op*/
255 }
256
257 if (redraw) {
258 draw();
259 eglSwapBuffers(egl_dpy, egl_surf);
260 }
261 }
262 }
263
264
265 static void
266 usage(void)
267 {
268 printf("Usage:\n");
269 printf(" -display <displayname> set the display to run on\n");
270 printf(" -info display OpenGL renderer info\n");
271 }
272
273
274 int
275 main(int argc, char *argv[])
276 {
277 const int winWidth = 300, winHeight = 300;
278 Display *x_dpy;
279 Window win;
280 EGLSurface egl_surf;
281 EGLContext egl_ctx;
282 EGLDisplay egl_dpy;
283 char *dpyName = NULL;
284 GLboolean printInfo = GL_FALSE;
285 EGLint egl_major, egl_minor;
286 int i;
287 const char *s;
288
289 for (i = 1; i < argc; i++) {
290 if (strcmp(argv[i], "-display") == 0) {
291 dpyName = argv[i+1];
292 i++;
293 }
294 else if (strcmp(argv[i], "-info") == 0) {
295 printInfo = GL_TRUE;
296 }
297 else {
298 usage();
299 return -1;
300 }
301 }
302
303 x_dpy = XOpenDisplay(dpyName);
304 if (!x_dpy) {
305 printf("Error: couldn't open display %s\n",
306 dpyName ? dpyName : getenv("DISPLAY"));
307 return -1;
308 }
309
310 egl_dpy = eglGetDisplay(x_dpy);
311 if (!egl_dpy) {
312 printf("Error: eglGetDisplay() failed\n");
313 return -1;
314 }
315
316 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
317 printf("Error: eglInitialize() failed\n");
318 return -1;
319 }
320
321 s = eglQueryString(egl_dpy, EGL_VERSION);
322 printf("EGL_VERSION = %s\n", s);
323
324 make_x_window(x_dpy, egl_dpy,
325 "xegl_tri", 0, 0, winWidth, winHeight,
326 &win, &egl_ctx, &egl_surf);
327
328 XMapWindow(x_dpy, win);
329 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
330 printf("Error: eglMakeCurrent() failed\n");
331 return -1;
332 }
333
334 if (printInfo) {
335 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
336 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
337 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
338 }
339
340 init();
341
342 /* Set initial projection/viewing transformation.
343 * We can't be sure we'll get a ConfigureNotify event when the window
344 * first appears.
345 */
346 reshape(winWidth, winHeight);
347
348 event_loop(x_dpy, win, egl_dpy, egl_surf);
349
350 eglDestroyContext(egl_dpy, egl_ctx);
351 eglDestroySurface(egl_dpy, egl_surf);
352 eglTerminate(egl_dpy);
353
354
355 XDestroyWindow(x_dpy, win);
356 XCloseDisplay(x_dpy);
357
358 return 0;
359 }