3 # (C) Copyright 2016, NVIDIA CORPORATION.
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:
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
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
26 # Kyle Brenneman <kbrenneman@nvidia.com>
29 Generates dispatch functions for EGL.
31 The list of functions and arguments is read from the Khronos's XML files, with
32 additional information defined in the module eglFunctionList.
37 import eglFunctionList
42 NEWAPI
= os
.path
.join(os
.path
.dirname(__file__
), "..", "..", "mapi", "new")
43 sys
.path
.insert(0, NEWAPI
)
47 parser
= argparse
.ArgumentParser()
48 parser
.add_argument("target", choices
=("header", "source"),
49 help="Whether to build the source or header file.")
50 parser
.add_argument("xml_files", nargs
="+", help="The XML files with the EGL function lists.")
52 args
= parser
.parse_args()
54 xmlFunctions
= genCommon
.getFunctions(args
.xml_files
)
55 xmlByName
= dict((f
.name
, f
) for f
in xmlFunctions
)
57 for (name
, eglFunc
) in eglFunctionList
.EGL_FUNCTIONS
:
58 func
= xmlByName
[name
]
59 eglFunc
= fixupEglFunc(func
, eglFunc
)
60 functions
.append((func
, eglFunc
))
62 # Sort the function list by name.
63 functions
= sorted(functions
, key
=lambda f
: f
[0].name
)
65 if args
.target
== "header":
66 text
= generateHeader(functions
)
67 elif args
.target
== "source":
68 text
= generateSource(functions
)
69 sys
.stdout
.write(text
)
71 def fixupEglFunc(func
, eglFunc
):
72 result
= dict(eglFunc
)
73 if result
.get("prefix") is None:
76 if result
.get("extension") is not None:
77 text
= "defined(" + result
["extension"] + ")"
78 result
["extension"] = text
80 if result
["method"] in ("none", "custom"):
83 if result
["method"] not in ("display", "device", "current"):
84 raise ValueError("Invalid dispatch method %r for function %r" % (result
["method"], func
.name
))
87 if result
.get("retval") is None:
88 result
["retval"] = getDefaultReturnValue(func
.rt
)
92 def generateHeader(functions
):
93 text
= textwrap
.dedent(r
"""
94 #ifndef G_EGLDISPATCH_STUBS_H
95 #define G_EGLDISPATCH_STUBS_H
102 #include <EGL/eglext.h>
103 #include "glvnd/libeglabi.h"
108 for (func
, eglFunc
) in functions
:
109 text
+= generateGuardBegin(func
, eglFunc
)
110 text
+= " __EGL_DISPATCH_" + func
.name
+ ",\n"
111 text
+= generateGuardEnd(func
, eglFunc
)
112 text
+= " __EGL_DISPATCH_COUNT\n"
115 for (func
, eglFunc
) in functions
:
116 if eglFunc
["inheader"]:
117 text
+= generateGuardBegin(func
, eglFunc
)
118 text
+= "{f.rt} EGLAPIENTRY {ex[prefix]}{f.name}({f.decArgs});\n".format(f
=func
, ex
=eglFunc
)
119 text
+= generateGuardEnd(func
, eglFunc
)
121 text
+= textwrap
.dedent(r
"""
125 #endif // G_EGLDISPATCH_STUBS_H
129 def generateSource(functions
):
130 # First, sort the function list by name.
132 text
+= '#include "egldispatchstubs.h"\n'
133 text
+= '#include "g_egldispatchstubs.h"\n'
134 text
+= '#include <stddef.h>\n'
137 for (func
, eglFunc
) in functions
:
138 if eglFunc
["method"] not in ("custom", "none"):
139 text
+= generateGuardBegin(func
, eglFunc
)
140 text
+= generateDispatchFunc(func
, eglFunc
)
141 text
+= generateGuardEnd(func
, eglFunc
)
144 text
+= "const char * const __EGL_DISPATCH_FUNC_NAMES[__EGL_DISPATCH_COUNT + 1] = {\n"
145 for (func
, eglFunc
) in functions
:
146 text
+= generateGuardBegin(func
, eglFunc
)
147 text
+= ' "' + func
.name
+ '",\n'
148 text
+= generateGuardEnd(func
, eglFunc
)
152 text
+= "const __eglMustCastToProperFunctionPointerType __EGL_DISPATCH_FUNCS[__EGL_DISPATCH_COUNT + 1] = {\n"
153 for (func
, eglFunc
) in functions
:
154 text
+= generateGuardBegin(func
, eglFunc
)
155 if eglFunc
["method"] != "none":
156 text
+= " (__eglMustCastToProperFunctionPointerType) " + eglFunc
.get("prefix", "") + func
.name
+ ",\n"
158 text
+= " NULL, // " + func
.name
+ "\n"
159 text
+= generateGuardEnd(func
, eglFunc
)
165 def generateGuardBegin(func
, eglFunc
):
166 ext
= eglFunc
.get("extension")
168 return "#if " + ext
+ "\n"
172 def generateGuardEnd(func
, eglFunc
):
173 if eglFunc
.get("extension") is not None:
178 def generateDispatchFunc(func
, eglFunc
):
181 if eglFunc
.get("static"):
183 elif eglFunc
.get("public"):
185 text
+= textwrap
.dedent(
187 {f.rt} EGLAPIENTRY {ef[prefix]}{f.name}({f.decArgs})
189 typedef {f.rt} EGLAPIENTRY (* _pfn_{f.name})({f.decArgs});
190 """).lstrip("\n").format(f
=func
, ef
=eglFunc
)
193 text
+= " {f.rt} _ret = {ef[retval]};\n".format(f
=func
, ef
=eglFunc
)
195 text
+= " _pfn_{f.name} _ptr_{f.name} = (_pfn_{f.name}) ".format(f
=func
)
196 if eglFunc
["method"] == "current":
197 text
+= "__eglDispatchFetchByCurrent(__EGL_DISPATCH_{f.name});\n".format(f
=func
)
199 elif eglFunc
["method"] in ("display", "device"):
200 if eglFunc
["method"] == "display":
201 lookupFunc
= "__eglDispatchFetchByDisplay"
202 lookupType
= "EGLDisplay"
204 assert eglFunc
["method"] == "device"
205 lookupFunc
= "__eglDispatchFetchByDevice"
206 lookupType
= "EGLDeviceEXT"
209 for arg
in func
.args
:
210 if arg
.type == lookupType
:
213 if lookupArg
is None:
214 raise ValueError("Can't find %s argument for function %s" % (lookupType
, func
.name
,))
216 text
+= "{lookupFunc}({lookupArg}, __EGL_DISPATCH_{f.name});\n".format(
217 f
=func
, lookupFunc
=lookupFunc
, lookupArg
=lookupArg
)
219 raise ValueError("Unknown dispatch method: %r" % (eglFunc
["method"],))
221 text
+= " if(_ptr_{f.name} != NULL) {{\n".format(f
=func
)
225 text
+= "_ptr_{f.name}({f.callArgs});\n".format(f
=func
)
229 text
+= " return _ret;\n"
233 def getDefaultReturnValue(typename
):
234 if typename
.endswith("*"):
236 elif typename
== "EGLDisplay":
237 return "EGL_NO_DISPLAY"
238 elif typename
== "EGLContext":
239 return "EGL_NO_CONTEXT"
240 elif typename
== "EGLSurface":
241 return "EGL_NO_SURFACE"
242 elif typename
== "EGLBoolean":
247 if __name__
== "__main__":