Merge commit 'origin/master' into gallium-sw-api-2
[mesa.git] / src / mesa / glapi / glapi_getproc.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 * \file glapi_getproc.c
27 *
28 * Code for implementing glXGetProcAddress(), etc.
29 * This was originally in glapi.c but refactored out.
30 */
31
32
33 #ifdef HAVE_DIX_CONFIG_H
34 #include <dix-config.h>
35 #include "glapi/mesa.h"
36 #else
37 #include "main/glheader.h"
38 #include "main/compiler.h"
39 #endif
40
41 #include "glapi/glapi.h"
42 #include "glapi/glapi_priv.h"
43 #include "glapi/glapitable.h"
44 #include "glapi/glapioffsets.h"
45
46
47 #if !defined(DISPATCH_FUNCTION_SIZE) && !defined(XFree86Server)
48 # define NEED_FUNCTION_POINTER
49 #endif
50
51 /* The code in this file is auto-generated with Python */
52 #include "glapi/glprocs.h"
53
54
55 /**
56 * Search the table of static entrypoint functions for the named function
57 * and return the corresponding glprocs_table_t entry.
58 */
59 static const glprocs_table_t *
60 find_entry( const char * n )
61 {
62 GLuint i;
63 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
64 const char *testName = gl_string_table + static_functions[i].Name_offset;
65 #ifdef MANGLE
66 /* skip the prefix on the name */
67 if (strcmp(testName, n + 1) == 0)
68 #else
69 if (strcmp(testName, n) == 0)
70 #endif
71 {
72 return &static_functions[i];
73 }
74 }
75 return NULL;
76 }
77
78
79 /**
80 * Return dispatch table offset of the named static (built-in) function.
81 * Return -1 if function not found.
82 */
83 static GLint
84 get_static_proc_offset(const char *funcName)
85 {
86 const glprocs_table_t * const f = find_entry( funcName );
87 if (f) {
88 return f->Offset;
89 }
90 return -1;
91 }
92
93
94 #if !defined(XFree86Server)
95
96 /**
97 * Return dispatch function address for the named static (built-in) function.
98 * Return NULL if function not found.
99 */
100 static _glapi_proc
101 get_static_proc_address(const char *funcName)
102 {
103 const glprocs_table_t * const f = find_entry( funcName );
104 if (f) {
105 #if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)
106 return (f->Address == NULL)
107 ? get_entrypoint_address(f->Offset)
108 : f->Address;
109 #elif defined(DISPATCH_FUNCTION_SIZE)
110 return get_entrypoint_address(f->Offset);
111 #else
112 return f->Address;
113 #endif
114 }
115 else {
116 return NULL;
117 }
118 }
119
120 #endif /* !defined(XFree86Server) */
121
122
123
124 /**
125 * Return the name of the function at the given offset in the dispatch
126 * table. For debugging only.
127 */
128 static const char *
129 get_static_proc_name( GLuint offset )
130 {
131 GLuint i;
132 for (i = 0; static_functions[i].Name_offset >= 0; i++) {
133 if (static_functions[i].Offset == offset) {
134 return gl_string_table + static_functions[i].Name_offset;
135 }
136 }
137 return NULL;
138 }
139
140
141
142 /**********************************************************************
143 * Extension function management.
144 */
145
146
147 /**
148 * Track information about a function added to the GL API.
149 */
150 struct _glapi_function {
151 /**
152 * Name of the function.
153 */
154 const char * name;
155
156
157 /**
158 * Text string that describes the types of the parameters passed to the
159 * named function. Parameter types are converted to characters using the
160 * following rules:
161 * - 'i' for \c GLint, \c GLuint, and \c GLenum
162 * - 'p' for any pointer type
163 * - 'f' for \c GLfloat and \c GLclampf
164 * - 'd' for \c GLdouble and \c GLclampd
165 */
166 const char * parameter_signature;
167
168
169 /**
170 * Offset in the dispatch table where the pointer to the real function is
171 * located. If the driver has not requested that the named function be
172 * added to the dispatch table, this will have the value ~0.
173 */
174 unsigned dispatch_offset;
175
176
177 /**
178 * Pointer to the dispatch stub for the named function.
179 *
180 * \todo
181 * The semantic of this field should be changed slightly. Currently, it
182 * is always expected to be non-\c NULL. However, it would be better to
183 * only allocate the entry-point stub when the application requests the
184 * function via \c glXGetProcAddress. This would save memory for all the
185 * functions that the driver exports but that the application never wants
186 * to call.
187 */
188 _glapi_proc dispatch_stub;
189 };
190
191
192 /*
193 * Number of extension functions which we can dynamically add at runtime.
194 */
195 #define MAX_EXTENSION_FUNCS 300
196
197
198 static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];
199 static GLuint NumExtEntryPoints = 0;
200
201
202 /**
203 * strdup() is actually not a standard ANSI C or POSIX routine.
204 * Irix will not define it if ANSI mode is in effect.
205 */
206 static char *
207 str_dup(const char *str)
208 {
209 char *copy;
210 copy = (char*) malloc(strlen(str) + 1);
211 if (!copy)
212 return NULL;
213 strcpy(copy, str);
214 return copy;
215 }
216
217
218 /**
219 * Generate new entrypoint
220 *
221 * Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver
222 * calls \c _glapi_add_dispatch we'll put in the proper offset. If that
223 * never happens, and the user calls this function, he'll segfault. That's
224 * what you get when you try calling a GL function that doesn't really exist.
225 *
226 * \param funcName Name of the function to create an entry-point for.
227 *
228 * \sa _glapi_add_entrypoint
229 */
230
231 static struct _glapi_function *
232 add_function_name( const char * funcName )
233 {
234 struct _glapi_function * entry = NULL;
235
236 if (NumExtEntryPoints < MAX_EXTENSION_FUNCS) {
237 _glapi_proc entrypoint = generate_entrypoint(~0);
238 if (entrypoint != NULL) {
239 entry = & ExtEntryTable[NumExtEntryPoints];
240
241 ExtEntryTable[NumExtEntryPoints].name = str_dup(funcName);
242 ExtEntryTable[NumExtEntryPoints].parameter_signature = NULL;
243 ExtEntryTable[NumExtEntryPoints].dispatch_offset = ~0;
244 ExtEntryTable[NumExtEntryPoints].dispatch_stub = entrypoint;
245 NumExtEntryPoints++;
246 }
247 }
248
249 return entry;
250 }
251
252
253 /**
254 * Fill-in the dispatch stub for the named function.
255 *
256 * This function is intended to be called by a hardware driver. When called,
257 * a dispatch stub may be created created for the function. A pointer to this
258 * dispatch function will be returned by glXGetProcAddress.
259 *
260 * \param function_names Array of pointers to function names that should
261 * share a common dispatch offset.
262 * \param parameter_signature String representing the types of the parameters
263 * passed to the named function. Parameter types
264 * are converted to characters using the following
265 * rules:
266 * - 'i' for \c GLint, \c GLuint, and \c GLenum
267 * - 'p' for any pointer type
268 * - 'f' for \c GLfloat and \c GLclampf
269 * - 'd' for \c GLdouble and \c GLclampd
270 *
271 * \returns
272 * The offset in the dispatch table of the named function. A pointer to the
273 * driver's implementation of the named function should be stored at
274 * \c dispatch_table[\c offset]. Return -1 if error/problem.
275 *
276 * \sa glXGetProcAddress
277 *
278 * \warning
279 * This function can only handle up to 8 names at a time. As far as I know,
280 * the maximum number of names ever associated with an existing GL function is
281 * 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,
282 * \c glPointParameterfARB, and \c glPointParameterf), so this should not be
283 * too painful of a limitation.
284 *
285 * \todo
286 * Determine whether or not \c parameter_signature should be allowed to be
287 * \c NULL. It doesn't seem like much of a hardship for drivers to have to
288 * pass in an empty string.
289 *
290 * \todo
291 * Determine if code should be added to reject function names that start with
292 * 'glX'.
293 *
294 * \bug
295 * Add code to compare \c parameter_signature with the parameter signature of
296 * a static function. In order to do that, we need to find a way to \b get
297 * the parameter signature of a static function.
298 */
299
300 PUBLIC int
301 _glapi_add_dispatch( const char * const * function_names,
302 const char * parameter_signature )
303 {
304 static int next_dynamic_offset = _gloffset_FIRST_DYNAMIC;
305 const char * const real_sig = (parameter_signature != NULL)
306 ? parameter_signature : "";
307 struct _glapi_function * entry[8];
308 GLboolean is_static[8];
309 unsigned i;
310 unsigned j;
311 int offset = ~0;
312 int new_offset;
313
314
315 (void) memset( is_static, 0, sizeof( is_static ) );
316 (void) memset( entry, 0, sizeof( entry ) );
317
318 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
319 /* Do some trivial validation on the name of the function.
320 */
321
322 if (!function_names[i] || function_names[i][0] != 'g' || function_names[i][1] != 'l')
323 return -1;
324
325 /* Determine if the named function already exists. If the function does
326 * exist, it must have the same parameter signature as the function
327 * being added.
328 */
329
330 new_offset = get_static_proc_offset(function_names[i]);
331 if (new_offset >= 0) {
332 /* FIXME: Make sure the parameter signatures match! How do we get
333 * FIXME: the parameter signature for static functions?
334 */
335
336 if ( (offset != ~0) && (new_offset != offset) ) {
337 return -1;
338 }
339
340 is_static[i] = GL_TRUE;
341 offset = new_offset;
342 }
343
344
345 for ( j = 0 ; j < NumExtEntryPoints ; j++ ) {
346 if (strcmp(ExtEntryTable[j].name, function_names[i]) == 0) {
347 /* The offset may be ~0 if the function name was added by
348 * glXGetProcAddress but never filled in by the driver.
349 */
350
351 if (ExtEntryTable[j].dispatch_offset != ~0) {
352 if (strcmp(real_sig, ExtEntryTable[j].parameter_signature)
353 != 0) {
354 return -1;
355 }
356
357 if ( (offset != ~0) && (ExtEntryTable[j].dispatch_offset != offset) ) {
358 return -1;
359 }
360
361 offset = ExtEntryTable[j].dispatch_offset;
362 }
363
364 entry[i] = & ExtEntryTable[j];
365 break;
366 }
367 }
368 }
369
370 if (offset == ~0) {
371 offset = next_dynamic_offset;
372 next_dynamic_offset++;
373 }
374
375 for ( i = 0 ; function_names[i] != NULL ; i++ ) {
376 if (! is_static[i] ) {
377 if (entry[i] == NULL) {
378 entry[i] = add_function_name( function_names[i] );
379 if (entry[i] == NULL) {
380 /* FIXME: Possible memory leak here.
381 */
382 return -1;
383 }
384 }
385
386 entry[i]->parameter_signature = str_dup(real_sig);
387 fill_in_entrypoint_offset(entry[i]->dispatch_stub, offset);
388 entry[i]->dispatch_offset = offset;
389 }
390 }
391
392 return offset;
393 }
394
395
396 /**
397 * Return offset of entrypoint for named function within dispatch table.
398 */
399 PUBLIC GLint
400 _glapi_get_proc_offset(const char *funcName)
401 {
402 /* search extension functions first */
403 GLuint i;
404 for (i = 0; i < NumExtEntryPoints; i++) {
405 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
406 return ExtEntryTable[i].dispatch_offset;
407 }
408 }
409 /* search static functions */
410 return get_static_proc_offset(funcName);
411 }
412
413
414
415 /**
416 * Return pointer to the named function. If the function name isn't found
417 * in the name of static functions, try generating a new API entrypoint on
418 * the fly with assembly language.
419 */
420 PUBLIC _glapi_proc
421 _glapi_get_proc_address(const char *funcName)
422 {
423 struct _glapi_function * entry;
424 GLuint i;
425
426 #ifdef MANGLE
427 /* skip the prefix on the name */
428 if (funcName[1] != 'g' || funcName[2] != 'l')
429 return NULL;
430 #else
431 if (funcName[0] != 'g' || funcName[1] != 'l')
432 return NULL;
433 #endif
434
435 /* search extension functions first */
436 for (i = 0; i < NumExtEntryPoints; i++) {
437 if (strcmp(ExtEntryTable[i].name, funcName) == 0) {
438 return ExtEntryTable[i].dispatch_stub;
439 }
440 }
441
442 #if !defined( XFree86Server )
443 /* search static functions */
444 {
445 const _glapi_proc func = get_static_proc_address(funcName);
446 if (func)
447 return func;
448 }
449 #endif /* !defined( XFree86Server ) */
450
451 entry = add_function_name(funcName);
452 return (entry == NULL) ? NULL : entry->dispatch_stub;
453 }
454
455
456
457 /**
458 * Return the name of the function at the given dispatch offset.
459 * This is only intended for debugging.
460 */
461 const char *
462 _glapi_get_proc_name(GLuint offset)
463 {
464 GLuint i;
465 const char * n;
466
467 /* search built-in functions */
468 n = get_static_proc_name(offset);
469 if ( n != NULL ) {
470 return n;
471 }
472
473 /* search added extension functions */
474 for (i = 0; i < NumExtEntryPoints; i++) {
475 if (ExtEntryTable[i].dispatch_offset == offset) {
476 return ExtEntryTable[i].name;
477 }
478 }
479 return NULL;
480 }
481
482
483
484 /**********************************************************************
485 * GL API table functions.
486 */
487
488
489 /*
490 * The dispatch table size (number of entries) is the size of the
491 * _glapi_table struct plus the number of dynamic entries we can add.
492 * The extra slots can be filled in by DRI drivers that register new extension
493 * functions.
494 */
495 #define DISPATCH_TABLE_SIZE (sizeof(struct _glapi_table) / sizeof(void *) + MAX_EXTENSION_FUNCS)
496
497
498 /**
499 * Return size of dispatch table struct as number of functions (or
500 * slots).
501 */
502 PUBLIC GLuint
503 _glapi_get_dispatch_table_size(void)
504 {
505 return DISPATCH_TABLE_SIZE;
506 }
507
508
509 /**
510 * Make sure there are no NULL pointers in the given dispatch table.
511 * Intended for debugging purposes.
512 */
513 void
514 _glapi_check_table_not_null(const struct _glapi_table *table)
515 {
516 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
517 const GLuint entries = _glapi_get_dispatch_table_size();
518 const void **tab = (const void **) table;
519 GLuint i;
520 for (i = 1; i < entries; i++) {
521 assert(tab[i]);
522 }
523 #else
524 (void) table;
525 #endif
526 }
527
528
529 /**
530 * Do some spot checks to be sure that the dispatch table
531 * slots are assigned correctly. For debugging only.
532 */
533 void
534 _glapi_check_table(const struct _glapi_table *table)
535 {
536 #ifdef EXTRA_DEBUG /* set to DEBUG for extra DEBUG */
537 {
538 GLuint BeginOffset = _glapi_get_proc_offset("glBegin");
539 char *BeginFunc = (char*) &table->Begin;
540 GLuint offset = (BeginFunc - (char *) table) / sizeof(void *);
541 assert(BeginOffset == _gloffset_Begin);
542 assert(BeginOffset == offset);
543 }
544 {
545 GLuint viewportOffset = _glapi_get_proc_offset("glViewport");
546 char *viewportFunc = (char*) &table->Viewport;
547 GLuint offset = (viewportFunc - (char *) table) / sizeof(void *);
548 assert(viewportOffset == _gloffset_Viewport);
549 assert(viewportOffset == offset);
550 }
551 {
552 GLuint VertexPointerOffset = _glapi_get_proc_offset("glVertexPointer");
553 char *VertexPointerFunc = (char*) &table->VertexPointer;
554 GLuint offset = (VertexPointerFunc - (char *) table) / sizeof(void *);
555 assert(VertexPointerOffset == _gloffset_VertexPointer);
556 assert(VertexPointerOffset == offset);
557 }
558 {
559 GLuint ResetMinMaxOffset = _glapi_get_proc_offset("glResetMinmax");
560 char *ResetMinMaxFunc = (char*) &table->ResetMinmax;
561 GLuint offset = (ResetMinMaxFunc - (char *) table) / sizeof(void *);
562 assert(ResetMinMaxOffset == _gloffset_ResetMinmax);
563 assert(ResetMinMaxOffset == offset);
564 }
565 {
566 GLuint blendColorOffset = _glapi_get_proc_offset("glBlendColor");
567 char *blendColorFunc = (char*) &table->BlendColor;
568 GLuint offset = (blendColorFunc - (char *) table) / sizeof(void *);
569 assert(blendColorOffset == _gloffset_BlendColor);
570 assert(blendColorOffset == offset);
571 }
572 {
573 GLuint secondaryColor3fOffset = _glapi_get_proc_offset("glSecondaryColor3fEXT");
574 char *secondaryColor3fFunc = (char*) &table->SecondaryColor3fEXT;
575 GLuint offset = (secondaryColor3fFunc - (char *) table) / sizeof(void *);
576 assert(secondaryColor3fOffset == _gloffset_SecondaryColor3fEXT);
577 assert(secondaryColor3fOffset == offset);
578 }
579 {
580 GLuint pointParameterivOffset = _glapi_get_proc_offset("glPointParameterivNV");
581 char *pointParameterivFunc = (char*) &table->PointParameterivNV;
582 GLuint offset = (pointParameterivFunc - (char *) table) / sizeof(void *);
583 assert(pointParameterivOffset == _gloffset_PointParameterivNV);
584 assert(pointParameterivOffset == offset);
585 }
586 {
587 GLuint setFenceOffset = _glapi_get_proc_offset("glSetFenceNV");
588 char *setFenceFunc = (char*) &table->SetFenceNV;
589 GLuint offset = (setFenceFunc - (char *) table) / sizeof(void *);
590 assert(setFenceOffset == _gloffset_SetFenceNV);
591 assert(setFenceOffset == offset);
592 }
593 #else
594 (void) table;
595 #endif
596 }