Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glthreads.c 9.8KB

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