glxcurrent.c revision 7f60d2b9
1/* 2 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008) 3 * Copyright (C) 1991-2000 Silicon Graphics, Inc. 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 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice including the dates of first publication and 13 * either this permission notice or a reference to 14 * http://oss.sgi.com/projects/FreeB/ 15 * shall be included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 22 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 * 25 * Except as contained in this notice, the name of Silicon Graphics, Inc. 26 * shall not be used in advertising or otherwise to promote the sale, use or 27 * other dealings in this Software without prior written authorization from 28 * Silicon Graphics, Inc. 29 */ 30 31/** 32 * \file glxcurrent.c 33 * Client-side GLX interface for current context management. 34 */ 35 36#include <pthread.h> 37 38#include "glxclient.h" 39#include "glapi.h" 40#include "glx_error.h" 41 42/* 43** We setup some dummy structures here so that the API can be used 44** even if no context is current. 45*/ 46 47static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE]; 48static struct glx_context_vtable dummyVtable; 49/* 50** Dummy context used by small commands when there is no current context. 51** All the 52** gl and glx entry points are designed to operate as nop's when using 53** the dummy context structure. 54*/ 55struct glx_context dummyContext = { 56 &dummyBuffer[0], 57 &dummyBuffer[0], 58 &dummyBuffer[0], 59 &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE], 60 sizeof(dummyBuffer), 61 &dummyVtable 62}; 63 64/* 65 * Current context management and locking 66 */ 67 68_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER; 69 70# if defined( GLX_USE_TLS ) 71 72/** 73 * Per-thread GLX context pointer. 74 * 75 * \c __glXSetCurrentContext is written is such a way that this pointer can 76 * \b never be \c NULL. This is important! Because of this 77 * \c __glXGetCurrentContext can be implemented as trivial macro. 78 */ 79__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec"))) 80#if defined(__NetBSD__) 81 = NULL; /* non-zero initializers not supported with dlopen */ 82#else 83 = &dummyContext; 84#endif 85 86_X_HIDDEN void 87__glXSetCurrentContext(struct glx_context * c) 88{ 89 __glX_tls_Context = (c != NULL) ? c : &dummyContext; 90} 91 92# else 93 94static pthread_once_t once_control = PTHREAD_ONCE_INIT; 95 96/** 97 * Per-thread data key. 98 * 99 * Once \c init_thread_data has been called, the per-thread data key will 100 * take a value of \c NULL. As each new thread is created the default 101 * value, in that thread, will be \c NULL. 102 */ 103static pthread_key_t ContextTSD; 104 105/** 106 * Initialize the per-thread data key. 107 * 108 * This function is called \b exactly once per-process (not per-thread!) to 109 * initialize the per-thread data key. This is ideally done using the 110 * \c pthread_once mechanism. 111 */ 112static void 113init_thread_data(void) 114{ 115 if (pthread_key_create(&ContextTSD, NULL) != 0) { 116 perror("pthread_key_create"); 117 exit(-1); 118 } 119} 120 121_X_HIDDEN void 122__glXSetCurrentContext(struct glx_context * c) 123{ 124 pthread_once(&once_control, init_thread_data); 125 pthread_setspecific(ContextTSD, c); 126} 127 128_X_HIDDEN struct glx_context * 129__glXGetCurrentContext(void) 130{ 131 void *v; 132 133 pthread_once(&once_control, init_thread_data); 134 135 v = pthread_getspecific(ContextTSD); 136 return (v == NULL) ? &dummyContext : (struct glx_context *) v; 137} 138 139# endif /* defined( GLX_USE_TLS ) */ 140 141 142_X_HIDDEN void 143__glXSetCurrentContextNull(void) 144{ 145 __glXSetCurrentContext(&dummyContext); 146#if defined(GLX_DIRECT_RENDERING) 147 _glapi_set_dispatch(NULL); /* no-op functions */ 148 _glapi_set_context(NULL); 149#endif 150} 151 152_GLX_PUBLIC GLXContext 153glXGetCurrentContext(void) 154{ 155 struct glx_context *cx = __glXGetCurrentContext(); 156 157 if (cx == &dummyContext) { 158 return NULL; 159 } 160 else { 161 return (GLXContext) cx; 162 } 163} 164 165_GLX_PUBLIC GLXDrawable 166glXGetCurrentDrawable(void) 167{ 168 struct glx_context *gc = __glXGetCurrentContext(); 169 return gc->currentDrawable; 170} 171 172/** 173 * Make a particular context current. 174 * 175 * \note This is in this file so that it can access dummyContext. 176 */ 177static Bool 178MakeContextCurrent(Display * dpy, GLXDrawable draw, 179 GLXDrawable read, GLXContext gc_user) 180{ 181 struct glx_context *gc = (struct glx_context *) gc_user; 182 struct glx_context *oldGC = __glXGetCurrentContext(); 183 184 /* Make sure that the new context has a nonzero ID. In the request, 185 * a zero context ID is used only to mean that we bind to no current 186 * context. 187 */ 188 if ((gc != NULL) && (gc->xid == None)) { 189 return GL_FALSE; 190 } 191 192 _glapi_check_multithread(); 193 194 __glXLock(); 195 if (oldGC == gc && 196 gc->currentDrawable == draw && gc->currentReadable == read) { 197 __glXUnlock(); 198 return True; 199 } 200 201 /* can't have only one be 0 */ 202 if (!!draw != !!read) { 203 __glXUnlock(); 204 __glXSendError(dpy, BadMatch, None, X_GLXMakeContextCurrent, True); 205 return False; 206 } 207 208 if (oldGC != &dummyContext) { 209 if (--oldGC->thread_refcount == 0) { 210 oldGC->vtable->unbind(oldGC, gc); 211 oldGC->currentDpy = 0; 212 } 213 } 214 215 if (gc) { 216 /* Attempt to bind the context. We do this before mucking with 217 * gc and __glXSetCurrentContext to properly handle our state in 218 * case of an error. 219 * 220 * If an error occurs, set the Null context since we've already 221 * blown away our old context. The caller is responsible for 222 * figuring out how to handle setting a valid context. 223 */ 224 if (gc->vtable->bind(gc, oldGC, draw, read) != Success) { 225 __glXSetCurrentContextNull(); 226 __glXUnlock(); 227 __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent, 228 False); 229 return GL_FALSE; 230 } 231 232 if (gc->thread_refcount == 0) { 233 gc->currentDpy = dpy; 234 gc->currentDrawable = draw; 235 gc->currentReadable = read; 236 } 237 gc->thread_refcount++; 238 __glXSetCurrentContext(gc); 239 } else { 240 __glXSetCurrentContextNull(); 241 } 242 243 if (oldGC->thread_refcount == 0 && oldGC != &dummyContext && oldGC->xid == None) { 244 /* We are switching away from a context that was 245 * previously destroyed, so we need to free the memory 246 * for the old handle. */ 247 oldGC->vtable->destroy(oldGC); 248 } 249 250 __glXUnlock(); 251 252 /* The indirect vertex array state must to be initialised after we 253 * have setup the context, as it needs to query server attributes. 254 */ 255 if (gc && !gc->isDirect) { 256 __GLXattribute *state = gc->client_state_private; 257 if (state && state->array_state == NULL) { 258 glGetString(GL_EXTENSIONS); 259 glGetString(GL_VERSION); 260 __glXInitVertexArrayState(gc); 261 } 262 } 263 264 return GL_TRUE; 265} 266 267 268_GLX_PUBLIC Bool 269glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc) 270{ 271 return MakeContextCurrent(dpy, draw, draw, gc); 272} 273 274_GLX_PUBLIC 275GLX_ALIAS(Bool, glXMakeCurrentReadSGI, 276 (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx), 277 (dpy, d, r, ctx), MakeContextCurrent) 278 279_GLX_PUBLIC 280GLX_ALIAS(Bool, glXMakeContextCurrent, 281 (Display * dpy, GLXDrawable d, GLXDrawable r, 282 GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent) 283