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.

opencloseopen.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
  3. * (C) Copyright IBM Corporation 2003
  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. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <string.h>
  26. #include <X11/Xlib.h>
  27. #include <GL/gl.h>
  28. #include <GL/glx.h>
  29. /** \file opencloseopen.c
  30. * Simple test for Mesa bug #508473. Create a window and rendering context.
  31. * Draw a single frame. Close the window, destroy the context, and close
  32. * the display. Re-open the display, create a new window and context. This
  33. * should work, but, at least as of Mesa 5.1, it segfaults. See the bug
  34. * report for more details.
  35. *
  36. * Most of the code here was lifed from various other Mesa xdemos.
  37. */
  38. static void
  39. draw(void)
  40. {
  41. glViewport(0, 0, 300, 300);
  42. glMatrixMode(GL_PROJECTION);
  43. glLoadIdentity();
  44. glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
  45. glMatrixMode(GL_MODELVIEW);
  46. glShadeModel(GL_FLAT);
  47. glClearColor(0.5, 0.5, 0.5, 1.0);
  48. glClear(GL_COLOR_BUFFER_BIT);
  49. /* draw blue quad */
  50. glLoadIdentity();
  51. glColor3f(0.3, 0.3, 1.0);
  52. glPushMatrix();
  53. glRotatef(0, 0, 0, 1);
  54. glBegin(GL_POLYGON);
  55. glVertex2f(-0.5, -0.25);
  56. glVertex2f( 0.5, -0.25);
  57. glVertex2f( 0.5, 0.25);
  58. glVertex2f(-0.5, 0.25);
  59. glEnd();
  60. glPopMatrix();}
  61. /*
  62. * Create an RGB, double-buffered window.
  63. * Return the window and context handles.
  64. */
  65. static void
  66. make_window( const char * dpyName, const char *name,
  67. int x, int y, int width, int height,
  68. Display **dpyRet, Window *winRet, GLXContext *ctxRet)
  69. {
  70. int attrib[] = { GLX_RGBA,
  71. GLX_RED_SIZE, 1,
  72. GLX_GREEN_SIZE, 1,
  73. GLX_BLUE_SIZE, 1,
  74. GLX_DOUBLEBUFFER,
  75. None };
  76. int scrnum;
  77. XSetWindowAttributes attr;
  78. unsigned long mask;
  79. Window root;
  80. Window win;
  81. GLXContext ctx;
  82. XVisualInfo *visinfo;
  83. Display *dpy;
  84. dpy = XOpenDisplay(dpyName);
  85. if (!dpy) {
  86. printf("Error: couldn't open display %s\n", XDisplayName(dpyName));
  87. exit(1);
  88. }
  89. *dpyRet = dpy;
  90. scrnum = DefaultScreen( dpy );
  91. root = RootWindow( dpy, scrnum );
  92. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  93. if (!visinfo) {
  94. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  95. exit(1);
  96. }
  97. /* window attributes */
  98. attr.background_pixel = 0;
  99. attr.border_pixel = 0;
  100. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  101. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  102. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  103. win = XCreateWindow( dpy, root, 0, 0, width, height,
  104. 0, visinfo->depth, InputOutput,
  105. visinfo->visual, mask, &attr );
  106. /* set hints and properties */
  107. {
  108. XSizeHints sizehints;
  109. sizehints.x = x;
  110. sizehints.y = y;
  111. sizehints.width = width;
  112. sizehints.height = height;
  113. sizehints.flags = USSize | USPosition;
  114. XSetNormalHints(dpy, win, &sizehints);
  115. XSetStandardProperties(dpy, win, name, name,
  116. None, (char **)NULL, 0, &sizehints);
  117. }
  118. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  119. if (!ctx) {
  120. printf("Error: glXCreateContext failed\n");
  121. exit(1);
  122. }
  123. XFree(visinfo);
  124. *winRet = win;
  125. *ctxRet = ctx;
  126. }
  127. static void
  128. destroy_window( Display *dpy, Window win, GLXContext ctx )
  129. {
  130. glXMakeCurrent(dpy, None, NULL);
  131. glXDestroyContext(dpy, ctx);
  132. XDestroyWindow(dpy, win);
  133. XCloseDisplay(dpy);
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. Display *dpy;
  139. Window win;
  140. GLXContext ctx;
  141. char *dpyName = ":0";
  142. int i;
  143. for (i = 1; i < argc; i++) {
  144. if (strcmp(argv[i], "-display") == 0) {
  145. dpyName = argv[i+1];
  146. i++;
  147. }
  148. }
  149. printf("If this program segfaults, then Mesa bug #508473 is probably "
  150. "back.\n");
  151. make_window(dpyName, "Open-close-open", 0, 0, 300, 300, &dpy, &win, &ctx);
  152. XMapWindow(dpy, win);
  153. glXMakeCurrent(dpy, win, ctx);
  154. draw();
  155. glXSwapBuffers(dpy, win);
  156. sleep(2);
  157. destroy_window(dpy, win, ctx);
  158. make_window(dpyName, "Open-close-open", 0, 0, 300, 300, &dpy, &win, &ctx);
  159. XMapWindow(dpy, win);
  160. glXMakeCurrent(dpy, win, ctx);
  161. destroy_window(dpy, win, ctx);
  162. return 0;
  163. }