glxcurrent.c revision 659dab6be6bc82f8a0551c30f1133e0010f04ed8
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 PTHREADS
37#include <pthread.h>
38#endif
39
40#include "glxclient.h"
41#ifdef GLX_USE_APPLEGL
42#include <stdlib.h>
43
44#include "apple_glx.h"
45#include "apple_glx_context.h"
46#else
47#include "glapi.h"
48#endif
49
50/*
51** We setup some dummy structures here so that the API can be used
52** even if no context is current.
53*/
54
55static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
56static struct glx_context_vtable dummyVtable;
57/*
58** Dummy context used by small commands when there is no current context.
59** All the
60** gl and glx entry points are designed to operate as nop's when using
61** the dummy context structure.
62*/
63struct glx_context dummyContext = {
64   &dummyBuffer[0],
65   &dummyBuffer[0],
66   &dummyBuffer[0],
67   &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
68   sizeof(dummyBuffer),
69   &dummyVtable
70};
71
72/*
73 * Current context management and locking
74 */
75
76#if defined( PTHREADS )
77
78_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
79
80# if defined( GLX_USE_TLS )
81
82/**
83 * Per-thread GLX context pointer.
84 *
85 * \c __glXSetCurrentContext is written is such a way that this pointer can
86 * \b never be \c NULL.  This is important!  Because of this
87 * \c __glXGetCurrentContext can be implemented as trivial macro.
88 */
89__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
90   = &dummyContext;
91
92_X_HIDDEN void
93__glXSetCurrentContext(struct glx_context * c)
94{
95   __glX_tls_Context = (c != NULL) ? c : &dummyContext;
96}
97
98# else
99
100static pthread_once_t once_control = PTHREAD_ONCE_INIT;
101
102/**
103 * Per-thread data key.
104 *
105 * Once \c init_thread_data has been called, the per-thread data key will
106 * take a value of \c NULL.  As each new thread is created the default
107 * value, in that thread, will be \c NULL.
108 */
109static pthread_key_t ContextTSD;
110
111/**
112 * Initialize the per-thread data key.
113 *
114 * This function is called \b exactly once per-process (not per-thread!) to
115 * initialize the per-thread data key.  This is ideally done using the
116 * \c pthread_once mechanism.
117 */
118static void
119init_thread_data(void)
120{
121   if (pthread_key_create(&ContextTSD, NULL) != 0) {
122      perror("pthread_key_create");
123      exit(-1);
124   }
125}
126
127_X_HIDDEN void
128__glXSetCurrentContext(struct glx_context * c)
129{
130   pthread_once(&once_control, init_thread_data);
131   pthread_setspecific(ContextTSD, c);
132}
133
134_X_HIDDEN struct glx_context *
135__glXGetCurrentContext(void)
136{
137   void *v;
138
139   pthread_once(&once_control, init_thread_data);
140
141   v = pthread_getspecific(ContextTSD);
142   return (v == NULL) ? &dummyContext : (struct glx_context *) v;
143}
144
145# endif /* defined( GLX_USE_TLS ) */
146
147#elif defined( THREADS )
148
149#error Unknown threading method specified.
150
151#else
152
153/* not thread safe */
154_X_HIDDEN struct glx_context *__glXcurrentContext = &dummyContext;
155
156#endif
157
158
159_X_HIDDEN void
160__glXSetCurrentContextNull(void)
161{
162   __glXSetCurrentContext(&dummyContext);
163#ifndef GLX_USE_APPLEGL
164#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
165   _glapi_set_dispatch(NULL);   /* no-op functions */
166   _glapi_set_context(NULL);
167#endif
168#endif
169}
170
171_X_EXPORT GLXContext
172glXGetCurrentContext(void)
173{
174   struct glx_context *cx = __glXGetCurrentContext();
175
176   if (cx == &dummyContext) {
177      return NULL;
178   }
179   else {
180      return (GLXContext) cx;
181   }
182}
183
184_X_EXPORT GLXDrawable
185glXGetCurrentDrawable(void)
186{
187   struct glx_context *gc = __glXGetCurrentContext();
188   return gc->currentDrawable;
189}
190
191static void
192__glXGenerateError(Display * dpy, struct glx_context *gc, XID resource,
193                   BYTE errorCode, CARD16 minorCode)
194{
195   xError error;
196
197   error.errorCode = errorCode;
198   error.resourceID = resource;
199   error.sequenceNumber = dpy->request;
200   error.type = X_Error;
201   error.majorCode = gc->majorOpcode;
202   error.minorCode = minorCode;
203   _XError(dpy, &error);
204}
205
206/**
207 * Make a particular context current.
208 *
209 * \note This is in this file so that it can access dummyContext.
210 */
211static Bool
212MakeContextCurrent(Display * dpy, GLXDrawable draw,
213                   GLXDrawable read, GLXContext gc_user)
214{
215   struct glx_context *gc = (struct glx_context *) gc_user;
216   struct glx_context *oldGC = __glXGetCurrentContext();
217   int ret = Success;
218
219   /* Make sure that the new context has a nonzero ID.  In the request,
220    * a zero context ID is used only to mean that we bind to no current
221    * context.
222    */
223   if ((gc != NULL) && (gc->xid == None)) {
224      return GL_FALSE;
225   }
226
227   if (gc == NULL && (draw != None || read != None)) {
228      __glXGenerateError(dpy, gc, (draw != None) ? draw : read,
229                         BadMatch, X_GLXMakeContextCurrent);
230      return False;
231   }
232   if (gc != NULL && (draw == None || read == None)) {
233      __glXGenerateError(dpy, gc, None, BadMatch, X_GLXMakeContextCurrent);
234      return False;
235   }
236
237   _glapi_check_multithread();
238
239   if (gc != NULL && gc->thread_id != 0 && gc->thread_id != _glthread_GetID()) {
240      __glXGenerateError(dpy, gc, gc->xid,
241                         BadAccess, X_GLXMakeContextCurrent);
242      return False;
243   }
244
245   if (oldGC != &dummyContext) {
246      oldGC->vtable->unbind(oldGC, gc);
247      oldGC->currentDpy = 0;
248      oldGC->currentDrawable = None;
249      oldGC->currentReadable = None;
250      oldGC->thread_id = 0;
251   }
252
253   if (gc) {
254      gc->currentDpy = dpy;
255      gc->currentDrawable = draw;
256      gc->currentReadable = read;
257      gc->thread_id = _glthread_GetID();
258      __glXSetCurrentContext(gc);
259      ret = gc->vtable->bind(gc, oldGC, draw, read);
260   } else {
261      __glXSetCurrentContextNull();
262   }
263
264   if (oldGC != &dummyContext && oldGC->xid == None && oldGC != gc) {
265      /* We are switching away from a context that was
266       * previously destroyed, so we need to free the memory
267       * for the old handle. */
268      oldGC->vtable->destroy(oldGC);
269   }
270
271   if (ret) {
272      __glXGenerateError(dpy, gc, None, ret, X_GLXMakeContextCurrent);
273      return GL_FALSE;
274   }
275
276   return GL_TRUE;
277}
278
279
280_X_EXPORT Bool
281glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
282{
283   return MakeContextCurrent(dpy, draw, draw, gc);
284}
285
286_X_EXPORT
287GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
288          (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
289          (dpy, d, r, ctx), MakeContextCurrent)
290
291_X_EXPORT
292GLX_ALIAS(Bool, glXMakeContextCurrent,
293          (Display * dpy, GLXDrawable d, GLXDrawable r,
294           GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)
295