1848b8605Smrg/*
2848b8605Smrg * Copyright © 2011 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21848b8605Smrg * DEALINGS IN THE SOFTWARE.
22848b8605Smrg */
23848b8605Smrg
24848b8605Smrg#include <limits.h>
25848b8605Smrg#include "glxclient.h"
26848b8605Smrg#include "glx_error.h"
27848b8605Smrg#include <xcb/glx.h>
28848b8605Smrg#include <X11/Xlib-xcb.h>
29848b8605Smrg
30848b8605Smrg#include <assert.h>
31848b8605Smrg
32848b8605Smrg#if INT_MAX != 2147483647
33848b8605Smrg#error This code requires sizeof(uint32_t) == sizeof(int).
34848b8605Smrg#endif
35848b8605Smrg
36848b8605Smrg_X_HIDDEN GLXContext
37848b8605SmrgglXCreateContextAttribsARB(Display *dpy, GLXFBConfig config,
38848b8605Smrg			   GLXContext share_context, Bool direct,
39848b8605Smrg			   const int *attrib_list)
40848b8605Smrg{
41848b8605Smrg   xcb_connection_t *const c = XGetXCBConnection(dpy);
42848b8605Smrg   struct glx_config *const cfg = (struct glx_config *) config;
43848b8605Smrg   struct glx_context *const share = (struct glx_context *) share_context;
44848b8605Smrg   struct glx_context *gc = NULL;
45848b8605Smrg   unsigned num_attribs = 0;
46848b8605Smrg   struct glx_screen *psc;
47848b8605Smrg   xcb_generic_error_t *err;
48848b8605Smrg   xcb_void_cookie_t cookie;
49848b8605Smrg   unsigned dummy_err = 0;
50848b8605Smrg
51848b8605Smrg
52848b8605Smrg   if (dpy == NULL || cfg == NULL)
53848b8605Smrg      return NULL;
54848b8605Smrg
55848b8605Smrg   /* This means that either the caller passed the wrong display pointer or
56848b8605Smrg    * one of the internal GLX data structures (probably the fbconfig) has an
57848b8605Smrg    * error.  There is nothing sensible to do, so return an error.
58848b8605Smrg    */
59848b8605Smrg   psc = GetGLXScreenConfigs(dpy, cfg->screen);
60848b8605Smrg   if (psc == NULL)
61848b8605Smrg      return NULL;
62848b8605Smrg
63848b8605Smrg   assert(cfg->screen == psc->scr);
64848b8605Smrg
65848b8605Smrg   /* Count the number of attributes specified by the application.  All
66848b8605Smrg    * attributes appear in pairs, except the terminating None.
67848b8605Smrg    */
68848b8605Smrg   if (attrib_list != NULL) {
69848b8605Smrg      for (/* empty */; attrib_list[num_attribs * 2] != 0; num_attribs++)
70848b8605Smrg	 /* empty */ ;
71848b8605Smrg   }
72848b8605Smrg
73848b8605Smrg   if (direct && psc->vtable->create_context_attribs) {
74848b8605Smrg      /* GLX drops the error returned by the driver.  The expectation is that
75848b8605Smrg       * an error will also be returned by the server.  The server's error
76848b8605Smrg       * will be delivered to the application.
77848b8605Smrg       */
78848b8605Smrg      gc = psc->vtable->create_context_attribs(psc, cfg, share, num_attribs,
79848b8605Smrg					       (const uint32_t *) attrib_list,
80848b8605Smrg					       &dummy_err);
81848b8605Smrg   }
82848b8605Smrg
83848b8605Smrg   if (gc == NULL) {
84848b8605Smrg#ifdef GLX_USE_APPLEGL
85848b8605Smrg      gc = applegl_create_context(psc, cfg, share, 0);
86848b8605Smrg#else
87848b8605Smrg      gc = indirect_create_context_attribs(psc, cfg, share, num_attribs,
88848b8605Smrg              (const uint32_t *) attrib_list,
89848b8605Smrg              &dummy_err);
90848b8605Smrg#endif
91848b8605Smrg   }
92848b8605Smrg
93848b8605Smrg   gc->xid = xcb_generate_id(c);
94848b8605Smrg   gc->share_xid = (share != NULL) ? share->xid : 0;
95848b8605Smrg
96848b8605Smrg   /* The manual pages for glXCreateContext and glXCreateNewContext say:
97848b8605Smrg    *
98848b8605Smrg    *     "NULL is returned if execution fails on the client side."
99848b8605Smrg    *
100848b8605Smrg    * If the server generates an error, the application is supposed to catch
101848b8605Smrg    * the protocol error and handle it.  Part of handling the error is freeing
102848b8605Smrg    * the possibly non-NULL value returned by this function.
103848b8605Smrg    */
104848b8605Smrg   cookie =
105848b8605Smrg      xcb_glx_create_context_attribs_arb_checked(c,
106848b8605Smrg						 gc->xid,
107848b8605Smrg						 cfg->fbconfigID,
108848b8605Smrg						 cfg->screen,
109848b8605Smrg						 gc->share_xid,
110848b8605Smrg						 gc->isDirect,
111848b8605Smrg						 num_attribs,
112848b8605Smrg						 (const uint32_t *)
113848b8605Smrg						 attrib_list);
114848b8605Smrg   err = xcb_request_check(c, cookie);
115848b8605Smrg   if (err != NULL) {
116848b8605Smrg      gc->vtable->destroy(gc);
117848b8605Smrg      gc = NULL;
118848b8605Smrg
119848b8605Smrg      __glXSendErrorForXcb(dpy, err);
120848b8605Smrg      free(err);
121848b8605Smrg   }
122848b8605Smrg
123848b8605Smrg   return (GLXContext) gc;
124848b8605Smrg}
125