glx: Implement GLX_EXT_no_config_context
[mesa.git] / src / glx / create_context.c
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 <limits.h>
25 #include "glxclient.h"
26 #include "glx_error.h"
27 #include <xcb/glx.h>
28 #include <X11/Xlib-xcb.h>
29
30 #include <assert.h>
31
32 #if INT_MAX != 2147483647
33 #error This code requires sizeof(uint32_t) == sizeof(int).
34 #endif
35
36 /* debian stretch still has ancient headers and we're apparently still
37 * using that for gitlab ci. please delete me when that's fixed.
38 */
39 #ifndef X_GLXCreateContextAttribsARB
40 #warning Please update your GLX protocol headers
41 #define X_GLXCreateContextAttribsARB 34
42 #endif
43
44 _X_HIDDEN GLXContext
45 glXCreateContextAttribsARB(Display *dpy, GLXFBConfig config,
46 GLXContext share_context, Bool direct,
47 const int *attrib_list)
48 {
49 xcb_connection_t *const c = XGetXCBConnection(dpy);
50 struct glx_config *const cfg = (struct glx_config *) config;
51 struct glx_context *const share = (struct glx_context *) share_context;
52 struct glx_context *gc = NULL;
53 unsigned num_attribs = 0;
54 struct glx_screen *psc;
55 xcb_generic_error_t *err;
56 xcb_void_cookie_t cookie;
57 unsigned dummy_err = 0;
58 int screen = -1;
59
60 if (dpy == NULL)
61 return NULL;
62
63 /* Count the number of attributes specified by the application. All
64 * attributes appear in pairs, except the terminating None.
65 */
66 if (attrib_list != NULL) {
67 for (/* empty */; attrib_list[num_attribs * 2] != 0; num_attribs++)
68 /* empty */ ;
69 }
70
71 if (cfg) {
72 screen = cfg->screen;
73 } else {
74 for (unsigned int i = 0; i < num_attribs; i++) {
75 if (attrib_list[i * 2] == GLX_SCREEN)
76 screen = attrib_list[i * 2 + 1];
77 }
78 if (screen == -1) {
79 __glXSendError(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True);
80 return NULL;
81 }
82 }
83
84 /* This means that either the caller passed the wrong display pointer or
85 * one of the internal GLX data structures (probably the fbconfig) has an
86 * error. There is nothing sensible to do, so return an error.
87 */
88 psc = GetGLXScreenConfigs(dpy, screen);
89 if (psc == NULL)
90 return NULL;
91
92 assert(screen == psc->scr);
93
94 if (direct && psc->vtable->create_context_attribs) {
95 /* GLX drops the error returned by the driver. The expectation is that
96 * an error will also be returned by the server. The server's error
97 * will be delivered to the application.
98 */
99 gc = psc->vtable->create_context_attribs(psc, cfg, share, num_attribs,
100 (const uint32_t *) attrib_list,
101 &dummy_err);
102 }
103
104 if (gc == NULL) {
105 #ifdef GLX_USE_APPLEGL
106 gc = applegl_create_context(psc, cfg, share, 0);
107 #else
108 gc = indirect_create_context_attribs(psc, cfg, share, num_attribs,
109 (const uint32_t *) attrib_list,
110 &dummy_err);
111 #endif
112 }
113
114 gc->xid = xcb_generate_id(c);
115 gc->share_xid = (share != NULL) ? share->xid : 0;
116
117 /* The manual pages for glXCreateContext and glXCreateNewContext say:
118 *
119 * "NULL is returned if execution fails on the client side."
120 *
121 * If the server generates an error, the application is supposed to catch
122 * the protocol error and handle it. Part of handling the error is freeing
123 * the possibly non-NULL value returned by this function.
124 */
125 cookie =
126 xcb_glx_create_context_attribs_arb_checked(c,
127 gc->xid,
128 cfg ? cfg->fbconfigID : 0,
129 screen,
130 gc->share_xid,
131 gc->isDirect,
132 num_attribs,
133 (const uint32_t *)
134 attrib_list);
135 err = xcb_request_check(c, cookie);
136 if (err != NULL) {
137 gc->vtable->destroy(gc);
138 gc = NULL;
139
140 __glXSendErrorForXcb(dpy, err);
141 free(err);
142 }
143
144 return (GLXContext) gc;
145 }