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 clientMajorVersion = (int)thread->currentContext->majorVersion; \
101         int idx = clientMajorVersion == 1 ? 0 : 1; \
102         if (!s_client_extensions[idx].fname) { \
103             return; \
104         } \
105         (*s_client_extensions[idx].fname) args; /* NOLINT */ \
106     }
107 
108 #define API_ENTRY_RET(rtype,fname,params,args) \
109     static rtype _egl_ ## fname params \
110     { \
111         EGLThreadInfo* thread  = getEGLThreadInfo(); \
112         if (!thread->currentContext) { \
113             return (rtype)0; \
114         } \
115         int idx = (int)thread->currentContext->majorVersion - 1; \
116         if (!s_client_extensions[idx].fname) { \
117             return (rtype)0; \
118         } \
119         return (*s_client_extensions[idx].fname) args; /* NOLINT */ \
120     }
121 
122 #include "ClientAPIExts.in"
123 #undef API_ENTRY
124 #undef API_ENTRY_RET
125 
126 //
127 // Define a table to map function names to the local _egl_ version of
128 // the extension function, to be used in eglGetProcAddress.
129 //
130 #define API_ENTRY(fname,params,args) \
131     { #fname, (void*)_egl_ ## fname},
132 #define API_ENTRY_RET(rtype,fname,params,args) \
133     API_ENTRY(fname,params,args)
134 
135 static const struct _client_ext_funcs {
136     const char *fname;
137     void* proc;
138 } s_client_ext_funcs[] = {
139 #include "ClientAPIExts.in"
140 };
141 static const int numExtFuncs = sizeof(s_client_ext_funcs) /
142                                sizeof(s_client_ext_funcs[0]);
143 
144 #undef API_ENTRY
145 #undef API_ENTRY_RET
146 
147 //
148 // returns the __egl_ version of the givven extension function name.
149 //
getProcAddress(const char * fname)150 void* getProcAddress(const char *fname)
151 {
152     for (int i=0; i<numExtFuncs; i++) {
153         if (!strcmp(fname, s_client_ext_funcs[i].fname)) {
154             return s_client_ext_funcs[i].proc;
155         }
156     }
157     return NULL;
158 }
159 
160 } // of namespace ClientAPIExts
161