mesa: return -1, not GL_FALSE if _glapi_add_dispatch() fails name sanity check
[mesa.git] / src / mesa / glapi / glapi.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 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 "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 /***** BEGIN NO-OP DISPATCH *****/
75
76 static GLboolean WarnFlag = GL_FALSE;
77 static _glapi_warning_func warning_func;
78
79 #if defined(PTHREADS) || defined(GLX_USE_TLS)
80 static void init_glapi_relocs(void);
81 #endif
82
83 static _glapi_proc generate_entrypoint(GLuint functionOffset);
84 static void fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset);
85
86 /*
87 * Enable/disable printing of warning messages.
88 */
89 PUBLIC void
90 _glapi_noop_enable_warnings(GLboolean enable)
91 {
92 WarnFlag = enable;
93 }
94
95 /*
96 * Register a callback function for reporting errors.
97 */
98 PUBLIC void
99 _glapi_set_warning_func( _glapi_warning_func func )
100 {
101 warning_func = func;
102 }
103
104 static GLboolean
105 warn(void)
106 {
107 if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
108 && warning_func) {
109 return GL_TRUE;
110 }
111 else {
112 return GL_FALSE;
113 }
114 }
115
116
117 #define KEYWORD1 static
118 #define KEYWORD1_ALT static
119 #define KEYWORD2 GLAPIENTRY
120 #define NAME(func) NoOp##func
121
122 #define F NULL
123
124 #define DISPATCH(func, args, msg) \
125 if (warn()) { \
126 warning_func(NULL, "GL User Error: called without context: %s", #func); \
127 }
128
129 #define RETURN_DISPATCH(func, args, msg) \
130 if (warn()) { \
131 warning_func(NULL, "GL User Error: called without context: %s", #func); \
132 } \
133 return 0
134
135 #define DISPATCH_TABLE_NAME __glapi_noop_table
136 #define UNUSED_TABLE_NAME __unused_noop_functions
137
138 #define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
139
140 static GLint NoOpUnused(void)
141 {
142 if (warn()) {
143 warning_func(NULL, "GL User Error: calling extension function without a current context\n");
144 }
145 return 0;
146 }
147
148 #include "glapitemp.h"
149
150 /***** END NO-OP DISPATCH *****/
151
152
153
154 /**
155 * \name Current dispatch and current context control variables
156 *
157 * Depending on whether or not multithreading is support, and the type of
158 * support available, several variables are used to store the current context
159 * pointer and the current dispatch table pointer. In the non-threaded case,
160 * the variables \c _glapi_Dispatch and \c _glapi_Context are used for this
161 * purpose.
162 *
163 * In the "normal" threaded case, the variables \c _glapi_Dispatch and
164 * \c _glapi_Context will be \c NULL if an application is detected as being
165 * multithreaded. Single-threaded applications will use \c _glapi_Dispatch
166 * and \c _glapi_Context just like the case without any threading support.
167 * When \c _glapi_Dispatch and \c _glapi_Context are \c NULL, the thread state
168 * data \c _gl_DispatchTSD and \c ContextTSD are used. Drivers and the
169 * static dispatch functions access these variables via \c _glapi_get_dispatch
170 * and \c _glapi_get_context.
171 *
172 * There is a race condition in setting \c _glapi_Dispatch to \c NULL. It is
173 * possible for the original thread to be setting it at the same instant a new
174 * thread, perhaps running on a different processor, is clearing it. Because
175 * of that, \c ThreadSafe, which can only ever be changed to \c GL_TRUE, is
176 * used to determine whether or not the application is multithreaded.
177 *
178 * In the TLS case, the variables \c _glapi_Dispatch and \c _glapi_Context are
179 * hardcoded to \c NULL. Instead the TLS variables \c _glapi_tls_Dispatch and
180 * \c _glapi_tls_Context are used. Having \c _glapi_Dispatch and
181 * \c _glapi_Context be hardcoded to \c NULL maintains binary compatability
182 * between TLS enabled loaders and non-TLS DRI drivers.
183 */
184 /*@{*/
185 #if defined(GLX_USE_TLS)
186
187 PUBLIC __thread struct _glapi_table * _glapi_tls_Dispatch
188 __attribute__((tls_model("initial-exec")))
189 = (struct _glapi_table *) __glapi_noop_table;
190
191 PUBLIC __thread void * _glapi_tls_Context
192 __attribute__((tls_model("initial-exec")));
193
194 PUBLIC const struct _glapi_table *_glapi_Dispatch = NULL;
195 PUBLIC const void *_glapi_Context = NULL;
196
197 #else
198
199 #if defined(THREADS)
200
201 static GLboolean ThreadSafe = GL_FALSE; /**< In thread-safe mode? */
202 _glthread_TSD _gl_DispatchTSD; /**< Per-thread dispatch pointer */
203 static _glthread_TSD ContextTSD; /**< Per-thread context pointer */
204
205 #if defined(WIN32_THREADS)
206 void FreeTSD(_glthread_TSD *p);
207 void FreeAllTSD(void)
208 {
209 FreeTSD(&_gl_DispatchTSD);
210 FreeTSD(&ContextTSD);
211 }
212 #endif /* defined(WIN32_THREADS) */
213
214 #endif /* defined(THREADS) */
215
216 PUBLIC struct _glapi_table *_glapi_Dispatch =
217 (struct _glapi_table *) __glapi_noop_table;
218 PUBLIC void *_glapi_Context = NULL;
219
220 #endif /* defined(GLX_USE_TLS) */
221 /*@}*/
222
223
224 /**
225 * strdup() is actually not a standard ANSI C or POSIX routine.
226 * Irix will not define it if ANSI mode is in effect.
227 */
228 static char *
229 str_dup(const char *str)
230 {
231 char *copy;
232 copy = (char*) malloc(strlen(str) + 1);
233 if (!copy)
234 return NULL;
235 strcpy(copy, str);
236 return copy;
237 }
238
239
240
241 /**
242 * We should call this periodically from a function such as glXMakeCurrent
243 * in order to test if multiple threads are being used.
244 */
245 void
246 _glapi_check_multithread(void)
247 {
248 #if defined(THREADS) && !defined(GLX_USE_TLS)
249 if (!ThreadSafe) {
250 static unsigned long knownID;
251 static GLboolean firstCall = GL_TRUE;
252 if (firstCall) {
253 knownID = _glthread_GetID();
254 firstCall = GL_FALSE;
255 }
256 else if (knownID != _glthread_GetID()) {
257 ThreadSafe = GL_TRUE;
258 _glapi_set_dispatch(NULL);
259 _glapi_set_context(NULL);
260 }
261 }
262 else if (!_glapi_get_dispatch()) {
263 /* make sure that this thread's dispatch pointer isn't null */
264 _glapi_set_dispatch(NULL);
265 }
266 #endif
267 }
268
269
270
271 /**
272 * Set the current context pointer for this thread.
273 * The context pointer is an opaque type which should be cast to
274 * void from the real context pointer type.
275 */
276 PUBLIC void
277 _glapi_set_context(void *context)
278 {
279 (void) __unused_noop_functions; /* silence a warning */
280 #if defined(GLX_USE_TLS)
281 _glapi_tls_Context = context;
282 #elif defined(THREADS)
283 _glthread_SetTSD(&ContextTSD, context);
284 _glapi_Context = (ThreadSafe) ? NULL : context;
285 #else
286 _glapi_Context = context;
287 #endif
288 }
289
290
291
292 /**
293 * Get the current context pointer for this thread.
294 * The context pointer is an opaque type which should be cast from
295 * void to the real context pointer type.
296 */
297 PUBLIC void *
298 _glapi_get_context(void)
299 {
300 #if defined(GLX_USE_TLS)
301 return _glapi_tls_Context;
302 #elif defined(THREADS)
303 if (ThreadSafe) {
304 return _glthread_GetTSD(&ContextTSD);
305 }
306 else {
307 return _glapi_Context;
308 }
309 #else
310 return _glapi_Context;
311 #endif
312 }
313
314
315
316 /**
317 * Set the global or per-thread dispatch table pointer.
318 * If the dispatch parameter is NULL we'll plug in the no-op dispatch
319 * table (__glapi_noop_table).
320 */
321 PUBLIC void
322 _glapi_set_dispatch(struct _glapi_table *dispatch)
323 {
324 #if defined(PTHREADS) || defined(GLX_USE_TLS)
325 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
326 pthread_once( & once_control, init_glapi_relocs );
327 #endif
328
329 if (!dispatch) {
330 /* use the no-op functions */
331 dispatch = (struct _glapi_table *) __glapi_noop_table;
332 }
333 #ifdef DEBUG
334 else {
335 _glapi_check_table(dispatch);
336 }
337 #endif
338
339 #if defined(GLX_USE_TLS)
340 _glapi_tls_Dispatch = dispatch;
341 #elif defined(THREADS)
342 _glthread_SetTSD(&_gl_DispatchTSD, (void *) dispatch);
343 _glapi_Dispatch = (ThreadSafe) ? NULL : dispatch;
344 #else /*THREADS*/
345 _glapi_Dispatch = dispatch;
346 #endif /*THREADS*/
347 }
348
349
350
351 /**
352 * Return pointer to current dispatch table for calling thread.
353 */
354 PUBLIC struct _glapi_table *
355 _glapi_get_dispatch(void)
356 {
357 struct _glapi_table * api;
358 #if defined(GLX_USE_TLS)
359 api = _glapi_tls_Dispatch;
360 #elif defined(THREADS)
361 api = (ThreadSafe)
362 ? (struct _glapi_table *) _glthread_GetTSD(&_gl_DispatchTSD)
363 : _glapi_Dispatch;
364 #else
365 api = _glapi_Dispatch;
366 #endif
367 return api;
368 }
369
370
371
372 /***
373 *** The rest of this file is pretty much concerned with GetProcAddress
374 *** functionality.
375 ***/
376
377 #if defined(USE_X64_64_ASM) && defined(GLX_USE_TLS)
378 # define DISPATCH_FUNCTION_SIZE 16
379 #elif defined(USE_X86_ASM)
380 # if defined(THREADS) && !defined(GLX_USE_TLS)
381 # define DISPATCH_FUNCTION_SIZE 32
382 # else
383 # define DISPATCH_FUNCTION_SIZE 16
384 # endif
385 #endif
386
387 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server) && !defined(XGLServer)
388 # define NEED_FUNCTION_POINTER
389 #endif
390
391 /* The code in this file is auto-generated with Python */
392 #include "glprocs.h"
393
394
395 /**
396 * Search the table of static entrypoint functions for the named function
397 * and return the corresponding glprocs_table_t entry.
398 */
399 static const glprocs_table_t *
400 find_entry( const char * n )
401 {
402 GLuint i;
403 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
404 const char *testName = gl_string_table + static_functions[i].Name_offset;
405 if (strcmp(testName, n) == 0) {
406 return &static_functions[i];
407 }
408 }
409 return NULL;
410 }
411
412
413 /**
414 * Return dispatch table offset of the named static (built-in) function.
415 * Return -1 if function not found.
416 */
417 static GLint
418 get_static_proc_offset(const char *funcName)
419 {
420 const glprocs_table_t * const f = find_entry( funcName );
421 if (f) {
422 return f->Offset;
423 }
424 return -1;
425 }
426
427
428 #if !defined(XFree86Server) && !defined(XGLServer)
429 #ifdef USE_X86_ASM
430
431 #if defined( GLX_USE_TLS )
432 extern GLubyte gl_dispatch_functions_start[];
433 extern GLubyte gl_dispatch_functions_end[];
434 #else
435 extern const GLubyte gl_dispatch_functions_start[];
436 #endif
437
438 #endif /* USE_X86_ASM */
439
440
441 /**
442 * Return dispatch function address for the named static (built-in) function.
443 * Return NULL if function not found.
444 */
445 static _glapi_proc
446 get_static_proc_address(const char *funcName)
447 {
448 const glprocs_table_t * const f = find_entry( funcName );
449 if (f) {
450 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
451 return (f->Address == NULL)
452 ? (_glapi_proc) (gl_dispatch_functions_start
453 + (DISPATCH_FUNCTION_SIZE * f->Offset))
454 : f->Address;
455 #elif defined(DISPATCH_FUNCTION_SIZE)
456 return (_glapi_proc) (gl_dispatch_functions_start
457 + (DISPATCH_FUNCTION_SIZE * f->Offset));
458 #else
459 return f->Address;
460 #endif
461 }
462 else {
463 return NULL;
464 }
465 }
466
467 #endif /* !defined(XFree86Server) && !defined(XGLServer) */
468
469
470
471 /**
472 * Return the name of the function at the given offset in the dispatch
473 * table. For debugging only.
474 */
475 static const char *
476 get_static_proc_name( GLuint offset )
477 {
478 GLuint i;
479 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
480 if (static_functions[i].Offset == offset) {
481 return gl_string_table + static_functions[i].Name_offset;
482 }
483 }
484 return NULL;
485 }
486
487
488
489 /**********************************************************************
490 * Extension function management.
491 */
492
493 /*
494 * Number of extension functions which we can dynamically add at runtime.
495 */
496 #define MAX_EXTENSION_FUNCS 300
497
498
499 /*
500 * The dispatch table size (number of entries) is the size of the
501 * _glapi_table struct plus the number of dynamic entries we can add.
502 * The extra slots can be filled in by DRI drivers that register new extension
503 * functions.
504 */
505 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
506
507
508 /**
509 * Track information about a function added to the GL API.
510 */
511 struct _glapi_function {
512 /**
513 * Name of the function.
514 */
515 const char * name;
516
517
518 /**
519 * Text string that describes the types of the parameters passed to the
520 * named function. Parameter types are converted to characters using the
521 * following rules:
522 * - 'i' for \c GLint, \c GLuint, and \c GLenum
523 * - 'p' for any pointer type
524 * - 'f' for \c GLfloat and \c GLclampf
525 * - 'd' for \c GLdouble and \c GLclampd
526 */
527 const char * parameter_signature;
528
529
530 /**
531 * Offset in the dispatch table where the pointer to the real function is
532 * located. If the driver has not requested that the named function be
533 * added to the dispatch table, this will have the value ~0.
534 */
535 unsigned dispatch_offset;
536
537
538 /**
539 * Pointer to the dispatch stub for the named function.
540 *
541 * \todo
542 * The semantic of this field should be changed slightly. Currently, it
543 * is always expected to be non-\c NULL. However, it would be better to
544 * only allocate the entry-point stub when the application requests the
545 * function via \c glXGetProcAddress. This would save memory for all the
546 * functions that the driver exports but that the application never wants
547 * to call.
548 */
549 _glapi_proc dispatch_stub;
550 };
551
552
553 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
554 static GLuint NumExtEntryPoints = 0;
555
556 #ifdef USE_SPARC_ASM
557 extern void __glapi_sparc_icache_flush(unsigned int *);
558 #endif
559
560 /**
561 * Generate a dispatch function (entrypoint) which jumps through
562 * the given slot number (offset) in the current dispatch table.
563 * We need assembly language in order to accomplish this.
564 */
565 static _glapi_proc
566 generate_entrypoint(GLuint functionOffset)
567 {
568 #if defined(USE_X86_ASM)
569 /* 32 is chosen as something of a magic offset. For x86, the dispatch
570 * at offset 32 is the first one where the offset in the
571 * "jmp OFFSET*4(%eax)" can't be encoded in a single byte.
572 */
573 const GLubyte * const template_func = gl_dispatch_functions_start
574 + (DISPATCH_FUNCTION_SIZE * 32);
575 GLubyte * const code = (GLubyte *) malloc(DISPATCH_FUNCTION_SIZE);
576
577
578 if ( code != NULL ) {
579 (void) memcpy(code, template_func, DISPATCH_FUNCTION_SIZE);
580 fill_in_entrypoint_offset( (_glapi_proc) code, functionOffset );
581 }
582
583 return (_glapi_proc) code;
584 #elif defined(USE_SPARC_ASM)
585
586 #ifdef __arch64__
587 static const unsigned int insn_template[] = {
588 0x05000000, /* sethi %uhi(_glapi_Dispatch), %g2 */
589 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
590 0x8410a000, /* or %g2, %ulo(_glapi_Dispatch), %g2 */
591 0x82106000, /* or %g1, %lo(_glapi_Dispatch), %g1 */
592 0x8528b020, /* sllx %g2, 32, %g2 */
593 0xc2584002, /* ldx [%g1 + %g2], %g1 */
594 0x05000000, /* sethi %hi(8 * glapioffset), %g2 */
595 0x8410a000, /* or %g2, %lo(8 * glapioffset), %g2 */
596 0xc6584002, /* ldx [%g1 + %g2], %g3 */
597 0x81c0c000, /* jmpl %g3, %g0 */
598 0x01000000 /* nop */
599 };
600 #else
601 static const unsigned int insn_template[] = {
602 0x03000000, /* sethi %hi(_glapi_Dispatch), %g1 */
603 0xc2006000, /* ld [%g1 + %lo(_glapi_Dispatch)], %g1 */
604 0xc6006000, /* ld [%g1 + %lo(4*glapioffset)], %g3 */
605 0x81c0c000, /* jmpl %g3, %g0 */
606 0x01000000 /* nop */
607 };
608 #endif /* __arch64__ */
609 unsigned int *code = (unsigned int *) malloc(sizeof(insn_template));
610 unsigned long glapi_addr = (unsigned long) &_glapi_Dispatch;
611 if (code) {
612 memcpy(code, insn_template, sizeof(insn_template));
613
614 #ifdef __arch64__
615 code[0] |= (glapi_addr >> (32 + 10));
616 code[1] |= ((glapi_addr & 0xffffffff) >> 10);
617 __glapi_sparc_icache_flush(&code[0]);
618 code[2] |= ((glapi_addr >> 32) & ((1 << 10) - 1));
619 code[3] |= (glapi_addr & ((1 << 10) - 1));
620 __glapi_sparc_icache_flush(&code[2]);
621 code[6] |= ((functionOffset * 8) >> 10);
622 code[7] |= ((functionOffset * 8) & ((1 << 10) - 1));
623 __glapi_sparc_icache_flush(&code[6]);
624 #else
625 code[0] |= (glapi_addr >> 10);
626 code[1] |= (glapi_addr & ((1 << 10) - 1));
627 __glapi_sparc_icache_flush(&code[0]);
628 code[2] |= (functionOffset * 4);
629 __glapi_sparc_icache_flush(&code[2]);
630 #endif /* __arch64__ */
631 }
632 return (_glapi_proc) code;
633 #else
634 (void) functionOffset;
635 return NULL;
636 #endif /* USE_*_ASM */
637 }
638
639
640 /**
641 * This function inserts a new dispatch offset into the assembly language
642 * stub that was generated with the preceeding function.
643 */
644 static void
645 fill_in_entrypoint_offset(_glapi_proc entrypoint, GLuint offset)
646 {
647 #if defined(USE_X86_ASM)
648 GLubyte * const code = (GLubyte *) entrypoint;
649
650 #if DISPATCH_FUNCTION_SIZE == 32
651 *((unsigned int *)(code + 11)) = 4 * offset;
652 *((unsigned int *)(code + 22)) = 4 * offset;
653 #elif DISPATCH_FUNCTION_SIZE == 16 && defined( GLX_USE_TLS )
654 *((unsigned int *)(code + 8)) = 4 * offset;
655 #elif DISPATCH_FUNCTION_SIZE == 16
656 *((unsigned int *)(code + 7)) = 4 * offset;
657 #else
658 # error Invalid DISPATCH_FUNCTION_SIZE!
659 #endif
660
661 #elif defined(USE_SPARC_ASM)
662
663 /* XXX this hasn't been tested! */
664 unsigned int *code = (unsigned int *) entrypoint;
665 #ifdef __arch64__
666 code[6] = 0x05000000; /* sethi %hi(8 * glapioffset), %g2 */
667 code[7] = 0x8410a000; /* or %g2, %lo(8 * glapioffset), %g2 */
668 code[6] |= ((offset * 8) >> 10);
669 code[7] |= ((offset * 8) & ((1 << 10) - 1));
670 __glapi_sparc_icache_flush(&code[6]);
671 #else /* __arch64__ */
672 code[2] = 0xc6006000; /* ld [%g1 + %lo(4*glapioffset)], %g3 */
673 code[2] |= (offset * 4);
674 __glapi_sparc_icache_flush(&code[2]);
675 #endif /* __arch64__ */
676
677 #else
678
679 /* an unimplemented architecture */
680 (void) entrypoint;
681 (void) offset;
682
683 #endif /* USE_*_ASM */
684 }
685
686
687 /**
688 * Generate new entrypoint
689 *
690 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
691 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
692 * never happens, and the user calls this function, he'll segfault. That's
693 * what you get when you try calling a GL function that doesn't really exist.
694 *
695 * \param funcName Name of the function to create an entry-point for.
696 *
697 * \sa _glapi_add_entrypoint
698 */
699
700 static struct _glapi_function *
701 add_function_name( const char * funcName )
702 {
703 struct _glapi_function * entry = NULL;
704
705 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
706 _glapi_proc entrypoint = generate_entrypoint(~0);
707 if (entrypoint != NULL) {
708 entry = & ExtEntryTable[NumExtEntryPoints];
709
710 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
711 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
712 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
713 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
714 NumExtEntryPoints++;
715 }
716 }
717
718 return entry;
719 }
720
721
722 /**
723 * Fill-in the dispatch stub for the named function.
724 *
725 * This function is intended to be called by a hardware driver. When called,
726 * a dispatch stub may be created created for the function. A pointer to this
727 * dispatch function will be returned by glXGetProcAddress.
728 *
729 * \param function_names Array of pointers to function names that should
730 * share a common dispatch offset.
731 * \param parameter_signature String representing the types of the parameters
732 * passed to the named function. Parameter types
733 * are converted to characters using the following
734 * rules:
735 * - 'i' for \c GLint, \c GLuint, and \c GLenum
736 * - 'p' for any pointer type
737 * - 'f' for \c GLfloat and \c GLclampf
738 * - 'd' for \c GLdouble and \c GLclampd
739 *
740 * \returns
741 * The offset in the dispatch table of the named function. A pointer to the
742 * driver's implementation of the named function should be stored at
743 * \c dispatch_table[\c offset]. Return -1 if error/problem.
744 *
745 * \sa glXGetProcAddress
746 *
747 * \warning
748 * This function can only handle up to 8 names at a time. As far as I know,
749 * the maximum number of names ever associated with an existing GL function is
750 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
751 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
752 * too painful of a limitation.
753 *
754 * \todo
755 * Determine whether or not \c parameter_signature should be allowed to be
756 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
757 * pass in an empty string.
758 *
759 * \todo
760 * Determine if code should be added to reject function names that start with
761 * 'glX'.
762 *
763 * \bug
764 * Add code to compare \c parameter_signature with the parameter signature of
765 * a static function. In order to do that, we need to find a way to \b get
766 * the parameter signature of a static function.
767 */
768
769 PUBLIC int
770 _glapi_add_dispatch( const char * const * function_names,
771 const char * parameter_signature )
772 {
773 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
774 const char * const real_sig = (parameter_signature != NULL)
775 ? parameter_signature : "";
776 struct _glapi_function * entry[8];
777 GLboolean is_static[8];
778 unsigned i;
779 unsigned j;
780 int offset = ~0;
781 int new_offset;
782
783
784 (void) memset( is_static, 0, sizeof( is_static ) );
785 (void) memset( entry, 0, sizeof( entry ) );
786
787 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
788 /* Do some trivial validation on the name of the function.
789 */
790
791 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
792 return -1;
793
794 /* Determine if the named function already exists. If the function does
795 * exist, it must have the same parameter signature as the function
796 * being added.
797 */
798
799 new_offset = get_static_proc_offset(function_names[i]);
800 if (new_offset >= 0) {
801 /* FIXME: Make sure the parameter signatures match! How do we get
802 * FIXME: the parameter signature for static functions?
803 */
804
805 if ( (offset != ~0) && (new_offset != offset) ) {
806 return -1;
807 }
808
809 is_static[i] = GL_TRUE;
810 offset = new_offset;
811 }
812
813
814 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
815 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
816 /* The offset may be ~0 if the function name was added by
817 * glXGetProcAddress but never filled in by the driver.
818 */
819
820 if (ExtEntryTable[j].dispatch_offset != ~0) {
821 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
822 != 0) {
823 return -1;
824 }
825
826 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
827 return -1;
828 }
829
830 offset = ExtEntryTable[j].dispatch_offset;
831 }
832
833 entry[i] = & ExtEntryTable[j];
834 break;
835 }
836 }
837 }
838
839 if (offset == ~0) {
840 offset = next_dynamic_offset;
841 next_dynamic_offset++;
842 }
843
844 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
845 if (! is_static[i] ) {
846 if (entry[i] == NULL) {
847 entry[i] = add_function_name( function_names[i] );
848 if (entry[i] == NULL) {
849 /* FIXME: Possible memory leak here.
850 */
851 return -1;
852 }
853 }
854
855 entry[i]->parameter_signature = str_dup(real_sig);
856 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
857 entry[i]->dispatch_offset = offset;
858 }
859 }
860
861 return offset;
862 }
863
864
865 /**
866 * Return offset of entrypoint for named function within dispatch table.
867 */
868 PUBLIC GLint
869 _glapi_get_proc_offset(const char *funcName)
870 {
871 /* search extension functions first */
872 GLuint i;
873 for (i = 0; i < NumExtEntryPoints; i++) {
874 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
875 return ExtEntryTable[i].dispatch_offset;
876 }
877 }
878 /* search static functions */
879 return get_static_proc_offset(funcName);
880 }
881
882
883
884 /**
885 * Return pointer to the named function. If the function name isn't found
886 * in the name of static functions, try generating a new API entrypoint on
887 * the fly with assembly language.
888 */
889 _glapi_proc
890 _glapi_get_proc_address(const char *funcName)
891 {
892 struct _glapi_function * entry;
893 GLuint i;
894
895 #ifdef MANGLE
896 if (funcName[0] != 'm' || funcName[1] != 'g' || funcName[2] != 'l')
897 return NULL;
898 #else
899 if (funcName[0] != 'g' || funcName[1] != 'l')
900 return NULL;
901 #endif
902
903 /* search extension functions first */
904 for (i = 0; i < NumExtEntryPoints; i++) {
905 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
906 return ExtEntryTable[i].dispatch_stub;
907 }
908 }
909
910 #if !defined( XFree86Server ) && !defined( XGLServer )
911 /* search static functions */
912 {
913 const _glapi_proc func = get_static_proc_address(funcName);
914 if (func)
915 return func;
916 }
917 #endif /* !defined( XFree86Server ) */
918
919 entry = add_function_name(funcName);
920 return (entry == NULL) ? NULL : entry->dispatch_stub;
921 }
922
923
924
925 /**
926 * Return the name of the function at the given dispatch offset.
927 * This is only intended for debugging.
928 */
929 const char *
930 _glapi_get_proc_name(GLuint offset)
931 {
932 GLuint i;
933 const char * n;
934
935 /* search built-in functions */
936 n = get_static_proc_name(offset);
937 if ( n != NULL ) {
938 return n;
939 }
940
941 /* search added extension functions */
942 for (i = 0; i < NumExtEntryPoints; i++) {
943 if (ExtEntryTable[i].dispatch_offset == offset) {
944 return ExtEntryTable[i].name;
945 }
946 }
947 return NULL;
948 }
949
950
951
952 /**
953 * Return size of dispatch table struct as number of functions (or
954 * slots).
955 */
956 PUBLIC GLuint
957 _glapi_get_dispatch_table_size(void)
958 {
959 return DISPATCH_TABLE_SIZE;
960 }
961
962
963
964 /**
965 * Make sure there are no NULL pointers in the given dispatch table.
966 * Intended for debugging purposes.
967 */
968 void
969 _glapi_check_table(const struct _glapi_table *table)
970 {
971 #ifdef DEBUG
972 const GLuint entries = _glapi_get_dispatch_table_size();
973 const void **tab = (const void **) table;
974 GLuint i;
975 for (i = 1; i < entries; i++) {
976 assert(tab[i]);
977 }
978
979 /* Do some spot checks to be sure that the dispatch table
980 * slots are assigned correctly.
981 */
982 {
983 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
984 char *BeginFunc = (char*) &table->Begin;
985 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
986 assert(BeginOffset == _gloffset_Begin);
987 assert(BeginOffset == offset);
988 }
989 {
990 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
991 char *viewportFunc = (char*) &table->Viewport;
992 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
993 assert(viewportOffset == _gloffset_Viewport);
994 assert(viewportOffset == offset);
995 }
996 {
997 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
998 char *VertexPointerFunc = (char*) &table->VertexPointer;
999 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
1000 assert(VertexPointerOffset == _gloffset_VertexPointer);
1001 assert(VertexPointerOffset == offset);
1002 }
1003 {
1004 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
1005 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
1006 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
1007 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
1008 assert(ResetMinMaxOffset == offset);
1009 }
1010 {
1011 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
1012 char *blendColorFunc = (char*) &table->BlendColor;
1013 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
1014 assert(blendColorOffset == _gloffset_BlendColor);
1015 assert(blendColorOffset == offset);
1016 }
1017 {
1018 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
1019 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
1020 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
1021 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
1022 assert(secondaryColor3fOffset == offset);
1023 }
1024 {
1025 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
1026 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
1027 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
1028 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
1029 assert(pointParameterivOffset == offset);
1030 }
1031 {
1032 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
1033 char *setFenceFunc = (char*) &table->SetFenceNV;
1034 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
1035 assert(setFenceOffset == _gloffset_SetFenceNV);
1036 assert(setFenceOffset == offset);
1037 }
1038 #else
1039 (void) table;
1040 #endif
1041 }
1042
1043
1044 #if defined(PTHREADS) || defined(GLX_USE_TLS)
1045 /**
1046 * Perform platform-specific GL API entry-point fixups.
1047 */
1048 static void
1049 init_glapi_relocs( void )
1050 {
1051 #if defined(USE_X86_ASM) && defined(GLX_USE_TLS) && !defined(GLX_X86_READONLY_TEXT)
1052 extern unsigned long _x86_get_dispatch(void);
1053 char run_time_patch[] = {
1054 0x65, 0xa1, 0, 0, 0, 0 /* movl %gs:0,%eax */
1055 };
1056 GLuint *offset = (GLuint *) &run_time_patch[2]; /* 32-bits for x86/32 */
1057 const GLubyte * const get_disp = (const GLubyte *) run_time_patch;
1058 GLubyte * curr_func = (GLubyte *) gl_dispatch_functions_start;
1059
1060 *offset = _x86_get_dispatch();
1061 while ( curr_func != (GLubyte *) gl_dispatch_functions_end ) {
1062 (void) memcpy( curr_func, get_disp, sizeof(run_time_patch));
1063 curr_func += DISPATCH_FUNCTION_SIZE;
1064 }
1065 #endif
1066 }
1067 #endif /* defined(PTHREADS) || defined(GLX_USE_TLS) */