glxcurrent.c 7.32 KB
Newer Older
1
/*
Adam Jackson's avatar
Adam Jackson committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
 * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice including the dates of first publication and
 * either this permission notice or a reference to
 * http://oss.sgi.com/projects/FreeB/
 * shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of Silicon Graphics, Inc.
 * shall not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization from
 * Silicon Graphics, Inc.
 */
30
31
32
33
34
35

/**
 * \file glxcurrent.c
 * Client-side GLX interface for current context management.
 */

36
37
#include <pthread.h>

38
#include "glxclient.h"
39
#include "glapi.h"
40
#include "glx_error.h"
41

42
43
44
45
46
47
/*
** We setup some dummy structures here so that the API can be used
** even if no context is current.
*/

static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
48
static struct glx_context_vtable dummyVtable;
49
50
51
52
53
54
/*
** Dummy context used by small commands when there is no current context.
** All the
** gl and glx entry points are designed to operate as nop's when using
** the dummy context structure.
*/
55
struct glx_context dummyContext = {
56
57
58
59
60
   &dummyBuffer[0],
   &dummyBuffer[0],
   &dummyBuffer[0],
   &dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
   sizeof(dummyBuffer),
61
   &dummyVtable
62
63
64
65
66
67
68
69
};

/*
 * Current context management and locking
 */

_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;

70
# if defined( USE_ELF_TLS )
71
72
73

/**
 * Per-thread GLX context pointer.
74
 *
75
76
77
78
 * \c __glXSetCurrentContext is written is such a way that this pointer can
 * \b never be \c NULL.  This is important!  Because of this
 * \c __glXGetCurrentContext can be implemented as trivial macro.
 */
79
80
__thread void *__glX_tls_Context __attribute__ ((tls_model("initial-exec")))
   = &dummyContext;
81

82
_X_HIDDEN void
83
__glXSetCurrentContext(struct glx_context * c)
84
{
85
   __glX_tls_Context = (c != NULL) ? c : &dummyContext;
86
87
88
89
90
91
92
93
}

# else

static pthread_once_t once_control = PTHREAD_ONCE_INIT;

/**
 * Per-thread data key.
94
 *
95
96
97
98
99
100
101
102
 * Once \c init_thread_data has been called, the per-thread data key will
 * take a value of \c NULL.  As each new thread is created the default
 * value, in that thread, will be \c NULL.
 */
static pthread_key_t ContextTSD;

/**
 * Initialize the per-thread data key.
103
 *
104
105
106
107
 * This function is called \b exactly once per-process (not per-thread!) to
 * initialize the per-thread data key.  This is ideally done using the
 * \c pthread_once mechanism.
 */
108
109
static void
init_thread_data(void)
110
{
111
112
113
114
   if (pthread_key_create(&ContextTSD, NULL) != 0) {
      perror("pthread_key_create");
      exit(-1);
   }
115
116
}

117
_X_HIDDEN void
118
__glXSetCurrentContext(struct glx_context * c)
119
{
120
121
   pthread_once(&once_control, init_thread_data);
   pthread_setspecific(ContextTSD, c);
122
123
}

124
_X_HIDDEN struct glx_context *
125
__glXGetCurrentContext(void)
126
{
127
   void *v;
128

129
   pthread_once(&once_control, init_thread_data);
130

131
   v = pthread_getspecific(ContextTSD);
132
   return (v == NULL) ? &dummyContext : (struct glx_context *) v;
133
134
}

135
# endif /* defined( USE_ELF_TLS ) */
136
137


138
139
_X_HIDDEN void
__glXSetCurrentContextNull(void)
140
{
141
   __glXSetCurrentContext(&dummyContext);
142
#if defined(GLX_DIRECT_RENDERING)
143
   _glapi_set_dispatch(NULL);   /* no-op functions */
144
   _glapi_set_context(NULL);
145
146
147
#endif
}

148
_GLX_PUBLIC GLXContext
149
glXGetCurrentContext(void)
150
{
151
   struct glx_context *cx = __glXGetCurrentContext();
152
153
154
155
156

   if (cx == &dummyContext) {
      return NULL;
   }
   else {
157
      return (GLXContext) cx;
158
   }
159
160
}

161
_GLX_PUBLIC GLXDrawable
162
glXGetCurrentDrawable(void)
163
{
164
   struct glx_context *gc = __glXGetCurrentContext();
165
   return gc->currentDrawable;
166
167
}

168
169
170
171
172
/**
 * Make a particular context current.
 *
 * \note This is in this file so that it can access dummyContext.
 */
173
174
static Bool
MakeContextCurrent(Display * dpy, GLXDrawable draw,
175
                   GLXDrawable read, GLXContext gc_user)
176
{
177
178
   struct glx_context *gc = (struct glx_context *) gc_user;
   struct glx_context *oldGC = __glXGetCurrentContext();
179

180
181
182
183
184
185
   /* Make sure that the new context has a nonzero ID.  In the request,
    * a zero context ID is used only to mean that we bind to no current
    * context.
    */
   if ((gc != NULL) && (gc->xid == None)) {
      return GL_FALSE;
Thomas Hellstrom's avatar
Thomas Hellstrom committed
186
187
   }

188
   _glapi_check_multithread();
189

190
   __glXLock();
191
   if (oldGC == gc &&
192
193
194
       gc->currentDrawable == draw && gc->currentReadable == read) {
      __glXUnlock();
      return True;
195
196
   }

197
198
199
200
201
202
203
204
205
206
207
   /* can't have only one be 0 */
   if (!!draw != !!read) {
      __glXUnlock();
      __glXSendError(dpy, BadMatch, None, X_GLXMakeContextCurrent, True);
      return False;
   }

   if (oldGC != &dummyContext) {
      if (--oldGC->thread_refcount == 0) {
	 oldGC->vtable->unbind(oldGC, gc);
	 oldGC->currentDpy = 0;
208
      }
209
   }
210

211
212
213
214
215
216
217
218
219
220
   if (gc) {
      /* Attempt to bind the context.  We do this before mucking with
       * gc and __glXSetCurrentContext to properly handle our state in
       * case of an error.
       *
       * If an error occurs, set the Null context since we've already
       * blown away our old context.  The caller is responsible for
       * figuring out how to handle setting a valid context.
       */
      if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
221
         __glXSetCurrentContextNull();
222
223
224
225
         __glXUnlock();
         __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
                        False);
         return GL_FALSE;
226
227
      }

228
229
230
231
      if (gc->thread_refcount == 0) {
         gc->currentDpy = dpy;
         gc->currentDrawable = draw;
         gc->currentReadable = read;
232
      }
233
234
235
      gc->thread_refcount++;
      __glXSetCurrentContext(gc);
   } else {
236
      __glXSetCurrentContextNull();
237
   }
238

239
240
241
242
243
   if (oldGC->thread_refcount == 0 && oldGC != &dummyContext && oldGC->xid == None) {
      /* We are switching away from a context that was
       * previously destroyed, so we need to free the memory
       * for the old handle. */
      oldGC->vtable->destroy(oldGC);
244
245
   }

246
   __glXUnlock();
247
248

   return GL_TRUE;
249
250
251
}


252
_GLX_PUBLIC Bool
253
glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
254
{
255
   return MakeContextCurrent(dpy, draw, draw, gc);
256
257
}

258
_GLX_PUBLIC
259
260
GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
          (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
261
          (dpy, d, r, ctx), MakeContextCurrent)
262

263
_GLX_PUBLIC
264
GLX_ALIAS(Bool, glXMakeContextCurrent,
265
266
          (Display * dpy, GLXDrawable d, GLXDrawable r,
           GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)