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 <idr@us.ibm.com>
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 
39 #define SET_BIT(m,b)   (m[ (b) / 8 ] |=  (1U << ((b) % 8)))
40 #define CLR_BIT(m,b)   (m[ (b) / 8 ] &= ~(1U << ((b) % 8)))
41 #define IS_SET(m,b)    ((m[ (b) / 8 ] & (1U << ((b) % 8))) != 0)
42 #define CONCAT(a,b) a ## b
43 #define GLX(n) "GLX_" # n, 4 + sizeof( # n ) - 1, CONCAT(n,_bit)
44 #define GL(n)  "GL_" # n,  3 + sizeof( # n ) - 1, GL_ ## n ## _bit
45 #define VER(a,b)  a, b
46 #define Y  1
47 #define N  0
48 #define EXT_ENABLED(bit,supported) (IS_SET( supported, bit ))
49 
50 
51 struct extension_info
52 {
53    const char *const name;
54    unsigned name_len;
55 
56    unsigned char bit;
57 
58    /* This is the lowest version of GLX that "requires" this extension.
59     * For example, GLX 1.3 requires SGIX_fbconfig, SGIX_pbuffer, and
60     * SGI_make_current_read.  If the extension is not required by any known
61     * version of GLX, use 0, 0.
62     */
63    unsigned char version_major;
64    unsigned char version_minor;
65 
66    /**
67     * The client (i.e., libGL) supports this extension.
68     *
69     * Except during bring up, all extensions should have this set to Y.  There
70     * are a few cases of extensions that have partial (or speculative)
71     * support, but these are rare.  There also shouldn't be any new ones
72     * added.
73     *
74     * Generally, extensions require server support and ::client_support to be
75     * enabled.  If the display is capable of direct rendering,
76     * ::direct_support is also required.
77     *
78     * \sa ::client_only
79     */
80    unsigned char client_support;
81 
82    /**
83     * The direct-renderer (e.g., i965_dri.so) supports this extension.
84     *
85     * For cases where all of the infrastructure to support the extension is a
86     * required part of the loader/driver interface, this can default to Y.
87     * For most cases, extended functionality, usually in the form of DRI2
88     * extensions, is necessary to support the extension.  The loader will set
89     * the flag true if all the requirements are met.
90     *
91     * If the display is capable of direct rendering, ::direct_support is
92     * required for the extension to be enabled.
93     */
94    unsigned char direct_support;
95 
96    /**
97     * The extension depends only on client support.
98     *
99     * This is for extensions like GLX_ARB_get_proc_address that are contained
100     * entirely in the client library.  There is no dependency on the server or
101     * the direct-renderer.
102     *
103     * These extensions will be enabled if ::client_support is set.
104     *
105     * \note
106     * An extension \b cannot be both client-only and direct-only because being
107     * direct-only implies a dependency on the direct renderer.
108     *
109     * \sa ::client_support, ::direct_only
110     */
111    unsigned char client_only;
112 
113    /**
114     * The extension only functions with direct-rendering contexts
115     *
116     * The extension has no GLX protocol, and, therefore, no explicit
117     * dependency on the server.  The functionality is contained entirely in
118     * the client library and the direct renderer.  A few of the swap-related
119     * extensions are intended to behave this way.
120     *
121     * These extensions will be enabled if both ::client_support and
122     * ::direct_support are set.
123     *
124     * \note
125     * An extension \b cannot be both client-only and direct-only because being
126     * client-only implies that all functionality is outside the
127     * direct-renderer.
128     *
129     * \sa ::direct_support, ::client_only
130     */
131    unsigned char direct_only;
132 };
133 
134 /* *INDENT-OFF* */
135 static const struct extension_info known_glx_extensions[] = {
136    { GLX(ARB_context_flush_control),   VER(0,0), Y, N, N, N },
137    { GLX(ARB_create_context),          VER(0,0), Y, N, N, N },
138    { GLX(ARB_create_context_no_error), VER(1,4), Y, N, N, N },
139    { GLX(ARB_create_context_profile),  VER(0,0), Y, N, N, N },
140    { GLX(ARB_create_context_robustness), VER(0,0), Y, N, N, N },
141    { GLX(ARB_fbconfig_float),          VER(0,0), Y, Y, N, N },
142    { GLX(ARB_framebuffer_sRGB),        VER(0,0), Y, Y, N, N },
143    { GLX(ARB_get_proc_address),        VER(1,4), Y, N, Y, N },
144    { GLX(ARB_multisample),             VER(1,4), Y, Y, N, N },
145    { GLX(EXT_buffer_age),              VER(0,0), Y, N, N, Y },
146    { GLX(EXT_create_context_es2_profile), VER(0,0), Y, N, N, N },
147    { GLX(EXT_create_context_es_profile), VER(0,0), Y, N, N, N },
148    { GLX(EXT_fbconfig_packed_float),   VER(0,0), Y, Y, N, N },
149    { GLX(EXT_framebuffer_sRGB),        VER(0,0), Y, Y, N, N },
150    { GLX(EXT_import_context),          VER(0,0), Y, Y, N, N },
151    { GLX(EXT_swap_control),            VER(0,0), Y, N, N, Y },
152    { GLX(EXT_swap_control_tear),       VER(0,0), Y, N, N, Y },
153    { GLX(EXT_texture_from_pixmap),     VER(0,0), Y, N, N, N },
154    { GLX(EXT_visual_info),             VER(0,0), Y, Y, N, N },
155    { GLX(EXT_visual_rating),           VER(0,0), Y, Y, N, N },
156    { GLX(ATI_pixel_format_float),      VER(0,0), N, N, N, N },
157    { GLX(INTEL_swap_event),            VER(0,0), Y, N, N, N },
158    { GLX(MESA_copy_sub_buffer),        VER(0,0), Y, N, N, N },
159    { GLX(MESA_multithread_makecurrent),VER(0,0), Y, N, N, Y },
160    { GLX(MESA_query_renderer),         VER(0,0), Y, N, N, Y },
161    { GLX(MESA_swap_control),           VER(0,0), Y, N, N, Y },
162    { GLX(NV_float_buffer),             VER(0,0), N, N, N, N },
163    { GLX(OML_swap_method),             VER(0,0), Y, Y, N, N },
164    { GLX(OML_sync_control),            VER(0,0), Y, N, N, Y },
165    { GLX(SGIS_multisample),            VER(0,0), Y, Y, N, N },
166    { GLX(SGIX_fbconfig),               VER(1,3), Y, Y, N, N },
167    { GLX(SGIX_pbuffer),                VER(1,3), Y, Y, N, N },
168    { GLX(SGIX_visual_select_group),    VER(0,0), Y, Y, N, N },
169    { GLX(SGI_make_current_read),       VER(1,3), Y, N, N, N },
170    { GLX(SGI_swap_control),            VER(0,0), Y, N, N, N },
171    { GLX(SGI_video_sync),              VER(0,0), Y, N, N, Y },
172    { NULL }
173 };
174 
175 static const struct extension_info known_gl_extensions[] = {
176    { GL(ARB_depth_texture),              VER(1,4), Y, N, N, N },
177    { GL(ARB_draw_buffers),               VER(0,0), Y, N, N, N },
178    { GL(ARB_fragment_program),           VER(0,0), Y, N, N, N },
179    { GL(ARB_fragment_program_shadow),    VER(0,0), Y, N, N, N },
180    { GL(ARB_framebuffer_object),         VER(0,0), Y, N, N, N },
181    { GL(ARB_imaging),                    VER(0,0), Y, N, N, N },
182    { GL(ARB_multisample),                VER(1,3), Y, N, N, N },
183    { GL(ARB_multitexture),               VER(1,3), Y, N, N, N },
184    { GL(ARB_occlusion_query),            VER(1,5), Y, N, N, N },
185    { GL(ARB_point_parameters),           VER(1,4), Y, N, N, N },
186    { GL(ARB_point_sprite),               VER(0,0), Y, N, N, N },
187    { GL(ARB_shadow),                     VER(1,4), Y, N, N, N },
188    { GL(ARB_shadow_ambient),             VER(0,0), Y, N, N, N },
189    { GL(ARB_texture_border_clamp),       VER(1,3), Y, N, N, N },
190    { GL(ARB_texture_compression),        VER(1,3), Y, N, N, N },
191    { GL(ARB_texture_cube_map),           VER(1,3), Y, N, N, N },
192    { GL(ARB_texture_env_add),            VER(1,3), Y, N, N, N },
193    { GL(ARB_texture_env_combine),        VER(1,3), Y, N, N, N },
194    { GL(ARB_texture_env_crossbar),       VER(1,4), Y, N, N, N },
195    { GL(ARB_texture_env_dot3),           VER(1,3), Y, N, N, N },
196    { GL(ARB_texture_filter_anisotropic), VER(0,0), Y, N, N, N },
197    { GL(ARB_texture_mirrored_repeat),    VER(1,4), Y, N, N, N },
198    { GL(ARB_texture_non_power_of_two),   VER(1,5), Y, N, N, N },
199    { GL(ARB_texture_rectangle),          VER(0,0), Y, N, N, N },
200    { GL(ARB_texture_rg),                 VER(0,0), Y, N, N, N },
201    { GL(ARB_transpose_matrix),           VER(1,3), Y, N, Y, N },
202    { GL(ARB_vertex_buffer_object),       VER(1,5), N, N, N, N },
203    { GL(ARB_vertex_program),             VER(0,0), Y, N, N, N },
204    { GL(ARB_window_pos),                 VER(1,4), Y, N, N, N },
205    { GL(EXT_abgr),                       VER(0,0), Y, N, N, N },
206    { GL(EXT_bgra),                       VER(1,2), Y, N, N, N },
207    { GL(EXT_blend_color),                VER(1,4), Y, N, N, N },
208    { GL(EXT_blend_equation_separate),    VER(0,0), Y, N, N, N },
209    { GL(EXT_blend_func_separate),        VER(1,4), Y, N, N, N },
210    { GL(EXT_blend_logic_op),             VER(1,4), Y, N, N, N },
211    { GL(EXT_blend_minmax),               VER(1,4), Y, N, N, N },
212    { GL(EXT_blend_subtract),             VER(1,4), Y, N, N, N },
213    { GL(EXT_clip_volume_hint),           VER(0,0), Y, N, N, N },
214    { GL(EXT_compiled_vertex_array),      VER(0,0), N, N, N, N },
215    { GL(EXT_convolution),                VER(0,0), N, N, N, N },
216    { GL(EXT_copy_texture),               VER(1,1), Y, N, N, N },
217    { GL(EXT_cull_vertex),                VER(0,0), N, N, N, N },
218    { GL(EXT_depth_bounds_test),          VER(0,0), N, N, N, N },
219    { GL(EXT_draw_range_elements),        VER(1,2), Y, N, Y, N },
220    { GL(EXT_fog_coord),                  VER(1,4), Y, N, N, N },
221    { GL(EXT_framebuffer_blit),           VER(0,0), Y, N, N, N },
222    { GL(EXT_framebuffer_multisample),    VER(0,0), Y, N, N, N },
223    { GL(EXT_framebuffer_object),         VER(0,0), Y, N, N, N },
224    { GL(EXT_framebuffer_sRGB),           VER(0,0), Y, N, N, N },
225    { GL(EXT_multi_draw_arrays),          VER(1,4), Y, N, Y, N },
226    { GL(EXT_packed_depth_stencil),       VER(0,0), Y, N, N, N },
227    { GL(EXT_packed_pixels),              VER(1,2), Y, N, N, N },
228    { GL(EXT_paletted_texture),           VER(0,0), Y, N, N, N },
229    { GL(EXT_pixel_buffer_object),        VER(0,0), N, N, N, N },
230    { GL(EXT_point_parameters),           VER(1,4), Y, N, N, N },
231    { GL(EXT_polygon_offset),             VER(1,1), Y, N, N, N },
232    { GL(EXT_rescale_normal),             VER(1,2), Y, N, N, N },
233    { GL(EXT_secondary_color),            VER(1,4), Y, N, N, N },
234    { GL(EXT_separate_specular_color),    VER(1,2), Y, N, N, N },
235    { GL(EXT_shadow_funcs),               VER(1,5), Y, N, N, N },
236    { GL(EXT_shared_texture_palette),     VER(0,0), Y, N, N, N },
237    { GL(EXT_stencil_two_side),           VER(0,0), Y, N, N, N },
238    { GL(EXT_stencil_wrap),               VER(1,4), Y, N, N, N },
239    { GL(EXT_subtexture),                 VER(1,1), Y, N, N, N },
240    { GL(EXT_texture),                    VER(1,1), Y, N, N, N },
241    { GL(EXT_texture3D),                  VER(1,2), Y, N, N, N },
242    { GL(EXT_texture_compression_dxt1),   VER(0,0), Y, N, N, N },
243    { GL(EXT_texture_compression_s3tc),   VER(0,0), Y, N, N, N },
244    { GL(EXT_texture_edge_clamp),         VER(1,2), Y, N, N, N },
245    { GL(EXT_texture_env_add),            VER(1,3), Y, N, N, N },
246    { GL(EXT_texture_env_combine),        VER(1,3), Y, N, N, N },
247    { GL(EXT_texture_env_dot3),           VER(0,0), Y, N, N, N },
248    { GL(EXT_texture_filter_anisotropic), VER(0,0), Y, N, N, N },
249    { GL(EXT_texture_integer),            VER(0,0), Y, N, N, N },
250    { GL(EXT_texture_lod),                VER(1,2), Y, N, N, N },
251    { GL(EXT_texture_lod_bias),           VER(1,4), Y, N, N, N },
252    { GL(EXT_texture_mirror_clamp),       VER(0,0), Y, N, N, N },
253    { GL(EXT_texture_object),             VER(1,1), Y, N, N, N },
254    { GL(EXT_texture_rectangle),          VER(0,0), Y, N, N, N },
255    { GL(EXT_vertex_array),               VER(0,0), Y, N, N, N },
256    { GL(3DFX_texture_compression_FXT1),  VER(0,0), Y, N, N, N },
257    { GL(APPLE_packed_pixels),            VER(1,2), Y, N, N, N },
258    { GL(APPLE_ycbcr_422),                VER(0,0), Y, N, N, N },
259    { GL(ATI_draw_buffers),               VER(0,0), Y, N, N, N },
260    { GL(ATI_text_fragment_shader),       VER(0,0), Y, N, N, N },
261    { GL(ATI_texture_env_combine3),       VER(0,0), Y, N, N, N },
262    { GL(ATI_texture_float),              VER(0,0), Y, N, N, N },
263    { GL(ATI_texture_mirror_once),        VER(0,0), Y, N, N, N },
264    { GL(ATIX_texture_env_combine3),      VER(0,0), Y, N, N, N },
265    { GL(HP_convolution_border_modes),    VER(0,0), Y, N, N, N },
266    { GL(HP_occlusion_test),              VER(0,0), Y, N, N, N },
267    { GL(IBM_cull_vertex),                VER(0,0), Y, N, N, N },
268    { GL(IBM_pixel_filter_hint),          VER(0,0), Y, N, N, N },
269    { GL(IBM_rasterpos_clip),             VER(0,0), Y, N, N, N },
270    { GL(IBM_texture_clamp_nodraw),       VER(0,0), Y, N, N, N },
271    { GL(IBM_texture_mirrored_repeat),    VER(0,0), Y, N, N, N },
272    { GL(INGR_blend_func_separate),       VER(0,0), Y, N, N, N },
273    { GL(INGR_interlace_read),            VER(0,0), Y, N, N, N },
274    { GL(MESA_pack_invert),               VER(0,0), Y, N, N, N },
275    { GL(MESA_ycbcr_texture),             VER(0,0), Y, N, N, N },
276    { GL(NV_blend_square),                VER(1,4), Y, N, N, N },
277    { GL(NV_copy_depth_to_color),         VER(0,0), Y, N, N, N },
278    { GL(NV_depth_clamp),                 VER(0,0), Y, N, N, N },
279    { GL(NV_fog_distance),                VER(0,0), Y, N, N, N },
280    { GL(NV_fragment_program),            VER(0,0), Y, N, N, N },
281    { GL(NV_fragment_program_option),     VER(0,0), Y, N, N, N },
282    { GL(NV_fragment_program2),           VER(0,0), Y, N, N, N },
283    { GL(NV_light_max_exponent),          VER(0,0), Y, N, N, N },
284    { GL(NV_multisample_filter_hint),     VER(0,0), Y, N, N, N },
285    { GL(NV_packed_depth_stencil),        VER(0,0), Y, N, N, N },
286    { GL(NV_point_sprite),                VER(0,0), Y, N, N, N },
287    { GL(NV_texgen_reflection),           VER(0,0), Y, N, N, N },
288    { GL(NV_texture_compression_vtc),     VER(0,0), Y, N, N, N },
289    { GL(NV_texture_env_combine4),        VER(0,0), Y, N, N, N },
290    { GL(NV_texture_rectangle),           VER(0,0), Y, N, N, N },
291    { GL(NV_vertex_program),              VER(0,0), Y, N, N, N },
292    { GL(NV_vertex_program1_1),           VER(0,0), Y, N, N, N },
293    { GL(NV_vertex_program2),             VER(0,0), Y, N, N, N },
294    { GL(NV_vertex_program2_option),      VER(0,0), Y, N, N, N },
295    { GL(NV_vertex_program3),             VER(0,0), Y, N, N, N },
296    { GL(OES_read_format),                VER(0,0), Y, N, N, N },
297    { GL(OES_compressed_paletted_texture),VER(0,0), Y, N, N, N },
298    { GL(SGI_color_matrix),               VER(0,0), Y, N, N, N },
299    { GL(SGI_color_table),                VER(0,0), Y, N, N, N },
300    { GL(SGI_texture_color_table),        VER(0,0), Y, N, N, N },
301    { GL(SGIS_generate_mipmap),           VER(1,4), Y, N, N, N },
302    { GL(SGIS_multisample),               VER(0,0), Y, N, N, N },
303    { GL(SGIS_texture_border_clamp),      VER(1,3), Y, N, N, N },
304    { GL(SGIS_texture_edge_clamp),        VER(1,2), Y, N, N, N },
305    { GL(SGIS_texture_lod),               VER(1,2), Y, N, N, N },
306    { GL(SGIX_blend_alpha_minmax),        VER(0,0), Y, N, N, N },
307    { GL(SGIX_clipmap),                   VER(0,0), Y, N, N, N },
308    { GL(SGIX_depth_texture),             VER(0,0), Y, N, N, N },
309    { GL(SGIX_fog_offset),                VER(0,0), Y, N, N, N },
310    { GL(SGIX_shadow),                    VER(0,0), Y, N, N, N },
311    { GL(SGIX_shadow_ambient),            VER(0,0), Y, N, N, N },
312    { GL(SGIX_texture_coordinate_clamp),  VER(0,0), Y, N, N, N },
313    { GL(SGIX_texture_lod_bias),          VER(0,0), Y, N, N, N },
314    { GL(SGIX_texture_range),             VER(0,0), Y, N, N, N },
315    { GL(SGIX_texture_scale_bias),        VER(0,0), Y, N, N, N },
316    { GL(SGIX_vertex_preclip),            VER(0,0), Y, N, N, N },
317    { GL(SGIX_vertex_preclip_hint),       VER(0,0), Y, N, N, N },
318    { GL(SGIX_ycrcb),                     VER(0,0), Y, N, N, N },
319    { GL(SUN_convolution_border_modes),   VER(0,0), Y, N, N, N },
320    { GL(SUN_multi_draw_arrays),          VER(0,0), Y, N, Y, N },
321    { GL(SUN_slice_accum),                VER(0,0), Y, N, N, N },
322    { NULL }
323 };
324 /* *INDENT-ON* */
325 
326 
327 /* global bit-fields of available extensions and their characteristics */
328 static unsigned char client_glx_support[__GLX_EXT_BYTES];
329 static unsigned char client_glx_only[__GLX_EXT_BYTES];
330 static unsigned char direct_glx_only[__GLX_EXT_BYTES];
331 static unsigned char client_gl_support[__GL_EXT_BYTES];
332 static unsigned char client_gl_only[__GL_EXT_BYTES];
333 
334 /**
335  * Bits representing the set of extensions that are enabled by default in all
336  * direct rendering drivers.
337  */
338 static unsigned char direct_glx_support[__GLX_EXT_BYTES];
339 
340 /**
341  * Highest core GL version that can be supported for indirect rendering.
342  */
343 static const unsigned gl_major = 1;
344 static const unsigned gl_minor = 4;
345 
346 /* client extensions string */
347 static const char *__glXGLXClientExtensions = NULL;
348 
349 static void __glXExtensionsCtr(void);
350 static void __glXExtensionsCtrScreen(struct glx_screen * psc);
351 static void __glXProcessServerString(const struct extension_info *ext,
352                                      const char *server_string,
353                                      unsigned char *server_support);
354 
355 /**
356  * Find an extension in the list based on its name.
357  *
358  * \param ext       List of extensions where to search.
359  * \param name      Name of the extension.
360  * \param name_len  Length, in characters, of the extension name.
361  */
362 static const struct extension_info *
find_extension(const struct extension_info * ext,const char * name,unsigned name_len)363 find_extension(const struct extension_info *ext, const char *name,
364                unsigned name_len)
365 {
366    unsigned i;
367 
368    for (i = 0; ext[i].name != NULL; i++) {
369       if ((name_len == ext[i].name_len)
370           && (strncmp(ext[i].name, name, name_len) == 0)) {
371          return &ext[i];
372       }
373    }
374 
375    return NULL;
376 }
377 
378 /**
379  * Set the state of a GLX extension.
380  *
381  * \param name      Name of the extension.
382  * \param name_len  Length, in characters, of the extension name.
383  * \param state     New state (either enabled or disabled) of the extension.
384  * \param supported Table in which the state of the extension is to be set.
385  */
386 static void
set_glx_extension(const struct extension_info * ext_list,const char * name,unsigned name_len,GLboolean state,unsigned char * supported)387 set_glx_extension(const struct extension_info *ext_list,
388                   const char *name, unsigned name_len, GLboolean state,
389                   unsigned char *supported)
390 {
391    const struct extension_info *ext = find_extension(ext_list, name, name_len);
392    if (!ext)
393        return;
394 
395    if (state) {
396       SET_BIT(supported, ext->bit);
397    } else {
398       CLR_BIT(supported, ext->bit);
399    }
400 }
401 
402 
403 #define NUL '\0'
404 #define SEPARATOR ' '
405 
406 /**
407  * Convert the server's extension string to a bit-field.
408  *
409  * \param server_string   GLX extension string from the server.
410  * \param server_support  Bit-field of supported extensions.
411  *
412  * \note
413  * This function is used to process both GLX and GL extension strings.  The
414  * bit-fields used to track each of these have different sizes.  Therefore,
415  * the data pointed by \c server_support must be preinitialized to zero.
416  */
417 static void
__glXProcessServerString(const struct extension_info * ext,const char * server_string,unsigned char * server_support)418 __glXProcessServerString(const struct extension_info *ext,
419                          const char *server_string,
420                          unsigned char *server_support)
421 {
422    unsigned base;
423    unsigned len;
424 
425    for (base = 0; server_string[base] != NUL; /* empty */ ) {
426       /* Determine the length of the next extension name.
427        */
428       for (len = 0; (server_string[base + len] != SEPARATOR)
429            && (server_string[base + len] != NUL); len++) {
430          /* empty */
431       }
432 
433       /* Set the bit for the extension in the server_support table.
434        */
435       set_glx_extension(ext, &server_string[base], len, GL_TRUE,
436                         server_support);
437 
438 
439       /* Advance to the next extension string.  This means that we skip
440        * over the previous string and any trialing white-space.
441        */
442       for (base += len; (server_string[base] == SEPARATOR)
443            && (server_string[base] != NUL); base++) {
444          /* empty */
445       }
446    }
447 }
448 
449 void
__glXEnableDirectExtension(struct glx_screen * psc,const char * name)450 __glXEnableDirectExtension(struct glx_screen * psc, const char *name)
451 {
452    __glXExtensionsCtr();
453    __glXExtensionsCtrScreen(psc);
454 
455    set_glx_extension(known_glx_extensions,
456                      name, strlen(name), GL_TRUE, psc->direct_support);
457 }
458 
459 static void
__ParseExtensionOverride(struct glx_screen * psc,const struct extension_info * ext_list,unsigned char * force_enable,unsigned char * force_disable,const char * override)460 __ParseExtensionOverride(struct glx_screen *psc,
461                          const struct extension_info *ext_list,
462                          unsigned char *force_enable,
463                          unsigned char *force_disable,
464                          const char *override)
465 {
466    const struct extension_info *ext;
467    char *env, *field;
468 
469    if (override == NULL)
470        return;
471 
472    /* Copy env_const because strtok() is destructive. */
473    env = strdup(override);
474    if (env == NULL)
475       return;
476 
477    for (field = strtok(env, " "); field!= NULL; field = strtok(NULL, " ")) {
478       GLboolean enable;
479 
480       switch (field[0]) {
481       case '+':
482          enable = GL_TRUE;
483          ++field;
484          break;
485       case '-':
486          enable = GL_FALSE;
487          ++field;
488          break;
489       default:
490          enable = GL_TRUE;
491          break;
492       }
493 
494       ext = find_extension(ext_list, field, strlen(field));
495       if (ext) {
496          if (enable)
497             SET_BIT(force_enable, ext->bit);
498          else
499             SET_BIT(force_disable, ext->bit);
500       } else {
501          fprintf(stderr, "WARNING: Trying to %s the unknown extension '%s'\n",
502                  enable ? "enable" : "disable", field);
503       }
504    }
505 
506    free(env);
507 }
508 
509 /**
510  * \brief Parse the list of GLX extensions that the user wants to
511  * force-enable/disable by using \c override, and write the results to the
512  * screen's context.
513  *
514  * \param psc        Pointer to GLX per-screen record.
515  * \param override   A space-separated list of extensions to enable or disable.
516  * The list is processed thus:
517  *    - Enable recognized extension names that are prefixed with '+'.
518  *    - Disable recognized extension names that are prefixed with '-'.
519  *    - Enable recognized extension names that are not prefixed.
520  */
521 void
__glXParseExtensionOverride(struct glx_screen * psc,const char * override)522 __glXParseExtensionOverride(struct glx_screen *psc, const char *override)
523 {
524     __ParseExtensionOverride(psc, known_glx_extensions, psc->glx_force_enabled,
525                              psc->glx_force_disabled, override);
526 }
527 
528 /**
529  * \brief Parse the list of GL extensions that the user wants to
530  * force-enable/disable by using \c override, and write the results to the
531  * screen's context.
532  *
533  * \param psc        Pointer to GLX per-screen record.
534  * \param override   A space-separated list of extensions to enable or disable.
535  * The list is processed thus:
536  *    - Enable recognized extension names that are prefixed with '+'.
537  *    - Disable recognized extension names that are prefixed with '-'.
538  *    - Enable recognized extension names that are not prefixed.
539  */
540 void
__IndirectGlParseExtensionOverride(struct glx_screen * psc,const char * override)541 __IndirectGlParseExtensionOverride(struct glx_screen *psc, const char *override)
542 {
543     __ParseExtensionOverride(psc, known_gl_extensions, psc->gl_force_enabled,
544                              psc->gl_force_disabled, override);
545 }
546 
547 
548 /**
549  * Initialize global extension support tables.
550  */
551 
552 static void
__glXExtensionsCtr(void)553 __glXExtensionsCtr(void)
554 {
555    unsigned i;
556    static GLboolean ext_list_first_time = GL_TRUE;
557 
558 
559    if (ext_list_first_time) {
560       ext_list_first_time = GL_FALSE;
561 
562       (void) memset(client_glx_support, 0, sizeof(client_glx_support));
563       (void) memset(direct_glx_support, 0, sizeof(direct_glx_support));
564       (void) memset(client_glx_only, 0, sizeof(client_glx_only));
565       (void) memset(direct_glx_only, 0, sizeof(direct_glx_only));
566 
567       (void) memset(client_gl_support, 0, sizeof(client_gl_support));
568       (void) memset(client_gl_only, 0, sizeof(client_gl_only));
569 
570       for (i = 0; known_glx_extensions[i].name != NULL; i++) {
571          const unsigned bit = known_glx_extensions[i].bit;
572 
573          if (known_glx_extensions[i].client_support) {
574             SET_BIT(client_glx_support, bit);
575          }
576 
577          if (known_glx_extensions[i].direct_support) {
578             SET_BIT(direct_glx_support, bit);
579          }
580 
581          if (known_glx_extensions[i].client_only) {
582             SET_BIT(client_glx_only, bit);
583          }
584 
585          if (known_glx_extensions[i].direct_only) {
586             SET_BIT(direct_glx_only, bit);
587          }
588       }
589 
590       for (i = 0; known_gl_extensions[i].name != NULL; i++) {
591          const unsigned bit = known_gl_extensions[i].bit;
592 
593          if (known_gl_extensions[i].client_support) {
594             SET_BIT(client_gl_support, bit);
595          }
596 
597          if (known_gl_extensions[i].client_only) {
598             SET_BIT(client_gl_only, bit);
599          }
600       }
601 
602 #if 0
603       fprintf(stderr, "[%s:%u] Maximum client library version: %u.%u\n",
604               __func__, __LINE__, gl_major, gl_minor);
605 #endif
606    }
607 }
608 
609 
610 /**
611  * Make sure that per-screen direct-support table is initialized.
612  *
613  * \param psc  Pointer to GLX per-screen record.
614  */
615 
616 static void
__glXExtensionsCtrScreen(struct glx_screen * psc)617 __glXExtensionsCtrScreen(struct glx_screen * psc)
618 {
619    if (psc->ext_list_first_time) {
620       psc->ext_list_first_time = GL_FALSE;
621       (void) memcpy(psc->direct_support, direct_glx_support,
622                     sizeof(direct_glx_support));
623       (void) memset(psc->glx_force_enabled, 0,
624                     sizeof(psc->glx_force_enabled));
625       (void) memset(psc->glx_force_disabled, 0,
626                     sizeof(psc->glx_force_disabled));
627       (void) memset(psc->gl_force_enabled, 0,
628                     sizeof(psc->gl_force_enabled));
629       (void) memset(psc->gl_force_disabled, 0,
630                     sizeof(psc->gl_force_disabled));
631    }
632 }
633 
634 
635 /**
636  * Check if a certain extension is enabled on a given screen.
637  *
638  * \param psc  Pointer to GLX per-screen record.
639  * \param bit  Bit index in the direct-support table.
640  * \returns If the extension bit is enabled for the screen, \c GL_TRUE is
641  *          returned.  If the extension bit is not enabled or if \c psc is
642  *          \c NULL, then \c GL_FALSE is returned.
643  */
644 GLboolean
__glXExtensionBitIsEnabled(struct glx_screen * psc,unsigned bit)645 __glXExtensionBitIsEnabled(struct glx_screen * psc, unsigned bit)
646 {
647    GLboolean enabled = GL_FALSE;
648 
649    if (psc != NULL) {
650       __glXExtensionsCtr();
651       __glXExtensionsCtrScreen(psc);
652       enabled = EXT_ENABLED(bit, psc->direct_support);
653    }
654 
655    return enabled;
656 }
657 
658 
659 /**
660  * Check if a certain extension is enabled in a given context.
661  *
662  */
663 GLboolean
__glExtensionBitIsEnabled(struct glx_context * gc,unsigned bit)664 __glExtensionBitIsEnabled(struct glx_context *gc, unsigned bit)
665 {
666    GLboolean enabled = GL_FALSE;
667 
668    if (gc != NULL) {
669       enabled = EXT_ENABLED(bit, gc->gl_extension_bits);
670    }
671 
672    return enabled;
673 }
674 
675 
676 
677 /**
678  * Convert a bit-field to a string of supported extensions.
679  */
680 static char *
__glXGetStringFromTable(const struct extension_info * ext,const unsigned char * supported)681 __glXGetStringFromTable(const struct extension_info *ext,
682                         const unsigned char *supported)
683 {
684    unsigned i;
685    unsigned ext_str_len;
686    char *ext_str;
687    char *point;
688 
689 
690    ext_str_len = 0;
691    for (i = 0; ext[i].name != NULL; i++) {
692       if (EXT_ENABLED(ext[i].bit, supported)) {
693          ext_str_len += ext[i].name_len + 1;
694       }
695    }
696 
697    ext_str = malloc(ext_str_len + 1);
698    if (ext_str != NULL) {
699       point = ext_str;
700 
701       for (i = 0; ext[i].name != NULL; i++) {
702          if (EXT_ENABLED(ext[i].bit, supported)) {
703             (void) memcpy(point, ext[i].name, ext[i].name_len);
704             point += ext[i].name_len;
705 
706             *point = ' ';
707             point++;
708          }
709       }
710 
711       *point = '\0';
712    }
713 
714    return ext_str;
715 }
716 
717 
718 /**
719  * Get the string of client library supported extensions.
720  */
721 const char *
__glXGetClientExtensions(void)722 __glXGetClientExtensions(void)
723 {
724    if (__glXGLXClientExtensions == NULL) {
725       __glXExtensionsCtr();
726       __glXGLXClientExtensions = __glXGetStringFromTable(known_glx_extensions,
727                                                          client_glx_support);
728    }
729 
730    return __glXGLXClientExtensions;
731 }
732 
733 
734 /**
735  * Calculate the list of application usable extensions.  The resulting
736  * string is stored in \c psc->effectiveGLXexts.
737  *
738  * \param psc                        Pointer to GLX per-screen record.
739  * \param display_is_direct_capable  True if the display is capable of
740  *                                   direct rendering.
741  * \param minor_version              GLX minor version from the server.
742  */
743 
744 void
__glXCalculateUsableExtensions(struct glx_screen * psc,GLboolean display_is_direct_capable,int minor_version)745 __glXCalculateUsableExtensions(struct glx_screen * psc,
746                                GLboolean display_is_direct_capable,
747                                int minor_version)
748 {
749    unsigned char server_support[__GLX_EXT_BYTES];
750    unsigned char usable[__GLX_EXT_BYTES];
751    unsigned i;
752 
753    __glXExtensionsCtr();
754    __glXExtensionsCtrScreen(psc);
755 
756    (void) memset(server_support, 0, sizeof(server_support));
757    __glXProcessServerString(known_glx_extensions,
758                             psc->serverGLXexts, server_support);
759 
760 
761    /* This is a hack.  Some servers support GLX 1.3 but don't export
762     * all of the extensions implied by GLX 1.3.  If the server claims
763     * support for GLX 1.3, enable support for the extensions that can be
764     * "emulated" as well.
765     */
766 #ifndef GLX_USE_APPLEGL
767    if (minor_version >= 3) {
768       SET_BIT(server_support, EXT_visual_info_bit);
769       SET_BIT(server_support, EXT_visual_rating_bit);
770       SET_BIT(server_support, SGI_make_current_read_bit);
771       SET_BIT(server_support, SGIX_fbconfig_bit);
772       SET_BIT(server_support, SGIX_pbuffer_bit);
773 
774       /* This one is a little iffy.  GLX 1.3 doesn't incorporate all of this
775        * extension.  However, the only part that is not strictly client-side
776        * is shared.  That's the glXQueryContext / glXQueryContextInfoEXT
777        * function.
778        */
779 
780       SET_BIT(server_support, EXT_import_context_bit);
781    }
782 #endif
783 
784    /* An extension is supported if the client-side (i.e., libGL) supports
785     * it and the "server" supports it.  In this case that means that either
786     * the true server supports it or it is only for direct-rendering and
787     * the direct rendering driver supports it.
788     *
789     * If the display is not capable of direct rendering, then the extension
790     * is enabled if and only if the client-side library and the server
791     * support it.
792     */
793 
794    if (display_is_direct_capable) {
795       for (i = 0; i < __GLX_EXT_BYTES; i++) {
796          /* Enable extensions that the client supports that only have a client-side
797           * component.
798           */
799          unsigned char u = client_glx_support[i] & client_glx_only[i];
800 
801          /* Enable extensions that the client supports, are supported for direct
802           * rendering, and either are supported by the server or only have a
803           * direct-rendering component.
804           */
805          u |= client_glx_support[i] & psc->direct_support[i] &
806                  (server_support[i] | direct_glx_only[i]);
807 
808          /* Finally, apply driconf options to force some extension bits either
809           * enabled or disabled.
810           */
811          u |= psc->glx_force_enabled[i];
812          u &= ~psc->glx_force_disabled[i];
813 
814          usable[i] = u;
815       }
816    }
817    else {
818       for (i = 0; i < __GLX_EXT_BYTES; i++) {
819          /* Enable extensions that the client supports that only have a
820           * client-side component.
821           */
822          unsigned char u = client_glx_support[i] & client_glx_only[i];
823 
824          /* Enable extensions that the client and server both support */
825          u |= client_glx_support[i] & server_support[i];
826 
827          /* Finally, apply driconf options to force some extension bits either
828           * enabled or disabled.
829           */
830          u |= psc->glx_force_enabled[i];
831          u &= ~psc->glx_force_disabled[i];
832 
833          usable[i] = u;
834       }
835    }
836 
837    psc->effectiveGLXexts = __glXGetStringFromTable(known_glx_extensions,
838                                                    usable);
839 }
840 
841 
842 /**
843  * Calculate the list of application usable extensions.  The resulting
844  * string is stored in \c gc->extensions.
845  *
846  * \param gc             Pointer to GLX context.
847  * \param server_string  Extension string from the server.
848  * \param major_version  GL major version from the server.
849  * \param minor_version  GL minor version from the server.
850  */
851 
852 void
__glXCalculateUsableGLExtensions(struct glx_context * gc,const char * server_string,int major_version,int minor_version)853 __glXCalculateUsableGLExtensions(struct glx_context * gc,
854                                  const char *server_string,
855                                  int major_version, int minor_version)
856 {
857    struct glx_screen *psc = gc->psc;
858    unsigned char server_support[__GL_EXT_BYTES];
859    unsigned char usable[__GL_EXT_BYTES];
860    unsigned i;
861 
862 
863    __glXExtensionsCtr();
864 
865    (void) memset(server_support, 0, sizeof(server_support));
866    __glXProcessServerString(known_gl_extensions, server_string,
867                             server_support);
868 
869 
870    /* Handle lazy servers that don't export all the extensions strings that
871     * are part of the GL core version that they support.
872     */
873 
874    for (i = 0; i < __GL_EXT_BYTES; i++) {
875       if ((known_gl_extensions[i].version_major != 0)
876           && ((major_version > known_gl_extensions[i].version_major)
877               || ((major_version == known_gl_extensions[i].version_major)
878                   && (minor_version >=
879                       known_gl_extensions[i].version_minor)))) {
880          SET_BIT(server_support, known_gl_extensions[i].bit);
881       }
882    }
883 
884 
885    /* An extension is supported if the client-side (i.e., libGL) supports
886     * it and the server supports it or the client-side library supports it
887     * and it only needs client-side support.
888     */
889 
890    for (i = 0; i < __GL_EXT_BYTES; i++) {
891       usable[i] = ((client_gl_support[i] & client_gl_only[i])
892          | (client_gl_support[i] & server_support[i])
893          | psc->gl_force_enabled[i]) & ~psc->gl_force_disabled[i];
894    }
895 
896    gc->extensions = (unsigned char *)
897       __glXGetStringFromTable(known_gl_extensions, usable);
898    (void) memcpy(gc->gl_extension_bits, usable, sizeof(usable));
899 }
900 
901 
902 /**
903  * Calculates the maximum core GL version that can be supported for indirect
904  * rendering.
905  */
906 void
__glXGetGLVersion(int * major_version,int * minor_version)907 __glXGetGLVersion(int *major_version, int *minor_version)
908 {
909    __glXExtensionsCtr();
910    *major_version = gl_major;
911    *minor_version = gl_minor;
912 }
913 
914 
915 /**
916  * Get a string representing the set of extensions supported by the client
917  * library.  This is currently only used to send the list of extensions
918  * supported by the client to the server.
919  */
920 char *
__glXGetClientGLExtensionString(void)921 __glXGetClientGLExtensionString(void)
922 {
923    __glXExtensionsCtr();
924    return __glXGetStringFromTable(known_gl_extensions, client_gl_support);
925 }
926