Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*
  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. * Ported to EGL by Chia-I Wu <olvaffe@gmail.com>
  22. */
  23. /*
  24. * This program tests EGL thread safety.
  25. * Command line options:
  26. * -p Open a display connection for each thread
  27. * -l Enable application-side locking
  28. * -n <num threads> Number of threads to create (default is 2)
  29. * -display <display name> Specify X display (default is $DISPLAY)
  30. * -t Use texture mapping
  31. *
  32. * Brian Paul 20 July 2000
  33. */
  34. /*
  35. * Notes:
  36. * - Each thread gets its own EGL context.
  37. *
  38. * - The EGL contexts share texture objects.
  39. *
  40. * - When 't' is pressed to update the texture image, the window/thread which
  41. * has input focus is signalled to change the texture. The other threads
  42. * should see the updated texture the next time they call glBindTexture.
  43. */
  44. #if defined(PTHREADS) /* defined by Mesa on Linux and other platforms */
  45. #include <assert.h>
  46. #include <X11/Xlib.h>
  47. #include <X11/Xutil.h>
  48. #include <GL/gl.h>
  49. #include <EGL/egl.h>
  50. #include <math.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #include <pthread.h>
  56. /*
  57. * Each window/thread/context:
  58. */
  59. struct winthread {
  60. Display *Dpy;
  61. int Index;
  62. pthread_t Thread;
  63. Window Win;
  64. EGLDisplay Display;
  65. EGLContext Context;
  66. EGLSurface Surface;
  67. float Angle;
  68. int WinWidth, WinHeight;
  69. GLboolean NewSize;
  70. GLboolean Initialized;
  71. GLboolean MakeNewTexture;
  72. };
  73. #define MAX_WINTHREADS 100
  74. static struct winthread WinThreads[MAX_WINTHREADS];
  75. static int NumWinThreads = 0;
  76. static volatile GLboolean ExitFlag = GL_FALSE;
  77. static GLboolean MultiDisplays = 0;
  78. static GLboolean Locking = 0;
  79. static GLboolean Texture = GL_FALSE;
  80. static GLuint TexObj = 12;
  81. static GLboolean Animate = GL_TRUE;
  82. static pthread_mutex_t Mutex;
  83. static pthread_cond_t CondVar;
  84. static pthread_mutex_t CondMutex;
  85. static void
  86. Error(const char *msg)
  87. {
  88. fprintf(stderr, "Error: %s\n", msg);
  89. exit(1);
  90. }
  91. static void
  92. signal_redraw(void)
  93. {
  94. pthread_mutex_lock(&CondMutex);
  95. pthread_cond_broadcast(&CondVar);
  96. pthread_mutex_unlock(&CondMutex);
  97. }
  98. static void
  99. MakeNewTexture(struct winthread *wt)
  100. {
  101. #define TEX_SIZE 128
  102. static float step = 0.0;
  103. GLfloat image[TEX_SIZE][TEX_SIZE][4];
  104. GLint width;
  105. int i, j;
  106. for (j = 0; j < TEX_SIZE; j++) {
  107. for (i = 0; i < TEX_SIZE; i++) {
  108. float dt = 5.0 * (j - 0.5 * TEX_SIZE) / TEX_SIZE;
  109. float ds = 5.0 * (i - 0.5 * TEX_SIZE) / TEX_SIZE;
  110. float r = dt * dt + ds * ds + step;
  111. image[j][i][0] =
  112. image[j][i][1] =
  113. image[j][i][2] = 0.75 + 0.25 * cos(r);
  114. image[j][i][3] = 1.0;
  115. }
  116. }
  117. step += 0.5;
  118. glBindTexture(GL_TEXTURE_2D, TexObj);
  119. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
  120. if (width) {
  121. assert(width == TEX_SIZE);
  122. /* sub-tex replace */
  123. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TEX_SIZE, TEX_SIZE,
  124. GL_RGBA, GL_FLOAT, image);
  125. }
  126. else {
  127. /* create new */
  128. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  129. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  130. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0,
  131. GL_RGBA, GL_FLOAT, image);
  132. }
  133. }
  134. /* draw a colored cube */
  135. static void
  136. draw_object(void)
  137. {
  138. glPushMatrix();
  139. glScalef(0.75, 0.75, 0.75);
  140. glColor3f(1, 0, 0);
  141. if (Texture) {
  142. glBindTexture(GL_TEXTURE_2D, TexObj);
  143. glEnable(GL_TEXTURE_2D);
  144. }
  145. else {
  146. glDisable(GL_TEXTURE_2D);
  147. }
  148. glBegin(GL_QUADS);
  149. /* -X */
  150. glColor3f(0, 1, 1);
  151. glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
  152. glTexCoord2f(1, 0); glVertex3f(-1, 1, -1);
  153. glTexCoord2f(1, 1); glVertex3f(-1, 1, 1);
  154. glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
  155. /* +X */
  156. glColor3f(1, 0, 0);
  157. glTexCoord2f(0, 0); glVertex3f(1, -1, -1);
  158. glTexCoord2f(1, 0); glVertex3f(1, 1, -1);
  159. glTexCoord2f(1, 1); glVertex3f(1, 1, 1);
  160. glTexCoord2f(0, 1); glVertex3f(1, -1, 1);
  161. /* -Y */
  162. glColor3f(1, 0, 1);
  163. glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
  164. glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
  165. glTexCoord2f(1, 1); glVertex3f( 1, -1, 1);
  166. glTexCoord2f(0, 1); glVertex3f(-1, -1, 1);
  167. /* +Y */
  168. glColor3f(0, 1, 0);
  169. glTexCoord2f(0, 0); glVertex3f(-1, 1, -1);
  170. glTexCoord2f(1, 0); glVertex3f( 1, 1, -1);
  171. glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
  172. glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
  173. /* -Z */
  174. glColor3f(1, 1, 0);
  175. glTexCoord2f(0, 0); glVertex3f(-1, -1, -1);
  176. glTexCoord2f(1, 0); glVertex3f( 1, -1, -1);
  177. glTexCoord2f(1, 1); glVertex3f( 1, 1, -1);
  178. glTexCoord2f(0, 1); glVertex3f(-1, 1, -1);
  179. /* +Y */
  180. glColor3f(0, 0, 1);
  181. glTexCoord2f(0, 0); glVertex3f(-1, -1, 1);
  182. glTexCoord2f(1, 0); glVertex3f( 1, -1, 1);
  183. glTexCoord2f(1, 1); glVertex3f( 1, 1, 1);
  184. glTexCoord2f(0, 1); glVertex3f(-1, 1, 1);
  185. glEnd();
  186. glPopMatrix();
  187. }
  188. /* signal resize of given window */
  189. static void
  190. resize(struct winthread *wt, int w, int h)
  191. {
  192. wt->NewSize = GL_TRUE;
  193. wt->WinWidth = w;
  194. wt->WinHeight = h;
  195. if (!Animate)
  196. signal_redraw();
  197. }
  198. /*
  199. * We have an instance of this for each thread.
  200. */
  201. static void
  202. draw_loop(struct winthread *wt)
  203. {
  204. while (!ExitFlag) {
  205. if (Locking)
  206. pthread_mutex_lock(&Mutex);
  207. if (!wt->Initialized) {
  208. eglMakeCurrent(wt->Display, wt->Surface, wt->Surface, wt->Context);
  209. printf("xeglthreads: %d: GL_RENDERER = %s\n", wt->Index,
  210. (char *) glGetString(GL_RENDERER));
  211. if (Texture /*&& wt->Index == 0*/) {
  212. MakeNewTexture(wt);
  213. }
  214. wt->Initialized = GL_TRUE;
  215. }
  216. if (Locking)
  217. pthread_mutex_unlock(&Mutex);
  218. eglBindAPI(EGL_OPENGL_API);
  219. if (eglGetCurrentContext() != wt->Context) {
  220. printf("xeglthreads: current context %p != %p\n",
  221. eglGetCurrentContext(), wt->Context);
  222. }
  223. glEnable(GL_DEPTH_TEST);
  224. if (wt->NewSize) {
  225. GLfloat w = (float) wt->WinWidth / (float) wt->WinHeight;
  226. glViewport(0, 0, wt->WinWidth, wt->WinHeight);
  227. glMatrixMode(GL_PROJECTION);
  228. glLoadIdentity();
  229. glFrustum(-w, w, -1.0, 1.0, 1.5, 10);
  230. glMatrixMode(GL_MODELVIEW);
  231. glLoadIdentity();
  232. glTranslatef(0, 0, -2.5);
  233. wt->NewSize = GL_FALSE;
  234. }
  235. if (wt->MakeNewTexture) {
  236. MakeNewTexture(wt);
  237. wt->MakeNewTexture = GL_FALSE;
  238. }
  239. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  240. glPushMatrix();
  241. glRotatef(wt->Angle, 0, 1, 0);
  242. glRotatef(wt->Angle, 1, 0, 0);
  243. glScalef(0.7, 0.7, 0.7);
  244. draw_object();
  245. glPopMatrix();
  246. if (Locking)
  247. pthread_mutex_lock(&Mutex);
  248. eglSwapBuffers(wt->Display, wt->Surface);
  249. if (Locking)
  250. pthread_mutex_unlock(&Mutex);
  251. if (Animate) {
  252. usleep(5000);
  253. }
  254. else {
  255. /* wait for signal to draw */
  256. pthread_mutex_lock(&CondMutex);
  257. pthread_cond_wait(&CondVar, &CondMutex);
  258. pthread_mutex_unlock(&CondMutex);
  259. }
  260. wt->Angle += 1.0;
  261. }
  262. eglMakeCurrent(wt->Display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  263. }
  264. static void
  265. keypress(XEvent *event, struct winthread *wt)
  266. {
  267. char buf[100];
  268. KeySym keySym;
  269. XComposeStatus stat;
  270. XLookupString(&event->xkey, buf, sizeof(buf), &keySym, &stat);
  271. switch (keySym) {
  272. case XK_Escape:
  273. /* tell all threads to exit */
  274. if (!Animate) {
  275. signal_redraw();
  276. }
  277. ExitFlag = GL_TRUE;
  278. /*printf("exit draw_loop %d\n", wt->Index);*/
  279. return;
  280. case XK_t:
  281. case XK_T:
  282. if (Texture) {
  283. wt->MakeNewTexture = GL_TRUE;
  284. if (!Animate)
  285. signal_redraw();
  286. }
  287. break;
  288. case XK_a:
  289. case XK_A:
  290. Animate = !Animate;
  291. if (Animate) /* yes, prev Animate state! */
  292. signal_redraw();
  293. break;
  294. case XK_s:
  295. case XK_S:
  296. if (!Animate)
  297. signal_redraw();
  298. break;
  299. default:
  300. ; /* nop */
  301. }
  302. }
  303. /*
  304. * The main process thread runs this loop.
  305. * Single display connection for all threads.
  306. */
  307. static void
  308. event_loop(Display *dpy)
  309. {
  310. XEvent event;
  311. int i;
  312. assert(!MultiDisplays);
  313. while (!ExitFlag) {
  314. if (Locking) {
  315. while (1) {
  316. int k;
  317. pthread_mutex_lock(&Mutex);
  318. k = XPending(dpy);
  319. if (k) {
  320. XNextEvent(dpy, &event);
  321. pthread_mutex_unlock(&Mutex);
  322. break;
  323. }
  324. pthread_mutex_unlock(&Mutex);
  325. usleep(5000);
  326. }
  327. }
  328. else {
  329. XNextEvent(dpy, &event);
  330. }
  331. switch (event.type) {
  332. case ConfigureNotify:
  333. /* Find winthread for this event's window */
  334. for (i = 0; i < NumWinThreads; i++) {
  335. struct winthread *wt = &WinThreads[i];
  336. if (event.xconfigure.window == wt->Win) {
  337. resize(wt, event.xconfigure.width,
  338. event.xconfigure.height);
  339. break;
  340. }
  341. }
  342. break;
  343. case KeyPress:
  344. for (i = 0; i < NumWinThreads; i++) {
  345. struct winthread *wt = &WinThreads[i];
  346. if (event.xkey.window == wt->Win) {
  347. keypress(&event, wt);
  348. break;
  349. }
  350. }
  351. break;
  352. default:
  353. /*no-op*/ ;
  354. }
  355. }
  356. }
  357. /*
  358. * Separate display connection for each thread.
  359. */
  360. static void
  361. event_loop_multi(void)
  362. {
  363. XEvent event;
  364. int w = 0;
  365. assert(MultiDisplays);
  366. while (!ExitFlag) {
  367. struct winthread *wt = &WinThreads[w];
  368. if (XPending(wt->Dpy)) {
  369. XNextEvent(wt->Dpy, &event);
  370. switch (event.type) {
  371. case ConfigureNotify:
  372. resize(wt, event.xconfigure.width, event.xconfigure.height);
  373. break;
  374. case KeyPress:
  375. keypress(&event, wt);
  376. break;
  377. default:
  378. ; /* nop */
  379. }
  380. }
  381. w = (w + 1) % NumWinThreads;
  382. usleep(5000);
  383. }
  384. }
  385. /*
  386. * we'll call this once for each thread, before the threads are created.
  387. */
  388. static void
  389. create_window(struct winthread *wt, EGLContext shareCtx)
  390. {
  391. Window win;
  392. EGLContext ctx;
  393. EGLSurface surf;
  394. EGLint attribs[] = { EGL_RED_SIZE, 1,
  395. EGL_GREEN_SIZE, 1,
  396. EGL_BLUE_SIZE, 1,
  397. EGL_DEPTH_SIZE, 1,
  398. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  399. EGL_NONE };
  400. EGLConfig config;
  401. EGLint num_configs;
  402. EGLint vid;
  403. int scrnum;
  404. XSetWindowAttributes attr;
  405. unsigned long mask;
  406. Window root;
  407. XVisualInfo *visinfo, visTemplate;
  408. int num_visuals;
  409. int width = 160, height = 160;
  410. int xpos = (wt->Index % 8) * (width + 10);
  411. int ypos = (wt->Index / 8) * (width + 20);
  412. scrnum = DefaultScreen(wt->Dpy);
  413. root = RootWindow(wt->Dpy, scrnum);
  414. if (!eglChooseConfig(wt->Display, attribs, &config, 1, &num_configs) ||
  415. !num_configs) {
  416. Error("Unable to choose an EGL config");
  417. }
  418. assert(config);
  419. assert(num_configs > 0);
  420. if (!eglGetConfigAttrib(wt->Display, config, EGL_NATIVE_VISUAL_ID, &vid)) {
  421. Error("Unable to get visual id of EGL config\n");
  422. }
  423. visTemplate.visualid = vid;
  424. visinfo = XGetVisualInfo(wt->Dpy, VisualIDMask,
  425. &visTemplate, &num_visuals);
  426. if (!visinfo) {
  427. Error("Unable to find RGB, Z, double-buffered visual");
  428. }
  429. /* window attributes */
  430. attr.background_pixel = 0;
  431. attr.border_pixel = 0;
  432. attr.colormap = XCreateColormap(wt->Dpy, root, visinfo->visual, AllocNone);
  433. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  434. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  435. win = XCreateWindow(wt->Dpy, root, xpos, ypos, width, height,
  436. 0, visinfo->depth, InputOutput,
  437. visinfo->visual, mask, &attr);
  438. if (!win) {
  439. Error("Couldn't create window");
  440. }
  441. XFree(visinfo);
  442. {
  443. XSizeHints sizehints;
  444. sizehints.x = xpos;
  445. sizehints.y = ypos;
  446. sizehints.width = width;
  447. sizehints.height = height;
  448. sizehints.flags = USSize | USPosition;
  449. XSetNormalHints(wt->Dpy, win, &sizehints);
  450. XSetStandardProperties(wt->Dpy, win, "xeglthreads", "xeglthreads",
  451. None, (char **)NULL, 0, &sizehints);
  452. }
  453. eglBindAPI(EGL_OPENGL_API);
  454. ctx = eglCreateContext(wt->Display, config, shareCtx, NULL);
  455. if (!ctx) {
  456. Error("Couldn't create EGL context");
  457. }
  458. surf = eglCreateWindowSurface(wt->Display, config, win, NULL);
  459. if (!surf) {
  460. Error("Couldn't create EGL surface");
  461. }
  462. XMapWindow(wt->Dpy, win);
  463. XSync(wt->Dpy, 0);
  464. /* save the info for this window/context */
  465. wt->Win = win;
  466. wt->Context = ctx;
  467. wt->Surface = surf;
  468. wt->Angle = 0.0;
  469. wt->WinWidth = width;
  470. wt->WinHeight = height;
  471. wt->NewSize = GL_TRUE;
  472. }
  473. /*
  474. * Called by pthread_create()
  475. */
  476. static void *
  477. thread_function(void *p)
  478. {
  479. struct winthread *wt = (struct winthread *) p;
  480. draw_loop(wt);
  481. return NULL;
  482. }
  483. /*
  484. * called before exit to wait for all threads to finish
  485. */
  486. static void
  487. clean_up(void)
  488. {
  489. int i;
  490. /* wait for threads to finish */
  491. for (i = 0; i < NumWinThreads; i++) {
  492. pthread_join(WinThreads[i].Thread, NULL);
  493. }
  494. for (i = 0; i < NumWinThreads; i++) {
  495. eglDestroyContext(WinThreads[i].Display, WinThreads[i].Context);
  496. XDestroyWindow(WinThreads[i].Dpy, WinThreads[i].Win);
  497. }
  498. }
  499. static void
  500. usage(void)
  501. {
  502. printf("xeglthreads: test of EGL/GL thread safety (any key = exit)\n");
  503. printf("Usage:\n");
  504. printf(" xeglthreads [options]\n");
  505. printf("Options:\n");
  506. printf(" -display DISPLAYNAME Specify display string\n");
  507. printf(" -n NUMTHREADS Number of threads to create\n");
  508. printf(" -p Use a separate display connection for each thread\n");
  509. printf(" -l Use application-side locking\n");
  510. printf(" -t Enable texturing\n");
  511. printf("Keyboard:\n");
  512. printf(" Esc Exit\n");
  513. printf(" t Change texture image (requires -t option)\n");
  514. printf(" a Toggle animation\n");
  515. printf(" s Step rotation (when not animating)\n");
  516. }
  517. int
  518. main(int argc, char *argv[])
  519. {
  520. char *displayName = NULL;
  521. int numThreads = 2;
  522. Display *dpy = NULL;
  523. EGLDisplay *egl_dpy = NULL;
  524. int i;
  525. Status threadStat;
  526. if (argc == 1) {
  527. usage();
  528. }
  529. else {
  530. int i;
  531. for (i = 1; i < argc; i++) {
  532. if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
  533. displayName = argv[i + 1];
  534. i++;
  535. }
  536. else if (strcmp(argv[i], "-p") == 0) {
  537. MultiDisplays = 1;
  538. }
  539. else if (strcmp(argv[i], "-l") == 0) {
  540. Locking = 1;
  541. }
  542. else if (strcmp(argv[i], "-t") == 0) {
  543. Texture = 1;
  544. }
  545. else if (strcmp(argv[i], "-n") == 0 && i + 1 < argc) {
  546. numThreads = atoi(argv[i + 1]);
  547. if (numThreads < 1)
  548. numThreads = 1;
  549. else if (numThreads > MAX_WINTHREADS)
  550. numThreads = MAX_WINTHREADS;
  551. i++;
  552. }
  553. else {
  554. usage();
  555. exit(1);
  556. }
  557. }
  558. }
  559. if (Locking)
  560. printf("xeglthreads: Using explicit locks around Xlib calls.\n");
  561. else
  562. printf("xeglthreads: No explict locking.\n");
  563. if (MultiDisplays)
  564. printf("xeglthreads: Per-thread display connections.\n");
  565. else
  566. printf("xeglthreads: Single display connection.\n");
  567. /*
  568. * VERY IMPORTANT: call XInitThreads() before any other Xlib functions.
  569. */
  570. if (!MultiDisplays) {
  571. if (!Locking) {
  572. threadStat = XInitThreads();
  573. if (threadStat) {
  574. printf("XInitThreads() returned %d (success)\n",
  575. (int) threadStat);
  576. }
  577. else {
  578. printf("XInitThreads() returned 0 "
  579. "(failure- this program may fail)\n");
  580. }
  581. }
  582. dpy = XOpenDisplay(displayName);
  583. if (!dpy) {
  584. fprintf(stderr, "Unable to open display %s\n",
  585. XDisplayName(displayName));
  586. return -1;
  587. }
  588. egl_dpy = eglGetDisplay(dpy);
  589. if (!egl_dpy) {
  590. fprintf(stderr, "Unable to get EGL display\n");
  591. XCloseDisplay(dpy);
  592. return -1;
  593. }
  594. if (!eglInitialize(egl_dpy, NULL, NULL)) {
  595. fprintf(stderr, "Unable to initialize EGL display\n");
  596. return -1;
  597. }
  598. }
  599. pthread_mutex_init(&Mutex, NULL);
  600. pthread_mutex_init(&CondMutex, NULL);
  601. pthread_cond_init(&CondVar, NULL);
  602. printf("xeglthreads: creating windows\n");
  603. NumWinThreads = numThreads;
  604. /* Create the EGL windows and contexts */
  605. for (i = 0; i < numThreads; i++) {
  606. EGLContext share;
  607. if (MultiDisplays) {
  608. WinThreads[i].Dpy = XOpenDisplay(displayName);
  609. assert(WinThreads[i].Dpy);
  610. WinThreads[i].Display = eglGetDisplay(WinThreads[i].Dpy);
  611. assert(eglInitialize(WinThreads[i].Display, NULL, NULL));
  612. }
  613. else {
  614. WinThreads[i].Dpy = dpy;
  615. WinThreads[i].Display = egl_dpy;
  616. }
  617. WinThreads[i].Index = i;
  618. WinThreads[i].Initialized = GL_FALSE;
  619. share = (Texture && i > 0) ? WinThreads[0].Context : 0;
  620. create_window(&WinThreads[i], share);
  621. }
  622. printf("xeglthreads: creating threads\n");
  623. /* Create the threads */
  624. for (i = 0; i < numThreads; i++) {
  625. pthread_create(&WinThreads[i].Thread, NULL, thread_function,
  626. (void*) &WinThreads[i]);
  627. printf("xeglthreads: Created thread %p\n",
  628. (void *) WinThreads[i].Thread);
  629. }
  630. if (MultiDisplays)
  631. event_loop_multi();
  632. else
  633. event_loop(dpy);
  634. clean_up();
  635. if (MultiDisplays) {
  636. for (i = 0; i < numThreads; i++) {
  637. eglTerminate(WinThreads[i].Display);
  638. XCloseDisplay(WinThreads[i].Dpy);
  639. }
  640. }
  641. else {
  642. eglTerminate(egl_dpy);
  643. XCloseDisplay(dpy);
  644. }
  645. return 0;
  646. }
  647. #else /* PTHREADS */
  648. #include <stdio.h>
  649. int
  650. main(int argc, char *argv[])
  651. {
  652. printf("Sorry, this program wasn't compiled with PTHREADS defined.\n");
  653. return 0;
  654. }
  655. #endif /* PTHREADS */