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.
26 * \file glapi_getproc.c
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
33 #ifdef HAVE_DIX_CONFIG_H
34 #include <dix-config.h>
35 #include "glapi/mesa.h"
37 #include "main/glheader.h"
38 #include "main/compiler.h"
41 #include "glapi/glapi.h"
42 #include "glapi/glapi_priv.h"
43 #include "glapi/glapitable.h"
44 #include "glapi/glapioffsets.h"
47 /**********************************************************************
48 * Static function management.
52 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server)
53 # define NEED_FUNCTION_POINTER
55 #include "glapi/glprocs.h"
59 * Search the table of static entrypoint functions for the named function
60 * and return the corresponding glprocs_table_t entry.
62 static const glprocs_table_t
*
63 get_static_proc( const char * n
)
66 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
67 const char *testName
= gl_string_table
+ static_functions
[i
].Name_offset
;
69 /* skip the prefix on the name */
70 if (strcmp(testName
, n
+ 1) == 0)
72 if (strcmp(testName
, n
) == 0)
75 return &static_functions
[i
];
83 * Return dispatch table offset of the named static (built-in) function.
84 * Return -1 if function not found.
87 get_static_proc_offset(const char *funcName
)
89 const glprocs_table_t
* const f
= get_static_proc( funcName
);
98 #if !defined(XFree86Server)
101 * Return dispatch function address for the named static (built-in) function.
102 * Return NULL if function not found.
105 get_static_proc_address(const char *funcName
)
107 const glprocs_table_t
* const f
= get_static_proc( funcName
);
112 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
113 return (f
->Address
== NULL
)
114 ? get_entrypoint_address(f
->Offset
)
116 #elif defined(DISPATCH_FUNCTION_SIZE)
117 return get_entrypoint_address(f
->Offset
);
126 get_static_proc_address(const char *funcName
)
132 #endif /* !defined(XFree86Server) */
136 * Return the name of the function at the given offset in the dispatch
137 * table. For debugging only.
140 get_static_proc_name( GLuint offset
)
143 for (i
= 0; static_functions
[i
].Name_offset
>= 0; i
++) {
144 if (static_functions
[i
].Offset
== offset
) {
145 return gl_string_table
+ static_functions
[i
].Name_offset
;
153 /**********************************************************************
154 * Extension function management.
159 * Track information about a function added to the GL API.
161 struct _glapi_function
{
163 * Name of the function.
169 * Text string that describes the types of the parameters passed to the
170 * named function. Parameter types are converted to characters using the
172 * - 'i' for \c GLint, \c GLuint, and \c GLenum
173 * - 'p' for any pointer type
174 * - 'f' for \c GLfloat and \c GLclampf
175 * - 'd' for \c GLdouble and \c GLclampd
177 const char * parameter_signature
;
181 * Offset in the dispatch table where the pointer to the real function is
182 * located. If the driver has not requested that the named function be
183 * added to the dispatch table, this will have the value ~0.
185 unsigned dispatch_offset
;
189 * Pointer to the dispatch stub for the named function.
192 * The semantic of this field should be changed slightly. Currently, it
193 * is always expected to be non-\c NULL. However, it would be better to
194 * only allocate the entry-point stub when the application requests the
195 * function via \c glXGetProcAddress. This would save memory for all the
196 * functions that the driver exports but that the application never wants
199 _glapi_proc dispatch_stub
;
203 static struct _glapi_function ExtEntryTable
[MAX_EXTENSION_FUNCS
];
204 static GLuint NumExtEntryPoints
= 0;
207 static struct _glapi_function
*
208 get_extension_proc(const char *funcName
)
211 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
212 if (strcmp(ExtEntryTable
[i
].name
, funcName
) == 0) {
213 return & ExtEntryTable
[i
];
221 get_extension_proc_offset(const char *funcName
)
223 const struct _glapi_function
* const f
= get_extension_proc( funcName
);
228 return f
->dispatch_offset
;
233 get_extension_proc_address(const char *funcName
)
235 const struct _glapi_function
* const f
= get_extension_proc( funcName
);
240 return f
->dispatch_stub
;
245 get_extension_proc_name(GLuint offset
)
248 for (i
= 0; i
< NumExtEntryPoints
; i
++) {
249 if (ExtEntryTable
[i
].dispatch_offset
== offset
) {
250 return ExtEntryTable
[i
].name
;
258 * strdup() is actually not a standard ANSI C or POSIX routine.
259 * Irix will not define it if ANSI mode is in effect.
262 str_dup(const char *str
)
265 copy
= (char*) malloc(strlen(str
) + 1);
274 * Generate new entrypoint
276 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
277 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
278 * never happens, and the user calls this function, he'll segfault. That's
279 * what you get when you try calling a GL function that doesn't really exist.
281 * \param funcName Name of the function to create an entry-point for.
283 * \sa _glapi_add_entrypoint
286 static struct _glapi_function
*
287 add_function_name( const char * funcName
)
289 struct _glapi_function
* entry
= NULL
;
290 _glapi_proc entrypoint
= NULL
;
291 char * name_dup
= NULL
;
293 if (NumExtEntryPoints
>= MAX_EXTENSION_FUNCS
)
296 if (funcName
== NULL
)
299 name_dup
= str_dup(funcName
);
300 if (name_dup
== NULL
)
303 entrypoint
= generate_entrypoint(~0);
305 if (entrypoint
== NULL
) {
310 entry
= & ExtEntryTable
[NumExtEntryPoints
];
313 entry
->name
= name_dup
;
314 entry
->parameter_signature
= NULL
;
315 entry
->dispatch_offset
= ~0;
316 entry
->dispatch_stub
= entrypoint
;
322 static struct _glapi_function
*
323 set_entry_info( struct _glapi_function
* entry
, const char * signature
, unsigned offset
)
325 char * sig_dup
= NULL
;
327 if (signature
== NULL
)
330 sig_dup
= str_dup(signature
);
334 fill_in_entrypoint_offset(entry
->dispatch_stub
, offset
);
336 entry
->parameter_signature
= sig_dup
;
337 entry
->dispatch_offset
= offset
;
344 * Fill-in the dispatch stub for the named function.
346 * This function is intended to be called by a hardware driver. When called,
347 * a dispatch stub may be created created for the function. A pointer to this
348 * dispatch function will be returned by glXGetProcAddress.
350 * \param function_names Array of pointers to function names that should
351 * share a common dispatch offset.
352 * \param parameter_signature String representing the types of the parameters
353 * passed to the named function. Parameter types
354 * are converted to characters using the following
356 * - 'i' for \c GLint, \c GLuint, and \c GLenum
357 * - 'p' for any pointer type
358 * - 'f' for \c GLfloat and \c GLclampf
359 * - 'd' for \c GLdouble and \c GLclampd
362 * The offset in the dispatch table of the named function. A pointer to the
363 * driver's implementation of the named function should be stored at
364 * \c dispatch_table[\c offset]. Return -1 if error/problem.
366 * \sa glXGetProcAddress
369 * This function can only handle up to 8 names at a time. As far as I know,
370 * the maximum number of names ever associated with an existing GL function is
371 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
372 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
373 * too painful of a limitation.
376 * Determine whether or not \c parameter_signature should be allowed to be
377 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
378 * pass in an empty string.
381 * Determine if code should be added to reject function names that start with
385 * Add code to compare \c parameter_signature with the parameter signature of
386 * a static function. In order to do that, we need to find a way to \b get
387 * the parameter signature of a static function.
391 _glapi_add_dispatch( const char * const * function_names
,
392 const char * parameter_signature
)
394 static int next_dynamic_offset
= _gloffset_FIRST_DYNAMIC
;
395 const char * const real_sig
= (parameter_signature
!= NULL
)
396 ? parameter_signature
: "";
397 struct _glapi_function
* entry
[8];
398 GLboolean is_static
[8];
402 init_glapi_relocs_once();
404 (void) memset( is_static
, 0, sizeof( is_static
) );
405 (void) memset( entry
, 0, sizeof( entry
) );
407 /* Find the _single_ dispatch offset for all function names that already
408 * exist (and have a dispatch offset).
411 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
412 const char * funcName
= function_names
[i
];
414 int extension_offset
;
416 if (funcName
[0] != 'g' || funcName
[1] != 'l')
419 /* search built-in functions */
420 static_offset
= get_static_proc_offset(funcName
);
422 if (static_offset
>= 0) {
424 is_static
[i
] = GL_TRUE
;
426 /* FIXME: Make sure the parameter signatures match! How do we get
427 * FIXME: the parameter signature for static functions?
430 if ( (offset
!= ~0) && (static_offset
!= offset
) ) {
434 offset
= static_offset
;
439 /* search added extension functions */
440 entry
[i
] = get_extension_proc(funcName
);
442 if (entry
[i
] != NULL
) {
443 extension_offset
= entry
[i
]->dispatch_offset
;
445 /* The offset may be ~0 if the function name was added by
446 * glXGetProcAddress but never filled in by the driver.
449 if (extension_offset
== ~0) {
453 if (strcmp(real_sig
, entry
[i
]->parameter_signature
) != 0) {
457 if ( (offset
!= ~0) && (extension_offset
!= offset
) ) {
461 offset
= extension_offset
;
465 /* If all function names are either new (or with no dispatch offset),
466 * allocate a new dispatch offset.
470 offset
= next_dynamic_offset
;
471 next_dynamic_offset
++;
474 /* Fill in the dispatch offset for the new function names (and those with
475 * no dispatch offset).
478 for ( i
= 0 ; function_names
[i
] != NULL
; i
++ ) {
483 /* generate entrypoints for new function names */
484 if (entry
[i
] == NULL
) {
485 entry
[i
] = add_function_name( function_names
[i
] );
486 if (entry
[i
] == NULL
) {
487 /* FIXME: Possible memory leak here. */
492 if (entry
[i
]->dispatch_offset
== ~0) {
493 set_entry_info( entry
[i
], real_sig
, offset
);
502 * Return offset of entrypoint for named function within dispatch table.
505 _glapi_get_proc_offset(const char *funcName
)
509 /* search extension functions first */
510 offset
= get_extension_proc_offset(funcName
);
514 /* search static functions */
515 return get_static_proc_offset(funcName
);
521 * Return pointer to the named function. If the function name isn't found
522 * in the name of static functions, try generating a new API entrypoint on
523 * the fly with assembly language.
526 _glapi_get_proc_address(const char *funcName
)
529 struct _glapi_function
* entry
;
531 init_glapi_relocs_once();
534 /* skip the prefix on the name */
535 if (funcName
[1] != 'g' || funcName
[2] != 'l')
538 if (funcName
[0] != 'g' || funcName
[1] != 'l')
542 /* search extension functions first */
543 func
= get_extension_proc_address(funcName
);
547 /* search static functions */
548 func
= get_static_proc_address(funcName
);
552 /* generate entrypoint, dispatch offset must be filled in by the driver */
553 entry
= add_function_name(funcName
);
557 return entry
->dispatch_stub
;
563 * Return the name of the function at the given dispatch offset.
564 * This is only intended for debugging.
567 _glapi_get_proc_name(GLuint offset
)
571 /* search built-in functions */
572 n
= get_static_proc_name(offset
);
577 /* search added extension functions */
578 return get_extension_proc_name(offset
);
583 /**********************************************************************
584 * GL API table functions.
589 * The dispatch table size (number of entries) is the size of the
590 * _glapi_table struct plus the number of dynamic entries we can add.
591 * The extra slots can be filled in by DRI drivers that register new extension
594 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
598 * Return size of dispatch table struct as number of functions (or
602 _glapi_get_dispatch_table_size(void)
604 return DISPATCH_TABLE_SIZE
;
609 * Make sure there are no NULL pointers in the given dispatch table.
610 * Intended for debugging purposes.
613 _glapi_check_table_not_null(const struct _glapi_table
*table
)
615 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
616 const GLuint entries
= _glapi_get_dispatch_table_size();
617 const void **tab
= (const void **) table
;
619 for (i
= 1; i
< entries
; i
++) {
629 * Do some spot checks to be sure that the dispatch table
630 * slots are assigned correctly. For debugging only.
633 _glapi_check_table(const struct _glapi_table
*table
)
635 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
637 GLuint BeginOffset
= _glapi_get_proc_offset("glBegin");
638 char *BeginFunc
= (char*) &table
->Begin
;
639 GLuint offset
= (BeginFunc
- (char *) table
) / sizeof(void *);
640 assert(BeginOffset
== _gloffset_Begin
);
641 assert(BeginOffset
== offset
);
644 GLuint viewportOffset
= _glapi_get_proc_offset("glViewport");
645 char *viewportFunc
= (char*) &table
->Viewport
;
646 GLuint offset
= (viewportFunc
- (char *) table
) / sizeof(void *);
647 assert(viewportOffset
== _gloffset_Viewport
);
648 assert(viewportOffset
== offset
);
651 GLuint VertexPointerOffset
= _glapi_get_proc_offset("glVertexPointer");
652 char *VertexPointerFunc
= (char*) &table
->VertexPointer
;
653 GLuint offset
= (VertexPointerFunc
- (char *) table
) / sizeof(void *);
654 assert(VertexPointerOffset
== _gloffset_VertexPointer
);
655 assert(VertexPointerOffset
== offset
);
658 GLuint ResetMinMaxOffset
= _glapi_get_proc_offset("glResetMinmax");
659 char *ResetMinMaxFunc
= (char*) &table
->ResetMinmax
;
660 GLuint offset
= (ResetMinMaxFunc
- (char *) table
) / sizeof(void *);
661 assert(ResetMinMaxOffset
== _gloffset_ResetMinmax
);
662 assert(ResetMinMaxOffset
== offset
);
665 GLuint blendColorOffset
= _glapi_get_proc_offset("glBlendColor");
666 char *blendColorFunc
= (char*) &table
->BlendColor
;
667 GLuint offset
= (blendColorFunc
- (char *) table
) / sizeof(void *);
668 assert(blendColorOffset
== _gloffset_BlendColor
);
669 assert(blendColorOffset
== offset
);
672 GLuint secondaryColor3fOffset
= _glapi_get_proc_offset("glSecondaryColor3fEXT");
673 char *secondaryColor3fFunc
= (char*) &table
->SecondaryColor3fEXT
;
674 GLuint offset
= (secondaryColor3fFunc
- (char *) table
) / sizeof(void *);
675 assert(secondaryColor3fOffset
== _gloffset_SecondaryColor3fEXT
);
676 assert(secondaryColor3fOffset
== offset
);
679 GLuint pointParameterivOffset
= _glapi_get_proc_offset("glPointParameterivNV");
680 char *pointParameterivFunc
= (char*) &table
->PointParameterivNV
;
681 GLuint offset
= (pointParameterivFunc
- (char *) table
) / sizeof(void *);
682 assert(pointParameterivOffset
== _gloffset_PointParameterivNV
);
683 assert(pointParameterivOffset
== offset
);
686 GLuint setFenceOffset
= _glapi_get_proc_offset("glSetFenceNV");
687 char *setFenceFunc
= (char*) &table
->SetFenceNV
;
688 GLuint offset
= (setFenceFunc
- (char *) table
) / sizeof(void *);
689 assert(setFenceOffset
== _gloffset_SetFenceNV
);
690 assert(setFenceOffset
== offset
);