Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 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> |
| 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 = () |
| 36 | self.headers = () |
| 37 | |
| 38 | def run(self): |
| 39 | self.protos = xgl.core + xgl.ext_wsi_x11 |
| 40 | self.headers = xgl.core_headers + xgl.ext_wsi_x11_headers |
| 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): |
| 89 | return "\n".join(["#include <" + h + ">" for h in self.headers]) |
| 90 | |
| 91 | def generate_body(self): |
| 92 | pass |
| 93 | |
| 94 | def generate_footer(self): |
| 95 | pass |
| 96 | |
| 97 | # Return set of printf '%' qualifier and input to that qualifier |
Tobin Ehlis | a554dc3 | 2014-11-19 15:52:46 -0700 | [diff] [blame] | 98 | def _get_printf_params(self, xgl_type, name, output_param): |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 99 | # TODO : Need ENUM and STRUCT checks here |
| 100 | if "_TYPE" in xgl_type: # TODO : This should be generic ENUM check |
| 101 | return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name)) |
| 102 | if "XGL_CHAR*" == xgl_type: |
| 103 | return ("%s", name) |
| 104 | if "UINT64" in xgl_type: |
| 105 | if '*' in xgl_type: |
| 106 | return ("%lu", "*%s" % name) |
| 107 | return ("%lu", name) |
| 108 | if "FLOAT" in xgl_type: |
| 109 | if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic) |
| 110 | return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name)) |
| 111 | return ("%f", name) |
Tobin Ehlis | 0a1e06d | 2014-11-11 17:28:22 -0700 | [diff] [blame] | 112 | if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type: |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 113 | return ("%u", name) |
Tobin Ehlis | 0a1e06d | 2014-11-11 17:28:22 -0700 | [diff] [blame] | 114 | if True in [t in xgl_type for t in ["INT", "SIZE", "FLAGS", "MASK", "xcb_window_t"]]: |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 115 | if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic) |
| 116 | return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name)) |
| 117 | if '*' in xgl_type: |
| 118 | return ("%i", "*%s" % name) |
| 119 | return ("%i", name) |
Tobin Ehlis | 0a1e06d | 2014-11-11 17:28:22 -0700 | [diff] [blame] | 120 | # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it |
| 121 | if "XGL_FORMAT" == xgl_type and "format" == name: |
| 122 | return ("{format.channelFormat = %s, format.numericFormat = %s}", "string_XGL_CHANNEL_FORMAT(format.channelFormat), string_XGL_NUM_FORMAT(format.numericFormat)") |
Tobin Ehlis | a554dc3 | 2014-11-19 15:52:46 -0700 | [diff] [blame] | 123 | if output_param: |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 124 | return ("%p", "(void*)*%s" % name) |
| 125 | return ("%p", "(void*)%s" % name) |
| 126 | |
| 127 | def _generate_icd_dispatch_table(self): |
| 128 | proto_map = {} |
| 129 | for proto in self.protos: |
| 130 | proto_map[proto.name] = proto |
| 131 | |
| 132 | entries = [] |
| 133 | for name in xgl.icd_dispatch_table: |
| 134 | proto = proto_map[name] |
| 135 | entries.append(proto.c_typedef(attr="XGLAPI")) |
| 136 | |
| 137 | return """struct icd_dispatch_table { |
| 138 | %s; |
| 139 | };""" % ";\n ".join(entries) |
| 140 | |
| 141 | def _generate_dispatch_entrypoints(self, qual="", layer="generic"): |
| 142 | if qual: |
| 143 | qual += " " |
| 144 | |
| 145 | funcs = [] |
| 146 | for proto in self.protos: |
| 147 | if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus": |
| 148 | if "generic" == layer: |
| 149 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 150 | param0_name = proto.params[0].name |
| 151 | ret_val = '' |
| 152 | stmt = '' |
| 153 | if proto.ret != "XGL_VOID": |
| 154 | ret_val = "XGL_RESULT result = " |
| 155 | stmt = " return result;\n" |
| 156 | if proto.params[0].ty != "XGL_PHYSICAL_GPU": |
| 157 | funcs.append('%s%s\n' |
| 158 | '{\n' |
| 159 | ' %snextTable.%s;\n' |
| 160 | '%s' |
| 161 | '}' % (qual, decl, ret_val, proto.c_call(), stmt)) |
| 162 | else: |
| 163 | c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1) |
| 164 | funcs.append('%s%s\n' |
| 165 | '{\n' |
| 166 | ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n' |
| 167 | ' printf("At start of layered %s\\n");\n' |
| 168 | ' pCurObj = gpuw;\n' |
| 169 | ' pthread_once(&tabOnce, initLayerTable);\n' |
| 170 | ' %snextTable.%s;\n' |
| 171 | ' printf("Completed layered %s\\n");\n' |
Courtney Goeltzenleuchter | e6094fc | 2014-11-18 10:40:29 -0700 | [diff] [blame] | 172 | ' fflush(stdout);\n' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 173 | '%s' |
| 174 | '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt)) |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 175 | elif "apidump" in layer: |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 176 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 177 | param0_name = proto.params[0].name |
| 178 | ret_val = '' |
| 179 | stmt = '' |
Tobin Ehlis | 73ff567 | 2014-10-23 08:19:47 -0600 | [diff] [blame] | 180 | cis_param_index = [] # Store list of indices when func has struct params |
Tobin Ehlis | a554dc3 | 2014-11-19 15:52:46 -0700 | [diff] [blame] | 181 | create_params = 0 # Num of params at end of function that are created and returned as output values |
| 182 | if 'WsiX11CreatePresentableImage' in proto.name: |
| 183 | create_params = -2 |
| 184 | elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name: |
| 185 | create_params = -1 |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 186 | if proto.ret != "XGL_VOID": |
| 187 | ret_val = "XGL_RESULT result = " |
| 188 | stmt = " return result;\n" |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 189 | f_open = '' |
| 190 | f_close = '' |
| 191 | if "file" in layer: |
Tobin Ehlis | 1eba779 | 2014-11-21 09:35:53 -0700 | [diff] [blame^] | 192 | file_mode = "a" |
| 193 | if 'CreateDevice' in proto.name: |
| 194 | file_mode = "w" |
| 195 | f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode) |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 196 | log_func = 'fprintf(pOutFile, "xgl%s(' % proto.name |
Tobin Ehlis | 1aa7d3e | 2014-11-20 12:18:45 -0700 | [diff] [blame] | 197 | f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 198 | else: |
Tobin Ehlis | 1aa7d3e | 2014-11-20 12:18:45 -0700 | [diff] [blame] | 199 | f_open = 'pthread_mutex_lock( &print_lock );\n ' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 200 | log_func = 'printf("xgl%s(' % proto.name |
Tobin Ehlis | 1aa7d3e | 2014-11-20 12:18:45 -0700 | [diff] [blame] | 201 | f_close = '\n pthread_mutex_unlock( &print_lock );' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 202 | print_vals = '' |
| 203 | pindex = 0 |
| 204 | for p in proto.params: |
Tobin Ehlis | a554dc3 | 2014-11-19 15:52:46 -0700 | [diff] [blame] | 205 | # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals |
| 206 | cp = False |
| 207 | if 0 != create_params: |
| 208 | # If this is any of the N last params of the func, treat as output |
| 209 | for y in range(-1, create_params-1, -1): |
| 210 | if p.name == proto.params[y].name: |
| 211 | cp = True |
| 212 | (pft, pfi) = self._get_printf_params(p.ty, p.name, cp) |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 213 | log_func += '%s = %s, ' % (p.name, pft) |
| 214 | print_vals += ', %s' % (pfi) |
Tobin Ehlis | 73ff567 | 2014-10-23 08:19:47 -0600 | [diff] [blame] | 215 | # TODO : Just want this to be simple check for params of STRUCT type |
| 216 | if "pCreateInfo" in p.name or ('const' in p.ty and '*' in p.ty and False not in [tmp_ty not in p.ty for tmp_ty in ['XGL_CHAR', 'XGL_VOID', 'XGL_CMD_BUFFER', 'XGL_QUEUE_SEMAPHORE', 'XGL_FENCE', 'XGL_SAMPLER', 'XGL_UINT32']]): |
Tobin Ehlis | 0a1e06d | 2014-11-11 17:28:22 -0700 | [diff] [blame] | 217 | if 'Wsi' not in proto.name: |
| 218 | cis_param_index.append(pindex) |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 219 | pindex += 1 |
| 220 | log_func = log_func.strip(', ') |
| 221 | if proto.ret != "XGL_VOID": |
| 222 | log_func += ') = %s\\n"' |
Courtney Goeltzenleuchter | e6094fc | 2014-11-18 10:40:29 -0700 | [diff] [blame] | 223 | print_vals += ', string_XGL_RESULT(result)' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 224 | else: |
| 225 | log_func += ')\\n"' |
| 226 | log_func = '%s%s);' % (log_func, print_vals) |
Tobin Ehlis | 73ff567 | 2014-10-23 08:19:47 -0600 | [diff] [blame] | 227 | if len(cis_param_index) > 0: |
| 228 | log_func += '\n char *pTmpStr;' |
| 229 | for sp_index in cis_param_index: |
| 230 | cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower()) |
| 231 | log_func += '\n if (%s) {' % (proto.params[sp_index].name) |
| 232 | log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name) |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 233 | if "file" in layer: |
| 234 | log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name) |
| 235 | else: |
| 236 | log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name) |
Courtney Goeltzenleuchter | e6094fc | 2014-11-18 10:40:29 -0700 | [diff] [blame] | 237 | log_func += '\n fflush(stdout);\n' |
Tobin Ehlis | 73ff567 | 2014-10-23 08:19:47 -0600 | [diff] [blame] | 238 | log_func += '\n free(pTmpStr);\n }' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 239 | if proto.params[0].ty != "XGL_PHYSICAL_GPU": |
| 240 | funcs.append('%s%s\n' |
| 241 | '{\n' |
| 242 | ' %snextTable.%s;\n' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 243 | ' %s%s%s\n' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 244 | '%s' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 245 | '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt)) |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 246 | else: |
| 247 | c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1) |
| 248 | funcs.append('%s%s\n' |
| 249 | '{\n' |
| 250 | ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n' |
| 251 | ' pCurObj = gpuw;\n' |
| 252 | ' pthread_once(&tabOnce, initLayerTable);\n' |
| 253 | ' %snextTable.%s;\n' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 254 | ' %s%s%s\n' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 255 | '%s' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 256 | '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt)) |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 257 | elif "objecttracker" == layer: |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 258 | obj_type_mapping = {"XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU", "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE", |
| 259 | "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE", "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE", |
| 260 | "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY", "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE", |
| 261 | "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL", "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT", |
| 262 | "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE", "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET", |
| 263 | "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER", "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER", |
| 264 | "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE", "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA", |
| 265 | "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER", "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW", |
| 266 | "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW", "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW", |
| 267 | "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE", "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE", |
| 268 | "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE", "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE", |
| 269 | "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE", "XGL_BASE_OBJECT" : "ll_get_obj_type(object)", |
| 270 | "XGL_OBJECT" : "ll_get_obj_type(object)"} |
| 271 | |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 272 | decl = proto.c_func(prefix="xgl", attr="XGLAPI") |
| 273 | param0_name = proto.params[0].name |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 274 | p0_type = proto.params[0].ty |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 275 | create_line = '' |
| 276 | destroy_line = '' |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 277 | if 'DbgRegisterMsgCallback' in proto.name: |
| 278 | using_line = ' // This layer intercepts callbacks\n' |
| 279 | using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n' |
| 280 | using_line += ' if (!pNewDbgFuncNode)\n' |
| 281 | using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n' |
| 282 | using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n' |
| 283 | using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n' |
| 284 | using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n' |
| 285 | using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n' |
| 286 | elif 'DbgUnregisterMsgCallback' in proto.name: |
| 287 | using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n' |
| 288 | using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n' |
| 289 | using_line += ' while (pTrav) {\n' |
| 290 | using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n' |
| 291 | using_line += ' pPrev->pNext = pTrav->pNext;\n' |
| 292 | using_line += ' if (pDbgFunctionHead == pTrav)\n' |
| 293 | using_line += ' pDbgFunctionHead = pTrav->pNext;\n' |
| 294 | using_line += ' free(pTrav);\n' |
| 295 | using_line += ' break;\n' |
| 296 | using_line += ' }\n' |
| 297 | using_line += ' pPrev = pTrav;\n' |
| 298 | using_line += ' pTrav = pTrav->pNext;\n' |
| 299 | using_line += ' }\n' |
| 300 | elif 'GlobalOption' in proto.name: |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 301 | using_line = '' |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 302 | else: |
| 303 | using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type]) |
| 304 | if 'Create' in proto.name or 'Alloc' in proto.name: |
| 305 | create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')]) |
| 306 | if 'DestroyObject' in proto.name: |
| 307 | destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name) |
| 308 | using_line = '' |
| 309 | else: |
| 310 | if 'Destroy' in proto.name or 'Free' in proto.name: |
| 311 | destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type]) |
| 312 | using_line = '' |
| 313 | if 'DestroyDevice' in proto.name: |
| 314 | destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n' |
| 315 | destroy_line += ' char str[1024];\n' |
| 316 | destroy_line += ' sprintf(str, "OBJ ERROR : %s object %p has not been destroyed (was used %lu times).", string_XGL_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, pTrav->obj.numUses);\n' |
| 317 | destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n' |
| 318 | destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 319 | ret_val = '' |
| 320 | stmt = '' |
| 321 | if proto.ret != "XGL_VOID": |
| 322 | ret_val = "XGL_RESULT result = " |
| 323 | stmt = " return result;\n" |
| 324 | if proto.params[0].ty != "XGL_PHYSICAL_GPU": |
| 325 | funcs.append('%s%s\n' |
| 326 | '{\n' |
| 327 | '%s' |
| 328 | ' %snextTable.%s;\n' |
| 329 | '%s%s' |
| 330 | '%s' |
| 331 | '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt)) |
| 332 | else: |
| 333 | c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1) |
| 334 | funcs.append('%s%s\n' |
| 335 | '{\n' |
| 336 | ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n' |
| 337 | '%s' |
| 338 | ' pCurObj = gpuw;\n' |
| 339 | ' pthread_once(&tabOnce, initLayerTable);\n' |
| 340 | ' %snextTable.%s;\n' |
| 341 | '%s%s' |
| 342 | '%s' |
| 343 | '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt)) |
| 344 | |
| 345 | # TODO : Put this code somewhere so it gets called at the end if objects not deleted : |
| 346 | # // Report any remaining objects in LL |
| 347 | # objNode *pTrav = pObjLLHead; |
| 348 | # while (pTrav) { |
| 349 | # printf("WARN : %s object %p has not been destroyed.\n", pTrav->objType, pTrav->pObj); |
| 350 | # } |
| 351 | |
| 352 | return "\n\n".join(funcs) |
| 353 | |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 354 | def _generate_extensions(self): |
| 355 | exts = [] |
| 356 | exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)') |
| 357 | exts.append('{') |
| 358 | exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];') |
| 359 | exts.append('}') |
| 360 | exts.append('') |
| 361 | exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)') |
| 362 | exts.append('{') |
| 363 | exts.append(" // This bool flags if we're pulling all objs or just a single class of objs") |
| 364 | exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);') |
| 365 | exts.append(' // Check the count first thing') |
| 366 | exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];') |
| 367 | exts.append(' if (objCount > maxObjCount) {') |
| 368 | exts.append(' char str[1024];') |
| 369 | exts.append(' sprintf(str, "OBJ ERROR : Received objTrackGetObjects() request for %lu objs, but there are only %lu objs of type %s", objCount, maxObjCount, string_XGL_OBJECT_TYPE(type));') |
| 370 | exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);') |
| 371 | exts.append(' return XGL_ERROR_INVALID_VALUE;') |
| 372 | exts.append(' }') |
| 373 | exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];') |
| 374 | exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {') |
| 375 | exts.append(' if (!pTrav) {') |
| 376 | exts.append(' char str[1024];') |
| 377 | exts.append(' sprintf(str, "OBJ INTERNAL ERROR : Ran out of %s objs! Should have %lu, but only copied %lu and not the requested %lu.", string_XGL_OBJECT_TYPE(type), maxObjCount, i, objCount);') |
| 378 | exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);') |
| 379 | exts.append(' return XGL_ERROR_UNKNOWN;') |
| 380 | exts.append(' }') |
| 381 | exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));') |
| 382 | exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;') |
| 383 | exts.append(' }') |
| 384 | exts.append(' return XGL_SUCCESS;') |
| 385 | exts.append('}') |
| 386 | |
| 387 | return "\n".join(exts) |
| 388 | |
| 389 | def _generate_layer_gpa_function(self, prefix="xgl", extensions=[]): |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 390 | func_body = [] |
| 391 | func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n" |
| 392 | "{\n" |
| 393 | " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n" |
| 394 | " if (gpu == NULL)\n" |
| 395 | " return NULL;\n" |
| 396 | " pCurObj = gpuw;\n" |
| 397 | " pthread_once(&tabOnce, initLayerTable);\n\n" |
| 398 | ' if (!strncmp("xglGetProcAddr", (const char *) funcName, sizeof("xglGetProcAddr")))\n' |
| 399 | ' return xglGetProcAddr;') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 400 | if 0 != len(extensions): |
| 401 | for ext_name in extensions: |
| 402 | func_body.append(' else if (!strncmp("%s", (const char *) funcName, sizeof("%s")))\n' |
| 403 | ' return %s;' % (ext_name, ext_name, ext_name)) |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 404 | for name in xgl.icd_dispatch_table: |
| 405 | if name == "GetProcAddr": |
| 406 | continue |
| 407 | if name == "InitAndEnumerateGpus": |
| 408 | func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n' |
| 409 | ' return nextTable.%s;' % (prefix, name, prefix, name, name)) |
| 410 | else: |
| 411 | func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n' |
| 412 | ' return %s%s;' % (prefix, name, prefix, name, prefix, name)) |
| 413 | |
| 414 | func_body.append(" else {\n" |
| 415 | " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n" |
| 416 | " if (gpuw->pGPA == NULL)\n" |
| 417 | " return NULL;\n" |
| 418 | " return gpuw->pGPA(gpuw->nextObject, funcName);\n" |
| 419 | " }\n" |
| 420 | "}\n") |
| 421 | return "\n".join(func_body) |
| 422 | |
| 423 | def _generate_layer_dispatch_table(self, prefix='xgl'): |
| 424 | func_body = [] |
| 425 | func_body.append('static void initLayerTable()\n' |
| 426 | '{\n' |
| 427 | ' GetProcAddrType fpNextGPA;\n' |
| 428 | ' fpNextGPA = pCurObj->pGPA;\n' |
| 429 | ' assert(fpNextGPA);\n'); |
| 430 | |
| 431 | for name in xgl.icd_dispatch_table: |
| 432 | func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n' |
| 433 | ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name)) |
| 434 | |
| 435 | func_body.append("}\n") |
| 436 | return "\n".join(func_body) |
| 437 | |
| 438 | class LayerFuncsSubcommand(Subcommand): |
| 439 | def generate_header(self): |
| 440 | return '#include <xglLayer.h>\n#include "loader.h"' |
| 441 | |
| 442 | def generate_body(self): |
| 443 | return self._generate_dispatch_entrypoints("static", True) |
| 444 | |
| 445 | class LayerDispatchSubcommand(Subcommand): |
| 446 | def generate_header(self): |
| 447 | return '#include "layer_wrappers.h"' |
| 448 | |
| 449 | def generate_body(self): |
| 450 | return self._generate_layer_dispatch_table() |
| 451 | |
| 452 | class GenericLayerSubcommand(Subcommand): |
| 453 | def generate_header(self): |
| 454 | 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' |
| 455 | |
| 456 | def generate_body(self): |
| 457 | body = [self._generate_layer_dispatch_table(), |
| 458 | self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "generic"), |
| 459 | self._generate_layer_gpa_function()] |
| 460 | |
| 461 | return "\n\n".join(body) |
| 462 | |
| 463 | class ApiDumpSubcommand(Subcommand): |
| 464 | def generate_header(self): |
Tobin Ehlis | 1aa7d3e | 2014-11-20 12:18:45 -0700 | [diff] [blame] | 465 | 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#include "xgl_struct_string_helper.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\npthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;\n' |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 466 | |
| 467 | def generate_body(self): |
| 468 | body = [self._generate_layer_dispatch_table(), |
| 469 | self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "apidump"), |
| 470 | self._generate_layer_gpa_function()] |
| 471 | |
| 472 | return "\n\n".join(body) |
| 473 | |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 474 | class ApiDumpFileSubcommand(Subcommand): |
| 475 | def generate_header(self): |
Tobin Ehlis | 1aa7d3e | 2014-11-20 12:18:45 -0700 | [diff] [blame] | 476 | 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#include "xgl_struct_string_helper.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\n\nstatic FILE* pOutFile;\nstatic char* outFileName = "xgl_apidump.txt";\npthread_mutex_t file_lock = PTHREAD_MUTEX_INITIALIZER;\n' |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 477 | |
| 478 | def generate_body(self): |
| 479 | body = [self._generate_layer_dispatch_table(), |
| 480 | self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "apidump-file"), |
| 481 | self._generate_layer_gpa_function()] |
| 482 | |
| 483 | return "\n\n".join(body) |
| 484 | |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 485 | class ObjectTrackerSubcommand(Subcommand): |
| 486 | def generate_header(self): |
| 487 | header_txt = [] |
| 488 | header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 489 | header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;') |
| 490 | header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;') |
| 491 | header_txt.append('// Ptr to LL of dbg functions') |
| 492 | header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;') |
| 493 | header_txt.append('// Utility function to handle reporting') |
| 494 | header_txt.append('// If callbacks are enabled, use them, otherwise use printf') |
| 495 | header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,') |
| 496 | header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,') |
| 497 | header_txt.append(' XGL_BASE_OBJECT srcObject,') |
| 498 | header_txt.append(' XGL_SIZE location,') |
| 499 | header_txt.append(' XGL_INT msgCode,') |
| 500 | header_txt.append(' const XGL_CHAR* pLayerPrefix,') |
| 501 | header_txt.append(' const XGL_CHAR* pMsg)') |
| 502 | header_txt.append('{') |
| 503 | header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;') |
| 504 | header_txt.append(' if (pTrav) {') |
| 505 | header_txt.append(' while (pTrav) {') |
| 506 | header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);') |
| 507 | header_txt.append(' pTrav = pTrav->pNext;') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 508 | header_txt.append(' }') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 509 | header_txt.append(' }') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 510 | header_txt.append(' else {') |
| 511 | header_txt.append(' switch (msgType) {') |
| 512 | header_txt.append(' case XGL_DBG_MSG_ERROR:') |
| 513 | header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);') |
| 514 | header_txt.append(' break;') |
| 515 | header_txt.append(' case XGL_DBG_MSG_WARNING:') |
| 516 | header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);') |
| 517 | header_txt.append(' break;') |
| 518 | header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:') |
| 519 | header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);') |
| 520 | header_txt.append(' break;') |
| 521 | header_txt.append(' default:') |
| 522 | header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);') |
| 523 | header_txt.append(' break;') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 524 | header_txt.append(' }') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 525 | header_txt.append(' }') |
| 526 | header_txt.append('}') |
| 527 | header_txt.append('// We maintain a "Global" list which links every object and a') |
| 528 | header_txt.append('// per-Object list which just links objects of a given type') |
| 529 | header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists') |
| 530 | header_txt.append('typedef struct _objNode {') |
| 531 | header_txt.append(' OBJTRACK_NODE obj;') |
| 532 | header_txt.append(' struct _objNode *pNextObj;') |
| 533 | header_txt.append(' struct _objNode *pNextGlobal;') |
| 534 | header_txt.append('} objNode;') |
| 535 | header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};') |
| 536 | header_txt.append('static objNode *pGlobalHead = NULL;') |
| 537 | header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};') |
| 538 | header_txt.append('static uint64_t numTotalObjs = 0;') |
| 539 | header_txt.append('// Debug function to print global list and each individual object list') |
| 540 | header_txt.append('static void ll_print_lists()') |
| 541 | header_txt.append('{') |
| 542 | header_txt.append(' objNode* pTrav = pGlobalHead;') |
| 543 | header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);') |
| 544 | header_txt.append(' while (pTrav) {') |
| 545 | header_txt.append(' printf(" ObjNode (%p) w/ %s obj %p has pNextGlobal %p\\n", (void*)pTrav, string_XGL_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, (void*)pTrav->pNextGlobal);') |
| 546 | header_txt.append(' pTrav = pTrav->pNextGlobal;') |
| 547 | header_txt.append(' }') |
| 548 | header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {') |
| 549 | header_txt.append(' pTrav = pObjectHead[i];') |
| 550 | header_txt.append(' if (pTrav) {') |
| 551 | header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);') |
| 552 | header_txt.append(' while (pTrav) {') |
| 553 | header_txt.append(' printf(" ObjNode (%p) w/ %s obj %p has pNextObj %p\\n", (void*)pTrav, string_XGL_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, (void*)pTrav->pNextObj);') |
| 554 | header_txt.append(' pTrav = pTrav->pNextObj;') |
| 555 | header_txt.append(' }') |
| 556 | header_txt.append(' }') |
| 557 | header_txt.append(' }') |
| 558 | header_txt.append('}') |
| 559 | header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {') |
| 560 | header_txt.append(' char str[1024];') |
| 561 | header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);') |
| 562 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);') |
| 563 | header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));') |
| 564 | header_txt.append(' pNewObjNode->obj.pObj = pObj;') |
| 565 | header_txt.append(' pNewObjNode->obj.objType = objType;') |
| 566 | header_txt.append(' pNewObjNode->obj.numUses = 0;') |
| 567 | header_txt.append(' // insert at front of global list') |
| 568 | header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;') |
| 569 | header_txt.append(' pGlobalHead = pNewObjNode;') |
| 570 | header_txt.append(' // insert at front of object list') |
| 571 | header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];') |
| 572 | header_txt.append(' pObjectHead[objType] = pNewObjNode;') |
| 573 | header_txt.append(' // increment obj counts') |
| 574 | header_txt.append(' numObjs[objType]++;') |
| 575 | header_txt.append(' numTotalObjs++;') |
| 576 | header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_XGL_OBJECT_TYPE(objType));') |
| 577 | header_txt.append(' //ll_print_lists();') |
| 578 | header_txt.append('}') |
| 579 | header_txt.append('// Traverse global list and return type for given object') |
| 580 | header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {') |
| 581 | header_txt.append(' objNode *pTrav = pGlobalHead;') |
| 582 | header_txt.append(' while (pTrav) {') |
| 583 | header_txt.append(' if (pTrav->obj.pObj == object)') |
| 584 | header_txt.append(' return pTrav->obj.objType;') |
| 585 | header_txt.append(' pTrav = pTrav->pNextGlobal;') |
| 586 | header_txt.append(' }') |
| 587 | header_txt.append(' char str[1024];') |
| 588 | header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);') |
| 589 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);') |
| 590 | header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;') |
| 591 | header_txt.append('}') |
| 592 | header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {') |
| 593 | header_txt.append(' objNode *pTrav = pObjectHead[objType];') |
| 594 | header_txt.append(' while (pTrav) {') |
| 595 | header_txt.append(' if (pTrav->obj.pObj == pObj) {') |
| 596 | header_txt.append(' return pTrav->obj.numUses;') |
| 597 | header_txt.append(' }') |
| 598 | header_txt.append(' pTrav = pTrav->pNextObj;') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 599 | header_txt.append(' }') |
| 600 | header_txt.append(' return 0;') |
| 601 | header_txt.append('}') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 602 | header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {') |
| 603 | header_txt.append(' objNode *pTrav = pObjectHead[objType];') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 604 | header_txt.append(' while (pTrav) {') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 605 | header_txt.append(' if (pTrav->obj.pObj == pObj) {') |
| 606 | header_txt.append(' pTrav->obj.numUses++;') |
| 607 | header_txt.append(' char str[1024];') |
| 608 | header_txt.append(' sprintf(str, "OBJ[%llu] : USING %s object %p (%lu total uses)", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj, pTrav->obj.numUses);') |
| 609 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);') |
| 610 | header_txt.append(' return;') |
| 611 | header_txt.append(' }') |
| 612 | header_txt.append(' pTrav = pTrav->pNextObj;') |
| 613 | header_txt.append(' }') |
| 614 | header_txt.append(' // If we do not find obj, insert it and then increment count') |
| 615 | header_txt.append(' char str[1024];') |
| 616 | header_txt.append(' sprintf(str, "Unable to increment count for obj %p, will add to list as %s type and increment count", pObj, string_XGL_OBJECT_TYPE(objType));') |
| 617 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);') |
| 618 | header_txt.append('') |
| 619 | header_txt.append(' ll_insert_obj(pObj, objType);') |
| 620 | header_txt.append(' ll_increment_use_count(pObj, objType);') |
| 621 | header_txt.append('}') |
| 622 | header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch') |
| 623 | header_txt.append('// Type from global list w/ ll_destroy_obj()') |
| 624 | header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()') |
| 625 | header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {') |
| 626 | header_txt.append(' objNode *pTrav = pObjectHead[objType];') |
| 627 | header_txt.append(' objNode *pPrev = pObjectHead[objType];') |
| 628 | header_txt.append(' while (pTrav) {') |
| 629 | header_txt.append(' if (pTrav->obj.pObj == pObj) {') |
| 630 | header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;') |
| 631 | header_txt.append(' // update HEAD of Obj list as needed') |
| 632 | header_txt.append(' if (pObjectHead[objType] == pTrav)') |
| 633 | header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;') |
| 634 | header_txt.append(' assert(numObjs[objType] > 0);') |
| 635 | header_txt.append(' numObjs[objType]--;') |
| 636 | header_txt.append(' char str[1024];') |
| 637 | header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);') |
| 638 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 639 | header_txt.append(' return;') |
| 640 | header_txt.append(' }') |
| 641 | header_txt.append(' pPrev = pTrav;') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 642 | header_txt.append(' pTrav = pTrav->pNextObj;') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 643 | header_txt.append(' }') |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 644 | header_txt.append(' char str[1024];') |
| 645 | header_txt.append(' sprintf(str, "OBJ INTERNAL ERROR : Obj %p was in global list but not in %s list", pObj, string_XGL_OBJECT_TYPE(objType));') |
| 646 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);') |
| 647 | header_txt.append('}') |
| 648 | header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally') |
| 649 | header_txt.append('// remove obj from global list') |
| 650 | header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {') |
| 651 | header_txt.append(' objNode *pTrav = pGlobalHead;') |
| 652 | header_txt.append(' objNode *pPrev = pGlobalHead;') |
| 653 | header_txt.append(' while (pTrav) {') |
| 654 | header_txt.append(' if (pTrav->obj.pObj == pObj) {') |
| 655 | header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);') |
| 656 | header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;') |
| 657 | header_txt.append(' // update HEAD of global list if needed') |
| 658 | header_txt.append(' if (pGlobalHead == pTrav)') |
| 659 | header_txt.append(' pGlobalHead = pTrav->pNextGlobal;') |
| 660 | header_txt.append(' free(pTrav);') |
| 661 | header_txt.append(' assert(numTotalObjs > 0);') |
| 662 | header_txt.append(' numTotalObjs--;') |
| 663 | header_txt.append(' char str[1024];') |
| 664 | header_txt.append(' sprintf(str, "OBJ_STAT Removed %s obj %p that was used %lu times (%lu total objs & %lu %s objs).", string_XGL_OBJECT_TYPE(pTrav->obj.objType), pTrav->obj.pObj, pTrav->obj.numUses, numTotalObjs, numObjs[pTrav->obj.objType], string_XGL_OBJECT_TYPE(pTrav->obj.objType));') |
| 665 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);') |
| 666 | header_txt.append(' return;') |
| 667 | header_txt.append(' }') |
| 668 | header_txt.append(' pPrev = pTrav;') |
| 669 | header_txt.append(' pTrav = pTrav->pNextGlobal;') |
| 670 | header_txt.append(' }') |
| 671 | header_txt.append(' char str[1024];') |
| 672 | header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);') |
| 673 | header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_DESTROY_OBJECT_FAILED, "OBJTRACK", str);') |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 674 | header_txt.append('}') |
| 675 | |
| 676 | return "\n".join(header_txt) |
| 677 | |
| 678 | def generate_body(self): |
| 679 | body = [self._generate_layer_dispatch_table(), |
| 680 | self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "objecttracker"), |
Tobin Ehlis | 3c26a54 | 2014-11-18 11:28:33 -0700 | [diff] [blame] | 681 | self._generate_extensions(), |
| 682 | self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])] |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 683 | |
| 684 | return "\n\n".join(body) |
Courtney Goeltzenleuchter | e6094fc | 2014-11-18 10:40:29 -0700 | [diff] [blame] | 685 | |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 686 | def main(): |
| 687 | subcommands = { |
| 688 | "layer-funcs" : LayerFuncsSubcommand, |
| 689 | "layer-dispatch" : LayerDispatchSubcommand, |
| 690 | "generic-layer" : GenericLayerSubcommand, |
| 691 | "api-dump" : ApiDumpSubcommand, |
Tobin Ehlis | 574b014 | 2014-11-12 13:11:15 -0700 | [diff] [blame] | 692 | "api-dump-file" : ApiDumpFileSubcommand, |
Tobin Ehlis | 92dbf80 | 2014-10-22 09:06:33 -0600 | [diff] [blame] | 693 | "object-tracker" : ObjectTrackerSubcommand, |
| 694 | } |
| 695 | |
| 696 | if len(sys.argv) < 2 or sys.argv[1] not in subcommands: |
| 697 | print("Usage: %s <subcommand> [options]" % sys.argv[0]) |
| 698 | print |
| 699 | print("Available sucommands are: %s" % " ".join(subcommands)) |
| 700 | exit(1) |
| 701 | |
| 702 | subcmd = subcommands[sys.argv[1]](sys.argv[2:]) |
| 703 | subcmd.run() |
| 704 | |
| 705 | if __name__ == "__main__": |
| 706 | main() |