glxcurrent.c revision b8e80941
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 * MASSIVE KLUDGE!
44 * We need these to not be extern in libGL.so because of
45 * PR toolchain/50277
46 */
47#if defined(GLX_USE_TLS) && defined(__NetBSD__)
48_X_EXPORT __thread struct _glapi_table * _glapi_tls_Dispatch
49    __attribute__((tls_model("initial-exec"))) = NULL;
50_X_EXPORT __thread void * _glapi_tls_Context
51    __attribute__((tls_model("initial-exec")));
52#endif
53
54/*
55** We setup some dummy structures here so that the API can be used
56** even if no context is current.
57*/
58
59static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
60static struct glx_context_vtable dummyVtable;
61/*
62** Dummy context used by small commands when there is no current context.
63** All the
64** gl and glx entry points are designed to operate as nop's when using
65** the dummy context structure.
66*/
67struct glx_context dummyContext = {
68   &dummyBuffer[0],
69   &dummyBuffer[0],
70   &dummyBuffer[0],
71   &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
72   sizeof(dummyBuffer),
73   &dummyVtable
74};
75
76/*
77 * Current context management and locking
78 */
79
80_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
81
82# if defined( GLX_USE_TLS )
83
84/**
85 * Per-thread GLX context pointer.
86 *
87 * \c __glXSetCurrentContext is written is such a way that this pointer can
88 * \b never be \c NULL.  This is important!  Because of this
89 * \c __glXGetCurrentContext can be implemented as trivial macro.
90 */
91__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
92#if defined(__NetBSD__)
93   = NULL; /* non-zero initializers not supported with dlopen */
94#else
95   = &dummyContext;
96#endif
97
98_X_HIDDEN void
99__glXSetCurrentContext(struct glx_context * c)
100{
101   __glX_tls_Context = (c != NULL) ? c : &dummyContext;
102}
103
104# else
105
106static pthread_once_t once_control = PTHREAD_ONCE_INIT;
107
108/**
109 * Per-thread data key.
110 *
111 * Once \c init_thread_data has been called, the per-thread data key will
112 * take a value of \c NULL.  As each new thread is created the default
113 * value, in that thread, will be \c NULL.
114 */
115static pthread_key_t ContextTSD;
116
117/**
118 * Initialize the per-thread data key.
119 *
120 * This function is called \b exactly once per-process (not per-thread!) to
121 * initialize the per-thread data key.  This is ideally done using the
122 * \c pthread_once mechanism.
123 */
124static void
125init_thread_data(void)
126{
127   if (pthread_key_create(&ContextTSD, NULL) != 0) {
128      perror("pthread_key_create");
129      exit(-1);
130   }
131}
132
133_X_HIDDEN void
134__glXSetCurrentContext(struct glx_context * c)
135{
136   pthread_once(&once_control, init_thread_data);
137   pthread_setspecific(ContextTSD, c);
138}
139
140_X_HIDDEN struct glx_context *
141__glXGetCurrentContext(void)
142{
143   void *v;
144
145   pthread_once(&once_control, init_thread_data);
146
147   v = pthread_getspecific(ContextTSD);
148   return (v == NULL) ? &dummyContext : (struct glx_context *) v;
149}
150
151# endif /* defined( GLX_USE_TLS ) */
152
153
154_X_HIDDEN void
155__glXSetCurrentContextNull(void)
156{
157   __glXSetCurrentContext(&dummyContext);
158#if defined(GLX_DIRECT_RENDERING)
159   _glapi_set_dispatch(NULL);   /* no-op functions */
160   _glapi_set_context(NULL);
161#endif
162}
163
164_GLX_PUBLIC GLXContext
165glXGetCurrentContext(void)
166{
167   struct glx_context *cx = __glXGetCurrentContext();
168
169   if (cx == &dummyContext) {
170      return NULL;
171   }
172   else {
173      return (GLXContext) cx;
174   }
175}
176
177_GLX_PUBLIC GLXDrawable
178glXGetCurrentDrawable(void)
179{
180   struct glx_context *gc = __glXGetCurrentContext();
181   return gc->currentDrawable;
182}
183
184/**
185 * Make a particular context current.
186 *
187 * \note This is in this file so that it can access dummyContext.
188 */
189static Bool
190MakeContextCurrent(Display * dpy, GLXDrawable draw,
191                   GLXDrawable read, GLXContext gc_user)
192{
193   struct glx_context *gc = (struct glx_context *) gc_user;
194   struct glx_context *oldGC = __glXGetCurrentContext();
195
196   /* Make sure that the new context has a nonzero ID.  In the request,
197    * a zero context ID is used only to mean that we bind to no current
198    * context.
199    */
200   if ((gc != NULL) && (gc->xid == None)) {
201      return GL_FALSE;
202   }
203
204   _glapi_check_multithread();
205
206   __glXLock();
207   if (oldGC == gc &&
208       gc->currentDrawable == draw && gc->currentReadable == read) {
209      __glXUnlock();
210      return True;
211   }
212
213   /* can't have only one be 0 */
214   if (!!draw != !!read) {
215      __glXUnlock();
216      __glXSendError(dpy, BadMatch, None, X_GLXMakeContextCurrent, True);
217      return False;
218   }
219
220   if (oldGC != &dummyContext) {
221      if (--oldGC->thread_refcount == 0) {
222	 oldGC->vtable->unbind(oldGC, gc);
223	 oldGC->currentDpy = 0;
224      }
225   }
226
227   if (gc) {
228      /* Attempt to bind the context.  We do this before mucking with
229       * gc and __glXSetCurrentContext to properly handle our state in
230       * case of an error.
231       *
232       * If an error occurs, set the Null context since we've already
233       * blown away our old context.  The caller is responsible for
234       * figuring out how to handle setting a valid context.
235       */
236      if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
237         __glXSetCurrentContextNull();
238         __glXUnlock();
239         __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
240                        False);
241         return GL_FALSE;
242      }
243
244      if (gc->thread_refcount == 0) {
245         gc->currentDpy = dpy;
246         gc->currentDrawable = draw;
247         gc->currentReadable = read;
248      }
249      gc->thread_refcount++;
250      __glXSetCurrentContext(gc);
251   } else {
252      __glXSetCurrentContextNull();
253   }
254
255   if (oldGC->thread_refcount == 0 && oldGC != &dummyContext && oldGC->xid == None) {
256      /* We are switching away from a context that was
257       * previously destroyed, so we need to free the memory
258       * for the old handle. */
259      oldGC->vtable->destroy(oldGC);
260   }
261
262   __glXUnlock();
263
264   /* The indirect vertex array state must to be initialised after we
265    * have setup the context, as it needs to query server attributes.
266    */
267   if (gc && !gc->isDirect) {
268      __GLXattribute *state = gc->client_state_private;
269      if (state && state->array_state == NULL) {
270         glGetString(GL_EXTENSIONS);
271         glGetString(GL_VERSION);
272         __glXInitVertexArrayState(gc);
273      }
274   }
275
276   return GL_TRUE;
277}
278
279
280_GLX_PUBLIC Bool
281glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
282{
283   return MakeContextCurrent(dpy, draw, draw, gc);
284}
285
286_GLX_PUBLIC
287GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
288          (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
289          (dpy, d, r, ctx), MakeContextCurrent)
290
291_GLX_PUBLIC
292GLX_ALIAS(Bool, glXMakeContextCurrent,
293          (Display * dpy, GLXDrawable d, GLXDrawable r,
294           GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)
295