631ffba3c7fdb399cb3d084b68ef5f0827af7069
[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 #include <dix-config.h>
55 #include "glapi/mesa.h"
56 #else
57 #include "main/glheader.h"
58 #include "main/compiler.h"
59 #endif
60
61 #include "glapi/glapi.h"
62 #include "glapi/glapioffsets.h"
63 #include "glapi/glapitable.h"
64
65 extern _glapi_proc __glapi_noop_table[];
66
67
68 /**
69 * \name Current dispatch and current context control variables
70 *
71 * Depending on whether or not multithreading is support, and the type of
72 * support available, several variables are used to store the current context
73 * pointer and the current dispatch table pointer. In the non-threaded case,
74 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
75 * purpose.
76 *
77 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
78 * \c _glapi_Context will be \c NULL if an application is detected as being
79 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
80 * and \c _glapi_Context just like the case without any threading support.
81 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
82 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
83 * static dispatch functions access these variables via \c _glapi_get_dispatch
84 * and \c _glapi_get_context.
85 *
86 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
87 * possible for the original thread to be setting it at the same instant a new
88 * thread, perhaps running on a different processor, is clearing it. Because
89 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
90 * used to determine whether or not the application is multithreaded.
91 *
92 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
93 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
94 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
95 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
96 * between TLS enabled loaders and non-TLS DRI drivers.
97 */
98 /*@{*/
99 #if defined(GLX_USE_TLS)
100
101 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
102 __attribute__((tls_model("initial-exec")))
103 = (struct _glapi_table *) __glapi_noop_table;
104
105 PUBLIC __thread void * _glapi_tls_Context
106 __attribute__((tls_model("initial-exec")));
107
108 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
109 PUBLIC const void *_glapi_Context = NULL;
110
111 #else
112
113 #if defined(THREADS)
114
115 #ifdef WIN32_THREADS
116 /* _glthread_DECLARE_STATIC_MUTEX is broken on windows. There will be race! */
117 #define CHECK_MULTITHREAD_LOCK()
118 #define CHECK_MULTITHREAD_UNLOCK()
119 #else
120 _glthread_DECLARE_STATIC_MUTEX(ThreadCheckMutex);
121 #define CHECK_MULTITHREAD_LOCK() _glthread_LOCK_MUTEX(ThreadCheckMutex)
122 #define CHECK_MULTITHREAD_UNLOCK() _glthread_UNLOCK_MUTEX(ThreadCheckMutex)
123 #endif
124
125 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
126 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
127 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
128
129 #if defined(WIN32_THREADS)
130 void FreeTSD(_glthread_TSD *p);
131 void FreeAllTSD(void)
132 {
133 FreeTSD(&_gl_DispatchTSD);
134 FreeTSD(&ContextTSD);
135 }
136 #endif /* defined(WIN32_THREADS) */
137
138 #endif /* defined(THREADS) */
139
140 PUBLIC struct _glapi_table *_glapi_Dispatch =
141 (struct _glapi_table *) __glapi_noop_table;
142 PUBLIC void *_glapi_Context = NULL;
143
144 #endif /* defined(GLX_USE_TLS) */
145 /*@}*/
146
147
148
149
150 /**
151 * We should call this periodically from a function such as glXMakeCurrent
152 * in order to test if multiple threads are being used.
153 */
154 PUBLIC void
155 _glapi_check_multithread(void)
156 {
157 #if defined(THREADS) && !defined(GLX_USE_TLS)
158 static unsigned long knownID;
159 static GLboolean firstCall = GL_TRUE;
160
161 if (ThreadSafe)
162 return;
163
164 CHECK_MULTITHREAD_LOCK();
165 if (firstCall) {
166 /* initialize TSDs */
167 (void) _glthread_GetTSD(&ContextTSD);
168 (void) _glthread_GetTSD(&_gl_DispatchTSD);
169
170 knownID = _glthread_GetID();
171 firstCall = GL_FALSE;
172 }
173 else if (knownID != _glthread_GetID()) {
174 ThreadSafe = GL_TRUE;
175 _glapi_set_dispatch(NULL);
176 _glapi_set_context(NULL);
177 }
178 CHECK_MULTITHREAD_UNLOCK();
179 #endif
180 }
181
182
183
184 /**
185 * Set the current context pointer for this thread.
186 * The context pointer is an opaque type which should be cast to
187 * void from the real context pointer type.
188 */
189 PUBLIC void
190 _glapi_set_context(void *context)
191 {
192 #if defined(GLX_USE_TLS)
193 _glapi_tls_Context = context;
194 #elif defined(THREADS)
195 _glthread_SetTSD(&ContextTSD, context);
196 _glapi_Context = (ThreadSafe) ? NULL : context;
197 #else
198 _glapi_Context = context;
199 #endif
200 }
201
202
203
204 /**
205 * Get the current context pointer for this thread.
206 * The context pointer is an opaque type which should be cast from
207 * void to the real context pointer type.
208 */
209 PUBLIC void *
210 _glapi_get_context(void)
211 {
212 #if defined(GLX_USE_TLS)
213 return _glapi_tls_Context;
214 #elif defined(THREADS)
215 if (ThreadSafe) {
216 return _glthread_GetTSD(&ContextTSD);
217 }
218 else {
219 return _glapi_Context;
220 }
221 #else
222 return _glapi_Context;
223 #endif
224 }
225
226 #ifdef USE_X86_ASM
227
228 #if defined( GLX_USE_TLS )
229 extern GLubyte gl_dispatch_functions_start[];
230 extern GLubyte gl_dispatch_functions_end[];
231 #else
232 extern const GLubyte gl_dispatch_functions_start[];
233 #endif
234
235 #endif /* USE_X86_ASM */
236
237
238 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
239 # define DISPATCH_FUNCTION_SIZE 16
240 #elif defined(USE_X86_ASM)
241 # if defined(THREADS) && !defined(GLX_USE_TLS)
242 # define DISPATCH_FUNCTION_SIZE 32
243 # else
244 # define DISPATCH_FUNCTION_SIZE 16
245 # endif
246 #endif
247
248 #ifdef USE_SPARC_ASM
249 #ifdef GLX_USE_TLS
250 extern unsigned int __glapi_sparc_tls_stub;
251 #else
252 extern unsigned int __glapi_sparc_pthread_stub;
253 #endif
254 #endif
255
256 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
257 # define NEED_FUNCTION_POINTER
258 #endif
259
260 #if defined(PTHREADS) || defined(GLX_USE_TLS)
261 /**
262 * Perform platform-specific GL API entry-point fixups.
263 */
264 static void
265 init_glapi_relocs( void )
266 {
267 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
268 extern unsigned long _x86_get_dispatch(void);
269 char run_time_patch[] = {
270 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
271 };
272 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
273 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
274 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
275
276 *offset = _x86_get_dispatch();
277 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
278 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
279 curr_func += DISPATCH_FUNCTION_SIZE;
280 }
281 #endif
282 #ifdef USE_SPARC_ASM
283 extern void __glapi_sparc_icache_flush(unsigned int *);
284 static const unsigned int template[] = {
285 #ifdef GLX_USE_TLS
286 0x05000000, /* sethi %hi(_glapi_tls_Dispatch), %g2 */
287 0x8730e00a, /* srl %g3, 10, %g3 */
288 0x8410a000, /* or %g2, %lo(_glapi_tls_Dispatch), %g2 */
289 #ifdef __arch64__
290 0xc259c002, /* ldx [%g7 + %g2], %g1 */
291 0xc2584003, /* ldx [%g1 + %g3], %g1 */
292 #else
293 0xc201c002, /* ld [%g7 + %g2], %g1 */
294 0xc2004003, /* ld [%g1 + %g3], %g1 */
295 #endif
296 0x81c04000, /* jmp %g1 */
297 0x01000000, /* nop */
298 #else
299 #ifdef __arch64__
300 0x03000000, /* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */
301 0x05000000, /* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */
302 0x82106000, /* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */
303 0x8730e00a, /* 64-bit 0x0c --> srl %g3, 10, %g3 */
304 0x83287020, /* 64-bit 0x10 --> sllx %g1, 32, %g1 */
305 0x82004002, /* 64-bit 0x14 --> add %g1, %g2, %g1 */
306 0xc2586000, /* 64-bit 0x18 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */
307 #else
308 0x03000000, /* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */
309 0x8730e00a, /* 32-bit 0x04 --> srl %g3, 10, %g3 */
310 0xc2006000, /* 32-bit 0x08 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
311 #endif
312 0x80a06000, /* --> cmp %g1, 0 */
313 0x02800005, /* --> be +4*5 */
314 0x01000000, /* --> nop */
315 #ifdef __arch64__
316 0xc2584003, /* 64-bit --> ldx [%g1 + %g3], %g1 */
317 #else
318 0xc2004003, /* 32-bit --> ld [%g1 + %g3], %g1 */
319 #endif
320 0x81c04000, /* --> jmp %g1 */
321 0x01000000, /* --> nop */
322 #ifdef __arch64__
323 0x9de3bf80, /* 64-bit --> save %sp, -128, %sp */
324 #else
325 0x9de3bfc0, /* 32-bit --> save %sp, -64, %sp */
326 #endif
327 0xa0100003, /* --> mov %g3, %l0 */
328 0x40000000, /* --> call _glapi_get_dispatch */
329 0x01000000, /* --> nop */
330 0x82100008, /* --> mov %o0, %g1 */
331 0x86100010, /* --> mov %l0, %g3 */
332 0x10bffff7, /* --> ba -4*9 */
333 0x81e80000, /* --> restore */
334 #endif
335 };
336 #ifdef GLX_USE_TLS
337 extern unsigned long __glapi_sparc_get_dispatch(void);
338 unsigned int *code = &__glapi_sparc_tls_stub;
339 unsigned long dispatch = __glapi_sparc_get_dispatch();
340 #else
341 unsigned int *code = &__glapi_sparc_pthread_stub;
342 unsigned long dispatch = (unsigned long) &_glapi_Dispatch;
343 unsigned long call_dest = (unsigned long ) &_glapi_get_dispatch;
344 int idx;
345 #endif
346
347 #if defined(GLX_USE_TLS)
348 code[0] = template[0] | (dispatch >> 10);
349 code[1] = template[1];
350 __glapi_sparc_icache_flush(&code[0]);
351 code[2] = template[2] | (dispatch & 0x3ff);
352 code[3] = template[3];
353 __glapi_sparc_icache_flush(&code[2]);
354 code[4] = template[4];
355 code[5] = template[5];
356 __glapi_sparc_icache_flush(&code[4]);
357 code[6] = template[6];
358 __glapi_sparc_icache_flush(&code[6]);
359 #else
360 #if defined(__arch64__)
361 code[0] = template[0] | (dispatch >> (32 + 10));
362 code[1] = template[1] | ((dispatch & 0xffffffff) >> 10);
363 __glapi_sparc_icache_flush(&code[0]);
364 code[2] = template[2] | ((dispatch >> 32) & 0x3ff);
365 code[3] = template[3];
366 __glapi_sparc_icache_flush(&code[2]);
367 code[4] = template[4];
368 code[5] = template[5];
369 __glapi_sparc_icache_flush(&code[4]);
370 code[6] = template[6] | (dispatch & 0x3ff);
371 idx = 7;
372 #else
373 code[0] = template[0] | (dispatch >> 10);
374 code[1] = template[1];
375 __glapi_sparc_icache_flush(&code[0]);
376 code[2] = template[2] | (dispatch & 0x3ff);
377 idx = 3;
378 #endif
379 code[idx + 0] = template[idx + 0];
380 __glapi_sparc_icache_flush(&code[idx - 1]);
381 code[idx + 1] = template[idx + 1];
382 code[idx + 2] = template[idx + 2];
383 __glapi_sparc_icache_flush(&code[idx + 1]);
384 code[idx + 3] = template[idx + 3];
385 code[idx + 4] = template[idx + 4];
386 __glapi_sparc_icache_flush(&code[idx + 3]);
387 code[idx + 5] = template[idx + 5];
388 code[idx + 6] = template[idx + 6];
389 __glapi_sparc_icache_flush(&code[idx + 5]);
390 code[idx + 7] = template[idx + 7];
391 code[idx + 8] = template[idx + 8] |
392 (((call_dest - ((unsigned long) &code[idx + 8]))
393 >> 2) & 0x3fffffff);
394 __glapi_sparc_icache_flush(&code[idx + 7]);
395 code[idx + 9] = template[idx + 9];
396 code[idx + 10] = template[idx + 10];
397 __glapi_sparc_icache_flush(&code[idx + 9]);
398 code[idx + 11] = template[idx + 11];
399 code[idx + 12] = template[idx + 12];
400 __glapi_sparc_icache_flush(&code[idx + 11]);
401 code[idx + 13] = template[idx + 13];
402 __glapi_sparc_icache_flush(&code[idx + 13]);
403 #endif
404 #endif
405 }
406 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */
407
408
409 /**
410 * Set the global or per-thread dispatch table pointer.
411 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
412 * table (__glapi_noop_table).
413 */
414 PUBLIC void
415 _glapi_set_dispatch(struct _glapi_table *dispatch)
416 {
417 #if defined(PTHREADS) || defined(GLX_USE_TLS)
418 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
419 pthread_once( & once_control, init_glapi_relocs );
420 #endif
421
422 if (!dispatch) {
423 /* use the no-op functions */
424 dispatch = (struct _glapi_table *) __glapi_noop_table;
425 }
426 #ifdef DEBUG
427 else {
428 _glapi_check_table(dispatch);
429 }
430 #endif
431
432 #if defined(GLX_USE_TLS)
433 _glapi_tls_Dispatch = dispatch;
434 #elif defined(THREADS)
435 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
436 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
437 #else /*THREADS*/
438 _glapi_Dispatch = dispatch;
439 #endif /*THREADS*/
440 }
441
442
443
444 /**
445 * Return pointer to current dispatch table for calling thread.
446 */
447 PUBLIC struct _glapi_table *
448 _glapi_get_dispatch(void)
449 {
450 struct _glapi_table * api;
451 #if defined(GLX_USE_TLS)
452 api = _glapi_tls_Dispatch;
453 #elif defined(THREADS)
454 api = (ThreadSafe)
455 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
456 : _glapi_Dispatch;
457 #else
458 api = _glapi_Dispatch;
459 #endif
460 return api;
461 }
462
463
464
465
466 /*
467 * The dispatch table size (number of entries) is the size of the
468 * _glapi_table struct plus the number of dynamic entries we can add.
469 * The extra slots can be filled in by DRI drivers that register new extension
470 * functions.
471 */
472 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
473
474
475 /**
476 * Return size of dispatch table struct as number of functions (or
477 * slots).
478 */
479 PUBLIC GLuint
480 _glapi_get_dispatch_table_size(void)
481 {
482 return DISPATCH_TABLE_SIZE;
483 }
484
485
486 /**
487 * Make sure there are no NULL pointers in the given dispatch table.
488 * Intended for debugging purposes.
489 */
490 void
491 _glapi_check_table(const struct _glapi_table *table)
492 {
493 #ifdef EXTRA_DEBUG
494 const GLuint entries = _glapi_get_dispatch_table_size();
495 const void **tab = (const void **) table;
496 GLuint i;
497 for (i = 1; i < entries; i++) {
498 assert(tab[i]);
499 }
500
501 /* Do some spot checks to be sure that the dispatch table
502 * slots are assigned correctly.
503 */
504 {
505 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
506 char *BeginFunc = (char*) &table->Begin;
507 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
508 assert(BeginOffset == _gloffset_Begin);
509 assert(BeginOffset == offset);
510 }
511 {
512 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
513 char *viewportFunc = (char*) &table->Viewport;
514 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
515 assert(viewportOffset == _gloffset_Viewport);
516 assert(viewportOffset == offset);
517 }
518 {
519 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
520 char *VertexPointerFunc = (char*) &table->VertexPointer;
521 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
522 assert(VertexPointerOffset == _gloffset_VertexPointer);
523 assert(VertexPointerOffset == offset);
524 }
525 {
526 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
527 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
528 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
529 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
530 assert(ResetMinMaxOffset == offset);
531 }
532 {
533 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
534 char *blendColorFunc = (char*) &table->BlendColor;
535 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
536 assert(blendColorOffset == _gloffset_BlendColor);
537 assert(blendColorOffset == offset);
538 }
539 {
540 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
541 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
542 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
543 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
544 assert(secondaryColor3fOffset == offset);
545 }
546 {
547 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
548 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
549 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
550 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
551 assert(pointParameterivOffset == offset);
552 }
553 {
554 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
555 char *setFenceFunc = (char*) &table->SetFenceNV;
556 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
557 assert(setFenceOffset == _gloffset_SetFenceNV);
558 assert(setFenceOffset == offset);
559 }
560 #else
561 (void) table;
562 #endif
563 }