glxcurrent.c revision 848b8605
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#ifdef HAVE_PTHREAD
37#include <pthread.h>
38#endif
39
40#include "glxclient.h"
41
42#include "glapi.h"
43
44/*
45** We setup some dummy structures here so that the API can be used
46** even if no context is current.
47*/
48
49static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
50static struct glx_context_vtable dummyVtable;
51/*
52** Dummy context used by small commands when there is no current context.
53** All the
54** gl and glx entry points are designed to operate as nop's when using
55** the dummy context structure.
56*/
57struct glx_context dummyContext = {
58   &dummyBuffer[0],
59   &dummyBuffer[0],
60   &dummyBuffer[0],
61   &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
62   sizeof(dummyBuffer),
63   &dummyVtable
64};
65
66/*
67 * Current context management and locking
68 */
69
70#if defined( HAVE_PTHREAD )
71
72_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
73
74# if defined( GLX_USE_TLS )
75
76/**
77 * Per-thread GLX context pointer.
78 *
79 * \c __glXSetCurrentContext is written is such a way that this pointer can
80 * \b never be \c NULL.  This is important!  Because of this
81 * \c __glXGetCurrentContext can be implemented as trivial macro.
82 */
83__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
84   = &dummyContext;
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#elif defined( THREADS )
142
143#error Unknown threading method specified.
144
145#else
146
147/* not thread safe */
148_X_HIDDEN struct glx_context *__glXcurrentContext = &dummyContext;
149
150#endif
151
152
153_X_HIDDEN void
154__glXSetCurrentContextNull(void)
155{
156   __glXSetCurrentContext(&dummyContext);
157#if defined(GLX_DIRECT_RENDERING)
158   _glapi_set_dispatch(NULL);   /* no-op functions */
159   _glapi_set_context(NULL);
160#endif
161}
162
163_X_EXPORT GLXContext
164glXGetCurrentContext(void)
165{
166   struct glx_context *cx = __glXGetCurrentContext();
167
168   if (cx == &dummyContext) {
169      return NULL;
170   }
171   else {
172      return (GLXContext) cx;
173   }
174}
175
176_X_EXPORT GLXDrawable
177glXGetCurrentDrawable(void)
178{
179   struct glx_context *gc = __glXGetCurrentContext();
180   return gc->currentDrawable;
181}
182
183static void
184__glXGenerateError(Display * dpy, XID resource,
185                   BYTE errorCode, CARD16 minorCode)
186{
187   xError error;
188
189   error.errorCode = errorCode;
190   error.resourceID = resource;
191   error.sequenceNumber = dpy->request;
192   error.type = X_Error;
193   error.majorCode = __glXSetupForCommand(dpy);
194   error.minorCode = minorCode;
195   _XError(dpy, &error);
196}
197
198/**
199 * Make a particular context current.
200 *
201 * \note This is in this file so that it can access dummyContext.
202 */
203static Bool
204MakeContextCurrent(Display * dpy, GLXDrawable draw,
205                   GLXDrawable read, GLXContext gc_user)
206{
207   struct glx_context *gc = (struct glx_context *) gc_user;
208   struct glx_context *oldGC = __glXGetCurrentContext();
209
210   /* Make sure that the new context has a nonzero ID.  In the request,
211    * a zero context ID is used only to mean that we bind to no current
212    * context.
213    */
214   if ((gc != NULL) && (gc->xid == None)) {
215      return GL_FALSE;
216   }
217
218   _glapi_check_multithread();
219
220   __glXLock();
221   if (oldGC == gc &&
222       gc->currentDrawable == draw && gc->currentReadable == read) {
223      __glXUnlock();
224      return True;
225   }
226
227   if (oldGC != &dummyContext) {
228      if (--oldGC->thread_refcount == 0) {
229	 oldGC->vtable->unbind(oldGC, gc);
230	 oldGC->currentDpy = 0;
231      }
232   }
233
234   if (gc) {
235      /* Attempt to bind the context.  We do this before mucking with
236       * gc and __glXSetCurrentContext to properly handle our state in
237       * case of an error.
238       *
239       * If an error occurs, set the Null context since we've already
240       * blown away our old context.  The caller is responsible for
241       * figuring out how to handle setting a valid context.
242       */
243      if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
244         __glXSetCurrentContextNull();
245         __glXUnlock();
246         __glXGenerateError(dpy, None, GLXBadContext, X_GLXMakeContextCurrent);
247         return GL_FALSE;
248      }
249
250      if (gc->thread_refcount == 0) {
251         gc->currentDpy = dpy;
252         gc->currentDrawable = draw;
253         gc->currentReadable = read;
254      }
255      gc->thread_refcount++;
256      __glXSetCurrentContext(gc);
257   } else {
258      __glXSetCurrentContextNull();
259   }
260
261   if (oldGC->thread_refcount == 0 && oldGC != &dummyContext && oldGC->xid == None) {
262      /* We are switching away from a context that was
263       * previously destroyed, so we need to free the memory
264       * for the old handle. */
265      oldGC->vtable->destroy(oldGC);
266   }
267
268   __glXUnlock();
269
270   return GL_TRUE;
271}
272
273
274_X_EXPORT Bool
275glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
276{
277   return MakeContextCurrent(dpy, draw, draw, gc);
278}
279
280_X_EXPORT
281GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
282          (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
283          (dpy, d, r, ctx), MakeContextCurrent)
284
285_X_EXPORT
286GLX_ALIAS(Bool, glXMakeContextCurrent,
287          (Display * dpy, GLXDrawable d, GLXDrawable r,
288           GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)
289