glxcurrent.c revision 1463c08d
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( USE_ELF_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_INITIAL_EXEC void *__glX_tls_Context
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( USE_ELF_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                   int opcode)
193{
194   struct glx_context *gc = (struct glx_context *) gc_user;
195   struct glx_context *oldGC = __glXGetCurrentContext();
196
197   /* Make sure that the new context has a nonzero ID.  In the request,
198    * a zero context ID is used only to mean that we bind to no current
199    * context.
200    */
201   if ((gc != NULL) && (gc->xid == None)) {
202      return GL_FALSE;
203   }
204
205   _glapi_check_multithread();
206
207   __glXLock();
208   if (oldGC == gc &&
209       gc->currentDrawable == draw && gc->currentReadable == read) {
210      __glXUnlock();
211      return True;
212   }
213
214   /* can't have only one be 0 */
215   if (!!draw != !!read) {
216      __glXUnlock();
217      __glXSendError(dpy, BadMatch, None, opcode, True);
218      return False;
219   }
220
221   if (oldGC != &dummyContext) {
222      if (--oldGC->thread_refcount == 0) {
223	 oldGC->vtable->unbind(oldGC, gc);
224	 oldGC->currentDpy = 0;
225      }
226   }
227
228   if (gc) {
229      /* Attempt to bind the context.  We do this before mucking with
230       * gc and __glXSetCurrentContext to properly handle our state in
231       * case of an error.
232       *
233       * If an error occurs, set the Null context since we've already
234       * blown away our old context.  The caller is responsible for
235       * figuring out how to handle setting a valid context.
236       */
237      if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
238         __glXSetCurrentContextNull();
239         __glXUnlock();
240         __glXSendError(dpy, GLXBadContext, None, opcode, 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   return GL_TRUE;
265}
266
267_GLX_PUBLIC Bool
268glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
269{
270   return MakeContextCurrent(dpy, draw, draw, gc, X_GLXMakeCurrent);
271}
272
273_GLX_PUBLIC Bool
274glXMakeContextCurrent(Display *dpy, GLXDrawable d, GLXDrawable r,
275                      GLXContext ctx)
276{
277   return MakeContextCurrent(dpy, d, r, ctx, X_GLXMakeContextCurrent);
278}
279
280_GLX_PUBLIC Bool
281glXMakeCurrentReadSGI(Display *dpy, GLXDrawable d, GLXDrawable r,
282                      GLXContext ctx)
283{
284   return MakeContextCurrent(dpy, d, r, ctx, X_GLXvop_MakeCurrentReadSGI);
285}
286