Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / glx / glxconfig.c
1 /*
2 * (C) Copyright IBM Corporation 2003
3 * All Rights Reserved.
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file glxconfig.c
27 * Utility routines for working with \c struct glx_config structures. At
28 * some point most or all of these functions will be moved to the Mesa
29 * code base.
30 *
31 * \author Ian Romanick <idr@us.ibm.com>
32 */
33
34 #include <GL/glx.h>
35 #include "GL/glxint.h"
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "glxconfig.h"
40
41 #define NUM_VISUAL_TYPES 6
42
43 /**
44 * Get data from a GLX config
45 *
46 * \param mode GL context mode whose data is to be returned.
47 * \param attribute Attribute of \c mode that is to be returned.
48 * \param value_return Location to store the data member of \c mode.
49 * \return If \c attribute is a valid attribute of \c mode, zero is
50 * returned. Otherwise \c GLX_BAD_ATTRIBUTE is returned.
51 */
52 _X_HIDDEN int
53 glx_config_get(struct glx_config * mode, int attribute, int *value_return)
54 {
55 switch (attribute) {
56 case GLX_USE_GL:
57 *value_return = GL_TRUE;
58 return 0;
59 case GLX_BUFFER_SIZE:
60 *value_return = mode->rgbBits;
61 return 0;
62 case GLX_RGBA:
63 *value_return = mode->rgbMode;
64 return 0;
65 case GLX_RED_SIZE:
66 *value_return = mode->redBits;
67 return 0;
68 case GLX_GREEN_SIZE:
69 *value_return = mode->greenBits;
70 return 0;
71 case GLX_BLUE_SIZE:
72 *value_return = mode->blueBits;
73 return 0;
74 case GLX_ALPHA_SIZE:
75 *value_return = mode->alphaBits;
76 return 0;
77 case GLX_DOUBLEBUFFER:
78 *value_return = mode->doubleBufferMode;
79 return 0;
80 case GLX_STEREO:
81 *value_return = mode->stereoMode;
82 return 0;
83 case GLX_AUX_BUFFERS:
84 *value_return = mode->numAuxBuffers;
85 return 0;
86 case GLX_DEPTH_SIZE:
87 *value_return = mode->depthBits;
88 return 0;
89 case GLX_STENCIL_SIZE:
90 *value_return = mode->stencilBits;
91 return 0;
92 case GLX_ACCUM_RED_SIZE:
93 *value_return = mode->accumRedBits;
94 return 0;
95 case GLX_ACCUM_GREEN_SIZE:
96 *value_return = mode->accumGreenBits;
97 return 0;
98 case GLX_ACCUM_BLUE_SIZE:
99 *value_return = mode->accumBlueBits;
100 return 0;
101 case GLX_ACCUM_ALPHA_SIZE:
102 *value_return = mode->accumAlphaBits;
103 return 0;
104 case GLX_LEVEL:
105 *value_return = mode->level;
106 return 0;
107 #ifndef GLX_USE_APPLEGL /* This isn't supported by CGL. */
108 case GLX_TRANSPARENT_TYPE_EXT:
109 *value_return = mode->transparentPixel;
110 return 0;
111 #endif
112 case GLX_TRANSPARENT_RED_VALUE:
113 *value_return = mode->transparentRed;
114 return 0;
115 case GLX_TRANSPARENT_GREEN_VALUE:
116 *value_return = mode->transparentGreen;
117 return 0;
118 case GLX_TRANSPARENT_BLUE_VALUE:
119 *value_return = mode->transparentBlue;
120 return 0;
121 case GLX_TRANSPARENT_ALPHA_VALUE:
122 *value_return = mode->transparentAlpha;
123 return 0;
124 case GLX_TRANSPARENT_INDEX_VALUE:
125 *value_return = mode->transparentIndex;
126 return 0;
127 case GLX_X_VISUAL_TYPE:
128 *value_return = mode->visualType;
129 return 0;
130 case GLX_CONFIG_CAVEAT:
131 *value_return = mode->visualRating;
132 return 0;
133 case GLX_VISUAL_ID:
134 *value_return = mode->visualID;
135 return 0;
136 case GLX_DRAWABLE_TYPE:
137 *value_return = mode->drawableType;
138 return 0;
139 case GLX_RENDER_TYPE:
140 *value_return = mode->renderType;
141 return 0;
142 case GLX_X_RENDERABLE:
143 *value_return = mode->xRenderable;
144 return 0;
145 case GLX_FBCONFIG_ID:
146 *value_return = mode->fbconfigID;
147 return 0;
148 case GLX_MAX_PBUFFER_WIDTH:
149 *value_return = mode->maxPbufferWidth;
150 return 0;
151 case GLX_MAX_PBUFFER_HEIGHT:
152 *value_return = mode->maxPbufferHeight;
153 return 0;
154 case GLX_MAX_PBUFFER_PIXELS:
155 *value_return = mode->maxPbufferPixels;
156 return 0;
157 #ifndef GLX_USE_APPLEGL /* These aren't supported by CGL. */
158 case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
159 *value_return = mode->optimalPbufferWidth;
160 return 0;
161 case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
162 *value_return = mode->optimalPbufferHeight;
163 return 0;
164 case GLX_SWAP_METHOD_OML:
165 *value_return = mode->swapMethod;
166 return 0;
167 #endif
168 case GLX_SAMPLE_BUFFERS_SGIS:
169 *value_return = mode->sampleBuffers;
170 return 0;
171 case GLX_SAMPLES_SGIS:
172 *value_return = mode->samples;
173 return 0;
174 case GLX_BIND_TO_TEXTURE_RGB_EXT:
175 *value_return = mode->bindToTextureRgb;
176 return 0;
177 case GLX_BIND_TO_TEXTURE_RGBA_EXT:
178 *value_return = mode->bindToTextureRgba;
179 return 0;
180 case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
181 *value_return = mode->bindToMipmapTexture == GL_TRUE ? GL_TRUE :
182 GL_FALSE;
183 return 0;
184 case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
185 *value_return = mode->bindToTextureTargets;
186 return 0;
187 case GLX_Y_INVERTED_EXT:
188 *value_return = mode->yInverted;
189 return 0;
190
191 /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
192 * It is ONLY for communication between the GLX client and the GLX
193 * server.
194 */
195 case GLX_VISUAL_SELECT_GROUP_SGIX:
196 default:
197 return GLX_BAD_ATTRIBUTE;
198 }
199 }
200
201
202 /**
203 * Allocate a linked list of \c struct glx_config structures. The fields of
204 * each structure will be initialized to "reasonable" default values. In
205 * most cases this is the default value defined by table 3.4 of the GLX
206 * 1.3 specification. This means that most values are either initialized to
207 * zero or \c GLX_DONT_CARE (which is -1). As support for additional
208 * extensions is added, the new values will be initialized to appropriate
209 * values from the extension specification.
210 *
211 * \param count Number of structures to allocate.
212 * \param minimum_size Minimum size of a structure to allocate. This allows
213 * for differences in the version of the
214 * \c struct glx_config stucture used in libGL and in a
215 * DRI-based driver.
216 * \returns A pointer to the first element in a linked list of \c count
217 * stuctures on success, or \c NULL on failure.
218 */
219 _X_HIDDEN struct glx_config *
220 glx_config_create_list(unsigned count)
221 {
222 const size_t size = sizeof(struct glx_config);
223 struct glx_config *base = NULL;
224 struct glx_config **next;
225 unsigned i;
226
227 next = &base;
228 for (i = 0; i < count; i++) {
229 *next = (struct glx_config *) malloc(size);
230 if (*next == NULL) {
231 glx_config_destroy_list(base);
232 base = NULL;
233 break;
234 }
235
236 (void) memset(*next, 0, size);
237 (*next)->visualID = GLX_DONT_CARE;
238 (*next)->visualType = GLX_DONT_CARE;
239 (*next)->visualRating = GLX_NONE;
240 (*next)->transparentPixel = GLX_NONE;
241 (*next)->transparentRed = GLX_DONT_CARE;
242 (*next)->transparentGreen = GLX_DONT_CARE;
243 (*next)->transparentBlue = GLX_DONT_CARE;
244 (*next)->transparentAlpha = GLX_DONT_CARE;
245 (*next)->transparentIndex = GLX_DONT_CARE;
246 (*next)->xRenderable = GLX_DONT_CARE;
247 (*next)->fbconfigID = GLX_DONT_CARE;
248 (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
249 (*next)->bindToTextureRgb = GLX_DONT_CARE;
250 (*next)->bindToTextureRgba = GLX_DONT_CARE;
251 (*next)->bindToMipmapTexture = GLX_DONT_CARE;
252 (*next)->bindToTextureTargets = GLX_DONT_CARE;
253 (*next)->yInverted = GLX_DONT_CARE;
254
255 next = &((*next)->next);
256 }
257
258 return base;
259 }
260
261 _X_HIDDEN void
262 glx_config_destroy_list(struct glx_config *configs)
263 {
264 while (configs != NULL) {
265 struct glx_config *const next = configs->next;
266
267 free(configs);
268 configs = next;
269 }
270 }
271
272
273 /**
274 * Find a context mode matching a Visual ID.
275 *
276 * \param modes List list of context-mode structures to be searched.
277 * \param vid Visual ID to be found.
278 * \returns A pointer to a context-mode in \c modes if \c vid was found in
279 * the list, or \c NULL if it was not.
280 */
281
282 _X_HIDDEN struct glx_config *
283 glx_config_find_visual(struct glx_config *configs, int vid)
284 {
285 struct glx_config *c;
286
287 for (c = configs; c != NULL; c = c->next)
288 if (c->visualID == vid)
289 return c;
290
291 return NULL;
292 }
293
294 _X_HIDDEN struct glx_config *
295 glx_config_find_fbconfig(struct glx_config *configs, int fbid)
296 {
297 struct glx_config *c;
298
299 for (c = configs; c != NULL; c = c->next)
300 if (c->fbconfigID == fbid)
301 return c;
302
303 return NULL;
304 }