blob: 8cac7826e002a5e2e2d3fd9fd06a1e4efff3a813 [file] [log] [blame]
Thomas Hellstrom81324052009-03-13 15:03:50 +01001/* $Id: sharedtex.c,v 1.2 2002/01/16 14:32:46 joukj Exp $ */
2
3/*
4 * Test sharing of display lists and texture objects between GLX contests.
5 * Brian Paul
6 * Summer 2000
7 *
8 *
9 * Copyright (C) 2000 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
25 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *
29 * Modified 2009 for multithreading by Thomas Hellstrom.
30 */
31
32
33#include <GL/gl.h>
34#include <GL/glx.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include <string.h>
39#include <pthread.h>
40#include <X11/X.h>
41
42struct thread_init_arg {
43 int id;
44};
45
46struct window {
47 pthread_mutex_t drawMutex;
48 char DisplayName[1000];
49 Display *Dpy;
50 Window Win;
51 GLXContext Context;
52 float Angle;
53 int Id;
54 XVisualInfo *visInfo;
55};
56
57
58#define MAX_WINDOWS 20
59static struct window Windows[MAX_WINDOWS];
60static int NumWindows = 0;
61static int terminate = 0;
62static GLXContext gCtx;
63static Display *gDpy;
64static GLuint Textures[3];
65
66
67
68static void
69Error(const char *display, const char *msg)
70{
71 fprintf(stderr, "Error on display %s - %s\n", display, msg);
72 exit(1);
73}
74
75
76static int
77initMainthread(Display *dpy, const char *displayName)
78{
79 int scrnum;
80 XVisualInfo *visinfo;
81 int attrib[] = { GLX_RGBA,
82 GLX_RED_SIZE, 1,
83 GLX_GREEN_SIZE, 1,
84 GLX_BLUE_SIZE, 1,
85 GLX_DOUBLEBUFFER,
86 GLX_DEPTH_SIZE, 1,
87 None };
88
89 scrnum = DefaultScreen(dpy);
90 visinfo = glXChooseVisual(dpy, scrnum, attrib);
91 if (!visinfo) {
92 Error(displayName, "Unable to find RGB, double-buffered visual");
93 return -1;
94 }
95 gCtx = glXCreateContext(dpy, visinfo, NULL, True);
96 if (!gCtx) {
97 Error(displayName, "Couldn't create GLX context");
98 return -1;
99 }
100 return 0;
101}
102
103static struct window *
104AddWindow(Display *dpy, const char *displayName, int xpos, int ypos,
105 GLXContext sCtx)
106{
107 Window win;
108 GLXContext ctx;
109 int attrib[] = { GLX_RGBA,
110 GLX_RED_SIZE, 1,
111 GLX_GREEN_SIZE, 1,
112 GLX_BLUE_SIZE, 1,
113 GLX_DOUBLEBUFFER,
114 GLX_DEPTH_SIZE, 1,
115 None };
116 int scrnum;
117 XSetWindowAttributes attr;
118 unsigned long mask;
119 Window root;
120 XVisualInfo *visinfo;
121 int width = 300, height = 300;
122
123 if (NumWindows >= MAX_WINDOWS)
124 return NULL;
125
126 scrnum = DefaultScreen(dpy);
127 root = RootWindow(dpy, scrnum);
128
129 visinfo = glXChooseVisual(dpy, scrnum, attrib);
130 if (!visinfo) {
131 Error(displayName, "Unable to find RGB, double-buffered visual");
132 return NULL;
133 }
134
135 /* window attributes */
136 attr.background_pixel = 0;
137 attr.border_pixel = 0;
138 attr.colormap = XCreateColormap(dpy, root, visinfo->visual, AllocNone);
139 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
140 mask = CWBorderPixel | CWColormap | CWEventMask;
141
142 win = XCreateWindow(dpy, root, xpos, ypos, width, height,
143 0, visinfo->depth, InputOutput,
144 visinfo->visual, mask, &attr);
145 if (!win) {
146 Error(displayName, "Couldn't create window");
147 return NULL;
148 }
149
150 {
151 XSizeHints sizehints;
152 sizehints.x = xpos;
153 sizehints.y = ypos;
154 sizehints.width = width;
155 sizehints.height = height;
156 sizehints.flags = USSize | USPosition;
157 XSetNormalHints(dpy, win, &sizehints);
158 XSetStandardProperties(dpy, win, displayName, displayName,
159 None, (char **)NULL, 0, &sizehints);
160 }
161
162
163 ctx = glXCreateContext(dpy, visinfo,
164 sCtx ? sCtx : NULL, True);
165
166 if (!ctx) {
167 Error(displayName, "Couldn't create GLX context");
168 return NULL;
169 }
170
171 XMapWindow(dpy, win);
172
173 if (!glXMakeCurrent(dpy, win, ctx)) {
174 Error(displayName, "glXMakeCurrent failed");
175 printf("glXMakeCurrent failed in AddWindow()\n");
176 return NULL;
177 }
178
179 /* save the info for this window */
180 {
181 static int id = 0;
182 struct window *h = &Windows[NumWindows];
183 strcpy(h->DisplayName, displayName);
184 h->Dpy = dpy;
185 h->Win = win;
186 h->Context = ctx;
187 h->Angle = 0.0;
188 h->Id = id++;
189 h->visInfo = visinfo;
190 pthread_mutex_init(&h->drawMutex, NULL);
191 NumWindows++;
192 return &Windows[NumWindows-1];
193 }
194}
195
196
197static void
198InitGLstuff(void)
199
200{
201 glGenTextures(3, Textures);
202
203 /* setup first texture object */
204 {
205 GLubyte image[16][16][4];
206 GLint i, j;
207 glBindTexture(GL_TEXTURE_2D, Textures[0]);
208
209 /* red/white checkerboard */
210 for (i = 0; i < 16; i++) {
211 for (j = 0; j < 16; j++) {
212 if ((i ^ j) & 1) {
213 image[i][j][0] = 255;
214 image[i][j][1] = 255;
215 image[i][j][2] = 255;
216 image[i][j][3] = 255;
217 }
218 else {
219 image[i][j][0] = 255;
220 image[i][j][1] = 0;
221 image[i][j][2] = 0;
222 image[i][j][3] = 255;
223 }
224 }
225 }
226
227 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
228 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA,
229 GL_UNSIGNED_BYTE, image);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
232 }
233
234 /* setup second texture object */
235 {
236 GLubyte image[8][8][3];
237 GLint i, j;
238 glBindTexture(GL_TEXTURE_2D, Textures[1]);
239
240 /* green/yellow checkerboard */
241 for (i = 0; i < 8; i++) {
242 for (j = 0; j < 8; j++) {
243 if ((i ^ j) & 1) {
244 image[i][j][0] = 0;
245 image[i][j][1] = 255;
246 image[i][j][2] = 0;
247 }
248 else {
249 image[i][j][0] = 255;
250 image[i][j][1] = 255;
251 image[i][j][2] = 0;
252 }
253 }
254 }
255
256 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
257 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB,
258 GL_UNSIGNED_BYTE, image);
259 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
261 }
262
263 /* setup second texture object */
264 {
265 GLubyte image[4][4][3];
266 GLint i, j;
267 glBindTexture(GL_TEXTURE_2D, Textures[2]);
268
269 /* blue/gray checkerboard */
270 for (i = 0; i < 4; i++) {
271 for (j = 0; j < 4; j++) {
272 if ((i ^ j) & 1) {
273 image[i][j][0] = 0;
274 image[i][j][1] = 0;
275 image[i][j][2] = 255;
276 }
277 else {
278 image[i][j][0] = 200;
279 image[i][j][1] = 200;
280 image[i][j][2] = 200;
281 }
282 }
283 }
284
285 glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
286 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_RGB,
287 GL_UNSIGNED_BYTE, image);
288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290 }
291
292 /* Now make the cube object display list */
293
294 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
295 printf("GL_VERSION: %s\n", (char *) glGetString(GL_VERSION));
296 printf("GL_VENDOR: %s\n", (char *) glGetString(GL_VENDOR));
297}
298
299static void
300Redraw(struct window *h)
301{
302 h->Angle += 1.0;
303
304 glShadeModel(GL_FLAT);
305 glClearColor(0.25, 0.25, 0.25, 1.0);
306 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
307
308 glEnable(GL_TEXTURE_2D);
309 glEnable(GL_DEPTH_TEST);
310 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
311
312 glColor3f(1, 1, 1);
313
314 glPushMatrix();
315 if (h->Id == 0)
316 glRotatef(h->Angle, 0, 1, -1);
317 else if (h->Id == 1)
318 glRotatef(-(h->Angle), 0, 1, -1);
319 else if (h->Id == 2)
320 glRotatef(h->Angle, 0, 1, 1);
321 else if (h->Id == 3)
322 glRotatef(-(h->Angle), 0, 1, 1);
323 glBindTexture(GL_TEXTURE_2D, Textures[0]);
324 glBegin(GL_POLYGON);
325 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
326 glTexCoord2f(1, 0); glVertex3f(-1, 1, -1);
327 glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
328 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
329 glEnd();
330 glBegin(GL_POLYGON);
331 glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
332 glTexCoord2f(1, 0); glVertex3f(1, 1, -1);
333 glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
334 glTexCoord2f(0, 1); glVertex3f(1, -1, 1);
335 glEnd();
336
337 glBindTexture(GL_TEXTURE_2D, Textures[1]);
338 glBegin(GL_POLYGON);
339 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
340 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
341 glTexCoord2f(1, 1); glVertex3f( 1, -1, 1);
342 glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
343 glEnd();
344 glBegin(GL_POLYGON);
345 glTexCoord2f(0, 0); glVertex3f(-1, 1, -1);
346 glTexCoord2f(1, 0); glVertex3f( 1, 1, -1);
347 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
348 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
349 glEnd();
350
351 glBindTexture(GL_TEXTURE_2D, Textures[2]);
352 glBegin(GL_POLYGON);
353 glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
354 glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
355 glTexCoord2f(1, 1); glVertex3f( 1, 1, -1);
356 glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
357 glEnd();
358 glBegin(GL_POLYGON);
359 glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
360 glTexCoord2f(1, 0); glVertex3f( 1, -1, 1);
361 glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
362 glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
363 glEnd();
364
365 glPopMatrix();
366
367 XLockDisplay(h->Dpy);
368 glXSwapBuffers(h->Dpy, h->Win);
369 XUnlockDisplay(h->Dpy);
370}
371
372static void *threadRunner (void *arg)
373{
374 struct thread_init_arg *tia = (struct thread_init_arg *) arg;
375 struct window *win;
376
377 win = &Windows[tia->id];
378 XLockDisplay(win->Dpy);
379 if (!glXMakeCurrent(win->Dpy, win->Win, win->Context)) {
380 Error(win->DisplayName, "glXMakeCurrent failed in threadRunner");
381 XUnlockDisplay(win->Dpy);
382 return NULL;
383 }
384 XUnlockDisplay(win->Dpy);
385
386 while(!terminate) {
387 usleep(1000);
388 pthread_mutex_lock(&win->drawMutex);
389 Redraw(win);
390 glFinish();
391 pthread_mutex_unlock(&win->drawMutex);
392 }
393
394 pthread_mutex_destroy(&win->drawMutex);
395 return NULL;
396}
397
398static void
399Resize(struct window *h, unsigned int width, unsigned int height)
400{
401 pthread_mutex_lock(&h->drawMutex);
402 XLockDisplay(h->Dpy);
403
404 if (!glXMakeCurrent(h->Dpy, h->Win, h->Context)) {
405 Error(h->DisplayName, "glXMakeCurrent failed in Resize()");
406 XUnlockDisplay(h->Dpy);
407 pthread_mutex_unlock(&h->drawMutex);
408 return;
409 }
410 XUnlockDisplay(h->Dpy);
411
412 glViewport(0, 0, width, height);
413 glMatrixMode(GL_PROJECTION);
414 glLoadIdentity();
415 glFrustum(-1, 1, -1, 1, 2, 10);
416 glMatrixMode(GL_MODELVIEW);
417 glLoadIdentity();
418 glTranslatef(0, 0, -4.5);
419 glFinish();
420 pthread_mutex_unlock(&h->drawMutex);
421}
422
423
424static void
425EventLoop(void)
426{
427 while (1) {
428 int i;
429 XLockDisplay(gDpy);
430 while (XPending(gDpy) > 0) {
431 XEvent event;
432 XNextEvent(gDpy, &event);
433 XUnlockDisplay(gDpy);
434 for (i = 0; i < NumWindows; i++) {
435 struct window *h = &Windows[i];
436 if (event.xany.window == h->Win) {
437 switch (event.type) {
438 case Expose:
439
440 /*
441 * Note, that here the main thread is actually sharing
442 * context
443 * with window h's thread. This sharing is protected by
444 * the h->drawMutex, and is a bit nasty.
445 * We always need to call glFinish() before releasing
446 * the mutex. One might think that glFlush() would be enough,
447 * but glFlush() doesn't imply that all driver buffers are
448 * _immediately_ flushed to the hardware.
449 */
450
451 pthread_mutex_lock(&h->drawMutex);
452 XLockDisplay(gDpy);
453 if (!glXMakeCurrent(gDpy, h->Win, h->Context)) {
454 Error(h->DisplayName, "glXMakeCurrent failed in Expose");
455 XUnlockDisplay(gDpy);
456 return;
457 }
458 XUnlockDisplay(gDpy);
459 Redraw(h);
460 glFinish();
461 pthread_mutex_unlock(&h->drawMutex);
462 break;
463 case ConfigureNotify:
464 Resize(h, event.xconfigure.width, event.xconfigure.height);
465 break;
466 case KeyPress:
467 terminate = 1;
468 return;
469 default:
470 /*no-op*/ ;
471 }
472 }
473 }
474 XLockDisplay(gDpy);
475 }
476 XUnlockDisplay(gDpy);
477 usleep(1000);
478 }
479}
480
481int
482main(int argc, char *argv[])
483{
484 const char *dpyName = XDisplayName(NULL);
485 pthread_t t0, t1, t2, t3;
486 struct thread_init_arg tia0, tia1, tia2, tia3;
487 struct window *h0, *h1, *h2, *h3;
488
489 XInitThreads();
490
491 gDpy = XOpenDisplay(dpyName);
492 if (!gDpy) {
493 Error(dpyName, "Unable to open display");
494 return -1;
495 }
496
497 if (initMainthread(gDpy, dpyName))
498 return -1;
499
500 /* four windows and contexts sharing display lists and texture objects */
501 h0 = AddWindow(gDpy, dpyName, 10, 10, gCtx);
502 h1 = AddWindow(gDpy, dpyName, 330, 10, gCtx);
503 h2 = AddWindow(gDpy, dpyName, 10, 350, gCtx);
504 h3 = AddWindow(gDpy, dpyName, 330, 350, gCtx);
505
506 if (!glXMakeCurrent(gDpy, h0->Win, gCtx)) {
507 Error(dpyName, "glXMakeCurrent failed for init thread.");
508 return -1;
509 }
510
511 InitGLstuff();
512
513 tia0.id = 0;
514 pthread_create(&t0, NULL, threadRunner, &tia0);
515 tia1.id = 1;
516 pthread_create(&t1, NULL, threadRunner, &tia1);
517 tia2.id = 2;
518 pthread_create(&t2, NULL, threadRunner, &tia2);
519 tia3.id = 3;
520 pthread_create(&t3, NULL, threadRunner, &tia3);
521 EventLoop();
522 return 0;
523}