blob: 57a03c06e97b352d1d5285e42e4a69008db68663 [file] [log] [blame]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001#!/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
28import sys
29
30import xgl
31
32class 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 Ehlisa554dc32014-11-19 15:52:46 -070098 def _get_printf_params(self, xgl_type, name, output_param):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060099 # 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 Ehlis0a1e06d2014-11-11 17:28:22 -0700112 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600113 return ("%u", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700114 if True in [t in xgl_type for t in ["INT", "SIZE", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600115 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 Ehlis0a1e06d2014-11-11 17:28:22 -0700120 # 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 Ehlisa554dc32014-11-19 15:52:46 -0700123 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600124 return ("%p", "(void*)*%s" % name)
125 return ("%p", "(void*)%s" % name)
126
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700127 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600128 if qual:
129 qual += " "
130
Tobin Ehlis907a0522014-11-25 16:59:27 -0700131 layer_name = layer
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700132 if no_addr:
133 layer_name = "%sNoAddr" % layer
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600134 funcs = []
135 for proto in self.protos:
136 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlis907a0522014-11-25 16:59:27 -0700137 if "Generic" == layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600138 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
139 param0_name = proto.params[0].name
140 ret_val = ''
141 stmt = ''
142 if proto.ret != "XGL_VOID":
143 ret_val = "XGL_RESULT result = "
144 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700145 if proto.name == "EnumerateLayers":
146 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
147 funcs.append('%s%s\n'
148 '{\n'
149 ' if (gpu != NULL) {\n'
150 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
151 ' printf("At start of layered %s\\n");\n'
152 ' pCurObj = gpuw;\n'
153 ' pthread_once(&tabOnce, initLayerTable);\n'
154 ' %snextTable.%s;\n'
155 ' printf("Completed layered %s\\n");\n'
156 ' fflush(stdout);\n'
157 ' %s'
158 ' } else {\n'
159 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
160 ' return XGL_ERROR_INVALID_POINTER;\n'
161 ' // This layer compatible with all GPUs\n'
162 ' *pOutLayerCount = 1;\n'
163 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
164 ' return XGL_SUCCESS;\n'
165 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700166 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Jon Ashburn451c16f2014-11-25 11:08:42 -0700167 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600168 funcs.append('%s%s\n'
169 '{\n'
170 ' %snextTable.%s;\n'
171 '%s'
172 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
173 else:
174 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
175 funcs.append('%s%s\n'
176 '{\n'
177 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
178 ' printf("At start of layered %s\\n");\n'
179 ' pCurObj = gpuw;\n'
180 ' pthread_once(&tabOnce, initLayerTable);\n'
181 ' %snextTable.%s;\n'
182 ' printf("Completed layered %s\\n");\n'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700183 ' fflush(stdout);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600184 '%s'
185 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700186 elif "APIDump" in layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600187 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
188 param0_name = proto.params[0].name
189 ret_val = ''
190 stmt = ''
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600191 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700192 create_params = 0 # Num of params at end of function that are created and returned as output values
193 if 'WsiX11CreatePresentableImage' in proto.name:
194 create_params = -2
195 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
196 create_params = -1
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600197 if proto.ret != "XGL_VOID":
198 ret_val = "XGL_RESULT result = "
199 stmt = " return result;\n"
Tobin Ehlis574b0142014-11-12 13:11:15 -0700200 f_open = ''
201 f_close = ''
Tobin Ehlis907a0522014-11-25 16:59:27 -0700202 if "File" in layer:
Tobin Ehlis1eba7792014-11-21 09:35:53 -0700203 file_mode = "a"
204 if 'CreateDevice' in proto.name:
205 file_mode = "w"
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700206 f_open = 'unsigned int tid = pthread_self();\n pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
207 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700208 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700209 else:
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700210 f_open = 'unsigned int tid = pthread_self();\n pthread_mutex_lock( &print_lock );\n '
211 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700212 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700213 print_vals = ', getTIDIndex()'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600214 pindex = 0
215 for p in proto.params:
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700216 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
217 cp = False
218 if 0 != create_params:
219 # If this is any of the N last params of the func, treat as output
220 for y in range(-1, create_params-1, -1):
221 if p.name == proto.params[y].name:
222 cp = True
223 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700224 if no_addr and "%p" == pft:
225 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600226 log_func += '%s = %s, ' % (p.name, pft)
227 print_vals += ', %s' % (pfi)
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600228 # TODO : Just want this to be simple check for params of STRUCT type
229 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 Ehlis0a1e06d2014-11-11 17:28:22 -0700230 if 'Wsi' not in proto.name:
231 cis_param_index.append(pindex)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600232 pindex += 1
233 log_func = log_func.strip(', ')
234 if proto.ret != "XGL_VOID":
235 log_func += ') = %s\\n"'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700236 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600237 else:
238 log_func += ')\\n"'
239 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600240 if len(cis_param_index) > 0:
241 log_func += '\n char *pTmpStr;'
242 for sp_index in cis_param_index:
243 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
244 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
245 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700246 if "File" in layer:
247 if no_addr:
248 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
249 else:
250 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700251 else:
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700252 if no_addr:
253 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
254 else:
255 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700256 log_func += '\n fflush(stdout);'
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600257 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700258 if proto.name == "EnumerateLayers":
259 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
260 funcs.append('%s%s\n'
261 '{\n'
262 ' if (gpu != NULL) {\n'
263 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
264 ' pCurObj = gpuw;\n'
265 ' pthread_once(&tabOnce, initLayerTable);\n'
266 ' %snextTable.%s;\n'
267 ' %s %s %s\n'
268 ' %s'
269 ' } else {\n'
270 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
271 ' return XGL_ERROR_INVALID_POINTER;\n'
272 ' // This layer compatible with all GPUs\n'
273 ' *pOutLayerCount = 1;\n'
274 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
275 ' return XGL_SUCCESS;\n'
276 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700277 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
Jon Ashburn451c16f2014-11-25 11:08:42 -0700278 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600279 funcs.append('%s%s\n'
280 '{\n'
281 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700282 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600283 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700284 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600285 else:
286 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
287 funcs.append('%s%s\n'
288 '{\n'
289 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
290 ' pCurObj = gpuw;\n'
291 ' pthread_once(&tabOnce, initLayerTable);\n'
292 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700293 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600294 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700295 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700296 elif "ObjectTracker" == layer:
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700297 obj_type_mapping = {"XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU", "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
298 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE", "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
299 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY", "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
300 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL", "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
301 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE", "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
302 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER", "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
303 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE", "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
304 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER", "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
305 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW", "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
306 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE", "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
307 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE", "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
308 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE", "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
309 "XGL_OBJECT" : "ll_get_obj_type(object)"}
310
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600311 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
312 param0_name = proto.params[0].name
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700313 p0_type = proto.params[0].ty
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600314 create_line = ''
315 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700316 if 'DbgRegisterMsgCallback' in proto.name:
317 using_line = ' // This layer intercepts callbacks\n'
318 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
319 using_line += ' if (!pNewDbgFuncNode)\n'
320 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
321 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
322 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
323 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
324 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
325 elif 'DbgUnregisterMsgCallback' in proto.name:
326 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
327 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
328 using_line += ' while (pTrav) {\n'
329 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
330 using_line += ' pPrev->pNext = pTrav->pNext;\n'
331 using_line += ' if (pDbgFunctionHead == pTrav)\n'
332 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
333 using_line += ' free(pTrav);\n'
334 using_line += ' break;\n'
335 using_line += ' }\n'
336 using_line += ' pPrev = pTrav;\n'
337 using_line += ' pTrav = pTrav->pNext;\n'
338 using_line += ' }\n'
339 elif 'GlobalOption' in proto.name:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600340 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700341 else:
342 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
343 if 'Create' in proto.name or 'Alloc' in proto.name:
344 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
345 if 'DestroyObject' in proto.name:
346 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
347 using_line = ''
348 else:
349 if 'Destroy' in proto.name or 'Free' in proto.name:
350 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
351 using_line = ''
352 if 'DestroyDevice' in proto.name:
353 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
354 destroy_line += ' char str[1024];\n'
355 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'
356 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
357 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600358 ret_val = ''
359 stmt = ''
360 if proto.ret != "XGL_VOID":
361 ret_val = "XGL_RESULT result = "
362 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700363 if proto.name == "EnumerateLayers":
364 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
365 funcs.append('%s%s\n'
366 '{\n'
367 ' if (gpu != NULL) {\n'
368 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
369 ' %s'
370 ' pCurObj = gpuw;\n'
371 ' pthread_once(&tabOnce, initLayerTable);\n'
372 ' %snextTable.%s;\n'
373 ' %s%s'
374 ' %s'
375 ' } else {\n'
376 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
377 ' return XGL_ERROR_INVALID_POINTER;\n'
378 ' // This layer compatible with all GPUs\n'
379 ' *pOutLayerCount = 1;\n'
380 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
381 ' return XGL_SUCCESS;\n'
382 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700383 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt, layer_name))
Jon Ashburn451c16f2014-11-25 11:08:42 -0700384 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600385 funcs.append('%s%s\n'
386 '{\n'
387 '%s'
388 ' %snextTable.%s;\n'
389 '%s%s'
390 '%s'
391 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
392 else:
393 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
394 funcs.append('%s%s\n'
395 '{\n'
396 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
397 '%s'
398 ' pCurObj = gpuw;\n'
399 ' pthread_once(&tabOnce, initLayerTable);\n'
400 ' %snextTable.%s;\n'
401 '%s%s'
402 '%s'
403 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
404
405 # TODO : Put this code somewhere so it gets called at the end if objects not deleted :
406 # // Report any remaining objects in LL
407 # objNode *pTrav = pObjLLHead;
408 # while (pTrav) {
409 # printf("WARN : %s object %p has not been destroyed.\n", pTrav->objType, pTrav->pObj);
410 # }
411
412 return "\n\n".join(funcs)
413
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700414 def _generate_extensions(self):
415 exts = []
416 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
417 exts.append('{')
418 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
419 exts.append('}')
420 exts.append('')
421 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
422 exts.append('{')
423 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
424 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
425 exts.append(' // Check the count first thing')
426 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
427 exts.append(' if (objCount > maxObjCount) {')
428 exts.append(' char str[1024];')
429 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));')
430 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
431 exts.append(' return XGL_ERROR_INVALID_VALUE;')
432 exts.append(' }')
433 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
434 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
435 exts.append(' if (!pTrav) {')
436 exts.append(' char str[1024];')
437 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);')
438 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
439 exts.append(' return XGL_ERROR_UNKNOWN;')
440 exts.append(' }')
441 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
442 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
443 exts.append(' }')
444 exts.append(' return XGL_SUCCESS;')
445 exts.append('}')
446
447 return "\n".join(exts)
448
449 def _generate_layer_gpa_function(self, prefix="xgl", extensions=[]):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600450 func_body = []
451 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
452 "{\n"
453 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
454 " if (gpu == NULL)\n"
455 " return NULL;\n"
456 " pCurObj = gpuw;\n"
457 " pthread_once(&tabOnce, initLayerTable);\n\n"
458 ' if (!strncmp("xglGetProcAddr", (const char *) funcName, sizeof("xglGetProcAddr")))\n'
459 ' return xglGetProcAddr;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700460 if 0 != len(extensions):
461 for ext_name in extensions:
462 func_body.append(' else if (!strncmp("%s", (const char *) funcName, sizeof("%s")))\n'
463 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600464 for name in xgl.icd_dispatch_table:
465 if name == "GetProcAddr":
466 continue
467 if name == "InitAndEnumerateGpus":
468 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
469 ' return nextTable.%s;' % (prefix, name, prefix, name, name))
470 else:
471 func_body.append(' else if (!strncmp("%s%s", (const char *) funcName, sizeof("%s%s")))\n'
472 ' return %s%s;' % (prefix, name, prefix, name, prefix, name))
473
474 func_body.append(" else {\n"
475 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
476 " if (gpuw->pGPA == NULL)\n"
477 " return NULL;\n"
478 " return gpuw->pGPA(gpuw->nextObject, funcName);\n"
479 " }\n"
480 "}\n")
481 return "\n".join(func_body)
482
483 def _generate_layer_dispatch_table(self, prefix='xgl'):
484 func_body = []
485 func_body.append('static void initLayerTable()\n'
486 '{\n'
487 ' GetProcAddrType fpNextGPA;\n'
488 ' fpNextGPA = pCurObj->pGPA;\n'
489 ' assert(fpNextGPA);\n');
490
491 for name in xgl.icd_dispatch_table:
492 func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n'
493 ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name))
494
495 func_body.append("}\n")
496 return "\n".join(func_body)
497
498class LayerFuncsSubcommand(Subcommand):
499 def generate_header(self):
500 return '#include <xglLayer.h>\n#include "loader.h"'
501
502 def generate_body(self):
503 return self._generate_dispatch_entrypoints("static", True)
504
505class LayerDispatchSubcommand(Subcommand):
506 def generate_header(self):
507 return '#include "layer_wrappers.h"'
508
509 def generate_body(self):
510 return self._generate_layer_dispatch_table()
511
512class GenericLayerSubcommand(Subcommand):
513 def generate_header(self):
514 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'
515
516 def generate_body(self):
517 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700518 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600519 self._generate_layer_gpa_function()]
520
521 return "\n\n".join(body)
522
523class ApiDumpSubcommand(Subcommand):
524 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700525 header_txt = []
526 header_txt.append('#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')
527 header_txt.append('#define MAX_TID 513')
528 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
529 header_txt.append('static uint32_t maxTID = 0;')
530 header_txt.append('// Map actual TID to an index value and return that index')
531 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
532 header_txt.append('static uint32_t getTIDIndex() {')
533 header_txt.append(' pthread_t tid = pthread_self();')
534 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
535 header_txt.append(' if (tid == tidMapping[i])')
536 header_txt.append(' return i;')
537 header_txt.append(' }')
538 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
539 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
540 header_txt.append(' tidMapping[maxTID++] = tid;')
541 header_txt.append(' assert(maxTID < MAX_TID);')
542 header_txt.append(' return retVal;')
543 header_txt.append('}')
544 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600545
546 def generate_body(self):
547 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700548 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600549 self._generate_layer_gpa_function()]
550
551 return "\n\n".join(body)
552
Tobin Ehlis574b0142014-11-12 13:11:15 -0700553class ApiDumpFileSubcommand(Subcommand):
554 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700555 header_txt = []
556 header_txt.append('#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')
557 header_txt.append('#define MAX_TID 513')
558 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
559 header_txt.append('static uint32_t maxTID = 0;')
560 header_txt.append('// Map actual TID to an index value and return that index')
561 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
562 header_txt.append('static uint32_t getTIDIndex() {')
563 header_txt.append(' pthread_t tid = pthread_self();')
564 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
565 header_txt.append(' if (tid == tidMapping[i])')
566 header_txt.append(' return i;')
567 header_txt.append(' }')
568 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
569 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
570 header_txt.append(' tidMapping[maxTID++] = tid;')
571 header_txt.append(' assert(maxTID < MAX_TID);')
572 header_txt.append(' return retVal;')
573 header_txt.append('}')
574 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700575
576 def generate_body(self):
577 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700578 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700579 self._generate_layer_gpa_function()]
580
581 return "\n\n".join(body)
582
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700583class ApiDumpNoAddrSubcommand(Subcommand):
584 def generate_header(self):
585 header_txt = []
586 header_txt.append('#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_no_addr.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')
587 header_txt.append('#define MAX_TID 513')
588 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
589 header_txt.append('static uint32_t maxTID = 0;')
590 header_txt.append('// Map actual TID to an index value and return that index')
591 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
592 header_txt.append('static uint32_t getTIDIndex() {')
593 header_txt.append(' pthread_t tid = pthread_self();')
594 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
595 header_txt.append(' if (tid == tidMapping[i])')
596 header_txt.append(' return i;')
597 header_txt.append(' }')
598 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
599 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
600 header_txt.append(' tidMapping[maxTID++] = tid;')
601 header_txt.append(' assert(maxTID < MAX_TID);')
602 header_txt.append(' return retVal;')
603 header_txt.append('}')
604 return "\n".join(header_txt)
605
606 def generate_body(self):
607 body = [self._generate_layer_dispatch_table(),
608 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
609 self._generate_layer_gpa_function()]
610
611 return "\n\n".join(body)
612
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600613class ObjectTrackerSubcommand(Subcommand):
614 def generate_header(self):
615 header_txt = []
616 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700617 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
618 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
619 header_txt.append('// Ptr to LL of dbg functions')
620 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
621 header_txt.append('// Utility function to handle reporting')
622 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
623 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
624 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
625 header_txt.append(' XGL_BASE_OBJECT srcObject,')
626 header_txt.append(' XGL_SIZE location,')
627 header_txt.append(' XGL_INT msgCode,')
628 header_txt.append(' const XGL_CHAR* pLayerPrefix,')
629 header_txt.append(' const XGL_CHAR* pMsg)')
630 header_txt.append('{')
631 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
632 header_txt.append(' if (pTrav) {')
633 header_txt.append(' while (pTrav) {')
634 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
635 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600636 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600637 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700638 header_txt.append(' else {')
639 header_txt.append(' switch (msgType) {')
640 header_txt.append(' case XGL_DBG_MSG_ERROR:')
641 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
642 header_txt.append(' break;')
643 header_txt.append(' case XGL_DBG_MSG_WARNING:')
644 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
645 header_txt.append(' break;')
646 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
647 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
648 header_txt.append(' break;')
649 header_txt.append(' default:')
650 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
651 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600652 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700653 header_txt.append(' }')
654 header_txt.append('}')
655 header_txt.append('// We maintain a "Global" list which links every object and a')
656 header_txt.append('// per-Object list which just links objects of a given type')
657 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
658 header_txt.append('typedef struct _objNode {')
659 header_txt.append(' OBJTRACK_NODE obj;')
660 header_txt.append(' struct _objNode *pNextObj;')
661 header_txt.append(' struct _objNode *pNextGlobal;')
662 header_txt.append('} objNode;')
663 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
664 header_txt.append('static objNode *pGlobalHead = NULL;')
665 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
666 header_txt.append('static uint64_t numTotalObjs = 0;')
667 header_txt.append('// Debug function to print global list and each individual object list')
668 header_txt.append('static void ll_print_lists()')
669 header_txt.append('{')
670 header_txt.append(' objNode* pTrav = pGlobalHead;')
671 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
672 header_txt.append(' while (pTrav) {')
673 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);')
674 header_txt.append(' pTrav = pTrav->pNextGlobal;')
675 header_txt.append(' }')
676 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
677 header_txt.append(' pTrav = pObjectHead[i];')
678 header_txt.append(' if (pTrav) {')
679 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
680 header_txt.append(' while (pTrav) {')
681 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);')
682 header_txt.append(' pTrav = pTrav->pNextObj;')
683 header_txt.append(' }')
684 header_txt.append(' }')
685 header_txt.append(' }')
686 header_txt.append('}')
687 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
688 header_txt.append(' char str[1024];')
689 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
690 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
691 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
692 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
693 header_txt.append(' pNewObjNode->obj.objType = objType;')
694 header_txt.append(' pNewObjNode->obj.numUses = 0;')
695 header_txt.append(' // insert at front of global list')
696 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
697 header_txt.append(' pGlobalHead = pNewObjNode;')
698 header_txt.append(' // insert at front of object list')
699 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
700 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
701 header_txt.append(' // increment obj counts')
702 header_txt.append(' numObjs[objType]++;')
703 header_txt.append(' numTotalObjs++;')
704 header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_XGL_OBJECT_TYPE(objType));')
705 header_txt.append(' //ll_print_lists();')
706 header_txt.append('}')
707 header_txt.append('// Traverse global list and return type for given object')
708 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
709 header_txt.append(' objNode *pTrav = pGlobalHead;')
710 header_txt.append(' while (pTrav) {')
711 header_txt.append(' if (pTrav->obj.pObj == object)')
712 header_txt.append(' return pTrav->obj.objType;')
713 header_txt.append(' pTrav = pTrav->pNextGlobal;')
714 header_txt.append(' }')
715 header_txt.append(' char str[1024];')
716 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
717 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
718 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
719 header_txt.append('}')
720 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
721 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
722 header_txt.append(' while (pTrav) {')
723 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
724 header_txt.append(' return pTrav->obj.numUses;')
725 header_txt.append(' }')
726 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600727 header_txt.append(' }')
728 header_txt.append(' return 0;')
729 header_txt.append('}')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700730 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
731 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600732 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700733 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
734 header_txt.append(' pTrav->obj.numUses++;')
735 header_txt.append(' char str[1024];')
736 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);')
737 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
738 header_txt.append(' return;')
739 header_txt.append(' }')
740 header_txt.append(' pTrav = pTrav->pNextObj;')
741 header_txt.append(' }')
742 header_txt.append(' // If we do not find obj, insert it and then increment count')
743 header_txt.append(' char str[1024];')
744 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));')
745 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
746 header_txt.append('')
747 header_txt.append(' ll_insert_obj(pObj, objType);')
748 header_txt.append(' ll_increment_use_count(pObj, objType);')
749 header_txt.append('}')
750 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
751 header_txt.append('// Type from global list w/ ll_destroy_obj()')
752 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
753 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
754 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
755 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
756 header_txt.append(' while (pTrav) {')
757 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
758 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
759 header_txt.append(' // update HEAD of Obj list as needed')
760 header_txt.append(' if (pObjectHead[objType] == pTrav)')
761 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
762 header_txt.append(' assert(numObjs[objType] > 0);')
763 header_txt.append(' numObjs[objType]--;')
764 header_txt.append(' char str[1024];')
765 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
766 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600767 header_txt.append(' return;')
768 header_txt.append(' }')
769 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700770 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600771 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700772 header_txt.append(' char str[1024];')
773 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));')
774 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
775 header_txt.append('}')
776 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
777 header_txt.append('// remove obj from global list')
778 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
779 header_txt.append(' objNode *pTrav = pGlobalHead;')
780 header_txt.append(' objNode *pPrev = pGlobalHead;')
781 header_txt.append(' while (pTrav) {')
782 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
783 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
784 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
785 header_txt.append(' // update HEAD of global list if needed')
786 header_txt.append(' if (pGlobalHead == pTrav)')
787 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
788 header_txt.append(' free(pTrav);')
789 header_txt.append(' assert(numTotalObjs > 0);')
790 header_txt.append(' numTotalObjs--;')
791 header_txt.append(' char str[1024];')
792 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));')
793 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
794 header_txt.append(' return;')
795 header_txt.append(' }')
796 header_txt.append(' pPrev = pTrav;')
797 header_txt.append(' pTrav = pTrav->pNextGlobal;')
798 header_txt.append(' }')
799 header_txt.append(' char str[1024];')
800 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
801 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_DESTROY_OBJECT_FAILED, "OBJTRACK", str);')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600802 header_txt.append('}')
803
804 return "\n".join(header_txt)
805
806 def generate_body(self):
807 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700808 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700809 self._generate_extensions(),
810 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600811
812 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700813
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600814def main():
815 subcommands = {
816 "layer-funcs" : LayerFuncsSubcommand,
817 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -0700818 "Generic" : GenericLayerSubcommand,
819 "ApiDump" : ApiDumpSubcommand,
820 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700821 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -0700822 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600823 }
824
825 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
826 print("Usage: %s <subcommand> [options]" % sys.argv[0])
827 print
828 print("Available sucommands are: %s" % " ".join(subcommands))
829 exit(1)
830
831 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
832 subcmd.run()
833
834if __name__ == "__main__":
835 main()