blob: 5999aeb8e71e00dba5315a4e32f2288cd3946059 [file] [log] [blame]
Tobin Ehlis12076fc2014-10-22 09:06:33 -06001#!/usr/bin/env python3
2#
3# XGL
4#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
27
28import sys
29
30import xgl
31
32class Subcommand(object):
33 def __init__(self, argv):
34 self.argv = argv
Chia-I Wuc4f24e82015-01-01 08:46:31 +080035 self.headers = xgl.headers
36 self.protos = xgl.protos
Tobin Ehlis12076fc2014-10-22 09:06:33 -060037
38 def run(self):
Tobin Ehlis12076fc2014-10-22 09:06:33 -060039 print(self.generate())
40
41 def generate(self):
42 copyright = self.generate_copyright()
43 header = self.generate_header()
44 body = self.generate_body()
45 footer = self.generate_footer()
46
47 contents = []
48 if copyright:
49 contents.append(copyright)
50 if header:
51 contents.append(header)
52 if body:
53 contents.append(body)
54 if footer:
55 contents.append(footer)
56
57 return "\n\n".join(contents)
58
59 def generate_copyright(self):
60 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
61
62/*
63 * XGL
64 *
65 * Copyright (C) 2014 LunarG, Inc.
66 *
67 * Permission is hereby granted, free of charge, to any person obtaining a
68 * copy of this software and associated documentation files (the "Software"),
69 * to deal in the Software without restriction, including without limitation
70 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
71 * and/or sell copies of the Software, and to permit persons to whom the
72 * Software is furnished to do so, subject to the following conditions:
73 *
74 * The above copyright notice and this permission notice shall be included
75 * in all copies or substantial portions of the Software.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
82 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
83 * DEALINGS IN THE SOFTWARE.
84 */"""
85
86 def generate_header(self):
87 return "\n".join(["#include <" + h + ">" for h in self.headers])
88
89 def generate_body(self):
90 pass
91
92 def generate_footer(self):
93 pass
94
95 # Return set of printf '%' qualifier and input to that qualifier
Tobin Ehlise7271572014-11-19 15:52:46 -070096 def _get_printf_params(self, xgl_type, name, output_param):
Tobin Ehlis12076fc2014-10-22 09:06:33 -060097 # TODO : Need ENUM and STRUCT checks here
98 if "_TYPE" in xgl_type: # TODO : This should be generic ENUM check
99 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
100 if "XGL_CHAR*" == xgl_type:
101 return ("%s", name)
102 if "UINT64" in xgl_type:
103 if '*' in xgl_type:
104 return ("%lu", "*%s" % name)
105 return ("%lu", name)
Chia-I Wu99ff89d2014-12-27 14:14:50 +0800106 if "SIZE" in xgl_type:
107 if '*' in xgl_type:
108 return ("%zu", "*%s" % name)
109 return ("%zu", name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600110 if "FLOAT" in xgl_type:
111 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
112 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
113 return ("%f", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700114 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600115 return ("%u", name)
Chia-I Wu99ff89d2014-12-27 14:14:50 +0800116 if True in [t in xgl_type for t in ["INT", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600117 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
118 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
119 if '*' in xgl_type:
Jon Ashburn52f79b52014-12-12 16:10:45 -0700120 return ("%i", "*(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600121 return ("%i", name)
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700122 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Jon Ashburn52f79b52014-12-12 16:10:45 -0700123 if "XGL_FORMAT" == xgl_type:
124 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_XGL_CHANNEL_FORMAT(%s.channelFormat), string_XGL_NUM_FORMAT(%s.numericFormat)" % (name, name))
Tobin Ehlise7271572014-11-19 15:52:46 -0700125 if output_param:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600126 return ("%p", "(void*)*%s" % name)
Jon Ashburn52f79b52014-12-12 16:10:45 -0700127 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600128
Tobin Ehlise8185062014-12-17 08:01:59 -0700129 def _gen_layer_dbg_callback_header(self):
130 cbh_body = []
131 cbh_body.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
132 cbh_body.append('// Utility function to handle reporting')
133 cbh_body.append('// If callbacks are enabled, use them, otherwise use printf')
134 cbh_body.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
135 cbh_body.append(' XGL_VALIDATION_LEVEL validationLevel,')
136 cbh_body.append(' XGL_BASE_OBJECT srcObject,')
137 cbh_body.append(' XGL_SIZE location,')
138 cbh_body.append(' XGL_INT msgCode,')
139 cbh_body.append(' const XGL_CHAR* pLayerPrefix,')
140 cbh_body.append(' const XGL_CHAR* pMsg)')
141 cbh_body.append('{')
142 cbh_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
143 cbh_body.append(' if (pTrav) {')
144 cbh_body.append(' while (pTrav) {')
145 cbh_body.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
146 cbh_body.append(' pTrav = pTrav->pNext;')
147 cbh_body.append(' }')
148 cbh_body.append(' }')
149 cbh_body.append(' else {')
150 cbh_body.append(' switch (msgType) {')
151 cbh_body.append(' case XGL_DBG_MSG_ERROR:')
152 cbh_body.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
153 cbh_body.append(' break;')
154 cbh_body.append(' case XGL_DBG_MSG_WARNING:')
155 cbh_body.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
156 cbh_body.append(' break;')
157 cbh_body.append(' case XGL_DBG_MSG_PERF_WARNING:')
158 cbh_body.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
159 cbh_body.append(' break;')
160 cbh_body.append(' default:')
161 cbh_body.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
162 cbh_body.append(' break;')
163 cbh_body.append(' }')
164 cbh_body.append(' }')
165 cbh_body.append('}')
166 return "\n".join(cbh_body)
167
168 def _gen_layer_dbg_callback_register(self):
169 r_body = []
170 r_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, XGL_VOID* pUserData)')
171 r_body.append('{')
172 r_body.append(' // This layer intercepts callbacks')
173 r_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));')
174 r_body.append(' if (!pNewDbgFuncNode)')
175 r_body.append(' return XGL_ERROR_OUT_OF_MEMORY;')
176 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
177 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
178 r_body.append(' pNewDbgFuncNode->pNext = pDbgFunctionHead;')
179 r_body.append(' pDbgFunctionHead = pNewDbgFuncNode;')
180 r_body.append(' XGL_RESULT result = nextTable.DbgRegisterMsgCallback(pfnMsgCallback, pUserData);')
181 r_body.append(' return result;')
182 r_body.append('}')
183 return "\n".join(r_body)
184
185 def _gen_layer_dbg_callback_unregister(self):
186 ur_body = []
187 ur_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
188 ur_body.append('{')
189 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
190 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
191 ur_body.append(' while (pTrav) {')
192 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
193 ur_body.append(' pPrev->pNext = pTrav->pNext;')
194 ur_body.append(' if (pDbgFunctionHead == pTrav)')
195 ur_body.append(' pDbgFunctionHead = pTrav->pNext;')
196 ur_body.append(' free(pTrav);')
197 ur_body.append(' break;')
198 ur_body.append(' }')
199 ur_body.append(' pPrev = pTrav;')
200 ur_body.append(' pTrav = pTrav->pNext;')
201 ur_body.append(' }')
202 ur_body.append(' XGL_RESULT result = nextTable.DbgUnregisterMsgCallback(pfnMsgCallback);')
203 ur_body.append(' return result;')
204 ur_body.append('}')
205 return "\n".join(ur_body)
206
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700207 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600208 if qual:
209 qual += " "
210
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700211 layer_name = layer
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700212 if no_addr:
213 layer_name = "%sNoAddr" % layer
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600214 funcs = []
215 for proto in self.protos:
216 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700217 if "Generic" == layer:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600218 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
219 param0_name = proto.params[0].name
220 ret_val = ''
221 stmt = ''
222 if proto.ret != "XGL_VOID":
223 ret_val = "XGL_RESULT result = "
224 stmt = " return result;\n"
Jon Ashburnf7a08742014-11-25 11:08:42 -0700225 if proto.name == "EnumerateLayers":
226 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
227 funcs.append('%s%s\n'
228 '{\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700229 ' char str[1024];\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700230 ' if (gpu != NULL) {\n'
231 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700232 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700233 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700234 ' pCurObj = gpuw;\n'
235 ' pthread_once(&tabOnce, initLayerTable);\n'
236 ' %snextTable.%s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700237 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700238 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700239 ' fflush(stdout);\n'
240 ' %s'
241 ' } else {\n'
242 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
243 ' return XGL_ERROR_INVALID_POINTER;\n'
244 ' // This layer compatible with all GPUs\n'
245 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800246 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700247 ' return XGL_SUCCESS;\n'
248 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700249 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlise8185062014-12-17 08:01:59 -0700250 elif 'DbgRegisterMsgCallback' == proto.name:
251 funcs.append(self._gen_layer_dbg_callback_register())
252 elif 'DbgUnregisterMsgCallback' == proto.name:
253 funcs.append(self._gen_layer_dbg_callback_unregister())
Jon Ashburnf7a08742014-11-25 11:08:42 -0700254 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600255 funcs.append('%s%s\n'
256 '{\n'
257 ' %snextTable.%s;\n'
258 '%s'
259 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
260 else:
261 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
262 funcs.append('%s%s\n'
263 '{\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700264 ' char str[1024];'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600265 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700266 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700267 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600268 ' pCurObj = gpuw;\n'
269 ' pthread_once(&tabOnce, initLayerTable);\n'
270 ' %snextTable.%s;\n'
Tobin Ehlise8185062014-12-17 08:01:59 -0700271 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburna5cbf0c2014-12-17 12:08:37 -0700272 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (XGL_CHAR *) "GENERIC", (XGL_CHAR *) str);\n'
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -0700273 ' fflush(stdout);\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600274 '%s'
275 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700276 elif "APIDump" in layer:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600277 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
278 param0_name = proto.params[0].name
279 ret_val = ''
280 stmt = ''
Tobin Ehlis083e9062014-10-23 08:19:47 -0600281 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlise7271572014-11-19 15:52:46 -0700282 create_params = 0 # Num of params at end of function that are created and returned as output values
283 if 'WsiX11CreatePresentableImage' in proto.name:
284 create_params = -2
285 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
286 create_params = -1
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600287 if proto.ret != "XGL_VOID":
288 ret_val = "XGL_RESULT result = "
289 stmt = " return result;\n"
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700290 f_open = ''
291 f_close = ''
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700292 if "File" in layer:
Tobin Ehlisdbcd2572014-11-21 09:35:53 -0700293 file_mode = "a"
294 if 'CreateDevice' in proto.name:
295 file_mode = "w"
Chia-I Wu3bf80a62014-12-16 00:36:58 +0800296 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700297 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis2b9313e2014-11-20 12:18:45 -0700298 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700299 else:
Chia-I Wu3bf80a62014-12-16 00:36:58 +0800300 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700301 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis2b9313e2014-11-20 12:18:45 -0700302 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700303 print_vals = ', getTIDIndex()'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600304 pindex = 0
305 for p in proto.params:
Tobin Ehlise7271572014-11-19 15:52:46 -0700306 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
307 cp = False
308 if 0 != create_params:
309 # If this is any of the N last params of the func, treat as output
310 for y in range(-1, create_params-1, -1):
311 if p.name == proto.params[y].name:
312 cp = True
313 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700314 if no_addr and "%p" == pft:
315 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600316 log_func += '%s = %s, ' % (p.name, pft)
317 print_vals += ', %s' % (pfi)
Tobin Ehlis083e9062014-10-23 08:19:47 -0600318 # TODO : Just want this to be simple check for params of STRUCT type
319 if "pCreateInfo" in p.name or ('const' in p.ty and '*' in p.ty and False not in [tmp_ty not in p.ty for tmp_ty in ['XGL_CHAR', 'XGL_VOID', 'XGL_CMD_BUFFER', 'XGL_QUEUE_SEMAPHORE', 'XGL_FENCE', 'XGL_SAMPLER', 'XGL_UINT32']]):
Tobin Ehlis3a1cc8d2014-11-11 17:28:22 -0700320 if 'Wsi' not in proto.name:
321 cis_param_index.append(pindex)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600322 pindex += 1
323 log_func = log_func.strip(', ')
324 if proto.ret != "XGL_VOID":
325 log_func += ') = %s\\n"'
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -0700326 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600327 else:
328 log_func += ')\\n"'
329 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlis083e9062014-10-23 08:19:47 -0600330 if len(cis_param_index) > 0:
331 log_func += '\n char *pTmpStr;'
332 for sp_index in cis_param_index:
333 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
334 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
335 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700336 if "File" in layer:
337 if no_addr:
338 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
339 else:
340 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700341 else:
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700342 if no_addr:
343 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
344 else:
345 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700346 log_func += '\n fflush(stdout);'
Tobin Ehlis083e9062014-10-23 08:19:47 -0600347 log_func += '\n free(pTmpStr);\n }'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700348 if proto.name == "EnumerateLayers":
349 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
350 funcs.append('%s%s\n'
351 '{\n'
352 ' if (gpu != NULL) {\n'
353 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
354 ' pCurObj = gpuw;\n'
355 ' pthread_once(&tabOnce, initLayerTable);\n'
356 ' %snextTable.%s;\n'
357 ' %s %s %s\n'
358 ' %s'
359 ' } else {\n'
360 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
361 ' return XGL_ERROR_INVALID_POINTER;\n'
362 ' // This layer compatible with all GPUs\n'
363 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800364 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700365 ' return XGL_SUCCESS;\n'
366 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700367 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
Jon Ashburnf7a08742014-11-25 11:08:42 -0700368 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600369 funcs.append('%s%s\n'
370 '{\n'
371 ' %snextTable.%s;\n'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700372 ' %s%s%s\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600373 '%s'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700374 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600375 else:
376 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
377 funcs.append('%s%s\n'
378 '{\n'
379 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
380 ' pCurObj = gpuw;\n'
381 ' pthread_once(&tabOnce, initLayerTable);\n'
382 ' %snextTable.%s;\n'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700383 ' %s%s%s\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600384 '%s'
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700385 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700386 elif "ObjectTracker" == layer:
Tobin Ehlisca915872014-11-18 11:28:33 -0700387 obj_type_mapping = {"XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU", "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
388 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE", "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
389 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY", "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
390 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL", "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
391 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE", "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
392 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER", "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
393 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE", "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
394 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER", "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
395 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW", "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
396 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE", "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
397 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE", "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
398 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE", "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
399 "XGL_OBJECT" : "ll_get_obj_type(object)"}
400
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600401 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
402 param0_name = proto.params[0].name
Tobin Ehlisca915872014-11-18 11:28:33 -0700403 p0_type = proto.params[0].ty
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600404 create_line = ''
405 destroy_line = ''
Tobin Ehlisca915872014-11-18 11:28:33 -0700406 if 'DbgRegisterMsgCallback' in proto.name:
407 using_line = ' // This layer intercepts callbacks\n'
408 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
409 using_line += ' if (!pNewDbgFuncNode)\n'
410 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
411 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
412 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
413 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
414 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
415 elif 'DbgUnregisterMsgCallback' in proto.name:
416 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
417 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
418 using_line += ' while (pTrav) {\n'
419 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
420 using_line += ' pPrev->pNext = pTrav->pNext;\n'
421 using_line += ' if (pDbgFunctionHead == pTrav)\n'
422 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
423 using_line += ' free(pTrav);\n'
424 using_line += ' break;\n'
425 using_line += ' }\n'
426 using_line += ' pPrev = pTrav;\n'
427 using_line += ' pTrav = pTrav->pNext;\n'
428 using_line += ' }\n'
429 elif 'GlobalOption' in proto.name:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600430 using_line = ''
Tobin Ehlisca915872014-11-18 11:28:33 -0700431 else:
432 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
433 if 'Create' in proto.name or 'Alloc' in proto.name:
434 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
435 if 'DestroyObject' in proto.name:
436 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
437 using_line = ''
438 else:
439 if 'Destroy' in proto.name or 'Free' in proto.name:
440 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
441 using_line = ''
442 if 'DestroyDevice' in proto.name:
443 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
444 destroy_line += ' char str[1024];\n'
445 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'
446 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
447 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600448 ret_val = ''
449 stmt = ''
450 if proto.ret != "XGL_VOID":
451 ret_val = "XGL_RESULT result = "
452 stmt = " return result;\n"
Jon Ashburnf7a08742014-11-25 11:08:42 -0700453 if proto.name == "EnumerateLayers":
454 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
455 funcs.append('%s%s\n'
456 '{\n'
457 ' if (gpu != NULL) {\n'
458 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
459 ' %s'
460 ' pCurObj = gpuw;\n'
461 ' pthread_once(&tabOnce, initLayerTable);\n'
462 ' %snextTable.%s;\n'
463 ' %s%s'
464 ' %s'
465 ' } else {\n'
466 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
467 ' return XGL_ERROR_INVALID_POINTER;\n'
468 ' // This layer compatible with all GPUs\n'
469 ' *pOutLayerCount = 1;\n'
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800470 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburnf7a08742014-11-25 11:08:42 -0700471 ' return XGL_SUCCESS;\n'
472 ' }\n'
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700473 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt, layer_name))
Jon Ashburnf7a08742014-11-25 11:08:42 -0700474 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600475 funcs.append('%s%s\n'
476 '{\n'
477 '%s'
478 ' %snextTable.%s;\n'
479 '%s%s'
480 '%s'
481 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
482 else:
483 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
484 funcs.append('%s%s\n'
485 '{\n'
486 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
487 '%s'
488 ' pCurObj = gpuw;\n'
489 ' pthread_once(&tabOnce, initLayerTable);\n'
490 ' %snextTable.%s;\n'
491 '%s%s'
492 '%s'
493 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
494
495 # TODO : Put this code somewhere so it gets called at the end if objects not deleted :
496 # // Report any remaining objects in LL
497 # objNode *pTrav = pObjLLHead;
498 # while (pTrav) {
499 # printf("WARN : %s object %p has not been destroyed.\n", pTrav->objType, pTrav->pObj);
500 # }
501
502 return "\n\n".join(funcs)
503
Tobin Ehlisca915872014-11-18 11:28:33 -0700504 def _generate_extensions(self):
505 exts = []
506 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
507 exts.append('{')
508 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
509 exts.append('}')
510 exts.append('')
511 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
512 exts.append('{')
513 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
514 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
515 exts.append(' // Check the count first thing')
516 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
517 exts.append(' if (objCount > maxObjCount) {')
518 exts.append(' char str[1024];')
519 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));')
520 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
521 exts.append(' return XGL_ERROR_INVALID_VALUE;')
522 exts.append(' }')
523 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
524 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
525 exts.append(' if (!pTrav) {')
526 exts.append(' char str[1024];')
527 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);')
528 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
529 exts.append(' return XGL_ERROR_UNKNOWN;')
530 exts.append(' }')
531 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
532 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
533 exts.append(' }')
534 exts.append(' return XGL_SUCCESS;')
535 exts.append('}')
536
537 return "\n".join(exts)
538
539 def _generate_layer_gpa_function(self, prefix="xgl", extensions=[]):
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600540 func_body = []
541 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
542 "{\n"
543 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
544 " if (gpu == NULL)\n"
545 " return NULL;\n"
546 " pCurObj = gpuw;\n"
547 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800548 ' if (!strncmp("xglGetProcAddr", funcName, sizeof("xglGetProcAddr")))\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600549 ' return xglGetProcAddr;')
Tobin Ehlisca915872014-11-18 11:28:33 -0700550 if 0 != len(extensions):
551 for ext_name in extensions:
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800552 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlisca915872014-11-18 11:28:33 -0700553 ' return %s;' % (ext_name, ext_name, ext_name))
Chia-I Wu9a4ceb12015-01-01 14:45:58 +0800554 for name in xgl.proto_names:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600555 if name == "GetProcAddr":
556 continue
557 if name == "InitAndEnumerateGpus":
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800558 func_body.append(' else if (!strncmp("%s%s", funcName, sizeof("%s%s")))\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600559 ' return nextTable.%s;' % (prefix, name, prefix, name, name))
560 else:
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800561 func_body.append(' else if (!strncmp("%s%s", funcName, sizeof("%s%s")))\n'
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600562 ' return %s%s;' % (prefix, name, prefix, name, prefix, name))
563
564 func_body.append(" else {\n"
565 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
566 " if (gpuw->pGPA == NULL)\n"
567 " return NULL;\n"
568 " return gpuw->pGPA(gpuw->nextObject, funcName);\n"
569 " }\n"
570 "}\n")
571 return "\n".join(func_body)
572
573 def _generate_layer_dispatch_table(self, prefix='xgl'):
574 func_body = []
575 func_body.append('static void initLayerTable()\n'
576 '{\n'
577 ' GetProcAddrType fpNextGPA;\n'
578 ' fpNextGPA = pCurObj->pGPA;\n'
579 ' assert(fpNextGPA);\n');
580
Chia-I Wu9a4ceb12015-01-01 14:45:58 +0800581 for name in xgl.proto_names:
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600582 func_body.append(' %sType fp%s = fpNextGPA((XGL_PHYSICAL_GPU) pCurObj->nextObject, (XGL_CHAR *) "%s%s");\n'
583 ' nextTable.%s = fp%s;' % (name, name, prefix, name, name, name))
584
585 func_body.append("}\n")
586 return "\n".join(func_body)
587
588class LayerFuncsSubcommand(Subcommand):
589 def generate_header(self):
590 return '#include <xglLayer.h>\n#include "loader.h"'
591
592 def generate_body(self):
593 return self._generate_dispatch_entrypoints("static", True)
594
595class LayerDispatchSubcommand(Subcommand):
596 def generate_header(self):
597 return '#include "layer_wrappers.h"'
598
599 def generate_body(self):
600 return self._generate_layer_dispatch_table()
601
602class GenericLayerSubcommand(Subcommand):
603 def generate_header(self):
604 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'
605
606 def generate_body(self):
Tobin Ehlise8185062014-12-17 08:01:59 -0700607 body = [self._gen_layer_dbg_callback_header(),
608 self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700609 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600610 self._generate_layer_gpa_function()]
611
612 return "\n\n".join(body)
613
614class ApiDumpSubcommand(Subcommand):
615 def generate_header(self):
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700616 header_txt = []
617 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')
618 header_txt.append('#define MAX_TID 513')
619 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
620 header_txt.append('static uint32_t maxTID = 0;')
621 header_txt.append('// Map actual TID to an index value and return that index')
622 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
623 header_txt.append('static uint32_t getTIDIndex() {')
624 header_txt.append(' pthread_t tid = pthread_self();')
625 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
626 header_txt.append(' if (tid == tidMapping[i])')
627 header_txt.append(' return i;')
628 header_txt.append(' }')
629 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
630 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
631 header_txt.append(' tidMapping[maxTID++] = tid;')
632 header_txt.append(' assert(maxTID < MAX_TID);')
633 header_txt.append(' return retVal;')
634 header_txt.append('}')
635 return "\n".join(header_txt)
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600636
637 def generate_body(self):
638 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700639 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600640 self._generate_layer_gpa_function()]
641
642 return "\n\n".join(body)
643
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700644class ApiDumpFileSubcommand(Subcommand):
645 def generate_header(self):
Tobin Ehlisd009bae2014-11-24 15:46:55 -0700646 header_txt = []
647 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')
648 header_txt.append('#define MAX_TID 513')
649 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
650 header_txt.append('static uint32_t maxTID = 0;')
651 header_txt.append('// Map actual TID to an index value and return that index')
652 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
653 header_txt.append('static uint32_t getTIDIndex() {')
654 header_txt.append(' pthread_t tid = pthread_self();')
655 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
656 header_txt.append(' if (tid == tidMapping[i])')
657 header_txt.append(' return i;')
658 header_txt.append(' }')
659 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
660 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
661 header_txt.append(' tidMapping[maxTID++] = tid;')
662 header_txt.append(' assert(maxTID < MAX_TID);')
663 header_txt.append(' return retVal;')
664 header_txt.append('}')
665 return "\n".join(header_txt)
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700666
667 def generate_body(self):
668 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700669 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlisea3d21b2014-11-12 13:11:15 -0700670 self._generate_layer_gpa_function()]
671
672 return "\n\n".join(body)
673
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700674class ApiDumpNoAddrSubcommand(Subcommand):
675 def generate_header(self):
676 header_txt = []
677 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')
678 header_txt.append('#define MAX_TID 513')
679 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
680 header_txt.append('static uint32_t maxTID = 0;')
681 header_txt.append('// Map actual TID to an index value and return that index')
682 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
683 header_txt.append('static uint32_t getTIDIndex() {')
684 header_txt.append(' pthread_t tid = pthread_self();')
685 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
686 header_txt.append(' if (tid == tidMapping[i])')
687 header_txt.append(' return i;')
688 header_txt.append(' }')
689 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
690 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
691 header_txt.append(' tidMapping[maxTID++] = tid;')
692 header_txt.append(' assert(maxTID < MAX_TID);')
693 header_txt.append(' return retVal;')
694 header_txt.append('}')
695 return "\n".join(header_txt)
696
697 def generate_body(self):
698 body = [self._generate_layer_dispatch_table(),
699 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
700 self._generate_layer_gpa_function()]
701
702 return "\n\n".join(body)
703
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600704class ObjectTrackerSubcommand(Subcommand):
705 def generate_header(self):
706 header_txt = []
707 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>')
Tobin Ehlisca915872014-11-18 11:28:33 -0700708 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
709 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
710 header_txt.append('// Ptr to LL of dbg functions')
711 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
712 header_txt.append('// Utility function to handle reporting')
713 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
714 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
715 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
716 header_txt.append(' XGL_BASE_OBJECT srcObject,')
717 header_txt.append(' XGL_SIZE location,')
718 header_txt.append(' XGL_INT msgCode,')
Chia-I Wu1da4b9f2014-12-16 10:47:33 +0800719 header_txt.append(' const char* pLayerPrefix,')
720 header_txt.append(' const char* pMsg)')
Tobin Ehlisca915872014-11-18 11:28:33 -0700721 header_txt.append('{')
722 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
723 header_txt.append(' if (pTrav) {')
724 header_txt.append(' while (pTrav) {')
Chia-I Wuf1a5a742014-12-27 15:16:07 +0800725 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlisca915872014-11-18 11:28:33 -0700726 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600727 header_txt.append(' }')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600728 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -0700729 header_txt.append(' else {')
730 header_txt.append(' switch (msgType) {')
731 header_txt.append(' case XGL_DBG_MSG_ERROR:')
732 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
733 header_txt.append(' break;')
734 header_txt.append(' case XGL_DBG_MSG_WARNING:')
735 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
736 header_txt.append(' break;')
737 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
738 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
739 header_txt.append(' break;')
740 header_txt.append(' default:')
741 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
742 header_txt.append(' break;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600743 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -0700744 header_txt.append(' }')
745 header_txt.append('}')
746 header_txt.append('// We maintain a "Global" list which links every object and a')
747 header_txt.append('// per-Object list which just links objects of a given type')
748 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
749 header_txt.append('typedef struct _objNode {')
750 header_txt.append(' OBJTRACK_NODE obj;')
751 header_txt.append(' struct _objNode *pNextObj;')
752 header_txt.append(' struct _objNode *pNextGlobal;')
753 header_txt.append('} objNode;')
754 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
755 header_txt.append('static objNode *pGlobalHead = NULL;')
756 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
757 header_txt.append('static uint64_t numTotalObjs = 0;')
758 header_txt.append('// Debug function to print global list and each individual object list')
759 header_txt.append('static void ll_print_lists()')
760 header_txt.append('{')
761 header_txt.append(' objNode* pTrav = pGlobalHead;')
762 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
763 header_txt.append(' while (pTrav) {')
764 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);')
765 header_txt.append(' pTrav = pTrav->pNextGlobal;')
766 header_txt.append(' }')
767 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
768 header_txt.append(' pTrav = pObjectHead[i];')
769 header_txt.append(' if (pTrav) {')
770 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
771 header_txt.append(' while (pTrav) {')
772 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);')
773 header_txt.append(' pTrav = pTrav->pNextObj;')
774 header_txt.append(' }')
775 header_txt.append(' }')
776 header_txt.append(' }')
777 header_txt.append('}')
778 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
779 header_txt.append(' char str[1024];')
780 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
781 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
782 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
783 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
784 header_txt.append(' pNewObjNode->obj.objType = objType;')
785 header_txt.append(' pNewObjNode->obj.numUses = 0;')
786 header_txt.append(' // insert at front of global list')
787 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
788 header_txt.append(' pGlobalHead = pNewObjNode;')
789 header_txt.append(' // insert at front of object list')
790 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
791 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
792 header_txt.append(' // increment obj counts')
793 header_txt.append(' numObjs[objType]++;')
794 header_txt.append(' numTotalObjs++;')
795 header_txt.append(' //sprintf(str, "OBJ_STAT : %lu total objs & %lu %s objs.", numTotalObjs, numObjs[objType], string_XGL_OBJECT_TYPE(objType));')
Chia-I Wu85763e52014-12-16 11:02:06 +0800796 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlisca915872014-11-18 11:28:33 -0700797 header_txt.append('}')
798 header_txt.append('// Traverse global list and return type for given object')
799 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
800 header_txt.append(' objNode *pTrav = pGlobalHead;')
801 header_txt.append(' while (pTrav) {')
802 header_txt.append(' if (pTrav->obj.pObj == object)')
803 header_txt.append(' return pTrav->obj.objType;')
804 header_txt.append(' pTrav = pTrav->pNextGlobal;')
805 header_txt.append(' }')
806 header_txt.append(' char str[1024];')
807 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
808 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
809 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
810 header_txt.append('}')
Chia-I Wu85763e52014-12-16 11:02:06 +0800811 header_txt.append('#if 0')
Tobin Ehlisca915872014-11-18 11:28:33 -0700812 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
813 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
814 header_txt.append(' while (pTrav) {')
815 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
816 header_txt.append(' return pTrav->obj.numUses;')
817 header_txt.append(' }')
818 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600819 header_txt.append(' }')
820 header_txt.append(' return 0;')
821 header_txt.append('}')
Chia-I Wu85763e52014-12-16 11:02:06 +0800822 header_txt.append('#endif')
Tobin Ehlisca915872014-11-18 11:28:33 -0700823 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
824 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600825 header_txt.append(' while (pTrav) {')
Tobin Ehlisca915872014-11-18 11:28:33 -0700826 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
827 header_txt.append(' pTrav->obj.numUses++;')
828 header_txt.append(' char str[1024];')
829 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);')
830 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
831 header_txt.append(' return;')
832 header_txt.append(' }')
833 header_txt.append(' pTrav = pTrav->pNextObj;')
834 header_txt.append(' }')
835 header_txt.append(' // If we do not find obj, insert it and then increment count')
836 header_txt.append(' char str[1024];')
837 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));')
838 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
839 header_txt.append('')
840 header_txt.append(' ll_insert_obj(pObj, objType);')
841 header_txt.append(' ll_increment_use_count(pObj, objType);')
842 header_txt.append('}')
843 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
844 header_txt.append('// Type from global list w/ ll_destroy_obj()')
845 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
846 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
847 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
848 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
849 header_txt.append(' while (pTrav) {')
850 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
851 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
852 header_txt.append(' // update HEAD of Obj list as needed')
853 header_txt.append(' if (pObjectHead[objType] == pTrav)')
854 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
855 header_txt.append(' assert(numObjs[objType] > 0);')
856 header_txt.append(' numObjs[objType]--;')
857 header_txt.append(' char str[1024];')
858 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
859 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600860 header_txt.append(' return;')
861 header_txt.append(' }')
862 header_txt.append(' pPrev = pTrav;')
Tobin Ehlisca915872014-11-18 11:28:33 -0700863 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600864 header_txt.append(' }')
Tobin Ehlisca915872014-11-18 11:28:33 -0700865 header_txt.append(' char str[1024];')
866 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));')
867 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
868 header_txt.append('}')
869 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
870 header_txt.append('// remove obj from global list')
871 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
872 header_txt.append(' objNode *pTrav = pGlobalHead;')
873 header_txt.append(' objNode *pPrev = pGlobalHead;')
874 header_txt.append(' while (pTrav) {')
875 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
876 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
877 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
878 header_txt.append(' // update HEAD of global list if needed')
879 header_txt.append(' if (pGlobalHead == pTrav)')
880 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
881 header_txt.append(' free(pTrav);')
882 header_txt.append(' assert(numTotalObjs > 0);')
883 header_txt.append(' numTotalObjs--;')
884 header_txt.append(' char str[1024];')
885 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));')
886 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
887 header_txt.append(' return;')
888 header_txt.append(' }')
889 header_txt.append(' pPrev = pTrav;')
890 header_txt.append(' pTrav = pTrav->pNextGlobal;')
891 header_txt.append(' }')
892 header_txt.append(' char str[1024];')
893 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
894 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_DESTROY_OBJECT_FAILED, "OBJTRACK", str);')
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600895 header_txt.append('}')
896
897 return "\n".join(header_txt)
898
899 def generate_body(self):
900 body = [self._generate_layer_dispatch_table(),
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700901 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlisca915872014-11-18 11:28:33 -0700902 self._generate_extensions(),
903 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600904
905 return "\n\n".join(body)
Courtney Goeltzenleuchterb412d212014-11-18 10:40:29 -0700906
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600907def main():
908 subcommands = {
909 "layer-funcs" : LayerFuncsSubcommand,
910 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700911 "Generic" : GenericLayerSubcommand,
912 "ApiDump" : ApiDumpSubcommand,
913 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700914 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlisa363cfa2014-11-25 16:59:27 -0700915 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis12076fc2014-10-22 09:06:33 -0600916 }
917
918 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
919 print("Usage: %s <subcommand> [options]" % sys.argv[0])
920 print
921 print("Available sucommands are: %s" % " ".join(subcommands))
922 exit(1)
923
924 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
925 subcmd.run()
926
927if __name__ == "__main__":
928 main()