blob: ed5b636b9da0b1f916ef7378218974b5e5ec511c [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu44e42362014-09-02 08:32:09 +08002#
3# XGL
4#
5# Copyright (C) 2014 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 <olv@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080027
28import sys
29
30import xgl
31
32class Subcommand(object):
33 def __init__(self, argv):
34 self.argv = argv
35 self.protos = ()
Chia-I Wu6ae460f2014-09-13 13:36:06 +080036 self.headers = ()
Chia-I Wufb2559d2014-08-01 11:19:52 +080037
38 def run(self):
Chia-I Wub8dceae2014-09-23 10:37:23 +080039 self.protos = xgl.core + xgl.ext_wsi_x11
40 self.headers = xgl.core_headers + xgl.ext_wsi_x11_headers
Chia-I Wufb2559d2014-08-01 11:19:52 +080041 print(self.generate())
42
43 def generate(self):
44 copyright = self.generate_copyright()
45 header = self.generate_header()
46 body = self.generate_body()
47 footer = self.generate_footer()
48
49 contents = []
50 if copyright:
51 contents.append(copyright)
52 if header:
53 contents.append(header)
54 if body:
55 contents.append(body)
56 if footer:
57 contents.append(footer)
58
59 return "\n\n".join(contents)
60
61 def generate_copyright(self):
62 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
63
64/*
65 * XGL
66 *
67 * Copyright (C) 2014 LunarG, Inc.
68 *
69 * Permission is hereby granted, free of charge, to any person obtaining a
70 * copy of this software and associated documentation files (the "Software"),
71 * to deal in the Software without restriction, including without limitation
72 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
73 * and/or sell copies of the Software, and to permit persons to whom the
74 * Software is furnished to do so, subject to the following conditions:
75 *
76 * The above copyright notice and this permission notice shall be included
77 * in all copies or substantial portions of the Software.
78 *
79 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
81 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
82 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
83 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
84 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
85 * DEALINGS IN THE SOFTWARE.
86 */"""
87
88 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080089 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080090
91 def generate_body(self):
92 pass
93
94 def generate_footer(self):
95 pass
96
Chia-I Wufb2559d2014-08-01 11:19:52 +080097 def _generate_icd_dispatch_table(self):
98 proto_map = {}
99 for proto in self.protos:
100 proto_map[proto.name] = proto
101
102 entries = []
103 for name in xgl.icd_dispatch_table:
104 proto = proto_map[name]
105 entries.append(proto.c_typedef(attr="XGLAPI"))
106
Jon Ashburn70f97242014-12-03 11:13:40 -0700107 return """XGL_LAYER_DISPATCH_TABLE {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800108 %s;
109};""" % ";\n ".join(entries)
110
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700111 def _generate_dispatch_entrypoints(self, qual="", unwrap=False):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800112 if qual:
113 qual += " "
114
Chia-I Wudac3e492014-08-02 23:49:43 +0800115 funcs = []
116 for proto in self.protos:
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700117 if not xgl.is_dispatchable(proto):
118 continue
119 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
120 stmt = "(*disp)->%s" % proto.c_call()
121 if proto.ret != "XGL_VOID":
122 stmt = "return " + stmt
123 if proto.name == "CreateDevice" and qual == "LOADER_EXPORT ":
124 funcs.append("%s%s\n"
125 "{\n"
Jon Ashburn10053332014-11-17 10:17:37 -0700126 " loader_activate_layers(%s, %s);\n"
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700127 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
128 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
129 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
130 " %s = wrapped_obj->nextObject;\n"
131 " %s;\n"
132 "}" % (qual, decl, proto.params[0].name, proto.params[1].name, proto.params[0].name, proto.params[0].name, stmt))
Jon Ashburn4cd79c92014-12-03 11:33:51 -0700133 elif proto.params[0].ty != "XGL_PHYSICAL_GPU" or qual != "LOADER_EXPORT ":
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700134 funcs.append("%s%s\n"
135 "{\n"
136 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
137 " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n"
138 " %s;\n"
139 "}" % (qual, decl, proto.params[0].name, stmt))
140 else:
141 funcs.append("%s%s\n"
142 "{\n"
143 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
144 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
145 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
146 " %s = wrapped_obj->nextObject;\n"
147 " %s;\n"
148 "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800149
150 return "\n\n".join(funcs)
151
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600152 def _generate_layer_gpa_function(self, prefix="xgl"):
153 func_body = []
154 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
155 "{\n"
156 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
157 " if (gpu == NULL)\n"
158 " return NULL;\n"
159 " pCurObj = gpuw;\n"
160 " pthread_once(&tabOnce, initLayerTable);\n\n"
161 ' if (!strncmp("xglGetProcAddr", (const char *) funcName, sizeof("xglGetProcAddr")))\n'
162 ' return xglGetProcAddr;')
163 for name in xgl.icd_dispatch_table:
164 if name == "GetProcAddr":
165 continue
Jon Ashburn5eee57a2014-10-15 12:08:33 -0600166 if name == "InitAndEnumerateGpus":
167 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
168 ' return nextTable.%s;' % (prefix, name, prefix, name, name))
169 else:
170 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600171 ' return %s%s;' % (prefix, name, prefix, name, prefix, name))
Jon Ashburn5eee57a2014-10-15 12:08:33 -0600172
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600173 func_body.append(" else {\n"
174 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
175 " if (gpuw->pGPA == NULL)\n"
176 " return NULL;\n"
177 " return gpuw->pGPA(gpuw->nextObject, funcName);\n"
178 " }\n"
179 "}\n")
180 return "\n".join(func_body)
181
182 def _generate_layer_dispatch_table(self, prefix='xgl'):
183 func_body = []
184 func_body.append('static void initLayerTable()\n'
185 '{\n'
186 ' GetProcAddrType fpNextGPA;\n'
187 ' fpNextGPA = pCurObj->pGPA;\n'
188 ' assert(fpNextGPA);\n');
189
190 for name in xgl.icd_dispatch_table:
191 func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n'
192 ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name))
193
194 func_body.append("}\n")
195 return "\n".join(func_body)
196
Chia-I Wu1e122262014-08-01 11:58:32 +0800197class LoaderSubcommand(Subcommand):
Chia-I Wu1e122262014-08-01 11:58:32 +0800198 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800199 return "#include \"loader.h\""
Chia-I Wu1e122262014-08-01 11:58:32 +0800200
Chia-I Wufb2559d2014-08-01 11:19:52 +0800201 def generate_body(self):
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600202 body = [self._generate_dispatch_entrypoints("LOADER_EXPORT")]
203
204 return "\n\n".join(body)
205
206class LayerFuncsSubcommand(Subcommand):
207 def generate_header(self):
208 return '#include <xglLayer.h>\n#include "loader.h"'
209
210 def generate_body(self):
211 return self._generate_dispatch_entrypoints("static", True)
212
213class LayerDispatchSubcommand(Subcommand):
214 def generate_header(self):
215 return '#include "layer_wrappers.h"'
216
217 def generate_body(self):
218 return self._generate_layer_dispatch_table()
219
Chia-I Wu84dace92014-08-03 09:55:18 +0800220class IcdDispatchTableSubcommand(Subcommand):
Chia-I Wu84dace92014-08-03 09:55:18 +0800221 def generate_body(self):
222 return self._generate_icd_dispatch_table()
223
224class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu84dace92014-08-03 09:55:18 +0800225 def generate_header(self):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800226 return "#include \"icd.h\""
Chia-I Wu84dace92014-08-03 09:55:18 +0800227
228 def generate_body(self):
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600229 return self._generate_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu84dace92014-08-03 09:55:18 +0800230
Chia-I Wu30c78292014-08-04 10:08:08 +0800231class IcdDispatchDummyImplSubcommand(Subcommand):
232 def run(self):
233 if len(self.argv) != 1:
234 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
235 return
236
237 self.prefix = self.argv[0]
238
239 super().run()
240
241 def generate_header(self):
242 return "#include \"icd.h\""
243
244 def _generate_stub_decl(self, proto):
245 plist = []
246 for param in proto.params:
247 idx = param.ty.find("[")
248 if idx < 0:
249 idx = len(param.ty)
250
251 pad = 44 - idx
252 if pad <= 0:
253 pad = 1
254
255 plist.append(" %s%s%s%s" % (param.ty[:idx],
256 " " * pad, param.name, param.ty[idx:]))
257
258 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
259 proto.name, ",\n".join(plist))
260
261 def _generate_stubs(self):
262 stubs = []
263 for proto in self.protos:
264 if not xgl.is_dispatchable(proto):
265 continue
266
267 decl = self._generate_stub_decl(proto)
268 if proto.ret != "XGL_VOID":
269 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
270 else:
271 stmt = ""
272
273 stubs.append("static %s\n{\n%s}" % (decl, stmt))
274
275 return "\n\n".join(stubs)
276
277
278 def _generate_tables(self):
279 initializer = []
280 for proto in self.protos:
281 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
282 initializer.append(".%s = %s%s" %
283 (proto.name, prefix, proto.name))
284
Jon Ashburn70f97242014-12-03 11:13:40 -0700285 return """const XGL_LAYER_DISPATCH_TABLE %s_normal_dispatch_table = {
Chia-I Wu30c78292014-08-04 10:08:08 +0800286 %s,
287};
288
Jon Ashburn70f97242014-12-03 11:13:40 -0700289const XGL_LAYER_DISPATCH_TABLE %s_debug_dispatch_table = {
Chia-I Wu30c78292014-08-04 10:08:08 +0800290 %s,
291};""" % (self.prefix, ",\n ".join(initializer),
292 self.prefix, ",\n ".join(initializer))
293
294 def generate_body(self):
295 body = [self._generate_stubs(),
296 self._generate_tables()]
297
298 return "\n\n".join(body)
299
Chia-I Wufb2559d2014-08-01 11:19:52 +0800300def main():
301 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800302 "loader": LoaderSubcommand,
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600303 "layer-funcs" : LayerFuncsSubcommand,
304 "layer-dispatch" : LayerDispatchSubcommand,
Chia-I Wu84dace92014-08-03 09:55:18 +0800305 "icd-dispatch-table": IcdDispatchTableSubcommand,
306 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wu30c78292014-08-04 10:08:08 +0800307 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800308 }
309
310 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
311 print("Usage: %s <subcommand> [options]" % sys.argv[0])
312 print
313 print("Available sucommands are: %s" % " ".join(subcommands))
314 exit(1)
315
316 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
317 subcmd.run()
318
319if __name__ == "__main__":
320 main()