Removed all RCS / CVS tags (Id, Header, Date, etc.) from everything.
[mesa.git] / progs / xdemos / pbutil.c
1
2 /*
3 * OpenGL pbuffers utility functions.
4 *
5 * Brian Paul
6 * April 1997
7 * Updated on 5 October 2002
8 */
9
10
11 #include <stdio.h>
12 #include <string.h>
13 #include "pbutil.h"
14
15
16
17 /*
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.
22 * 1 = pixel buffers are available.
23 */
24 int
25 QueryPbuffers(Display *dpy, int screen)
26 {
27 #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
28 char *extensions;
29
30 extensions = (char *) glXQueryServerString(dpy, screen, GLX_EXTENSIONS);
31 if (!strstr(extensions,"GLX_SGIX_fbconfig")) {
32 return 0;
33 }
34 if (!strstr(extensions,"GLX_SGIX_pbuffer")) {
35 return 0;
36 }
37 return 1;
38 #else
39 return 0;
40 #endif
41 }
42
43
44
45 #ifdef GLX_SGIX_fbconfig
46
47
48 /*
49 * Print parameters for a GLXFBConfig to stdout.
50 * Input: dpy - the X display
51 * fbConfig - the fbconfig handle
52 * horizFormat - if true, print in horizontal format
53 */
54 void
55 PrintFBConfigInfo(Display *dpy, GLXFBConfigSGIX fbConfig, Bool horizFormat)
56 {
57 int pbAttribs[] = {GLX_LARGEST_PBUFFER_SGIX, True,
58 GLX_PRESERVED_CONTENTS_SGIX, False,
59 None};
60 GLXPbufferSGIX pBuffer;
61 int width=2, height=2;
62 int bufferSize, level, doubleBuffer, stereo, auxBuffers;
63 int redSize, greenSize, blueSize, alphaSize;
64 int depthSize, stencilSize;
65 int accumRedSize, accumBlueSize, accumGreenSize, accumAlphaSize;
66 int sampleBuffers, samples;
67 int drawableType, renderType, xRenderable, xVisual, id;
68 int maxWidth, maxHeight, maxPixels;
69 int optWidth, optHeight;
70
71 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_BUFFER_SIZE, &bufferSize);
72 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_LEVEL, &level);
73 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DOUBLEBUFFER, &doubleBuffer);
74 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_STEREO, &stereo);
75 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_AUX_BUFFERS, &auxBuffers);
76 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_RED_SIZE, &redSize);
77 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_GREEN_SIZE, &greenSize);
78 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_BLUE_SIZE, &blueSize);
79 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ALPHA_SIZE, &alphaSize);
80 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DEPTH_SIZE, &depthSize);
81 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_STENCIL_SIZE, &stencilSize);
82 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_RED_SIZE, &accumRedSize);
83 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_GREEN_SIZE, &accumGreenSize);
84 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_BLUE_SIZE, &accumBlueSize);
85 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_ACCUM_ALPHA_SIZE, &accumAlphaSize);
86 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_SAMPLE_BUFFERS_SGIS, &sampleBuffers);
87 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_SAMPLES_SGIS, &samples);
88 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_DRAWABLE_TYPE_SGIX, &drawableType);
89 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_RENDER_TYPE_SGIX, &renderType);
90 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_X_RENDERABLE_SGIX, &xRenderable);
91 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_X_VISUAL_TYPE_EXT, &xVisual);
92 if (!xRenderable || !(drawableType & GLX_WINDOW_BIT_SGIX))
93 xVisual = -1;
94 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_FBCONFIG_ID_SGIX, &id);
95
96 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_WIDTH_SGIX,
97 &maxWidth);
98 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_HEIGHT_SGIX,
99 &maxHeight);
100 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_MAX_PBUFFER_PIXELS_SGIX,
101 &maxPixels);
102 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_OPTIMAL_PBUFFER_WIDTH_SGIX,
103 &optWidth);
104 glXGetFBConfigAttribSGIX(dpy, fbConfig, GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX,
105 &optHeight);
106
107 pBuffer = CreatePbuffer(dpy, fbConfig, width, height, pbAttribs);
108
109 if (horizFormat) {
110 printf("0x%03x ", id);
111 if (xVisual==GLX_STATIC_GRAY) printf("StaticGray ");
112 else if (xVisual==GLX_GRAY_SCALE) printf("GrayScale ");
113 else if (xVisual==GLX_STATIC_COLOR) printf("StaticColor ");
114 else if (xVisual==GLX_PSEUDO_COLOR) printf("PseudoColor ");
115 else if (xVisual==GLX_TRUE_COLOR) printf("TrueColor ");
116 else if (xVisual==GLX_DIRECT_COLOR) printf("DirectColor ");
117 else printf(" -none- ");
118 printf(" %3d %3d %s %s %s %2s ", bufferSize, level,
119 (renderType & GLX_RGBA_BIT_SGIX) ? "y" : "n",
120 (renderType & GLX_COLOR_INDEX_BIT_SGIX) ? "y" : "n",
121 doubleBuffer ? "y" : "n",
122 stereo ? "y" : "n");
123 printf("%2d %2d %2d %2d ", redSize, greenSize, blueSize, alphaSize);
124 printf("%2d %2d ", depthSize, stencilSize);
125 printf("%2d %2d %2d %2d", accumRedSize, accumGreenSize, accumBlueSize,
126 accumAlphaSize);
127 printf(" %2d %2d", sampleBuffers, samples);
128 printf(" %s", pBuffer ? "y" : "n");
129 printf("\n");
130 }
131 else {
132 printf("Id 0x%x\n", id);
133 printf(" Buffer Size: %d\n", bufferSize);
134 printf(" Level: %d\n", level);
135 printf(" Double Buffer: %s\n", doubleBuffer ? "yes" : "no");
136 printf(" Stereo: %s\n", stereo ? "yes" : "no");
137 printf(" Aux Buffers: %d\n", auxBuffers);
138 printf(" Red Size: %d\n", redSize);
139 printf(" Green Size: %d\n", greenSize);
140 printf(" Blue Size: %d\n", blueSize);
141 printf(" Alpha Size: %d\n", alphaSize);
142 printf(" Depth Size: %d\n", depthSize);
143 printf(" Stencil Size: %d\n", stencilSize);
144 printf(" Accum Red Size: %d\n", accumRedSize);
145 printf(" Accum Green Size: %d\n", accumGreenSize);
146 printf(" Accum Blue Size: %d\n", accumBlueSize);
147 printf(" Accum Alpha Size: %d\n", accumAlphaSize);
148 printf(" Sample Buffers: %d\n", sampleBuffers);
149 printf(" Samples/Pixel: %d\n", samples);
150 printf(" Drawable Types: ");
151 if (drawableType & GLX_WINDOW_BIT_SGIX) printf("Window ");
152 if (drawableType & GLX_PIXMAP_BIT_SGIX) printf("Pixmap ");
153 if (drawableType & GLX_PBUFFER_BIT_SGIX) printf("PBuffer");
154 printf("\n");
155 printf(" Render Types: ");
156 if (renderType & GLX_RGBA_BIT_SGIX) printf("RGBA ");
157 if (renderType & GLX_COLOR_INDEX_BIT_SGIX) printf("CI ");
158 printf("\n");
159 printf(" X Renderable: %s\n", xRenderable ? "yes" : "no");
160 /*
161 printf(" Max width: %d\n", maxWidth);
162 printf(" Max height: %d\n", maxHeight);
163 printf(" Max pixels: %d\n", maxPixels);
164 printf(" Optimum width: %d\n", optWidth);
165 printf(" Optimum height: %d\n", optHeight);
166 */
167 printf(" Pbuffer: %s\n", pBuffer ? "yes" : "no");
168 }
169
170 if (pBuffer) {
171 glXDestroyGLXPbufferSGIX(dpy, pBuffer);
172 }
173 }
174
175
176
177 /* This is only used by CreatePbuffer() */
178 static int XErrorFlag = 0;
179 static int HandleXError( Display *dpy, XErrorEvent *event )
180 {
181 XErrorFlag = 1;
182 return 0;
183 }
184
185
186 /*
187 * Create a pixel buffer. We loop over the list of fbconfigs trying to create
188 * a pixel buffer. We return the first pixel buffer which we successfully
189 * create. This function hides the ugliness of dealing with BadAlloc X
190 * protocol errors.
191 *
192 * Input: dpy - the X display
193 * fbConfig - an FBConfig as returned by glXChooseFBConfigSGIX().
194 * width, height - size of pixel buffer to request, in pixels.
195 * pbAttribs - list of pixel buffer attributes as used by
196 * glXCreateGLXPbufferSGIX().
197 * Return: a pixel buffer or None.
198 */
199 GLXPbufferSGIX
200 CreatePbuffer( Display *dpy, GLXFBConfigSGIX fbConfig,
201 int width, int height, int *pbAttribs )
202 {
203 int (*oldHandler)( Display *, XErrorEvent * );
204 GLXPbufferSGIX pBuffer = None;
205
206 /* Catch X protocol errors with our own error handler */
207 oldHandler = XSetErrorHandler( HandleXError );
208
209 XErrorFlag = 0;
210 pBuffer = glXCreateGLXPbufferSGIX(dpy, fbConfig, width, height, pbAttribs);
211
212 /* Restore original X error handler */
213 (void) XSetErrorHandler( oldHandler );
214
215 /* Return pbuffer (may be None) */
216 if (!XErrorFlag && pBuffer!=None) {
217 /*printf("config %d worked!\n", i);*/
218 return pBuffer;
219 }
220 else {
221 return None;
222 }
223 }
224
225
226
227 #endif /*GLX_SGIX_fbconfig*/
228
229