9ddc39d98ca77ddd254cdd2fbf5b023d2305d93c
[profile/ivi/mesa.git] / src / glx / glxextensions.c
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
38 #define SET_BIT(m,b)   (m[ (b) / 8 ] |=  (1U << ((b) % 8)))
39 #define CLR_BIT(m,b)   (m[ (b) / 8 ] &= ~(1U << ((b) % 8)))
40 #define IS_SET(m,b)    ((m[ (b) / 8 ] & (1U << ((b) % 8))) != 0)
41 #define CONCAT(a,b) a ## b
42 #define GLX(n) "GLX_" # n, 4 + sizeof( # n ) - 1, CONCAT(n,_bit)
43 #define GL(n)  "GL_" # n,  3 + sizeof( # n ) - 1, GL_ ## n ## _bit
44 #define VER(a,b)  a, b
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    /* This is the lowest version of GLX that "requires" this extension.
58     * For example, GLX 1.3 requires SGIX_fbconfig, SGIX_pbuffer, and
59     * SGI_make_current_read.  If the extension is not required by any known
60     * version of GLX, use 0, 0.
61     */
62    unsigned char version_major;
63    unsigned char version_minor;
64    unsigned char client_support;
65    unsigned char direct_support;
66    unsigned char client_only;        /** Is the extension client-side only? */
67    unsigned char direct_only;        /** Is the extension for direct
68                                       * contexts only?
69                                       */
70 };
71
72 /* *INDENT-OFF* */
73 static const struct extension_info known_glx_extensions[] = {
74 #ifdef HAVE_XCB_GLX_CREATE_CONTEXT
75    { GLX(ARB_create_context),          VER(0,0), Y, N, N, N },
76    { GLX(ARB_create_context_profile),  VER(0,0), Y, N, N, N },
77    { GLX(ARB_create_context_robustness), VER(0,0), Y, N, N, N },
78 #else
79    { GLX(ARB_create_context),          VER(0,0), N, N, N, N },
80    { GLX(ARB_create_context_profile),  VER(0,0), N, N, N, N },
81    { GLX(ARB_create_context_robustness), VER(0,0), N, N, N, N },
82 #endif
83    { GLX(ARB_get_proc_address),        VER(1,4), Y, N, Y, N },
84    { GLX(ARB_multisample),             VER(1,4), Y, Y, N, N },
85    { GLX(ATI_pixel_format_float),      VER(0,0), N, N, N, N },
86    { GLX(EXT_import_context),          VER(0,0), Y, Y, N, N },
87    { GLX(EXT_visual_info),             VER(0,0), Y, Y, N, N },
88    { GLX(EXT_visual_rating),           VER(0,0), Y, Y, N, N },
89    { GLX(EXT_framebuffer_sRGB),        VER(0,0), Y, Y, N, N },
90 #ifdef HAVE_XCB_GLX_CREATE_CONTEXT
91    { GLX(EXT_create_context_es2_profile), VER(0,0), Y, N, N, Y },
92 #else
93    { GLX(EXT_create_context_es2_profile), VER(0,0), N, N, N, N },
94 #endif
95    { GLX(MESA_copy_sub_buffer),        VER(0,0), Y, N, N, N },
96    { GLX(MESA_multithread_makecurrent),VER(0,0), Y, N, Y, N },
97    { GLX(MESA_swap_control),           VER(0,0), Y, N, N, Y },
98    { GLX(NV_float_buffer),             VER(0,0), N, N, N, N },
99    { GLX(OML_swap_method),             VER(0,0), Y, Y, N, N },
100    { GLX(OML_sync_control),            VER(0,0), Y, N, N, Y },
101    { GLX(SGI_make_current_read),       VER(1,3), Y, N, N, N },
102    { GLX(SGI_swap_control),            VER(0,0), Y, N, N, N },
103    { GLX(SGI_video_sync),              VER(0,0), Y, N, N, Y },
104    { GLX(SGIS_multisample),            VER(0,0), Y, Y, N, N },
105    { GLX(SGIX_fbconfig),               VER(1,3), Y, Y, N, N },
106    { GLX(SGIX_pbuffer),                VER(1,3), Y, Y, N, N },
107    { GLX(SGIX_swap_barrier),           VER(0,0), N, N, N, N },
108    { GLX(SGIX_swap_group),             VER(0,0), N, N, N, N },
109    { GLX(SGIX_visual_select_group),    VER(0,0), Y, Y, N, N },
110    { GLX(EXT_texture_from_pixmap),     VER(0,0), Y, N, N, N },
111    { GLX(INTEL_swap_event),            VER(0,0), Y, N, N, N },
112    { NULL }
113 };
114
115 static const struct extension_info known_gl_extensions[] = {
116    { GL(ARB_depth_texture),              VER(1,4), Y, N, N, N },
117    { GL(ARB_draw_buffers),               VER(0,0), Y, N, N, N },
118    { GL(ARB_fragment_program),           VER(0,0), Y, N, N, N },
119    { GL(ARB_fragment_program_shadow),    VER(0,0), Y, N, N, N },
120    { GL(ARB_framebuffer_object),         VER(0,0), Y, N, N, N },
121    { GL(ARB_imaging),                    VER(0,0), Y, N, N, N },
122    { GL(ARB_multisample),                VER(1,3), Y, N, N, N },
123    { GL(ARB_multitexture),               VER(1,3), Y, N, N, N },
124    { GL(ARB_occlusion_query),            VER(1,5), Y, N, N, N },
125    { GL(ARB_point_parameters),           VER(1,4), Y, N, N, N },
126    { GL(ARB_point_sprite),               VER(0,0), Y, N, N, N },
127    { GL(ARB_shadow),                     VER(1,4), Y, N, N, N },
128    { GL(ARB_shadow_ambient),             VER(0,0), Y, N, N, N },
129    { GL(ARB_texture_border_clamp),       VER(1,3), Y, N, N, N },
130    { GL(ARB_texture_compression),        VER(1,3), Y, N, N, N },
131    { GL(ARB_texture_cube_map),           VER(1,3), Y, N, N, N },
132    { GL(ARB_texture_env_add),            VER(1,3), Y, N, N, N },
133    { GL(ARB_texture_env_combine),        VER(1,3), Y, N, N, N },
134    { GL(ARB_texture_env_crossbar),       VER(1,4), Y, N, N, N },
135    { GL(ARB_texture_env_dot3),           VER(1,3), Y, N, N, N },
136    { GL(ARB_texture_mirrored_repeat),    VER(1,4), Y, N, N, N },
137    { GL(ARB_texture_non_power_of_two),   VER(1,5), Y, N, N, N },
138    { GL(ARB_texture_rectangle),          VER(0,0), Y, N, N, N },
139    { GL(ARB_texture_rg),                 VER(0,0), Y, N, N, N },
140    { GL(ARB_transpose_matrix),           VER(1,3), Y, N, Y, N },
141    { GL(ARB_vertex_buffer_object),       VER(1,5), N, N, N, N },
142    { GL(ARB_vertex_program),             VER(0,0), Y, N, N, N },
143    { GL(ARB_window_pos),                 VER(1,4), Y, N, N, N },
144    { GL(EXT_abgr),                       VER(0,0), Y, N, N, N },
145    { GL(EXT_bgra),                       VER(1,2), Y, N, N, N },
146    { GL(EXT_blend_color),                VER(1,4), Y, N, N, N },
147    { GL(EXT_blend_equation_separate),    VER(0,0), Y, N, N, N },
148    { GL(EXT_blend_func_separate),        VER(1,4), Y, N, N, N },
149    { GL(EXT_blend_logic_op),             VER(1,4), Y, N, N, N },
150    { GL(EXT_blend_minmax),               VER(1,4), Y, N, N, N },
151    { GL(EXT_blend_subtract),             VER(1,4), Y, N, N, N },
152    { GL(EXT_clip_volume_hint),           VER(0,0), Y, N, N, N },
153    { GL(EXT_compiled_vertex_array),      VER(0,0), N, N, N, N },
154    { GL(EXT_convolution),                VER(0,0), N, N, N, N },
155    { GL(EXT_copy_texture),               VER(1,1), Y, N, N, N },
156    { GL(EXT_cull_vertex),                VER(0,0), N, N, N, N },
157    { GL(EXT_depth_bounds_test),          VER(0,0), N, N, N, N },
158    { GL(EXT_draw_range_elements),        VER(1,2), Y, N, Y, N },
159    { GL(EXT_fog_coord),                  VER(1,4), Y, N, N, N },
160    { GL(EXT_framebuffer_blit),           VER(0,0), Y, N, N, N },
161    { GL(EXT_framebuffer_multisample),    VER(0,0), Y, N, N, N },
162    { GL(EXT_framebuffer_object),         VER(0,0), Y, N, N, N },
163    { GL(EXT_framebuffer_sRGB),           VER(0,0), Y, N, N, N },
164    { GL(EXT_multi_draw_arrays),          VER(1,4), Y, N, Y, N },
165    { GL(EXT_packed_depth_stencil),       VER(0,0), Y, N, N, N },
166    { GL(EXT_packed_pixels),              VER(1,2), Y, N, N, N },
167    { GL(EXT_paletted_texture),           VER(0,0), Y, N, N, N },
168    { GL(EXT_pixel_buffer_object),        VER(0,0), N, N, N, N },
169    { GL(EXT_point_parameters),           VER(1,4), Y, N, N, N },
170    { GL(EXT_polygon_offset),             VER(1,1), Y, N, N, N },
171    { GL(EXT_rescale_normal),             VER(1,2), Y, N, N, N },
172    { GL(EXT_secondary_color),            VER(1,4), Y, N, N, N },
173    { GL(EXT_separate_specular_color),    VER(1,2), Y, N, N, N },
174    { GL(EXT_shadow_funcs),               VER(1,5), Y, N, N, N },
175    { GL(EXT_shared_texture_palette),     VER(0,0), Y, N, N, N },
176    { GL(EXT_stencil_two_side),           VER(0,0), Y, N, N, N },
177    { GL(EXT_stencil_wrap),               VER(1,4), Y, N, N, N },
178    { GL(EXT_subtexture),                 VER(1,1), Y, N, N, N },
179    { GL(EXT_texture),                    VER(1,1), Y, N, N, N },
180    { GL(EXT_texture3D),                  VER(1,2), Y, N, N, N },
181    { GL(EXT_texture_compression_dxt1),   VER(0,0), Y, N, N, N },
182    { GL(EXT_texture_compression_s3tc),   VER(0,0), Y, N, N, N },
183    { GL(EXT_texture_edge_clamp),         VER(1,2), Y, N, N, N },
184    { GL(EXT_texture_env_add),            VER(1,3), Y, N, N, N },
185    { GL(EXT_texture_env_combine),        VER(1,3), Y, N, N, N },
186    { GL(EXT_texture_env_dot3),           VER(0,0), Y, N, N, N },
187    { GL(EXT_texture_filter_anisotropic), VER(0,0), Y, N, N, N },
188    { GL(EXT_texture_lod),                VER(1,2), Y, N, N, N },
189    { GL(EXT_texture_lod_bias),           VER(1,4), Y, N, N, N },
190    { GL(EXT_texture_mirror_clamp),       VER(0,0), Y, N, N, N },
191    { GL(EXT_texture_object),             VER(1,1), Y, N, N, N },
192    { GL(EXT_texture_rectangle),          VER(0,0), Y, N, N, N },
193    { GL(EXT_vertex_array),               VER(0,0), Y, N, N, N },
194    { GL(3DFX_texture_compression_FXT1),  VER(0,0), Y, N, N, N },
195    { GL(APPLE_packed_pixels),            VER(1,2), Y, N, N, N },
196    { GL(APPLE_ycbcr_422),                VER(0,0), Y, N, N, N },
197    { GL(ATI_draw_buffers),               VER(0,0), Y, N, N, N },
198    { GL(ATI_text_fragment_shader),       VER(0,0), Y, N, N, N },
199    { GL(ATI_texture_env_combine3),       VER(0,0), Y, N, N, N },
200    { GL(ATI_texture_float),              VER(0,0), Y, N, N, N },
201    { GL(ATI_texture_mirror_once),        VER(0,0), Y, N, N, N },
202    { GL(ATIX_texture_env_combine3),      VER(0,0), Y, N, N, N },
203    { GL(HP_convolution_border_modes),    VER(0,0), Y, N, N, N },
204    { GL(HP_occlusion_test),              VER(0,0), Y, N, N, N },
205    { GL(IBM_cull_vertex),                VER(0,0), Y, N, N, N },
206    { GL(IBM_pixel_filter_hint),          VER(0,0), Y, N, N, N },
207    { GL(IBM_rasterpos_clip),             VER(0,0), Y, N, N, N },
208    { GL(IBM_texture_clamp_nodraw),       VER(0,0), Y, N, N, N },
209    { GL(IBM_texture_mirrored_repeat),    VER(0,0), Y, N, N, N },
210    { GL(INGR_blend_func_separate),       VER(0,0), Y, N, N, N },
211    { GL(INGR_interlace_read),            VER(0,0), Y, N, N, N },
212    { GL(MESA_pack_invert),               VER(0,0), Y, N, N, N },
213    { GL(MESA_ycbcr_texture),             VER(0,0), Y, N, N, N },
214    { GL(NV_blend_square),                VER(1,4), Y, N, N, N },
215    { GL(NV_copy_depth_to_color),         VER(0,0), Y, N, N, N },
216    { GL(NV_depth_clamp),                 VER(0,0), Y, N, N, N },
217    { GL(NV_fog_distance),                VER(0,0), Y, N, N, N },
218    { GL(NV_fragment_program),            VER(0,0), Y, N, N, N },
219    { GL(NV_fragment_program_option),     VER(0,0), Y, N, N, N },
220    { GL(NV_fragment_program2),           VER(0,0), Y, N, N, N },
221    { GL(NV_light_max_exponent),          VER(0,0), Y, N, N, N },
222    { GL(NV_multisample_filter_hint),     VER(0,0), Y, N, N, N },
223    { GL(NV_packed_depth_stencil),        VER(0,0), Y, N, N, N },
224    { GL(NV_point_sprite),                VER(0,0), Y, N, N, N },
225    { GL(NV_texgen_reflection),           VER(0,0), Y, N, N, N },
226    { GL(NV_texture_compression_vtc),     VER(0,0), Y, N, N, N },
227    { GL(NV_texture_env_combine4),        VER(0,0), Y, N, N, N },
228    { GL(NV_texture_rectangle),           VER(0,0), Y, N, N, N },
229    { GL(NV_vertex_program),              VER(0,0), Y, N, N, N },
230    { GL(NV_vertex_program1_1),           VER(0,0), Y, N, N, N },
231    { GL(NV_vertex_program2),             VER(0,0), Y, N, N, N },
232    { GL(NV_vertex_program2_option),      VER(0,0), Y, N, N, N },
233    { GL(NV_vertex_program3),             VER(0,0), Y, N, N, N },
234    { GL(OES_read_format),                VER(0,0), Y, N, N, N },
235    { GL(OES_compressed_paletted_texture),VER(0,0), Y, N, N, N },
236    { GL(SGI_color_matrix),               VER(0,0), Y, N, N, N },
237    { GL(SGI_color_table),                VER(0,0), Y, N, N, N },
238    { GL(SGI_texture_color_table),        VER(0,0), Y, N, N, N },
239    { GL(SGIS_generate_mipmap),           VER(1,4), Y, N, N, N },
240    { GL(SGIS_multisample),               VER(0,0), Y, N, N, N },
241    { GL(SGIS_texture_border_clamp),      VER(1,3), Y, N, N, N },
242    { GL(SGIS_texture_edge_clamp),        VER(1,2), Y, N, N, N },
243    { GL(SGIS_texture_lod),               VER(1,2), Y, N, N, N },
244    { GL(SGIX_blend_alpha_minmax),        VER(0,0), Y, N, N, N },
245    { GL(SGIX_clipmap),                   VER(0,0), Y, N, N, N },
246    { GL(SGIX_depth_texture),             VER(0,0), Y, N, N, N },
247    { GL(SGIX_fog_offset),                VER(0,0), Y, N, N, N },
248    { GL(SGIX_shadow),                    VER(0,0), Y, N, N, N },
249    { GL(SGIX_shadow_ambient),            VER(0,0), Y, N, N, N },
250    { GL(SGIX_texture_coordinate_clamp),  VER(0,0), Y, N, N, N },
251    { GL(SGIX_texture_lod_bias),          VER(0,0), Y, N, N, N },
252    { GL(SGIX_texture_range),             VER(0,0), Y, N, N, N },
253    { GL(SGIX_texture_scale_bias),        VER(0,0), Y, N, N, N },
254    { GL(SGIX_vertex_preclip),            VER(0,0), Y, N, N, N },
255    { GL(SGIX_vertex_preclip_hint),       VER(0,0), Y, N, N, N },
256    { GL(SGIX_ycrcb),                     VER(0,0), Y, N, N, N },
257    { GL(SUN_convolution_border_modes),   VER(0,0), Y, N, N, N },
258    { GL(SUN_multi_draw_arrays),          VER(0,0), Y, N, Y, N },
259    { GL(SUN_slice_accum),                VER(0,0), Y, N, N, N },
260    { NULL }
261 };
262 /* *INDENT-ON* */
263
264
265 /* global bit-fields of available extensions and their characteristics */
266 static unsigned char client_glx_support[8];
267 static unsigned char client_glx_only[8];
268 static unsigned char direct_glx_only[8];
269 static unsigned char client_gl_support[__GL_EXT_BYTES];
270 static unsigned char client_gl_only[__GL_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[8];
277
278 /**
279  * Highest core GL version that can be supported for indirect rendering.
280  */
281 static const unsigned gl_major = 1;
282 static const unsigned gl_minor = 4;
283
284 /* client extensions string */
285 static const char *__glXGLXClientExtensions = NULL;
286
287 static void __glXExtensionsCtr(void);
288 static void __glXExtensionsCtrScreen(struct glx_screen * psc);
289 static void __glXProcessServerString(const struct extension_info *ext,
290                                      const char *server_string,
291                                      unsigned char *server_support);
292
293 /**
294  * Set the state of a GLX extension.
295  *
296  * \param name      Name of the extension.
297  * \param name_len  Length, in characters, of the extension name.
298  * \param state     New state (either enabled or disabled) of the extension.
299  * \param supported Table in which the state of the extension is to be set.
300  */
301 static void
302 set_glx_extension(const struct extension_info *ext,
303                   const char *name, unsigned name_len, GLboolean state,
304                   unsigned char *supported)
305 {
306    unsigned i;
307
308
309    for (i = 0; ext[i].name != NULL; i++) {
310       if ((name_len == ext[i].name_len)
311           && (strncmp(ext[i].name, name, name_len) == 0)) {
312          if (state) {
313             SET_BIT(supported, ext[i].bit);
314          }
315          else {
316             CLR_BIT(supported, ext[i].bit);
317          }
318
319          return;
320       }
321    }
322 }
323
324
325 #define NUL '\0'
326 #define SEPARATOR ' '
327
328 /**
329  * Convert the server's extension string to a bit-field.
330  *
331  * \param server_string   GLX extension string from the server.
332  * \param server_support  Bit-field of supported extensions.
333  *
334  * \note
335  * This function is used to process both GLX and GL extension strings.  The
336  * bit-fields used to track each of these have different sizes.  Therefore,
337  * the data pointed by \c server_support must be preinitialized to zero.
338  */
339 static void
340 __glXProcessServerString(const struct extension_info *ext,
341                          const char *server_string,
342                          unsigned char *server_support)
343 {
344    unsigned base;
345    unsigned len;
346
347    for (base = 0; server_string[base] != NUL; /* empty */ ) {
348       /* Determine the length of the next extension name.
349        */
350       for (len = 0; (server_string[base + len] != SEPARATOR)
351            && (server_string[base + len] != NUL); len++) {
352          /* empty */
353       }
354
355       /* Set the bit for the extension in the server_support table.
356        */
357       set_glx_extension(ext, &server_string[base], len, GL_TRUE,
358                         server_support);
359
360
361       /* Advance to the next extension string.  This means that we skip
362        * over the previous string and any trialing white-space.
363        */
364       for (base += len; (server_string[base] == SEPARATOR)
365            && (server_string[base] != NUL); base++) {
366          /* empty */
367       }
368    }
369 }
370
371 void
372 __glXEnableDirectExtension(struct glx_screen * psc, const char *name)
373 {
374    __glXExtensionsCtr();
375    __glXExtensionsCtrScreen(psc);
376
377    set_glx_extension(known_glx_extensions,
378                      name, strlen(name), GL_TRUE, psc->direct_support);
379 }
380
381 /**
382  * Initialize global extension support tables.
383  */
384
385 static void
386 __glXExtensionsCtr(void)
387 {
388    unsigned i;
389    static GLboolean ext_list_first_time = GL_TRUE;
390
391
392    if (ext_list_first_time) {
393       ext_list_first_time = GL_FALSE;
394
395       (void) memset(client_glx_support, 0, sizeof(client_glx_support));
396       (void) memset(direct_glx_support, 0, sizeof(direct_glx_support));
397       (void) memset(client_glx_only, 0, sizeof(client_glx_only));
398       (void) memset(direct_glx_only, 0, sizeof(direct_glx_only));
399
400       (void) memset(client_gl_support, 0, sizeof(client_gl_support));
401       (void) memset(client_gl_only, 0, sizeof(client_gl_only));
402
403       for (i = 0; known_glx_extensions[i].name != NULL; i++) {
404          const unsigned bit = known_glx_extensions[i].bit;
405
406          if (known_glx_extensions[i].client_support) {
407             SET_BIT(client_glx_support, bit);
408          }
409
410          if (known_glx_extensions[i].direct_support) {
411             SET_BIT(direct_glx_support, bit);
412          }
413
414          if (known_glx_extensions[i].client_only) {
415             SET_BIT(client_glx_only, bit);
416          }
417
418          if (known_glx_extensions[i].direct_only) {
419             SET_BIT(direct_glx_only, bit);
420          }
421       }
422
423       for (i = 0; known_gl_extensions[i].name != NULL; i++) {
424          const unsigned bit = known_gl_extensions[i].bit;
425
426          if (known_gl_extensions[i].client_support) {
427             SET_BIT(client_gl_support, bit);
428          }
429
430          if (known_gl_extensions[i].client_only) {
431             SET_BIT(client_gl_only, bit);
432          }
433       }
434
435 #if 0
436       fprintf(stderr, "[%s:%u] Maximum client library version: %u.%u\n",
437               __func__, __LINE__, gl_major, gl_minor);
438 #endif
439    }
440 }
441
442
443 /**
444  * Make sure that per-screen direct-support table is initialized.
445  *
446  * \param psc  Pointer to GLX per-screen record.
447  */
448
449 static void
450 __glXExtensionsCtrScreen(struct glx_screen * psc)
451 {
452    if (psc->ext_list_first_time) {
453       psc->ext_list_first_time = GL_FALSE;
454       (void) memcpy(psc->direct_support, direct_glx_support,
455                     sizeof(direct_glx_support));
456    }
457 }
458
459
460 /**
461  * Check if a certain extension is enabled on a given screen.
462  *
463  * \param psc  Pointer to GLX per-screen record.
464  * \param bit  Bit index in the direct-support table.
465  * \returns If the extension bit is enabled for the screen, \c GL_TRUE is
466  *          returned.  If the extension bit is not enabled or if \c psc is
467  *          \c NULL, then \c GL_FALSE is returned.
468  */
469 GLboolean
470 __glXExtensionBitIsEnabled(struct glx_screen * psc, unsigned bit)
471 {
472    GLboolean enabled = GL_FALSE;
473
474    if (psc != NULL) {
475       __glXExtensionsCtr();
476       __glXExtensionsCtrScreen(psc);
477       enabled = EXT_ENABLED(bit, psc->direct_support);
478    }
479
480    return enabled;
481 }
482
483
484 /**
485  * Check if a certain extension is enabled in a given context.
486  *
487  */
488 GLboolean
489 __glExtensionBitIsEnabled(struct glx_context *gc, unsigned bit)
490 {
491    GLboolean enabled = GL_FALSE;
492
493    if (gc != NULL) {
494       enabled = EXT_ENABLED(bit, gc->gl_extension_bits);
495    }
496
497    return enabled;
498 }
499
500
501
502 /**
503  * Convert a bit-field to a string of supported extensions.
504  */
505 static char *
506 __glXGetStringFromTable(const struct extension_info *ext,
507                         const unsigned char *supported)
508 {
509    unsigned i;
510    unsigned ext_str_len;
511    char *ext_str;
512    char *point;
513
514
515    ext_str_len = 0;
516    for (i = 0; ext[i].name != NULL; i++) {
517       if (EXT_ENABLED(ext[i].bit, supported)) {
518          ext_str_len += ext[i].name_len + 1;
519       }
520    }
521
522    ext_str = Xmalloc(ext_str_len + 1);
523    if (ext_str != NULL) {
524       point = ext_str;
525
526       for (i = 0; ext[i].name != NULL; i++) {
527          if (EXT_ENABLED(ext[i].bit, supported)) {
528             (void) memcpy(point, ext[i].name, ext[i].name_len);
529             point += ext[i].name_len;
530
531             *point = ' ';
532             point++;
533          }
534       }
535
536       *point = '\0';
537    }
538
539    return ext_str;
540 }
541
542
543 /**
544  * Get the string of client library supported extensions.
545  */
546 const char *
547 __glXGetClientExtensions(void)
548 {
549    if (__glXGLXClientExtensions == NULL) {
550       __glXExtensionsCtr();
551       __glXGLXClientExtensions = __glXGetStringFromTable(known_glx_extensions,
552                                                          client_glx_support);
553    }
554
555    return __glXGLXClientExtensions;
556 }
557
558
559 /**
560  * Calculate the list of application usable extensions.  The resulting
561  * string is stored in \c psc->effectiveGLXexts.
562  *
563  * \param psc                        Pointer to GLX per-screen record.
564  * \param display_is_direct_capable  True if the display is capable of
565  *                                   direct rendering.
566  * \param minor_version              GLX minor version from the server.
567  */
568
569 void
570 __glXCalculateUsableExtensions(struct glx_screen * psc,
571                                GLboolean display_is_direct_capable,
572                                int minor_version)
573 {
574    unsigned char server_support[8];
575    unsigned char usable[8];
576    unsigned i;
577
578    __glXExtensionsCtr();
579    __glXExtensionsCtrScreen(psc);
580
581    (void) memset(server_support, 0, sizeof(server_support));
582    __glXProcessServerString(known_glx_extensions,
583                             psc->serverGLXexts, server_support);
584
585
586    /* This is a hack.  Some servers support GLX 1.3 but don't export
587     * all of the extensions implied by GLX 1.3.  If the server claims
588     * support for GLX 1.3, enable support for the extensions that can be
589     * "emulated" as well.
590     */
591 #ifndef GLX_USE_APPLEGL
592    if (minor_version >= 3) {
593       SET_BIT(server_support, EXT_visual_info_bit);
594       SET_BIT(server_support, EXT_visual_rating_bit);
595       SET_BIT(server_support, SGI_make_current_read_bit);
596       SET_BIT(server_support, SGIX_fbconfig_bit);
597       SET_BIT(server_support, SGIX_pbuffer_bit);
598
599       /* This one is a little iffy.  GLX 1.3 doesn't incorporate all of this
600        * extension.  However, the only part that is not strictly client-side
601        * is shared.  That's the glXQueryContext / glXQueryContextInfoEXT
602        * function.
603        */
604
605       SET_BIT(server_support, EXT_import_context_bit);
606    }
607 #endif
608
609    /* An extension is supported if the client-side (i.e., libGL) supports
610     * it and the "server" supports it.  In this case that means that either
611     * the true server supports it or it is only for direct-rendering and
612     * the direct rendering driver supports it.
613     *
614     * If the display is not capable of direct rendering, then the extension
615     * is enabled if and only if the client-side library and the server
616     * support it.
617     */
618
619    if (display_is_direct_capable) {
620       for (i = 0; i < 8; i++) {
621          usable[i] = (client_glx_support[i] & client_glx_only[i])
622             | (client_glx_support[i] & psc->direct_support[i] &
623                server_support[i])
624             | (client_glx_support[i] & psc->direct_support[i] &
625                direct_glx_only[i]);
626       }
627    }
628    else {
629       for (i = 0; i < 8; i++) {
630          usable[i] = (client_glx_support[i] & client_glx_only[i])
631             | (client_glx_support[i] & server_support[i]);
632       }
633    }
634
635    /* This hack is necessary because GLX_ARB_create_context_profile depends on
636     * server support, but GLX_EXT_create_context_es2_profile is direct-only.
637     * Without this hack, it would be possible to advertise
638     * GLX_EXT_create_context_es2_profile without
639     * GLX_ARB_create_context_profile.  That would be a problem.
640     */
641    if (!IS_SET(server_support, ARB_create_context_profile_bit)) {
642       CLR_BIT(usable, EXT_create_context_es2_profile_bit);
643    }
644
645    psc->effectiveGLXexts = __glXGetStringFromTable(known_glx_extensions,
646                                                    usable);
647 }
648
649
650 /**
651  * Calculate the list of application usable extensions.  The resulting
652  * string is stored in \c gc->extensions.
653  *
654  * \param gc             Pointer to GLX context.
655  * \param server_string  Extension string from the server.
656  * \param major_version  GL major version from the server.
657  * \param minor_version  GL minor version from the server.
658  */
659
660 void
661 __glXCalculateUsableGLExtensions(struct glx_context * gc,
662                                  const char *server_string,
663                                  int major_version, int minor_version)
664 {
665    unsigned char server_support[__GL_EXT_BYTES];
666    unsigned char usable[__GL_EXT_BYTES];
667    unsigned i;
668
669
670    __glXExtensionsCtr();
671
672    (void) memset(server_support, 0, sizeof(server_support));
673    __glXProcessServerString(known_gl_extensions, server_string,
674                             server_support);
675
676
677    /* Handle lazy servers that don't export all the extensions strings that
678     * are part of the GL core version that they support.
679     */
680
681    for (i = 0; i < __GL_EXT_BYTES; i++) {
682       if ((known_gl_extensions[i].version_major != 0)
683           && ((major_version > known_gl_extensions[i].version_major)
684               || ((major_version == known_gl_extensions[i].version_major)
685                   && (minor_version >=
686                       known_gl_extensions[i].version_minor)))) {
687          SET_BIT(server_support, known_gl_extensions[i].bit);
688       }
689    }
690
691
692    /* An extension is supported if the client-side (i.e., libGL) supports
693     * it and the server supports it or the client-side library supports it
694     * and it only needs client-side support.
695     */
696
697    for (i = 0; i < __GL_EXT_BYTES; i++) {
698       usable[i] = (client_gl_support[i] & client_gl_only[i])
699          | (client_gl_support[i] & server_support[i]);
700    }
701
702    gc->extensions = (unsigned char *)
703       __glXGetStringFromTable(known_gl_extensions, usable);
704    (void) memcpy(gc->gl_extension_bits, usable, sizeof(usable));
705 }
706
707
708 /**
709  * Calculates the maximum core GL version that can be supported for indirect
710  * rendering.
711  */
712 void
713 __glXGetGLVersion(int *major_version, int *minor_version)
714 {
715    __glXExtensionsCtr();
716    *major_version = gl_major;
717    *minor_version = gl_minor;
718 }
719
720
721 /**
722  * Get a string representing the set of extensions supported by the client
723  * library.  This is currently only used to send the list of extensions
724  * supported by the client to the server.
725  */
726 char *
727 __glXGetClientGLExtensionString(void)
728 {
729    __glXExtensionsCtr();
730    return __glXGetStringFromTable(known_gl_extensions, client_gl_support);
731 }