Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * 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 * 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.
23 */
24
25
26 /*
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.
33 *
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.
37 *
38 * NOTE: There are no dependencies on Mesa in this code.
39 *
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.
49 */
50
51
52
53 #ifdef HAVE_DIX_CONFIG_H
54
55 #include <dix-config.h>
56 #define PUBLIC
57
58 #else
59
60 #include "main/glheader.h"
61
62 #endif
63
64 #include <stdlib.h>
65 #include <string.h>
66 #ifdef DEBUG
67 #include <assert.h>
68 #endif
69
70 #include "glapi.h"
71 #include "glapioffsets.h"
72 #include "glapitable.h"
73
74
75 /***** BEGIN NO-OP DISPATCH *****/
76
77 static GLboolean WarnFlag = GL_FALSE;
78 static _glapi_warning_func warning_func;
79
80 /*
81 * Enable/disable printing of warning messages.
82 */
83 PUBLIC void
84 _glapi_noop_enable_warnings(GLboolean enable)
85 {
86 WarnFlag = enable;
87 }
88
89 /*
90 * Register a callback function for reporting errors.
91 */
92 PUBLIC void
93 _glapi_set_warning_func( _glapi_warning_func func )
94 {
95 warning_func = func;
96 }
97
98 static GLboolean
99 warn(void)
100 {
101 #if !defined(_WIN32_WCE)
102 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
103 && warning_func) {
104 return GL_TRUE;
105 }
106 else {
107 return GL_FALSE;
108 }
109 #else
110 return GL_FALSE;
111 #endif
112 }
113
114
115 #define KEYWORD1 static
116 #define KEYWORD1_ALT static
117 #define KEYWORD2 GLAPIENTRY
118 #define NAME(func) NoOp##func
119
120 #define F NULL
121
122 #define DISPATCH(func, args, msg) \
123 if (warn()) { \
124 warning_func(NULL, "GL User Error: called without context: %s", #func); \
125 }
126
127 #define RETURN_DISPATCH(func, args, msg) \
128 if (warn()) { \
129 warning_func(NULL, "GL User Error: called without context: %s", #func); \
130 } \
131 return 0
132
133 #define DISPATCH_TABLE_NAME __glapi_noop_table
134 #define UNUSED_TABLE_NAME __unused_noop_functions
135
136 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
137
138 static GLint NoOpUnused(void)
139 {
140 if (warn()) {
141 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
142 }
143 return 0;
144 }
145
146 #include "glapitemp.h"
147
148 /***** END NO-OP DISPATCH *****/
149
150
151
152 /**
153 * \name Current dispatch and current context control variables
154 *
155 * Depending on whether or not multithreading is support, and the type of
156 * support available, several variables are used to store the current context
157 * pointer and the current dispatch table pointer. In the non-threaded case,
158 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
159 * purpose.
160 *
161 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
162 * \c _glapi_Context will be \c NULL if an application is detected as being
163 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
164 * and \c _glapi_Context just like the case without any threading support.
165 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
166 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
167 * static dispatch functions access these variables via \c _glapi_get_dispatch
168 * and \c _glapi_get_context.
169 *
170 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
171 * possible for the original thread to be setting it at the same instant a new
172 * thread, perhaps running on a different processor, is clearing it. Because
173 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
174 * used to determine whether or not the application is multithreaded.
175 *
176 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
177 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
178 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
179 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
180 * between TLS enabled loaders and non-TLS DRI drivers.
181 */
182 /*@{*/
183 #if defined(GLX_USE_TLS)
184
185 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
186 __attribute__((tls_model("initial-exec")))
187 = (struct _glapi_table *) __glapi_noop_table;
188
189 PUBLIC __thread void * _glapi_tls_Context
190 __attribute__((tls_model("initial-exec")));
191
192 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
193 PUBLIC const void *_glapi_Context = NULL;
194
195 #else
196
197 #if defined(THREADS)
198
199 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
200 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
201 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
202
203 #if defined(WIN32_THREADS)
204 void FreeTSD(_glthread_TSD *p);
205 void FreeAllTSD(void)
206 {
207 FreeTSD(&_gl_DispatchTSD);
208 FreeTSD(&ContextTSD);
209 }
210 #endif /* defined(WIN32_THREADS) */
211
212 #endif /* defined(THREADS) */
213
214 PUBLIC struct _glapi_table *_glapi_Dispatch =
215 (struct _glapi_table *) __glapi_noop_table;
216 PUBLIC void *_glapi_Context = NULL;
217
218 #endif /* defined(GLX_USE_TLS) */
219 /*@}*/
220
221
222
223
224 /**
225 * We should call this periodically from a function such as glXMakeCurrent
226 * in order to test if multiple threads are being used.
227 */
228 void
229 _glapi_check_multithread(void)
230 {
231 #if defined(THREADS) && !defined(GLX_USE_TLS)
232 if (!ThreadSafe) {
233 static unsigned long knownID;
234 static GLboolean firstCall = GL_TRUE;
235 if (firstCall) {
236 knownID = _glthread_GetID();
237 firstCall = GL_FALSE;
238 }
239 else if (knownID != _glthread_GetID()) {
240 ThreadSafe = GL_TRUE;
241 _glapi_set_dispatch(NULL);
242 _glapi_set_context(NULL);
243 }
244 }
245 else if (!_glapi_get_dispatch()) {
246 /* make sure that this thread's dispatch pointer isn't null */
247 _glapi_set_dispatch(NULL);
248 }
249 #endif
250 }
251
252
253
254 /**
255 * Set the current context pointer for this thread.
256 * The context pointer is an opaque type which should be cast to
257 * void from the real context pointer type.
258 */
259 PUBLIC void
260 _glapi_set_context(void *context)
261 {
262 (void) __unused_noop_functions; /* silence a warning */
263 #if defined(GLX_USE_TLS)
264 _glapi_tls_Context = context;
265 #elif defined(THREADS)
266 _glthread_SetTSD(&ContextTSD, context);
267 _glapi_Context = (ThreadSafe) ? NULL : context;
268 #else
269 _glapi_Context = context;
270 #endif
271 }
272
273
274
275 /**
276 * Get the current context pointer for this thread.
277 * The context pointer is an opaque type which should be cast from
278 * void to the real context pointer type.
279 */
280 PUBLIC void *
281 _glapi_get_context(void)
282 {
283 #if defined(GLX_USE_TLS)
284 return _glapi_tls_Context;
285 #elif defined(THREADS)
286 if (ThreadSafe) {
287 return _glthread_GetTSD(&ContextTSD);
288 }
289 else {
290 return _glapi_Context;
291 }
292 #else
293 return _glapi_Context;
294 #endif
295 }
296
297 #ifdef USE_X86_ASM
298
299 #if defined( GLX_USE_TLS )
300 extern GLubyte gl_dispatch_functions_start[];
301 extern GLubyte gl_dispatch_functions_end[];
302 #else
303 extern const GLubyte gl_dispatch_functions_start[];
304 #endif
305
306 #endif /* USE_X86_ASM */
307
308
309 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
310 # define DISPATCH_FUNCTION_SIZE 16
311 #elif defined(USE_X86_ASM)
312 # if defined(THREADS) && !defined(GLX_USE_TLS)
313 # define DISPATCH_FUNCTION_SIZE 32
314 # else
315 # define DISPATCH_FUNCTION_SIZE 16
316 # endif
317 #endif
318
319 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
320 # define NEED_FUNCTION_POINTER
321 #endif
322
323 #if defined(PTHREADS) || defined(GLX_USE_TLS)
324 /**
325 * Perform platform-specific GL API entry-point fixups.
326 */
327 static void
328 init_glapi_relocs( void )
329 {
330 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
331 extern unsigned long _x86_get_dispatch(void);
332 char run_time_patch[] = {
333 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
334 };
335 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
336 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
337 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
338
339 *offset = _x86_get_dispatch();
340 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
341 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
342 curr_func += DISPATCH_FUNCTION_SIZE;
343 }
344 #endif
345 }
346 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
347
348
349 /**
350 * Set the global or per-thread dispatch table pointer.
351 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
352 * table (__glapi_noop_table).
353 */
354 PUBLIC void
355 _glapi_set_dispatch(struct _glapi_table *dispatch)
356 {
357 #if defined(PTHREADS) || defined(GLX_USE_TLS)
358 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
359 pthread_once( & once_control, init_glapi_relocs );
360 #endif
361
362 if (!dispatch) {
363 /* use the no-op functions */
364 dispatch = (struct _glapi_table *) __glapi_noop_table;
365 }
366 #ifdef DEBUG
367 else {
368 _glapi_check_table(dispatch);
369 }
370 #endif
371
372 #if defined(GLX_USE_TLS)
373 _glapi_tls_Dispatch = dispatch;
374 #elif defined(THREADS)
375 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
376 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
377 #else /*THREADS*/
378 _glapi_Dispatch = dispatch;
379 #endif /*THREADS*/
380 }
381
382
383
384 /**
385 * Return pointer to current dispatch table for calling thread.
386 */
387 PUBLIC struct _glapi_table *
388 _glapi_get_dispatch(void)
389 {
390 struct _glapi_table * api;
391 #if defined(GLX_USE_TLS)
392 api = _glapi_tls_Dispatch;
393 #elif defined(THREADS)
394 api = (ThreadSafe)
395 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
396 : _glapi_Dispatch;
397 #else
398 api = _glapi_Dispatch;
399 #endif
400 return api;
401 }
402
403
404
405
406 /*
407 * The dispatch table size (number of entries) is the size of the
408 * _glapi_table struct plus the number of dynamic entries we can add.
409 * The extra slots can be filled in by DRI drivers that register new extension
410 * functions.
411 */
412 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
413
414
415 /**
416 * Return size of dispatch table struct as number of functions (or
417 * slots).
418 */
419 PUBLIC GLuint
420 _glapi_get_dispatch_table_size(void)
421 {
422 return DISPATCH_TABLE_SIZE;
423 }
424
425
426 /**
427 * Make sure there are no NULL pointers in the given dispatch table.
428 * Intended for debugging purposes.
429 */
430 void
431 _glapi_check_table(const struct _glapi_table *table)
432 {
433 #ifdef EXTRA_DEBUG
434 const GLuint entries = _glapi_get_dispatch_table_size();
435 const void **tab = (const void **) table;
436 GLuint i;
437 for (i = 1; i < entries; i++) {
438 assert(tab[i]);
439 }
440
441 /* Do some spot checks to be sure that the dispatch table
442 * slots are assigned correctly.
443 */
444 {
445 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
446 char *BeginFunc = (char*) &table->Begin;
447 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
448 assert(BeginOffset == _gloffset_Begin);
449 assert(BeginOffset == offset);
450 }
451 {
452 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
453 char *viewportFunc = (char*) &table->Viewport;
454 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
455 assert(viewportOffset == _gloffset_Viewport);
456 assert(viewportOffset == offset);
457 }
458 {
459 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
460 char *VertexPointerFunc = (char*) &table->VertexPointer;
461 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
462 assert(VertexPointerOffset == _gloffset_VertexPointer);
463 assert(VertexPointerOffset == offset);
464 }
465 {
466 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
467 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
468 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
469 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
470 assert(ResetMinMaxOffset == offset);
471 }
472 {
473 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
474 char *blendColorFunc = (char*) &table->BlendColor;
475 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
476 assert(blendColorOffset == _gloffset_BlendColor);
477 assert(blendColorOffset == offset);
478 }
479 {
480 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
481 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
482 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
483 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
484 assert(secondaryColor3fOffset == offset);
485 }
486 {
487 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
488 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
489 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
490 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
491 assert(pointParameterivOffset == offset);
492 }
493 {
494 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
495 char *setFenceFunc = (char*) &table->SetFenceNV;
496 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
497 assert(setFenceOffset == _gloffset_SetFenceNV);
498 assert(setFenceOffset == offset);
499 }
500 #else
501 (void) table;
502 #endif
503 }