blob: a81ee4479b6231c644db472eddb3ec1bf2af50c5 [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
Tobin Ehlis6cd06372014-12-17 17:44:50 -070029import os
Tobin Ehlis92dbf802014-10-22 09:06:33 -060030
31import xgl
Tobin Ehlis6cd06372014-12-17 17:44:50 -070032import xgl_helper
Tobin Ehlis92dbf802014-10-22 09:06:33 -060033
34class Subcommand(object):
35 def __init__(self, argv):
36 self.argv = argv
Chia-I Wuec30dcb2015-01-01 08:46:31 +080037 self.headers = xgl.headers
38 self.protos = xgl.protos
Tobin Ehlis92dbf802014-10-22 09:06:33 -060039
40 def run(self):
Tobin Ehlis92dbf802014-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 Ehlis434db7c2015-01-10 12:42:41 -070098 def _get_printf_params(self, xgl_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060099 # TODO : Need ENUM and STRUCT checks here
100 if "_TYPE" in xgl_type: # TODO : This should be generic ENUM check
101 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
102 if "XGL_CHAR*" == xgl_type:
103 return ("%s", name)
104 if "UINT64" in xgl_type:
105 if '*' in xgl_type:
106 return ("%lu", "*%s" % name)
107 return ("%lu", name)
Chia-I Wu54ed0792014-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 Ehlis92dbf802014-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 Ehlis434db7c2015-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 Ehlis92dbf802014-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 Ehlis0a1e06d2014-11-11 17:28:22 -0700118 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600119 return ("%u", name)
Chia-I Wu54ed0792014-12-27 14:14:50 +0800120 if True in [t in xgl_type for t in ["INT", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600121 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-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 Ehlis92dbf802014-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 Ashburn1f7e2d72014-12-12 16:10:45 -0700126 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600127 return ("%i", name)
Tobin Ehlis0a1e06d2014-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 Ashburn1f7e2d72014-12-12 16:10:45 -0700129 if "XGL_FORMAT" == xgl_type:
Tobin Ehlis434db7c2015-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 Ehlisa554dc32014-11-19 15:52:46 -0700133 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600134 return ("%p", "(void*)*%s" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700135 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600136
Tobin Ehlisc54139f2014-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 Ehlis07fe9ab2014-11-25 17:43:26 -0700215 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600216 if qual:
217 qual += " "
218
Tobin Ehlis907a0522014-11-25 16:59:27 -0700219 layer_name = layer
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700220 if no_addr:
221 layer_name = "%sNoAddr" % layer
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700222 if 'Cpp' in layer_name:
223 layer_name = "APIDumpNoAddrCpp"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600224 funcs = []
225 for proto in self.protos:
226 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlis907a0522014-11-25 16:59:27 -0700227 if "Generic" == layer:
Tobin Ehlis92dbf802014-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 Ashburn451c16f2014-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 Ehlisc54139f2014-12-17 08:01:59 -0700239 ' char str[1024];\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700240 ' if (gpu != NULL) {\n'
241 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700242 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ashburn451c16f2014-11-25 11:08:42 -0700244 ' pCurObj = gpuw;\n'
245 ' pthread_once(&tabOnce, initLayerTable);\n'
246 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700247 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ashburn451c16f2014-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 Wua837c522014-12-16 10:47:33 +0800256 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700257 ' return XGL_SUCCESS;\n'
258 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700259 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlisc54139f2014-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 Ashburn451c16f2014-11-25 11:08:42 -0700264 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-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 Ehlisc54139f2014-12-17 08:01:59 -0700274 ' char str[1024];'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600275 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700276 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ehlis92dbf802014-10-22 09:06:33 -0600278 ' pCurObj = gpuw;\n'
279 ' pthread_once(&tabOnce, initLayerTable);\n'
280 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700281 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700283 ' fflush(stdout);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600284 '%s'
285 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis434db7c2015-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 = ''
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700291 sp_param_dict = {} # Store 'index' func has struct param to print, or an name of binding "Count" param for array to print
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700292 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 Ehlis907a0522014-11-25 16:59:27 -0700397 elif "APIDump" in layer:
Tobin Ehlis92dbf802014-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 Ehlis73ff5672014-10-23 08:19:47 -0600402 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlisa554dc32014-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 Ehlis92dbf802014-10-22 09:06:33 -0600408 if proto.ret != "XGL_VOID":
409 ret_val = "XGL_RESULT result = "
410 stmt = " return result;\n"
Tobin Ehlis574b0142014-11-12 13:11:15 -0700411 f_open = ''
412 f_close = ''
Tobin Ehlis907a0522014-11-25 16:59:27 -0700413 if "File" in layer:
Tobin Ehlis1eba7792014-11-21 09:35:53 -0700414 file_mode = "a"
415 if 'CreateDevice' in proto.name:
416 file_mode = "w"
Chia-I Wu81f46672014-12-16 00:36:58 +0800417 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700418 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700419 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700420 else:
Chia-I Wu81f46672014-12-16 00:36:58 +0800421 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700422 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700423 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700424 print_vals = ', getTIDIndex()'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600425 pindex = 0
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700426 prev_count_name = ''
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600427 for p in proto.params:
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700428 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 Ehlis07fe9ab2014-11-25 17:43:26 -0700435 if no_addr and "%p" == pft:
436 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600437 log_func += '%s = %s, ' % (p.name, pft)
438 print_vals += ', %s' % (pfi)
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700439 # Catch array inputs that are bound by a "Count" param
440 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
441 sp_param_dict[pindex] = prev_count_name
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700442 # 'format' gets special treatment as a small struct that we print inline
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700443 elif 'Wsi' not in proto.name and 'format' != p.name and xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct'):
444 sp_param_dict[pindex] = 'index'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600445 pindex += 1
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700446 if p.name.endswith('Count'):
447 prev_count_name = p.name
448 else:
449 prev_count_name = ''
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600450 log_func = log_func.strip(', ')
451 if proto.ret != "XGL_VOID":
452 log_func += ') = %s\\n"'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700453 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600454 else:
455 log_func += ')\\n"'
456 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700457 if len(sp_param_dict) > 0:
458 i_decl = False
459 log_func += '\n char *pTmpStr = "";'
460 for sp_index in sorted(sp_param_dict):
461 # TODO : Clean this if/else block up, too much duplicated code
462 if 'index' == sp_param_dict[sp_index]:
463 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
464 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
465 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
466 if "File" in layer:
467 if no_addr:
468 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
469 else:
470 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700471 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700472 if no_addr:
473 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
474 else:
475 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
476 log_func += '\n fflush(stdout);'
477 log_func += '\n free(pTmpStr);\n }'
478 else: # should have a count value stored to iterate over array
479 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
480 cis_print_func = 'pTmpStr = xgl_print_%s(&%s[i], " ");' % (proto.params[sp_index].ty.strip('const ').strip('*').lower(), proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700481 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700482 cis_print_func = 'pTmpStr = (char*)malloc(sizeof(char));\n sprintf(pTmpStr, " %%p", %s[i]);' % proto.params[sp_index].name
483 if not i_decl:
484 log_func += '\n uint32_t i;'
485 i_decl = True
486 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
487 log_func += '\n %s' % (cis_print_func)
488 if "File" in layer:
489 if no_addr:
490 log_func += '\n fprintf(pOutFile, " %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
491 else:
492 log_func += '\n fprintf(pOutFile, " %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
493 else:
494 if no_addr:
495 log_func += '\n printf(" %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
496 else:
497 log_func += '\n printf(" %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
498 log_func += '\n fflush(stdout);'
499 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700500 if proto.name == "EnumerateLayers":
501 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
502 funcs.append('%s%s\n'
503 '{\n'
504 ' if (gpu != NULL) {\n'
505 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
506 ' pCurObj = gpuw;\n'
507 ' pthread_once(&tabOnce, initLayerTable);\n'
508 ' %snextTable.%s;\n'
509 ' %s %s %s\n'
510 ' %s'
511 ' } else {\n'
512 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
513 ' return XGL_ERROR_INVALID_POINTER;\n'
514 ' // This layer compatible with all GPUs\n'
515 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800516 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700517 ' return XGL_SUCCESS;\n'
518 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700519 '}' % (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 -0700520 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600521 funcs.append('%s%s\n'
522 '{\n'
523 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700524 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600525 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700526 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600527 else:
528 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
529 funcs.append('%s%s\n'
530 '{\n'
531 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
532 ' pCurObj = gpuw;\n'
533 ' pthread_once(&tabOnce, initLayerTable);\n'
534 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700535 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600536 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700537 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700538 elif "ObjectTracker" == layer:
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800539 obj_type_mapping = {
540 "XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU",
541 "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
542 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE",
543 "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
544 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY",
545 "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
546 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL",
547 "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
548 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE",
549 "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
550 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER",
551 "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
552 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE",
553 "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
554 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER",
555 "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
556 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW",
557 "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
558 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE",
559 "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
560 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE",
561 "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
562 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE",
563 "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
564 "XGL_OBJECT" : "ll_get_obj_type(object)"
565 }
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700566
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600567 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
568 param0_name = proto.params[0].name
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700569 p0_type = proto.params[0].ty
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600570 create_line = ''
571 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700572 if 'DbgRegisterMsgCallback' in proto.name:
573 using_line = ' // This layer intercepts callbacks\n'
574 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
575 using_line += ' if (!pNewDbgFuncNode)\n'
576 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
577 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
578 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
579 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
580 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
581 elif 'DbgUnregisterMsgCallback' in proto.name:
582 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
583 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
584 using_line += ' while (pTrav) {\n'
585 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
586 using_line += ' pPrev->pNext = pTrav->pNext;\n'
587 using_line += ' if (pDbgFunctionHead == pTrav)\n'
588 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
589 using_line += ' free(pTrav);\n'
590 using_line += ' break;\n'
591 using_line += ' }\n'
592 using_line += ' pPrev = pTrav;\n'
593 using_line += ' pTrav = pTrav->pNext;\n'
594 using_line += ' }\n'
595 elif 'GlobalOption' in proto.name:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600596 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700597 else:
598 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
599 if 'Create' in proto.name or 'Alloc' in proto.name:
600 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
601 if 'DestroyObject' in proto.name:
602 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
603 using_line = ''
604 else:
605 if 'Destroy' in proto.name or 'Free' in proto.name:
606 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
607 using_line = ''
608 if 'DestroyDevice' in proto.name:
609 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
610 destroy_line += ' char str[1024];\n'
611 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'
612 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
613 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600614 ret_val = ''
615 stmt = ''
616 if proto.ret != "XGL_VOID":
617 ret_val = "XGL_RESULT result = "
618 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700619 if proto.name == "EnumerateLayers":
620 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
621 funcs.append('%s%s\n'
622 '{\n'
623 ' if (gpu != NULL) {\n'
624 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
625 ' %s'
626 ' pCurObj = gpuw;\n'
627 ' pthread_once(&tabOnce, initLayerTable);\n'
628 ' %snextTable.%s;\n'
629 ' %s%s'
630 ' %s'
631 ' } else {\n'
632 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
633 ' return XGL_ERROR_INVALID_POINTER;\n'
634 ' // This layer compatible with all GPUs\n'
635 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800636 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700637 ' return XGL_SUCCESS;\n'
638 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700639 '}' % (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 -0700640 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600641 funcs.append('%s%s\n'
642 '{\n'
643 '%s'
644 ' %snextTable.%s;\n'
645 '%s%s'
646 '%s'
647 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
648 else:
649 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
650 funcs.append('%s%s\n'
651 '{\n'
652 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
653 '%s'
654 ' pCurObj = gpuw;\n'
655 ' pthread_once(&tabOnce, initLayerTable);\n'
656 ' %snextTable.%s;\n'
657 '%s%s'
658 '%s'
659 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700660 elif "ParamChecker" == layer:
661 # TODO : Need to fix up the non-else cases below to do param checking as well
662 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
663 param0_name = proto.params[0].name
664 ret_val = ''
665 stmt = ''
666 param_checks = []
667 # Add code to check enums and structs
668 # TODO : Currently only validating enum values, need to validate everything
669 str_decl = False
Tobin Ehlis773371f2014-12-18 13:51:21 -0700670 prev_count_name = ''
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700671 for p in proto.params:
672 if xgl_helper.is_type(p.ty.strip('*').strip('const '), 'enum'):
673 if not str_decl:
674 param_checks.append(' char str[1024];')
675 str_decl = True
676 param_checks.append(' if (!validate_%s(%s)) {' % (p.ty, p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700677 param_checks.append(' sprintf(str, "Parameter %s to function %s has invalid value of %%i.", (int)%s);' % (p.name, proto.name, p.name))
678 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
679 param_checks.append(' }')
680 elif xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct') and 'const' in p.ty:
Tobin Ehlis773371f2014-12-18 13:51:21 -0700681 is_array = False
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700682 if not str_decl:
683 param_checks.append(' char str[1024];')
684 str_decl = True
685 if '*' in p.ty: # First check for null ptr
Tobin Ehlis773371f2014-12-18 13:51:21 -0700686 # If this is an input array, parse over all of the array elements
687 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
688 #if 'pImageViews' in p.name:
689 is_array = True
690 param_checks.append(' uint32_t i;')
691 param_checks.append(' for (i = 0; i < %s; i++) {' % prev_count_name)
692 param_checks.append(' if (!xgl_validate_%s(&%s[i])) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
693 param_checks.append(' sprintf(str, "Parameter %s[%%i] to function %s contains an invalid value.", i);' % (p.name, proto.name))
694 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
695 param_checks.append(' }')
696 param_checks.append(' }')
697 else:
698 param_checks.append(' if (!%s) {' % p.name)
699 param_checks.append(' sprintf(str, "Struct ptr parameter %s to function %s is NULL.");' % (p.name, proto.name))
700 param_checks.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
701 param_checks.append(' }')
702 param_checks.append(' else if (!xgl_validate_%s(%s)) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700703 else:
704 param_checks.append(' if (!xgl_validate_%s(%s)) {' % (p.ty.strip('const ').lower(), p.name))
Tobin Ehlis773371f2014-12-18 13:51:21 -0700705 if not is_array:
706 param_checks.append(' sprintf(str, "Parameter %s to function %s contains an invalid value.");' % (p.name, proto.name))
707 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
708 param_checks.append(' }')
709 if p.name.endswith('Count'):
710 prev_count_name = p.name
711 else:
712 prev_count_name = ''
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700713 if proto.ret != "XGL_VOID":
714 ret_val = "XGL_RESULT result = "
715 stmt = " return result;\n"
716 if proto.name == "EnumerateLayers":
717 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
718 funcs.append('%s%s\n'
719 '{\n'
720 ' char str[1024];\n'
721 ' if (gpu != NULL) {\n'
722 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
723 ' sprintf(str, "At start of layered %s\\n");\n'
724 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
725 ' pCurObj = gpuw;\n'
726 ' pthread_once(&tabOnce, initLayerTable);\n'
727 ' %snextTable.%s;\n'
728 ' sprintf(str, "Completed layered %s\\n");\n'
729 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
730 ' fflush(stdout);\n'
731 ' %s'
732 ' } else {\n'
733 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
734 ' return XGL_ERROR_INVALID_POINTER;\n'
735 ' // This layer compatible with all GPUs\n'
736 ' *pOutLayerCount = 1;\n'
737 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
738 ' return XGL_SUCCESS;\n'
739 ' }\n'
740 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
741 elif 'DbgRegisterMsgCallback' == proto.name:
742 funcs.append(self._gen_layer_dbg_callback_register())
743 elif 'DbgUnregisterMsgCallback' == proto.name:
744 funcs.append(self._gen_layer_dbg_callback_unregister())
745 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
746 funcs.append('%s%s\n'
747 '{\n'
748 '%s\n'
749 ' %snextTable.%s;\n'
750 '%s'
751 '}' % (qual, decl, "\n".join(param_checks), ret_val, proto.c_call(), stmt))
752 else:
753 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
754 funcs.append('%s%s\n'
755 '{\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700756 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700757 ' pCurObj = gpuw;\n'
758 ' pthread_once(&tabOnce, initLayerTable);\n'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700759 '%s\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700760 ' %snextTable.%s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700761 '%s'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700762 '}' % (qual, decl, proto.params[0].name, "\n".join(param_checks), ret_val, c_call, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600763
764 return "\n\n".join(funcs)
765
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700766 def _generate_extensions(self):
767 exts = []
768 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
769 exts.append('{')
770 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
771 exts.append('}')
772 exts.append('')
773 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
774 exts.append('{')
775 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
776 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
777 exts.append(' // Check the count first thing')
778 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
779 exts.append(' if (objCount > maxObjCount) {')
780 exts.append(' char str[1024];')
781 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));')
782 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
783 exts.append(' return XGL_ERROR_INVALID_VALUE;')
784 exts.append(' }')
785 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
786 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
787 exts.append(' if (!pTrav) {')
788 exts.append(' char str[1024];')
789 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);')
790 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
791 exts.append(' return XGL_ERROR_UNKNOWN;')
792 exts.append(' }')
793 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
794 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
795 exts.append(' }')
796 exts.append(' return XGL_SUCCESS;')
797 exts.append('}')
798
799 return "\n".join(exts)
800
Chia-I Wu706533e2015-01-05 13:18:57 +0800801 def _generate_layer_gpa_function(self, extensions=[]):
802 func_body = ["#include \"xgl_generic_intercept_proc_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600803 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
804 "{\n"
805 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800806 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600807 " if (gpu == NULL)\n"
808 " return NULL;\n"
809 " pCurObj = gpuw;\n"
810 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800811 " addr = layer_intercept_proc(funcName);\n"
812 " if (addr)\n"
813 " return addr;")
814
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700815 if 0 != len(extensions):
816 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800817 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700818 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600819 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600820 " if (gpuw->pGPA == NULL)\n"
821 " return NULL;\n"
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700822 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600823 " }\n"
824 "}\n")
825 return "\n".join(func_body)
826
827 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800828 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600829 func_body.append('static void initLayerTable()\n'
830 '{\n'
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600831 ' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600832 ' fpNextGPA = pCurObj->pGPA;\n'
833 ' assert(fpNextGPA);\n');
834
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800835 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600836 func_body.append("}\n")
837 return "\n".join(func_body)
838
839class LayerFuncsSubcommand(Subcommand):
840 def generate_header(self):
841 return '#include <xglLayer.h>\n#include "loader.h"'
842
843 def generate_body(self):
844 return self._generate_dispatch_entrypoints("static", True)
845
846class LayerDispatchSubcommand(Subcommand):
847 def generate_header(self):
848 return '#include "layer_wrappers.h"'
849
850 def generate_body(self):
851 return self._generate_layer_dispatch_table()
852
853class GenericLayerSubcommand(Subcommand):
854 def generate_header(self):
855 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'
856
857 def generate_body(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700858 body = [self._gen_layer_dbg_callback_header(),
859 self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700860 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600861 self._generate_layer_gpa_function()]
862
863 return "\n\n".join(body)
864
865class ApiDumpSubcommand(Subcommand):
866 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700867 header_txt = []
868 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')
869 header_txt.append('#define MAX_TID 513')
870 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
871 header_txt.append('static uint32_t maxTID = 0;')
872 header_txt.append('// Map actual TID to an index value and return that index')
873 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
874 header_txt.append('static uint32_t getTIDIndex() {')
875 header_txt.append(' pthread_t tid = pthread_self();')
876 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
877 header_txt.append(' if (tid == tidMapping[i])')
878 header_txt.append(' return i;')
879 header_txt.append(' }')
880 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
881 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
882 header_txt.append(' tidMapping[maxTID++] = tid;')
883 header_txt.append(' assert(maxTID < MAX_TID);')
884 header_txt.append(' return retVal;')
885 header_txt.append('}')
886 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600887
888 def generate_body(self):
889 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700890 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600891 self._generate_layer_gpa_function()]
892
893 return "\n\n".join(body)
894
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700895class ApiDumpCppSubcommand(Subcommand):
896 def generate_header(self):
897 header_txt = []
898 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')
899 header_txt.append('#define MAX_TID 513')
900 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
901 header_txt.append('static uint32_t maxTID = 0;')
902 header_txt.append('// Map actual TID to an index value and return that index')
903 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
904 header_txt.append('static uint32_t getTIDIndex() {')
905 header_txt.append(' pthread_t tid = pthread_self();')
906 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
907 header_txt.append(' if (tid == tidMapping[i])')
908 header_txt.append(' return i;')
909 header_txt.append(' }')
910 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
911 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
912 header_txt.append(' tidMapping[maxTID++] = tid;')
913 header_txt.append(' assert(maxTID < MAX_TID);')
914 header_txt.append(' return retVal;')
915 header_txt.append('}')
916 return "\n".join(header_txt)
917
918 def generate_body(self):
919 body = [self._generate_layer_dispatch_table(),
920 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp"),
921 self._generate_layer_gpa_function()]
922
923 return "\n\n".join(body)
924
Tobin Ehlis574b0142014-11-12 13:11:15 -0700925class ApiDumpFileSubcommand(Subcommand):
926 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700927 header_txt = []
928 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')
929 header_txt.append('#define MAX_TID 513')
930 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
931 header_txt.append('static uint32_t maxTID = 0;')
932 header_txt.append('// Map actual TID to an index value and return that index')
933 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
934 header_txt.append('static uint32_t getTIDIndex() {')
935 header_txt.append(' pthread_t tid = pthread_self();')
936 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
937 header_txt.append(' if (tid == tidMapping[i])')
938 header_txt.append(' return i;')
939 header_txt.append(' }')
940 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
941 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
942 header_txt.append(' tidMapping[maxTID++] = tid;')
943 header_txt.append(' assert(maxTID < MAX_TID);')
944 header_txt.append(' return retVal;')
945 header_txt.append('}')
946 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700947
948 def generate_body(self):
949 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700950 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700951 self._generate_layer_gpa_function()]
952
953 return "\n\n".join(body)
954
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700955class ApiDumpNoAddrSubcommand(Subcommand):
956 def generate_header(self):
957 header_txt = []
958 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')
959 header_txt.append('#define MAX_TID 513')
960 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
961 header_txt.append('static uint32_t maxTID = 0;')
962 header_txt.append('// Map actual TID to an index value and return that index')
963 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
964 header_txt.append('static uint32_t getTIDIndex() {')
965 header_txt.append(' pthread_t tid = pthread_self();')
966 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
967 header_txt.append(' if (tid == tidMapping[i])')
968 header_txt.append(' return i;')
969 header_txt.append(' }')
970 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
971 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
972 header_txt.append(' tidMapping[maxTID++] = tid;')
973 header_txt.append(' assert(maxTID < MAX_TID);')
974 header_txt.append(' return retVal;')
975 header_txt.append('}')
976 return "\n".join(header_txt)
977
978 def generate_body(self):
979 body = [self._generate_layer_dispatch_table(),
980 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
981 self._generate_layer_gpa_function()]
982
983 return "\n\n".join(body)
984
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700985class ApiDumpNoAddrCppSubcommand(Subcommand):
986 def generate_header(self):
987 header_txt = []
988 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')
989 header_txt.append('#define MAX_TID 513')
990 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
991 header_txt.append('static uint32_t maxTID = 0;')
992 header_txt.append('// Map actual TID to an index value and return that index')
993 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
994 header_txt.append('static uint32_t getTIDIndex() {')
995 header_txt.append(' pthread_t tid = pthread_self();')
996 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
997 header_txt.append(' if (tid == tidMapping[i])')
998 header_txt.append(' return i;')
999 header_txt.append(' }')
1000 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
1001 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
1002 header_txt.append(' tidMapping[maxTID++] = tid;')
1003 header_txt.append(' assert(maxTID < MAX_TID);')
1004 header_txt.append(' return retVal;')
1005 header_txt.append('}')
1006 return "\n".join(header_txt)
1007
1008 def generate_body(self):
1009 body = [self._generate_layer_dispatch_table(),
1010 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp", True),
1011 self._generate_layer_gpa_function()]
1012
1013 return "\n\n".join(body)
1014
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001015class ObjectTrackerSubcommand(Subcommand):
1016 def generate_header(self):
1017 header_txt = []
1018 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 -07001019 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
1020 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
1021 header_txt.append('// Ptr to LL of dbg functions')
1022 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
1023 header_txt.append('// Utility function to handle reporting')
1024 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
1025 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
1026 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
1027 header_txt.append(' XGL_BASE_OBJECT srcObject,')
1028 header_txt.append(' XGL_SIZE location,')
1029 header_txt.append(' XGL_INT msgCode,')
Chia-I Wua837c522014-12-16 10:47:33 +08001030 header_txt.append(' const char* pLayerPrefix,')
1031 header_txt.append(' const char* pMsg)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001032 header_txt.append('{')
1033 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
1034 header_txt.append(' if (pTrav) {')
1035 header_txt.append(' while (pTrav) {')
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001036 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001037 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001038 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001039 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001040 header_txt.append(' else {')
1041 header_txt.append(' switch (msgType) {')
1042 header_txt.append(' case XGL_DBG_MSG_ERROR:')
1043 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
1044 header_txt.append(' break;')
1045 header_txt.append(' case XGL_DBG_MSG_WARNING:')
1046 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
1047 header_txt.append(' break;')
1048 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
1049 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
1050 header_txt.append(' break;')
1051 header_txt.append(' default:')
1052 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
1053 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001054 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001055 header_txt.append(' }')
1056 header_txt.append('}')
1057 header_txt.append('// We maintain a "Global" list which links every object and a')
1058 header_txt.append('// per-Object list which just links objects of a given type')
1059 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
1060 header_txt.append('typedef struct _objNode {')
1061 header_txt.append(' OBJTRACK_NODE obj;')
1062 header_txt.append(' struct _objNode *pNextObj;')
1063 header_txt.append(' struct _objNode *pNextGlobal;')
1064 header_txt.append('} objNode;')
1065 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
1066 header_txt.append('static objNode *pGlobalHead = NULL;')
1067 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
1068 header_txt.append('static uint64_t numTotalObjs = 0;')
1069 header_txt.append('// Debug function to print global list and each individual object list')
1070 header_txt.append('static void ll_print_lists()')
1071 header_txt.append('{')
1072 header_txt.append(' objNode* pTrav = pGlobalHead;')
1073 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
1074 header_txt.append(' while (pTrav) {')
1075 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);')
1076 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1077 header_txt.append(' }')
1078 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
1079 header_txt.append(' pTrav = pObjectHead[i];')
1080 header_txt.append(' if (pTrav) {')
1081 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
1082 header_txt.append(' while (pTrav) {')
1083 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);')
1084 header_txt.append(' pTrav = pTrav->pNextObj;')
1085 header_txt.append(' }')
1086 header_txt.append(' }')
1087 header_txt.append(' }')
1088 header_txt.append('}')
1089 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1090 header_txt.append(' char str[1024];')
1091 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1092 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1093 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
1094 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
1095 header_txt.append(' pNewObjNode->obj.objType = objType;')
1096 header_txt.append(' pNewObjNode->obj.numUses = 0;')
1097 header_txt.append(' // insert at front of global list')
1098 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
1099 header_txt.append(' pGlobalHead = pNewObjNode;')
1100 header_txt.append(' // insert at front of object list')
1101 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
1102 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
1103 header_txt.append(' // increment obj counts')
1104 header_txt.append(' numObjs[objType]++;')
1105 header_txt.append(' numTotalObjs++;')
1106 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 +08001107 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001108 header_txt.append('}')
1109 header_txt.append('// Traverse global list and return type for given object')
1110 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
1111 header_txt.append(' objNode *pTrav = pGlobalHead;')
1112 header_txt.append(' while (pTrav) {')
1113 header_txt.append(' if (pTrav->obj.pObj == object)')
1114 header_txt.append(' return pTrav->obj.objType;')
1115 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1116 header_txt.append(' }')
1117 header_txt.append(' char str[1024];')
1118 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
1119 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
1120 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
1121 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001122 header_txt.append('#if 0')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001123 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1124 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1125 header_txt.append(' while (pTrav) {')
1126 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1127 header_txt.append(' return pTrav->obj.numUses;')
1128 header_txt.append(' }')
1129 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001130 header_txt.append(' }')
1131 header_txt.append(' return 0;')
1132 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001133 header_txt.append('#endif')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001134 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1135 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001136 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001137 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1138 header_txt.append(' pTrav->obj.numUses++;')
1139 header_txt.append(' char str[1024];')
1140 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);')
1141 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1142 header_txt.append(' return;')
1143 header_txt.append(' }')
1144 header_txt.append(' pTrav = pTrav->pNextObj;')
1145 header_txt.append(' }')
1146 header_txt.append(' // If we do not find obj, insert it and then increment count')
1147 header_txt.append(' char str[1024];')
1148 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));')
1149 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1150 header_txt.append('')
1151 header_txt.append(' ll_insert_obj(pObj, objType);')
1152 header_txt.append(' ll_increment_use_count(pObj, objType);')
1153 header_txt.append('}')
1154 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1155 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1156 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
1157 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1158 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1159 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1160 header_txt.append(' while (pTrav) {')
1161 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1162 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1163 header_txt.append(' // update HEAD of Obj list as needed')
1164 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1165 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1166 header_txt.append(' assert(numObjs[objType] > 0);')
1167 header_txt.append(' numObjs[objType]--;')
1168 header_txt.append(' char str[1024];')
1169 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1170 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 -06001171 header_txt.append(' return;')
1172 header_txt.append(' }')
1173 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001174 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001175 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001176 header_txt.append(' char str[1024];')
1177 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));')
1178 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1179 header_txt.append('}')
1180 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1181 header_txt.append('// remove obj from global list')
1182 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
1183 header_txt.append(' objNode *pTrav = pGlobalHead;')
1184 header_txt.append(' objNode *pPrev = pGlobalHead;')
1185 header_txt.append(' while (pTrav) {')
1186 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1187 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1188 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1189 header_txt.append(' // update HEAD of global list if needed')
1190 header_txt.append(' if (pGlobalHead == pTrav)')
1191 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
1192 header_txt.append(' free(pTrav);')
1193 header_txt.append(' assert(numTotalObjs > 0);')
1194 header_txt.append(' numTotalObjs--;')
1195 header_txt.append(' char str[1024];')
1196 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));')
1197 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1198 header_txt.append(' return;')
1199 header_txt.append(' }')
1200 header_txt.append(' pPrev = pTrav;')
1201 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1202 header_txt.append(' }')
1203 header_txt.append(' char str[1024];')
1204 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1205 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 -06001206 header_txt.append('}')
1207
1208 return "\n".join(header_txt)
1209
1210 def generate_body(self):
1211 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -07001212 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001213 self._generate_extensions(),
1214 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001215
1216 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001217
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001218class ParamCheckerSubcommand(Subcommand):
1219 def generate_header(self):
1220 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'
1221
1222 def generate_body(self):
1223 body = [self._gen_layer_dbg_callback_header(),
1224 self._generate_layer_dispatch_table(),
1225 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ParamChecker"),
1226 self._generate_layer_gpa_function()]
1227
1228 return "\n\n".join(body)
1229
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001230def main():
1231 subcommands = {
1232 "layer-funcs" : LayerFuncsSubcommand,
1233 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001234 "Generic" : GenericLayerSubcommand,
1235 "ApiDump" : ApiDumpSubcommand,
1236 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -07001237 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001238 "ApiDumpCpp" : ApiDumpCppSubcommand,
1239 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001240 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001241 "ParamChecker" : ParamCheckerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001242 }
1243
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001244 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1245 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001246 print
1247 print("Available sucommands are: %s" % " ".join(subcommands))
1248 exit(1)
1249
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001250 hfp = xgl_helper.HeaderFileParser(sys.argv[2])
1251 hfp.parse()
1252 xgl_helper.enum_val_dict = hfp.get_enum_val_dict()
1253 xgl_helper.enum_type_dict = hfp.get_enum_type_dict()
1254 xgl_helper.struct_dict = hfp.get_struct_dict()
1255 xgl_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1256 xgl_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1257 xgl_helper.types_dict = hfp.get_types_dict()
1258
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001259 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1260 subcmd.run()
1261
1262if __name__ == "__main__":
1263 main()