1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "ClientAPIExts.h"
17 #include "ThreadInfo.h"
18 #include <GLES/gl.h>
19 #include <GLES/glext.h>
20 #include "eglContext.h"
21
22 namespace ClientAPIExts
23 {
24
25 //
26 // define function pointer type for each extention function
27 // typename has the form __egl_{funcname}_t
28 //
29 #define FUNC_TYPE(fname) __egl_ ## fname ## _t
30 // NOLINT: clang-tidy adds parentheses around 'params'.
31 #define API_ENTRY(fname,params,args) \
32 typedef void (GL_APIENTRY *FUNC_TYPE(fname)) params; // NOLINT
33
34 #define API_ENTRY_RET(rtype,fname,params,args) \
35 typedef rtype (GL_APIENTRY *FUNC_TYPE(fname)) params; // NOLINT
36
37 #include "ClientAPIExts.in"
38 #undef API_ENTRY
39 #undef API_ENTRY_RET
40
41 /////
42 // Define static table to store the function value for each
43 // client API. functions pointers will get initialized through
44 // ClientAPIExts::initClientFuncs function after each client API has been
45 // loaded.
46 /////
47 #define API_ENTRY(fname,params,args) \
48 FUNC_TYPE(fname) (fname);
49
50 #define API_ENTRY_RET(rtype,fname,params,args) \
51 API_ENTRY(fname,params,args)
52
53 static struct _ext_table
54 {
55 #include "ClientAPIExts.in"
56 } s_client_extensions[2];
57
58 #undef API_ENTRY
59 #undef API_ENTRY_RET
60
61 //
62 // This function initialized each entry in the s_client_extensions
63 // struct at the givven index using the givven client interface
64 //
initClientFuncs(const EGLClient_glesInterface * iface,int idx)65 void initClientFuncs(const EGLClient_glesInterface *iface, int idx)
66 {
67 #define API_ENTRY(fname,params,args) \
68 s_client_extensions[idx].fname = \
69 (FUNC_TYPE(fname))iface->getProcAddress(#fname);
70
71 #define API_ENTRY_RET(rtype,fname,params,args) \
72 API_ENTRY(fname,params,args)
73
74 //
75 // reset all func pointers to NULL
76 //
77 memset(&s_client_extensions[idx], 0, sizeof(struct _ext_table));
78
79 //
80 // And now query the GLES library for each proc address
81 //
82 #include "ClientAPIExts.in"
83 #undef API_ENTRY
84 #undef API_ENTRY_RET
85 }
86
87 //
88 // Define implementation for each extension function which checks
89 // the current context version and calls to the correct client API
90 // function.
91 //
92 // NOLINT: clang-tidy adds parentheses around 'args'.
93 #define API_ENTRY(fname,params,args) \
94 static void _egl_ ## fname params \
95 { \
96 EGLThreadInfo* thread = getEGLThreadInfo(); \
97 if (!thread->currentContext) { \
98 return; \
99 } \
100 int idx = (int)thread->currentContext->majorVersion - 1; \
101 if (!s_client_extensions[idx].fname) { \
102 return; \
103 } \
104 (*s_client_extensions[idx].fname) args; /* NOLINT */ \
105 }
106
107 #define API_ENTRY_RET(rtype,fname,params,args) \
108 static rtype _egl_ ## fname params \
109 { \
110 EGLThreadInfo* thread = getEGLThreadInfo(); \
111 if (!thread->currentContext) { \
112 return (rtype)0; \
113 } \
114 int idx = (int)thread->currentContext->majorVersion - 1; \
115 if (!s_client_extensions[idx].fname) { \
116 return (rtype)0; \
117 } \
118 return (*s_client_extensions[idx].fname) args; /* NOLINT */ \
119 }
120
121 #include "ClientAPIExts.in"
122 #undef API_ENTRY
123 #undef API_ENTRY_RET
124
125 //
126 // Define a table to map function names to the local _egl_ version of
127 // the extension function, to be used in eglGetProcAddress.
128 //
129 #define API_ENTRY(fname,params,args) \
130 { #fname, (void*)_egl_ ## fname},
131 #define API_ENTRY_RET(rtype,fname,params,args) \
132 API_ENTRY(fname,params,args)
133
134 static const struct _client_ext_funcs {
135 const char *fname;
136 void* proc;
137 } s_client_ext_funcs[] = {
138 #include "ClientAPIExts.in"
139 };
140 static const int numExtFuncs = sizeof(s_client_ext_funcs) /
141 sizeof(s_client_ext_funcs[0]);
142
143 #undef API_ENTRY
144 #undef API_ENTRY_RET
145
146 //
147 // returns the __egl_ version of the givven extension function name.
148 //
getProcAddress(const char * fname)149 void* getProcAddress(const char *fname)
150 {
151 for (int i=0; i<numExtFuncs; i++) {
152 if (!strcmp(fname, s_client_ext_funcs[i].fname)) {
153 return s_client_ext_funcs[i].proc;
154 }
155 }
156 return NULL;
157 }
158
159 } // of namespace ClientAPIExts
160