Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * OpenGL pbuffers utility functions. |
| 4 | * |
| 5 | * Brian Paul |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 6 | * Original code: April 1997 |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 7 | * Updated on 5 October 2002 |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 8 | * Updated again on 3 January 2005 to use GLX 1.3 functions in preference |
| 9 | * to the GLX_SGIX_fbconfig/pbuffer extensions. |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include "pbutil.h" |
| 15 | |
| 16 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 17 | /** |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 18 | * Test if we pixel buffers are available for a particular X screen. |
| 19 | * Input: dpy - the X display |
| 20 | * screen - screen number |
| 21 | * Return: 0 = pixel buffers not available. |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 22 | * 1 = pixel buffers are available via GLX 1.3. |
| 23 | * 2 = pixel buffers are available via GLX_SGIX_fbconfig/pbuffer. |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 24 | */ |
| 25 | int |
| 26 | QueryPbuffers(Display *dpy, int screen) |
| 27 | { |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 28 | #if defined(GLX_VERSION_1_3) |
| 29 | { |
| 30 | /* GLX 1.3 supports pbuffers */ |
| 31 | int glxVersionMajor, glxVersionMinor; |
| 32 | if (!glXQueryVersion(dpy, &glxVersionMajor, &glxVersionMinor)) { |
| 33 | /* GLX not available! */ |
| 34 | return 0; |
| 35 | } |
| 36 | if (glxVersionMajor * 100 + glxVersionMinor >= 103) { |
| 37 | return 1; |
| 38 | } |
| 39 | /* fall-through */ |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 40 | } |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 41 | #endif |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 42 | |
| 43 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 44 | /* Try the SGIX extensions */ |
| 45 | { |
| 46 | char *extensions; |
| 47 | extensions = (char *) glXQueryServerString(dpy, screen, GLX_EXTENSIONS); |
| 48 | if (!extensions || |
| 49 | !strstr(extensions,"GLX_SGIX_fbconfig") || |
| 50 | !strstr(extensions,"GLX_SGIX_pbuffer")) { |
| 51 | return 0; |
| 52 | } |
| 53 | return 2; |
| 54 | } |
| 55 | #endif |
| 56 | |
| 57 | return 0; |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
| 61 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 62 | FBCONFIG * |
| 63 | ChooseFBConfig(Display *dpy, int screen, const int attribs[], int *nConfigs) |
| 64 | { |
| 65 | int pbSupport = QueryPbuffers(dpy, screen); |
| 66 | #if defined(GLX_VERSION_1_3) |
| 67 | if (pbSupport == 1) { |
| 68 | return glXChooseFBConfig(dpy, screen, attribs, nConfigs); |
| 69 | } |
| 70 | #endif |
| 71 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 72 | if (pbSupport == 2) { |
| 73 | return glXChooseFBConfigSGIX(dpy, screen, (int *) attribs, nConfigs); |
| 74 | } |
| 75 | #endif |
| 76 | return NULL; |
| 77 | } |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 78 | |
| 79 | |
Brian Paul | ba5ceda | 2005-01-07 01:17:42 +0000 | [diff] [blame] | 80 | FBCONFIG * |
| 81 | GetAllFBConfigs(Display *dpy, int screen, int *nConfigs) |
| 82 | { |
| 83 | int pbSupport = QueryPbuffers(dpy, screen); |
| 84 | #if defined(GLX_VERSION_1_3) |
| 85 | if (pbSupport == 1) { |
| 86 | return glXGetFBConfigs(dpy, screen, nConfigs); |
| 87 | } |
| 88 | #endif |
| 89 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 90 | if (pbSupport == 2) { |
Brian Paul | c3984fc | 2005-04-06 22:40:39 +0000 | [diff] [blame] | 91 | /* The GLX_SGIX_fbconfig extensions says to pass NULL to get list |
| 92 | * of all available configurations. |
| 93 | */ |
| 94 | return glXChooseFBConfigSGIX(dpy, screen, NULL, nConfigs); |
Brian Paul | ba5ceda | 2005-01-07 01:17:42 +0000 | [diff] [blame] | 95 | } |
| 96 | #endif |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 100 | |
| 101 | XVisualInfo * |
| 102 | GetVisualFromFBConfig(Display *dpy, int screen, FBCONFIG config) |
| 103 | { |
| 104 | int pbSupport = QueryPbuffers(dpy, screen); |
| 105 | #if defined(GLX_VERSION_1_3) |
| 106 | if (pbSupport == 1) { |
| 107 | return glXGetVisualFromFBConfig(dpy, config); |
| 108 | } |
| 109 | #endif |
| 110 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 111 | if (pbSupport == 2) { |
| 112 | return glXGetVisualFromFBConfigSGIX(dpy, config); |
| 113 | } |
| 114 | #endif |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Either use glXGetFBConfigAttrib() or glXGetFBConfigAttribSGIX() |
| 121 | * to query an fbconfig attribute. |
| 122 | */ |
| 123 | static int |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 124 | GetFBConfigAttrib(Display *dpy, int screen, |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 125 | #if defined(GLX_VERSION_1_3) |
| 126 | const GLXFBConfig config, |
| 127 | #elif defined(GLX_SGIX_fbconfig) |
| 128 | const GLXFBConfigSGIX config, |
| 129 | #endif |
| 130 | int attrib |
| 131 | ) |
| 132 | { |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 133 | int pbSupport = QueryPbuffers(dpy, screen); |
| 134 | int value = 0; |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 135 | |
| 136 | #if defined(GLX_VERSION_1_3) |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 137 | if (pbSupport == 1) { |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 138 | /* ok */ |
| 139 | if (glXGetFBConfigAttrib(dpy, config, attrib, &value) != 0) { |
| 140 | value = 0; |
| 141 | } |
| 142 | return value; |
| 143 | } |
| 144 | /* fall-through */ |
| 145 | #endif |
| 146 | |
| 147 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 148 | if (pbSupport == 2) { |
| 149 | if (glXGetFBConfigAttribSGIX(dpy, config, attrib, &value) != 0) { |
| 150 | value = 0; |
| 151 | } |
| 152 | return value; |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 153 | } |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 154 | #endif |
| 155 | |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 156 | return value; |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | /** |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 162 | * Print parameters for a GLXFBConfig to stdout. |
| 163 | * Input: dpy - the X display |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 164 | * screen - the X screen number |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 165 | * fbConfig - the fbconfig handle |
| 166 | * horizFormat - if true, print in horizontal format |
| 167 | */ |
| 168 | void |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 169 | PrintFBConfigInfo(Display *dpy, int screen, FBCONFIG config, Bool horizFormat) |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 170 | { |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 171 | PBUFFER pBuffer; |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 172 | int width=2, height=2; |
| 173 | int bufferSize, level, doubleBuffer, stereo, auxBuffers; |
| 174 | int redSize, greenSize, blueSize, alphaSize; |
| 175 | int depthSize, stencilSize; |
| 176 | int accumRedSize, accumBlueSize, accumGreenSize, accumAlphaSize; |
| 177 | int sampleBuffers, samples; |
| 178 | int drawableType, renderType, xRenderable, xVisual, id; |
| 179 | int maxWidth, maxHeight, maxPixels; |
| 180 | int optWidth, optHeight; |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 181 | int floatComponents = 0; |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 182 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 183 | /* do queries using the GLX 1.3 tokens (same as the SGIX tokens) */ |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 184 | bufferSize = GetFBConfigAttrib(dpy, screen, config, GLX_BUFFER_SIZE); |
| 185 | level = GetFBConfigAttrib(dpy, screen, config, GLX_LEVEL); |
| 186 | doubleBuffer = GetFBConfigAttrib(dpy, screen, config, GLX_DOUBLEBUFFER); |
| 187 | stereo = GetFBConfigAttrib(dpy, screen, config, GLX_STEREO); |
| 188 | auxBuffers = GetFBConfigAttrib(dpy, screen, config, GLX_AUX_BUFFERS); |
| 189 | redSize = GetFBConfigAttrib(dpy, screen, config, GLX_RED_SIZE); |
| 190 | greenSize = GetFBConfigAttrib(dpy, screen, config, GLX_GREEN_SIZE); |
| 191 | blueSize = GetFBConfigAttrib(dpy, screen, config, GLX_BLUE_SIZE); |
| 192 | alphaSize = GetFBConfigAttrib(dpy, screen, config, GLX_ALPHA_SIZE); |
| 193 | depthSize = GetFBConfigAttrib(dpy, screen, config, GLX_DEPTH_SIZE); |
| 194 | stencilSize = GetFBConfigAttrib(dpy, screen, config, GLX_STENCIL_SIZE); |
| 195 | accumRedSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_RED_SIZE); |
| 196 | accumGreenSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_GREEN_SIZE); |
| 197 | accumBlueSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_BLUE_SIZE); |
| 198 | accumAlphaSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_ALPHA_SIZE); |
| 199 | sampleBuffers = GetFBConfigAttrib(dpy, screen, config, GLX_SAMPLE_BUFFERS); |
| 200 | samples = GetFBConfigAttrib(dpy, screen, config, GLX_SAMPLES); |
| 201 | drawableType = GetFBConfigAttrib(dpy, screen, config, GLX_DRAWABLE_TYPE); |
| 202 | renderType = GetFBConfigAttrib(dpy, screen, config, GLX_RENDER_TYPE); |
| 203 | xRenderable = GetFBConfigAttrib(dpy, screen, config, GLX_X_RENDERABLE); |
| 204 | xVisual = GetFBConfigAttrib(dpy, screen, config, GLX_X_VISUAL_TYPE); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 205 | if (!xRenderable || !(drawableType & GLX_WINDOW_BIT_SGIX)) |
| 206 | xVisual = -1; |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 207 | |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 208 | id = GetFBConfigAttrib(dpy, screen, config, GLX_FBCONFIG_ID); |
| 209 | maxWidth = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_WIDTH); |
| 210 | maxHeight = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_HEIGHT); |
| 211 | maxPixels = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_PIXELS); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 212 | #if defined(GLX_SGIX_pbuffer) |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 213 | optWidth = GetFBConfigAttrib(dpy, screen, config, GLX_OPTIMAL_PBUFFER_WIDTH_SGIX); |
| 214 | optHeight = GetFBConfigAttrib(dpy, screen, config, GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 215 | #else |
| 216 | optWidth = optHeight = 0; |
| 217 | #endif |
| 218 | #if defined(GLX_NV_float_buffer) |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 219 | floatComponents = GetFBConfigAttrib(dpy, screen, config, GLX_FLOAT_COMPONENTS_NV); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 220 | #endif |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 221 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 222 | /* See if we can create a pbuffer with this config */ |
| 223 | pBuffer = CreatePbuffer(dpy, screen, config, width, height, False, False); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 224 | |
| 225 | if (horizFormat) { |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 226 | printf("0x%-9x ", id); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 227 | if (xVisual==GLX_STATIC_GRAY) printf("StaticGray "); |
| 228 | else if (xVisual==GLX_GRAY_SCALE) printf("GrayScale "); |
| 229 | else if (xVisual==GLX_STATIC_COLOR) printf("StaticColor "); |
| 230 | else if (xVisual==GLX_PSEUDO_COLOR) printf("PseudoColor "); |
| 231 | else if (xVisual==GLX_TRUE_COLOR) printf("TrueColor "); |
| 232 | else if (xVisual==GLX_DIRECT_COLOR) printf("DirectColor "); |
| 233 | else printf(" -none- "); |
| 234 | printf(" %3d %3d %s %s %s %2s ", bufferSize, level, |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 235 | (renderType & GLX_RGBA_BIT_SGIX) ? "y" : ".", |
| 236 | (renderType & GLX_COLOR_INDEX_BIT_SGIX) ? "y" : ".", |
| 237 | doubleBuffer ? "y" : ".", |
| 238 | stereo ? "y" : "."); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 239 | printf("%2d %2d %2d %2d ", redSize, greenSize, blueSize, alphaSize); |
| 240 | printf("%2d %2d ", depthSize, stencilSize); |
| 241 | printf("%2d %2d %2d %2d", accumRedSize, accumGreenSize, accumBlueSize, |
| 242 | accumAlphaSize); |
| 243 | printf(" %2d %2d", sampleBuffers, samples); |
Brian Paul | ff53a4e | 2005-01-10 23:15:59 +0000 | [diff] [blame] | 244 | printf(" %s %c", pBuffer ? "y" : ".", |
| 245 | ".y"[floatComponents]); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 246 | printf("\n"); |
| 247 | } |
| 248 | else { |
| 249 | printf("Id 0x%x\n", id); |
| 250 | printf(" Buffer Size: %d\n", bufferSize); |
| 251 | printf(" Level: %d\n", level); |
| 252 | printf(" Double Buffer: %s\n", doubleBuffer ? "yes" : "no"); |
| 253 | printf(" Stereo: %s\n", stereo ? "yes" : "no"); |
| 254 | printf(" Aux Buffers: %d\n", auxBuffers); |
| 255 | printf(" Red Size: %d\n", redSize); |
| 256 | printf(" Green Size: %d\n", greenSize); |
| 257 | printf(" Blue Size: %d\n", blueSize); |
| 258 | printf(" Alpha Size: %d\n", alphaSize); |
| 259 | printf(" Depth Size: %d\n", depthSize); |
| 260 | printf(" Stencil Size: %d\n", stencilSize); |
| 261 | printf(" Accum Red Size: %d\n", accumRedSize); |
| 262 | printf(" Accum Green Size: %d\n", accumGreenSize); |
| 263 | printf(" Accum Blue Size: %d\n", accumBlueSize); |
| 264 | printf(" Accum Alpha Size: %d\n", accumAlphaSize); |
| 265 | printf(" Sample Buffers: %d\n", sampleBuffers); |
| 266 | printf(" Samples/Pixel: %d\n", samples); |
| 267 | printf(" Drawable Types: "); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 268 | if (drawableType & GLX_WINDOW_BIT) printf("Window "); |
| 269 | if (drawableType & GLX_PIXMAP_BIT) printf("Pixmap "); |
| 270 | if (drawableType & GLX_PBUFFER_BIT) printf("PBuffer"); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 271 | printf("\n"); |
| 272 | printf(" Render Types: "); |
| 273 | if (renderType & GLX_RGBA_BIT_SGIX) printf("RGBA "); |
| 274 | if (renderType & GLX_COLOR_INDEX_BIT_SGIX) printf("CI "); |
| 275 | printf("\n"); |
| 276 | printf(" X Renderable: %s\n", xRenderable ? "yes" : "no"); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 277 | |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 278 | printf(" Pbuffer: %s\n", pBuffer ? "yes" : "no"); |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 279 | printf(" Max Pbuffer width: %d\n", maxWidth); |
| 280 | printf(" Max Pbuffer height: %d\n", maxHeight); |
| 281 | printf(" Max Pbuffer pixels: %d\n", maxPixels); |
| 282 | printf(" Optimum Pbuffer width: %d\n", optWidth); |
| 283 | printf(" Optimum Pbuffer height: %d\n", optHeight); |
| 284 | |
| 285 | printf(" Float Components: %s\n", floatComponents ? "yes" : "no"); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (pBuffer) { |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 289 | DestroyPbuffer(dpy, screen, pBuffer); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | |
| 294 | |
Brian Paul | c617829 | 2005-04-06 22:41:46 +0000 | [diff] [blame] | 295 | GLXContext |
| 296 | CreateContext(Display *dpy, int screen, FBCONFIG config) |
| 297 | { |
| 298 | int pbSupport = QueryPbuffers(dpy, screen); |
| 299 | #if defined(GLX_VERSION_1_3) |
| 300 | if (pbSupport == 1) { |
| 301 | /* GLX 1.3 */ |
| 302 | GLXContext c; |
| 303 | c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, True); |
| 304 | if (!c) { |
| 305 | /* try indirect */ |
| 306 | c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, False); |
| 307 | } |
| 308 | return c; |
| 309 | } |
| 310 | #endif |
| 311 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 312 | if (pbSupport == 2) { |
| 313 | GLXContext c; |
| 314 | c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, True); |
| 315 | if (!c) { |
| 316 | c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, False); |
| 317 | } |
| 318 | return c; |
| 319 | } |
| 320 | #endif |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | void |
| 326 | DestroyContext(Display *dpy, GLXContext ctx) |
| 327 | { |
| 328 | glXDestroyContext(dpy, ctx); |
| 329 | } |
| 330 | |
| 331 | |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 332 | /* This is only used by CreatePbuffer() */ |
| 333 | static int XErrorFlag = 0; |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 334 | static int HandleXError(Display *dpy, XErrorEvent *event) |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 335 | { |
| 336 | XErrorFlag = 1; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 341 | /** |
Brian Paul | 18a9490 | 2004-02-02 15:38:26 +0000 | [diff] [blame] | 342 | * Create a Pbuffer. Use an X error handler to deal with potential |
| 343 | * BadAlloc errors. |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 344 | * |
| 345 | * Input: dpy - the X display |
| 346 | * fbConfig - an FBConfig as returned by glXChooseFBConfigSGIX(). |
| 347 | * width, height - size of pixel buffer to request, in pixels. |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 348 | * pbAttribs - list of optional pixel buffer attributes |
| 349 | * Return: a Pbuffer or None. |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 350 | */ |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 351 | PBUFFER |
| 352 | CreatePbuffer(Display *dpy, int screen, FBCONFIG config, |
| 353 | int width, int height, Bool largest, Bool preserve) |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 354 | { |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 355 | int (*oldHandler)(Display *, XErrorEvent *); |
| 356 | PBUFFER pBuffer = None; |
| 357 | int pbSupport = QueryPbuffers(dpy, screen); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 358 | |
| 359 | /* Catch X protocol errors with our own error handler */ |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 360 | oldHandler = XSetErrorHandler(HandleXError); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 361 | XErrorFlag = 0; |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 362 | |
| 363 | #if defined(GLX_VERSION_1_3) |
| 364 | if (pbSupport == 1) { |
| 365 | /* GLX 1.3 */ |
| 366 | int attribs[100], i = 0; |
| 367 | attribs[i++] = GLX_PBUFFER_WIDTH; |
| 368 | attribs[i++] = width; |
| 369 | attribs[i++] = GLX_PBUFFER_HEIGHT; |
| 370 | attribs[i++] = height; |
| 371 | attribs[i++] = GLX_PRESERVED_CONTENTS; |
| 372 | attribs[i++] = preserve; |
| 373 | attribs[i++] = GLX_LARGEST_PBUFFER; |
| 374 | attribs[i++] = largest; |
| 375 | attribs[i++] = 0; |
| 376 | pBuffer = glXCreatePbuffer(dpy, config, attribs); |
| 377 | } |
| 378 | else |
| 379 | #endif |
| 380 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 381 | if (pbSupport == 2) { |
| 382 | int attribs[100], i = 0; |
| 383 | attribs[i++] = GLX_PRESERVED_CONTENTS; |
| 384 | attribs[i++] = preserve; |
| 385 | attribs[i++] = GLX_LARGEST_PBUFFER; |
| 386 | attribs[i++] = largest; |
| 387 | attribs[i++] = 0; |
| 388 | pBuffer = glXCreateGLXPbufferSGIX(dpy, config, width, height, attribs); |
| 389 | } |
| 390 | else |
| 391 | #endif |
| 392 | { |
| 393 | pBuffer = None; |
| 394 | } |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 395 | |
Kristian Høgsberg | c095f7e | 2007-08-30 13:00:20 -0400 | [diff] [blame^] | 396 | XSync(dpy, False); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 397 | /* Restore original X error handler */ |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 398 | (void) XSetErrorHandler(oldHandler); |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 399 | |
| 400 | /* Return pbuffer (may be None) */ |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 401 | if (!XErrorFlag && pBuffer != None) { |
Brian Paul | 21666e3 | 2002-10-05 18:30:13 +0000 | [diff] [blame] | 402 | /*printf("config %d worked!\n", i);*/ |
| 403 | return pBuffer; |
| 404 | } |
| 405 | else { |
| 406 | return None; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
Brian Paul | f72e442 | 2005-01-04 00:58:29 +0000 | [diff] [blame] | 411 | void |
| 412 | DestroyPbuffer(Display *dpy, int screen, PBUFFER pbuffer) |
| 413 | { |
| 414 | int pbSupport = QueryPbuffers(dpy, screen); |
| 415 | #if defined(GLX_VERSION_1_3) |
| 416 | if (pbSupport == 1) { |
| 417 | glXDestroyPbuffer(dpy, pbuffer); |
| 418 | return; |
| 419 | } |
| 420 | #endif |
| 421 | #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer) |
| 422 | if (pbSupport == 2) { |
| 423 | glXDestroyGLXPbufferSGIX(dpy, pbuffer); |
| 424 | return; |
| 425 | } |
| 426 | #endif |
| 427 | } |