xref: /third_party/mesa3d/src/glx/glxconfig.c (revision bf215546)
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 <stdlib.h>
36#include <string.h>
37
38#include "glxconfig.h"
39
40#define NUM_VISUAL_TYPES   6
41
42/**
43 * Get data from a GLX config
44 *
45 * \param mode         GL context mode whose data is to be returned.
46 * \param attribute    Attribute of \c mode that is to be returned.
47 * \param value_return Location to store the data member of \c mode.
48 * \return  If \c attribute is a valid attribute of \c mode, zero is
49 *          returned.  Otherwise \c GLX_BAD_ATTRIBUTE is returned.
50 */
51_X_HIDDEN int
52glx_config_get(struct glx_config * mode, int attribute, int *value_return)
53{
54   switch (attribute) {
55   case GLX_USE_GL:
56      *value_return = GL_TRUE;
57      return 0;
58   case GLX_BUFFER_SIZE:
59      *value_return = mode->rgbBits;
60      return 0;
61   case GLX_RGBA:
62      *value_return = !(mode->renderType & GLX_COLOR_INDEX_BIT);
63      return 0;
64   case GLX_RED_SIZE:
65      *value_return = mode->redBits;
66      return 0;
67   case GLX_GREEN_SIZE:
68      *value_return = mode->greenBits;
69      return 0;
70   case GLX_BLUE_SIZE:
71      *value_return = mode->blueBits;
72      return 0;
73   case GLX_ALPHA_SIZE:
74      *value_return = mode->alphaBits;
75      return 0;
76   case GLX_DOUBLEBUFFER:
77      *value_return = mode->doubleBufferMode;
78      return 0;
79   case GLX_STEREO:
80      *value_return = mode->stereoMode;
81      return 0;
82   case GLX_AUX_BUFFERS:
83      *value_return = mode->numAuxBuffers;
84      return 0;
85   case GLX_DEPTH_SIZE:
86      *value_return = mode->depthBits;
87      return 0;
88   case GLX_STENCIL_SIZE:
89      *value_return = mode->stencilBits;
90      return 0;
91   case GLX_ACCUM_RED_SIZE:
92      *value_return = mode->accumRedBits;
93      return 0;
94   case GLX_ACCUM_GREEN_SIZE:
95      *value_return = mode->accumGreenBits;
96      return 0;
97   case GLX_ACCUM_BLUE_SIZE:
98      *value_return = mode->accumBlueBits;
99      return 0;
100   case GLX_ACCUM_ALPHA_SIZE:
101      *value_return = mode->accumAlphaBits;
102      return 0;
103   case GLX_LEVEL:
104      *value_return = mode->level;
105      return 0;
106#ifndef GLX_USE_APPLEGL               /* This isn't supported by CGL. */
107   case GLX_TRANSPARENT_TYPE_EXT:
108      *value_return = mode->transparentPixel;
109      return 0;
110#endif
111   case GLX_TRANSPARENT_RED_VALUE:
112      *value_return = mode->transparentRed;
113      return 0;
114   case GLX_TRANSPARENT_GREEN_VALUE:
115      *value_return = mode->transparentGreen;
116      return 0;
117   case GLX_TRANSPARENT_BLUE_VALUE:
118      *value_return = mode->transparentBlue;
119      return 0;
120   case GLX_TRANSPARENT_ALPHA_VALUE:
121      *value_return = mode->transparentAlpha;
122      return 0;
123   case GLX_TRANSPARENT_INDEX_VALUE:
124      *value_return = mode->transparentIndex;
125      return 0;
126   case GLX_X_VISUAL_TYPE:
127      *value_return = mode->visualType;
128      return 0;
129   case GLX_CONFIG_CAVEAT:
130      *value_return = mode->visualRating;
131      return 0;
132   case GLX_VISUAL_ID:
133      *value_return = mode->visualID;
134      return 0;
135   case GLX_DRAWABLE_TYPE:
136      *value_return = mode->drawableType;
137      return 0;
138   case GLX_RENDER_TYPE:
139      *value_return = mode->renderType;
140      return 0;
141   case GLX_X_RENDERABLE:
142      *value_return = mode->xRenderable;
143      return 0;
144   case GLX_FBCONFIG_ID:
145      *value_return = mode->fbconfigID;
146      return 0;
147   case GLX_MAX_PBUFFER_WIDTH:
148      *value_return = 4096; /* _EGL_MAX_PBUFFER_WIDTH */
149      return 0;
150   case GLX_MAX_PBUFFER_HEIGHT:
151      *value_return = 4096; /* _EGL_MAX_PBUFFER_HEIGHT */
152      return 0;
153   case GLX_MAX_PBUFFER_PIXELS:
154      *value_return = mode->maxPbufferPixels;
155      return 0;
156#ifndef GLX_USE_APPLEGL               /* These aren't supported by CGL. */
157   case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
158      *value_return = mode->optimalPbufferWidth;
159      return 0;
160   case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
161      *value_return = mode->optimalPbufferHeight;
162      return 0;
163   case GLX_SWAP_METHOD_OML:
164      *value_return = mode->swapMethod;
165      return 0;
166#endif
167   case GLX_SAMPLE_BUFFERS_SGIS:
168      *value_return = mode->sampleBuffers;
169      return 0;
170   case GLX_SAMPLES_SGIS:
171      *value_return = mode->samples;
172      return 0;
173   case GLX_BIND_TO_TEXTURE_RGB_EXT:
174      *value_return = mode->bindToTextureRgb;
175      return 0;
176   case GLX_BIND_TO_TEXTURE_RGBA_EXT:
177      *value_return = mode->bindToTextureRgba;
178      return 0;
179   case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
180      *value_return = mode->bindToMipmapTexture == GL_TRUE ? GL_TRUE :
181         GL_FALSE;
182      return 0;
183   case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
184      *value_return = mode->bindToTextureTargets;
185      return 0;
186   case GLX_Y_INVERTED_EXT:
187      *value_return = mode->yInverted;
188      return 0;
189   case GLX_FRAMEBUFFER_SRGB_CAPABLE_EXT:
190      *value_return = mode->sRGBCapable;
191      return 0;
192   case GLX_FLOAT_COMPONENTS_NV:
193      *value_return = mode->floatComponentsNV;
194      return 0;
195
196      /* Applications are NOT allowed to query GLX_VISUAL_SELECT_GROUP_SGIX.
197       * It is ONLY for communication between the GLX client and the GLX
198       * server.
199       */
200   case GLX_VISUAL_SELECT_GROUP_SGIX:
201   default:
202      return GLX_BAD_ATTRIBUTE;
203   }
204}
205
206
207/**
208 * Allocate a linked list of \c struct glx_config structures.  The fields of
209 * each structure will be initialized to "reasonable" default values.  In
210 * most cases this is the default value defined by table 3.4 of the GLX
211 * 1.3 specification.  This means that most values are either initialized to
212 * zero or \c GLX_DONT_CARE (which is -1).  As support for additional
213 * extensions is added, the new values will be initialized to appropriate
214 * values from the extension specification.
215 *
216 * \param count         Number of structures to allocate.
217 * \returns A pointer to the first element in a linked list of \c count
218 *          stuctures on success, or \c NULL on failure.
219 */
220_X_HIDDEN struct glx_config *
221glx_config_create_list(unsigned count)
222{
223   const size_t size = sizeof(struct glx_config);
224   struct glx_config *base = NULL;
225   struct glx_config **next;
226   unsigned i;
227
228   next = &base;
229   for (i = 0; i < count; i++) {
230      *next = calloc(1, size);
231      if (*next == NULL) {
232	 glx_config_destroy_list(base);
233	 base = NULL;
234	 break;
235      }
236
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      (*next)->sRGBCapable = GL_FALSE;
255
256      next = &((*next)->next);
257   }
258
259   return base;
260}
261
262_X_HIDDEN void
263glx_config_destroy_list(struct glx_config *configs)
264{
265   while (configs != NULL) {
266      struct glx_config *const next = configs->next;
267
268      free(configs);
269      configs = next;
270   }
271}
272
273
274/**
275 * Find a context mode matching a Visual ID.
276 *
277 * \param modes  List list of context-mode structures to be searched.
278 * \param vid    Visual ID to be found.
279 * \returns A pointer to a context-mode in \c modes if \c vid was found in
280 *          the list, or \c NULL if it was not.
281 */
282
283_X_HIDDEN struct glx_config *
284glx_config_find_visual(struct glx_config *configs, int vid)
285{
286   struct glx_config *c;
287
288   for (c = configs; c != NULL; c = c->next)
289      if (c->visualID == vid)
290	 return c;
291
292   return NULL;
293}
294
295_X_HIDDEN struct glx_config *
296glx_config_find_fbconfig(struct glx_config *configs, int fbid)
297{
298   struct glx_config *c;
299
300   for (c = configs; c != NULL; c = c->next)
301      if (c->fbconfigID == fbid)
302	 return c;
303
304   return NULL;
305}
306