Debian Codegraph @

src/egl/generate/gen_egl_dispatch.py

raw
1 #!/usr/bin/env python3
2
3 # (C) Copyright 2016, NVIDIA CORPORATION.
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
15 # 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 NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS 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 DEALINGS
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Kyle Brenneman <[email protected]>
27
28 """
29 Generates dispatch functions for EGL.
30
31 The list of functions and arguments is read from the Khronos's XML files, with
32 additional information defined in the module eglFunctionList.
33 """
34
35 import argparse
36 import collections
37 import eglFunctionList
38 import sys
39 import textwrap
40
41 import os
42 NEWAPI = os.path.join(os.path.dirname(__file__), "..", "..", "mesa", "glapi", "new")
43 sys.path.insert(0, NEWAPI)
44 import genCommon
45
46 def main():
47 parser = argparse.ArgumentParser()
48 parser.add_argument("--bind-wl-display", action="store_true",
49 help="Include EGL_WL_bind_wayland_display")
50 parser.add_argument("target", choices=("header", "source"),
51 help="Whether to build the source or header file.")
52 parser.add_argument("xml_files", nargs="+",
53 help="The XML files with the EGL function lists.")
54
55 args = parser.parse_args()
56
57 xmlFunctions = genCommon.getFunctions(args.xml_files)
58 xmlByName = dict((f.name, f) for f in xmlFunctions)
59 functions = []
60 for (name, eglFunc) in eglFunctionList.EGL_FUNCTIONS:
61 if not args.bind_wl_display and "Wayland" in name:
62 continue
63 func = xmlByName[name]
64 eglFunc = fixupEglFunc(func, eglFunc)
65 functions.append((func, eglFunc))
66
67 # Sort the function list by name.
68 functions = sorted(functions, key=lambda f: f[0].name)
69
70 if args.target == "header":
71 text = generateHeader(functions)
72 elif args.target == "source":
73 text = generateSource(functions)
74 sys.stdout.write(text)
75
76
77 def fixupEglFunc(func, eglFunc):
78 result = dict(eglFunc)
79 if result.get("prefix") is None:
80 result["prefix"] = ""
81
82 if result.get("extension") is not None:
83 text = "defined(" + result["extension"] + ")"
84 result["extension"] = text
85
86 if result["method"] in ("none", "custom"):
87 return result
88
89 if result["method"] not in ("display", "device", "current"):
90 raise ValueError("Invalid dispatch method %r for function %r" %
91 (result["method"], func.name))
92
93 if func.hasReturn():
94 if result.get("retval") is None:
95 result["retval"] = getDefaultReturnValue(func.rt)
96
97 return result
98
99
100 def generateHeader(functions):
101 text = textwrap.dedent(r"""
102 #ifndef G_EGLDISPATCH_STUBS_H
103 #define G_EGLDISPATCH_STUBS_H
104
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108
109 #include <EGL/egl.h>
110 #include <EGL/eglext.h>
111 #include <EGL/eglmesaext.h>
112 #include <EGL/eglext_angle.h>
113 #include <GL/mesa_glinterop.h>
114 #include "glvnd/libeglabi.h"
115
116 """.lstrip("\n"))
117
118 text += "enum {\n"
119 for (func, eglFunc) in functions:
120 text += generateGuardBegin(func, eglFunc)
121 text += " __EGL_DISPATCH_" + func.name + ",\n"
122 text += generateGuardEnd(func, eglFunc)
123 text += " __EGL_DISPATCH_COUNT\n"
124 text += "};\n"
125
126 for (func, eglFunc) in functions:
127 if eglFunc["inheader"]:
128 text += generateGuardBegin(func, eglFunc)
129 text += "{f.rt} EGLAPIENTRY {ex[prefix]}{f.name}({f.decArgs});\n".format(
130 f=func, ex=eglFunc)
131 text += generateGuardEnd(func, eglFunc)
132
133 text += textwrap.dedent(r"""
134 #ifdef __cplusplus
135 }
136 #endif
137 #endif // G_EGLDISPATCH_STUBS_H
138 """)
139 return text
140
141
142 def generateSource(functions):
143 # First, sort the function list by name.
144 text = ""
145 text += '#include "egldispatchstubs.h"\n'
146 text += '#include "g_egldispatchstubs.h"\n'
147 text += '#include <stddef.h>\n'
148 text += "\n"
149
150 for (func, eglFunc) in functions:
151 if eglFunc["method"] not in ("custom", "none"):
152 text += generateGuardBegin(func, eglFunc)
153 text += generateDispatchFunc(func, eglFunc)
154 text += generateGuardEnd(func, eglFunc)
155
156 text += "\n"
157 text += "const char * const __EGL_DISPATCH_FUNC_NAMES[__EGL_DISPATCH_COUNT + 1] = {\n"
158 for (func, eglFunc) in functions:
159 text += generateGuardBegin(func, eglFunc)
160 text += ' "' + func.name + '",\n'
161 text += generateGuardEnd(func, eglFunc)
162 text += " NULL\n"
163 text += "};\n"
164
165 text += "const __eglMustCastToProperFunctionPointerType __EGL_DISPATCH_FUNCS[__EGL_DISPATCH_COUNT + 1] = {\n"
166 for (func, eglFunc) in functions:
167 text += generateGuardBegin(func, eglFunc)
168 if eglFunc["method"] != "none":
169 text += " (__eglMustCastToProperFunctionPointerType) " + \
170 eglFunc.get("prefix", "") + func.name + ",\n"
171 else:
172 text += " NULL, // " + func.name + "\n"
173 text += generateGuardEnd(func, eglFunc)
174 text += " NULL\n"
175 text += "};\n"
176
177 return text
178
179
180 def generateGuardBegin(func, eglFunc):
181 ext = eglFunc.get("extension")
182 if ext is not None:
183 return "#if " + ext + "\n"
184 else:
185 return ""
186
187
188 def generateGuardEnd(func, eglFunc):
189 if eglFunc.get("extension") is not None:
190 return "#endif\n"
191 else:
192 return ""
193
194
195 def generateDispatchFunc(func, eglFunc):
196 text = ""
197
198 if eglFunc.get("static"):
199 text += "static "
200 elif eglFunc.get("public"):
201 text += "PUBLIC "
202 text += textwrap.dedent(
203 r"""
204 {f.rt} EGLAPIENTRY {ef[prefix]}{f.name}({f.decArgs})
205 {{
206 typedef {f.rt} EGLAPIENTRY (* _pfn_{f.name})({f.decArgs});
207 """).lstrip("\n").format(f=func, ef=eglFunc)
208
209 if func.hasReturn():
210 text += " {f.rt} _ret = {ef[retval]};\n".format(f=func, ef=eglFunc)
211
212 text += " _pfn_{f.name} _ptr_{f.name} = (_pfn_{f.name}) ".format(f=func)
213 if eglFunc["method"] == "current":
214 text += "__eglDispatchFetchByCurrent(__EGL_DISPATCH_{f.name});\n".format(
215 f=func)
216
217 elif eglFunc["method"] in ("display", "device"):
218 if eglFunc["method"] == "display":
219 lookupFunc = "__eglDispatchFetchByDisplay"
220 lookupType = "EGLDisplay"
221 else:
222 assert eglFunc["method"] == "device"
223 lookupFunc = "__eglDispatchFetchByDevice"
224 lookupType = "EGLDeviceEXT"
225
226 lookupArg = None
227 for arg in func.args:
228 if arg.type == lookupType:
229 lookupArg = arg.name
230 break
231 if lookupArg is None:
232 raise ValueError("Can't find %s argument for function %s" %
233 (lookupType, func.name,))
234
235 text += "{lookupFunc}({lookupArg}, __EGL_DISPATCH_{f.name});\n".format(
236 f=func, lookupFunc=lookupFunc, lookupArg=lookupArg)
237 else:
238 raise ValueError("Unknown dispatch method: %r" % (eglFunc["method"],))
239
240 text += " if(_ptr_{f.name} != NULL) {{\n".format(f=func)
241 text += " "
242 if func.hasReturn():
243 text += "_ret = "
244 text += "_ptr_{f.name}({f.callArgs});\n".format(f=func)
245 text += " }\n"
246
247 if func.hasReturn():
248 text += " return _ret;\n"
249 text += "}\n"
250 return text
251
252
253 def getDefaultReturnValue(typename):
254 if typename.endswith("*"):
255 return "NULL"
256 elif typename == "EGLDisplay":
257 return "EGL_NO_DISPLAY"
258 elif typename == "EGLContext":
259 return "EGL_NO_CONTEXT"
260 elif typename == "EGLSurface":
261 return "EGL_NO_SURFACE"
262 elif typename == "EGLBoolean":
263 return "EGL_FALSE"
264
265 return "0"
266
267
268 if __name__ == "__main__":
269 main()