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

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