blob: e8b35a3389f389752616b4778e19ae0719a0bd38 [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu701f3f62014-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 Wu6bdf0192014-09-13 13:36:06 +080036 self.headers = ()
Chia-I Wufb2559d2014-08-01 11:19:52 +080037
38 def run(self):
Chia-I Wu6dee8b82014-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 Wu6bdf0192014-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
107 return """struct icd_dispatch_table {
108 %s;
109};""" % ";\n ".join(entries)
110
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600111 def _generate_dispatch_entrypoints(self, qual="", unwrap=False, layer=False):
Chia-I Wu19300602014-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:
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600117 if not layer:
118 if not xgl.is_dispatchable(proto):
119 continue
120 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
121 stmt = "(*disp)->%s" % proto.c_call()
122 if proto.ret != "XGL_VOID":
123 stmt = "return " + stmt
124 if proto.name == "CreateDevice" and qual == "LOADER_EXPORT ":
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600125 funcs.append("%s%s\n"
126 "{\n"
Jon Ashburnf2610012014-10-24 15:48:55 -0600127 " ActivateLayers(%s, %s);\n"
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600128 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
129 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
130 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
131 " %s = wrapped_obj->nextObject;\n"
132 " %s;\n"
Jon Ashburn6b4d70c2014-10-22 18:13:16 -0600133 "}" % (qual, decl, proto.params[0].name, proto.params[1].name, proto.params[0].name, proto.params[0].name, stmt))
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600134 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
135 funcs.append("%s%s\n"
136 "{\n"
137 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
138 " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n"
139 " %s;\n"
140 "}" % (qual, decl, proto.params[0].name, stmt))
141 else:
142 funcs.append("%s%s\n"
143 "{\n"
144 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
145 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
146 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
147 " %s = wrapped_obj->nextObject;\n"
148 " %s;\n"
149 "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt))
Jon Ashburn57f61dd2014-10-15 12:08:33 -0600150 elif proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600151 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
152 param0_name = proto.params[0].name
153 ret_val = ''
154 stmt = ''
155 if proto.ret != "XGL_VOID":
156 ret_val = "XGL_RESULT result = "
157 stmt = " return result;\n"
158 if proto.params[0].ty != "XGL_PHYSICAL_GPU":
159 funcs.append('%s%s\n'
160 '{\n'
161 ' %snextTable.%s;\n'
162 '%s'
163 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
164 else:
165 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
166 funcs.append('%s%s\n'
167 '{\n'
168 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
169 ' printf("At start of layered %s\\n");\n'
170 ' pCurObj = gpuw;\n'
171 ' pthread_once(&tabOnce, initLayerTable);\n'
172 ' %snextTable.%s;\n'
173 ' printf("Completed layered %s\\n");\n'
174 '%s'
175 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800176
177 return "\n\n".join(funcs)
178
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600179 def _generate_layer_gpa_function(self, prefix="xgl"):
180 func_body = []
181 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
182 "{\n"
183 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
184 " if (gpu == NULL)\n"
185 " return NULL;\n"
186 " pCurObj = gpuw;\n"
187 " pthread_once(&tabOnce, initLayerTable);\n\n"
188 ' if (!strncmp("xglGetProcAddr", (const char *) funcName, sizeof("xglGetProcAddr")))\n'
189 ' return xglGetProcAddr;')
190 for name in xgl.icd_dispatch_table:
191 if name == "GetProcAddr":
192 continue
Jon Ashburn57f61dd2014-10-15 12:08:33 -0600193 if name == "InitAndEnumerateGpus":
194 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
195 ' return nextTable.%s;' % (prefix, name, prefix, name, name))
196 else:
197 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600198 ' return %s%s;' % (prefix, name, prefix, name, prefix, name))
Jon Ashburn57f61dd2014-10-15 12:08:33 -0600199
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600200 func_body.append(" else {\n"
201 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
202 " if (gpuw->pGPA == NULL)\n"
203 " return NULL;\n"
204 " return gpuw->pGPA(gpuw->nextObject, funcName);\n"
205 " }\n"
206 "}\n")
207 return "\n".join(func_body)
208
209 def _generate_layer_dispatch_table(self, prefix='xgl'):
210 func_body = []
211 func_body.append('static void initLayerTable()\n'
212 '{\n'
213 ' GetProcAddrType fpNextGPA;\n'
214 ' fpNextGPA = pCurObj->pGPA;\n'
215 ' assert(fpNextGPA);\n');
216
217 for name in xgl.icd_dispatch_table:
218 func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n'
219 ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name))
220
221 func_body.append("}\n")
222 return "\n".join(func_body)
223
Chia-I Wu1e122262014-08-01 11:58:32 +0800224class LoaderSubcommand(Subcommand):
Chia-I Wu1e122262014-08-01 11:58:32 +0800225 def generate_header(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800226 return "#include \"loader.h\""
Chia-I Wu1e122262014-08-01 11:58:32 +0800227
Chia-I Wufb2559d2014-08-01 11:19:52 +0800228 def generate_body(self):
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600229 body = [self._generate_dispatch_entrypoints("LOADER_EXPORT")]
230
231 return "\n\n".join(body)
232
233class LayerFuncsSubcommand(Subcommand):
234 def generate_header(self):
235 return '#include <xglLayer.h>\n#include "loader.h"'
236
237 def generate_body(self):
238 return self._generate_dispatch_entrypoints("static", True)
239
240class LayerDispatchSubcommand(Subcommand):
241 def generate_header(self):
242 return '#include "layer_wrappers.h"'
243
244 def generate_body(self):
245 return self._generate_layer_dispatch_table()
246
247class GenericLayerSubcommand(Subcommand):
248 def generate_header(self):
249 return '#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include "xglLayer.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\n'
250
251 def generate_body(self):
252 body = [self._generate_layer_dispatch_table(),
253 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", True, True),
254 self._generate_layer_gpa_function()]
Chia-I Wufb2559d2014-08-01 11:19:52 +0800255
256 return "\n\n".join(body)
257
Chia-I Wu03537f72014-08-03 09:55:18 +0800258class IcdDispatchTableSubcommand(Subcommand):
Chia-I Wu03537f72014-08-03 09:55:18 +0800259 def generate_body(self):
260 return self._generate_icd_dispatch_table()
261
262class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu03537f72014-08-03 09:55:18 +0800263 def generate_header(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800264 return "#include \"icd.h\""
Chia-I Wu03537f72014-08-03 09:55:18 +0800265
266 def generate_body(self):
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600267 return self._generate_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu03537f72014-08-03 09:55:18 +0800268
Chia-I Wudf3c4322014-08-04 10:08:08 +0800269class IcdDispatchDummyImplSubcommand(Subcommand):
270 def run(self):
271 if len(self.argv) != 1:
272 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
273 return
274
275 self.prefix = self.argv[0]
276
277 super().run()
278
279 def generate_header(self):
280 return "#include \"icd.h\""
281
282 def _generate_stub_decl(self, proto):
283 plist = []
284 for param in proto.params:
285 idx = param.ty.find("[")
286 if idx < 0:
287 idx = len(param.ty)
288
289 pad = 44 - idx
290 if pad <= 0:
291 pad = 1
292
293 plist.append(" %s%s%s%s" % (param.ty[:idx],
294 " " * pad, param.name, param.ty[idx:]))
295
296 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
297 proto.name, ",\n".join(plist))
298
299 def _generate_stubs(self):
300 stubs = []
301 for proto in self.protos:
302 if not xgl.is_dispatchable(proto):
303 continue
304
305 decl = self._generate_stub_decl(proto)
306 if proto.ret != "XGL_VOID":
307 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
308 else:
309 stmt = ""
310
311 stubs.append("static %s\n{\n%s}" % (decl, stmt))
312
313 return "\n\n".join(stubs)
314
315
316 def _generate_tables(self):
317 initializer = []
318 for proto in self.protos:
319 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
320 initializer.append(".%s = %s%s" %
321 (proto.name, prefix, proto.name))
322
323 return """const struct icd_dispatch_table %s_normal_dispatch_table = {
324 %s,
325};
326
327const struct icd_dispatch_table %s_debug_dispatch_table = {
328 %s,
329};""" % (self.prefix, ",\n ".join(initializer),
330 self.prefix, ",\n ".join(initializer))
331
332 def generate_body(self):
333 body = [self._generate_stubs(),
334 self._generate_tables()]
335
336 return "\n\n".join(body)
337
Chia-I Wufb2559d2014-08-01 11:19:52 +0800338def main():
339 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800340 "loader": LoaderSubcommand,
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600341 "layer-funcs" : LayerFuncsSubcommand,
342 "layer-dispatch" : LayerDispatchSubcommand,
343 "generic-layer" : GenericLayerSubcommand,
Chia-I Wu03537f72014-08-03 09:55:18 +0800344 "icd-dispatch-table": IcdDispatchTableSubcommand,
345 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wudf3c4322014-08-04 10:08:08 +0800346 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800347 }
348
349 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
350 print("Usage: %s <subcommand> [options]" % sys.argv[0])
351 print
352 print("Available sucommands are: %s" % " ".join(subcommands))
353 exit(1)
354
355 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
356 subcmd.run()
357
358if __name__ == "__main__":
359 main()