blob: 85c4a81933e824f864288fcb793d1ba7bb75ca5c [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 = ''
Jon Ashburn068f13d2015-01-13 08:38:14 -0700291 cis_param_index = [] # 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 = ''
Jon Ashburn068f13d2015-01-13 08:38:14 -0700402 sp_param_dict = {} # 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'):
Courtney Goeltzenleuchter08cf7cc2015-01-13 15:32:18 -0700447 if '*' in p.ty:
448 prev_count_name = "*%s" % p.name
449 else:
450 prev_count_name = p.name
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700451 else:
452 prev_count_name = ''
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600453 log_func = log_func.strip(', ')
454 if proto.ret != "XGL_VOID":
455 log_func += ') = %s\\n"'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700456 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600457 else:
458 log_func += ')\\n"'
459 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700460 if len(sp_param_dict) > 0:
461 i_decl = False
462 log_func += '\n char *pTmpStr = "";'
463 for sp_index in sorted(sp_param_dict):
464 # TODO : Clean this if/else block up, too much duplicated code
465 if 'index' == sp_param_dict[sp_index]:
466 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
467 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
468 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
469 if "File" in layer:
470 if no_addr:
471 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
472 else:
473 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 -0700474 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700475 if no_addr:
476 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
477 else:
478 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
479 log_func += '\n fflush(stdout);'
480 log_func += '\n free(pTmpStr);\n }'
481 else: # should have a count value stored to iterate over array
482 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
483 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 -0700484 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700485 cis_print_func = 'pTmpStr = (char*)malloc(sizeof(char));\n sprintf(pTmpStr, " %%p", %s[i]);' % proto.params[sp_index].name
486 if not i_decl:
487 log_func += '\n uint32_t i;'
488 i_decl = True
Jon Ashburn48637592015-01-14 08:52:37 -0700489 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700490 log_func += '\n %s' % (cis_print_func)
491 if "File" in layer:
492 if no_addr:
493 log_func += '\n fprintf(pOutFile, " %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
494 else:
495 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)
496 else:
497 if no_addr:
498 log_func += '\n printf(" %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
499 else:
500 log_func += '\n printf(" %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
501 log_func += '\n fflush(stdout);'
502 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700503 if proto.name == "EnumerateLayers":
504 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
505 funcs.append('%s%s\n'
506 '{\n'
507 ' if (gpu != NULL) {\n'
508 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
509 ' pCurObj = gpuw;\n'
510 ' pthread_once(&tabOnce, initLayerTable);\n'
511 ' %snextTable.%s;\n'
512 ' %s %s %s\n'
513 ' %s'
514 ' } else {\n'
515 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
516 ' return XGL_ERROR_INVALID_POINTER;\n'
517 ' // This layer compatible with all GPUs\n'
518 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800519 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700520 ' return XGL_SUCCESS;\n'
521 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700522 '}' % (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 -0700523 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600524 funcs.append('%s%s\n'
525 '{\n'
526 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700527 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600528 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700529 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600530 else:
531 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
532 funcs.append('%s%s\n'
533 '{\n'
534 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
535 ' pCurObj = gpuw;\n'
536 ' pthread_once(&tabOnce, initLayerTable);\n'
537 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700538 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600539 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700540 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700541 elif "ObjectTracker" == layer:
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800542 obj_type_mapping = {
543 "XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU",
544 "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
545 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE",
546 "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
547 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY",
548 "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
549 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL",
550 "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
551 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE",
552 "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
553 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER",
554 "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
555 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE",
556 "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
557 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER",
558 "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
559 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW",
560 "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
Tony Barbourfa6cac72015-01-16 14:27:35 -0700561 "XGL_DYNAMIC_VP_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE",
562 "XGL_DYNAMIC_RS_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800563 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE",
Tony Barbourfa6cac72015-01-16 14:27:35 -0700564 "XGL_DYNAMIC_CB_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
565 "XGL_DYNAMIC_DS_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE",
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800566 "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
567 "XGL_OBJECT" : "ll_get_obj_type(object)"
568 }
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700569
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600570 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
571 param0_name = proto.params[0].name
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700572 p0_type = proto.params[0].ty
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600573 create_line = ''
574 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700575 if 'DbgRegisterMsgCallback' in proto.name:
576 using_line = ' // This layer intercepts callbacks\n'
577 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
578 using_line += ' if (!pNewDbgFuncNode)\n'
579 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
580 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
581 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
582 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
583 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
584 elif 'DbgUnregisterMsgCallback' in proto.name:
585 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
586 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
587 using_line += ' while (pTrav) {\n'
588 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
589 using_line += ' pPrev->pNext = pTrav->pNext;\n'
590 using_line += ' if (pDbgFunctionHead == pTrav)\n'
591 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
592 using_line += ' free(pTrav);\n'
593 using_line += ' break;\n'
594 using_line += ' }\n'
595 using_line += ' pPrev = pTrav;\n'
596 using_line += ' pTrav = pTrav->pNext;\n'
597 using_line += ' }\n'
598 elif 'GlobalOption' in proto.name:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600599 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700600 else:
601 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
602 if 'Create' in proto.name or 'Alloc' in proto.name:
603 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
604 if 'DestroyObject' in proto.name:
605 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
606 using_line = ''
607 else:
608 if 'Destroy' in proto.name or 'Free' in proto.name:
609 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
610 using_line = ''
611 if 'DestroyDevice' in proto.name:
612 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
613 destroy_line += ' char str[1024];\n'
614 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'
615 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
616 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600617 ret_val = ''
618 stmt = ''
619 if proto.ret != "XGL_VOID":
620 ret_val = "XGL_RESULT result = "
621 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700622 if proto.name == "EnumerateLayers":
623 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
624 funcs.append('%s%s\n'
625 '{\n'
626 ' if (gpu != NULL) {\n'
627 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
628 ' %s'
629 ' pCurObj = gpuw;\n'
630 ' pthread_once(&tabOnce, initLayerTable);\n'
631 ' %snextTable.%s;\n'
632 ' %s%s'
633 ' %s'
634 ' } else {\n'
635 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
636 ' return XGL_ERROR_INVALID_POINTER;\n'
637 ' // This layer compatible with all GPUs\n'
638 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800639 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700640 ' return XGL_SUCCESS;\n'
641 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700642 '}' % (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 -0700643 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600644 funcs.append('%s%s\n'
645 '{\n'
646 '%s'
647 ' %snextTable.%s;\n'
648 '%s%s'
649 '%s'
650 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
651 else:
652 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
653 funcs.append('%s%s\n'
654 '{\n'
655 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
656 '%s'
657 ' pCurObj = gpuw;\n'
658 ' pthread_once(&tabOnce, initLayerTable);\n'
659 ' %snextTable.%s;\n'
660 '%s%s'
661 '%s'
662 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700663 elif "ParamChecker" == layer:
664 # TODO : Need to fix up the non-else cases below to do param checking as well
665 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
666 param0_name = proto.params[0].name
667 ret_val = ''
668 stmt = ''
669 param_checks = []
670 # Add code to check enums and structs
671 # TODO : Currently only validating enum values, need to validate everything
672 str_decl = False
Tobin Ehlis773371f2014-12-18 13:51:21 -0700673 prev_count_name = ''
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700674 for p in proto.params:
675 if xgl_helper.is_type(p.ty.strip('*').strip('const '), 'enum'):
676 if not str_decl:
677 param_checks.append(' char str[1024];')
678 str_decl = True
679 param_checks.append(' if (!validate_%s(%s)) {' % (p.ty, p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700680 param_checks.append(' sprintf(str, "Parameter %s to function %s has invalid value of %%i.", (int)%s);' % (p.name, proto.name, p.name))
681 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
682 param_checks.append(' }')
683 elif xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct') and 'const' in p.ty:
Tobin Ehlis773371f2014-12-18 13:51:21 -0700684 is_array = False
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700685 if not str_decl:
686 param_checks.append(' char str[1024];')
687 str_decl = True
688 if '*' in p.ty: # First check for null ptr
Tobin Ehlis773371f2014-12-18 13:51:21 -0700689 # If this is an input array, parse over all of the array elements
690 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
691 #if 'pImageViews' in p.name:
692 is_array = True
693 param_checks.append(' uint32_t i;')
694 param_checks.append(' for (i = 0; i < %s; i++) {' % prev_count_name)
695 param_checks.append(' if (!xgl_validate_%s(&%s[i])) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
696 param_checks.append(' sprintf(str, "Parameter %s[%%i] to function %s contains an invalid value.", i);' % (p.name, proto.name))
697 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
698 param_checks.append(' }')
699 param_checks.append(' }')
700 else:
701 param_checks.append(' if (!%s) {' % p.name)
702 param_checks.append(' sprintf(str, "Struct ptr parameter %s to function %s is NULL.");' % (p.name, proto.name))
703 param_checks.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
704 param_checks.append(' }')
705 param_checks.append(' else if (!xgl_validate_%s(%s)) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700706 else:
707 param_checks.append(' if (!xgl_validate_%s(%s)) {' % (p.ty.strip('const ').lower(), p.name))
Tobin Ehlis773371f2014-12-18 13:51:21 -0700708 if not is_array:
709 param_checks.append(' sprintf(str, "Parameter %s to function %s contains an invalid value.");' % (p.name, proto.name))
710 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
711 param_checks.append(' }')
712 if p.name.endswith('Count'):
713 prev_count_name = p.name
714 else:
715 prev_count_name = ''
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700716 if proto.ret != "XGL_VOID":
717 ret_val = "XGL_RESULT result = "
718 stmt = " return result;\n"
719 if proto.name == "EnumerateLayers":
720 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
721 funcs.append('%s%s\n'
722 '{\n'
723 ' char str[1024];\n'
724 ' if (gpu != NULL) {\n'
725 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
726 ' sprintf(str, "At start of layered %s\\n");\n'
727 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
728 ' pCurObj = gpuw;\n'
729 ' pthread_once(&tabOnce, initLayerTable);\n'
730 ' %snextTable.%s;\n'
731 ' sprintf(str, "Completed layered %s\\n");\n'
732 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
733 ' fflush(stdout);\n'
734 ' %s'
735 ' } else {\n'
736 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
737 ' return XGL_ERROR_INVALID_POINTER;\n'
738 ' // This layer compatible with all GPUs\n'
739 ' *pOutLayerCount = 1;\n'
740 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
741 ' return XGL_SUCCESS;\n'
742 ' }\n'
743 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
744 elif 'DbgRegisterMsgCallback' == proto.name:
745 funcs.append(self._gen_layer_dbg_callback_register())
746 elif 'DbgUnregisterMsgCallback' == proto.name:
747 funcs.append(self._gen_layer_dbg_callback_unregister())
748 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
749 funcs.append('%s%s\n'
750 '{\n'
751 '%s\n'
752 ' %snextTable.%s;\n'
753 '%s'
754 '}' % (qual, decl, "\n".join(param_checks), ret_val, proto.c_call(), stmt))
755 else:
756 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
757 funcs.append('%s%s\n'
758 '{\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700759 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700760 ' pCurObj = gpuw;\n'
761 ' pthread_once(&tabOnce, initLayerTable);\n'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700762 '%s\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700763 ' %snextTable.%s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700764 '%s'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700765 '}' % (qual, decl, proto.params[0].name, "\n".join(param_checks), ret_val, c_call, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600766
767 return "\n\n".join(funcs)
768
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700769 def _generate_extensions(self):
770 exts = []
771 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
772 exts.append('{')
773 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
774 exts.append('}')
775 exts.append('')
776 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
777 exts.append('{')
778 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
779 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
780 exts.append(' // Check the count first thing')
781 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
782 exts.append(' if (objCount > maxObjCount) {')
783 exts.append(' char str[1024];')
784 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));')
785 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
786 exts.append(' return XGL_ERROR_INVALID_VALUE;')
787 exts.append(' }')
788 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
789 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
790 exts.append(' if (!pTrav) {')
791 exts.append(' char str[1024];')
792 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);')
793 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
794 exts.append(' return XGL_ERROR_UNKNOWN;')
795 exts.append(' }')
796 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
797 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
798 exts.append(' }')
799 exts.append(' return XGL_SUCCESS;')
800 exts.append('}')
801
802 return "\n".join(exts)
803
Chia-I Wu706533e2015-01-05 13:18:57 +0800804 def _generate_layer_gpa_function(self, extensions=[]):
805 func_body = ["#include \"xgl_generic_intercept_proc_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600806 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
807 "{\n"
808 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800809 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600810 " if (gpu == NULL)\n"
811 " return NULL;\n"
812 " pCurObj = gpuw;\n"
813 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800814 " addr = layer_intercept_proc(funcName);\n"
815 " if (addr)\n"
816 " return addr;")
817
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700818 if 0 != len(extensions):
819 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800820 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700821 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600822 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600823 " if (gpuw->pGPA == NULL)\n"
824 " return NULL;\n"
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700825 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600826 " }\n"
827 "}\n")
828 return "\n".join(func_body)
829
830 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800831 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600832 func_body.append('static void initLayerTable()\n'
833 '{\n'
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600834 ' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600835 ' fpNextGPA = pCurObj->pGPA;\n'
836 ' assert(fpNextGPA);\n');
837
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800838 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600839 func_body.append("}\n")
840 return "\n".join(func_body)
841
842class LayerFuncsSubcommand(Subcommand):
843 def generate_header(self):
844 return '#include <xglLayer.h>\n#include "loader.h"'
845
846 def generate_body(self):
847 return self._generate_dispatch_entrypoints("static", True)
848
849class LayerDispatchSubcommand(Subcommand):
850 def generate_header(self):
851 return '#include "layer_wrappers.h"'
852
853 def generate_body(self):
854 return self._generate_layer_dispatch_table()
855
856class GenericLayerSubcommand(Subcommand):
857 def generate_header(self):
858 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'
859
860 def generate_body(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700861 body = [self._gen_layer_dbg_callback_header(),
862 self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700863 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600864 self._generate_layer_gpa_function()]
865
866 return "\n\n".join(body)
867
868class ApiDumpSubcommand(Subcommand):
869 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700870 header_txt = []
871 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')
872 header_txt.append('#define MAX_TID 513')
873 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
874 header_txt.append('static uint32_t maxTID = 0;')
875 header_txt.append('// Map actual TID to an index value and return that index')
876 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
877 header_txt.append('static uint32_t getTIDIndex() {')
878 header_txt.append(' pthread_t tid = pthread_self();')
879 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
880 header_txt.append(' if (tid == tidMapping[i])')
881 header_txt.append(' return i;')
882 header_txt.append(' }')
883 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
884 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
885 header_txt.append(' tidMapping[maxTID++] = tid;')
886 header_txt.append(' assert(maxTID < MAX_TID);')
887 header_txt.append(' return retVal;')
888 header_txt.append('}')
889 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600890
891 def generate_body(self):
892 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700893 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600894 self._generate_layer_gpa_function()]
895
896 return "\n\n".join(body)
897
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700898class ApiDumpCppSubcommand(Subcommand):
899 def generate_header(self):
900 header_txt = []
901 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')
902 header_txt.append('#define MAX_TID 513')
903 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
904 header_txt.append('static uint32_t maxTID = 0;')
905 header_txt.append('// Map actual TID to an index value and return that index')
906 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
907 header_txt.append('static uint32_t getTIDIndex() {')
908 header_txt.append(' pthread_t tid = pthread_self();')
909 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
910 header_txt.append(' if (tid == tidMapping[i])')
911 header_txt.append(' return i;')
912 header_txt.append(' }')
913 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
914 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
915 header_txt.append(' tidMapping[maxTID++] = tid;')
916 header_txt.append(' assert(maxTID < MAX_TID);')
917 header_txt.append(' return retVal;')
918 header_txt.append('}')
919 return "\n".join(header_txt)
920
921 def generate_body(self):
922 body = [self._generate_layer_dispatch_table(),
923 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp"),
924 self._generate_layer_gpa_function()]
925
926 return "\n\n".join(body)
927
Tobin Ehlis574b0142014-11-12 13:11:15 -0700928class ApiDumpFileSubcommand(Subcommand):
929 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700930 header_txt = []
931 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')
932 header_txt.append('#define MAX_TID 513')
933 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
934 header_txt.append('static uint32_t maxTID = 0;')
935 header_txt.append('// Map actual TID to an index value and return that index')
936 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
937 header_txt.append('static uint32_t getTIDIndex() {')
938 header_txt.append(' pthread_t tid = pthread_self();')
939 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
940 header_txt.append(' if (tid == tidMapping[i])')
941 header_txt.append(' return i;')
942 header_txt.append(' }')
943 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
944 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
945 header_txt.append(' tidMapping[maxTID++] = tid;')
946 header_txt.append(' assert(maxTID < MAX_TID);')
947 header_txt.append(' return retVal;')
948 header_txt.append('}')
949 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700950
951 def generate_body(self):
952 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700953 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700954 self._generate_layer_gpa_function()]
955
956 return "\n\n".join(body)
957
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700958class ApiDumpNoAddrSubcommand(Subcommand):
959 def generate_header(self):
960 header_txt = []
961 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')
962 header_txt.append('#define MAX_TID 513')
963 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
964 header_txt.append('static uint32_t maxTID = 0;')
965 header_txt.append('// Map actual TID to an index value and return that index')
966 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
967 header_txt.append('static uint32_t getTIDIndex() {')
968 header_txt.append(' pthread_t tid = pthread_self();')
969 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
970 header_txt.append(' if (tid == tidMapping[i])')
971 header_txt.append(' return i;')
972 header_txt.append(' }')
973 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
974 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
975 header_txt.append(' tidMapping[maxTID++] = tid;')
976 header_txt.append(' assert(maxTID < MAX_TID);')
977 header_txt.append(' return retVal;')
978 header_txt.append('}')
979 return "\n".join(header_txt)
980
981 def generate_body(self):
982 body = [self._generate_layer_dispatch_table(),
983 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
984 self._generate_layer_gpa_function()]
985
986 return "\n\n".join(body)
987
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700988class ApiDumpNoAddrCppSubcommand(Subcommand):
989 def generate_header(self):
990 header_txt = []
991 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')
992 header_txt.append('#define MAX_TID 513')
993 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
994 header_txt.append('static uint32_t maxTID = 0;')
995 header_txt.append('// Map actual TID to an index value and return that index')
996 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
997 header_txt.append('static uint32_t getTIDIndex() {')
998 header_txt.append(' pthread_t tid = pthread_self();')
999 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
1000 header_txt.append(' if (tid == tidMapping[i])')
1001 header_txt.append(' return i;')
1002 header_txt.append(' }')
1003 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
1004 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
1005 header_txt.append(' tidMapping[maxTID++] = tid;')
1006 header_txt.append(' assert(maxTID < MAX_TID);')
1007 header_txt.append(' return retVal;')
1008 header_txt.append('}')
1009 return "\n".join(header_txt)
1010
1011 def generate_body(self):
1012 body = [self._generate_layer_dispatch_table(),
1013 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp", True),
1014 self._generate_layer_gpa_function()]
1015
1016 return "\n\n".join(body)
1017
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001018class ObjectTrackerSubcommand(Subcommand):
1019 def generate_header(self):
1020 header_txt = []
1021 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 -07001022 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
1023 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
1024 header_txt.append('// Ptr to LL of dbg functions')
1025 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
1026 header_txt.append('// Utility function to handle reporting')
1027 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
1028 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
1029 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
1030 header_txt.append(' XGL_BASE_OBJECT srcObject,')
1031 header_txt.append(' XGL_SIZE location,')
1032 header_txt.append(' XGL_INT msgCode,')
Chia-I Wua837c522014-12-16 10:47:33 +08001033 header_txt.append(' const char* pLayerPrefix,')
1034 header_txt.append(' const char* pMsg)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001035 header_txt.append('{')
1036 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
1037 header_txt.append(' if (pTrav) {')
1038 header_txt.append(' while (pTrav) {')
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001039 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001040 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001041 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001042 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001043 header_txt.append(' else {')
1044 header_txt.append(' switch (msgType) {')
1045 header_txt.append(' case XGL_DBG_MSG_ERROR:')
1046 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
1047 header_txt.append(' break;')
1048 header_txt.append(' case XGL_DBG_MSG_WARNING:')
1049 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
1050 header_txt.append(' break;')
1051 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
1052 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
1053 header_txt.append(' break;')
1054 header_txt.append(' default:')
1055 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
1056 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001057 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001058 header_txt.append(' }')
1059 header_txt.append('}')
1060 header_txt.append('// We maintain a "Global" list which links every object and a')
1061 header_txt.append('// per-Object list which just links objects of a given type')
1062 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
1063 header_txt.append('typedef struct _objNode {')
1064 header_txt.append(' OBJTRACK_NODE obj;')
1065 header_txt.append(' struct _objNode *pNextObj;')
1066 header_txt.append(' struct _objNode *pNextGlobal;')
1067 header_txt.append('} objNode;')
1068 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
1069 header_txt.append('static objNode *pGlobalHead = NULL;')
1070 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
1071 header_txt.append('static uint64_t numTotalObjs = 0;')
1072 header_txt.append('// Debug function to print global list and each individual object list')
1073 header_txt.append('static void ll_print_lists()')
1074 header_txt.append('{')
1075 header_txt.append(' objNode* pTrav = pGlobalHead;')
1076 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
1077 header_txt.append(' while (pTrav) {')
1078 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);')
1079 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1080 header_txt.append(' }')
1081 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
1082 header_txt.append(' pTrav = pObjectHead[i];')
1083 header_txt.append(' if (pTrav) {')
1084 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
1085 header_txt.append(' while (pTrav) {')
1086 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);')
1087 header_txt.append(' pTrav = pTrav->pNextObj;')
1088 header_txt.append(' }')
1089 header_txt.append(' }')
1090 header_txt.append(' }')
1091 header_txt.append('}')
1092 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1093 header_txt.append(' char str[1024];')
1094 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1095 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1096 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
1097 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
1098 header_txt.append(' pNewObjNode->obj.objType = objType;')
1099 header_txt.append(' pNewObjNode->obj.numUses = 0;')
1100 header_txt.append(' // insert at front of global list')
1101 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
1102 header_txt.append(' pGlobalHead = pNewObjNode;')
1103 header_txt.append(' // insert at front of object list')
1104 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
1105 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
1106 header_txt.append(' // increment obj counts')
1107 header_txt.append(' numObjs[objType]++;')
1108 header_txt.append(' numTotalObjs++;')
1109 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 +08001110 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001111 header_txt.append('}')
1112 header_txt.append('// Traverse global list and return type for given object')
1113 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
1114 header_txt.append(' objNode *pTrav = pGlobalHead;')
1115 header_txt.append(' while (pTrav) {')
1116 header_txt.append(' if (pTrav->obj.pObj == object)')
1117 header_txt.append(' return pTrav->obj.objType;')
1118 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1119 header_txt.append(' }')
1120 header_txt.append(' char str[1024];')
1121 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
1122 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
1123 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
1124 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001125 header_txt.append('#if 0')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001126 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1127 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1128 header_txt.append(' while (pTrav) {')
1129 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1130 header_txt.append(' return pTrav->obj.numUses;')
1131 header_txt.append(' }')
1132 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001133 header_txt.append(' }')
1134 header_txt.append(' return 0;')
1135 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001136 header_txt.append('#endif')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001137 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1138 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001139 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001140 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1141 header_txt.append(' pTrav->obj.numUses++;')
1142 header_txt.append(' char str[1024];')
1143 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);')
1144 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1145 header_txt.append(' return;')
1146 header_txt.append(' }')
1147 header_txt.append(' pTrav = pTrav->pNextObj;')
1148 header_txt.append(' }')
1149 header_txt.append(' // If we do not find obj, insert it and then increment count')
1150 header_txt.append(' char str[1024];')
1151 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));')
1152 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1153 header_txt.append('')
1154 header_txt.append(' ll_insert_obj(pObj, objType);')
1155 header_txt.append(' ll_increment_use_count(pObj, objType);')
1156 header_txt.append('}')
1157 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1158 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1159 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
1160 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
1161 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1162 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1163 header_txt.append(' while (pTrav) {')
1164 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1165 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1166 header_txt.append(' // update HEAD of Obj list as needed')
1167 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1168 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1169 header_txt.append(' assert(numObjs[objType] > 0);')
1170 header_txt.append(' numObjs[objType]--;')
1171 header_txt.append(' char str[1024];')
1172 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1173 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 -06001174 header_txt.append(' return;')
1175 header_txt.append(' }')
1176 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001177 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001178 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001179 header_txt.append(' char str[1024];')
1180 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));')
1181 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1182 header_txt.append('}')
1183 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1184 header_txt.append('// remove obj from global list')
1185 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
1186 header_txt.append(' objNode *pTrav = pGlobalHead;')
1187 header_txt.append(' objNode *pPrev = pGlobalHead;')
1188 header_txt.append(' while (pTrav) {')
1189 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1190 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1191 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1192 header_txt.append(' // update HEAD of global list if needed')
1193 header_txt.append(' if (pGlobalHead == pTrav)')
1194 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
1195 header_txt.append(' free(pTrav);')
1196 header_txt.append(' assert(numTotalObjs > 0);')
1197 header_txt.append(' numTotalObjs--;')
1198 header_txt.append(' char str[1024];')
1199 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));')
1200 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1201 header_txt.append(' return;')
1202 header_txt.append(' }')
1203 header_txt.append(' pPrev = pTrav;')
1204 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1205 header_txt.append(' }')
1206 header_txt.append(' char str[1024];')
1207 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1208 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 -06001209 header_txt.append('}')
1210
1211 return "\n".join(header_txt)
1212
1213 def generate_body(self):
1214 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -07001215 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001216 self._generate_extensions(),
1217 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001218
1219 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001220
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001221class ParamCheckerSubcommand(Subcommand):
1222 def generate_header(self):
1223 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'
1224
1225 def generate_body(self):
1226 body = [self._gen_layer_dbg_callback_header(),
1227 self._generate_layer_dispatch_table(),
1228 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ParamChecker"),
1229 self._generate_layer_gpa_function()]
1230
1231 return "\n\n".join(body)
1232
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001233def main():
1234 subcommands = {
1235 "layer-funcs" : LayerFuncsSubcommand,
1236 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001237 "Generic" : GenericLayerSubcommand,
1238 "ApiDump" : ApiDumpSubcommand,
1239 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -07001240 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001241 "ApiDumpCpp" : ApiDumpCppSubcommand,
1242 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001243 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001244 "ParamChecker" : ParamCheckerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001245 }
1246
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001247 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1248 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001249 print
1250 print("Available sucommands are: %s" % " ".join(subcommands))
1251 exit(1)
1252
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001253 hfp = xgl_helper.HeaderFileParser(sys.argv[2])
1254 hfp.parse()
1255 xgl_helper.enum_val_dict = hfp.get_enum_val_dict()
1256 xgl_helper.enum_type_dict = hfp.get_enum_type_dict()
1257 xgl_helper.struct_dict = hfp.get_struct_dict()
1258 xgl_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1259 xgl_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1260 xgl_helper.types_dict = hfp.get_types_dict()
1261
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001262 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1263 subcmd.run()
1264
1265if __name__ == "__main__":
1266 main()