2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * This file manages the OpenGL API dispatch layer.
28 * The dispatch table (struct _glapi_table) is basically just a list
29 * of function pointers.
30 * There are functions to set/get the current dispatch table for the
31 * current thread and to manage registration/dispatch of dynamically
32 * added extension functions.
34 * It's intended that this file and the other glapi*.[ch] files are
35 * flexible enough to be reused in several places: XFree86, DRI-
36 * based libGL.so, and perhaps the SGI SI.
38 * NOTE: There are no dependencies on Mesa in this code.
40 * Versions (API changes):
41 * 2000/02/23 - original version for Mesa 3.3 and XFree86 4.0
42 * 2001/01/16 - added dispatch override feature for Mesa 3.5
43 * 2002/06/28 - added _glapi_set_warning_func(), Mesa 4.1.
44 * 2002/10/01 - _glapi_get_proc_address() will now generate new entrypoints
45 * itself (using offset ~0). _glapi_add_entrypoint() can be
46 * called afterward and it'll fill in the correct dispatch
47 * offset. This allows DRI libGL to avoid probing for DRI
48 * drivers! No changes to the public glapi interface.
53 #ifdef HAVE_DIX_CONFIG_H
55 #include <dix-config.h>
60 #include "main/glheader.h"
71 #include "glapioffsets.h"
72 #include "glapitable.h"
75 /***** BEGIN NO-OP DISPATCH *****/
77 static GLboolean WarnFlag
= GL_FALSE
;
78 static _glapi_warning_func warning_func
;
81 * Enable/disable printing of warning messages.
84 _glapi_noop_enable_warnings(GLboolean enable
)
90 * Register a callback function for reporting errors.
93 _glapi_set_warning_func( _glapi_warning_func func
)
101 if ((WarnFlag
|| getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
111 #define KEYWORD1 static
112 #define KEYWORD1_ALT static
113 #define KEYWORD2 GLAPIENTRY
114 #define NAME(func) NoOp##func
118 #define DISPATCH(func, args, msg) \
120 warning_func(NULL, "GL User Error: called without context: %s", #func); \
123 #define RETURN_DISPATCH(func, args, msg) \
125 warning_func(NULL, "GL User Error: called without context: %s", #func); \
129 #define DISPATCH_TABLE_NAME __glapi_noop_table
130 #define UNUSED_TABLE_NAME __unused_noop_functions
132 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
134 static GLint
NoOpUnused(void)
137 warning_func(NULL
, "GL User Error: calling extension function without a current context\n");
142 #include "glapitemp.h"
144 /***** END NO-OP DISPATCH *****/
149 * \name Current dispatch and current context control variables
151 * Depending on whether or not multithreading is support, and the type of
152 * support available, several variables are used to store the current context
153 * pointer and the current dispatch table pointer. In the non-threaded case,
154 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
157 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
158 * \c _glapi_Context will be \c NULL if an application is detected as being
159 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
160 * and \c _glapi_Context just like the case without any threading support.
161 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
162 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
163 * static dispatch functions access these variables via \c _glapi_get_dispatch
164 * and \c _glapi_get_context.
166 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
167 * possible for the original thread to be setting it at the same instant a new
168 * thread, perhaps running on a different processor, is clearing it. Because
169 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
170 * used to determine whether or not the application is multithreaded.
172 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
173 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
174 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
175 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
176 * between TLS enabled loaders and non-TLS DRI drivers.
179 #if defined(GLX_USE_TLS)
181 PUBLIC __thread
struct _glapi_table
* _glapi_tls_Dispatch
182 __attribute__((tls_model("initial-exec")))
183 = (struct _glapi_table
*) __glapi_noop_table
;
185 PUBLIC __thread
void * _glapi_tls_Context
186 __attribute__((tls_model("initial-exec")));
188 PUBLIC
const struct _glapi_table
*_glapi_Dispatch
= NULL
;
189 PUBLIC
const void *_glapi_Context
= NULL
;
195 static GLboolean ThreadSafe
= GL_FALSE
; /**< In thread-safe mode? */
196 _glthread_TSD _gl_DispatchTSD
; /**< Per-thread dispatch pointer */
197 static _glthread_TSD ContextTSD
; /**< Per-thread context pointer */
199 #if defined(WIN32_THREADS)
200 void FreeTSD(_glthread_TSD
*p
);
201 void FreeAllTSD(void)
203 FreeTSD(&_gl_DispatchTSD
);
204 FreeTSD(&ContextTSD
);
206 #endif /* defined(WIN32_THREADS) */
208 #endif /* defined(THREADS) */
210 PUBLIC
struct _glapi_table
*_glapi_Dispatch
=
211 (struct _glapi_table
*) __glapi_noop_table
;
212 PUBLIC
void *_glapi_Context
= NULL
;
214 #endif /* defined(GLX_USE_TLS) */
221 * We should call this periodically from a function such as glXMakeCurrent
222 * in order to test if multiple threads are being used.
225 _glapi_check_multithread(void)
227 #if defined(THREADS) && !defined(GLX_USE_TLS)
229 static unsigned long knownID
;
230 static GLboolean firstCall
= GL_TRUE
;
232 knownID
= _glthread_GetID();
233 firstCall
= GL_FALSE
;
235 else if (knownID
!= _glthread_GetID()) {
236 ThreadSafe
= GL_TRUE
;
237 _glapi_set_dispatch(NULL
);
238 _glapi_set_context(NULL
);
241 else if (!_glapi_get_dispatch()) {
242 /* make sure that this thread's dispatch pointer isn't null */
243 _glapi_set_dispatch(NULL
);
251 * Set the current context pointer for this thread.
252 * The context pointer is an opaque type which should be cast to
253 * void from the real context pointer type.
256 _glapi_set_context(void *context
)
258 (void) __unused_noop_functions
; /* silence a warning */
259 #if defined(GLX_USE_TLS)
260 _glapi_tls_Context
= context
;
261 #elif defined(THREADS)
262 _glthread_SetTSD(&ContextTSD
, context
);
263 _glapi_Context
= (ThreadSafe
) ? NULL
: context
;
265 _glapi_Context
= context
;
272 * Get the current context pointer for this thread.
273 * The context pointer is an opaque type which should be cast from
274 * void to the real context pointer type.
277 _glapi_get_context(void)
279 #if defined(GLX_USE_TLS)
280 return _glapi_tls_Context
;
281 #elif defined(THREADS)
283 return _glthread_GetTSD(&ContextTSD
);
286 return _glapi_Context
;
289 return _glapi_Context
;
295 #if defined(PTHREADS) || defined(GLX_USE_TLS)
297 * Perform platform-specific GL API entry-point fixups.
300 init_glapi_relocs( void )
302 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
303 extern unsigned long _x86_get_dispatch(void);
304 char run_time_patch
[] = {
305 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
307 GLuint
*offset
= (GLuint
*) &run_time_patch
[2]; /* 32-bits for x86/32 */
308 const GLubyte
* const get_disp
= (const GLubyte
*) run_time_patch
;
309 GLubyte
* curr_func
= (GLubyte
*) gl_dispatch_functions_start
;
311 *offset
= _x86_get_dispatch();
312 while ( curr_func
!= (GLubyte
*) gl_dispatch_functions_end
) {
313 (void) memcpy( curr_func
, get_disp
, sizeof(run_time_patch
));
314 curr_func
+= DISPATCH_FUNCTION_SIZE
;
318 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
322 * Set the global or per-thread dispatch table pointer.
323 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
324 * table (__glapi_noop_table).
327 _glapi_set_dispatch(struct _glapi_table
*dispatch
)
329 #if defined(PTHREADS) || defined(GLX_USE_TLS)
330 static pthread_once_t once_control
= PTHREAD_ONCE_INIT
;
331 pthread_once( & once_control
, init_glapi_relocs
);
335 /* use the no-op functions */
336 dispatch
= (struct _glapi_table
*) __glapi_noop_table
;
340 _glapi_check_table(dispatch
);
344 #if defined(GLX_USE_TLS)
345 _glapi_tls_Dispatch
= dispatch
;
346 #elif defined(THREADS)
347 _glthread_SetTSD(&_gl_DispatchTSD
, (void *) dispatch
);
348 _glapi_Dispatch
= (ThreadSafe
) ? NULL
: dispatch
;
350 _glapi_Dispatch
= dispatch
;
357 * Return pointer to current dispatch table for calling thread.
359 PUBLIC
struct _glapi_table
*
360 _glapi_get_dispatch(void)
362 struct _glapi_table
* api
;
363 #if defined(GLX_USE_TLS)
364 api
= _glapi_tls_Dispatch
;
365 #elif defined(THREADS)
367 ? (struct _glapi_table
*) _glthread_GetTSD(&_gl_DispatchTSD
)
370 api
= _glapi_Dispatch
;
379 * The dispatch table size (number of entries) is the size of the
380 * _glapi_table struct plus the number of dynamic entries we can add.
381 * The extra slots can be filled in by DRI drivers that register new extension
384 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
388 * Return size of dispatch table struct as number of functions (or
392 _glapi_get_dispatch_table_size(void)
394 return DISPATCH_TABLE_SIZE
;
399 * Make sure there are no NULL pointers in the given dispatch table.
400 * Intended for debugging purposes.
403 _glapi_check_table(const struct _glapi_table
*table
)
406 const GLuint entries
= _glapi_get_dispatch_table_size();
407 const void **tab
= (const void **) table
;
409 for (i
= 1; i
< entries
; i
++) {
413 /* Do some spot checks to be sure that the dispatch table
414 * slots are assigned correctly.
417 GLuint BeginOffset
= _glapi_get_proc_offset("glBegin");
418 char *BeginFunc
= (char*) &table
->Begin
;
419 GLuint offset
= (BeginFunc
- (char *) table
) / sizeof(void *);
420 assert(BeginOffset
== _gloffset_Begin
);
421 assert(BeginOffset
== offset
);
424 GLuint viewportOffset
= _glapi_get_proc_offset("glViewport");
425 char *viewportFunc
= (char*) &table
->Viewport
;
426 GLuint offset
= (viewportFunc
- (char *) table
) / sizeof(void *);
427 assert(viewportOffset
== _gloffset_Viewport
);
428 assert(viewportOffset
== offset
);
431 GLuint VertexPointerOffset
= _glapi_get_proc_offset("glVertexPointer");
432 char *VertexPointerFunc
= (char*) &table
->VertexPointer
;
433 GLuint offset
= (VertexPointerFunc
- (char *) table
) / sizeof(void *);
434 assert(VertexPointerOffset
== _gloffset_VertexPointer
);
435 assert(VertexPointerOffset
== offset
);
438 GLuint ResetMinMaxOffset
= _glapi_get_proc_offset("glResetMinmax");
439 char *ResetMinMaxFunc
= (char*) &table
->ResetMinmax
;
440 GLuint offset
= (ResetMinMaxFunc
- (char *) table
) / sizeof(void *);
441 assert(ResetMinMaxOffset
== _gloffset_ResetMinmax
);
442 assert(ResetMinMaxOffset
== offset
);
445 GLuint blendColorOffset
= _glapi_get_proc_offset("glBlendColor");
446 char *blendColorFunc
= (char*) &table
->BlendColor
;
447 GLuint offset
= (blendColorFunc
- (char *) table
) / sizeof(void *);
448 assert(blendColorOffset
== _gloffset_BlendColor
);
449 assert(blendColorOffset
== offset
);
452 GLuint secondaryColor3fOffset
= _glapi_get_proc_offset("glSecondaryColor3fEXT");
453 char *secondaryColor3fFunc
= (char*) &table
->SecondaryColor3fEXT
;
454 GLuint offset
= (secondaryColor3fFunc
- (char *) table
) / sizeof(void *);
455 assert(secondaryColor3fOffset
== _gloffset_SecondaryColor3fEXT
);
456 assert(secondaryColor3fOffset
== offset
);
459 GLuint pointParameterivOffset
= _glapi_get_proc_offset("glPointParameterivNV");
460 char *pointParameterivFunc
= (char*) &table
->PointParameterivNV
;
461 GLuint offset
= (pointParameterivFunc
- (char *) table
) / sizeof(void *);
462 assert(pointParameterivOffset
== _gloffset_PointParameterivNV
);
463 assert(pointParameterivOffset
== offset
);
466 GLuint setFenceOffset
= _glapi_get_proc_offset("glSetFenceNV");
467 char *setFenceFunc
= (char*) &table
->SetFenceNV
;
468 GLuint offset
= (setFenceFunc
- (char *) table
) / sizeof(void *);
469 assert(setFenceOffset
== _gloffset_SetFenceNV
);
470 assert(setFenceOffset
== offset
);