blob: 76e592484fbfa81899ff8dd8d28d955fd215867c [file] [log] [blame]
Tobin Ehlis12076fc2014-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
Tobin Ehlis14ff0852014-12-17 17:44:50 -070029import os
Tobin Ehlis12076fc2014-10-22 09:06:33 -060030
31import xgl
Tobin Ehlis14ff0852014-12-17 17:44:50 -070032import xgl_helper
Tobin Ehlis12076fc2014-10-22 09:06:33 -060033
34class Subcommand(object):
35 def __init__(self, argv):
36 self.argv = argv
Chia-I Wuc4f24e82015-01-01 08:46:31 +080037 self.headers = xgl.headers
38 self.protos = xgl.protos
Tobin Ehlis12076fc2014-10-22 09:06:33 -060039
40 def run(self):
Tobin Ehlis12076fc2014-10-22 09:06:33 -060041 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 Ehlis99f88672015-01-10 12:42:41 -070098 def _get_printf_params(self, xgl_type, name, output_param, cpp=False):
Tobin Ehlis12076fc2014-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)
Chia-I Wu99ff89d2014-12-27 14:14:50 +0800108 if "SIZE" in xgl_type:
109 if '*' in xgl_type:
110 return ("%zu", "*%s" % name)
111 return ("%zu", name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600112 if "FLOAT" in xgl_type:
113 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700114 if cpp:
115 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600116 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
117 return ("%f", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700118 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600119 return ("%u", name)
Chia-I Wu99ff89d2014-12-27 14:14:50 +0800120 if True in [t in xgl_type for t in ["INT", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600121 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700122 if cpp:
123 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600124 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
125 if '*' in xgl_type:
Jon Ashburn52f79b52014-12-12 16:10:45 -0700126 return ("%i", "*(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600127 return ("%i", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700128 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Jon Ashburn52f79b52014-12-12 16:10:45 -0700129 if "XGL_FORMAT" == xgl_type:
Tobin Ehlis99f88672015-01-10 12:42:41 -0700130 if cpp:
131 return ("%p", "&%s" % name)
132 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_XGL_CHANNEL_FORMAT(%s.channelFormat), string_XGL_NUM_FORMAT(%s.numericFormat)" % (name, name))
Tobin Ehlise7271572014-11-19 15:52:46 -0700133 if output_param:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600134 return ("%p", "(void*)*%s" % name)
Jon Ashburn52f79b52014-12-12 16:10:45 -0700135 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600136
Tobin Ehlise8185062014-12-17 08:01:59 -0700137 def _gen_layer_dbg_callback_header(self):
138 cbh_body = []
139 cbh_body.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
140 cbh_body.append('// Utility function to handle reporting')
141 cbh_body.append('// If callbacks are enabled, use them, otherwise use printf')
142 cbh_body.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
143 cbh_body.append(' XGL_VALIDATION_LEVEL validationLevel,')
144 cbh_body.append(' XGL_BASE_OBJECT srcObject,')
145 cbh_body.append(' XGL_SIZE location,')
146 cbh_body.append(' XGL_INT msgCode,')
147 cbh_body.append(' const XGL_CHAR* pLayerPrefix,')
148 cbh_body.append(' const XGL_CHAR* pMsg)')
149 cbh_body.append('{')
150 cbh_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
151 cbh_body.append(' if (pTrav) {')
152 cbh_body.append(' while (pTrav) {')
153 cbh_body.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
154 cbh_body.append(' pTrav = pTrav->pNext;')
155 cbh_body.append(' }')
156 cbh_body.append(' }')
157 cbh_body.append(' else {')
158 cbh_body.append(' switch (msgType) {')
159 cbh_body.append(' case XGL_DBG_MSG_ERROR:')
160 cbh_body.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
161 cbh_body.append(' break;')
162 cbh_body.append(' case XGL_DBG_MSG_WARNING:')
163 cbh_body.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
164 cbh_body.append(' break;')
165 cbh_body.append(' case XGL_DBG_MSG_PERF_WARNING:')
166 cbh_body.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
167 cbh_body.append(' break;')
168 cbh_body.append(' default:')
169 cbh_body.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
170 cbh_body.append(' break;')
171 cbh_body.append(' }')
172 cbh_body.append(' }')
173 cbh_body.append('}')
174 return "\n".join(cbh_body)
175
176 def _gen_layer_dbg_callback_register(self):
177 r_body = []
178 r_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)')
179 r_body.append('{')
180 r_body.append(' // This layer intercepts callbacks')
181 r_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));')
182 r_body.append(' if (!pNewDbgFuncNode)')
183 r_body.append(' return XGL_ERROR_OUT_OF_MEMORY;')
184 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
185 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
186 r_body.append(' pNewDbgFuncNode->pNext = pDbgFunctionHead;')
187 r_body.append(' pDbgFunctionHead = pNewDbgFuncNode;')
188 r_body.append(' XGL_RESULT result = nextTable.DbgRegisterMsgCallback(pfnMsgCallback, pUserData);')
189 r_body.append(' return result;')
190 r_body.append('}')
191 return "\n".join(r_body)
192
193 def _gen_layer_dbg_callback_unregister(self):
194 ur_body = []
195 ur_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
196 ur_body.append('{')
197 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
198 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
199 ur_body.append(' while (pTrav) {')
200 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
201 ur_body.append(' pPrev->pNext = pTrav->pNext;')
202 ur_body.append(' if (pDbgFunctionHead == pTrav)')
203 ur_body.append(' pDbgFunctionHead = pTrav->pNext;')
204 ur_body.append(' free(pTrav);')
205 ur_body.append(' break;')
206 ur_body.append(' }')
207 ur_body.append(' pPrev = pTrav;')
208 ur_body.append(' pTrav = pTrav->pNext;')
209 ur_body.append(' }')
210 ur_body.append(' XGL_RESULT result = nextTable.DbgUnregisterMsgCallback(pfnMsgCallback);')
211 ur_body.append(' return result;')
212 ur_body.append('}')
213 return "\n".join(ur_body)
214
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700215 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600216 if qual:
217 qual += " "
218
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700219 layer_name = layer
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700220 if no_addr:
221 layer_name = "%sNoAddr" % layer
Tobin Ehlis99f88672015-01-10 12:42:41 -0700222 if 'Cpp' in layer_name:
223 layer_name = "APIDumpNoAddrCpp"
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600224 funcs = []
225 for proto in self.protos:
226 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700227 if "Generic" == layer:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600228 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
229 param0_name = proto.params[0].name
230 ret_val = ''
231 stmt = ''
232 if proto.ret != "XGL_VOID":
233 ret_val = "XGL_RESULT result = "
234 stmt = " return result;\n"
Jon Ashburnf7a08742014-11-25 11:08:42 -0700235 if proto.name == "EnumerateLayers":
236 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
237 funcs.append('%s%s\n'
238 '{\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700239 ' char str[1024];\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700240 ' if (gpu != NULL) {\n'
241 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700242 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700243 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700244 ' pCurObj = gpuw;\n'
245 ' pthread_once(&tabOnce, initLayerTable);\n'
246 ' %snextTable.%s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700247 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700248 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700249 ' fflush(stdout);\n'
250 ' %s'
251 ' } else {\n'
252 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
253 ' return XGL_ERROR_INVALID_POINTER;\n'
254 ' // This layer compatible with all GPUs\n'
255 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800256 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700257 ' return XGL_SUCCESS;\n'
258 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700259 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlise8185062014-12-17 08:01:59 -0700260 elif 'DbgRegisterMsgCallback' == proto.name:
261 funcs.append(self._gen_layer_dbg_callback_register())
262 elif 'DbgUnregisterMsgCallback' == proto.name:
263 funcs.append(self._gen_layer_dbg_callback_unregister())
Jon Ashburnf7a08742014-11-25 11:08:42 -0700264 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600265 funcs.append('%s%s\n'
266 '{\n'
267 ' %snextTable.%s;\n'
268 '%s'
269 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
270 else:
271 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
272 funcs.append('%s%s\n'
273 '{\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700274 ' char str[1024];'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600275 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700276 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700277 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600278 ' pCurObj = gpuw;\n'
279 ' pthread_once(&tabOnce, initLayerTable);\n'
280 ' %snextTable.%s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700281 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700282 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -0700283 ' fflush(stdout);\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600284 '%s'
285 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis99f88672015-01-10 12:42:41 -0700286 elif "APIDumpCpp" in layer:
287 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
288 param0_name = proto.params[0].name
289 ret_val = ''
290 stmt = ''
291 cis_param_index = [] # Store list of indices when func has struct params
292 create_params = 0 # Num of params at end of function that are created and returned as output values
293 if 'WsiX11CreatePresentableImage' in proto.name:
294 create_params = -2
295 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
296 create_params = -1
297 if proto.ret != "XGL_VOID":
298 ret_val = "XGL_RESULT result = "
299 stmt = " return result;\n"
300 f_open = ''
301 f_close = ''
302 if "File" in layer:
303 file_mode = "a"
304 if 'CreateDevice' in proto.name:
305 file_mode = "w"
306 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
307 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
308 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
309 else:
310 f_open = 'pthread_mutex_lock( &print_lock );\n '
311 log_func = 'cout << "t{" << getTIDIndex() << "} xgl%s(' % proto.name
312 f_close = '\n pthread_mutex_unlock( &print_lock );'
313 pindex = 0
314 for p in proto.params:
315 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
316 cp = False
317 if 0 != create_params:
318 # If this is any of the N last params of the func, treat as output
319 for y in range(-1, create_params-1, -1):
320 if p.name == proto.params[y].name:
321 cp = True
322 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
323 if no_addr and "%p" == pft:
324 (pft, pfi) = ("%s", '"addr"')
325 log_func += '%s = " << %s << ", ' % (p.name, pfi)
326 #print_vals += ', %s' % (pfi)
327 # TODO : Just want this to be simple check for params of STRUCT type
328 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']]):
329 if 'Wsi' not in proto.name:
330 cis_param_index.append(pindex)
331 pindex += 1
332 log_func = log_func.strip(', ')
333 if proto.ret != "XGL_VOID":
334 log_func += ') = " << string_XGL_RESULT(result) << "\\n"'
335 #print_vals += ', string_XGL_RESULT(result)'
336 else:
337 log_func += ')\\n"'
338 log_func += ';'
339 if len(cis_param_index) > 0:
340 log_func += '\n string tmp_str;'
341 for sp_index in cis_param_index:
342 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
343 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
344 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
345 if "File" in layer:
346 if no_addr:
347 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
348 else:
349 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
350 else:
351 if no_addr:
352 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
353 log_func += '\n cout << " %s (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
354 else:
355 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
356 log_func += '\n cout << " %s (" << %s << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, proto.params[sp_index].name)
357 #log_func += '\n fflush(stdout);'
358 log_func += '\n }'
359 if proto.name == "EnumerateLayers":
360 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
361 funcs.append('%s%s\n'
362 '{\n'
363 ' if (gpu != NULL) {\n'
364 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
365 ' pCurObj = gpuw;\n'
366 ' pthread_once(&tabOnce, initLayerTable);\n'
367 ' %snextTable.%s;\n'
368 ' %s %s %s\n'
369 ' %s'
370 ' } else {\n'
371 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
372 ' return XGL_ERROR_INVALID_POINTER;\n'
373 ' // This layer compatible with all GPUs\n'
374 ' *pOutLayerCount = 1;\n'
375 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
376 ' return XGL_SUCCESS;\n'
377 ' }\n'
378 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
379 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
380 funcs.append('%s%s\n'
381 '{\n'
382 ' %snextTable.%s;\n'
383 ' %s%s%s\n'
384 '%s'
385 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
386 else:
387 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
388 funcs.append('%s%s\n'
389 '{\n'
390 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
391 ' pCurObj = gpuw;\n'
392 ' pthread_once(&tabOnce, initLayerTable);\n'
393 ' %snextTable.%s;\n'
394 ' %s%s%s\n'
395 '%s'
396 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700397 elif "APIDump" in layer:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600398 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
399 param0_name = proto.params[0].name
400 ret_val = ''
401 stmt = ''
Tobin Ehlis083e9062014-10-23 08:19:47 -0600402 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlise7271572014-11-19 15:52:46 -0700403 create_params = 0 # Num of params at end of function that are created and returned as output values
404 if 'WsiX11CreatePresentableImage' in proto.name:
405 create_params = -2
406 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
407 create_params = -1
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600408 if proto.ret != "XGL_VOID":
409 ret_val = "XGL_RESULT result = "
410 stmt = " return result;\n"
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700411 f_open = ''
412 f_close = ''
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700413 if "File" in layer:
Tobin Ehlisdbcd2572014-11-21 09:35:53 -0700414 file_mode = "a"
415 if 'CreateDevice' in proto.name:
416 file_mode = "w"
Chia-I Wu3bf80a62014-12-16 00:36:58 +0800417 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700418 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis2b9313e2014-11-20 12:18:45 -0700419 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700420 else:
Chia-I Wu3bf80a62014-12-16 00:36:58 +0800421 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700422 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis2b9313e2014-11-20 12:18:45 -0700423 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700424 print_vals = ', getTIDIndex()'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600425 pindex = 0
426 for p in proto.params:
Tobin Ehlise7271572014-11-19 15:52:46 -0700427 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
428 cp = False
429 if 0 != create_params:
430 # If this is any of the N last params of the func, treat as output
431 for y in range(-1, create_params-1, -1):
432 if p.name == proto.params[y].name:
433 cp = True
434 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700435 if no_addr and "%p" == pft:
436 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600437 log_func += '%s = %s, ' % (p.name, pft)
438 print_vals += ', %s' % (pfi)
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700439 # 'format' gets special treatment as a small struct that we print inline
440 if 'Wsi' not in proto.name and 'format' != p.name and xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct'):
441 cis_param_index.append(pindex)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600442 pindex += 1
443 log_func = log_func.strip(', ')
444 if proto.ret != "XGL_VOID":
445 log_func += ') = %s\\n"'
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -0700446 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600447 else:
448 log_func += ')\\n"'
449 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlis083e9062014-10-23 08:19:47 -0600450 if len(cis_param_index) > 0:
451 log_func += '\n char *pTmpStr;'
452 for sp_index in cis_param_index:
453 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
454 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
455 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700456 if "File" in layer:
457 if no_addr:
458 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
459 else:
460 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700461 else:
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700462 if no_addr:
463 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
464 else:
465 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700466 log_func += '\n fflush(stdout);'
Tobin Ehlis083e9062014-10-23 08:19:47 -0600467 log_func += '\n free(pTmpStr);\n }'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700468 if proto.name == "EnumerateLayers":
469 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
470 funcs.append('%s%s\n'
471 '{\n'
472 ' if (gpu != NULL) {\n'
473 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
474 ' pCurObj = gpuw;\n'
475 ' pthread_once(&tabOnce, initLayerTable);\n'
476 ' %snextTable.%s;\n'
477 ' %s %s %s\n'
478 ' %s'
479 ' } else {\n'
480 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
481 ' return XGL_ERROR_INVALID_POINTER;\n'
482 ' // This layer compatible with all GPUs\n'
483 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800484 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700485 ' return XGL_SUCCESS;\n'
486 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700487 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
Jon Ashburnf7a08742014-11-25 11:08:42 -0700488 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600489 funcs.append('%s%s\n'
490 '{\n'
491 ' %snextTable.%s;\n'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700492 ' %s%s%s\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600493 '%s'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700494 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600495 else:
496 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
497 funcs.append('%s%s\n'
498 '{\n'
499 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
500 ' pCurObj = gpuw;\n'
501 ' pthread_once(&tabOnce, initLayerTable);\n'
502 ' %snextTable.%s;\n'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700503 ' %s%s%s\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600504 '%s'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700505 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700506 elif "ObjectTracker" == layer:
Chia-I Wud9cc9752015-01-05 14:37:39 +0800507 obj_type_mapping = {
508 "XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU",
509 "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
510 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE",
511 "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
512 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY",
513 "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
514 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL",
515 "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
516 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE",
517 "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
518 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER",
519 "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
520 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE",
521 "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
522 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER",
523 "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
524 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW",
525 "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
526 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE",
527 "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
528 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE",
529 "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
530 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE",
531 "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
532 "XGL_OBJECT" : "ll_get_obj_type(object)"
533 }
Tobin Ehlisca915872014-11-18 11:28:33 -0700534
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600535 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
536 param0_name = proto.params[0].name
Tobin Ehlisca915872014-11-18 11:28:33 -0700537 p0_type = proto.params[0].ty
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600538 create_line = ''
539 destroy_line = ''
Tobin Ehlisca915872014-11-18 11:28:33 -0700540 if 'DbgRegisterMsgCallback' in proto.name:
541 using_line = ' // This layer intercepts callbacks\n'
542 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
543 using_line += ' if (!pNewDbgFuncNode)\n'
544 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
545 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
546 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
547 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
548 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
549 elif 'DbgUnregisterMsgCallback' in proto.name:
550 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
551 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
552 using_line += ' while (pTrav) {\n'
553 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
554 using_line += ' pPrev->pNext = pTrav->pNext;\n'
555 using_line += ' if (pDbgFunctionHead == pTrav)\n'
556 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
557 using_line += ' free(pTrav);\n'
558 using_line += ' break;\n'
559 using_line += ' }\n'
560 using_line += ' pPrev = pTrav;\n'
561 using_line += ' pTrav = pTrav->pNext;\n'
562 using_line += ' }\n'
563 elif 'GlobalOption' in proto.name:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600564 using_line = ''
Tobin Ehlisca915872014-11-18 11:28:33 -0700565 else:
566 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
567 if 'Create' in proto.name or 'Alloc' in proto.name:
568 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
569 if 'DestroyObject' in proto.name:
570 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
571 using_line = ''
572 else:
573 if 'Destroy' in proto.name or 'Free' in proto.name:
574 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
575 using_line = ''
576 if 'DestroyDevice' in proto.name:
577 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
578 destroy_line += ' char str[1024];\n'
579 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'
580 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
581 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600582 ret_val = ''
583 stmt = ''
584 if proto.ret != "XGL_VOID":
585 ret_val = "XGL_RESULT result = "
586 stmt = " return result;\n"
Jon Ashburnf7a08742014-11-25 11:08:42 -0700587 if proto.name == "EnumerateLayers":
588 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
589 funcs.append('%s%s\n'
590 '{\n'
591 ' if (gpu != NULL) {\n'
592 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
593 ' %s'
594 ' pCurObj = gpuw;\n'
595 ' pthread_once(&tabOnce, initLayerTable);\n'
596 ' %snextTable.%s;\n'
597 ' %s%s'
598 ' %s'
599 ' } else {\n'
600 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
601 ' return XGL_ERROR_INVALID_POINTER;\n'
602 ' // This layer compatible with all GPUs\n'
603 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800604 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700605 ' return XGL_SUCCESS;\n'
606 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700607 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt, layer_name))
Jon Ashburnf7a08742014-11-25 11:08:42 -0700608 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600609 funcs.append('%s%s\n'
610 '{\n'
611 '%s'
612 ' %snextTable.%s;\n'
613 '%s%s'
614 '%s'
615 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
616 else:
617 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
618 funcs.append('%s%s\n'
619 '{\n'
620 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
621 '%s'
622 ' pCurObj = gpuw;\n'
623 ' pthread_once(&tabOnce, initLayerTable);\n'
624 ' %snextTable.%s;\n'
625 '%s%s'
626 '%s'
627 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700628 elif "ParamChecker" == layer:
629 # TODO : Need to fix up the non-else cases below to do param checking as well
630 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
631 param0_name = proto.params[0].name
632 ret_val = ''
633 stmt = ''
634 param_checks = []
635 # Add code to check enums and structs
636 # TODO : Currently only validating enum values, need to validate everything
637 str_decl = False
638 for p in proto.params:
639 if xgl_helper.is_type(p.ty.strip('*').strip('const '), 'enum'):
640 if not str_decl:
641 param_checks.append(' char str[1024];')
642 str_decl = True
643 param_checks.append(' if (!validate_%s(%s)) {' % (p.ty, p.name))
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700644 param_checks.append(' sprintf(str, "Parameter %s to function %s has invalid value of %%i.", (int)%s);' % (p.name, proto.name, p.name))
645 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
646 param_checks.append(' }')
647 elif xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct') and 'const' in p.ty:
648 if not str_decl:
649 param_checks.append(' char str[1024];')
650 str_decl = True
651 if '*' in p.ty: # First check for null ptr
652 param_checks.append(' if (!%s) {' % p.name)
653 param_checks.append(' sprintf(str, "Struct ptr parameter %s to function %s is NULL.");' % (p.name, proto.name))
654 param_checks.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
655 param_checks.append(' }')
656 param_checks.append(' else if (!xgl_validate_%s(%s)) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
657 else:
658 param_checks.append(' if (!xgl_validate_%s(%s)) {' % (p.ty.strip('const ').lower(), p.name))
659 param_checks.append(' sprintf(str, "Parameter %s to function %s contains an invalid value.");' % (p.name, proto.name))
660 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
661 param_checks.append(' }')
662 if proto.ret != "XGL_VOID":
663 ret_val = "XGL_RESULT result = "
664 stmt = " return result;\n"
665 if proto.name == "EnumerateLayers":
666 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
667 funcs.append('%s%s\n'
668 '{\n'
669 ' char str[1024];\n'
670 ' if (gpu != NULL) {\n'
671 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
672 ' sprintf(str, "At start of layered %s\\n");\n'
673 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
674 ' pCurObj = gpuw;\n'
675 ' pthread_once(&tabOnce, initLayerTable);\n'
676 ' %snextTable.%s;\n'
677 ' sprintf(str, "Completed layered %s\\n");\n'
678 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
679 ' fflush(stdout);\n'
680 ' %s'
681 ' } else {\n'
682 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
683 ' return XGL_ERROR_INVALID_POINTER;\n'
684 ' // This layer compatible with all GPUs\n'
685 ' *pOutLayerCount = 1;\n'
686 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
687 ' return XGL_SUCCESS;\n'
688 ' }\n'
689 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
690 elif 'DbgRegisterMsgCallback' == proto.name:
691 funcs.append(self._gen_layer_dbg_callback_register())
692 elif 'DbgUnregisterMsgCallback' == proto.name:
693 funcs.append(self._gen_layer_dbg_callback_unregister())
694 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
695 funcs.append('%s%s\n'
696 '{\n'
697 '%s\n'
698 ' %snextTable.%s;\n'
699 '%s'
700 '}' % (qual, decl, "\n".join(param_checks), ret_val, proto.c_call(), stmt))
701 else:
702 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
703 funcs.append('%s%s\n'
704 '{\n'
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700705 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700706 ' pCurObj = gpuw;\n'
707 ' pthread_once(&tabOnce, initLayerTable);\n'
Tobin Ehlisbf4c4de2014-12-18 08:44:01 -0700708 '%s\n'
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700709 ' %snextTable.%s;\n'
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700710 '%s'
Tobin Ehlisbf4c4de2014-12-18 08:44:01 -0700711 '}' % (qual, decl, proto.params[0].name, "\n".join(param_checks), ret_val, c_call, stmt))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600712
713 return "\n\n".join(funcs)
714
Tobin Ehlisca915872014-11-18 11:28:33 -0700715 def _generate_extensions(self):
716 exts = []
717 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
718 exts.append('{')
719 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
720 exts.append('}')
721 exts.append('')
722 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
723 exts.append('{')
724 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
725 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
726 exts.append(' // Check the count first thing')
727 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
728 exts.append(' if (objCount > maxObjCount) {')
729 exts.append(' char str[1024];')
730 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));')
731 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
732 exts.append(' return XGL_ERROR_INVALID_VALUE;')
733 exts.append(' }')
734 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
735 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
736 exts.append(' if (!pTrav) {')
737 exts.append(' char str[1024];')
738 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);')
739 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
740 exts.append(' return XGL_ERROR_UNKNOWN;')
741 exts.append(' }')
742 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
743 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
744 exts.append(' }')
745 exts.append(' return XGL_SUCCESS;')
746 exts.append('}')
747
748 return "\n".join(exts)
749
Chia-I Wu4d11dcc2015-01-05 13:18:57 +0800750 def _generate_layer_gpa_function(self, extensions=[]):
751 func_body = ["#include \"xgl_generic_intercept_proc_helper.h\""]
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600752 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
753 "{\n"
754 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu4d11dcc2015-01-05 13:18:57 +0800755 " void* addr;\n"
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600756 " if (gpu == NULL)\n"
757 " return NULL;\n"
758 " pCurObj = gpuw;\n"
759 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu4d11dcc2015-01-05 13:18:57 +0800760 " addr = layer_intercept_proc(funcName);\n"
761 " if (addr)\n"
762 " return addr;")
763
Tobin Ehlisca915872014-11-18 11:28:33 -0700764 if 0 != len(extensions):
765 for ext_name in extensions:
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800766 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlisca915872014-11-18 11:28:33 -0700767 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600768 func_body.append(" else {\n"
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600769 " if (gpuw->pGPA == NULL)\n"
770 " return NULL;\n"
Tobin Ehlis99f88672015-01-10 12:42:41 -0700771 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600772 " }\n"
773 "}\n")
774 return "\n".join(func_body)
775
776 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wuaa4121f2015-01-04 23:11:43 +0800777 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600778 func_body.append('static void initLayerTable()\n'
779 '{\n'
Mark Lobodzinski391bb6d2015-01-09 15:12:03 -0600780 ' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600781 ' fpNextGPA = pCurObj->pGPA;\n'
782 ' assert(fpNextGPA);\n');
783
Chia-I Wuaa4121f2015-01-04 23:11:43 +0800784 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600785 func_body.append("}\n")
786 return "\n".join(func_body)
787
788class LayerFuncsSubcommand(Subcommand):
789 def generate_header(self):
790 return '#include <xglLayer.h>\n#include "loader.h"'
791
792 def generate_body(self):
793 return self._generate_dispatch_entrypoints("static", True)
794
795class LayerDispatchSubcommand(Subcommand):
796 def generate_header(self):
797 return '#include "layer_wrappers.h"'
798
799 def generate_body(self):
800 return self._generate_layer_dispatch_table()
801
802class GenericLayerSubcommand(Subcommand):
803 def generate_header(self):
804 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'
805
806 def generate_body(self):
Tobin Ehlise8185062014-12-17 08:01:59 -0700807 body = [self._gen_layer_dbg_callback_header(),
808 self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700809 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600810 self._generate_layer_gpa_function()]
811
812 return "\n\n".join(body)
813
814class ApiDumpSubcommand(Subcommand):
815 def generate_header(self):
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700816 header_txt = []
817 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')
818 header_txt.append('#define MAX_TID 513')
819 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
820 header_txt.append('static uint32_t maxTID = 0;')
821 header_txt.append('// Map actual TID to an index value and return that index')
822 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
823 header_txt.append('static uint32_t getTIDIndex() {')
824 header_txt.append(' pthread_t tid = pthread_self();')
825 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
826 header_txt.append(' if (tid == tidMapping[i])')
827 header_txt.append(' return i;')
828 header_txt.append(' }')
829 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
830 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
831 header_txt.append(' tidMapping[maxTID++] = tid;')
832 header_txt.append(' assert(maxTID < MAX_TID);')
833 header_txt.append(' return retVal;')
834 header_txt.append('}')
835 return "\n".join(header_txt)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600836
837 def generate_body(self):
838 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700839 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600840 self._generate_layer_gpa_function()]
841
842 return "\n\n".join(body)
843
Tobin Ehlis99f88672015-01-10 12:42:41 -0700844class ApiDumpCppSubcommand(Subcommand):
845 def generate_header(self):
846 header_txt = []
847 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')
848 header_txt.append('#define MAX_TID 513')
849 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
850 header_txt.append('static uint32_t maxTID = 0;')
851 header_txt.append('// Map actual TID to an index value and return that index')
852 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
853 header_txt.append('static uint32_t getTIDIndex() {')
854 header_txt.append(' pthread_t tid = pthread_self();')
855 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
856 header_txt.append(' if (tid == tidMapping[i])')
857 header_txt.append(' return i;')
858 header_txt.append(' }')
859 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
860 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
861 header_txt.append(' tidMapping[maxTID++] = tid;')
862 header_txt.append(' assert(maxTID < MAX_TID);')
863 header_txt.append(' return retVal;')
864 header_txt.append('}')
865 return "\n".join(header_txt)
866
867 def generate_body(self):
868 body = [self._generate_layer_dispatch_table(),
869 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp"),
870 self._generate_layer_gpa_function()]
871
872 return "\n\n".join(body)
873
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700874class ApiDumpFileSubcommand(Subcommand):
875 def generate_header(self):
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700876 header_txt = []
877 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')
878 header_txt.append('#define MAX_TID 513')
879 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
880 header_txt.append('static uint32_t maxTID = 0;')
881 header_txt.append('// Map actual TID to an index value and return that index')
882 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
883 header_txt.append('static uint32_t getTIDIndex() {')
884 header_txt.append(' pthread_t tid = pthread_self();')
885 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
886 header_txt.append(' if (tid == tidMapping[i])')
887 header_txt.append(' return i;')
888 header_txt.append(' }')
889 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
890 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
891 header_txt.append(' tidMapping[maxTID++] = tid;')
892 header_txt.append(' assert(maxTID < MAX_TID);')
893 header_txt.append(' return retVal;')
894 header_txt.append('}')
895 return "\n".join(header_txt)
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700896
897 def generate_body(self):
898 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700899 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700900 self._generate_layer_gpa_function()]
901
902 return "\n\n".join(body)
903
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700904class ApiDumpNoAddrSubcommand(Subcommand):
905 def generate_header(self):
906 header_txt = []
907 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')
908 header_txt.append('#define MAX_TID 513')
909 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
910 header_txt.append('static uint32_t maxTID = 0;')
911 header_txt.append('// Map actual TID to an index value and return that index')
912 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
913 header_txt.append('static uint32_t getTIDIndex() {')
914 header_txt.append(' pthread_t tid = pthread_self();')
915 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
916 header_txt.append(' if (tid == tidMapping[i])')
917 header_txt.append(' return i;')
918 header_txt.append(' }')
919 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
920 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
921 header_txt.append(' tidMapping[maxTID++] = tid;')
922 header_txt.append(' assert(maxTID < MAX_TID);')
923 header_txt.append(' return retVal;')
924 header_txt.append('}')
925 return "\n".join(header_txt)
926
927 def generate_body(self):
928 body = [self._generate_layer_dispatch_table(),
929 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
930 self._generate_layer_gpa_function()]
931
932 return "\n\n".join(body)
933
Tobin Ehlis99f88672015-01-10 12:42:41 -0700934class ApiDumpNoAddrCppSubcommand(Subcommand):
935 def generate_header(self):
936 header_txt = []
937 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')
938 header_txt.append('#define MAX_TID 513')
939 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
940 header_txt.append('static uint32_t maxTID = 0;')
941 header_txt.append('// Map actual TID to an index value and return that index')
942 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
943 header_txt.append('static uint32_t getTIDIndex() {')
944 header_txt.append(' pthread_t tid = pthread_self();')
945 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
946 header_txt.append(' if (tid == tidMapping[i])')
947 header_txt.append(' return i;')
948 header_txt.append(' }')
949 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
950 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
951 header_txt.append(' tidMapping[maxTID++] = tid;')
952 header_txt.append(' assert(maxTID < MAX_TID);')
953 header_txt.append(' return retVal;')
954 header_txt.append('}')
955 return "\n".join(header_txt)
956
957 def generate_body(self):
958 body = [self._generate_layer_dispatch_table(),
959 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp", True),
960 self._generate_layer_gpa_function()]
961
962 return "\n\n".join(body)
963
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600964class ObjectTrackerSubcommand(Subcommand):
965 def generate_header(self):
966 header_txt = []
967 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>')
Tobin Ehlisca915872014-11-18 11:28:33 -0700968 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
969 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
970 header_txt.append('// Ptr to LL of dbg functions')
971 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
972 header_txt.append('// Utility function to handle reporting')
973 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
974 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
975 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
976 header_txt.append(' XGL_BASE_OBJECT srcObject,')
977 header_txt.append(' XGL_SIZE location,')
978 header_txt.append(' XGL_INT msgCode,')
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800979 header_txt.append(' const char* pLayerPrefix,')
980 header_txt.append(' const char* pMsg)')
Tobin Ehlisca915872014-11-18 11:28:33 -0700981 header_txt.append('{')
982 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
983 header_txt.append(' if (pTrav) {')
984 header_txt.append(' while (pTrav) {')
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800985 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlisca915872014-11-18 11:28:33 -0700986 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600987 header_txt.append(' }')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600988 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -0700989 header_txt.append(' else {')
990 header_txt.append(' switch (msgType) {')
991 header_txt.append(' case XGL_DBG_MSG_ERROR:')
992 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
993 header_txt.append(' break;')
994 header_txt.append(' case XGL_DBG_MSG_WARNING:')
995 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
996 header_txt.append(' break;')
997 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
998 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
999 header_txt.append(' break;')
1000 header_txt.append(' default:')
1001 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
1002 header_txt.append(' break;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001003 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -07001004 header_txt.append(' }')
1005 header_txt.append('}')
1006 header_txt.append('// We maintain a "Global" list which links every object and a')
1007 header_txt.append('// per-Object list which just links objects of a given type')
1008 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
1009 header_txt.append('typedef struct _objNode {')
1010 header_txt.append(' OBJTRACK_NODE obj;')
1011 header_txt.append(' struct _objNode *pNextObj;')
1012 header_txt.append(' struct _objNode *pNextGlobal;')
1013 header_txt.append('} objNode;')
1014 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
1015 header_txt.append('static objNode *pGlobalHead = NULL;')
1016 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
1017 header_txt.append('static uint64_t numTotalObjs = 0;')
1018 header_txt.append('// Debug function to print global list and each individual object list')
1019 header_txt.append('static void ll_print_lists()')
1020 header_txt.append('{')
1021 header_txt.append(' objNode* pTrav = pGlobalHead;')
1022 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
1023 header_txt.append(' while (pTrav) {')
1024 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);')
1025 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1026 header_txt.append(' }')
1027 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
1028 header_txt.append(' pTrav = pObjectHead[i];')
1029 header_txt.append(' if (pTrav) {')
1030 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
1031 header_txt.append(' while (pTrav) {')
1032 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);')
1033 header_txt.append(' pTrav = pTrav->pNextObj;')
1034 header_txt.append(' }')
1035 header_txt.append(' }')
1036 header_txt.append(' }')
1037 header_txt.append('}')
1038 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1039 header_txt.append(' char str[1024];')
1040 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %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);')
1042 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
1043 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
1044 header_txt.append(' pNewObjNode->obj.objType = objType;')
1045 header_txt.append(' pNewObjNode->obj.numUses = 0;')
1046 header_txt.append(' // insert at front of global list')
1047 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
1048 header_txt.append(' pGlobalHead = pNewObjNode;')
1049 header_txt.append(' // insert at front of object list')
1050 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
1051 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
1052 header_txt.append(' // increment obj counts')
1053 header_txt.append(' numObjs[objType]++;')
1054 header_txt.append(' numTotalObjs++;')
1055 header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_XGL_OBJECT_TYPE(objType));')
Chia-I Wu85763e52014-12-16 11:02:06 +08001056 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlisca915872014-11-18 11:28:33 -07001057 header_txt.append('}')
1058 header_txt.append('// Traverse global list and return type for given object')
1059 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
1060 header_txt.append(' objNode *pTrav = pGlobalHead;')
1061 header_txt.append(' while (pTrav) {')
1062 header_txt.append(' if (pTrav->obj.pObj == object)')
1063 header_txt.append(' return pTrav->obj.objType;')
1064 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1065 header_txt.append(' }')
1066 header_txt.append(' char str[1024];')
1067 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
1068 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
1069 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
1070 header_txt.append('}')
Chia-I Wu85763e52014-12-16 11:02:06 +08001071 header_txt.append('#if 0')
Tobin Ehlisca915872014-11-18 11:28:33 -07001072 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1073 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1074 header_txt.append(' while (pTrav) {')
1075 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1076 header_txt.append(' return pTrav->obj.numUses;')
1077 header_txt.append(' }')
1078 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001079 header_txt.append(' }')
1080 header_txt.append(' return 0;')
1081 header_txt.append('}')
Chia-I Wu85763e52014-12-16 11:02:06 +08001082 header_txt.append('#endif')
Tobin Ehlisca915872014-11-18 11:28:33 -07001083 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1084 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001085 header_txt.append(' while (pTrav) {')
Tobin Ehlisca915872014-11-18 11:28:33 -07001086 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1087 header_txt.append(' pTrav->obj.numUses++;')
1088 header_txt.append(' char str[1024];')
1089 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);')
1090 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1091 header_txt.append(' return;')
1092 header_txt.append(' }')
1093 header_txt.append(' pTrav = pTrav->pNextObj;')
1094 header_txt.append(' }')
1095 header_txt.append(' // If we do not find obj, insert it and then increment count')
1096 header_txt.append(' char str[1024];')
1097 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));')
1098 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1099 header_txt.append('')
1100 header_txt.append(' ll_insert_obj(pObj, objType);')
1101 header_txt.append(' ll_increment_use_count(pObj, objType);')
1102 header_txt.append('}')
1103 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1104 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1105 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
1106 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1107 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1108 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1109 header_txt.append(' while (pTrav) {')
1110 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1111 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1112 header_txt.append(' // update HEAD of Obj list as needed')
1113 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1114 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1115 header_txt.append(' assert(numObjs[objType] > 0);')
1116 header_txt.append(' numObjs[objType]--;')
1117 header_txt.append(' char str[1024];')
1118 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1119 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001120 header_txt.append(' return;')
1121 header_txt.append(' }')
1122 header_txt.append(' pPrev = pTrav;')
Tobin Ehlisca915872014-11-18 11:28:33 -07001123 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001124 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -07001125 header_txt.append(' char str[1024];')
1126 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));')
1127 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1128 header_txt.append('}')
1129 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1130 header_txt.append('// remove obj from global list')
1131 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
1132 header_txt.append(' objNode *pTrav = pGlobalHead;')
1133 header_txt.append(' objNode *pPrev = pGlobalHead;')
1134 header_txt.append(' while (pTrav) {')
1135 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1136 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1137 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1138 header_txt.append(' // update HEAD of global list if needed')
1139 header_txt.append(' if (pGlobalHead == pTrav)')
1140 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
1141 header_txt.append(' free(pTrav);')
1142 header_txt.append(' assert(numTotalObjs > 0);')
1143 header_txt.append(' numTotalObjs--;')
1144 header_txt.append(' char str[1024];')
1145 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));')
1146 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1147 header_txt.append(' return;')
1148 header_txt.append(' }')
1149 header_txt.append(' pPrev = pTrav;')
1150 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1151 header_txt.append(' }')
1152 header_txt.append(' char str[1024];')
1153 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1154 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_DESTROY_OBJECT_FAILED, "OBJTRACK", str);')
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001155 header_txt.append('}')
1156
1157 return "\n".join(header_txt)
1158
1159 def generate_body(self):
1160 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -07001161 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlisca915872014-11-18 11:28:33 -07001162 self._generate_extensions(),
1163 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001164
1165 return "\n\n".join(body)
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -07001166
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001167class ParamCheckerSubcommand(Subcommand):
1168 def generate_header(self):
1169 return '#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include "xglLayer.h"\n#include "xgl_enum_validate_helper.h"\n#include "xgl_struct_validate_helper.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\n'
1170
1171 def generate_body(self):
1172 body = [self._gen_layer_dbg_callback_header(),
1173 self._generate_layer_dispatch_table(),
1174 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ParamChecker"),
1175 self._generate_layer_gpa_function()]
1176
1177 return "\n\n".join(body)
1178
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001179def main():
1180 subcommands = {
1181 "layer-funcs" : LayerFuncsSubcommand,
1182 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlisa363cfa2014-11-25 16:59:27 -07001183 "Generic" : GenericLayerSubcommand,
1184 "ApiDump" : ApiDumpSubcommand,
1185 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlisd49efcb2014-11-25 17:43:26 -07001186 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis99f88672015-01-10 12:42:41 -07001187 "ApiDumpCpp" : ApiDumpCppSubcommand,
1188 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlisa363cfa2014-11-25 16:59:27 -07001189 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001190 "ParamChecker" : ParamCheckerSubcommand,
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001191 }
1192
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001193 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1194 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001195 print
1196 print("Available sucommands are: %s" % " ".join(subcommands))
1197 exit(1)
1198
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001199 hfp = xgl_helper.HeaderFileParser(sys.argv[2])
1200 hfp.parse()
1201 xgl_helper.enum_val_dict = hfp.get_enum_val_dict()
1202 xgl_helper.enum_type_dict = hfp.get_enum_type_dict()
1203 xgl_helper.struct_dict = hfp.get_struct_dict()
1204 xgl_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1205 xgl_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1206 xgl_helper.types_dict = hfp.get_types_dict()
1207
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001208 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1209 subcmd.run()
1210
1211if __name__ == "__main__":
1212 main()