fix some minor warnings
[mesa.git] / progs / xdemos / glthreads.c
1 /* $Id: glthreads.c,v 1.2 2002/03/08 19:44:28 brianp Exp $ */
2
3 /*
4 * Copyright (C) 2000 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24
25 /*
26 * This program tests GLX thread safety.
27 * Command line options:
28 * -n <num threads> Number of threads to create (default is 2)
29 * -display <display name> Specify X display (default is :0.0)
30 *
31 * Brian Paul 20 July 2000
32 */
33
34
35 #if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
36
37 #include <GL/gl.h>
38 #include <GL/glx.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <pthread.h>
44
45
46 /*
47 * Each window/thread/context:
48 */
49 struct winthread {
50 Display *Dpy;
51 int Index;
52 pthread_t Thread;
53 Window Win;
54 GLXContext Context;
55 float Angle;
56 int WinWidth, WinHeight;
57 GLboolean NewSize;
58 };
59
60
61 #define MAX_WINTHREADS 100
62 static struct winthread WinThreads[MAX_WINTHREADS];
63 static int NumWinThreads = 0;
64 static GLboolean ExitFlag = GL_FALSE;
65
66
67
68 static void
69 Error(const char *msg)
70 {
71 fprintf(stderr, "Error: %s\n", msg);
72 exit(1);
73 }
74
75
76 /* draw a colored cube */
77 static void
78 draw_object(void)
79 {
80 glPushMatrix();
81 glScalef(0.75, 0.75, 0.75);
82
83 glColor3f(1, 0, 0);
84 glBegin(GL_POLYGON);
85 glVertex3f(1, -1, -1);
86 glVertex3f(1, 1, -1);
87 glVertex3f(1, 1, 1);
88 glVertex3f(1, -1, 1);
89 glEnd();
90
91 glColor3f(0, 1, 1);
92 glBegin(GL_POLYGON);
93 glVertex3f(-1, -1, -1);
94 glVertex3f(-1, 1, -1);
95 glVertex3f(-1, 1, 1);
96 glVertex3f(-1, -1, 1);
97 glEnd();
98
99 glColor3f(0, 1, 0);
100 glBegin(GL_POLYGON);
101 glVertex3f(-1, 1, -1);
102 glVertex3f( 1, 1, -1);
103 glVertex3f( 1, 1, 1);
104 glVertex3f(-1, 1, 1);
105 glEnd();
106
107 glColor3f(1, 0, 1);
108 glBegin(GL_POLYGON);
109 glVertex3f(-1, -1, -1);
110 glVertex3f( 1, -1, -1);
111 glVertex3f( 1, -1, 1);
112 glVertex3f(-1, -1, 1);
113 glEnd();
114
115 glColor3f(0, 0, 1);
116 glBegin(GL_POLYGON);
117 glVertex3f(-1, -1, 1);
118 glVertex3f( 1, -1, 1);
119 glVertex3f( 1, 1, 1);
120 glVertex3f(-1, 1, 1);
121 glEnd();
122
123 glColor3f(1, 1, 0);
124 glBegin(GL_POLYGON);
125 glVertex3f(-1, -1, -1);
126 glVertex3f( 1, -1, -1);
127 glVertex3f( 1, 1, -1);
128 glVertex3f(-1, 1, -1);
129 glEnd();
130 glPopMatrix();
131 }
132
133
134 /* signal resize of given window */
135 static void
136 resize(struct winthread *wt, int w, int h)
137 {
138 wt->NewSize = GL_TRUE;
139 wt->WinWidth = w;
140 wt->WinHeight = h;
141 }
142
143
144 /*
145 * We have an instance of this for each thread.
146 */
147 static void
148 draw_loop(struct winthread *wt)
149 {
150 while (!ExitFlag) {
151
152 glXMakeCurrent(wt->Dpy, wt->Win, wt->Context);
153
154 glEnable(GL_DEPTH_TEST);
155
156 if (wt->NewSize) {
157 GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
158 glViewport(0, 0, wt->WinWidth, wt->WinHeight);
159 glMatrixMode(GL_PROJECTION);
160 glLoadIdentity();
161 glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
162 glMatrixMode(GL_MODELVIEW);
163 glLoadIdentity();
164 glTranslatef(0, 0, -2.5);
165 wt->NewSize = GL_FALSE;
166 }
167
168 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
169
170 glPushMatrix();
171 glRotatef(wt->Angle, 0, 0, 1);
172 glRotatef(wt->Angle, 1, 0, 0);
173 glScalef(0.7, 0.7, 0.7);
174 draw_object();
175 glPopMatrix();
176
177 glXSwapBuffers(wt->Dpy, wt->Win);
178
179 wt->Angle += 1.0;
180 }
181 }
182
183
184 /*
185 * The main process thread runs this loop.
186 */
187 static void
188 event_loop(Display *dpy)
189 {
190 while (!ExitFlag) {
191 static long mask = StructureNotifyMask | ExposureMask | KeyPressMask;
192 XEvent event;
193 int i;
194
195 for (i = 0; i < NumWinThreads; i++) {
196 struct winthread *wt = &WinThreads[i];
197 while (XCheckWindowEvent(dpy, wt->Win, mask, &event)) {
198 if (event.xany.window == wt->Win) {
199 switch (event.type) {
200 case ConfigureNotify:
201 resize(wt, event.xconfigure.width,
202 event.xconfigure.height);
203 break;
204 case KeyPress:
205 /* tell all threads to exit */
206 ExitFlag = GL_TRUE;
207 /*printf("exit draw_loop %d\n", wt->Index);*/
208 return;
209 default:
210 /*no-op*/ ;
211 }
212 }
213 else {
214 printf("window mismatch\n");
215 }
216 }
217 }
218 }
219 }
220
221
222 /*
223 * we'll call this once for each thread, before the threads are created.
224 */
225 static void
226 create_window(struct winthread *wt)
227 {
228 Window win;
229 GLXContext ctx;
230 int attrib[] = { GLX_RGBA,
231 GLX_RED_SIZE, 1,
232 GLX_GREEN_SIZE, 1,
233 GLX_BLUE_SIZE, 1,
234 GLX_DEPTH_SIZE, 1,
235 GLX_DOUBLEBUFFER,
236 None };
237 int scrnum;
238 XSetWindowAttributes attr;
239 unsigned long mask;
240 Window root;
241 XVisualInfo *visinfo;
242 int width = 80, height = 80;
243 int xpos = (wt->Index % 10) * 90;
244 int ypos = (wt->Index / 10) * 100;
245
246 scrnum = DefaultScreen(wt->Dpy);
247 root = RootWindow(wt->Dpy, scrnum);
248
249 visinfo = glXChooseVisual(wt->Dpy, scrnum, attrib);
250 if (!visinfo) {
251 Error("Unable to find RGB, Z, double-buffered visual");
252 }
253
254 /* window attributes */
255 attr.background_pixel = 0;
256 attr.border_pixel = 0;
257 attr.colormap = XCreateColormap(wt->Dpy, root, visinfo->visual, AllocNone);
258 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
259 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
260
261 win = XCreateWindow(wt->Dpy, root, xpos, ypos, width, height,
262 0, visinfo->depth, InputOutput,
263 visinfo->visual, mask, &attr);
264 if (!win) {
265 Error("Couldn't create window");
266 }
267
268 {
269 XSizeHints sizehints;
270 sizehints.x = xpos;
271 sizehints.y = ypos;
272 sizehints.width = width;
273 sizehints.height = height;
274 sizehints.flags = USSize | USPosition;
275 XSetNormalHints(wt->Dpy, win, &sizehints);
276 XSetStandardProperties(wt->Dpy, win, "glthreads", "glthreads",
277 None, (char **)NULL, 0, &sizehints);
278 }
279
280
281 ctx = glXCreateContext(wt->Dpy, visinfo, NULL, True);
282 if (!ctx) {
283 Error("Couldn't create GLX context");
284 }
285
286 XMapWindow(wt->Dpy, win);
287 XSync(wt->Dpy, 0);
288
289 /* save the info for this window/context */
290 wt->Win = win;
291 wt->Context = ctx;
292 wt->Angle = 0.0;
293 wt->WinWidth = width;
294 wt->WinHeight = height;
295 wt->NewSize = GL_TRUE;
296 }
297
298
299 /*
300 * Called by pthread_create()
301 */
302 static void *
303 thread_function(void *p)
304 {
305 struct winthread *wt = (struct winthread *) p;
306 draw_loop(wt);
307 return NULL;
308 }
309
310
311 /*
312 * called before exit to wait for all threads to finish
313 */
314 static void
315 clean_up(void)
316 {
317 int i;
318
319 /* wait for threads to finish */
320 for (i = 0; i < NumWinThreads; i++) {
321 pthread_join(WinThreads[i].Thread, NULL);
322 }
323
324 for (i = 0; i < NumWinThreads; i++) {
325 glXDestroyContext(WinThreads[i].Dpy, WinThreads[i].Context);
326 XDestroyWindow(WinThreads[i].Dpy, WinThreads[i].Win);
327 }
328 }
329
330
331
332 int
333 main(int argc, char *argv[])
334 {
335 char *displayName = ":0.0";
336 int numThreads = 2;
337 Display *dpy;
338 int i;
339 Status threadStat;
340
341 if (argc == 1) {
342 printf("threadgl: test of GL thread safety (any key = exit)\n");
343 printf("Usage:\n");
344 printf(" threadgl [-display dpyName] [-n numthreads]\n");
345 }
346 else {
347 int i;
348 for (i = 1; i < argc; i++) {
349 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
350 displayName = argv[i + 1];
351 i++;
352 }
353 else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
354 numThreads = atoi(argv[i + 1]);
355 if (numThreads < 1)
356 numThreads = 1;
357 else if (numThreads > MAX_WINTHREADS)
358 numThreads = MAX_WINTHREADS;
359 i++;
360 }
361 }
362 }
363
364 /*
365 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
366 */
367 threadStat = XInitThreads();
368 if (threadStat) {
369 printf("XInitThreads() returned %d (success)\n", (int) threadStat);
370 }
371 else {
372 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
373 }
374
375
376 dpy = XOpenDisplay(displayName);
377 if (!dpy) {
378 fprintf(stderr, "Unable to open display %s\n", displayName);
379 return -1;
380 }
381
382 NumWinThreads = numThreads;
383
384 /* Create the GLX windows and contexts */
385 for (i = 0; i < numThreads; i++) {
386 WinThreads[i].Dpy = dpy;
387 WinThreads[i].Index = i;
388 create_window(&WinThreads[i]);
389 }
390
391 /* Create the threads */
392 for (i = 0; i < numThreads; i++) {
393 pthread_create(&WinThreads[i].Thread, NULL, thread_function,
394 (void*) &WinThreads[i]);
395 printf("Created Thread %p\n", WinThreads[i].Thread);
396 }
397
398 event_loop(dpy);
399
400 clean_up();
401
402 XCloseDisplay(dpy);
403
404 return 0;
405 }
406
407
408 #else /* PTHREADS */
409
410
411 #include <stdio.h>
412
413 int
414 main(int argc, char *argv[])
415 {
416 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
417 return 0;
418 }
419
420
421 #endif /* PTHREADS */