Imported Upstream version 1.15.1
[deb_xorg-server.git] / glx / glxscreens.c
CommitLineData
a09e091a
JB
1/*
2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. 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 * 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 including the dates of first publication and
13 * either this permission notice or a reference to
14 * http://oss.sgi.com/projects/FreeB/
15 * shall be included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Except as contained in this notice, the name of Silicon Graphics, Inc.
26 * shall not be used in advertising or otherwise to promote the sale, use or
27 * other dealings in this Software without prior written authorization from
28 * Silicon Graphics, Inc.
29 */
30
31#ifdef HAVE_DIX_CONFIG_H
32#include <dix-config.h>
33#endif
34
35#include <GL/glxtokens.h>
36#include <string.h>
37#include <windowstr.h>
38#include <os.h>
39#include <colormapst.h>
40
41#include "privates.h"
42#include "glxserver.h"
43#include "glxutil.h"
44#include "glxext.h"
45#include "protocol-versions.h"
46
47static DevPrivateKeyRec glxScreenPrivateKeyRec;
48
49#define glxScreenPrivateKey (&glxScreenPrivateKeyRec)
50
51const char GLServerVersion[] = "1.4";
52static const char GLServerExtensions[] =
53 "GL_ARB_depth_texture "
54 "GL_ARB_draw_buffers "
55 "GL_ARB_fragment_program "
56 "GL_ARB_fragment_program_shadow "
57 "GL_ARB_imaging "
58 "GL_ARB_multisample "
59 "GL_ARB_multitexture "
60 "GL_ARB_occlusion_query "
61 "GL_ARB_point_parameters "
62 "GL_ARB_point_sprite "
63 "GL_ARB_shadow "
64 "GL_ARB_shadow_ambient "
65 "GL_ARB_texture_border_clamp "
66 "GL_ARB_texture_compression "
67 "GL_ARB_texture_cube_map "
68 "GL_ARB_texture_env_add "
69 "GL_ARB_texture_env_combine "
70 "GL_ARB_texture_env_crossbar "
71 "GL_ARB_texture_env_dot3 "
72 "GL_ARB_texture_mirrored_repeat "
73 "GL_ARB_texture_non_power_of_two "
74 "GL_ARB_transpose_matrix "
75 "GL_ARB_vertex_program "
76 "GL_ARB_window_pos "
77 "GL_EXT_abgr "
78 "GL_EXT_bgra "
79 "GL_EXT_blend_color "
80 "GL_EXT_blend_equation_separate "
81 "GL_EXT_blend_func_separate "
82 "GL_EXT_blend_logic_op "
83 "GL_EXT_blend_minmax "
84 "GL_EXT_blend_subtract "
85 "GL_EXT_clip_volume_hint "
86 "GL_EXT_copy_texture "
87 "GL_EXT_draw_range_elements "
88 "GL_EXT_fog_coord "
89 "GL_EXT_framebuffer_object "
90 "GL_EXT_multi_draw_arrays "
91 "GL_EXT_packed_pixels "
92 "GL_EXT_paletted_texture "
93 "GL_EXT_point_parameters "
94 "GL_EXT_polygon_offset "
95 "GL_EXT_rescale_normal "
96 "GL_EXT_secondary_color "
97 "GL_EXT_separate_specular_color "
98 "GL_EXT_shadow_funcs "
99 "GL_EXT_shared_texture_palette "
100 "GL_EXT_stencil_two_side "
101 "GL_EXT_stencil_wrap "
102 "GL_EXT_subtexture "
103 "GL_EXT_texture "
104 "GL_EXT_texture3D "
105 "GL_EXT_texture_compression_dxt1 "
106 "GL_EXT_texture_compression_s3tc "
107 "GL_EXT_texture_edge_clamp "
108 "GL_EXT_texture_env_add "
109 "GL_EXT_texture_env_combine "
110 "GL_EXT_texture_env_dot3 "
111 "GL_EXT_texture_filter_anisotropic "
112 "GL_EXT_texture_lod "
113 "GL_EXT_texture_lod_bias "
114 "GL_EXT_texture_mirror_clamp "
115 "GL_EXT_texture_object "
116 "GL_EXT_texture_rectangle "
117 "GL_EXT_vertex_array "
118 "GL_3DFX_texture_compression_FXT1 "
119 "GL_APPLE_packed_pixels "
120 "GL_ATI_draw_buffers "
121 "GL_ATI_texture_env_combine3 "
122 "GL_ATI_texture_mirror_once "
123 "GL_HP_occlusion_test "
124 "GL_IBM_texture_mirrored_repeat "
125 "GL_INGR_blend_func_separate "
126 "GL_MESA_pack_invert "
127 "GL_MESA_ycbcr_texture "
128 "GL_NV_blend_square "
129 "GL_NV_depth_clamp "
130 "GL_NV_fog_distance "
131 "GL_NV_fragment_program_option "
132 "GL_NV_fragment_program2 "
133 "GL_NV_light_max_exponent "
134 "GL_NV_multisample_filter_hint "
135 "GL_NV_point_sprite "
136 "GL_NV_texgen_reflection "
137 "GL_NV_texture_compression_vtc "
138 "GL_NV_texture_env_combine4 "
139 "GL_NV_texture_expand_normal "
140 "GL_NV_texture_rectangle "
141 "GL_NV_vertex_program2_option "
142 "GL_NV_vertex_program3 "
143 "GL_OES_compressed_paletted_texture "
144 "GL_SGI_color_matrix "
145 "GL_SGI_color_table "
146 "GL_SGIS_generate_mipmap "
147 "GL_SGIS_multisample "
148 "GL_SGIS_point_parameters "
149 "GL_SGIS_texture_border_clamp "
150 "GL_SGIS_texture_edge_clamp "
151 "GL_SGIS_texture_lod "
152 "GL_SGIX_depth_texture "
153 "GL_SGIX_shadow "
154 "GL_SGIX_shadow_ambient "
155 "GL_SUN_slice_accum ";
156
157/*
158** We have made the simplifying assuption that the same extensions are
159** supported across all screens in a multi-screen system.
160*/
161unsigned glxMajorVersion = SERVER_GLX_MAJOR_VERSION;
162unsigned glxMinorVersion = SERVER_GLX_MINOR_VERSION;
163static char GLXServerExtensions[] =
164 "GLX_ARB_multisample "
165 "GLX_EXT_visual_info "
166 "GLX_EXT_visual_rating "
167 "GLX_EXT_import_context "
168 "GLX_EXT_texture_from_pixmap "
169 "GLX_OML_swap_method "
170 "GLX_SGI_make_current_read "
171#ifndef __APPLE__
172 "GLX_SGIS_multisample "
173#endif
174 "GLX_SGIX_fbconfig "
175 "GLX_SGIX_pbuffer "
176 "GLX_MESA_copy_sub_buffer ";
177
178static Bool
179glxCloseScreen(ScreenPtr pScreen)
180{
181 __GLXscreen *pGlxScreen = glxGetScreen(pScreen);
182
183 pScreen->CloseScreen = pGlxScreen->CloseScreen;
184
185 pGlxScreen->destroy(pGlxScreen);
186
187 return pScreen->CloseScreen(pScreen);
188}
189
190__GLXscreen *
191glxGetScreen(ScreenPtr pScreen)
192{
193 return dixLookupPrivate(&pScreen->devPrivates, glxScreenPrivateKey);
194}
195
196_X_EXPORT void
197GlxSetVisualConfigs(int nconfigs, void *configs, void **privates)
198{
199 /* We keep this stub around for the DDX drivers that still
200 * call it. */
201}
202
203GLint
204glxConvertToXVisualType(int visualType)
205{
206 static const int x_visual_types[] = {
207 TrueColor, DirectColor,
208 PseudoColor, StaticColor,
209 GrayScale, StaticGray
210 };
211
212 return ((unsigned) (visualType - GLX_TRUE_COLOR) < 6)
213 ? x_visual_types[visualType - GLX_TRUE_COLOR] : -1;
214}
215
216/* This code inspired by composite/compinit.c. We could move this to
217 * mi/ and share it with composite.*/
218
219static VisualPtr
220AddScreenVisuals(ScreenPtr pScreen, int count, int d)
221{
222 int i;
223 DepthPtr depth;
224
225 depth = NULL;
226 for (i = 0; i < pScreen->numDepths; i++) {
227 if (pScreen->allowedDepths[i].depth == d) {
228 depth = &pScreen->allowedDepths[i];
229 break;
230 }
231 }
232 if (depth == NULL)
233 return NULL;
234
235 if (ResizeVisualArray(pScreen, count, depth) == FALSE)
236 return NULL;
237
238 /* Return a pointer to the first of the added visuals. */
239 return pScreen->visuals + pScreen->numVisuals - count;
240}
241
242static int
243findFirstSet(unsigned int v)
244{
245 int i;
246
247 for (i = 0; i < 32; i++)
248 if (v & (1 << i))
249 return i;
250
251 return -1;
252}
253
254static void
255initGlxVisual(VisualPtr visual, __GLXconfig * config)
256{
257 int maxBits;
258
259 maxBits = max(config->redBits, max(config->greenBits, config->blueBits));
260
261 config->visualID = visual->vid;
262 visual->class = glxConvertToXVisualType(config->visualType);
263 visual->bitsPerRGBValue = maxBits;
264 visual->ColormapEntries = 1 << maxBits;
265 visual->nplanes = config->redBits + config->greenBits + config->blueBits;
266
267 visual->redMask = config->redMask;
268 visual->greenMask = config->greenMask;
269 visual->blueMask = config->blueMask;
270 visual->offsetRed = findFirstSet(config->redMask);
271 visual->offsetGreen = findFirstSet(config->greenMask);
272 visual->offsetBlue = findFirstSet(config->blueMask);
273}
274
275static __GLXconfig *
276pickFBConfig(__GLXscreen * pGlxScreen, VisualPtr visual)
277{
278 __GLXconfig *best = NULL, *config;
279 int best_score = 0;
280
281 for (config = pGlxScreen->fbconfigs; config != NULL; config = config->next) {
282 int score = 0;
283
284 if (config->redMask != visual->redMask ||
285 config->greenMask != visual->greenMask ||
286 config->blueMask != visual->blueMask)
287 continue;
288 if (config->visualRating != GLX_NONE)
289 continue;
290 /* Ignore multisampled configs */
291 if (config->sampleBuffers)
292 continue;
293 if (glxConvertToXVisualType(config->visualType) != visual->class)
294 continue;
295 /* If it's the 32-bit RGBA visual, demand a 32-bit fbconfig. */
296 if (visual->nplanes == 32 && config->rgbBits != 32)
297 continue;
298 /* Can't use the same FBconfig for multiple X visuals. I think. */
299 if (config->visualID != 0)
300 continue;
301
302 if (config->doubleBufferMode > 0)
303 score += 8;
304 if (config->depthBits > 0)
305 score += 4;
306 if (config->stencilBits > 0)
307 score += 2;
308 if (config->alphaBits > 0)
309 score++;
310
311 if (score > best_score) {
312 best = config;
313 best_score = score;
314 }
315 }
316
317 return best;
318}
319
320void
321__glXScreenInit(__GLXscreen * pGlxScreen, ScreenPtr pScreen)
322{
323 __GLXconfig *m;
324 __GLXconfig *config;
325 int i;
326
327 if (!dixRegisterPrivateKey(&glxScreenPrivateKeyRec, PRIVATE_SCREEN, 0))
328 return;
329
330 pGlxScreen->pScreen = pScreen;
331 pGlxScreen->GLextensions = strdup(GLServerExtensions);
332 pGlxScreen->GLXextensions = strdup(GLXServerExtensions);
333
334 /* All GLX providers must support all of the functionality required for at
335 * least GLX 1.2. If the provider supports a higher version, the GLXminor
336 * version can be changed in the provider's screen-probe routine. For
337 * most providers, the screen-probe routine is the caller of this
338 * function.
339 */
340 pGlxScreen->GLXmajor = 1;
341 pGlxScreen->GLXminor = 2;
342
343 pGlxScreen->CloseScreen = pScreen->CloseScreen;
344 pScreen->CloseScreen = glxCloseScreen;
345
346 i = 0;
347 for (m = pGlxScreen->fbconfigs; m != NULL; m = m->next) {
348 m->fbconfigID = FakeClientID(0);
349 m->visualID = 0;
350 i++;
351 }
352 pGlxScreen->numFBConfigs = i;
353
354 pGlxScreen->visuals =
355 calloc(pGlxScreen->numFBConfigs, sizeof(__GLXconfig *));
356
357 /* First, try to choose featureful FBconfigs for the existing X visuals.
358 * Note that if multiple X visuals end up with the same FBconfig being
359 * chosen, the later X visuals don't get GLX visuals (because we want to
360 * prioritize the root visual being GLX).
361 */
362 for (i = 0; i < pScreen->numVisuals; i++) {
363 VisualPtr visual = &pScreen->visuals[i];
364
365 config = pickFBConfig(pGlxScreen, visual);
366 if (config) {
367 pGlxScreen->visuals[pGlxScreen->numVisuals++] = config;
368 config->visualID = visual->vid;
369 }
370 }
371
372 /* Then, add new visuals corresponding to all FBconfigs that didn't have
373 * an existing, appropriate visual.
374 */
375 for (config = pGlxScreen->fbconfigs; config != NULL; config = config->next) {
376 int depth;
377
378 VisualPtr visual;
379
380 if (config->visualID != 0)
381 continue;
382
383 /* Only count RGB bits and not alpha, as we're not trying to create
384 * visuals for compositing (that's what the 32-bit composite visual
385 * set up above is for.
386 */
387 depth = config->redBits + config->greenBits + config->blueBits;
388
389 /* Make sure that our FBconfig's depth can actually be displayed
390 * (corresponds to an existing visual).
391 */
392 for (i = 0; i < pScreen->numVisuals; i++) {
393 if (depth == pScreen->visuals[i].nplanes)
394 break;
395 }
396 /* if it can't, fix up the fbconfig to not advertise window support */
397 if (i == pScreen->numVisuals)
398 config->drawableType &= ~(GLX_WINDOW_BIT);
399
400 /* fbconfig must support window drawables */
401 if (!(config->drawableType & GLX_WINDOW_BIT)) {
402 config->visualID = 0;
403 continue;
404 }
405
406 /* Create a new X visual for our FBconfig. */
407 visual = AddScreenVisuals(pScreen, 1, depth);
408 if (visual == NULL)
409 continue;
410
411 pGlxScreen->visuals[pGlxScreen->numVisuals++] = config;
412 initGlxVisual(visual, config);
413 }
414
415 dixSetPrivate(&pScreen->devPrivates, glxScreenPrivateKey, pGlxScreen);
416}
417
418void
419__glXScreenDestroy(__GLXscreen * screen)
420{
421 free(screen->GLXextensions);
422 free(screen->GLextensions);
423 free(screen->visuals);
424}