xref: /aosp_15_r20/external/mesa3d/src/mapi/shared-glapi/glapi.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  * Copyright (C) 2010 LunarG Inc.
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 OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Chia-I Wu <[email protected]>
27  */
28 
29 #include <string.h>
30 #include <stdlib.h>
31 #include "glapi/glapi.h"
32 #include "u_current.h"
33 #include "table.h" /* for MAPI_TABLE_NUM_SLOTS */
34 #include "stub.h"
35 
36 /*
37  * _glapi_Dispatch, _glapi_Context
38  * _glapi_tls_Dispatch, _glapi_tls_Context,
39  * _glapi_set_context, _glapi_get_context,
40  * _glapi_destroy_multithread, _glapi_check_multithread
41  * _glapi_set_dispatch, and _glapi_get_dispatch
42  * are defined in u_current.c.
43  */
44 
45 /**
46  * Return size of dispatch table struct as number of functions (or
47  * slots).
48  */
49 unsigned int
_glapi_get_dispatch_table_size(void)50 _glapi_get_dispatch_table_size(void)
51 {
52    return MAPI_TABLE_NUM_SLOTS;
53 }
54 
55 /**
56  * Initializes the glapi relocs table (no-op for shared-glapi), and returns the
57  * offset of the given function in the dispatch table.
58  */
59 int
_glapi_add_dispatch(const char * function_name)60 _glapi_add_dispatch( const char * function_name )
61 {
62    assert(function_name[0] == 'g' && function_name[1] == 'l');
63 
64    const struct mapi_stub *stub = stub_find_public(function_name + 2);
65    if (!stub)
66       return -1;
67 
68    return stub_get_slot(stub);
69 }
70 
71 static const struct mapi_stub *
_glapi_get_stub(const char * name)72 _glapi_get_stub(const char *name)
73 {
74    if (!name || name[0] != 'g' || name[1] != 'l')
75       return NULL;
76    name += 2;
77 
78    return stub_find_public(name);
79 }
80 
81 /**
82  * Return offset of entrypoint for named function within dispatch table.
83  */
84 int
_glapi_get_proc_offset(const char * funcName)85 _glapi_get_proc_offset(const char *funcName)
86 {
87    const struct mapi_stub *stub = _glapi_get_stub(funcName);
88    return (stub) ? stub_get_slot(stub) : -1;
89 }
90 
91 /**
92  * Return pointer to the named function.  If the function name isn't found
93  * in the name of static functions, try generating a new API entrypoint on
94  * the fly with assembly language.
95  */
96 _glapi_proc
_glapi_get_proc_address(const char * funcName)97 _glapi_get_proc_address(const char *funcName)
98 {
99    const struct mapi_stub *stub = _glapi_get_stub(funcName);
100    return (stub) ? (_glapi_proc) stub_get_addr(stub) : NULL;
101 }
102 
103 /**
104  * Return the name of the function at the given dispatch offset.
105  * This is only intended for debugging.
106  */
107 const char *
_glapi_get_proc_name(unsigned int offset)108 _glapi_get_proc_name(unsigned int offset)
109 {
110    const struct mapi_stub *stub = stub_find_by_slot(offset);
111    return stub ? stub_get_name(stub) : NULL;
112 }
113 
114 /** Return pointer to new dispatch table filled with no-op functions */
115 struct _glapi_table *
_glapi_new_nop_table(unsigned num_entries)116 _glapi_new_nop_table(unsigned num_entries)
117 {
118    struct _glapi_table *table;
119 
120    if (num_entries > MAPI_TABLE_NUM_SLOTS)
121       num_entries = MAPI_TABLE_NUM_SLOTS;
122 
123    table = malloc(num_entries * sizeof(mapi_func));
124    if (table) {
125       memcpy(table, table_noop_array, num_entries * sizeof(mapi_func));
126    }
127    return table;
128 }
129 
130 void
_glapi_set_nop_handler(_glapi_nop_handler_proc func)131 _glapi_set_nop_handler(_glapi_nop_handler_proc func)
132 {
133    table_set_noop_handler(func);
134 }
135 
136 /**
137  * This is a deprecated function which should not be used anymore.
138  * It's only present to satisfy linking with older versions of libGL.
139  */
140 unsigned long
_glthread_GetID(void)141 _glthread_GetID(void)
142 {
143    return 0;
144 }
145 
146 void
_glapi_noop_enable_warnings(unsigned char enable)147 _glapi_noop_enable_warnings(unsigned char enable)
148 {
149 }
150 
151 void
_glapi_set_warning_func(_glapi_proc func)152 _glapi_set_warning_func(_glapi_proc func)
153 {
154 }
155