blob: f36f9a42470bf632321eff1f3562e71e3980d17b [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
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +080032def generate_get_proc_addr_check(name):
33 return " if (!%s || %s[0] != 'x' || %s[1] != 'g' || %s[2] != 'l')\n" \
34 " return NULL;" % ((name,) * 4)
35
Chia-I Wufb2559d2014-08-01 11:19:52 +080036class Subcommand(object):
37 def __init__(self, argv):
38 self.argv = argv
Chia-I Wuec30dcb2015-01-01 08:46:31 +080039 self.headers = xgl.headers
40 self.protos = xgl.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080041
42 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080043 print(self.generate())
44
45 def generate(self):
46 copyright = self.generate_copyright()
47 header = self.generate_header()
48 body = self.generate_body()
49 footer = self.generate_footer()
50
51 contents = []
52 if copyright:
53 contents.append(copyright)
54 if header:
55 contents.append(header)
56 if body:
57 contents.append(body)
58 if footer:
59 contents.append(footer)
60
61 return "\n\n".join(contents)
62
63 def generate_copyright(self):
64 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
65
66/*
67 * XGL
68 *
69 * Copyright (C) 2014 LunarG, Inc.
70 *
71 * Permission is hereby granted, free of charge, to any person obtaining a
72 * copy of this software and associated documentation files (the "Software"),
73 * to deal in the Software without restriction, including without limitation
74 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
75 * and/or sell copies of the Software, and to permit persons to whom the
76 * Software is furnished to do so, subject to the following conditions:
77 *
78 * The above copyright notice and this permission notice shall be included
79 * in all copies or substantial portions of the Software.
80 *
81 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
82 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
83 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
84 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
85 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
86 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
87 * DEALINGS IN THE SOFTWARE.
88 */"""
89
90 def generate_header(self):
Chia-I Wu6ae460f2014-09-13 13:36:06 +080091 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080092
93 def generate_body(self):
94 pass
95
96 def generate_footer(self):
97 pass
98
Chia-I Wud4a76ae2015-01-04 10:15:48 +080099class LoaderEntrypointsSubcommand(Subcommand):
Chia-I Wu26864352015-01-02 00:21:24 +0800100 def generate_header(self):
101 return "#include \"loader.h\""
102
Chia-I Wu9e7e5542015-01-04 15:51:00 +0800103 def _does_function_create_object(self, proto):
104 out_objs = proto.object_out_params()
105 return out_objs and out_objs[-1] == proto.params[-1]
Chia-I Wu565734c2015-01-04 15:32:52 +0800106
Chia-I Wu565734c2015-01-04 15:32:52 +0800107 def _is_dispatchable(self, proto):
Chia-I Wu31b115a2015-01-04 15:38:47 +0800108 if proto.name in ["GetProcAddr", "EnumerateLayers"]:
109 return False
Chia-I Wu565734c2015-01-04 15:32:52 +0800110
Chia-I Wu31b115a2015-01-04 15:38:47 +0800111 in_objs = proto.object_in_params()
112 return in_objs and in_objs[0] == proto.params[0]
Chia-I Wu565734c2015-01-04 15:32:52 +0800113
Chia-I Wu26864352015-01-02 00:21:24 +0800114 def _generate_loader_dispatch_entrypoints(self, qual=""):
Chia-I Wu468e3c32014-08-04 08:03:57 +0800115 if qual:
116 qual += " "
117
Chia-I Wudac3e492014-08-02 23:49:43 +0800118 funcs = []
119 for proto in self.protos:
Chia-I Wu565734c2015-01-04 15:32:52 +0800120 if not self._is_dispatchable(proto):
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700121 continue
122 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
123 stmt = "(*disp)->%s" % proto.c_call()
Chia-I Wu26864352015-01-02 00:21:24 +0800124 if proto.name == "CreateDevice":
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700125 funcs.append("%s%s\n"
126 "{\n"
Jon Ashburn10053332014-11-17 10:17:37 -0700127 " loader_activate_layers(%s, %s);\n"
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700128 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700129 " const XGL_LAYER_DISPATCH_TABLE **disp =\n"
130 " (const XGL_LAYER_DISPATCH_TABLE **) wrapped_obj->baseObject;\n"
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700131 " %s = wrapped_obj->nextObject;\n"
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700132 " XGL_RESULT res = %s;\n"
Jon Ashburnb7428b32015-01-09 10:13:48 -0700133 " if (res == XGL_SUCCESS)\n"
134 " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n"
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700135 " return res;\n"
136 "}" % (qual, decl, proto.params[0].name, proto.params[1].name,
137 proto.params[0].name, proto.params[0].name, stmt,
138 proto.params[-1].name))
Chia-I Wu9e7e5542015-01-04 15:51:00 +0800139 elif self._does_function_create_object(proto):
Jon Ashburnb7428b32015-01-09 10:13:48 -0700140 if proto.name == "WsiX11CreatePresentableImage":
141 obj_write_stmt = "\n *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;" % ( proto.params[-2].name)
142 else:
143 obj_write_stmt = ""
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700144 funcs.append("%s%s\n"
145 "{\n"
146 " const XGL_LAYER_DISPATCH_TABLE **disp =\n"
147 " (const XGL_LAYER_DISPATCH_TABLE **) %s;\n"
148 " XGL_RESULT res = %s;\n"
Jon Ashburnb7428b32015-01-09 10:13:48 -0700149 " if (res == XGL_SUCCESS) {%s\n"
150 " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n"
151 " }\n"
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700152 " return res;\n"
Jon Ashburnb7428b32015-01-09 10:13:48 -0700153 "}"
154 % (qual, decl, proto.params[0].name, stmt, obj_write_stmt, proto.params[-1].name))
Chia-I Wu26864352015-01-02 00:21:24 +0800155 elif proto.name == "GetMultiGpuCompatibility":
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700156 funcs.append("%s%s\n"
157 "{\n"
158 " XGL_BASE_LAYER_OBJECT* wrapped_obj0 = (XGL_BASE_LAYER_OBJECT*)%s;\n"
159 " XGL_BASE_LAYER_OBJECT* wrapped_obj1 = (XGL_BASE_LAYER_OBJECT*)%s;\n"
160 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
161 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj0->baseObject;\n"
162 " %s = wrapped_obj0->nextObject;\n"
163 " %s = wrapped_obj1->nextObject;\n"
164 " return %s;\n"
165 "}" % (qual, decl, proto.params[0].name, proto.params[1].name,
166 proto.params[0].name, proto.params[1].name, stmt))
Chia-I Wu26864352015-01-02 00:21:24 +0800167 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700168 if proto.ret != "XGL_VOID":
169 stmt = "return " + stmt
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700170 funcs.append("%s%s\n"
171 "{\n"
172 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
173 " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n"
174 " %s;\n"
175 "}" % (qual, decl, proto.params[0].name, stmt))
176 else:
Jon Ashburnd6bfe022014-12-03 14:30:48 -0700177 if proto.ret != "XGL_VOID":
178 stmt = "return " + stmt
Tobin Ehlis66bbbf52014-11-12 11:47:07 -0700179 funcs.append("%s%s\n"
180 "{\n"
181 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
182 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
183 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
184 " %s = wrapped_obj->nextObject;\n"
185 " %s;\n"
186 "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800187
188 return "\n\n".join(funcs)
189
Chia-I Wufb2559d2014-08-01 11:19:52 +0800190 def generate_body(self):
Chia-I Wu26864352015-01-02 00:21:24 +0800191 body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")]
Jon Ashburnd43f9b62014-10-14 19:15:22 -0600192
193 return "\n\n".join(body)
194
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800195class DispatchTableOpsSubcommand(Subcommand):
196 def run(self):
197 if len(self.argv) != 1:
198 print("DispatchTableOpsSubcommand: <prefix> unspecified")
199 return
200
201 self.prefix = self.argv[0]
202 super().run()
203
204 def generate_header(self):
205 return "\n".join(["#include <xgl.h>",
206 "#include <xglLayer.h>",
207 "#include <string.h>"])
208
209 def _generate_init(self):
210 stmts = []
211 for proto in self.protos:
212 if proto.name == "GetProcAddr":
213 stmts.append("table->%s = gpa; /* direct assignment */" %
214 proto.name)
215 else:
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600216 stmts.append("table->%s = (xgl%sType) gpa(gpu, \"xgl%s\");" %
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800217 (proto.name, proto.name, proto.name))
218
219 func = []
220 func.append("static inline void %s_initialize_dispatch_table(XGL_LAYER_DISPATCH_TABLE *table,"
221 % self.prefix)
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600222 func.append("%s xglGetProcAddrType gpa,"
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800223 % (" " * len(self.prefix)))
224 func.append("%s XGL_PHYSICAL_GPU gpu)"
225 % (" " * len(self.prefix)))
226 func.append("{")
227 func.append(" %s" % "\n ".join(stmts))
228 func.append("}")
229
230 return "\n".join(func)
231
232 def _generate_lookup(self):
233 lookups = []
234 for proto in self.protos:
235 lookups.append("if (!strcmp(name, \"%s\"))" % (proto.name))
236 lookups.append(" return (void *) table->%s;"
237 % (proto.name))
238
239 func = []
240 func.append("static inline void *%s_lookup_dispatch_table(const XGL_LAYER_DISPATCH_TABLE *table,"
241 % self.prefix)
242 func.append("%s const char *name)"
243 % (" " * len(self.prefix)))
244 func.append("{")
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +0800245 func.append(generate_get_proc_addr_check("name"))
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800246 func.append("")
247 func.append(" name += 3;")
248 func.append(" %s" % "\n ".join(lookups))
249 func.append("")
250 func.append(" return NULL;")
251 func.append("}")
252
253 return "\n".join(func)
254
255 def generate_body(self):
256 body = [self._generate_init(),
257 self._generate_lookup()]
258
259 return "\n\n".join(body)
260
Chia-I Wu731517d2015-01-03 23:48:15 +0800261class IcdDummyEntrypointsSubcommand(Subcommand):
Chia-I Wu30c78292014-08-04 10:08:08 +0800262 def run(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800263 if len(self.argv) == 1:
264 self.prefix = self.argv[0]
265 self.qual = "static"
266 else:
267 self.prefix = "xgl"
268 self.qual = "ICD_EXPORT"
Chia-I Wu30c78292014-08-04 10:08:08 +0800269
270 super().run()
271
272 def generate_header(self):
273 return "#include \"icd.h\""
274
275 def _generate_stub_decl(self, proto):
Chia-I Wu77c5cdf2015-01-04 12:05:41 +0800276 return proto.c_pretty_decl(self.prefix + proto.name, attr="XGLAPI")
Chia-I Wu30c78292014-08-04 10:08:08 +0800277
278 def _generate_stubs(self):
279 stubs = []
280 for proto in self.protos:
Chia-I Wu30c78292014-08-04 10:08:08 +0800281 decl = self._generate_stub_decl(proto)
282 if proto.ret != "XGL_VOID":
Chia-I Wu731517d2015-01-03 23:48:15 +0800283 stmt = " return XGL_ERROR_UNKNOWN;\n"
Chia-I Wu30c78292014-08-04 10:08:08 +0800284 else:
285 stmt = ""
286
Chia-I Wu731517d2015-01-03 23:48:15 +0800287 stubs.append("%s %s\n{\n%s}" % (self.qual, decl, stmt))
Chia-I Wu30c78292014-08-04 10:08:08 +0800288
289 return "\n\n".join(stubs)
290
Chia-I Wu30c78292014-08-04 10:08:08 +0800291 def generate_body(self):
Chia-I Wu731517d2015-01-03 23:48:15 +0800292 return self._generate_stubs()
Chia-I Wu30c78292014-08-04 10:08:08 +0800293
Chia-I Wu58f20852015-01-04 00:11:17 +0800294class IcdGetProcAddrSubcommand(IcdDummyEntrypointsSubcommand):
295 def generate_header(self):
296 return "\n".join(["#include <string.h>", "#include \"icd.h\""])
297
298 def generate_body(self):
299 for proto in self.protos:
300 if proto.name == "GetProcAddr":
301 gpa_proto = proto
302
303 gpa_decl = self._generate_stub_decl(gpa_proto)
304 gpa_pname = gpa_proto.params[-1].name
305
306 lookups = []
307 for proto in self.protos:
308 lookups.append("if (!strcmp(%s, \"%s\"))" %
309 (gpa_pname, proto.name))
310 lookups.append(" return (%s) %s%s;" %
311 (gpa_proto.ret, self.prefix, proto.name))
312
313 body = []
314 body.append("%s %s" % (self.qual, gpa_decl))
315 body.append("{")
Chia-I Wuf7d6d3c2015-01-05 12:55:13 +0800316 body.append(generate_get_proc_addr_check(gpa_pname))
Chia-I Wu58f20852015-01-04 00:11:17 +0800317 body.append("")
318 body.append(" %s += 3;" % gpa_pname)
319 body.append(" %s" % "\n ".join(lookups))
320 body.append("")
321 body.append(" return NULL;")
322 body.append("}")
323
324 return "\n".join(body)
325
Chia-I Wuf4bd2e42015-01-05 12:56:13 +0800326class LayerInterceptProcSubcommand(Subcommand):
327 def run(self):
328 self.prefix = "xgl"
329
330 # we could get the list from argv if wanted
331 self.intercepted = [proto.name for proto in self.protos
332 if proto.name not in ["InitAndEnumerateGpus"]]
333
334 for proto in self.protos:
335 if proto.name == "GetProcAddr":
336 self.gpa = proto
337
338 super().run()
339
340 def generate_header(self):
341 return "\n".join(["#include <string.h>", "#include \"xglLayer.h\""])
342
343 def generate_body(self):
344 lookups = []
345 for proto in self.protos:
346 if proto.name not in self.intercepted:
347 lookups.append("/* no %s%s */" % (self.prefix, proto.name))
348 continue
349
350 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
351 lookups.append(" return (%s) %s%s;" %
352 (self.gpa.ret, self.prefix, proto.name))
353
354 body = []
355 body.append("static inline %s layer_intercept_proc(const char *name)" %
356 self.gpa.ret)
357 body.append("{")
358 body.append(generate_get_proc_addr_check("name"))
359 body.append("")
360 body.append(" name += 3;")
361 body.append(" %s" % "\n ".join(lookups))
362 body.append("")
363 body.append(" return NULL;")
364 body.append("}")
365
366 return "\n".join(body)
367
Chia-I Wufb2559d2014-08-01 11:19:52 +0800368def main():
369 subcommands = {
Chia-I Wud4a76ae2015-01-04 10:15:48 +0800370 "loader-entrypoints": LoaderEntrypointsSubcommand,
Chia-I Wu28b8d8c2015-01-04 10:19:50 +0800371 "dispatch-table-ops": DispatchTableOpsSubcommand,
Chia-I Wu731517d2015-01-03 23:48:15 +0800372 "icd-dummy-entrypoints": IcdDummyEntrypointsSubcommand,
Chia-I Wu58f20852015-01-04 00:11:17 +0800373 "icd-get-proc-addr": IcdGetProcAddrSubcommand,
Chia-I Wuf4bd2e42015-01-05 12:56:13 +0800374 "layer-intercept-proc": LayerInterceptProcSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800375 }
376
377 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
378 print("Usage: %s <subcommand> [options]" % sys.argv[0])
379 print
380 print("Available sucommands are: %s" % " ".join(subcommands))
381 exit(1)
382
383 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
384 subcmd.run()
385
386if __name__ == "__main__":
387 main()