added GLX multi-thread demo
[mesa.git] / progs / xdemos / glthreads.c
1 /* $Id: glthreads.c,v 1.1 2000/07/20 20:12:17 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 <unistd.h>
42 #include <pthread.h>
43
44
45 /*
46 * Each window/thread/context:
47 */
48 struct winthread {
49 Display *Dpy;
50 int Index;
51 pthread_t Thread;
52 Window Win;
53 GLXContext Context;
54 float Angle;
55 int WinWidth, WinHeight;
56 GLboolean NewSize;
57 };
58
59
60 #define MAX_WINTHREADS 100
61 static struct winthread WinThreads[MAX_WINTHREADS];
62 static int NumWinThreads = 0;
63 static GLboolean ExitFlag = GL_FALSE;
64
65
66
67 static void
68 Error(const char *msg)
69 {
70 fprintf(stderr, "Error: %s\n", msg);
71 exit(1);
72 }
73
74
75 /* draw a colored cube */
76 static void
77 draw_object(void)
78 {
79 glPushMatrix();
80 glScalef(0.75, 0.75, 0.75);
81
82 glColor3f(1, 0, 0);
83 glBegin(GL_POLYGON);
84 glVertex3f(1, -1, -1);
85 glVertex3f(1, 1, -1);
86 glVertex3f(1, 1, 1);
87 glVertex3f(1, -1, 1);
88 glEnd();
89
90 glColor3f(0, 1, 1);
91 glBegin(GL_POLYGON);
92 glVertex3f(-1, -1, -1);
93 glVertex3f(-1, 1, -1);
94 glVertex3f(-1, 1, 1);
95 glVertex3f(-1, -1, 1);
96 glEnd();
97
98 glColor3f(0, 1, 0);
99 glBegin(GL_POLYGON);
100 glVertex3f(-1, 1, -1);
101 glVertex3f( 1, 1, -1);
102 glVertex3f( 1, 1, 1);
103 glVertex3f(-1, 1, 1);
104 glEnd();
105
106 glColor3f(1, 0, 1);
107 glBegin(GL_POLYGON);
108 glVertex3f(-1, -1, -1);
109 glVertex3f( 1, -1, -1);
110 glVertex3f( 1, -1, 1);
111 glVertex3f(-1, -1, 1);
112 glEnd();
113
114 glColor3f(0, 0, 1);
115 glBegin(GL_POLYGON);
116 glVertex3f(-1, -1, 1);
117 glVertex3f( 1, -1, 1);
118 glVertex3f( 1, 1, 1);
119 glVertex3f(-1, 1, 1);
120 glEnd();
121
122 glColor3f(1, 1, 0);
123 glBegin(GL_POLYGON);
124 glVertex3f(-1, -1, -1);
125 glVertex3f( 1, -1, -1);
126 glVertex3f( 1, 1, -1);
127 glVertex3f(-1, 1, -1);
128 glEnd();
129 glPopMatrix();
130 }
131
132
133 /* signal resize of given window */
134 static void
135 resize(struct winthread *wt, int w, int h)
136 {
137 wt->NewSize = GL_TRUE;
138 wt->WinWidth = w;
139 wt->WinHeight = h;
140 }
141
142
143 /*
144 * We have an instance of this for each thread.
145 */
146 static void
147 draw_loop(struct winthread *wt)
148 {
149 while (!ExitFlag) {
150
151 glXMakeCurrent(wt->Dpy, wt->Win, wt->Context);
152
153 glEnable(GL_DEPTH_TEST);
154
155 if (wt->NewSize) {
156 GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
157 glViewport(0, 0, wt->WinWidth, wt->WinHeight);
158 glMatrixMode(GL_PROJECTION);
159 glLoadIdentity();
160 glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
161 glMatrixMode(GL_MODELVIEW);
162 glLoadIdentity();
163 glTranslatef(0, 0, -2.5);
164 wt->NewSize = GL_FALSE;
165 }
166
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
168
169 glPushMatrix();
170 glRotatef(wt->Angle, 0, 0, 1);
171 glRotatef(wt->Angle, 1, 0, 0);
172 glScalef(0.7, 0.7, 0.7);
173 draw_object();
174 glPopMatrix();
175
176 glXSwapBuffers(wt->Dpy, wt->Win);
177
178 wt->Angle += 1.0;
179 }
180 }
181
182
183 /*
184 * The main process thread runs this loop.
185 */
186 static void
187 event_loop(Display *dpy)
188 {
189 while (!ExitFlag) {
190 static long mask = StructureNotifyMask | ExposureMask | KeyPressMask;
191 XEvent event;
192 int i;
193
194 for (i = 0; i < NumWinThreads; i++) {
195 struct winthread *wt = &WinThreads[i];
196 while (XCheckWindowEvent(dpy, wt->Win, mask, &event)) {
197 if (event.xany.window == wt->Win) {
198 switch (event.type) {
199 case ConfigureNotify:
200 resize(wt, event.xconfigure.width,
201 event.xconfigure.height);
202 break;
203 case KeyPress:
204 /* tell all threads to exit */
205 ExitFlag = GL_TRUE;
206 /*printf("exit draw_loop %d\n", wt->Index);*/
207 return;
208 default:
209 /*no-op*/ ;
210 }
211 }
212 else {
213 printf("window mismatch\n");
214 }
215 }
216 }
217 }
218 }
219
220
221 /*
222 * we'll call this once for each thread, before the threads are created.
223 */
224 static void
225 create_window(struct winthread *wt)
226 {
227 Window win;
228 GLXContext ctx;
229 int attrib[] = { GLX_RGBA,
230 GLX_RED_SIZE, 1,
231 GLX_GREEN_SIZE, 1,
232 GLX_BLUE_SIZE, 1,
233 GLX_DEPTH_SIZE, 1,
234 GLX_DOUBLEBUFFER,
235 None };
236 int scrnum;
237 XSetWindowAttributes attr;
238 unsigned long mask;
239 Window root;
240 XVisualInfo *visinfo;
241 int width = 80, height = 80;
242 int xpos = (wt->Index % 10) * 90;
243 int ypos = (wt->Index / 10) * 100;
244
245 scrnum = DefaultScreen(wt->Dpy);
246 root = RootWindow(wt->Dpy, scrnum);
247
248 visinfo = glXChooseVisual(wt->Dpy, scrnum, attrib);
249 if (!visinfo) {
250 Error("Unable to find RGB, Z, double-buffered visual");
251 }
252
253 /* window attributes */
254 attr.background_pixel = 0;
255 attr.border_pixel = 0;
256 attr.colormap = XCreateColormap(wt->Dpy, root, visinfo->visual, AllocNone);
257 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
258 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
259
260 win = XCreateWindow(wt->Dpy, root, xpos, ypos, width, height,
261 0, visinfo->depth, InputOutput,
262 visinfo->visual, mask, &attr);
263 if (!win) {
264 Error("Couldn't create window");
265 }
266
267 {
268 XSizeHints sizehints;
269 sizehints.x = xpos;
270 sizehints.y = ypos;
271 sizehints.width = width;
272 sizehints.height = height;
273 sizehints.flags = USSize | USPosition;
274 XSetNormalHints(wt->Dpy, win, &sizehints);
275 XSetStandardProperties(wt->Dpy, win, "glthreads", "glthreads",
276 None, (char **)NULL, 0, &sizehints);
277 }
278
279
280 ctx = glXCreateContext(wt->Dpy, visinfo, NULL, True);
281 if (!ctx) {
282 Error("Couldn't create GLX context");
283 }
284
285 XMapWindow(wt->Dpy, win);
286 XSync(wt->Dpy, 0);
287
288 /* save the info for this window/context */
289 wt->Win = win;
290 wt->Context = ctx;
291 wt->Angle = 0.0;
292 wt->WinWidth = width;
293 wt->WinHeight = height;
294 wt->NewSize = GL_TRUE;
295 }
296
297
298 /*
299 * Called by pthread_create()
300 */
301 static void *
302 thread_function(void *p)
303 {
304 struct winthread *wt = (struct winthread *) p;
305 draw_loop(wt);
306 return NULL;
307 }
308
309
310 /*
311 * called before exit to wait for all threads to finish
312 */
313 static void
314 clean_up(void)
315 {
316 int i;
317
318 /* wait for threads to finish */
319 for (i = 0; i < NumWinThreads; i++) {
320 pthread_join(WinThreads[i].Thread, NULL);
321 }
322
323 for (i = 0; i < NumWinThreads; i++) {
324 glXDestroyContext(WinThreads[i].Dpy, WinThreads[i].Context);
325 XDestroyWindow(WinThreads[i].Dpy, WinThreads[i].Win);
326 }
327 }
328
329
330
331 int
332 main(int argc, char *argv[])
333 {
334 char *displayName = ":0.0";
335 int numThreads = 2;
336 Display *dpy;
337 int i;
338 Status threadStat;
339
340 if (argc == 1) {
341 printf("threadgl: test of GL thread safety (any key = exit)\n");
342 printf("Usage:\n");
343 printf(" threadgl [-display dpyName] [-n numthreads]\n");
344 }
345 else {
346 int i;
347 for (i = 1; i < argc; i++) {
348 if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
349 displayName = argv[i + 1];
350 i++;
351 }
352 else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
353 numThreads = atoi(argv[i + 1]);
354 if (numThreads < 1)
355 numThreads = 1;
356 else if (numThreads > MAX_WINTHREADS)
357 numThreads = MAX_WINTHREADS;
358 i++;
359 }
360 }
361 }
362
363 /*
364 * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
365 */
366 threadStat = XInitThreads();
367 if (threadStat) {
368 printf("XInitThreads() returned %d (success)\n", (int) threadStat);
369 }
370 else {
371 printf("XInitThreads() returned 0 (failure- this program may fail)\n");
372 }
373
374
375 dpy = XOpenDisplay(displayName);
376 if (!dpy) {
377 fprintf(stderr, "Unable to open display %s\n", displayName);
378 return -1;
379 }
380
381 NumWinThreads = numThreads;
382
383 /* Create the GLX windows and contexts */
384 for (i = 0; i < numThreads; i++) {
385 WinThreads[i].Dpy = dpy;
386 WinThreads[i].Index = i;
387 create_window(&WinThreads[i]);
388 }
389
390 /* Create the threads */
391 for (i = 0; i < numThreads; i++) {
392 pthread_create(&WinThreads[i].Thread, NULL, thread_function,
393 (void*) &WinThreads[i]);
394 printf("Created Thread %p\n", WinThreads[i].Thread);
395 }
396
397 event_loop(dpy);
398
399 clean_up();
400
401 XCloseDisplay(dpy);
402
403 return 0;
404 }
405
406
407 #else /* PTHREADS */
408
409
410 #include <stdio.h>
411
412 int
413 main(int argc, char *argv[])
414 {
415 printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
416 return 0;
417 }
418
419
420 #endif /* PTHREADS */