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.

jkrahntest.c 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* This is a good test for glXSwapBuffers on non-current windows,
  2. * and the glXCopyContext function. Fixed several Mesa/DRI bugs with
  3. * this program on 15 June 2002.
  4. *
  5. * Joe's comments follow:
  6. *
  7. * I have tried some different approaches for being able to
  8. * draw to multiple windows using one context, or a copied
  9. * context. Mesa/indirect rendering works to use one context
  10. * for multiple windows, but crashes with glXCopyContext.
  11. * DRI is badly broken, at least for ATI.
  12. *
  13. * I also noticed that glXMakeCurrent allows a window and context
  14. * from different visuals to be attached (haven't tested recently).
  15. *
  16. * Joe Krahn <jkrahn@nc.rr.com>
  17. */
  18. #include <GL/glx.h>
  19. #include <GL/gl.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <math.h>
  25. #ifndef M_PI
  26. #define M_PI 3.14159
  27. #endif
  28. #define DEGTOR (M_PI/180.0)
  29. static int AttributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
  30. int main(int argc, char **argv)
  31. {
  32. Window win1, win2;
  33. XVisualInfo *vi;
  34. XSetWindowAttributes swa;
  35. Display *dpy;
  36. GLXContext ctx1, ctx2;
  37. float angle;
  38. int test;
  39. if (argc < 2) {
  40. fprintf(stderr, "This program tests GLX context switching.\n");
  41. fprintf(stderr, "Usage: jkrahntest <n>\n");
  42. fprintf(stderr, "Where n is:\n");
  43. fprintf(stderr, "\t1) Use two contexts and swap only when the context is current (typical case).\n");
  44. fprintf(stderr, "\t2) Use two contexts and swap at the same time.\n");
  45. fprintf(stderr, "\t\t Used to crash Mesa & nVidia, and DRI artifacts. Seems OK now.\n");
  46. fprintf(stderr, "\t3) Use one context, but only swap when a context is current.\n");
  47. fprintf(stderr, "\t\t Serious artifacts for DRI at least with ATI.\n");
  48. fprintf(stderr, "\t4) Use one context, swap both windows at the same time, so the left\n");
  49. fprintf(stderr, "\t\t window has no context at swap time. Severe artifacts for DRI.\n");
  50. fprintf(stderr, "\t5) Use two contexts, copying one to the other when switching windows.\n");
  51. fprintf(stderr, "\t\t DRI gives an error, indirect rendering crashes server.\n");
  52. exit(1);
  53. }
  54. test = atoi(argv[1]);
  55. /* get a connection */
  56. dpy = XOpenDisplay(NULL);
  57. /* Get an appropriate visual */
  58. vi = glXChooseVisual(dpy, DefaultScreen(dpy), AttributeList);
  59. if (vi == 0) {
  60. fprintf(stderr, "No matching visuals found.\n");
  61. exit(-1);
  62. }
  63. /* Create two GLX contexts, with list sharing */
  64. ctx1 = glXCreateContext(dpy, vi, 0, True);
  65. ctx2 = glXCreateContext(dpy, vi, ctx1, True);
  66. /* create a colormap */
  67. swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  68. vi->visual, AllocNone);
  69. swa.border_pixel = 0;
  70. /* Create two windows */
  71. win1 = XCreateWindow(dpy, RootWindow(dpy, vi->screen),
  72. 10, 10, 200, 200,
  73. 0, vi->depth, InputOutput, vi->visual,
  74. CWBorderPixel | CWColormap, &swa);
  75. XStoreName(dpy, win1, "Test [L]");
  76. XMapWindow(dpy, win1);
  77. XMoveWindow(dpy, win1, 10, 10); /* Initial requested x,y may not be honored */
  78. {
  79. XSizeHints sizehints;
  80. static const char *name = "window";
  81. sizehints.x = 10;
  82. sizehints.y = 10;
  83. sizehints.width = 200;
  84. sizehints.height = 200;
  85. sizehints.flags = USSize | USPosition;
  86. XSetNormalHints(dpy, win1, &sizehints);
  87. XSetStandardProperties(dpy, win1, name, name,
  88. None, (char **)NULL, 0, &sizehints);
  89. }
  90. win2 = XCreateWindow(dpy, RootWindow(dpy, vi->screen),
  91. 250, 10, 200, 200,
  92. 0, vi->depth, InputOutput, vi->visual,
  93. CWBorderPixel | CWColormap, &swa);
  94. XStoreName(dpy, win1, "Test [R]");
  95. XMapWindow(dpy, win2);
  96. XMoveWindow(dpy, win2, 260, 10);
  97. {
  98. XSizeHints sizehints;
  99. static const char *name = "window";
  100. sizehints.x = 10;
  101. sizehints.y = 10;
  102. sizehints.width = 200;
  103. sizehints.height = 200;
  104. sizehints.flags = USSize | USPosition;
  105. XSetNormalHints(dpy, win2, &sizehints);
  106. XSetStandardProperties(dpy, win2, name, name,
  107. None, (char **)NULL, 0, &sizehints);
  108. }
  109. /* Now draw some spinning things */
  110. for (angle = 0; angle < 360*4; angle += 10.0) {
  111. /* Connect the context to window 1 */
  112. glXMakeCurrent(dpy, win1, ctx1);
  113. /* Clear and draw in window 1 */
  114. glDrawBuffer(GL_BACK);
  115. glClearColor(1, 1, 0, 1);
  116. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  117. glColor3f(1, 0, 0);
  118. glBegin(GL_TRIANGLES);
  119. glVertex2f(0, 0);
  120. glVertex2f(cos(angle * DEGTOR), sin(angle * DEGTOR));
  121. glVertex2f(cos((angle + 20.0) * DEGTOR),
  122. sin((angle + 20.0) * DEGTOR));
  123. glEnd();
  124. glFlush();
  125. if (test == 1 || test == 3 || test == 5)
  126. glXSwapBuffers(dpy, win1);
  127. if (test == 5)
  128. glXCopyContext(dpy, ctx1, ctx2, GL_ALL_ATTRIB_BITS);
  129. /* Connect the context to window 2 */
  130. if (test == 3 || test == 4) {
  131. glXMakeCurrent(dpy, win2, ctx1);
  132. } else {
  133. glXMakeCurrent(dpy, win2, ctx2);
  134. }
  135. /* Clear and draw in window 2 */
  136. glDrawBuffer(GL_BACK);
  137. glClearColor(0, 0, 1, 1);
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. glColor3f(1, 1, 0);
  140. glBegin(GL_TRIANGLES);
  141. glVertex2f(0, 0);
  142. glVertex2f(cos(angle * DEGTOR), sin(angle * DEGTOR));
  143. glVertex2f(cos((angle + 20.0) * DEGTOR),
  144. sin((angle + 20.0) * DEGTOR));
  145. glEnd();
  146. glFlush();
  147. /* Swap buffers */
  148. if (test == 2 || test == 4)
  149. glXSwapBuffers(dpy, win1);
  150. glXSwapBuffers(dpy, win2);
  151. /* wait a while */
  152. glXWaitX();
  153. usleep(20000);
  154. }
  155. return 0;
  156. }