xref: /aosp_15_r20/external/mesa3d/src/glx/clientinfo.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <string.h>
25 #include <ctype.h>
26 
27 #include "glxclient.h"
28 #include <xcb/glx.h>
29 #include <X11/Xlib-xcb.h>
30 
31 void
glxSendClientInfo(struct glx_display * glx_dpy,int screen)32 glxSendClientInfo(struct glx_display *glx_dpy, int screen)
33 {
34    const unsigned ext_length = strlen("GLX_ARB_create_context");
35    const unsigned prof_length = strlen("_profile");
36    char *gl_extension_string;
37    int gl_extension_length;
38    xcb_connection_t *c;
39    Bool any_screen_has_ARB_create_context = False;
40    Bool any_screen_has_ARB_create_context_profile = False;
41    unsigned i;
42    /* You need GLX_ARB_create_context_profile to get beyond 3.1 anyway */
43    static const uint32_t gl_versions[] = {
44       2, 1,
45       3, 0,
46       3, 1,
47    };
48    /*
49     * This is weird, but it matches what NVIDIA does/expects. For big-GL
50     * below 3.2 there is no such thing as a "profile", so we name them all
51     * with no profile bits. Except we don't name anything lower than 2.1,
52     * since GLX_ARB_create_context_profile says:
53     *
54     *   "Only the highest supported version below 3.0 should be sent, since
55     *   OpenGL 2.1 is backwards compatible with all earlier versions."
56     *
57     * In order to also support GLES below 3.2, we name every possible GLES
58     * version with the ES2 bit set, which happens to just mean GLES generally
59     * and not a particular major version. 3.2 happens to be a legal version
60     * number for both big-GL and GLES, so it gets all three bits set.
61     * Everything 3.3 and above is big-GL only so gets the core and compat
62     * bits set.
63     */
64    static const uint32_t gl_versions_profiles[] = {
65       1, 0, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
66       1, 1, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
67       2, 0, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
68       2, 1, 0x0,
69       3, 0, 0x0,
70       3, 0, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
71       3, 1, 0x0,
72       3, 1, GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
73       3, 2, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
74             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB |
75             GLX_CONTEXT_ES2_PROFILE_BIT_EXT,
76       3, 3, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
77             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
78       4, 0, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
79             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
80       4, 1, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
81             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
82       4, 2, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
83             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
84       4, 3, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
85             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
86       4, 4, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
87             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
88       4, 5, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
89             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
90       4, 6, GLX_CONTEXT_CORE_PROFILE_BIT_ARB |
91             GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
92    };
93    static const char glx_extensions[] =
94       "GLX_ARB_create_context GLX_ARB_create_context_profile";
95 
96    /* There are three possible flavors of the client info structure that the
97     * client could send to the server.  The version sent depends on the
98     * combination of GLX versions and extensions supported by the client and
99     * the server. This client only supports GLX version >= 1.3.
100     *
101     * Server supports                  Client sends
102     * ----------------------------------------------------------------------
103     * GLX version = 1.0                Nothing.
104     *
105     * GLX version >= 1.1               struct GLXClientInfo
106     *
107     * GLX version >= 1.4 and
108     * GLX_ARB_create_context           struct glXSetClientInfoARB
109     *
110     * GLX version >= 1.4 and
111     * GLX_ARB_create_context_profile   struct glXSetClientInfo2ARB
112     *
113     * GLX_ARB_create_context and GLX_ARB_create_context_profile use FBConfigs,
114     * and these only exist in GLX 1.4 or with GLX_SGIX_fbconfig.  I can't
115     * imagine an implementation that supports GLX_SGIX_fbconfig and
116     * GLX_ARB_create_context but not GLX 1.4.  Making GLX 1.4 a hard
117     * requirement in this case does not seem like a limitation.
118     */
119 
120    /* Determine whether any screen on the server supports either of the
121     * create-context extensions.
122     */
123    for (i = 0; i < ScreenCount(glx_dpy->dpy); i++) {
124       struct glx_screen *src = glx_dpy->screens[i];
125 
126       const char *haystack = src->serverGLXexts;
127       while (haystack != NULL) {
128     char *match = strstr(haystack, "GLX_ARB_create_context");
129 
130     if (match == NULL)
131        break;
132 
133     match += ext_length;
134 
135     switch (match[0]) {
136     case '\0':
137     case ' ':
138        any_screen_has_ARB_create_context = True;
139        break;
140 
141     case '_':
142        if (strncmp(match, "_profile", prof_length) == 0
143           && (match[prof_length] == '\0'
144          || match[prof_length] == ' ')) {
145           any_screen_has_ARB_create_context_profile = True;
146           match += prof_length;
147        }
148        break;
149     }
150 
151     haystack = match;
152       }
153    }
154 
155    gl_extension_string = __glXGetClientGLExtensionString(screen);
156    gl_extension_length = strlen(gl_extension_string) + 1;
157 
158    c = XGetXCBConnection(glx_dpy->dpy);
159 
160    /* Depending on the GLX version and the available extensions on the server,
161     * send the correct "flavor" of protocol to the server.
162     *
163     * THE ORDER IS IMPORTANT.  We want to send the most recent version of the
164     * protocol that the server can support.
165     */
166    if (glx_dpy->minorVersion == 4
167        && any_screen_has_ARB_create_context_profile) {
168       xcb_glx_set_client_info_2arb(c,
169               GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
170                sizeof(gl_versions_profiles)
171                / (3 * sizeof(gl_versions_profiles[0])),
172               gl_extension_length,
173               strlen(glx_extensions) + 1,
174               gl_versions_profiles,
175               gl_extension_string,
176               glx_extensions);
177    } else if (glx_dpy->minorVersion == 4
178          && any_screen_has_ARB_create_context) {
179       xcb_glx_set_client_info_arb(c,
180               GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
181               sizeof(gl_versions)
182               / (2 * sizeof(gl_versions[0])),
183               gl_extension_length,
184               strlen(glx_extensions) + 1,
185               gl_versions,
186               gl_extension_string,
187               glx_extensions);
188    } else {
189       xcb_glx_client_info(c,
190            GLX_MAJOR_VERSION, GLX_MINOR_VERSION,
191            gl_extension_length,
192            gl_extension_string);
193    }
194 
195    free(gl_extension_string);
196 }
197