blob: aeb6a666319b7b0ac67e10746347c3460178004d [file] [log] [blame]
Brian Paulc7d14442000-07-20 20:12:17 +00001/*
2 * Copyright (C) 2000 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/*
24 * This program tests GLX thread safety.
25 * Command line options:
26 * -n <num threads> Number of threads to create (default is 2)
27 * -display <display name> Specify X display (default is :0.0)
28 *
29 * Brian Paul 20 July 2000
30 */
31
32
33#if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
34
35#include <GL/gl.h>
36#include <GL/glx.h>
37#include <stdio.h>
38#include <stdlib.h>
Brian Paula3e44f42002-03-08 19:44:28 +000039#include <string.h>
Brian Paulc7d14442000-07-20 20:12:17 +000040#include <unistd.h>
41#include <pthread.h>
42
43
44/*
45 * Each window/thread/context:
46 */
47struct winthread {
48 Display *Dpy;
49 int Index;
50 pthread_t Thread;
51 Window Win;
52 GLXContext Context;
53 float Angle;
54 int WinWidth, WinHeight;
55 GLboolean NewSize;
56};
57
58
59#define MAX_WINTHREADS 100
60static struct winthread WinThreads[MAX_WINTHREADS];
61static int NumWinThreads = 0;
Brian Pauld07859e2004-07-02 14:35:05 +000062static volatile GLboolean ExitFlag = GL_FALSE;
Brian Paulc7d14442000-07-20 20:12:17 +000063
64
65
66static void
67Error(const char *msg)
68{
69 fprintf(stderr, "Error: %s\n", msg);
70 exit(1);
71}
72
73
74/* draw a colored cube */
75static void
76draw_object(void)
77{
78 glPushMatrix();
79 glScalef(0.75, 0.75, 0.75);
80
81 glColor3f(1, 0, 0);
82 glBegin(GL_POLYGON);
83 glVertex3f(1, -1, -1);
84 glVertex3f(1, 1, -1);
85 glVertex3f(1, 1, 1);
86 glVertex3f(1, -1, 1);
87 glEnd();
88
89 glColor3f(0, 1, 1);
90 glBegin(GL_POLYGON);
91 glVertex3f(-1, -1, -1);
92 glVertex3f(-1, 1, -1);
93 glVertex3f(-1, 1, 1);
94 glVertex3f(-1, -1, 1);
95 glEnd();
96
97 glColor3f(0, 1, 0);
98 glBegin(GL_POLYGON);
99 glVertex3f(-1, 1, -1);
100 glVertex3f( 1, 1, -1);
101 glVertex3f( 1, 1, 1);
102 glVertex3f(-1, 1, 1);
103 glEnd();
104
105 glColor3f(1, 0, 1);
106 glBegin(GL_POLYGON);
107 glVertex3f(-1, -1, -1);
108 glVertex3f( 1, -1, -1);
109 glVertex3f( 1, -1, 1);
110 glVertex3f(-1, -1, 1);
111 glEnd();
112
113 glColor3f(0, 0, 1);
114 glBegin(GL_POLYGON);
115 glVertex3f(-1, -1, 1);
116 glVertex3f( 1, -1, 1);
117 glVertex3f( 1, 1, 1);
118 glVertex3f(-1, 1, 1);
119 glEnd();
120
121 glColor3f(1, 1, 0);
122 glBegin(GL_POLYGON);
123 glVertex3f(-1, -1, -1);
124 glVertex3f( 1, -1, -1);
125 glVertex3f( 1, 1, -1);
126 glVertex3f(-1, 1, -1);
127 glEnd();
128 glPopMatrix();
129}
130
131
132/* signal resize of given window */
133static void
134resize(struct winthread *wt, int w, int h)
135{
136 wt->NewSize = GL_TRUE;
137 wt->WinWidth = w;
138 wt->WinHeight = h;
139}
140
141
142/*
143 * We have an instance of this for each thread.
144 */
145static void
146draw_loop(struct winthread *wt)
147{
148 while (!ExitFlag) {
149
150 glXMakeCurrent(wt->Dpy, wt->Win, wt->Context);
151
152 glEnable(GL_DEPTH_TEST);
153
154 if (wt->NewSize) {
155 GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
156 glViewport(0, 0, wt->WinWidth, wt->WinHeight);
157 glMatrixMode(GL_PROJECTION);
158 glLoadIdentity();
159 glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
160 glMatrixMode(GL_MODELVIEW);
161 glLoadIdentity();
162 glTranslatef(0, 0, -2.5);
163 wt->NewSize = GL_FALSE;
164 }
165
166 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167
168 glPushMatrix();
169 glRotatef(wt->Angle, 0, 0, 1);
170 glRotatef(wt->Angle, 1, 0, 0);
171 glScalef(0.7, 0.7, 0.7);
172 draw_object();
173 glPopMatrix();
174
175 glXSwapBuffers(wt->Dpy, wt->Win);
176
177 wt->Angle += 1.0;
Brian Paul30655c22006-03-15 20:30:17 +0000178
179 /* XXX Calling sched_yield() here smooths out performance a lot! */
180 /* Not sure how portable it is though, so leave out for now. */
181 /*sched_yield();*/
Brian Paulc7d14442000-07-20 20:12:17 +0000182 }
183}
184
185
186/*
187 * The main process thread runs this loop.
188 */
189static void
190event_loop(Display *dpy)
191{
Brian Pauld07859e2004-07-02 14:35:05 +0000192 XEvent event;
193 int i;
Brian Paulc7d14442000-07-20 20:12:17 +0000194
Brian Pauld07859e2004-07-02 14:35:05 +0000195 while (!ExitFlag) {
196 XNextEvent(dpy, &event);
197 switch (event.type) {
198 case ConfigureNotify:
199 /* Find winthread for this event's window */
200 for (i = 0; i < NumWinThreads; i++) {
201 struct winthread *wt = &WinThreads[i];
202 if (event.xconfigure.window == wt->Win) {
203 resize(wt, event.xconfigure.width,
204 event.xconfigure.height);
205 break;
Brian Paulc7d14442000-07-20 20:12:17 +0000206 }
207 }
Brian Pauld07859e2004-07-02 14:35:05 +0000208 break;
209 case KeyPress:
210 /* tell all threads to exit */
211 ExitFlag = GL_TRUE;
212 /*printf("exit draw_loop %d\n", wt->Index);*/
213 return;
214 default:
215 /*no-op*/ ;
Brian Paulc7d14442000-07-20 20:12:17 +0000216 }
217 }
218}
219
220
221/*
222 * we'll call this once for each thread, before the threads are created.
223 */
224static void
225create_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 */
301static void *
302thread_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 */
313static void
314clean_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
331int
332main(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) {
Brian Paul30655c22006-03-15 20:30:17 +0000341 printf("glthreads: test of GL thread safety (any key = exit)\n");
Brian Paulc7d14442000-07-20 20:12:17 +0000342 printf("Usage:\n");
Brian Paul30655c22006-03-15 20:30:17 +0000343 printf(" glthreads [-display dpyName] [-n numthreads]\n");
Brian Paulc7d14442000-07-20 20:12:17 +0000344 }
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]);
Brian Paul30655c22006-03-15 20:30:17 +0000394 printf("Created Thread %u\n", (unsigned int) WinThreads[i].Thread);
Brian Paulc7d14442000-07-20 20:12:17 +0000395 }
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
412int
413main(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 */