blob: 928dc687ff97ea984c528de1170eaee7473e7415 [file] [log] [blame]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001#!/usr/bin/env python3
2#
3# XGL
4#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
27
28import sys
29
30import xgl
31
32class Subcommand(object):
33 def __init__(self, argv):
34 self.argv = argv
Chia-I Wuec30dcb2015-01-01 08:46:31 +080035 self.headers = xgl.headers
36 self.protos = xgl.protos
Tobin Ehlis92dbf802014-10-22 09:06:33 -060037
38 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060039 print(self.generate())
40
41 def generate(self):
42 copyright = self.generate_copyright()
43 header = self.generate_header()
44 body = self.generate_body()
45 footer = self.generate_footer()
46
47 contents = []
48 if copyright:
49 contents.append(copyright)
50 if header:
51 contents.append(header)
52 if body:
53 contents.append(body)
54 if footer:
55 contents.append(footer)
56
57 return "\n\n".join(contents)
58
59 def generate_copyright(self):
60 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
61
62/*
63 * XGL
64 *
65 * Copyright (C) 2014 LunarG, Inc.
66 *
67 * Permission is hereby granted, free of charge, to any person obtaining a
68 * copy of this software and associated documentation files (the "Software"),
69 * to deal in the Software without restriction, including without limitation
70 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
71 * and/or sell copies of the Software, and to permit persons to whom the
72 * Software is furnished to do so, subject to the following conditions:
73 *
74 * The above copyright notice and this permission notice shall be included
75 * in all copies or substantial portions of the Software.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
82 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
83 * DEALINGS IN THE SOFTWARE.
84 */"""
85
86 def generate_header(self):
87 return "\n".join(["#include <" + h + ">" for h in self.headers])
88
89 def generate_body(self):
90 pass
91
92 def generate_footer(self):
93 pass
94
95 # Return set of printf '%' qualifier and input to that qualifier
Tobin Ehlisa554dc32014-11-19 15:52:46 -070096 def _get_printf_params(self, xgl_type, name, output_param):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060097 # TODO : Need ENUM and STRUCT checks here
98 if "_TYPE" in xgl_type: # TODO : This should be generic ENUM check
99 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
100 if "XGL_CHAR*" == xgl_type:
101 return ("%s", name)
102 if "UINT64" in xgl_type:
103 if '*' in xgl_type:
104 return ("%lu", "*%s" % name)
105 return ("%lu", name)
Chia-I Wu54ed0792014-12-27 14:14:50 +0800106 if "SIZE" in xgl_type:
107 if '*' in xgl_type:
108 return ("%zu", "*%s" % name)
109 return ("%zu", name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600110 if "FLOAT" in xgl_type:
111 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
112 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
113 return ("%f", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700114 if "BOOL" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600115 return ("%u", name)
Chia-I Wu54ed0792014-12-27 14:14:50 +0800116 if True in [t in xgl_type for t in ["INT", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-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 Ashburn1f7e2d72014-12-12 16:10:45 -0700120 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600121 return ("%i", name)
Tobin Ehlis0a1e06d2014-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 Ashburn1f7e2d72014-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 Ehlisa554dc32014-11-19 15:52:46 -0700125 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600126 return ("%p", "(void*)*%s" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700127 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600128
Tobin Ehlisc54139f2014-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 Ehlis07fe9ab2014-11-25 17:43:26 -0700207 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600208 if qual:
209 qual += " "
210
Tobin Ehlis907a0522014-11-25 16:59:27 -0700211 layer_name = layer
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700212 if no_addr:
213 layer_name = "%sNoAddr" % layer
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600214 funcs = []
215 for proto in self.protos:
216 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlis907a0522014-11-25 16:59:27 -0700217 if "Generic" == layer:
Tobin Ehlis92dbf802014-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 Ashburn451c16f2014-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 Ehlisc54139f2014-12-17 08:01:59 -0700229 ' char str[1024];\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700230 ' if (gpu != NULL) {\n'
231 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700232 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ashburn451c16f2014-11-25 11:08:42 -0700234 ' pCurObj = gpuw;\n'
235 ' pthread_once(&tabOnce, initLayerTable);\n'
236 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700237 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ashburn451c16f2014-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 Wua837c522014-12-16 10:47:33 +0800246 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700247 ' return XGL_SUCCESS;\n'
248 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700249 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlisc54139f2014-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 Ashburn451c16f2014-11-25 11:08:42 -0700254 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-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 Ehlisc54139f2014-12-17 08:01:59 -0700264 ' char str[1024];'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600265 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700266 ' sprintf(str, "At start of layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Ehlis92dbf802014-10-22 09:06:33 -0600268 ' pCurObj = gpuw;\n'
269 ' pthread_once(&tabOnce, initLayerTable);\n'
270 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700271 ' sprintf(str, "Completed layered %s\\n");\n'
Jon Ashburn8aa32902014-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 Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700273 ' fflush(stdout);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600274 '%s'
275 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700276 elif "APIDump" in layer:
Tobin Ehlis92dbf802014-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 Ehlis73ff5672014-10-23 08:19:47 -0600281 cis_param_index = [] # Store list of indices when func has struct params
Tobin Ehlisa554dc32014-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 Ehlis92dbf802014-10-22 09:06:33 -0600287 if proto.ret != "XGL_VOID":
288 ret_val = "XGL_RESULT result = "
289 stmt = " return result;\n"
Tobin Ehlis574b0142014-11-12 13:11:15 -0700290 f_open = ''
291 f_close = ''
Tobin Ehlis907a0522014-11-25 16:59:27 -0700292 if "File" in layer:
Tobin Ehlis1eba7792014-11-21 09:35:53 -0700293 file_mode = "a"
294 if 'CreateDevice' in proto.name:
295 file_mode = "w"
Chia-I Wu81f46672014-12-16 00:36:58 +0800296 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700297 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700298 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700299 else:
Chia-I Wu81f46672014-12-16 00:36:58 +0800300 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700301 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700302 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700303 print_vals = ', getTIDIndex()'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600304 pindex = 0
305 for p in proto.params:
Tobin Ehlisa554dc32014-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 Ehlis07fe9ab2014-11-25 17:43:26 -0700314 if no_addr and "%p" == pft:
315 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600316 log_func += '%s = %s, ' % (p.name, pft)
317 print_vals += ', %s' % (pfi)
Tobin Ehlis73ff5672014-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 Ehlis0a1e06d2014-11-11 17:28:22 -0700320 if 'Wsi' not in proto.name:
321 cis_param_index.append(pindex)
Tobin Ehlis92dbf802014-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 Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700326 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600327 else:
328 log_func += ')\\n"'
329 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlis73ff5672014-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 Ehlis07fe9ab2014-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 Ehlis574b0142014-11-12 13:11:15 -0700341 else:
Tobin Ehlis07fe9ab2014-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 Ehlis0c68c9c2014-11-24 15:46:55 -0700346 log_func += '\n fflush(stdout);'
Tobin Ehlis73ff5672014-10-23 08:19:47 -0600347 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-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 Wua837c522014-12-16 10:47:33 +0800364 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700365 ' return XGL_SUCCESS;\n'
366 ' }\n'
Tobin Ehlis07fe9ab2014-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 Ashburn451c16f2014-11-25 11:08:42 -0700368 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600369 funcs.append('%s%s\n'
370 '{\n'
371 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700372 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600373 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700374 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-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 Ehlis574b0142014-11-12 13:11:15 -0700383 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600384 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700385 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700386 elif "ObjectTracker" == layer:
Chia-I Wudf2efbd2015-01-05 14:37:39 +0800387 obj_type_mapping = {
388 "XGL_PHYSICAL_GPU" : "XGL_OBJECT_TYPE_PHYSICAL_GPU",
389 "XGL_DEVICE" : "XGL_OBJECT_TYPE_DEVICE",
390 "XGL_QUEUE" : "XGL_OBJECT_TYPE_QUEUE",
391 "XGL_QUEUE_SEMAPHORE" : "XGL_OBJECT_TYPE_QUEUE_SEMAPHORE",
392 "XGL_GPU_MEMORY" : "XGL_OBJECT_TYPE_GPU_MEMORY",
393 "XGL_FENCE" : "XGL_OBJECT_TYPE_FENCE",
394 "XGL_QUERY_POOL" : "XGL_OBJECT_TYPE_QUERY_POOL",
395 "XGL_EVENT" : "XGL_OBJECT_TYPE_EVENT",
396 "XGL_IMAGE" : "XGL_OBJECT_TYPE_IMAGE",
397 "XGL_DESCRIPTOR_SET" : "XGL_OBJECT_TYPE_DESCRIPTOR_SET",
398 "XGL_CMD_BUFFER" : "XGL_OBJECT_TYPE_CMD_BUFFER",
399 "XGL_SAMPLER" : "XGL_OBJECT_TYPE_SAMPLER",
400 "XGL_PIPELINE" : "XGL_OBJECT_TYPE_PIPELINE",
401 "XGL_PIPELINE_DELTA" : "XGL_OBJECT_TYPE_PIPELINE_DELTA",
402 "XGL_SHADER" : "XGL_OBJECT_TYPE_SHADER",
403 "XGL_IMAGE_VIEW" : "XGL_OBJECT_TYPE_IMAGE_VIEW",
404 "XGL_COLOR_ATTACHMENT_VIEW" : "XGL_OBJECT_TYPE_COLOR_ATTACHMENT_VIEW",
405 "XGL_DEPTH_STENCIL_VIEW" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_VIEW",
406 "XGL_VIEWPORT_STATE_OBJECT" : "XGL_OBJECT_TYPE_VIEWPORT_STATE",
407 "XGL_RASTER_STATE_OBJECT" : "XGL_OBJECT_TYPE_RASTER_STATE",
408 "XGL_MSAA_STATE_OBJECT" : "XGL_OBJECT_TYPE_MSAA_STATE",
409 "XGL_COLOR_BLEND_STATE_OBJECT" : "XGL_OBJECT_TYPE_COLOR_BLEND_STATE",
410 "XGL_DEPTH_STENCIL_STATE_OBJECT" : "XGL_OBJECT_TYPE_DEPTH_STENCIL_STATE",
411 "XGL_BASE_OBJECT" : "ll_get_obj_type(object)",
412 "XGL_OBJECT" : "ll_get_obj_type(object)"
413 }
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700414
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600415 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
416 param0_name = proto.params[0].name
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700417 p0_type = proto.params[0].ty
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600418 create_line = ''
419 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700420 if 'DbgRegisterMsgCallback' in proto.name:
421 using_line = ' // This layer intercepts callbacks\n'
422 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
423 using_line += ' if (!pNewDbgFuncNode)\n'
424 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
425 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
426 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
427 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
428 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
429 elif 'DbgUnregisterMsgCallback' in proto.name:
430 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
431 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
432 using_line += ' while (pTrav) {\n'
433 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
434 using_line += ' pPrev->pNext = pTrav->pNext;\n'
435 using_line += ' if (pDbgFunctionHead == pTrav)\n'
436 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
437 using_line += ' free(pTrav);\n'
438 using_line += ' break;\n'
439 using_line += ' }\n'
440 using_line += ' pPrev = pTrav;\n'
441 using_line += ' pTrav = pTrav->pNext;\n'
442 using_line += ' }\n'
443 elif 'GlobalOption' in proto.name:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600444 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700445 else:
446 using_line = ' ll_increment_use_count((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
447 if 'Create' in proto.name or 'Alloc' in proto.name:
448 create_line = ' ll_insert_obj((XGL_VOID*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*')])
449 if 'DestroyObject' in proto.name:
450 destroy_line = ' ll_destroy_obj((XGL_VOID*)%s);\n' % (param0_name)
451 using_line = ''
452 else:
453 if 'Destroy' in proto.name or 'Free' in proto.name:
454 destroy_line = ' ll_remove_obj_type((XGL_VOID*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
455 using_line = ''
456 if 'DestroyDevice' in proto.name:
457 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
458 destroy_line += ' char str[1024];\n'
459 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'
460 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
461 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600462 ret_val = ''
463 stmt = ''
464 if proto.ret != "XGL_VOID":
465 ret_val = "XGL_RESULT result = "
466 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700467 if proto.name == "EnumerateLayers":
468 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
469 funcs.append('%s%s\n'
470 '{\n'
471 ' if (gpu != NULL) {\n'
472 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
473 ' %s'
474 ' pCurObj = gpuw;\n'
475 ' pthread_once(&tabOnce, initLayerTable);\n'
476 ' %snextTable.%s;\n'
477 ' %s%s'
478 ' %s'
479 ' } else {\n'
480 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
481 ' return XGL_ERROR_INVALID_POINTER;\n'
482 ' // This layer compatible with all GPUs\n'
483 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800484 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700485 ' return XGL_SUCCESS;\n'
486 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700487 '}' % (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 -0700488 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600489 funcs.append('%s%s\n'
490 '{\n'
491 '%s'
492 ' %snextTable.%s;\n'
493 '%s%s'
494 '%s'
495 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
496 else:
497 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
498 funcs.append('%s%s\n'
499 '{\n'
500 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
501 '%s'
502 ' pCurObj = gpuw;\n'
503 ' pthread_once(&tabOnce, initLayerTable);\n'
504 ' %snextTable.%s;\n'
505 '%s%s'
506 '%s'
507 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
508
509 # TODO : Put this code somewhere so it gets called at the end if objects not deleted :
510 # // Report any remaining objects in LL
511 # objNode *pTrav = pObjLLHead;
512 # while (pTrav) {
513 # printf("WARN : %s object %p has not been destroyed.\n", pTrav->objType, pTrav->pObj);
514 # }
515
516 return "\n\n".join(funcs)
517
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700518 def _generate_extensions(self):
519 exts = []
520 exts.append('XGL_UINT64 objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
521 exts.append('{')
522 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
523 exts.append('}')
524 exts.append('')
525 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, XGL_UINT64 objCount, OBJTRACK_NODE* pObjNodeArray)')
526 exts.append('{')
527 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
528 exts.append(' XGL_BOOL bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
529 exts.append(' // Check the count first thing')
530 exts.append(' XGL_UINT64 maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
531 exts.append(' if (objCount > maxObjCount) {')
532 exts.append(' char str[1024];')
533 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));')
534 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
535 exts.append(' return XGL_ERROR_INVALID_VALUE;')
536 exts.append(' }')
537 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
538 exts.append(' for (XGL_UINT64 i = 0; i < objCount; i++) {')
539 exts.append(' if (!pTrav) {')
540 exts.append(' char str[1024];')
541 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);')
542 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
543 exts.append(' return XGL_ERROR_UNKNOWN;')
544 exts.append(' }')
545 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
546 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
547 exts.append(' }')
548 exts.append(' return XGL_SUCCESS;')
549 exts.append('}')
550
551 return "\n".join(exts)
552
553 def _generate_layer_gpa_function(self, prefix="xgl", extensions=[]):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600554 func_body = []
555 func_body.append("XGL_LAYER_EXPORT XGL_VOID* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const XGL_CHAR* funcName)\n"
556 "{\n"
557 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
558 " if (gpu == NULL)\n"
559 " return NULL;\n"
560 " pCurObj = gpuw;\n"
561 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800562 ' if (!strncmp("xglGetProcAddr", funcName, sizeof("xglGetProcAddr")))\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600563 ' return xglGetProcAddr;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700564 if 0 != len(extensions):
565 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800566 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700567 ' return %s;' % (ext_name, ext_name, ext_name))
Chia-I Wuf91902a2015-01-01 14:45:58 +0800568 for name in xgl.proto_names:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600569 if name == "GetProcAddr":
570 continue
571 if name == "InitAndEnumerateGpus":
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800572 func_body.append(' else if (!strncmp("%s%s", funcName, sizeof("%s%s")))\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600573 ' return nextTable.%s;' % (prefix, name, prefix, name, name))
574 else:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800575 func_body.append(' else if (!strncmp("%s%s", funcName, sizeof("%s%s")))\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600576 ' return %s%s;' % (prefix, name, prefix, name, prefix, name))
577
578 func_body.append(" else {\n"
579 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
580 " if (gpuw->pGPA == NULL)\n"
581 " return NULL;\n"
582 " return gpuw->pGPA(gpuw->nextObject, funcName);\n"
583 " }\n"
584 "}\n")
585 return "\n".join(func_body)
586
587 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800588 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600589 func_body.append('static void initLayerTable()\n'
590 '{\n'
591 ' GetProcAddrType fpNextGPA;\n'
592 ' fpNextGPA = pCurObj->pGPA;\n'
593 ' assert(fpNextGPA);\n');
594
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800595 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600596 func_body.append("}\n")
597 return "\n".join(func_body)
598
599class LayerFuncsSubcommand(Subcommand):
600 def generate_header(self):
601 return '#include <xglLayer.h>\n#include "loader.h"'
602
603 def generate_body(self):
604 return self._generate_dispatch_entrypoints("static", True)
605
606class LayerDispatchSubcommand(Subcommand):
607 def generate_header(self):
608 return '#include "layer_wrappers.h"'
609
610 def generate_body(self):
611 return self._generate_layer_dispatch_table()
612
613class GenericLayerSubcommand(Subcommand):
614 def generate_header(self):
615 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'
616
617 def generate_body(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700618 body = [self._gen_layer_dbg_callback_header(),
619 self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700620 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600621 self._generate_layer_gpa_function()]
622
623 return "\n\n".join(body)
624
625class ApiDumpSubcommand(Subcommand):
626 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700627 header_txt = []
628 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')
629 header_txt.append('#define MAX_TID 513')
630 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
631 header_txt.append('static uint32_t maxTID = 0;')
632 header_txt.append('// Map actual TID to an index value and return that index')
633 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
634 header_txt.append('static uint32_t getTIDIndex() {')
635 header_txt.append(' pthread_t tid = pthread_self();')
636 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
637 header_txt.append(' if (tid == tidMapping[i])')
638 header_txt.append(' return i;')
639 header_txt.append(' }')
640 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
641 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
642 header_txt.append(' tidMapping[maxTID++] = tid;')
643 header_txt.append(' assert(maxTID < MAX_TID);')
644 header_txt.append(' return retVal;')
645 header_txt.append('}')
646 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600647
648 def generate_body(self):
649 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700650 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600651 self._generate_layer_gpa_function()]
652
653 return "\n\n".join(body)
654
Tobin Ehlis574b0142014-11-12 13:11:15 -0700655class ApiDumpFileSubcommand(Subcommand):
656 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700657 header_txt = []
658 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')
659 header_txt.append('#define MAX_TID 513')
660 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
661 header_txt.append('static uint32_t maxTID = 0;')
662 header_txt.append('// Map actual TID to an index value and return that index')
663 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
664 header_txt.append('static uint32_t getTIDIndex() {')
665 header_txt.append(' pthread_t tid = pthread_self();')
666 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
667 header_txt.append(' if (tid == tidMapping[i])')
668 header_txt.append(' return i;')
669 header_txt.append(' }')
670 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
671 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
672 header_txt.append(' tidMapping[maxTID++] = tid;')
673 header_txt.append(' assert(maxTID < MAX_TID);')
674 header_txt.append(' return retVal;')
675 header_txt.append('}')
676 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700677
678 def generate_body(self):
679 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700680 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700681 self._generate_layer_gpa_function()]
682
683 return "\n\n".join(body)
684
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700685class ApiDumpNoAddrSubcommand(Subcommand):
686 def generate_header(self):
687 header_txt = []
688 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')
689 header_txt.append('#define MAX_TID 513')
690 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
691 header_txt.append('static uint32_t maxTID = 0;')
692 header_txt.append('// Map actual TID to an index value and return that index')
693 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
694 header_txt.append('static uint32_t getTIDIndex() {')
695 header_txt.append(' pthread_t tid = pthread_self();')
696 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
697 header_txt.append(' if (tid == tidMapping[i])')
698 header_txt.append(' return i;')
699 header_txt.append(' }')
700 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
701 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
702 header_txt.append(' tidMapping[maxTID++] = tid;')
703 header_txt.append(' assert(maxTID < MAX_TID);')
704 header_txt.append(' return retVal;')
705 header_txt.append('}')
706 return "\n".join(header_txt)
707
708 def generate_body(self):
709 body = [self._generate_layer_dispatch_table(),
710 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
711 self._generate_layer_gpa_function()]
712
713 return "\n\n".join(body)
714
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600715class ObjectTrackerSubcommand(Subcommand):
716 def generate_header(self):
717 header_txt = []
718 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 -0700719 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
720 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
721 header_txt.append('// Ptr to LL of dbg functions')
722 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
723 header_txt.append('// Utility function to handle reporting')
724 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
725 header_txt.append('static XGL_VOID layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
726 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
727 header_txt.append(' XGL_BASE_OBJECT srcObject,')
728 header_txt.append(' XGL_SIZE location,')
729 header_txt.append(' XGL_INT msgCode,')
Chia-I Wua837c522014-12-16 10:47:33 +0800730 header_txt.append(' const char* pLayerPrefix,')
731 header_txt.append(' const char* pMsg)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700732 header_txt.append('{')
733 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
734 header_txt.append(' if (pTrav) {')
735 header_txt.append(' while (pTrav) {')
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800736 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700737 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600738 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600739 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700740 header_txt.append(' else {')
741 header_txt.append(' switch (msgType) {')
742 header_txt.append(' case XGL_DBG_MSG_ERROR:')
743 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
744 header_txt.append(' break;')
745 header_txt.append(' case XGL_DBG_MSG_WARNING:')
746 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
747 header_txt.append(' break;')
748 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
749 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
750 header_txt.append(' break;')
751 header_txt.append(' default:')
752 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
753 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600754 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700755 header_txt.append(' }')
756 header_txt.append('}')
757 header_txt.append('// We maintain a "Global" list which links every object and a')
758 header_txt.append('// per-Object list which just links objects of a given type')
759 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
760 header_txt.append('typedef struct _objNode {')
761 header_txt.append(' OBJTRACK_NODE obj;')
762 header_txt.append(' struct _objNode *pNextObj;')
763 header_txt.append(' struct _objNode *pNextGlobal;')
764 header_txt.append('} objNode;')
765 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
766 header_txt.append('static objNode *pGlobalHead = NULL;')
767 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
768 header_txt.append('static uint64_t numTotalObjs = 0;')
769 header_txt.append('// Debug function to print global list and each individual object list')
770 header_txt.append('static void ll_print_lists()')
771 header_txt.append('{')
772 header_txt.append(' objNode* pTrav = pGlobalHead;')
773 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
774 header_txt.append(' while (pTrav) {')
775 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);')
776 header_txt.append(' pTrav = pTrav->pNextGlobal;')
777 header_txt.append(' }')
778 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
779 header_txt.append(' pTrav = pObjectHead[i];')
780 header_txt.append(' if (pTrav) {')
781 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
782 header_txt.append(' while (pTrav) {')
783 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);')
784 header_txt.append(' pTrav = pTrav->pNextObj;')
785 header_txt.append(' }')
786 header_txt.append(' }')
787 header_txt.append(' }')
788 header_txt.append('}')
789 header_txt.append('static void ll_insert_obj(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
790 header_txt.append(' char str[1024];')
791 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
792 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
793 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
794 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
795 header_txt.append(' pNewObjNode->obj.objType = objType;')
796 header_txt.append(' pNewObjNode->obj.numUses = 0;')
797 header_txt.append(' // insert at front of global list')
798 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
799 header_txt.append(' pGlobalHead = pNewObjNode;')
800 header_txt.append(' // insert at front of object list')
801 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
802 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
803 header_txt.append(' // increment obj counts')
804 header_txt.append(' numObjs[objType]++;')
805 header_txt.append(' numTotalObjs++;')
806 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 +0800807 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700808 header_txt.append('}')
809 header_txt.append('// Traverse global list and return type for given object')
810 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
811 header_txt.append(' objNode *pTrav = pGlobalHead;')
812 header_txt.append(' while (pTrav) {')
813 header_txt.append(' if (pTrav->obj.pObj == object)')
814 header_txt.append(' return pTrav->obj.objType;')
815 header_txt.append(' pTrav = pTrav->pNextGlobal;')
816 header_txt.append(' }')
817 header_txt.append(' char str[1024];')
818 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
819 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
820 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
821 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800822 header_txt.append('#if 0')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700823 header_txt.append('static uint64_t ll_get_obj_uses(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
824 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
825 header_txt.append(' while (pTrav) {')
826 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
827 header_txt.append(' return pTrav->obj.numUses;')
828 header_txt.append(' }')
829 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600830 header_txt.append(' }')
831 header_txt.append(' return 0;')
832 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +0800833 header_txt.append('#endif')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700834 header_txt.append('static void ll_increment_use_count(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
835 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600836 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700837 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
838 header_txt.append(' pTrav->obj.numUses++;')
839 header_txt.append(' char str[1024];')
840 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);')
841 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
842 header_txt.append(' return;')
843 header_txt.append(' }')
844 header_txt.append(' pTrav = pTrav->pNextObj;')
845 header_txt.append(' }')
846 header_txt.append(' // If we do not find obj, insert it and then increment count')
847 header_txt.append(' char str[1024];')
848 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));')
849 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
850 header_txt.append('')
851 header_txt.append(' ll_insert_obj(pObj, objType);')
852 header_txt.append(' ll_increment_use_count(pObj, objType);')
853 header_txt.append('}')
854 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
855 header_txt.append('// Type from global list w/ ll_destroy_obj()')
856 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
857 header_txt.append('static void ll_remove_obj_type(XGL_VOID* pObj, XGL_OBJECT_TYPE objType) {')
858 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
859 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
860 header_txt.append(' while (pTrav) {')
861 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
862 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
863 header_txt.append(' // update HEAD of Obj list as needed')
864 header_txt.append(' if (pObjectHead[objType] == pTrav)')
865 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
866 header_txt.append(' assert(numObjs[objType] > 0);')
867 header_txt.append(' numObjs[objType]--;')
868 header_txt.append(' char str[1024];')
869 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
870 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 -0600871 header_txt.append(' return;')
872 header_txt.append(' }')
873 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700874 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600875 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700876 header_txt.append(' char str[1024];')
877 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));')
878 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
879 header_txt.append('}')
880 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
881 header_txt.append('// remove obj from global list')
882 header_txt.append('static void ll_destroy_obj(XGL_VOID* pObj) {')
883 header_txt.append(' objNode *pTrav = pGlobalHead;')
884 header_txt.append(' objNode *pPrev = pGlobalHead;')
885 header_txt.append(' while (pTrav) {')
886 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
887 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
888 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
889 header_txt.append(' // update HEAD of global list if needed')
890 header_txt.append(' if (pGlobalHead == pTrav)')
891 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
892 header_txt.append(' free(pTrav);')
893 header_txt.append(' assert(numTotalObjs > 0);')
894 header_txt.append(' numTotalObjs--;')
895 header_txt.append(' char str[1024];')
896 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));')
897 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
898 header_txt.append(' return;')
899 header_txt.append(' }')
900 header_txt.append(' pPrev = pTrav;')
901 header_txt.append(' pTrav = pTrav->pNextGlobal;')
902 header_txt.append(' }')
903 header_txt.append(' char str[1024];')
904 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
905 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 -0600906 header_txt.append('}')
907
908 return "\n".join(header_txt)
909
910 def generate_body(self):
911 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700912 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700913 self._generate_extensions(),
914 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600915
916 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700917
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600918def main():
919 subcommands = {
920 "layer-funcs" : LayerFuncsSubcommand,
921 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -0700922 "Generic" : GenericLayerSubcommand,
923 "ApiDump" : ApiDumpSubcommand,
924 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700925 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -0700926 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600927 }
928
929 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
930 print("Usage: %s <subcommand> [options]" % sys.argv[0])
931 print
932 print("Available sucommands are: %s" % " ".join(subcommands))
933 exit(1)
934
935 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
936 subcmd.run()
937
938if __name__ == "__main__":
939 main()