1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2011 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include <limits.h> 25bf215546Sopenharmony_ci#include "glxclient.h" 26bf215546Sopenharmony_ci#include "glx_error.h" 27bf215546Sopenharmony_ci#include <xcb/glx.h> 28bf215546Sopenharmony_ci#include <X11/Xlib-xcb.h> 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include <assert.h> 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#if INT_MAX != 2147483647 33bf215546Sopenharmony_ci#error This code requires sizeof(uint32_t) == sizeof(int). 34bf215546Sopenharmony_ci#endif 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci/* An "Atrribs/Attribs" typo was fixed in glxproto.h in Nov 2014. 37bf215546Sopenharmony_ci * This is in case we don't have the updated header. 38bf215546Sopenharmony_ci */ 39bf215546Sopenharmony_ci#if !defined(X_GLXCreateContextAttribsARB) && \ 40bf215546Sopenharmony_ci defined(X_GLXCreateContextAtrribsARB) 41bf215546Sopenharmony_ci#define X_GLXCreateContextAttribsARB X_GLXCreateContextAtrribsARB 42bf215546Sopenharmony_ci#endif 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_ci_X_HIDDEN GLXContext 45bf215546Sopenharmony_ciglXCreateContextAttribsARB(Display *dpy, GLXFBConfig config, 46bf215546Sopenharmony_ci GLXContext share_context, Bool direct, 47bf215546Sopenharmony_ci const int *attrib_list) 48bf215546Sopenharmony_ci{ 49bf215546Sopenharmony_ci xcb_connection_t *const c = XGetXCBConnection(dpy); 50bf215546Sopenharmony_ci struct glx_config *const cfg = (struct glx_config *) config; 51bf215546Sopenharmony_ci struct glx_context *const share = (struct glx_context *) share_context; 52bf215546Sopenharmony_ci struct glx_context *gc = NULL; 53bf215546Sopenharmony_ci unsigned num_attribs = 0; 54bf215546Sopenharmony_ci struct glx_screen *psc; 55bf215546Sopenharmony_ci xcb_generic_error_t *err; 56bf215546Sopenharmony_ci xcb_void_cookie_t cookie; 57bf215546Sopenharmony_ci unsigned dummy_err = 0; 58bf215546Sopenharmony_ci uint32_t xid, share_xid; 59bf215546Sopenharmony_ci int screen = -1; 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_ci if (dpy == NULL) 62bf215546Sopenharmony_ci return NULL; 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci /* Count the number of attributes specified by the application. All 65bf215546Sopenharmony_ci * attributes appear in pairs, except the terminating None. 66bf215546Sopenharmony_ci */ 67bf215546Sopenharmony_ci if (attrib_list != NULL) { 68bf215546Sopenharmony_ci for (/* empty */; attrib_list[num_attribs * 2] != 0; num_attribs++) 69bf215546Sopenharmony_ci /* empty */ ; 70bf215546Sopenharmony_ci } 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ci if (cfg) { 73bf215546Sopenharmony_ci screen = cfg->screen; 74bf215546Sopenharmony_ci } else { 75bf215546Sopenharmony_ci for (unsigned int i = 0; i < num_attribs; i++) { 76bf215546Sopenharmony_ci if (attrib_list[i * 2] == GLX_SCREEN) 77bf215546Sopenharmony_ci screen = attrib_list[i * 2 + 1]; 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci if (screen == -1) { 80bf215546Sopenharmony_ci __glXSendError(dpy, BadValue, 0, X_GLXCreateContextAttribsARB, True); 81bf215546Sopenharmony_ci return NULL; 82bf215546Sopenharmony_ci } 83bf215546Sopenharmony_ci } 84bf215546Sopenharmony_ci 85bf215546Sopenharmony_ci /* This means that either the caller passed the wrong display pointer or 86bf215546Sopenharmony_ci * one of the internal GLX data structures (probably the fbconfig) has an 87bf215546Sopenharmony_ci * error. There is nothing sensible to do, so return an error. 88bf215546Sopenharmony_ci */ 89bf215546Sopenharmony_ci psc = GetGLXScreenConfigs(dpy, screen); 90bf215546Sopenharmony_ci if (psc == NULL) 91bf215546Sopenharmony_ci return NULL; 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci assert(screen == psc->scr); 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci /* Some application may request an indirect context but we may want to force a direct 96bf215546Sopenharmony_ci * one because Xorg only allows indirect contexts if they were enabled. 97bf215546Sopenharmony_ci */ 98bf215546Sopenharmony_ci if (!direct && 99bf215546Sopenharmony_ci psc->force_direct_context) { 100bf215546Sopenharmony_ci direct = true; 101bf215546Sopenharmony_ci } 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci if (direct && psc->vtable->create_context_attribs) { 105bf215546Sopenharmony_ci /* GLX drops the error returned by the driver. The expectation is that 106bf215546Sopenharmony_ci * an error will also be returned by the server. The server's error 107bf215546Sopenharmony_ci * will be delivered to the application. 108bf215546Sopenharmony_ci */ 109bf215546Sopenharmony_ci gc = psc->vtable->create_context_attribs(psc, cfg, share, num_attribs, 110bf215546Sopenharmony_ci (const uint32_t *) attrib_list, 111bf215546Sopenharmony_ci &dummy_err); 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci if (gc == NULL) { 115bf215546Sopenharmony_ci#ifdef GLX_USE_APPLEGL 116bf215546Sopenharmony_ci gc = applegl_create_context(psc, cfg, share, 0); 117bf215546Sopenharmony_ci#else 118bf215546Sopenharmony_ci gc = indirect_create_context_attribs(psc, cfg, share, num_attribs, 119bf215546Sopenharmony_ci (const uint32_t *) attrib_list, 120bf215546Sopenharmony_ci &dummy_err); 121bf215546Sopenharmony_ci#endif 122bf215546Sopenharmony_ci } 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci xid = xcb_generate_id(c); 125bf215546Sopenharmony_ci share_xid = (share != NULL) ? share->xid : 0; 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ci /* The manual pages for glXCreateContext and glXCreateNewContext say: 128bf215546Sopenharmony_ci * 129bf215546Sopenharmony_ci * "NULL is returned if execution fails on the client side." 130bf215546Sopenharmony_ci * 131bf215546Sopenharmony_ci * If the server generates an error, the application is supposed to catch 132bf215546Sopenharmony_ci * the protocol error and handle it. Part of handling the error is freeing 133bf215546Sopenharmony_ci * the possibly non-NULL value returned by this function. 134bf215546Sopenharmony_ci */ 135bf215546Sopenharmony_ci cookie = 136bf215546Sopenharmony_ci xcb_glx_create_context_attribs_arb_checked(c, 137bf215546Sopenharmony_ci xid, 138bf215546Sopenharmony_ci cfg ? cfg->fbconfigID : 0, 139bf215546Sopenharmony_ci screen, 140bf215546Sopenharmony_ci share_xid, 141bf215546Sopenharmony_ci gc ? gc->isDirect : direct, 142bf215546Sopenharmony_ci num_attribs, 143bf215546Sopenharmony_ci (const uint32_t *) 144bf215546Sopenharmony_ci attrib_list); 145bf215546Sopenharmony_ci err = xcb_request_check(c, cookie); 146bf215546Sopenharmony_ci if (err != NULL) { 147bf215546Sopenharmony_ci if (gc) 148bf215546Sopenharmony_ci gc->vtable->destroy(gc); 149bf215546Sopenharmony_ci gc = NULL; 150bf215546Sopenharmony_ci 151bf215546Sopenharmony_ci __glXSendErrorForXcb(dpy, err); 152bf215546Sopenharmony_ci free(err); 153bf215546Sopenharmony_ci } else if (!gc) { 154bf215546Sopenharmony_ci /* the server thought the context description was okay, but we failed 155bf215546Sopenharmony_ci * somehow on the client side. clean up the server resource and panic. 156bf215546Sopenharmony_ci */ 157bf215546Sopenharmony_ci xcb_glx_destroy_context(c, xid); 158bf215546Sopenharmony_ci /* increment dpy->request in order to give a unique serial number to the 159bf215546Sopenharmony_ci * error */ 160bf215546Sopenharmony_ci XNoOp(dpy); 161bf215546Sopenharmony_ci __glXSendError(dpy, GLXBadFBConfig, xid, 0, False); 162bf215546Sopenharmony_ci } else { 163bf215546Sopenharmony_ci gc->xid = xid; 164bf215546Sopenharmony_ci gc->share_xid = share_xid; 165bf215546Sopenharmony_ci } 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci return (GLXContext) gc; 168bf215546Sopenharmony_ci} 169