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 | |
| 107 | return """struct icd_dispatch_table { |
| 108 | %s; |
| 109 | };""" % ";\n ".join(entries) |
| 110 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 111 | def _generate_dispatch_entrypoints(self, qual="", unwrap=False, layer=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: |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 117 | 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 ": |
| 125 | stmt_cd = "XGL_RESULT res = " + "(*disp)->%s" % proto.c_call() |
| 126 | funcs.append("%s%s\n" |
| 127 | "{\n" |
| 128 | " ActivateLayers(&%s);\n" |
| 129 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 130 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 131 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n" |
| 132 | " %s = wrapped_obj->nextObject;\n" |
| 133 | " %s;\n" |
| 134 | " const XGL_LAYER_DISPATCH_TABLE * *disp_dev = (const XGL_LAYER_DISPATCH_TABLE * *) *%s;\n" |
| 135 | " *disp_dev = (const XGL_LAYER_DISPATCH_TABLE *) *disp;\n" |
| 136 | " return res;\n" |
| 137 | "}" % (qual, decl, proto.params[0].name, proto.params[0].name, proto.params[0].name, stmt_cd, proto.params[2].name)) |
| 138 | elif proto.params[0].ty != "XGL_PHYSICAL_GPU": |
| 139 | funcs.append("%s%s\n" |
| 140 | "{\n" |
| 141 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 142 | " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n" |
| 143 | " %s;\n" |
| 144 | "}" % (qual, decl, proto.params[0].name, stmt)) |
| 145 | else: |
| 146 | funcs.append("%s%s\n" |
| 147 | "{\n" |
| 148 | " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n" |
| 149 | " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n" |
| 150 | " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n" |
| 151 | " %s = wrapped_obj->nextObject;\n" |
| 152 | " %s;\n" |
| 153 | "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt)) |
Jon Ashburn | 5eee57a | 2014-10-15 12:08:33 -0600 | [diff] [blame] | 154 | elif proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus": |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 155 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 156 | param0_name = proto.params[0].name |
| 157 | ret_val = '' |
| 158 | stmt = '' |
| 159 | if proto.ret != "XGL_VOID": |
| 160 | ret_val = "XGL_RESULT result = " |
| 161 | stmt = " return result;\n" |
| 162 | if proto.params[0].ty != "XGL_PHYSICAL_GPU": |
| 163 | funcs.append('%s%s\n' |
| 164 | '{\n' |
| 165 | ' %snextTable.%s;\n' |
| 166 | '%s' |
| 167 | '}' % (qual, decl, ret_val, proto.c_call(), stmt)) |
| 168 | else: |
| 169 | c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1) |
| 170 | funcs.append('%s%s\n' |
| 171 | '{\n' |
| 172 | ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n' |
| 173 | ' printf("At start of layered %s\\n");\n' |
| 174 | ' pCurObj = gpuw;\n' |
| 175 | ' pthread_once(&tabOnce, initLayerTable);\n' |
| 176 | ' %snextTable.%s;\n' |
| 177 | ' printf("Completed layered %s\\n");\n' |
| 178 | '%s' |
| 179 | '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt)) |
Chia-I Wu | dac3e49 | 2014-08-02 23:49:43 +0800 | [diff] [blame] | 180 | |
| 181 | return "\n\n".join(funcs) |
| 182 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 183 | def _generate_layer_gpa_function(self, prefix="xgl"): |
| 184 | func_body = [] |
| 185 | func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n" |
| 186 | "{\n" |
| 187 | " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n" |
| 188 | " if (gpu == NULL)\n" |
| 189 | " return NULL;\n" |
| 190 | " pCurObj = gpuw;\n" |
| 191 | " pthread_once(&tabOnce, initLayerTable);\n\n" |
| 192 | ' if (!strncmp("xglGetProcAddr", (const char *) funcName, sizeof("xglGetProcAddr")))\n' |
| 193 | ' return xglGetProcAddr;') |
| 194 | for name in xgl.icd_dispatch_table: |
| 195 | if name == "GetProcAddr": |
| 196 | continue |
Jon Ashburn | 5eee57a | 2014-10-15 12:08:33 -0600 | [diff] [blame] | 197 | if name == "InitAndEnumerateGpus": |
| 198 | func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n' |
| 199 | ' return nextTable.%s;' % (prefix, name, prefix, name, name)) |
| 200 | else: |
| 201 | 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] | 202 | ' return %s%s;' % (prefix, name, prefix, name, prefix, name)) |
Jon Ashburn | 5eee57a | 2014-10-15 12:08:33 -0600 | [diff] [blame] | 203 | |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 204 | func_body.append(" else {\n" |
| 205 | " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n" |
| 206 | " if (gpuw->pGPA == NULL)\n" |
| 207 | " return NULL;\n" |
| 208 | " return gpuw->pGPA(gpuw->nextObject, funcName);\n" |
| 209 | " }\n" |
| 210 | "}\n") |
| 211 | return "\n".join(func_body) |
| 212 | |
| 213 | def _generate_layer_dispatch_table(self, prefix='xgl'): |
| 214 | func_body = [] |
| 215 | func_body.append('static void initLayerTable()\n' |
| 216 | '{\n' |
| 217 | ' GetProcAddrType fpNextGPA;\n' |
| 218 | ' fpNextGPA = pCurObj->pGPA;\n' |
| 219 | ' assert(fpNextGPA);\n'); |
| 220 | |
| 221 | for name in xgl.icd_dispatch_table: |
| 222 | func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n' |
| 223 | ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name)) |
| 224 | |
| 225 | func_body.append("}\n") |
| 226 | return "\n".join(func_body) |
| 227 | |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 228 | class LoaderSubcommand(Subcommand): |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 229 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 230 | return "#include \"loader.h\"" |
Chia-I Wu | 1e12226 | 2014-08-01 11:58:32 +0800 | [diff] [blame] | 231 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 232 | def generate_body(self): |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 233 | body = [self._generate_dispatch_entrypoints("LOADER_EXPORT")] |
| 234 | |
| 235 | return "\n\n".join(body) |
| 236 | |
| 237 | class LayerFuncsSubcommand(Subcommand): |
| 238 | def generate_header(self): |
| 239 | return '#include <xglLayer.h>\n#include "loader.h"' |
| 240 | |
| 241 | def generate_body(self): |
| 242 | return self._generate_dispatch_entrypoints("static", True) |
| 243 | |
| 244 | class LayerDispatchSubcommand(Subcommand): |
| 245 | def generate_header(self): |
| 246 | return '#include "layer_wrappers.h"' |
| 247 | |
| 248 | def generate_body(self): |
| 249 | return self._generate_layer_dispatch_table() |
| 250 | |
| 251 | class GenericLayerSubcommand(Subcommand): |
| 252 | def generate_header(self): |
| 253 | 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' |
| 254 | |
| 255 | def generate_body(self): |
| 256 | body = [self._generate_layer_dispatch_table(), |
| 257 | self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", True, True), |
| 258 | self._generate_layer_gpa_function()] |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 259 | |
| 260 | return "\n\n".join(body) |
| 261 | |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 262 | class IcdDispatchTableSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 263 | def generate_body(self): |
| 264 | return self._generate_icd_dispatch_table() |
| 265 | |
| 266 | class IcdDispatchEntrypointsSubcommand(Subcommand): |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 267 | def generate_header(self): |
Chia-I Wu | 468e3c3 | 2014-08-04 08:03:57 +0800 | [diff] [blame] | 268 | return "#include \"icd.h\"" |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 269 | |
| 270 | def generate_body(self): |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 271 | return self._generate_dispatch_entrypoints("ICD_EXPORT") |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 272 | |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 273 | class IcdDispatchDummyImplSubcommand(Subcommand): |
| 274 | def run(self): |
| 275 | if len(self.argv) != 1: |
| 276 | print("IcdDispatchDummyImplSubcommand: <prefix> unspecified") |
| 277 | return |
| 278 | |
| 279 | self.prefix = self.argv[0] |
| 280 | |
| 281 | super().run() |
| 282 | |
| 283 | def generate_header(self): |
| 284 | return "#include \"icd.h\"" |
| 285 | |
| 286 | def _generate_stub_decl(self, proto): |
| 287 | plist = [] |
| 288 | for param in proto.params: |
| 289 | idx = param.ty.find("[") |
| 290 | if idx < 0: |
| 291 | idx = len(param.ty) |
| 292 | |
| 293 | pad = 44 - idx |
| 294 | if pad <= 0: |
| 295 | pad = 1 |
| 296 | |
| 297 | plist.append(" %s%s%s%s" % (param.ty[:idx], |
| 298 | " " * pad, param.name, param.ty[idx:])) |
| 299 | |
| 300 | return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix, |
| 301 | proto.name, ",\n".join(plist)) |
| 302 | |
| 303 | def _generate_stubs(self): |
| 304 | stubs = [] |
| 305 | for proto in self.protos: |
| 306 | if not xgl.is_dispatchable(proto): |
| 307 | continue |
| 308 | |
| 309 | decl = self._generate_stub_decl(proto) |
| 310 | if proto.ret != "XGL_VOID": |
| 311 | stmt = " return XGL_ERROR_UNAVAILABLE;\n" |
| 312 | else: |
| 313 | stmt = "" |
| 314 | |
| 315 | stubs.append("static %s\n{\n%s}" % (decl, stmt)) |
| 316 | |
| 317 | return "\n\n".join(stubs) |
| 318 | |
| 319 | |
| 320 | def _generate_tables(self): |
| 321 | initializer = [] |
| 322 | for proto in self.protos: |
| 323 | prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl" |
| 324 | initializer.append(".%s = %s%s" % |
| 325 | (proto.name, prefix, proto.name)) |
| 326 | |
| 327 | return """const struct icd_dispatch_table %s_normal_dispatch_table = { |
| 328 | %s, |
| 329 | }; |
| 330 | |
| 331 | const struct icd_dispatch_table %s_debug_dispatch_table = { |
| 332 | %s, |
| 333 | };""" % (self.prefix, ",\n ".join(initializer), |
| 334 | self.prefix, ",\n ".join(initializer)) |
| 335 | |
| 336 | def generate_body(self): |
| 337 | body = [self._generate_stubs(), |
| 338 | self._generate_tables()] |
| 339 | |
| 340 | return "\n\n".join(body) |
| 341 | |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 342 | def main(): |
| 343 | subcommands = { |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 344 | "loader": LoaderSubcommand, |
Jon Ashburn | d43f9b6 | 2014-10-14 19:15:22 -0600 | [diff] [blame] | 345 | "layer-funcs" : LayerFuncsSubcommand, |
| 346 | "layer-dispatch" : LayerDispatchSubcommand, |
| 347 | "generic-layer" : GenericLayerSubcommand, |
Chia-I Wu | 84dace9 | 2014-08-03 09:55:18 +0800 | [diff] [blame] | 348 | "icd-dispatch-table": IcdDispatchTableSubcommand, |
| 349 | "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand, |
Chia-I Wu | 30c7829 | 2014-08-04 10:08:08 +0800 | [diff] [blame] | 350 | "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand, |
Chia-I Wu | fb2559d | 2014-08-01 11:19:52 +0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 354 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 355 | print |
| 356 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 357 | exit(1) |
| 358 | |
| 359 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 360 | subcmd.run() |
| 361 | |
| 362 | if __name__ == "__main__": |
| 363 | main() |