Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Chia-I Wu | 44e4236 | 2014-09-02 08:32:09 +0800 | [diff] [blame] | 2 | # |
| 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 Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 27 | |
| 28 | import sys |
| 29 | |
| 30 | import xgl |
| 31 | |
| 32 | class Subcommand(object): |
| 33 | def __init__(self, argv): |
| 34 | self.argv = argv |
| 35 | self.protos = () |
Chia-I Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame] | 36 | self.headers = () |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 37 | |
| 38 | def run(self): |
Chia-I Wu | b8dceae | 2014-09-23 10:37:23 +0800 | [diff] [blame] | 39 | self.protos = xgl.core + xgl.ext_wsi_x11 |
| 40 | self.headers = xgl.core_headers + xgl.ext_wsi_x11_headers |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 41 | 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 Wu | 6ae460f | 2014-09-13 13:36:06 +0800 | [diff] [blame] | 89 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 90 | |
| 91 | def generate_body(self): |
| 92 | pass |
| 93 | |
| 94 | def generate_footer(self): |
| 95 | pass |
| 96 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 97 | 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 Ashburn | 70f9724 | 2014-12-03 11:13:40 -0700 | [diff] [blame] | 107 | return """XGL_LAYER_DISPATCH_TABLE { |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 108 | %s; |
| 109 | };""" % ";\n ".join(entries) |
| 110 | |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 111 | def _generate_dispatch_entrypoints(self, qual="", unwrap=False): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 112 | if qual: |
| 113 | qual += " " |
| 114 | |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 115 | funcs = [] |
| 116 | for proto in self.protos: |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 117 | if not xgl.is_dispatchable(proto): |
| 118 | continue |
| 119 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 120 | stmt = "(*disp)->%s" % proto.c_call() |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 121 | if proto.name == "CreateDevice" and qual == "LOADER_EXPORT ": |
| 122 | funcs.append("%s%s\n" |
| 123 | "{\n" |
Jon Ashburn | 1005333 | 2014-11-17 10:17:37 -0700 | [diff] [blame] | 124 | " loader_activate_layers(%s, %s);\n" |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 125 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
Jon Ashburn | d6bfe02 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 126 | " const XGL_LAYER_DISPATCH_TABLE **disp =\n" |
| 127 | " (const XGL_LAYER_DISPATCH_TABLE **) wrapped_obj->baseObject;\n" |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 128 | " %s = wrapped_obj->nextObject;\n" |
Jon Ashburn | d6bfe02 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 129 | " XGL_RESULT res = %s;\n" |
| 130 | " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n" |
| 131 | " return res;\n" |
| 132 | "}" % (qual, decl, proto.params[0].name, proto.params[1].name, |
| 133 | proto.params[0].name, proto.params[0].name, stmt, |
| 134 | proto.params[-1].name)) |
| 135 | elif xgl.does_function_create_object(proto.name) and qual == "LOADER_EXPORT ": |
| 136 | funcs.append("%s%s\n" |
| 137 | "{\n" |
| 138 | " const XGL_LAYER_DISPATCH_TABLE **disp =\n" |
| 139 | " (const XGL_LAYER_DISPATCH_TABLE **) %s;\n" |
| 140 | " XGL_RESULT res = %s;\n" |
| 141 | " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n" |
| 142 | " return res;\n" |
| 143 | "}" % (qual, decl, proto.params[0].name, stmt, proto.params[-1].name)) |
| 144 | elif proto.name == "GetMultiGpuCompatibility" and qual == "LOADER_EXPORT ": |
| 145 | funcs.append("%s%s\n" |
| 146 | "{\n" |
| 147 | " XGL_BASE_LAYER_OBJECT* wrapped_obj0 = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 148 | " XGL_BASE_LAYER_OBJECT* wrapped_obj1 = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 149 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 150 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj0->baseObject;\n" |
| 151 | " %s = wrapped_obj0->nextObject;\n" |
| 152 | " %s = wrapped_obj1->nextObject;\n" |
| 153 | " return %s;\n" |
| 154 | "}" % (qual, decl, proto.params[0].name, proto.params[1].name, |
| 155 | proto.params[0].name, proto.params[1].name, stmt)) |
Jon Ashburn | 4cd79c9 | 2014-12-03 11:33:51 -0700 | [diff] [blame] | 156 | elif proto.params[0].ty != "XGL_PHYSICAL_GPU" or qual != "LOADER_EXPORT ": |
Jon Ashburn | d6bfe02 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 157 | if proto.ret != "XGL_VOID": |
| 158 | stmt = "return " + stmt |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 159 | funcs.append("%s%s\n" |
| 160 | "{\n" |
| 161 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 162 | " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n" |
| 163 | " %s;\n" |
| 164 | "}" % (qual, decl, proto.params[0].name, stmt)) |
| 165 | else: |
Jon Ashburn | d6bfe02 | 2014-12-03 14:30:48 -0700 | [diff] [blame] | 166 | if proto.ret != "XGL_VOID": |
| 167 | stmt = "return " + stmt |
Tobin Ehlis | 66bbbf5 | 2014-11-12 11:47:07 -0700 | [diff] [blame] | 168 | funcs.append("%s%s\n" |
| 169 | "{\n" |
| 170 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 171 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 172 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n" |
| 173 | " %s = wrapped_obj->nextObject;\n" |
| 174 | " %s;\n" |
| 175 | "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 176 | |
| 177 | return "\n\n".join(funcs) |
| 178 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 179 | 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 Ashburn | 5eee57a | 2014-10-15 12:08:33 -0600 | [diff] [blame] | 193 | 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 Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 198 | ' return %s%s;' % (prefix, name, prefix, name, prefix, name)) |
Jon Ashburn | 5eee57a | 2014-10-15 12:08:33 -0600 | [diff] [blame] | 199 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 200 | 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 Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 224 | class LoaderSubcommand(Subcommand): |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 225 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 226 | return "#include \"loader.h\"" |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 227 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 228 | def generate_body(self): |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 229 | body = [self._generate_dispatch_entrypoints("LOADER_EXPORT")] |
| 230 | |
| 231 | return "\n\n".join(body) |
| 232 | |
| 233 | class 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 | |
| 240 | class 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 | |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 247 | class IcdDispatchTableSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 248 | def generate_body(self): |
| 249 | return self._generate_icd_dispatch_table() |
| 250 | |
| 251 | class IcdDispatchEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 252 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 253 | return "#include \"icd.h\"" |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 254 | |
| 255 | def generate_body(self): |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 256 | return self._generate_dispatch_entrypoints("ICD_EXPORT") |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 257 | |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 258 | class IcdDispatchDummyImplSubcommand(Subcommand): |
| 259 | def run(self): |
| 260 | if len(self.argv) != 1: |
| 261 | print("IcdDispatchDummyImplSubcommand: <prefix> unspecified") |
| 262 | return |
| 263 | |
| 264 | self.prefix = self.argv[0] |
| 265 | |
| 266 | super().run() |
| 267 | |
| 268 | def generate_header(self): |
| 269 | return "#include \"icd.h\"" |
| 270 | |
| 271 | def _generate_stub_decl(self, proto): |
| 272 | plist = [] |
| 273 | for param in proto.params: |
| 274 | idx = param.ty.find("[") |
| 275 | if idx < 0: |
| 276 | idx = len(param.ty) |
| 277 | |
| 278 | pad = 44 - idx |
| 279 | if pad <= 0: |
| 280 | pad = 1 |
| 281 | |
| 282 | plist.append(" %s%s%s%s" % (param.ty[:idx], |
| 283 | " " * pad, param.name, param.ty[idx:])) |
| 284 | |
| 285 | return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix, |
| 286 | proto.name, ",\n".join(plist)) |
| 287 | |
| 288 | def _generate_stubs(self): |
| 289 | stubs = [] |
| 290 | for proto in self.protos: |
| 291 | if not xgl.is_dispatchable(proto): |
| 292 | continue |
| 293 | |
| 294 | decl = self._generate_stub_decl(proto) |
| 295 | if proto.ret != "XGL_VOID": |
| 296 | stmt = " return XGL_ERROR_UNAVAILABLE;\n" |
| 297 | else: |
| 298 | stmt = "" |
| 299 | |
| 300 | stubs.append("static %s\n{\n%s}" % (decl, stmt)) |
| 301 | |
| 302 | return "\n\n".join(stubs) |
| 303 | |
| 304 | |
| 305 | def _generate_tables(self): |
| 306 | initializer = [] |
| 307 | for proto in self.protos: |
| 308 | prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl" |
| 309 | initializer.append(".%s = %s%s" % |
| 310 | (proto.name, prefix, proto.name)) |
| 311 | |
Jon Ashburn | 70f9724 | 2014-12-03 11:13:40 -0700 | [diff] [blame] | 312 | return """const XGL_LAYER_DISPATCH_TABLE %s_normal_dispatch_table = { |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 313 | %s, |
| 314 | }; |
| 315 | |
Jon Ashburn | 70f9724 | 2014-12-03 11:13:40 -0700 | [diff] [blame] | 316 | const XGL_LAYER_DISPATCH_TABLE %s_debug_dispatch_table = { |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 317 | %s, |
| 318 | };""" % (self.prefix, ",\n ".join(initializer), |
| 319 | self.prefix, ",\n ".join(initializer)) |
| 320 | |
| 321 | def generate_body(self): |
| 322 | body = [self._generate_stubs(), |
| 323 | self._generate_tables()] |
| 324 | |
| 325 | return "\n\n".join(body) |
| 326 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 327 | def main(): |
| 328 | subcommands = { |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 329 | "loader": LoaderSubcommand, |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 330 | "layer-funcs" : LayerFuncsSubcommand, |
| 331 | "layer-dispatch" : LayerDispatchSubcommand, |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 332 | "icd-dispatch-table": IcdDispatchTableSubcommand, |
| 333 | "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand, |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 334 | "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 338 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 339 | print |
| 340 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 341 | exit(1) |
| 342 | |
| 343 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 344 | subcmd.run() |
| 345 | |
| 346 | if __name__ == "__main__": |
| 347 | main() |