blob: d19a63f8c837dabe15f2995054b65ac9b9048006 [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
Chia-I Wuec30dcb2015-01-01 08:46:31 +080035 self.headers = xgl.headers
36 self.protos = xgl.protos
Tobin Ehlis92dbf802014-10-22 09:06:33 -060037
38 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060039 print(self.generate())
40
41 def generate(self):
42 copyright = self.generate_copyright()
43 header = self.generate_header()
44 body = self.generate_body()
45 footer = self.generate_footer()
46
47 contents = []
48 if copyright:
49 contents.append(copyright)
50 if header:
51 contents.append(header)
52 if body:
53 contents.append(body)
54 if footer:
55 contents.append(footer)
56
57 return "\n\n".join(contents)
58
59 def generate_copyright(self):
60 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
61
62/*
63 * XGL
64 *
65 * Copyright (C) 2014 LunarG, Inc.
66 *
67 * Permission is hereby granted, free of charge, to any person obtaining a
68 * copy of this software and associated documentation files (the "Software"),
69 * to deal in the Software without restriction, including without limitation
70 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
71 * and/or sell copies of the Software, and to permit persons to whom the
72 * Software is furnished to do so, subject to the following conditions:
73 *
74 * The above copyright notice and this permission notice shall be included
75 * in all copies or substantial portions of the Software.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
82 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
83 * DEALINGS IN THE SOFTWARE.
84 */"""
85
86 def generate_header(self):
87 return "\n".join(["#include <" + h + ">" for h in self.headers])
88
89 def generate_body(self):
90 pass
91
92 def generate_footer(self):
93 pass
94
95 # Return set of printf '%' qualifier and input to that qualifier
Tobin Ehlis434db7c2015-01-10 12:42:41 -070096 def _get_printf_params(self, xgl_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060097 # TODO : Need ENUM and STRUCT checks here
98 if "_TYPE" in xgl_type: # TODO : This should be generic ENUM check
99 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
100 if "XGL_CHAR*" == xgl_type:
101 return ("%s", name)
102 if "UINT64" in xgl_type:
103 if '*' in xgl_type:
104 return ("%lu", "*%s" % name)
105 return ("%lu", name)
Chia-I Wu54ed0792014-12-27 14:14:50 +0800106 if "SIZE" in xgl_type:
107 if '*' in xgl_type:
108 return ("%zu", "*%s" % name)
109 return ("%zu", name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600110 if "FLOAT" in xgl_type:
111 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700112 if cpp:
113 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600114 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
115 return ("%f", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700116 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600117 return ("%u", name)
Chia-I Wu54ed0792014-12-27 14:14:50 +0800118 if True in [t in xgl_type for t in ["INT", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600119 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700120 if cpp:
121 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600122 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
123 if '*' in xgl_type:
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700124 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600125 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700126 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700127 if "XGL_FORMAT" == xgl_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700128 if cpp:
129 return ("%p", "&%s" % name)
130 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_XGL_CHANNEL_FORMAT(%s.channelFormat), string_XGL_NUM_FORMAT(%s.numericFormat)" % (name, name))
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700131 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600132 return ("%p", "(void*)*%s" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700133 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600134
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700135 def _gen_layer_dbg_callback_header(self):
136 cbh_body = []
137 cbh_body.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
138 cbh_body.append('// Utility function to handle reporting')
139 cbh_body.append('// If callbacks are enabled, use them, otherwise use printf')
140 cbh_body.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
141 cbh_body.append(' XGL_VALIDATION_LEVEL validationLevel,')
142 cbh_body.append(' XGL_BASE_OBJECT srcObject,')
143 cbh_body.append(' XGL_SIZE location,')
144 cbh_body.append(' XGL_INT msgCode,')
145 cbh_body.append(' const XGL_CHAR* pLayerPrefix,')
146 cbh_body.append(' const XGL_CHAR* pMsg)')
147 cbh_body.append('{')
148 cbh_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
149 cbh_body.append(' if (pTrav) {')
150 cbh_body.append(' while (pTrav) {')
151 cbh_body.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
152 cbh_body.append(' pTrav = pTrav->pNext;')
153 cbh_body.append(' }')
154 cbh_body.append(' }')
155 cbh_body.append(' else {')
156 cbh_body.append(' switch (msgType) {')
157 cbh_body.append(' case XGL_DBG_MSG_ERROR:')
158 cbh_body.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
159 cbh_body.append(' break;')
160 cbh_body.append(' case XGL_DBG_MSG_WARNING:')
161 cbh_body.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
162 cbh_body.append(' break;')
163 cbh_body.append(' case XGL_DBG_MSG_PERF_WARNING:')
164 cbh_body.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
165 cbh_body.append(' break;')
166 cbh_body.append(' default:')
167 cbh_body.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
168 cbh_body.append(' break;')
169 cbh_body.append(' }')
170 cbh_body.append(' }')
171 cbh_body.append('}')
172 return "\n".join(cbh_body)
173
174 def _gen_layer_dbg_callback_register(self):
175 r_body = []
176 r_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)')
177 r_body.append('{')
178 r_body.append(' // This layer intercepts callbacks')
179 r_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));')
180 r_body.append(' if (!pNewDbgFuncNode)')
181 r_body.append(' return XGL_ERROR_OUT_OF_MEMORY;')
182 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
183 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
184 r_body.append(' pNewDbgFuncNode->pNext = pDbgFunctionHead;')
185 r_body.append(' pDbgFunctionHead = pNewDbgFuncNode;')
186 r_body.append(' XGL_RESULT result = nextTable.DbgRegisterMsgCallback(pfnMsgCallback, pUserData);')
187 r_body.append(' return result;')
188 r_body.append('}')
189 return "\n".join(r_body)
190
191 def _gen_layer_dbg_callback_unregister(self):
192 ur_body = []
193 ur_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
194 ur_body.append('{')
195 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
196 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
197 ur_body.append(' while (pTrav) {')
198 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
199 ur_body.append(' pPrev->pNext = pTrav->pNext;')
200 ur_body.append(' if (pDbgFunctionHead == pTrav)')
201 ur_body.append(' pDbgFunctionHead = pTrav->pNext;')
202 ur_body.append(' free(pTrav);')
203 ur_body.append(' break;')
204 ur_body.append(' }')
205 ur_body.append(' pPrev = pTrav;')
206 ur_body.append(' pTrav = pTrav->pNext;')
207 ur_body.append(' }')
208 ur_body.append(' XGL_RESULT result = nextTable.DbgUnregisterMsgCallback(pfnMsgCallback);')
209 ur_body.append(' return result;')
210 ur_body.append('}')
211 return "\n".join(ur_body)
212
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700213 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600214 if qual:
215 qual += " "
216
Tobin Ehlis907a0522014-11-25 16:59:27 -0700217 layer_name = layer
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700218 if no_addr:
219 layer_name = "%sNoAddr" % layer
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700220 if 'Cpp' in layer_name:
221 layer_name = "APIDumpNoAddrCpp"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600222 funcs = []
223 for proto in self.protos:
224 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlis907a0522014-11-25 16:59:27 -0700225 if "Generic" == layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600226 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
227 param0_name = proto.params[0].name
228 ret_val = ''
229 stmt = ''
230 if proto.ret != "XGL_VOID":
231 ret_val = "XGL_RESULT result = "
232 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700233 if proto.name == "EnumerateLayers":
234 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
235 funcs.append('%s%s\n'
236 '{\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700237 ' char str[1024];\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700238 ' if (gpu != NULL) {\n'
239 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700240 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-12-17 12:08:37 -0700241 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700242 ' pCurObj = gpuw;\n'
243 ' pthread_once(&tabOnce, initLayerTable);\n'
244 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700245 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-12-17 12:08:37 -0700246 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700247 ' fflush(stdout);\n'
248 ' %s'
249 ' } else {\n'
250 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
251 ' return XGL_ERROR_INVALID_POINTER;\n'
252 ' // This layer compatible with all GPUs\n'
253 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800254 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700255 ' return XGL_SUCCESS;\n'
256 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700257 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700258 elif 'DbgRegisterMsgCallback' == proto.name:
259 funcs.append(self._gen_layer_dbg_callback_register())
260 elif 'DbgUnregisterMsgCallback' == proto.name:
261 funcs.append(self._gen_layer_dbg_callback_unregister())
Jon Ashburn451c16f2014-11-25 11:08:42 -0700262 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600263 funcs.append('%s%s\n'
264 '{\n'
265 ' %snextTable.%s;\n'
266 '%s'
267 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
268 else:
269 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
270 funcs.append('%s%s\n'
271 '{\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700272 ' char str[1024];'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600273 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700274 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-12-17 12:08:37 -0700275 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600276 ' pCurObj = gpuw;\n'
277 ' pthread_once(&tabOnce, initLayerTable);\n'
278 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700279 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-12-17 12:08:37 -0700280 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700281 ' fflush(stdout);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600282 '%s'
283 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700284 elif "APIDumpCpp" in layer:
285 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
286 param0_name = proto.params[0].name
287 ret_val = ''
288 stmt = ''
289 cis_param_index = [] # Store list of indices when func has struct params
290 create_params = 0 # Num of params at end of function that are created and returned as output values
291 if 'WsiX11CreatePresentableImage' in proto.name:
292 create_params = -2
293 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
294 create_params = -1
295 if proto.ret != "XGL_VOID":
296 ret_val = "XGL_RESULT result = "
297 stmt = " return result;\n"
298 f_open = ''
299 f_close = ''
300 if "File" in layer:
301 file_mode = "a"
302 if 'CreateDevice' in proto.name:
303 file_mode = "w"
304 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
305 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
306 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
307 else:
308 f_open = 'pthread_mutex_lock( &print_lock );\n '
309 log_func = 'cout << "t{" << getTIDIndex() << "} xgl%s(' % proto.name
310 f_close = '\n pthread_mutex_unlock( &print_lock );'
311 pindex = 0
312 for p in proto.params:
313 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
314 cp = False
315 if 0 != create_params:
316 # If this is any of the N last params of the func, treat as output
317 for y in range(-1, create_params-1, -1):
318 if p.name == proto.params[y].name:
319 cp = True
320 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
321 if no_addr and "%p" == pft:
322 (pft, pfi) = ("%s", '"addr"')
323 log_func += '%s = " << %s << ", ' % (p.name, pfi)
324 #print_vals += ', %s' % (pfi)
325 # TODO : Just want this to be simple check for params of STRUCT type
326 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']]):
327 if 'Wsi' not in proto.name:
328 cis_param_index.append(pindex)
329 pindex += 1
330 log_func = log_func.strip(', ')
331 if proto.ret != "XGL_VOID":
332 log_func += ') = " << string_XGL_RESULT(result) << "\\n"'
333 #print_vals += ', string_XGL_RESULT(result)'
334 else:
335 log_func += ')\\n"'
336 log_func += ';'
337 if len(cis_param_index) > 0:
338 log_func += '\n string tmp_str;'
339 for sp_index in cis_param_index:
340 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
341 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
342 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
343 if "File" in layer:
344 if no_addr:
345 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
346 else:
347 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
348 else:
349 if no_addr:
350 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
351 log_func += '\n cout << " %s (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
352 else:
353 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
354 log_func += '\n cout << " %s (" << %s << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, proto.params[sp_index].name)
355 #log_func += '\n fflush(stdout);'
356 log_func += '\n }'
357 if proto.name == "EnumerateLayers":
358 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
359 funcs.append('%s%s\n'
360 '{\n'
361 ' if (gpu != NULL) {\n'
362 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
363 ' pCurObj = gpuw;\n'
364 ' pthread_once(&tabOnce, initLayerTable);\n'
365 ' %snextTable.%s;\n'
366 ' %s %s %s\n'
367 ' %s'
368 ' } else {\n'
369 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
370 ' return XGL_ERROR_INVALID_POINTER;\n'
371 ' // This layer compatible with all GPUs\n'
372 ' *pOutLayerCount = 1;\n'
373 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
374 ' return XGL_SUCCESS;\n'
375 ' }\n'
376 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
377 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
378 funcs.append('%s%s\n'
379 '{\n'
380 ' %snextTable.%s;\n'
381 ' %s%s%s\n'
382 '%s'
383 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
384 else:
385 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
386 funcs.append('%s%s\n'
387 '{\n'
388 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
389 ' pCurObj = gpuw;\n'
390 ' pthread_once(&tabOnce, initLayerTable);\n'
391 ' %snextTable.%s;\n'
392 ' %s%s%s\n'
393 '%s'
394 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700395 elif "APIDump" in layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600396 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
397 param0_name = proto.params[0].name
398 ret_val = ''
399 stmt = ''
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600400 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700401 create_params = 0 # Num of params at end of function that are created and returned as output values
402 if 'WsiX11CreatePresentableImage' in proto.name:
403 create_params = -2
404 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
405 create_params = -1
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600406 if proto.ret != "XGL_VOID":
407 ret_val = "XGL_RESULT result = "
408 stmt = " return result;\n"
Tobin Ehlis574b0142014-11-12 13:11:15 -0700409 f_open = ''
410 f_close = ''
Tobin Ehlis907a0522014-11-25 16:59:27 -0700411 if "File" in layer:
Tobin Ehlis1eba7792014-11-21 09:35:53 -0700412 file_mode = "a"
413 if 'CreateDevice' in proto.name:
414 file_mode = "w"
Chia-I Wu81f46672014-12-16 00:36:58 +0800415 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700416 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700417 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700418 else:
Chia-I Wu81f46672014-12-16 00:36:58 +0800419 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700420 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700421 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700422 print_vals = ', getTIDIndex()'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600423 pindex = 0
424 for p in proto.params:
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700425 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
426 cp = False
427 if 0 != create_params:
428 # If this is any of the N last params of the func, treat as output
429 for y in range(-1, create_params-1, -1):
430 if p.name == proto.params[y].name:
431 cp = True
432 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700433 if no_addr and "%p" == pft:
434 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600435 log_func += '%s = %s, ' % (p.name, pft)
436 print_vals += ', %s' % (pfi)
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600437 # TODO : Just want this to be simple check for params of STRUCT type
438 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 -0700439 if 'Wsi' not in proto.name:
440 cis_param_index.append(pindex)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600441 pindex += 1
442 log_func = log_func.strip(', ')
443 if proto.ret != "XGL_VOID":
444 log_func += ') = %s\\n"'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700445 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600446 else:
447 log_func += ')\\n"'
448 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600449 if len(cis_param_index) > 0:
450 log_func += '\n char *pTmpStr;'
451 for sp_index in cis_param_index:
452 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
453 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
454 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700455 if "File" in layer:
456 if no_addr:
457 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
458 else:
459 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 -0700460 else:
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700461 if no_addr:
462 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
463 else:
464 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 -0700465 log_func += '\n fflush(stdout);'
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600466 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700467 if proto.name == "EnumerateLayers":
468 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
469 funcs.append('%s%s\n'
470 '{\n'
471 ' if (gpu != NULL) {\n'
472 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
473 ' pCurObj = gpuw;\n'
474 ' pthread_once(&tabOnce, initLayerTable);\n'
475 ' %snextTable.%s;\n'
476 ' %s %s %s\n'
477 ' %s'
478 ' } else {\n'
479 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
480 ' return XGL_ERROR_INVALID_POINTER;\n'
481 ' // This layer compatible with all GPUs\n'
482 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800483 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700484 ' return XGL_SUCCESS;\n'
485 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700486 '}' % (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 -0700487 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600488 funcs.append('%s%s\n'
489 '{\n'
490 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700491 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600492 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700493 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600494 else:
495 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
496 funcs.append('%s%s\n'
497 '{\n'
498 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
499 ' pCurObj = gpuw;\n'
500 ' pthread_once(&tabOnce, initLayerTable);\n'
501 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700502 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600503 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700504 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700505 elif "ObjectTracker" == layer:
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800506 obj_type_mapping = {
507 "XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU",
508 "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
509 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE",
510 "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
511 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY",
512 "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
513 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL",
514 "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
515 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE",
516 "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
517 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER",
518 "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
519 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE",
520 "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
521 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER",
522 "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
523 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW",
524 "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
525 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE",
526 "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
527 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE",
528 "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
529 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE",
530 "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
531 "XGL_OBJECT" : "ll_get_obj_type(object)"
532 }
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700533
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600534 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
535 param0_name = proto.params[0].name
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700536 p0_type = proto.params[0].ty
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600537 create_line = ''
538 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700539 if 'DbgRegisterMsgCallback' in proto.name:
540 using_line = ' // This layer intercepts callbacks\n'
541 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
542 using_line += ' if (!pNewDbgFuncNode)\n'
543 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
544 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
545 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
546 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
547 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
548 elif 'DbgUnregisterMsgCallback' in proto.name:
549 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
550 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
551 using_line += ' while (pTrav) {\n'
552 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
553 using_line += ' pPrev->pNext = pTrav->pNext;\n'
554 using_line += ' if (pDbgFunctionHead == pTrav)\n'
555 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
556 using_line += ' free(pTrav);\n'
557 using_line += ' break;\n'
558 using_line += ' }\n'
559 using_line += ' pPrev = pTrav;\n'
560 using_line += ' pTrav = pTrav->pNext;\n'
561 using_line += ' }\n'
562 elif 'GlobalOption' in proto.name:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600563 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700564 else:
565 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
566 if 'Create' in proto.name or 'Alloc' in proto.name:
567 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
568 if 'DestroyObject' in proto.name:
569 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
570 using_line = ''
571 else:
572 if 'Destroy' in proto.name or 'Free' in proto.name:
573 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
574 using_line = ''
575 if 'DestroyDevice' in proto.name:
576 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
577 destroy_line += ' char str[1024];\n'
578 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'
579 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
580 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600581 ret_val = ''
582 stmt = ''
583 if proto.ret != "XGL_VOID":
584 ret_val = "XGL_RESULT result = "
585 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700586 if proto.name == "EnumerateLayers":
587 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
588 funcs.append('%s%s\n'
589 '{\n'
590 ' if (gpu != NULL) {\n'
591 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
592 ' %s'
593 ' pCurObj = gpuw;\n'
594 ' pthread_once(&tabOnce, initLayerTable);\n'
595 ' %snextTable.%s;\n'
596 ' %s%s'
597 ' %s'
598 ' } else {\n'
599 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
600 ' return XGL_ERROR_INVALID_POINTER;\n'
601 ' // This layer compatible with all GPUs\n'
602 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800603 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700604 ' return XGL_SUCCESS;\n'
605 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700606 '}' % (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 -0700607 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600608 funcs.append('%s%s\n'
609 '{\n'
610 '%s'
611 ' %snextTable.%s;\n'
612 '%s%s'
613 '%s'
614 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
615 else:
616 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
617 funcs.append('%s%s\n'
618 '{\n'
619 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
620 '%s'
621 ' pCurObj = gpuw;\n'
622 ' pthread_once(&tabOnce, initLayerTable);\n'
623 ' %snextTable.%s;\n'
624 '%s%s'
625 '%s'
626 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
627
628 # TODO : Put this code somewhere so it gets called at the end if objects not deleted :
629 # // Report any remaining objects in LL
630 # objNode *pTrav = pObjLLHead;
631 # while (pTrav) {
632 # printf("WARN : %s object %p has not been destroyed.\n", pTrav->objType, pTrav->pObj);
633 # }
634
635 return "\n\n".join(funcs)
636
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700637 def _generate_extensions(self):
638 exts = []
639 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
640 exts.append('{')
641 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
642 exts.append('}')
643 exts.append('')
644 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
645 exts.append('{')
646 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
647 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
648 exts.append(' // Check the count first thing')
649 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
650 exts.append(' if (objCount > maxObjCount) {')
651 exts.append(' char str[1024];')
652 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));')
653 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
654 exts.append(' return XGL_ERROR_INVALID_VALUE;')
655 exts.append(' }')
656 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
657 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
658 exts.append(' if (!pTrav) {')
659 exts.append(' char str[1024];')
660 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);')
661 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
662 exts.append(' return XGL_ERROR_UNKNOWN;')
663 exts.append(' }')
664 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
665 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
666 exts.append(' }')
667 exts.append(' return XGL_SUCCESS;')
668 exts.append('}')
669
670 return "\n".join(exts)
671
Chia-I Wu706533e2015-01-05 13:18:57 +0800672 def _generate_layer_gpa_function(self, extensions=[]):
673 func_body = ["#include \"xgl_generic_intercept_proc_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600674 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
675 "{\n"
676 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800677 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600678 " if (gpu == NULL)\n"
679 " return NULL;\n"
680 " pCurObj = gpuw;\n"
681 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800682 " addr = layer_intercept_proc(funcName);\n"
683 " if (addr)\n"
684 " return addr;")
685
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700686 if 0 != len(extensions):
687 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800688 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700689 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600690 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600691 " if (gpuw->pGPA == NULL)\n"
692 " return NULL;\n"
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700693 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600694 " }\n"
695 "}\n")
696 return "\n".join(func_body)
697
698 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800699 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600700 func_body.append('static void initLayerTable()\n'
701 '{\n'
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600702 ' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600703 ' fpNextGPA = pCurObj->pGPA;\n'
704 ' assert(fpNextGPA);\n');
705
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800706 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600707 func_body.append("}\n")
708 return "\n".join(func_body)
709
710class LayerFuncsSubcommand(Subcommand):
711 def generate_header(self):
712 return '#include <xglLayer.h>\n#include "loader.h"'
713
714 def generate_body(self):
715 return self._generate_dispatch_entrypoints("static", True)
716
717class LayerDispatchSubcommand(Subcommand):
718 def generate_header(self):
719 return '#include "layer_wrappers.h"'
720
721 def generate_body(self):
722 return self._generate_layer_dispatch_table()
723
724class GenericLayerSubcommand(Subcommand):
725 def generate_header(self):
726 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'
727
728 def generate_body(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700729 body = [self._gen_layer_dbg_callback_header(),
730 self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700731 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600732 self._generate_layer_gpa_function()]
733
734 return "\n\n".join(body)
735
736class ApiDumpSubcommand(Subcommand):
737 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700738 header_txt = []
739 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')
740 header_txt.append('#define MAX_TID 513')
741 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
742 header_txt.append('static uint32_t maxTID = 0;')
743 header_txt.append('// Map actual TID to an index value and return that index')
744 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
745 header_txt.append('static uint32_t getTIDIndex() {')
746 header_txt.append(' pthread_t tid = pthread_self();')
747 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
748 header_txt.append(' if (tid == tidMapping[i])')
749 header_txt.append(' return i;')
750 header_txt.append(' }')
751 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
752 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
753 header_txt.append(' tidMapping[maxTID++] = tid;')
754 header_txt.append(' assert(maxTID < MAX_TID);')
755 header_txt.append(' return retVal;')
756 header_txt.append('}')
757 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600758
759 def generate_body(self):
760 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700761 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600762 self._generate_layer_gpa_function()]
763
764 return "\n\n".join(body)
765
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700766class ApiDumpCppSubcommand(Subcommand):
767 def generate_header(self):
768 header_txt = []
769 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_cpp.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')
770 header_txt.append('#define MAX_TID 513')
771 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
772 header_txt.append('static uint32_t maxTID = 0;')
773 header_txt.append('// Map actual TID to an index value and return that index')
774 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
775 header_txt.append('static uint32_t getTIDIndex() {')
776 header_txt.append(' pthread_t tid = pthread_self();')
777 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
778 header_txt.append(' if (tid == tidMapping[i])')
779 header_txt.append(' return i;')
780 header_txt.append(' }')
781 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
782 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
783 header_txt.append(' tidMapping[maxTID++] = tid;')
784 header_txt.append(' assert(maxTID < MAX_TID);')
785 header_txt.append(' return retVal;')
786 header_txt.append('}')
787 return "\n".join(header_txt)
788
789 def generate_body(self):
790 body = [self._generate_layer_dispatch_table(),
791 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp"),
792 self._generate_layer_gpa_function()]
793
794 return "\n\n".join(body)
795
Tobin Ehlis574b0142014-11-12 13:11:15 -0700796class ApiDumpFileSubcommand(Subcommand):
797 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700798 header_txt = []
799 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')
800 header_txt.append('#define MAX_TID 513')
801 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
802 header_txt.append('static uint32_t maxTID = 0;')
803 header_txt.append('// Map actual TID to an index value and return that index')
804 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
805 header_txt.append('static uint32_t getTIDIndex() {')
806 header_txt.append(' pthread_t tid = pthread_self();')
807 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
808 header_txt.append(' if (tid == tidMapping[i])')
809 header_txt.append(' return i;')
810 header_txt.append(' }')
811 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
812 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
813 header_txt.append(' tidMapping[maxTID++] = tid;')
814 header_txt.append(' assert(maxTID < MAX_TID);')
815 header_txt.append(' return retVal;')
816 header_txt.append('}')
817 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700818
819 def generate_body(self):
820 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700821 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700822 self._generate_layer_gpa_function()]
823
824 return "\n\n".join(body)
825
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700826class ApiDumpNoAddrSubcommand(Subcommand):
827 def generate_header(self):
828 header_txt = []
829 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')
830 header_txt.append('#define MAX_TID 513')
831 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
832 header_txt.append('static uint32_t maxTID = 0;')
833 header_txt.append('// Map actual TID to an index value and return that index')
834 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
835 header_txt.append('static uint32_t getTIDIndex() {')
836 header_txt.append(' pthread_t tid = pthread_self();')
837 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
838 header_txt.append(' if (tid == tidMapping[i])')
839 header_txt.append(' return i;')
840 header_txt.append(' }')
841 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
842 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
843 header_txt.append(' tidMapping[maxTID++] = tid;')
844 header_txt.append(' assert(maxTID < MAX_TID);')
845 header_txt.append(' return retVal;')
846 header_txt.append('}')
847 return "\n".join(header_txt)
848
849 def generate_body(self):
850 body = [self._generate_layer_dispatch_table(),
851 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
852 self._generate_layer_gpa_function()]
853
854 return "\n\n".join(body)
855
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700856class ApiDumpNoAddrCppSubcommand(Subcommand):
857 def generate_header(self):
858 header_txt = []
859 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_cpp.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')
860 header_txt.append('#define MAX_TID 513')
861 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
862 header_txt.append('static uint32_t maxTID = 0;')
863 header_txt.append('// Map actual TID to an index value and return that index')
864 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
865 header_txt.append('static uint32_t getTIDIndex() {')
866 header_txt.append(' pthread_t tid = pthread_self();')
867 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
868 header_txt.append(' if (tid == tidMapping[i])')
869 header_txt.append(' return i;')
870 header_txt.append(' }')
871 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
872 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
873 header_txt.append(' tidMapping[maxTID++] = tid;')
874 header_txt.append(' assert(maxTID < MAX_TID);')
875 header_txt.append(' return retVal;')
876 header_txt.append('}')
877 return "\n".join(header_txt)
878
879 def generate_body(self):
880 body = [self._generate_layer_dispatch_table(),
881 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp", True),
882 self._generate_layer_gpa_function()]
883
884 return "\n\n".join(body)
885
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600886class ObjectTrackerSubcommand(Subcommand):
887 def generate_header(self):
888 header_txt = []
889 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 -0700890 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
891 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
892 header_txt.append('// Ptr to LL of dbg functions')
893 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
894 header_txt.append('// Utility function to handle reporting')
895 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
896 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
897 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
898 header_txt.append(' XGL_BASE_OBJECT srcObject,')
899 header_txt.append(' XGL_SIZE location,')
900 header_txt.append(' XGL_INT msgCode,')
Chia-I Wua837c522014-12-16 10:47:33 +0800901 header_txt.append(' const char* pLayerPrefix,')
902 header_txt.append(' const char* pMsg)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700903 header_txt.append('{')
904 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
905 header_txt.append(' if (pTrav) {')
906 header_txt.append(' while (pTrav) {')
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800907 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700908 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600909 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600910 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700911 header_txt.append(' else {')
912 header_txt.append(' switch (msgType) {')
913 header_txt.append(' case XGL_DBG_MSG_ERROR:')
914 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
915 header_txt.append(' break;')
916 header_txt.append(' case XGL_DBG_MSG_WARNING:')
917 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
918 header_txt.append(' break;')
919 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
920 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
921 header_txt.append(' break;')
922 header_txt.append(' default:')
923 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
924 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600925 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700926 header_txt.append(' }')
927 header_txt.append('}')
928 header_txt.append('// We maintain a "Global" list which links every object and a')
929 header_txt.append('// per-Object list which just links objects of a given type')
930 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
931 header_txt.append('typedef struct _objNode {')
932 header_txt.append(' OBJTRACK_NODE obj;')
933 header_txt.append(' struct _objNode *pNextObj;')
934 header_txt.append(' struct _objNode *pNextGlobal;')
935 header_txt.append('} objNode;')
936 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
937 header_txt.append('static objNode *pGlobalHead = NULL;')
938 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
939 header_txt.append('static uint64_t numTotalObjs = 0;')
940 header_txt.append('// Debug function to print global list and each individual object list')
941 header_txt.append('static void ll_print_lists()')
942 header_txt.append('{')
943 header_txt.append(' objNode* pTrav = pGlobalHead;')
944 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
945 header_txt.append(' while (pTrav) {')
946 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);')
947 header_txt.append(' pTrav = pTrav->pNextGlobal;')
948 header_txt.append(' }')
949 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
950 header_txt.append(' pTrav = pObjectHead[i];')
951 header_txt.append(' if (pTrav) {')
952 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
953 header_txt.append(' while (pTrav) {')
954 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);')
955 header_txt.append(' pTrav = pTrav->pNextObj;')
956 header_txt.append(' }')
957 header_txt.append(' }')
958 header_txt.append(' }')
959 header_txt.append('}')
960 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
961 header_txt.append(' char str[1024];')
962 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
963 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
964 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
965 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
966 header_txt.append(' pNewObjNode->obj.objType = objType;')
967 header_txt.append(' pNewObjNode->obj.numUses = 0;')
968 header_txt.append(' // insert at front of global list')
969 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
970 header_txt.append(' pGlobalHead = pNewObjNode;')
971 header_txt.append(' // insert at front of object list')
972 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
973 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
974 header_txt.append(' // increment obj counts')
975 header_txt.append(' numObjs[objType]++;')
976 header_txt.append(' numTotalObjs++;')
977 header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_XGL_OBJECT_TYPE(objType));')
Chia-I Wudf142a32014-12-16 11:02:06 +0800978 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700979 header_txt.append('}')
980 header_txt.append('// Traverse global list and return type for given object')
981 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
982 header_txt.append(' objNode *pTrav = pGlobalHead;')
983 header_txt.append(' while (pTrav) {')
984 header_txt.append(' if (pTrav->obj.pObj == object)')
985 header_txt.append(' return pTrav->obj.objType;')
986 header_txt.append(' pTrav = pTrav->pNextGlobal;')
987 header_txt.append(' }')
988 header_txt.append(' char str[1024];')
989 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
990 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
991 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
992 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800993 header_txt.append('#if 0')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700994 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
995 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
996 header_txt.append(' while (pTrav) {')
997 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
998 header_txt.append(' return pTrav->obj.numUses;')
999 header_txt.append(' }')
1000 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001001 header_txt.append(' }')
1002 header_txt.append(' return 0;')
1003 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001004 header_txt.append('#endif')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001005 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1006 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001007 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001008 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1009 header_txt.append(' pTrav->obj.numUses++;')
1010 header_txt.append(' char str[1024];')
1011 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);')
1012 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1013 header_txt.append(' return;')
1014 header_txt.append(' }')
1015 header_txt.append(' pTrav = pTrav->pNextObj;')
1016 header_txt.append(' }')
1017 header_txt.append(' // If we do not find obj, insert it and then increment count')
1018 header_txt.append(' char str[1024];')
1019 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));')
1020 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1021 header_txt.append('')
1022 header_txt.append(' ll_insert_obj(pObj, objType);')
1023 header_txt.append(' ll_increment_use_count(pObj, objType);')
1024 header_txt.append('}')
1025 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1026 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1027 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
1028 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1029 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1030 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1031 header_txt.append(' while (pTrav) {')
1032 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1033 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1034 header_txt.append(' // update HEAD of Obj list as needed')
1035 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1036 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1037 header_txt.append(' assert(numObjs[objType] > 0);')
1038 header_txt.append(' numObjs[objType]--;')
1039 header_txt.append(' char str[1024];')
1040 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1041 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 -06001042 header_txt.append(' return;')
1043 header_txt.append(' }')
1044 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001045 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001046 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001047 header_txt.append(' char str[1024];')
1048 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));')
1049 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1050 header_txt.append('}')
1051 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1052 header_txt.append('// remove obj from global list')
1053 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
1054 header_txt.append(' objNode *pTrav = pGlobalHead;')
1055 header_txt.append(' objNode *pPrev = pGlobalHead;')
1056 header_txt.append(' while (pTrav) {')
1057 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1058 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1059 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1060 header_txt.append(' // update HEAD of global list if needed')
1061 header_txt.append(' if (pGlobalHead == pTrav)')
1062 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
1063 header_txt.append(' free(pTrav);')
1064 header_txt.append(' assert(numTotalObjs > 0);')
1065 header_txt.append(' numTotalObjs--;')
1066 header_txt.append(' char str[1024];')
1067 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));')
1068 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1069 header_txt.append(' return;')
1070 header_txt.append(' }')
1071 header_txt.append(' pPrev = pTrav;')
1072 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1073 header_txt.append(' }')
1074 header_txt.append(' char str[1024];')
1075 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1076 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 -06001077 header_txt.append('}')
1078
1079 return "\n".join(header_txt)
1080
1081 def generate_body(self):
1082 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -07001083 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001084 self._generate_extensions(),
1085 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001086
1087 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001088
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001089def main():
1090 subcommands = {
1091 "layer-funcs" : LayerFuncsSubcommand,
1092 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001093 "Generic" : GenericLayerSubcommand,
1094 "ApiDump" : ApiDumpSubcommand,
1095 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -07001096 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001097 "ApiDumpCpp" : ApiDumpCppSubcommand,
1098 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001099 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001100 }
1101
1102 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
1103 print("Usage: %s <subcommand> [options]" % sys.argv[0])
1104 print
1105 print("Available sucommands are: %s" % " ".join(subcommands))
1106 exit(1)
1107
1108 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1109 subcmd.run()
1110
1111if __name__ == "__main__":
1112 main()