Path: blob/21.2-virgl/src/mapi/glapi/glapi_getproc.c
4560 views
/*1* Mesa 3-D graphics library2*3* Copyright (C) 1999-2008 Brian Paul All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included13* in all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS16* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*/2324/**25* \file glapi_getproc.c26*27* Code for implementing glXGetProcAddress(), etc.28* This was originally in glapi.c but refactored out.29*/303132#include <assert.h>33#include <string.h>34#include <stdlib.h>35#include "glapi/glapi_priv.h"36#include "glapitable.h"373839#define FIRST_DYNAMIC_OFFSET (sizeof(struct _glapi_table) / sizeof(void *))40414243/**********************************************************************44* Static function management.45*/464748#if !defined(DISPATCH_FUNCTION_SIZE)49# define NEED_FUNCTION_POINTER50#endif51#include "glprocs.h"525354/**55* Search the table of static entrypoint functions for the named function56* and return the corresponding glprocs_table_t entry.57*/58static const glprocs_table_t *59get_static_proc( const char * n )60{61GLuint i;62for (i = 0; static_functions[i].Name_offset >= 0; i++) {63const char *testName = gl_string_table + static_functions[i].Name_offset;64if (strcmp(testName, n) == 0)65{66return &static_functions[i];67}68}69return NULL;70}717273/**74* Return dispatch table offset of the named static (built-in) function.75* Return -1 if function not found.76*/77static GLint78get_static_proc_offset(const char *funcName)79{80const glprocs_table_t * const f = get_static_proc( funcName );81if (f == NULL) {82return -1;83}8485return f->Offset;86}87888990/**91* Return dispatch function address for the named static (built-in) function.92* Return NULL if function not found.93*/94static _glapi_proc95get_static_proc_address(const char *funcName)96{97const glprocs_table_t * const f = get_static_proc( funcName );98if (f == NULL) {99return NULL;100}101102#if defined(DISPATCH_FUNCTION_SIZE) && defined(GLX_INDIRECT_RENDERING)103return (f->Address == NULL)104? get_entrypoint_address(f->Offset)105: f->Address;106#elif defined(DISPATCH_FUNCTION_SIZE)107return get_entrypoint_address(f->Offset);108#else109return f->Address;110#endif111}112113114115/**116* Return the name of the function at the given offset in the dispatch117* table. For debugging only.118*/119static const char *120get_static_proc_name( GLuint offset )121{122GLuint i;123for (i = 0; static_functions[i].Name_offset >= 0; i++) {124if (static_functions[i].Offset == offset) {125return gl_string_table + static_functions[i].Name_offset;126}127}128return NULL;129}130131132133/**********************************************************************134* Extension function management.135*/136137138/**139* Track information about a function added to the GL API.140*/141struct _glapi_function {142/**143* Name of the function.144*/145const char * name;146147148/**149* Text string that describes the types of the parameters passed to the150* named function. Parameter types are converted to characters using the151* following rules:152* - 'i' for \c GLint, \c GLuint, and \c GLenum153* - 'p' for any pointer type154* - 'f' for \c GLfloat and \c GLclampf155* - 'd' for \c GLdouble and \c GLclampd156*/157const char * parameter_signature;158159160/**161* Offset in the dispatch table where the pointer to the real function is162* located. If the driver has not requested that the named function be163* added to the dispatch table, this will have the value ~0.164*/165unsigned dispatch_offset;166167168/**169* Pointer to the dispatch stub for the named function.170*171* \todo172* The semantic of this field should be changed slightly. Currently, it173* is always expected to be non-\c NULL. However, it would be better to174* only allocate the entry-point stub when the application requests the175* function via \c glXGetProcAddress. This would save memory for all the176* functions that the driver exports but that the application never wants177* to call.178*/179_glapi_proc dispatch_stub;180};181182183static struct _glapi_function ExtEntryTable[MAX_EXTENSION_FUNCS];184static GLuint NumExtEntryPoints = 0;185186187static struct _glapi_function *188get_extension_proc(const char *funcName)189{190GLuint i;191for (i = 0; i < NumExtEntryPoints; i++) {192if (strcmp(ExtEntryTable[i].name, funcName) == 0) {193return & ExtEntryTable[i];194}195}196return NULL;197}198199200static GLint201get_extension_proc_offset(const char *funcName)202{203const struct _glapi_function * const f = get_extension_proc( funcName );204if (f == NULL) {205return -1;206}207208return f->dispatch_offset;209}210211212static _glapi_proc213get_extension_proc_address(const char *funcName)214{215const struct _glapi_function * const f = get_extension_proc( funcName );216if (f == NULL) {217return NULL;218}219220return f->dispatch_stub;221}222223224static const char *225get_extension_proc_name(GLuint offset)226{227GLuint i;228for (i = 0; i < NumExtEntryPoints; i++) {229if (ExtEntryTable[i].dispatch_offset == offset) {230return ExtEntryTable[i].name;231}232}233return NULL;234}235236237/**238* strdup() is actually not a standard ANSI C or POSIX routine.239* Irix will not define it if ANSI mode is in effect.240*/241static char *242str_dup(const char *str)243{244char *copy;245copy = malloc(strlen(str) + 1);246if (!copy)247return NULL;248strcpy(copy, str);249return copy;250}251252253/**254* Generate new entrypoint255*256* Use a temporary dispatch offset of ~0 (i.e. -1). Later, when the driver257* calls \c _glapi_add_dispatch we'll put in the proper offset. If that258* never happens, and the user calls this function, he'll segfault. That's259* what you get when you try calling a GL function that doesn't really exist.260*261* \param funcName Name of the function to create an entry-point for.262*263* \sa _glapi_add_entrypoint264*/265266static struct _glapi_function *267add_function_name( const char * funcName )268{269struct _glapi_function * entry = NULL;270_glapi_proc entrypoint = NULL;271char * name_dup = NULL;272273if (NumExtEntryPoints >= MAX_EXTENSION_FUNCS)274return NULL;275276if (funcName == NULL)277return NULL;278279name_dup = str_dup(funcName);280if (name_dup == NULL)281return NULL;282283entrypoint = generate_entrypoint(~0);284285if (entrypoint == NULL) {286free(name_dup);287return NULL;288}289290entry = & ExtEntryTable[NumExtEntryPoints];291NumExtEntryPoints++;292293entry->name = name_dup;294entry->parameter_signature = NULL;295entry->dispatch_offset = ~0;296entry->dispatch_stub = entrypoint;297298return entry;299}300301302static struct _glapi_function *303set_entry_info( struct _glapi_function * entry, const char * signature, unsigned offset )304{305char * sig_dup = NULL;306307if (signature == NULL)308return NULL;309310sig_dup = str_dup(signature);311if (sig_dup == NULL)312return NULL;313314fill_in_entrypoint_offset(entry->dispatch_stub, offset);315316entry->parameter_signature = sig_dup;317entry->dispatch_offset = offset;318319return entry;320}321322323/**324* Fill-in the dispatch stub for the named function.325*326* This function is intended to be called by a hardware driver. When called,327* a dispatch stub may be created for the function. A pointer to this328* dispatch function will be returned by glXGetProcAddress.329*330* \param function_names Array of pointers to function names that should331* share a common dispatch offset.332* \param parameter_signature String representing the types of the parameters333* passed to the named function. Parameter types334* are converted to characters using the following335* rules:336* - 'i' for \c GLint, \c GLuint, and \c GLenum337* - 'p' for any pointer type338* - 'f' for \c GLfloat and \c GLclampf339* - 'd' for \c GLdouble and \c GLclampd340*341* \returns342* The offset in the dispatch table of the named function. A pointer to the343* driver's implementation of the named function should be stored at344* \c dispatch_table[\c offset]. Return -1 if error/problem.345*346* \sa glXGetProcAddress347*348* \warning349* This function can only handle up to 8 names at a time. As far as I know,350* the maximum number of names ever associated with an existing GL function is351* 4 (\c glPointParameterfSGIS, \c glPointParameterfEXT,352* \c glPointParameterfARB, and \c glPointParameterf), so this should not be353* too painful of a limitation.354*355* \todo356* Determine whether or not \c parameter_signature should be allowed to be357* \c NULL. It doesn't seem like much of a hardship for drivers to have to358* pass in an empty string.359*360* \todo361* Determine if code should be added to reject function names that start with362* 'glX'.363*364* \bug365* Add code to compare \c parameter_signature with the parameter signature of366* a static function. In order to do that, we need to find a way to \b get367* the parameter signature of a static function.368*/369370int371_glapi_add_dispatch( const char * const * function_names,372const char * parameter_signature )373{374static int next_dynamic_offset = FIRST_DYNAMIC_OFFSET;375const char * const real_sig = (parameter_signature != NULL)376? parameter_signature : "";377struct _glapi_function * entry[8];378GLboolean is_static[8];379unsigned i;380int offset = ~0;381382init_glapi_relocs_once();383384(void) memset( is_static, 0, sizeof( is_static ) );385(void) memset( entry, 0, sizeof( entry ) );386387/* Find the _single_ dispatch offset for all function names that already388* exist (and have a dispatch offset).389*/390391for ( i = 0 ; function_names[i] != NULL ; i++ ) {392const char * funcName = function_names[i];393int static_offset;394int extension_offset;395396if (funcName[0] != 'g' || funcName[1] != 'l')397return -1;398399/* search built-in functions */400static_offset = get_static_proc_offset(funcName);401402if (static_offset >= 0) {403404is_static[i] = GL_TRUE;405406/* FIXME: Make sure the parameter signatures match! How do we get407* FIXME: the parameter signature for static functions?408*/409410if ( (offset != ~0) && (static_offset != offset) ) {411return -1;412}413414offset = static_offset;415416continue;417}418419/* search added extension functions */420entry[i] = get_extension_proc(funcName);421422if (entry[i] != NULL) {423extension_offset = entry[i]->dispatch_offset;424425/* The offset may be ~0 if the function name was added by426* glXGetProcAddress but never filled in by the driver.427*/428429if (extension_offset == ~0) {430continue;431}432433if (strcmp(real_sig, entry[i]->parameter_signature) != 0) {434return -1;435}436437if ( (offset != ~0) && (extension_offset != offset) ) {438return -1;439}440441offset = extension_offset;442}443}444445/* If all function names are either new (or with no dispatch offset),446* allocate a new dispatch offset.447*/448449if (offset == ~0) {450offset = next_dynamic_offset;451next_dynamic_offset++;452}453454/* Fill in the dispatch offset for the new function names (and those with455* no dispatch offset).456*/457458for ( i = 0 ; function_names[i] != NULL ; i++ ) {459if (is_static[i]) {460continue;461}462463/* generate entrypoints for new function names */464if (entry[i] == NULL) {465entry[i] = add_function_name( function_names[i] );466if (entry[i] == NULL) {467/* FIXME: Possible memory leak here. */468return -1;469}470}471472if (entry[i]->dispatch_offset == ~0) {473set_entry_info( entry[i], real_sig, offset );474}475}476477return offset;478}479480481/**482* Return offset of entrypoint for named function within dispatch table.483*/484GLint485_glapi_get_proc_offset(const char *funcName)486{487GLint offset;488489/* search extension functions first */490offset = get_extension_proc_offset(funcName);491if (offset >= 0)492return offset;493494/* search static functions */495return get_static_proc_offset(funcName);496}497498499500/**501* Return pointer to the named function. If the function name isn't found502* in the name of static functions, try generating a new API entrypoint on503* the fly with assembly language.504*/505_glapi_proc506_glapi_get_proc_address(const char *funcName)507{508_glapi_proc func;509struct _glapi_function * entry;510511init_glapi_relocs_once();512513if (!funcName || funcName[0] != 'g' || funcName[1] != 'l')514return NULL;515516/* search extension functions first */517func = get_extension_proc_address(funcName);518if (func)519return func;520521/* search static functions */522func = get_static_proc_address(funcName);523if (func)524return func;525526/* generate entrypoint, dispatch offset must be filled in by the driver */527entry = add_function_name(funcName);528if (entry == NULL)529return NULL;530531return entry->dispatch_stub;532}533534535536/**537* Return the name of the function at the given dispatch offset.538* This is only intended for debugging.539*/540const char *541_glapi_get_proc_name(GLuint offset)542{543const char * n;544545/* search built-in functions */546n = get_static_proc_name(offset);547if ( n != NULL ) {548return n;549}550551/* search added extension functions */552return get_extension_proc_name(offset);553}554555556557/**********************************************************************558* GL API table functions.559*/560561562/**563* Return size of dispatch table struct as number of functions (or564* slots).565*/566GLuint567_glapi_get_dispatch_table_size(void)568{569/*570* The dispatch table size (number of entries) is the size of the571* _glapi_table struct plus the number of dynamic entries we can add.572* The extra slots can be filled in by DRI drivers that register new573* extension functions.574*/575return FIRST_DYNAMIC_OFFSET + MAX_EXTENSION_FUNCS;576}577578579