xref: /aosp_15_r20/external/mesa3d/src/glx/glxextensions.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * (C) Copyright IBM Corporation 2002, 2004
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  * THE COPYRIGHT HOLDERS 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 glxextensions.c
27  *
28  * \author Ian Romanick <[email protected]>
29  */
30 
31 #include "glxclient.h"
32 #include <X11/extensions/extutil.h>
33 #include <X11/extensions/Xext.h>
34 #include <string.h>
35 #include "glxextensions.h"
36 
37 #include "util/driconf.h"
38 #include "util/macros.h"
39 
40 #define SET_BIT(m,b)   (m[ (b) / 8 ] |=  (1U << ((b) % 8)))
41 #define CLR_BIT(m,b)   (m[ (b) / 8 ] &= ~(1U << ((b) % 8)))
42 #define IS_SET(m,b)    ((m[ (b) / 8 ] & (1U << ((b) % 8))) != 0)
43 #define GLX(n) "GLX_" # n, 4 + sizeof( # n ) - 1, PASTE2(n,_bit)
44 #define GL(n)  "GL_" # n,  3 + sizeof( # n ) - 1, GL_ ## n ## _bit
45 #define Y  1
46 #define N  0
47 #define EXT_ENABLED(bit,supported) (IS_SET( supported, bit ))
48 
49 
50 struct extension_info
51 {
52    const char *const name;
53    unsigned name_len;
54 
55    unsigned char bit;
56 
57    /**
58     * The direct-renderer (e.g., i965_dri.so) supports this extension.
59     *
60     * For cases where all of the infrastructure to support the extension is a
61     * required part of the loader/driver interface, this can default to Y.
62     * For most cases, extended functionality, usually in the form of DRI2
63     * extensions, is necessary to support the extension.  The loader will set
64     * the flag true if all the requirements are met.
65     *
66     * If the display is capable of direct rendering, ::direct_support is
67     * required for the extension to be enabled.
68     */
69    unsigned char direct_support;
70 
71    /**
72     * The extension only functions with direct-rendering contexts
73     *
74     * The extension has no GLX protocol, and, therefore, no explicit
75     * dependency on the server.  The functionality is contained entirely in
76     * the client library and the direct renderer.  A few of the swap-related
77     * extensions are intended to behave this way.
78     */
79    unsigned char direct_only;
80 };
81 
82 /* *INDENT-OFF* */
83 static const struct extension_info known_glx_extensions[] = {
84    { GLX(ARB_context_flush_control),      N, N },
85    { GLX(ARB_create_context),             N, N },
86    { GLX(ARB_create_context_no_error),    N, N },
87    { GLX(ARB_create_context_profile),     N, N },
88    { GLX(ARB_create_context_robustness),  N, N },
89    { GLX(ARB_fbconfig_float),             Y, N },
90    { GLX(ARB_framebuffer_sRGB),           Y, N },
91    { GLX(ARB_get_proc_address),           N, N },
92    { GLX(ARB_multisample),                Y, N },
93    { GLX(EXT_buffer_age),                 N, Y },
94    { GLX(EXT_create_context_es2_profile), N, N },
95    { GLX(EXT_create_context_es_profile),  N, N },
96    { GLX(EXT_fbconfig_packed_float),      Y, N },
97    { GLX(EXT_framebuffer_sRGB),           Y, N },
98    { GLX(EXT_import_context),             Y, N },
99    { GLX(EXT_no_config_context),          N, N },
100    { GLX(EXT_swap_control),               N, Y },
101    { GLX(EXT_swap_control_tear),          N, Y },
102    { GLX(EXT_texture_from_pixmap),        N, N },
103    { GLX(EXT_visual_info),                Y, N },
104    { GLX(EXT_visual_rating),              Y, N },
105    { GLX(ATI_pixel_format_float),         N, N },
106    { GLX(INTEL_swap_event),               N, N },
107    { GLX(MESA_copy_sub_buffer),           N, N },
108    { GLX(MESA_gl_interop),                N, Y },
109    { GLX(MESA_query_renderer),            N, Y },
110    { GLX(MESA_swap_control),              N, Y },
111    { GLX(NV_float_buffer),                N, N },
112    { GLX(OML_sync_control),               N, Y },
113    { GLX(SGIS_multisample),               Y, N },
114    { GLX(SGIX_fbconfig),                  Y, N },
115    { GLX(SGIX_pbuffer),                   Y, N },
116    { GLX(SGIX_visual_select_group),       Y, N },
117    { GLX(SGI_make_current_read),          N, N },
118    { GLX(SGI_swap_control),               N, N },
119    { GLX(SGI_video_sync),                 N, Y },
120    { NULL }
121 };
122 
123 static const struct extension_info known_gl_extensions[] = {
124    { GL(ARB_depth_texture),               N, N },
125    { GL(ARB_draw_buffers),                N, N },
126    { GL(ARB_fragment_program),            N, N },
127    { GL(ARB_fragment_program_shadow),     N, N },
128    { GL(ARB_framebuffer_object),          N, N },
129    { GL(ARB_imaging),                     N, N },
130    { GL(ARB_multisample),                 N, N },
131    { GL(ARB_multitexture),                N, N },
132    { GL(ARB_occlusion_query),             N, N },
133    { GL(ARB_point_parameters),            N, N },
134    { GL(ARB_point_sprite),                N, N },
135    { GL(ARB_shadow),                      N, N },
136    { GL(ARB_shadow_ambient),              N, N },
137    { GL(ARB_texture_border_clamp),        N, N },
138    { GL(ARB_texture_compression),         N, N },
139    { GL(ARB_texture_cube_map),            N, N },
140    { GL(ARB_texture_env_add),             N, N },
141    { GL(ARB_texture_env_combine),         N, N },
142    { GL(ARB_texture_env_crossbar),        N, N },
143    { GL(ARB_texture_env_dot3),            N, N },
144    { GL(ARB_texture_filter_anisotropic),  N, N },
145    { GL(ARB_texture_mirrored_repeat),     N, N },
146    { GL(ARB_texture_non_power_of_two),    N, N },
147    { GL(ARB_texture_rectangle),           N, N },
148    { GL(ARB_texture_rg),                  N, N },
149    { GL(ARB_transpose_matrix),            N, N },
150    { GL(ARB_vertex_program),              N, N },
151    { GL(ARB_window_pos),                  N, N },
152    { GL(EXT_abgr),                        N, N },
153    { GL(EXT_bgra),                        N, N },
154    { GL(EXT_blend_color),                 N, N },
155    { GL(EXT_blend_equation_separate),     N, N },
156    { GL(EXT_blend_func_separate),         N, N },
157    { GL(EXT_blend_logic_op),              N, N },
158    { GL(EXT_blend_minmax),                N, N },
159    { GL(EXT_blend_subtract),              N, N },
160    { GL(EXT_clip_volume_hint),            N, N },
161    { GL(EXT_copy_texture),                N, N },
162    { GL(EXT_draw_range_elements),         N, N },
163    { GL(EXT_fog_coord),                   N, N },
164    { GL(EXT_framebuffer_blit),            N, N },
165    { GL(EXT_framebuffer_multisample),     N, N },
166    { GL(EXT_framebuffer_object),          N, N },
167    { GL(EXT_framebuffer_sRGB),            N, N },
168    { GL(EXT_multi_draw_arrays),           N, N },
169    { GL(EXT_packed_depth_stencil),        N, N },
170    { GL(EXT_packed_pixels),               N, N },
171    { GL(EXT_paletted_texture),            N, N },
172    { GL(EXT_point_parameters),            N, N },
173    { GL(EXT_polygon_offset),              N, N },
174    { GL(EXT_rescale_normal),              N, N },
175    { GL(EXT_secondary_color),             N, N },
176    { GL(EXT_separate_specular_color),     N, N },
177    { GL(EXT_shadow_funcs),                N, N },
178    { GL(EXT_shared_texture_palette),      N, N },
179    { GL(EXT_stencil_two_side),            N, N },
180    { GL(EXT_stencil_wrap),                N, N },
181    { GL(EXT_subtexture),                  N, N },
182    { GL(EXT_texture),                     N, N },
183    { GL(EXT_texture3D),                   N, N },
184    { GL(EXT_texture_compression_dxt1),    N, N },
185    { GL(EXT_texture_compression_s3tc),    N, N },
186    { GL(EXT_texture_edge_clamp),          N, N },
187    { GL(EXT_texture_env_add),             N, N },
188    { GL(EXT_texture_env_combine),         N, N },
189    { GL(EXT_texture_env_dot3),            N, N },
190    { GL(EXT_texture_filter_anisotropic),  N, N },
191    { GL(EXT_texture_integer),             N, N },
192    { GL(EXT_texture_lod),                 N, N },
193    { GL(EXT_texture_lod_bias),            N, N },
194    { GL(EXT_texture_mirror_clamp),        N, N },
195    { GL(EXT_texture_rectangle),           N, N },
196    { GL(EXT_vertex_array),                N, N },
197    { GL(3DFX_texture_compression_FXT1),   N, N },
198    { GL(APPLE_packed_pixels),             N, N },
199    { GL(APPLE_ycbcr_422),                 N, N },
200    { GL(ATI_draw_buffers),                N, N },
201    { GL(ATI_text_fragment_shader),        N, N },
202    { GL(ATI_texture_env_combine3),        N, N },
203    { GL(ATI_texture_float),               N, N },
204    { GL(ATI_texture_mirror_once),         N, N },
205    { GL(ATIX_texture_env_combine3),       N, N },
206    { GL(HP_convolution_border_modes),     N, N },
207    { GL(HP_occlusion_test),               N, N },
208    { GL(IBM_cull_vertex),                 N, N },
209    { GL(IBM_pixel_filter_hint),           N, N },
210    { GL(IBM_rasterpos_clip),              N, N },
211    { GL(IBM_texture_clamp_nodraw),        N, N },
212    { GL(IBM_texture_mirrored_repeat),     N, N },
213    { GL(INGR_blend_func_separate),        N, N },
214    { GL(INGR_interlace_read),             N, N },
215    { GL(MESA_pack_invert),                N, N },
216    { GL(MESA_ycbcr_texture),              N, N },
217    { GL(NV_blend_square),                 N, N },
218    { GL(NV_copy_depth_to_color),          N, N },
219    { GL(NV_depth_clamp),                  N, N },
220    { GL(NV_fog_distance),                 N, N },
221    { GL(NV_fragment_program),             N, N },
222    { GL(NV_fragment_program_option),      N, N },
223    { GL(NV_fragment_program2),            N, N },
224    { GL(NV_light_max_exponent),           N, N },
225    { GL(NV_multisample_filter_hint),      N, N },
226    { GL(NV_packed_depth_stencil),         N, N },
227    { GL(NV_point_sprite),                 N, N },
228    { GL(NV_texgen_reflection),            N, N },
229    { GL(NV_texture_compression_vtc),      N, N },
230    { GL(NV_texture_env_combine4),         N, N },
231    { GL(NV_texture_rectangle),            N, N },
232    { GL(NV_vertex_program),               N, N },
233    { GL(NV_vertex_program1_1),            N, N },
234    { GL(NV_vertex_program2),              N, N },
235    { GL(NV_vertex_program2_option),       N, N },
236    { GL(NV_vertex_program3),              N, N },
237    { GL(OES_read_format),                 N, N },
238    { GL(OES_compressed_paletted_texture), N, N },
239    { GL(SGI_color_matrix),                N, N },
240    { GL(SGI_color_table),                 N, N },
241    { GL(SGI_texture_color_table),         N, N },
242    { GL(SGIS_generate_mipmap),            N, N },
243    { GL(SGIS_multisample),                N, N },
244    { GL(SGIS_texture_border_clamp),       N, N },
245    { GL(SGIS_texture_edge_clamp),         N, N },
246    { GL(SGIS_texture_lod),                N, N },
247    { GL(SGIX_blend_alpha_minmax),         N, N },
248    { GL(SGIX_clipmap),                    N, N },
249    { GL(SGIX_depth_texture),              N, N },
250    { GL(SGIX_fog_offset),                 N, N },
251    { GL(SGIX_shadow),                     N, N },
252    { GL(SGIX_shadow_ambient),             N, N },
253    { GL(SGIX_texture_coordinate_clamp),   N, N },
254    { GL(SGIX_texture_lod_bias),           N, N },
255    { GL(SGIX_texture_range),              N, N },
256    { GL(SGIX_texture_scale_bias),         N, N },
257    { GL(SGIX_vertex_preclip),             N, N },
258    { GL(SGIX_vertex_preclip_hint),        N, N },
259    { GL(SGIX_ycrcb),                      N, N },
260    { GL(SUN_convolution_border_modes),    N, N },
261    { GL(SUN_multi_draw_arrays),           N, N },
262    { GL(SUN_slice_accum),                 N, N },
263    { NULL }
264 };
265 /* *INDENT-ON* */
266 
267 
268 /* global bit-fields of available extensions and their characteristics */
269 static unsigned char client_glx_only[__GLX_EXT_BYTES];
270 static unsigned char direct_glx_only[__GLX_EXT_BYTES];
271 
272 /**
273  * Bits representing the set of extensions that are enabled by default in all
274  * direct rendering drivers.
275  */
276 static unsigned char direct_glx_support[__GLX_EXT_BYTES];
277 
278 /* client extensions string */
279 static const char *__glXGLXClientExtensions = NULL;
280 
281 static void __glXExtensionsCtr(void);
282 static void __glXExtensionsCtrScreen(struct glx_screen * psc);
283 static void __glXProcessServerString(const struct extension_info *ext,
284                                      const char *server_string,
285                                      unsigned char *server_support);
286 
287 /**
288  * Find an extension in the list based on its name.
289  *
290  * \param ext       List of extensions where to search.
291  * \param name      Name of the extension.
292  * \param name_len  Length, in characters, of the extension name.
293  */
294 static const struct extension_info *
find_extension(const struct extension_info * ext,const char * name,unsigned name_len)295 find_extension(const struct extension_info *ext, const char *name,
296                unsigned name_len)
297 {
298    unsigned i;
299 
300    for (i = 0; ext[i].name != NULL; i++) {
301       if ((name_len == ext[i].name_len)
302           && (strncmp(ext[i].name, name, name_len) == 0)) {
303          return &ext[i];
304       }
305    }
306 
307    return NULL;
308 }
309 
310 /**
311  * Set the state of a GLX extension.
312  *
313  * \param name      Name of the extension.
314  * \param name_len  Length, in characters, of the extension name.
315  * \param state     New state (either enabled or disabled) of the extension.
316  * \param supported Table in which the state of the extension is to be set.
317  */
318 static void
set_glx_extension(const struct extension_info * ext_list,const char * name,unsigned name_len,GLboolean state,unsigned char * supported)319 set_glx_extension(const struct extension_info *ext_list,
320                   const char *name, unsigned name_len, GLboolean state,
321                   unsigned char *supported)
322 {
323    const struct extension_info *ext = find_extension(ext_list, name, name_len);
324    if (!ext)
325        return;
326 
327    if (state) {
328       SET_BIT(supported, ext->bit);
329    } else {
330       CLR_BIT(supported, ext->bit);
331    }
332 }
333 
334 
335 #define NUL '\0'
336 #define SEPARATOR ' '
337 
338 /**
339  * Convert the server's extension string to a bit-field.
340  *
341  * \param server_string   GLX extension string from the server.
342  * \param server_support  Bit-field of supported extensions.
343  *
344  * \note
345  * This function is used to process both GLX and GL extension strings.  The
346  * bit-fields used to track each of these have different sizes.  Therefore,
347  * the data pointed by \c server_support must be preinitialized to zero.
348  */
349 static void
__glXProcessServerString(const struct extension_info * ext,const char * server_string,unsigned char * server_support)350 __glXProcessServerString(const struct extension_info *ext,
351                          const char *server_string,
352                          unsigned char *server_support)
353 {
354    unsigned base;
355    unsigned len;
356 
357    for (base = 0; server_string[base] != NUL; /* empty */ ) {
358       /* Determine the length of the next extension name.
359        */
360       for (len = 0; (server_string[base + len] != SEPARATOR)
361            && (server_string[base + len] != NUL); len++) {
362          /* empty */
363       }
364 
365       /* Set the bit for the extension in the server_support table.
366        */
367       set_glx_extension(ext, &server_string[base], len, GL_TRUE,
368                         server_support);
369 
370 
371       /* Advance to the next extension string.  This means that we skip
372        * over the previous string and any trialing white-space.
373        */
374       for (base += len; (server_string[base] == SEPARATOR)
375            && (server_string[base] != NUL); base++) {
376          /* empty */
377       }
378    }
379 }
380 
381 void
__glXEnableDirectExtension(struct glx_screen * psc,const char * name)382 __glXEnableDirectExtension(struct glx_screen * psc, const char *name)
383 {
384    __glXExtensionsCtr();
385    __glXExtensionsCtrScreen(psc);
386 
387    set_glx_extension(known_glx_extensions,
388                      name, strlen(name), GL_TRUE, psc->direct_support);
389 }
390 
391 static void
__ParseExtensionOverride(struct glx_screen * psc,const struct extension_info * ext_list,unsigned char * force_enable,unsigned char * force_disable,const char * override)392 __ParseExtensionOverride(struct glx_screen *psc,
393                          const struct extension_info *ext_list,
394                          unsigned char *force_enable,
395                          unsigned char *force_disable,
396                          const char *override)
397 {
398    const struct extension_info *ext;
399    char *env, *field;
400 
401    if (override == NULL)
402        return;
403 
404    /* Copy env_const because strtok() is destructive. */
405    env = strdup(override);
406    if (env == NULL)
407       return;
408 
409    for (field = strtok(env, " "); field!= NULL; field = strtok(NULL, " ")) {
410       GLboolean enable;
411 
412       switch (field[0]) {
413       case '+':
414          enable = GL_TRUE;
415          ++field;
416          break;
417       case '-':
418          enable = GL_FALSE;
419          ++field;
420          break;
421       default:
422          enable = GL_TRUE;
423          break;
424       }
425 
426       ext = find_extension(ext_list, field, strlen(field));
427       if (ext) {
428          if (enable)
429             SET_BIT(force_enable, ext->bit);
430          else
431             SET_BIT(force_disable, ext->bit);
432       } else {
433          fprintf(stderr, "WARNING: Trying to %s the unknown extension '%s'\n",
434                  enable ? "enable" : "disable", field);
435       }
436    }
437 
438    free(env);
439 }
440 
441 /**
442  * \brief Parse the list of GLX extensions that the user wants to
443  * force-enable/disable by using \c override, and write the results to the
444  * screen's context.
445  *
446  * \param psc        Pointer to GLX per-screen record.
447  * \param override   A space-separated list of extensions to enable or disable.
448  * The list is processed thus:
449  *    - Enable recognized extension names that are prefixed with '+'.
450  *    - Disable recognized extension names that are prefixed with '-'.
451  *    - Enable recognized extension names that are not prefixed.
452  */
453 void
__glXParseExtensionOverride(struct glx_screen * psc,const char * override)454 __glXParseExtensionOverride(struct glx_screen *psc, const char *override)
455 {
456     __ParseExtensionOverride(psc, known_glx_extensions, psc->glx_force_enabled,
457                              psc->glx_force_disabled, override);
458 }
459 
460 /**
461  * \brief Parse the list of GL extensions that the user wants to
462  * force-enable/disable by using \c override, and write the results to the
463  * screen's context.
464  *
465  * \param psc        Pointer to GLX per-screen record.
466  * \param override   A space-separated list of extensions to enable or disable.
467  * The list is processed thus:
468  *    - Enable recognized extension names that are prefixed with '+'.
469  *    - Disable recognized extension names that are prefixed with '-'.
470  *    - Enable recognized extension names that are not prefixed.
471  */
472 void
__IndirectGlParseExtensionOverride(struct glx_screen * psc,const char * override)473 __IndirectGlParseExtensionOverride(struct glx_screen *psc, const char *override)
474 {
475     __ParseExtensionOverride(psc, known_gl_extensions, psc->gl_force_enabled,
476                              psc->gl_force_disabled, override);
477 }
478 
479 
480 /**
481  * Initialize global extension support tables.
482  */
483 
484 static void
__glXExtensionsCtr(void)485 __glXExtensionsCtr(void)
486 {
487    unsigned i;
488    static GLboolean ext_list_first_time = GL_TRUE;
489 
490 
491    if (ext_list_first_time) {
492       ext_list_first_time = GL_FALSE;
493 
494       (void) memset(direct_glx_support, 0, sizeof(direct_glx_support));
495       (void) memset(client_glx_only, 0, sizeof(client_glx_only));
496       (void) memset(direct_glx_only, 0, sizeof(direct_glx_only));
497 
498       SET_BIT(client_glx_only, ARB_get_proc_address_bit);
499       for (i = 0; known_glx_extensions[i].name != NULL; i++) {
500          const unsigned bit = known_glx_extensions[i].bit;
501 
502          if (known_glx_extensions[i].direct_support) {
503             SET_BIT(direct_glx_support, bit);
504          }
505 
506          if (known_glx_extensions[i].direct_only) {
507             SET_BIT(direct_glx_only, bit);
508          }
509       }
510    }
511 }
512 
513 
514 /**
515  * Make sure that per-screen direct-support table is initialized.
516  *
517  * \param psc  Pointer to GLX per-screen record.
518  */
519 
520 static void
__glXExtensionsCtrScreen(struct glx_screen * psc)521 __glXExtensionsCtrScreen(struct glx_screen * psc)
522 {
523    if (psc->ext_list_first_time) {
524       psc->ext_list_first_time = GL_FALSE;
525       (void) memcpy(psc->direct_support, direct_glx_support,
526                     sizeof(direct_glx_support));
527       (void) memset(psc->glx_force_enabled, 0,
528                     sizeof(psc->glx_force_enabled));
529       (void) memset(psc->glx_force_disabled, 0,
530                     sizeof(psc->glx_force_disabled));
531       (void) memset(psc->gl_force_enabled, 0,
532                     sizeof(psc->gl_force_enabled));
533       (void) memset(psc->gl_force_disabled, 0,
534                     sizeof(psc->gl_force_disabled));
535    }
536 }
537 
538 
539 /**
540  * Check if a certain extension is enabled on a given screen.
541  *
542  * \param psc  Pointer to GLX per-screen record.
543  * \param bit  Bit index in the direct-support table.
544  * \returns If the extension bit is enabled for the screen, \c GL_TRUE is
545  *          returned.  If the extension bit is not enabled or if \c psc is
546  *          \c NULL, then \c GL_FALSE is returned.
547  */
548 GLboolean
__glXExtensionBitIsEnabled(struct glx_screen * psc,unsigned bit)549 __glXExtensionBitIsEnabled(struct glx_screen * psc, unsigned bit)
550 {
551    GLboolean enabled = GL_FALSE;
552 
553    if (psc != NULL) {
554       __glXExtensionsCtr();
555       __glXExtensionsCtrScreen(psc);
556       enabled = EXT_ENABLED(bit, psc->direct_support);
557    }
558 
559    return enabled;
560 }
561 
562 
563 /**
564  * Check if a certain extension is enabled in a given context.
565  *
566  */
567 GLboolean
__glExtensionBitIsEnabled(struct glx_context * gc,unsigned bit)568 __glExtensionBitIsEnabled(struct glx_context *gc, unsigned bit)
569 {
570    GLboolean enabled = GL_FALSE;
571 
572    if (gc != NULL) {
573       enabled = EXT_ENABLED(bit, gc->gl_extension_bits);
574    }
575 
576    return enabled;
577 }
578 
579 
580 
581 /**
582  * Convert a bit-field to a string of supported extensions.
583  */
584 static char *
__glXGetStringFromTable(const struct extension_info * ext,const unsigned char * filter)585 __glXGetStringFromTable(const struct extension_info *ext,
586                         const unsigned char *filter)
587 {
588    unsigned i;
589    unsigned ext_str_len;
590    char *ext_str;
591    char *point;
592 
593 
594    ext_str_len = 0;
595    for (i = 0; ext[i].name != NULL; i++) {
596       if (!filter || EXT_ENABLED(ext[i].bit, filter)) {
597          ext_str_len += ext[i].name_len + 1;
598       }
599    }
600 
601    ext_str = malloc(ext_str_len + 1);
602    if (ext_str != NULL) {
603       point = ext_str;
604 
605       for (i = 0; ext[i].name != NULL; i++) {
606          if (!filter || EXT_ENABLED(ext[i].bit, filter)) {
607             (void) memcpy(point, ext[i].name, ext[i].name_len);
608             point += ext[i].name_len;
609 
610             *point = ' ';
611             point++;
612          }
613       }
614 
615       *point = '\0';
616    }
617 
618    return ext_str;
619 }
620 
621 
622 /**
623  * Get the string of client library supported extensions.
624  */
625 const char *
__glXGetClientExtensions(Display * dpy)626 __glXGetClientExtensions(Display *dpy)
627 {
628    if (__glXGLXClientExtensions == NULL) {
629       __glXExtensionsCtr();
630       __glXGLXClientExtensions = __glXGetStringFromTable(known_glx_extensions,
631                                                          NULL);
632    }
633 
634    return __glXGLXClientExtensions;
635 }
636 
637 
638 /**
639  * Calculate the list of application usable extensions.  The resulting
640  * string is stored in \c psc->effectiveGLXexts.
641  *
642  * \param psc                        Pointer to GLX per-screen record.
643  * \param display_is_direct_capable  True if the display is capable of
644  *                                   direct rendering.
645  */
646 
647 void
__glXCalculateUsableExtensions(struct glx_screen * psc,GLboolean display_is_direct_capable)648 __glXCalculateUsableExtensions(struct glx_screen * psc,
649                                GLboolean display_is_direct_capable)
650 {
651    unsigned char server_support[__GLX_EXT_BYTES];
652    unsigned char usable[__GLX_EXT_BYTES];
653    unsigned i;
654 
655    __glXExtensionsCtr();
656    __glXExtensionsCtrScreen(psc);
657 
658    (void) memset(server_support, 0, sizeof(server_support));
659    __glXProcessServerString(known_glx_extensions,
660                             psc->serverGLXexts, server_support);
661 
662 
663    /* An extension is supported if the client-side (i.e., libGL) supports
664     * it and the "server" supports it.  In this case that means that either
665     * the true server supports it or it is only for direct-rendering and
666     * the direct rendering driver supports it.
667     *
668     * If the display is not capable of direct rendering, then the extension
669     * is enabled if and only if the client-side library and the server
670     * support it.
671     */
672 
673    if (display_is_direct_capable) {
674       for (i = 0; i < __GLX_EXT_BYTES; i++) {
675          /* Enable extensions that the client supports that only have a client-side
676           * component.
677           */
678          unsigned char u = client_glx_only[i];
679 
680          /* Enable extensions that are supported for direct rendering, and either
681           * are supported by the server or only have a direct-rendering component.
682           */
683          u |= psc->direct_support[i] & (server_support[i] | direct_glx_only[i]);
684 
685          /* Finally, apply driconf options to force some extension bits either
686           * enabled or disabled.
687           */
688          u |= psc->glx_force_enabled[i];
689          u &= ~psc->glx_force_disabled[i];
690 
691          usable[i] = u;
692       }
693    }
694    else {
695       for (i = 0; i < __GLX_EXT_BYTES; i++) {
696          /* Enable extensions that the client supports that only have a
697           * client-side component.
698           */
699          unsigned char u = client_glx_only[i];
700 
701          /* Enable extensions that the client and server both support */
702          u |= server_support[i];
703 
704          /* Finally, apply driconf options to force some extension bits either
705           * enabled or disabled.
706           */
707          u |= psc->glx_force_enabled[i];
708          u &= ~psc->glx_force_disabled[i];
709 
710          usable[i] = u;
711       }
712    }
713 
714    psc->effectiveGLXexts = __glXGetStringFromTable(known_glx_extensions,
715                                                    usable);
716 }
717 
718 /**
719  * Calculate the list of application usable extensions.  The resulting
720  * string is stored in \c gc->extensions.
721  *
722  * \param gc             Pointer to GLX context.
723  * \param server_string  Extension string from the server.
724  */
725 
726 void
__glXCalculateUsableGLExtensions(struct glx_context * gc,const char * server_string)727 __glXCalculateUsableGLExtensions(struct glx_context * gc,
728                                  const char *server_string)
729 {
730    struct glx_screen *psc = gc->psc;
731    unsigned char server_support[__GL_EXT_BYTES];
732    unsigned char usable[__GL_EXT_BYTES];
733    unsigned i;
734 
735    (void) memset(server_support, 0, sizeof(server_support));
736    __glXProcessServerString(known_gl_extensions, server_string,
737                             server_support);
738 
739    /* These extensions are wholly inside the client-side indirect code */
740    (void) memset(usable, 0, sizeof(usable));
741    SET_BIT(usable, GL_ARB_transpose_matrix_bit);
742    SET_BIT(usable, GL_EXT_draw_range_elements_bit);
743    SET_BIT(usable, GL_EXT_multi_draw_arrays_bit);
744    SET_BIT(usable, GL_SUN_multi_draw_arrays_bit);
745 
746    for (i = 0; i < __GL_EXT_BYTES; i++) {
747       /* Usable if the server supports it, or if it's been forced on */
748       usable[i] = server_support[i] | psc->gl_force_enabled[i];
749 
750       /* But not if it's been forced off */
751       usable[i] &= ~psc->gl_force_disabled[i];
752    }
753 
754    gc->extensions = (unsigned char *)
755       __glXGetStringFromTable(known_gl_extensions, usable);
756    (void) memcpy(gc->gl_extension_bits, usable, sizeof(usable));
757 }
758 
759 /**
760  * Get a string representing the set of extensions supported by the client
761  * library.  This is currently only used to send the list of extensions
762  * supported by the client to the server.
763  */
764 char *
__glXGetClientGLExtensionString(int screen)765 __glXGetClientGLExtensionString(int screen)
766 {
767    if (screen < 0)
768       return strdup("");
769 
770    return __glXGetStringFromTable(known_gl_extensions, NULL);
771 }
772