blob: fbc47d7cde5eef6ea1cd3e17cab0bed849a67589 [file] [log] [blame]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001#!/usr/bin/env python3
2#
3# XGL
4#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
27
28import sys
Tobin Ehlis6cd06372014-12-17 17:44:50 -070029import os
Tobin Ehlis92dbf802014-10-22 09:06:33 -060030
31import xgl
Tobin Ehlis6cd06372014-12-17 17:44:50 -070032import xgl_helper
Tobin Ehlis92dbf802014-10-22 09:06:33 -060033
34class Subcommand(object):
35 def __init__(self, argv):
36 self.argv = argv
Chia-I Wuec30dcb2015-01-01 08:46:31 +080037 self.headers = xgl.headers
38 self.protos = xgl.protos
Tobin Ehlis92dbf802014-10-22 09:06:33 -060039
40 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060041 print(self.generate())
42
43 def generate(self):
44 copyright = self.generate_copyright()
45 header = self.generate_header()
46 body = self.generate_body()
47 footer = self.generate_footer()
48
49 contents = []
50 if copyright:
51 contents.append(copyright)
52 if header:
53 contents.append(header)
54 if body:
55 contents.append(body)
56 if footer:
57 contents.append(footer)
58
59 return "\n\n".join(contents)
60
61 def generate_copyright(self):
62 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
63
64/*
65 * XGL
66 *
67 * Copyright (C) 2014 LunarG, Inc.
68 *
69 * Permission is hereby granted, free of charge, to any person obtaining a
70 * copy of this software and associated documentation files (the "Software"),
71 * to deal in the Software without restriction, including without limitation
72 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
73 * and/or sell copies of the Software, and to permit persons to whom the
74 * Software is furnished to do so, subject to the following conditions:
75 *
76 * The above copyright notice and this permission notice shall be included
77 * in all copies or substantial portions of the Software.
78 *
79 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
81 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
82 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
83 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
84 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
85 * DEALINGS IN THE SOFTWARE.
86 */"""
87
88 def generate_header(self):
89 return "\n".join(["#include <" + h + ">" for h in self.headers])
90
91 def generate_body(self):
92 pass
93
94 def generate_footer(self):
95 pass
96
97 # Return set of printf '%' qualifier and input to that qualifier
Tobin Ehlis434db7c2015-01-10 12:42:41 -070098 def _get_printf_params(self, xgl_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060099 # TODO : Need ENUM and STRUCT checks here
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700100 if xgl_helper.is_type(xgl_type, 'enum'):#"_TYPE" in xgl_type: # TODO : This should be generic ENUM check
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600101 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600102 if "char*" == xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600103 return ("%s", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700104 if "uint64" in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600105 if '*' in xgl_type:
106 return ("%lu", "*%s" % name)
107 return ("%lu", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700108 if "size" in xgl_type:
Chia-I Wu54ed0792014-12-27 14:14:50 +0800109 if '*' in xgl_type:
110 return ("%zu", "*%s" % name)
111 return ("%zu", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700112 if "float" in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600113 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700114 if cpp:
115 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600116 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
117 return ("%f", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700118 if "bool" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600119 return ("%u", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700120 if True in [t in xgl_type for t in ["int", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600121 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700122 if cpp:
123 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600124 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
125 if '*' in xgl_type:
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700126 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600127 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700128 # TODO : This is special-cased as there's only one "format" param currently and it's nice to expand it
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700129 if "XGL_FORMAT" == xgl_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700130 if cpp:
131 return ("%p", "&%s" % name)
132 return ("{%s.channelFormat = %%s, %s.numericFormat = %%s}" % (name, name), "string_XGL_CHANNEL_FORMAT(%s.channelFormat), string_XGL_NUM_FORMAT(%s.numericFormat)" % (name, name))
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700133 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600134 return ("%p", "(void*)*%s" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700135 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600136
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700137 def _gen_layer_dbg_callback_header(self):
138 cbh_body = []
139 cbh_body.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
140 cbh_body.append('// Utility function to handle reporting')
141 cbh_body.append('// If callbacks are enabled, use them, otherwise use printf')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600142 cbh_body.append('static void layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700143 cbh_body.append(' XGL_VALIDATION_LEVEL validationLevel,')
144 cbh_body.append(' XGL_BASE_OBJECT srcObject,')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600145 cbh_body.append(' size_t location,')
146 cbh_body.append(' int32_t msgCode,')
147 cbh_body.append(' const char* pLayerPrefix,')
148 cbh_body.append(' const char* pMsg)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700149 cbh_body.append('{')
150 cbh_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
151 cbh_body.append(' if (pTrav) {')
152 cbh_body.append(' while (pTrav) {')
153 cbh_body.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
154 cbh_body.append(' pTrav = pTrav->pNext;')
155 cbh_body.append(' }')
156 cbh_body.append(' }')
157 cbh_body.append(' else {')
158 cbh_body.append(' switch (msgType) {')
159 cbh_body.append(' case XGL_DBG_MSG_ERROR:')
160 cbh_body.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
161 cbh_body.append(' break;')
162 cbh_body.append(' case XGL_DBG_MSG_WARNING:')
163 cbh_body.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
164 cbh_body.append(' break;')
165 cbh_body.append(' case XGL_DBG_MSG_PERF_WARNING:')
166 cbh_body.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
167 cbh_body.append(' break;')
168 cbh_body.append(' default:')
169 cbh_body.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
170 cbh_body.append(' break;')
171 cbh_body.append(' }')
172 cbh_body.append(' }')
173 cbh_body.append('}')
174 return "\n".join(cbh_body)
175
176 def _gen_layer_dbg_callback_register(self):
177 r_body = []
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600178 r_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700179 r_body.append('{')
180 r_body.append(' // This layer intercepts callbacks')
181 r_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));')
182 r_body.append(' if (!pNewDbgFuncNode)')
183 r_body.append(' return XGL_ERROR_OUT_OF_MEMORY;')
184 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
185 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
186 r_body.append(' pNewDbgFuncNode->pNext = pDbgFunctionHead;')
187 r_body.append(' pDbgFunctionHead = pNewDbgFuncNode;')
188 r_body.append(' XGL_RESULT result = nextTable.DbgRegisterMsgCallback(pfnMsgCallback, pUserData);')
189 r_body.append(' return result;')
190 r_body.append('}')
191 return "\n".join(r_body)
192
193 def _gen_layer_dbg_callback_unregister(self):
194 ur_body = []
195 ur_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
196 ur_body.append('{')
197 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
198 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
199 ur_body.append(' while (pTrav) {')
200 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
201 ur_body.append(' pPrev->pNext = pTrav->pNext;')
202 ur_body.append(' if (pDbgFunctionHead == pTrav)')
203 ur_body.append(' pDbgFunctionHead = pTrav->pNext;')
204 ur_body.append(' free(pTrav);')
205 ur_body.append(' break;')
206 ur_body.append(' }')
207 ur_body.append(' pPrev = pTrav;')
208 ur_body.append(' pTrav = pTrav->pNext;')
209 ur_body.append(' }')
210 ur_body.append(' XGL_RESULT result = nextTable.DbgUnregisterMsgCallback(pfnMsgCallback);')
211 ur_body.append(' return result;')
212 ur_body.append('}')
213 return "\n".join(ur_body)
214
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700215 def _generate_dispatch_entrypoints(self, qual="", layer="Generic", no_addr=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600216 if qual:
217 qual += " "
218
Tobin Ehlis907a0522014-11-25 16:59:27 -0700219 layer_name = layer
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700220 if no_addr:
221 layer_name = "%sNoAddr" % layer
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700222 if 'Cpp' in layer_name:
223 layer_name = "APIDumpNoAddrCpp"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600224 funcs = []
225 for proto in self.protos:
226 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Tobin Ehlis907a0522014-11-25 16:59:27 -0700227 if "Generic" == layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600228 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
229 param0_name = proto.params[0].name
230 ret_val = ''
231 stmt = ''
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600232 if proto.ret != "void":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600233 ret_val = "XGL_RESULT result = "
234 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700235 if proto.name == "EnumerateLayers":
236 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
237 funcs.append('%s%s\n'
238 '{\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700239 ' char str[1024];\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700240 ' if (gpu != NULL) {\n'
241 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700242 ' sprintf(str, "At start of layered %s\\n");\n'
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600243 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700244 ' pCurObj = gpuw;\n'
245 ' pthread_once(&tabOnce, initLayerTable);\n'
246 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700247 ' sprintf(str, "Completed layered %s\\n");\n'
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600248 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700249 ' fflush(stdout);\n'
250 ' %s'
251 ' } else {\n'
252 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
253 ' return XGL_ERROR_INVALID_POINTER;\n'
254 ' // This layer compatible with all GPUs\n'
255 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800256 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700257 ' return XGL_SUCCESS;\n'
258 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700259 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700260 elif 'DbgRegisterMsgCallback' == proto.name:
261 funcs.append(self._gen_layer_dbg_callback_register())
262 elif 'DbgUnregisterMsgCallback' == proto.name:
263 funcs.append(self._gen_layer_dbg_callback_unregister())
Jon Ashburn451c16f2014-11-25 11:08:42 -0700264 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600265 funcs.append('%s%s\n'
266 '{\n'
267 ' %snextTable.%s;\n'
268 '%s'
269 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
270 else:
271 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
272 funcs.append('%s%s\n'
273 '{\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700274 ' char str[1024];'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600275 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700276 ' sprintf(str, "At start of layered %s\\n");\n'
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600277 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600278 ' pCurObj = gpuw;\n'
279 ' pthread_once(&tabOnce, initLayerTable);\n'
280 ' %snextTable.%s;\n'
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700281 ' sprintf(str, "Completed layered %s\\n");\n'
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600282 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700283 ' fflush(stdout);\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600284 '%s'
285 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt))
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700286 elif "APIDumpCpp" in layer:
287 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
288 param0_name = proto.params[0].name
289 ret_val = ''
290 stmt = ''
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700291 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700292 create_params = 0 # Num of params at end of function that are created and returned as output values
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700293 if 'WsiX11CreatePresentableImage' in proto.name or 'AllocDescriptorSets' in proto.name:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700294 create_params = -2
295 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
296 create_params = -1
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600297 if proto.ret != "void":
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700298 ret_val = "XGL_RESULT result = "
299 stmt = " return result;\n"
300 f_open = ''
301 f_close = ''
302 if "File" in layer:
303 file_mode = "a"
304 if 'CreateDevice' in proto.name:
305 file_mode = "w"
306 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
307 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
308 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
309 else:
310 f_open = 'pthread_mutex_lock( &print_lock );\n '
311 log_func = 'cout << "t{" << getTIDIndex() << "} xgl%s(' % proto.name
312 f_close = '\n pthread_mutex_unlock( &print_lock );'
313 pindex = 0
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700314 prev_count_name = ''
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700315 for p in proto.params:
316 # TODO : Need to handle xglWsiX11CreatePresentableImage for which the last 2 params are returned vals
317 cp = False
318 if 0 != create_params:
319 # If this is any of the N last params of the func, treat as output
320 for y in range(-1, create_params-1, -1):
321 if p.name == proto.params[y].name:
322 cp = True
323 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
324 if no_addr and "%p" == pft:
325 (pft, pfi) = ("%s", '"addr"')
326 log_func += '%s = " << %s << ", ' % (p.name, pfi)
327 #print_vals += ', %s' % (pfi)
328 # TODO : Just want this to be simple check for params of STRUCT type
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700329 #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 ['char', 'void', 'int', 'XGL_CMD_BUFFER', 'XGL_QUEUE_SEMAPHORE', 'XGL_FENCE', 'XGL_SAMPLER']]):
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700330 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
331 sp_param_dict[pindex] = prev_count_name
332 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
333 sp_param_dict[pindex] = '*pCount'
334 elif xgl_helper.is_type(p.ty.strip('const').strip('*'), 'struct'):
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700335 if 'Wsi' not in proto.name:
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700336 sp_param_dict[pindex] = 'index'
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700337 pindex += 1
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700338 if p.name.endswith('Count'):
339 if '*' in p.ty:
340 prev_count_name = "*%s" % p.name
341 else:
342 prev_count_name = p.name
343 else:
344 prev_count_name = ''
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700345 log_func = log_func.strip(', ')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600346 if proto.ret != "void":
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700347 log_func += ') = " << string_XGL_RESULT((XGL_RESULT)result) << "\\n"'
348 #print_vals += ', string_XGL_RESULT_CODE(result)'
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700349 else:
350 log_func += ')\\n"'
351 log_func += ';'
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700352 if len(sp_param_dict) > 0:
353 i_decl = False
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700354 log_func += '\n string tmp_str;'
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700355 for sp_index in sp_param_dict:
356 if 'index' == sp_param_dict[sp_index]:
357 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
358 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
359 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
360 if "File" in layer:
361 if no_addr:
362 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
363 else:
364 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700365 else:
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700366 if no_addr:
367 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
368 log_func += '\n cout << " %s (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
369 else:
370 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
371 log_func += '\n cout << " %s (" << %s << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, proto.params[sp_index].name)
372 #log_func += '\n fflush(stdout);'
373 log_func += '\n }'
374 else: # We have a count value stored to iterate over an array
375 print_cast = ''
376 print_func = ''
377 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
378 print_cast = '&'
379 print_func = 'xgl_print_%s' % proto.params[sp_index].ty.strip('const ').strip('*').lower()
380 #cis_print_func = 'tmp_str = xgl_print_%s(&%s[i], " ");' % (proto.params[sp_index].ty.strip('const ').strip('*').lower(), proto.params[sp_index].name)
381# TODO : Need to display this address as a string
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700382 else:
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700383 print_cast = '(void*)'
384 print_func = 'string_convert_helper'
385 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
386 cis_print_func = 'tmp_str = %s(%s%s[i], " ");' % (print_func, print_cast, proto.params[sp_index].name)
387# else:
388# cis_print_func = ''
389 if not i_decl:
390 log_func += '\n uint32_t i;'
391 i_decl = True
392 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
393 log_func += '\n %s' % (cis_print_func)
394 if "File" in layer:
395 if no_addr:
396 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
397 else:
398 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
399 else:
400 if no_addr:
401 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
402 log_func += '\n cout << " %s[" << (uint32_t)i << "] (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
403 else:
404 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
405 #log_func += '\n cout << " %s[" << (uint32_t)i << "] (" << %s[i] << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, proto.params[sp_index].name)
406 log_func += '\n cout << " %s[" << i << "] (" << %s%s[i] << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, print_cast, proto.params[sp_index].name)
407 #log_func += '\n fflush(stdout);'
408 log_func += '\n }'
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700409 if proto.name == "EnumerateLayers":
410 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
411 funcs.append('%s%s\n'
412 '{\n'
413 ' if (gpu != NULL) {\n'
414 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
415 ' pCurObj = gpuw;\n'
416 ' pthread_once(&tabOnce, initLayerTable);\n'
417 ' %snextTable.%s;\n'
418 ' %s %s %s\n'
419 ' %s'
420 ' } else {\n'
421 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
422 ' return XGL_ERROR_INVALID_POINTER;\n'
423 ' // This layer compatible with all GPUs\n'
424 ' *pOutLayerCount = 1;\n'
425 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
426 ' return XGL_SUCCESS;\n'
427 ' }\n'
428 '}' % (qual, decl, proto.params[0].name, ret_val, c_call,f_open, log_func, f_close, stmt, layer_name))
429 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
430 funcs.append('%s%s\n'
431 '{\n'
432 ' %snextTable.%s;\n'
433 ' %s%s%s\n'
434 '%s'
435 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
436 else:
437 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
438 funcs.append('%s%s\n'
439 '{\n'
440 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
441 ' pCurObj = gpuw;\n'
442 ' pthread_once(&tabOnce, initLayerTable);\n'
443 ' %snextTable.%s;\n'
444 ' %s%s%s\n'
445 '%s'
446 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700447 elif "APIDump" in layer:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600448 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
449 param0_name = proto.params[0].name
450 ret_val = ''
451 stmt = ''
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700452 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700453 create_params = 0 # Num of params at end of function that are created and returned as output values
454 if 'WsiX11CreatePresentableImage' in proto.name:
455 create_params = -2
456 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
457 create_params = -1
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600458 if proto.ret != "void":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600459 ret_val = "XGL_RESULT result = "
460 stmt = " return result;\n"
Tobin Ehlis574b0142014-11-12 13:11:15 -0700461 f_open = ''
462 f_close = ''
Tobin Ehlis907a0522014-11-25 16:59:27 -0700463 if "File" in layer:
Tobin Ehlis1eba7792014-11-21 09:35:53 -0700464 file_mode = "a"
465 if 'CreateDevice' in proto.name:
466 file_mode = "w"
Chia-I Wu81f46672014-12-16 00:36:58 +0800467 f_open = 'pthread_mutex_lock( &file_lock );\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700468 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700469 f_close = '\n fclose(pOutFile);\n pthread_mutex_unlock( &file_lock );'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700470 else:
Chia-I Wu81f46672014-12-16 00:36:58 +0800471 f_open = 'pthread_mutex_lock( &print_lock );\n '
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700472 log_func = 'printf("t{%%u} xgl%s(' % proto.name
Tobin Ehlis1aa7d3e2014-11-20 12:18:45 -0700473 f_close = '\n pthread_mutex_unlock( &print_lock );'
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700474 print_vals = ', getTIDIndex()'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600475 pindex = 0
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700476 prev_count_name = ''
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600477 for p in proto.params:
Tobin Ehlisa554dc32014-11-19 15:52:46 -0700478 cp = False
479 if 0 != create_params:
480 # If this is any of the N last params of the func, treat as output
481 for y in range(-1, create_params-1, -1):
482 if p.name == proto.params[y].name:
483 cp = True
484 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700485 if no_addr and "%p" == pft:
486 (pft, pfi) = ("%s", '"addr"')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600487 log_func += '%s = %s, ' % (p.name, pft)
488 print_vals += ', %s' % (pfi)
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700489 # Catch array inputs that are bound by a "Count" param
490 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
491 sp_param_dict[pindex] = prev_count_name
Tobin Ehlisd204b1a2015-01-20 09:48:48 -0700492 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
493 sp_param_dict[pindex] = '*pCount'
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700494 elif 'Wsi' not in proto.name and xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct'):
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700495 sp_param_dict[pindex] = 'index'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600496 pindex += 1
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700497 if p.name.endswith('Count'):
Courtney Goeltzenleuchter08cf7cc2015-01-13 15:32:18 -0700498 if '*' in p.ty:
499 prev_count_name = "*%s" % p.name
500 else:
501 prev_count_name = p.name
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700502 else:
503 prev_count_name = ''
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600504 log_func = log_func.strip(', ')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600505 if proto.ret != "void":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600506 log_func += ') = %s\\n"'
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -0700507 print_vals += ', string_XGL_RESULT(result)'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600508 else:
509 log_func += ')\\n"'
510 log_func = '%s%s);' % (log_func, print_vals)
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700511 if len(sp_param_dict) > 0:
512 i_decl = False
513 log_func += '\n char *pTmpStr = "";'
514 for sp_index in sorted(sp_param_dict):
515 # TODO : Clean this if/else block up, too much duplicated code
516 if 'index' == sp_param_dict[sp_index]:
517 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
518 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
519 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, proto.params[sp_index].name)
520 if "File" in layer:
521 if no_addr:
522 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
523 else:
524 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700525 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700526 if no_addr:
527 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
528 else:
529 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
530 log_func += '\n fflush(stdout);'
531 log_func += '\n free(pTmpStr);\n }'
532 else: # should have a count value stored to iterate over array
533 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
534 cis_print_func = 'pTmpStr = xgl_print_%s(&%s[i], " ");' % (proto.params[sp_index].ty.strip('const ').strip('*').lower(), proto.params[sp_index].name)
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700535 else:
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700536 cis_print_func = 'pTmpStr = (char*)malloc(sizeof(char));\n sprintf(pTmpStr, " %%p", %s[i]);' % proto.params[sp_index].name
537 if not i_decl:
538 log_func += '\n uint32_t i;'
539 i_decl = True
Jon Ashburn48637592015-01-14 08:52:37 -0700540 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
Tobin Ehlisc7e926b2014-12-18 15:20:05 -0700541 log_func += '\n %s' % (cis_print_func)
542 if "File" in layer:
543 if no_addr:
544 log_func += '\n fprintf(pOutFile, " %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
545 else:
546 log_func += '\n fprintf(pOutFile, " %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
547 else:
548 if no_addr:
549 log_func += '\n printf(" %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
550 else:
551 log_func += '\n printf(" %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
552 log_func += '\n fflush(stdout);'
553 log_func += '\n free(pTmpStr);\n }'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700554 if proto.name == "EnumerateLayers":
555 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
556 funcs.append('%s%s\n'
557 '{\n'
558 ' if (gpu != NULL) {\n'
559 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
560 ' pCurObj = gpuw;\n'
561 ' pthread_once(&tabOnce, initLayerTable);\n'
562 ' %snextTable.%s;\n'
563 ' %s %s %s\n'
564 ' %s'
565 ' } else {\n'
566 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
567 ' return XGL_ERROR_INVALID_POINTER;\n'
568 ' // This layer compatible with all GPUs\n'
569 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800570 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700571 ' return XGL_SUCCESS;\n'
572 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700573 '}' % (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 -0700574 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600575 funcs.append('%s%s\n'
576 '{\n'
577 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700578 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600579 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700580 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600581 else:
582 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
583 funcs.append('%s%s\n'
584 '{\n'
585 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
586 ' pCurObj = gpuw;\n'
587 ' pthread_once(&tabOnce, initLayerTable);\n'
588 ' %snextTable.%s;\n'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700589 ' %s%s%s\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600590 '%s'
Tobin Ehlis574b0142014-11-12 13:11:15 -0700591 '}' % (qual, decl, proto.params[0].name, ret_val, c_call, f_open, log_func, f_close, stmt))
Tobin Ehlis907a0522014-11-25 16:59:27 -0700592 elif "ObjectTracker" == layer:
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700593 obj_type_mapping = {base_t : base_t.replace("XGL_", "XGL_OBJECT_TYPE_") for base_t in xgl.object_type_list}
594 # For the various "super-types" we have to use function to distinguish sub type
595 for obj_type in ["XGL_BASE_OBJECT", "XGL_OBJECT", "XGL_DYNAMIC_STATE_OBJECT"]:
596 obj_type_mapping[obj_type] = "ll_get_obj_type(object)"
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700597
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600598 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
599 param0_name = proto.params[0].name
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700600 p0_type = proto.params[0].ty.strip('*').strip('const ')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600601 create_line = ''
602 destroy_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700603 if 'DbgRegisterMsgCallback' in proto.name:
604 using_line = ' // This layer intercepts callbacks\n'
605 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));\n'
606 using_line += ' if (!pNewDbgFuncNode)\n'
607 using_line += ' return XGL_ERROR_OUT_OF_MEMORY;\n'
608 using_line += ' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;\n'
609 using_line += ' pNewDbgFuncNode->pUserData = pUserData;\n'
610 using_line += ' pNewDbgFuncNode->pNext = pDbgFunctionHead;\n'
611 using_line += ' pDbgFunctionHead = pNewDbgFuncNode;\n'
612 elif 'DbgUnregisterMsgCallback' in proto.name:
613 using_line = ' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;\n'
614 using_line += ' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;\n'
615 using_line += ' while (pTrav) {\n'
616 using_line += ' if (pTrav->pfnMsgCallback == pfnMsgCallback) {\n'
617 using_line += ' pPrev->pNext = pTrav->pNext;\n'
618 using_line += ' if (pDbgFunctionHead == pTrav)\n'
619 using_line += ' pDbgFunctionHead = pTrav->pNext;\n'
620 using_line += ' free(pTrav);\n'
621 using_line += ' break;\n'
622 using_line += ' }\n'
623 using_line += ' pPrev = pTrav;\n'
624 using_line += ' pTrav = pTrav->pNext;\n'
625 using_line += ' }\n'
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700626 # Special cases for API funcs that don't use an object as first arg
627 elif True in [no_use_proto in proto.name for no_use_proto in ['GlobalOption', 'CreateInstance']]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600628 using_line = ''
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700629 else:
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600630 using_line = ' ll_increment_use_count((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
Tobin Ehlis235c20e2015-01-16 08:56:30 -0700631 if 'QueueSubmit' in proto.name:
632 using_line += ' set_status((void*)fence, XGL_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED);\n'
633 elif 'GetFenceStatus' in proto.name:
634 using_line += ' // Warn if submitted_flag is not set\n'
635 using_line += ' validate_status((void*)fence, XGL_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED, "Status Requested for Unsubmitted Fence");\n'
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700636 if 'AllocDescriptor' in proto.name: # Allocates array of DSs
637 create_line = ' for (uint32_t i; i < *pCount; i++) {\n'
638 create_line += ' ll_insert_obj((void*)pDescriptorSets[i], XGL_OBJECT_TYPE_DESCRIPTOR_SET);\n'
639 create_line += ' }\n'
640 elif 'Create' in proto.name or 'Alloc' in proto.name:
641 create_line = ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*').strip('const ')])
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700642 if 'DestroyObject' in proto.name:
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600643 destroy_line = ' ll_destroy_obj((void*)%s);\n' % (param0_name)
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700644 using_line = ''
645 else:
646 if 'Destroy' in proto.name or 'Free' in proto.name:
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600647 destroy_line = ' ll_remove_obj_type((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700648 using_line = ''
649 if 'DestroyDevice' in proto.name:
650 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
651 destroy_line += ' char str[1024];\n'
652 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'
653 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
654 destroy_line += ' pTrav = pTrav->pNextGlobal;\n }\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600655 ret_val = ''
656 stmt = ''
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600657 if proto.ret != "void":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600658 ret_val = "XGL_RESULT result = "
659 stmt = " return result;\n"
Jon Ashburn451c16f2014-11-25 11:08:42 -0700660 if proto.name == "EnumerateLayers":
661 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
662 funcs.append('%s%s\n'
663 '{\n'
664 ' if (gpu != NULL) {\n'
665 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
666 ' %s'
667 ' pCurObj = gpuw;\n'
668 ' pthread_once(&tabOnce, initLayerTable);\n'
669 ' %snextTable.%s;\n'
670 ' %s%s'
671 ' %s'
672 ' } else {\n'
673 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
674 ' return XGL_ERROR_INVALID_POINTER;\n'
675 ' // This layer compatible with all GPUs\n'
676 ' *pOutLayerCount = 1;\n'
Chia-I Wua837c522014-12-16 10:47:33 +0800677 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
Jon Ashburn451c16f2014-11-25 11:08:42 -0700678 ' return XGL_SUCCESS;\n'
679 ' }\n'
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700680 '}' % (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 -0700681 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600682 funcs.append('%s%s\n'
683 '{\n'
684 '%s'
685 ' %snextTable.%s;\n'
686 '%s%s'
687 '%s'
688 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
689 else:
690 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
691 funcs.append('%s%s\n'
692 '{\n'
693 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
694 '%s'
695 ' pCurObj = gpuw;\n'
696 ' pthread_once(&tabOnce, initLayerTable);\n'
697 ' %snextTable.%s;\n'
698 '%s%s'
699 '%s'
700 '}' % (qual, decl, proto.params[0].name, using_line, ret_val, c_call, create_line, destroy_line, stmt))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700701 elif "ParamChecker" == layer:
702 # TODO : Need to fix up the non-else cases below to do param checking as well
703 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
704 param0_name = proto.params[0].name
705 ret_val = ''
706 stmt = ''
707 param_checks = []
708 # Add code to check enums and structs
709 # TODO : Currently only validating enum values, need to validate everything
710 str_decl = False
Tobin Ehlis773371f2014-12-18 13:51:21 -0700711 prev_count_name = ''
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700712 for p in proto.params:
713 if xgl_helper.is_type(p.ty.strip('*').strip('const '), 'enum'):
714 if not str_decl:
715 param_checks.append(' char str[1024];')
716 str_decl = True
717 param_checks.append(' if (!validate_%s(%s)) {' % (p.ty, p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700718 param_checks.append(' sprintf(str, "Parameter %s to function %s has invalid value of %%i.", (int)%s);' % (p.name, proto.name, p.name))
719 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
720 param_checks.append(' }')
721 elif xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct') and 'const' in p.ty:
Tobin Ehlis773371f2014-12-18 13:51:21 -0700722 is_array = False
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700723 if not str_decl:
724 param_checks.append(' char str[1024];')
725 str_decl = True
726 if '*' in p.ty: # First check for null ptr
Tobin Ehlis773371f2014-12-18 13:51:21 -0700727 # If this is an input array, parse over all of the array elements
728 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
729 #if 'pImageViews' in p.name:
730 is_array = True
731 param_checks.append(' uint32_t i;')
732 param_checks.append(' for (i = 0; i < %s; i++) {' % prev_count_name)
733 param_checks.append(' if (!xgl_validate_%s(&%s[i])) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
734 param_checks.append(' sprintf(str, "Parameter %s[%%i] to function %s contains an invalid value.", i);' % (p.name, proto.name))
735 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
736 param_checks.append(' }')
737 param_checks.append(' }')
738 else:
739 param_checks.append(' if (!%s) {' % p.name)
740 param_checks.append(' sprintf(str, "Struct ptr parameter %s to function %s is NULL.");' % (p.name, proto.name))
741 param_checks.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
742 param_checks.append(' }')
743 param_checks.append(' else if (!xgl_validate_%s(%s)) {' % (p.ty.strip('*').strip('const ').lower(), p.name))
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700744 else:
745 param_checks.append(' if (!xgl_validate_%s(%s)) {' % (p.ty.strip('const ').lower(), p.name))
Tobin Ehlis773371f2014-12-18 13:51:21 -0700746 if not is_array:
747 param_checks.append(' sprintf(str, "Parameter %s to function %s contains an invalid value.");' % (p.name, proto.name))
748 param_checks.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, 1, "PARAMCHECK", str);')
749 param_checks.append(' }')
750 if p.name.endswith('Count'):
751 prev_count_name = p.name
752 else:
753 prev_count_name = ''
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600754 if proto.ret != "void":
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700755 ret_val = "XGL_RESULT result = "
756 stmt = " return result;\n"
757 if proto.name == "EnumerateLayers":
758 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
759 funcs.append('%s%s\n'
760 '{\n'
761 ' char str[1024];\n'
762 ' if (gpu != NULL) {\n'
763 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
764 ' sprintf(str, "At start of layered %s\\n");\n'
765 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
766 ' pCurObj = gpuw;\n'
767 ' pthread_once(&tabOnce, initLayerTable);\n'
768 ' %snextTable.%s;\n'
769 ' sprintf(str, "Completed layered %s\\n");\n'
770 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, "PARAMCHECK", str);\n'
771 ' fflush(stdout);\n'
772 ' %s'
773 ' } else {\n'
774 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
775 ' return XGL_ERROR_INVALID_POINTER;\n'
776 ' // This layer compatible with all GPUs\n'
777 ' *pOutLayerCount = 1;\n'
778 ' strncpy(pOutLayers[0], "%s", maxStringSize);\n'
779 ' return XGL_SUCCESS;\n'
780 ' }\n'
781 '}' % (qual, decl, proto.params[0].name, proto.name, ret_val, c_call, proto.name, stmt, layer_name))
782 elif 'DbgRegisterMsgCallback' == proto.name:
783 funcs.append(self._gen_layer_dbg_callback_register())
784 elif 'DbgUnregisterMsgCallback' == proto.name:
785 funcs.append(self._gen_layer_dbg_callback_unregister())
786 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
787 funcs.append('%s%s\n'
788 '{\n'
789 '%s\n'
790 ' %snextTable.%s;\n'
791 '%s'
792 '}' % (qual, decl, "\n".join(param_checks), ret_val, proto.c_call(), stmt))
793 else:
794 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
795 funcs.append('%s%s\n'
796 '{\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700797 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700798 ' pCurObj = gpuw;\n'
799 ' pthread_once(&tabOnce, initLayerTable);\n'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700800 '%s\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700801 ' %snextTable.%s;\n'
Tobin Ehlis6cd06372014-12-17 17:44:50 -0700802 '%s'
Tobin Ehlis9d139862014-12-18 08:44:01 -0700803 '}' % (qual, decl, proto.params[0].name, "\n".join(param_checks), ret_val, c_call, stmt))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600804
805 return "\n\n".join(funcs)
806
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700807 def _generate_extensions(self):
808 exts = []
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600809 exts.append('uint64_t objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700810 exts.append('{')
811 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
812 exts.append('}')
813 exts.append('')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600814 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, uint64_t objCount, OBJTRACK_NODE* pObjNodeArray)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700815 exts.append('{')
816 exts.append(" // This bool flags if we're pulling all objs or just a single class of objs")
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600817 exts.append(' bool32_t bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700818 exts.append(' // Check the count first thing')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600819 exts.append(' uint64_t maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700820 exts.append(' if (objCount > maxObjCount) {')
821 exts.append(' char str[1024];')
822 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));')
823 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
824 exts.append(' return XGL_ERROR_INVALID_VALUE;')
825 exts.append(' }')
826 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600827 exts.append(' for (uint64_t i = 0; i < objCount; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700828 exts.append(' if (!pTrav) {')
829 exts.append(' char str[1024];')
830 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);')
831 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
832 exts.append(' return XGL_ERROR_UNKNOWN;')
833 exts.append(' }')
834 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
835 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
836 exts.append(' }')
837 exts.append(' return XGL_SUCCESS;')
838 exts.append('}')
839
840 return "\n".join(exts)
841
Chia-I Wu706533e2015-01-05 13:18:57 +0800842 def _generate_layer_gpa_function(self, extensions=[]):
843 func_body = ["#include \"xgl_generic_intercept_proc_helper.h\""]
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600844 func_body.append("XGL_LAYER_EXPORT void* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* funcName)\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600845 "{\n"
846 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800847 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600848 " if (gpu == NULL)\n"
849 " return NULL;\n"
850 " pCurObj = gpuw;\n"
851 " pthread_once(&tabOnce, initLayerTable);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800852 " addr = layer_intercept_proc(funcName);\n"
853 " if (addr)\n"
854 " return addr;")
855
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700856 if 0 != len(extensions):
857 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800858 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700859 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600860 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600861 " if (gpuw->pGPA == NULL)\n"
862 " return NULL;\n"
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700863 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600864 " }\n"
865 "}\n")
866 return "\n".join(func_body)
867
868 def _generate_layer_dispatch_table(self, prefix='xgl'):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800869 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600870 func_body.append('static void initLayerTable()\n'
871 '{\n'
Mark Lobodzinski953a1692015-01-09 15:12:03 -0600872 ' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600873 ' fpNextGPA = pCurObj->pGPA;\n'
874 ' assert(fpNextGPA);\n');
875
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800876 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600877 func_body.append("}\n")
878 return "\n".join(func_body)
879
880class LayerFuncsSubcommand(Subcommand):
881 def generate_header(self):
882 return '#include <xglLayer.h>\n#include "loader.h"'
883
884 def generate_body(self):
885 return self._generate_dispatch_entrypoints("static", True)
886
887class LayerDispatchSubcommand(Subcommand):
888 def generate_header(self):
889 return '#include "layer_wrappers.h"'
890
891 def generate_body(self):
892 return self._generate_layer_dispatch_table()
893
894class GenericLayerSubcommand(Subcommand):
895 def generate_header(self):
896 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'
897
898 def generate_body(self):
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700899 body = [self._gen_layer_dbg_callback_header(),
900 self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700901 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "Generic"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600902 self._generate_layer_gpa_function()]
903
904 return "\n\n".join(body)
905
906class ApiDumpSubcommand(Subcommand):
907 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700908 header_txt = []
909 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')
910 header_txt.append('#define MAX_TID 513')
911 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
912 header_txt.append('static uint32_t maxTID = 0;')
913 header_txt.append('// Map actual TID to an index value and return that index')
914 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
915 header_txt.append('static uint32_t getTIDIndex() {')
916 header_txt.append(' pthread_t tid = pthread_self();')
917 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
918 header_txt.append(' if (tid == tidMapping[i])')
919 header_txt.append(' return i;')
920 header_txt.append(' }')
921 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
922 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
923 header_txt.append(' tidMapping[maxTID++] = tid;')
924 header_txt.append(' assert(maxTID < MAX_TID);')
925 header_txt.append(' return retVal;')
926 header_txt.append('}')
927 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600928
929 def generate_body(self):
930 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700931 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump"),
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600932 self._generate_layer_gpa_function()]
933
934 return "\n\n".join(body)
935
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700936class ApiDumpCppSubcommand(Subcommand):
937 def generate_header(self):
938 header_txt = []
939 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include "xglLayer.h"\n#include "xgl_struct_string_helper_cpp.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\npthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;\n')
940 header_txt.append('#define MAX_TID 513')
941 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
942 header_txt.append('static uint32_t maxTID = 0;')
943 header_txt.append('// Map actual TID to an index value and return that index')
944 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
945 header_txt.append('static uint32_t getTIDIndex() {')
946 header_txt.append(' pthread_t tid = pthread_self();')
947 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
948 header_txt.append(' if (tid == tidMapping[i])')
949 header_txt.append(' return i;')
950 header_txt.append(' }')
951 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
952 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
953 header_txt.append(' tidMapping[maxTID++] = tid;')
954 header_txt.append(' assert(maxTID < MAX_TID);')
955 header_txt.append(' return retVal;')
956 header_txt.append('}')
957 return "\n".join(header_txt)
958
959 def generate_body(self):
960 body = [self._generate_layer_dispatch_table(),
961 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp"),
962 self._generate_layer_gpa_function()]
963
964 return "\n\n".join(body)
965
Tobin Ehlis574b0142014-11-12 13:11:15 -0700966class ApiDumpFileSubcommand(Subcommand):
967 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700968 header_txt = []
969 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')
970 header_txt.append('#define MAX_TID 513')
971 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
972 header_txt.append('static uint32_t maxTID = 0;')
973 header_txt.append('// Map actual TID to an index value and return that index')
974 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
975 header_txt.append('static uint32_t getTIDIndex() {')
976 header_txt.append(' pthread_t tid = pthread_self();')
977 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
978 header_txt.append(' if (tid == tidMapping[i])')
979 header_txt.append(' return i;')
980 header_txt.append(' }')
981 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
982 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
983 header_txt.append(' tidMapping[maxTID++] = tid;')
984 header_txt.append(' assert(maxTID < MAX_TID);')
985 header_txt.append(' return retVal;')
986 header_txt.append('}')
987 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700988
989 def generate_body(self):
990 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -0700991 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpFile"),
Tobin Ehlis574b0142014-11-12 13:11:15 -0700992 self._generate_layer_gpa_function()]
993
994 return "\n\n".join(body)
995
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700996class ApiDumpNoAddrSubcommand(Subcommand):
997 def generate_header(self):
998 header_txt = []
999 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')
1000 header_txt.append('#define MAX_TID 513')
1001 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
1002 header_txt.append('static uint32_t maxTID = 0;')
1003 header_txt.append('// Map actual TID to an index value and return that index')
1004 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
1005 header_txt.append('static uint32_t getTIDIndex() {')
1006 header_txt.append(' pthread_t tid = pthread_self();')
1007 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
1008 header_txt.append(' if (tid == tidMapping[i])')
1009 header_txt.append(' return i;')
1010 header_txt.append(' }')
1011 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
1012 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
1013 header_txt.append(' tidMapping[maxTID++] = tid;')
1014 header_txt.append(' assert(maxTID < MAX_TID);')
1015 header_txt.append(' return retVal;')
1016 header_txt.append('}')
1017 return "\n".join(header_txt)
1018
1019 def generate_body(self):
1020 body = [self._generate_layer_dispatch_table(),
1021 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDump", True),
1022 self._generate_layer_gpa_function()]
1023
1024 return "\n\n".join(body)
1025
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001026class ApiDumpNoAddrCppSubcommand(Subcommand):
1027 def generate_header(self):
1028 header_txt = []
1029 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include "xglLayer.h"\n#include "xgl_struct_string_helper_no_addr_cpp.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\npthread_mutex_t print_lock = PTHREAD_MUTEX_INITIALIZER;\n')
1030 header_txt.append('#define MAX_TID 513')
1031 header_txt.append('static pthread_t tidMapping[MAX_TID] = {0};')
1032 header_txt.append('static uint32_t maxTID = 0;')
1033 header_txt.append('// Map actual TID to an index value and return that index')
1034 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
1035 header_txt.append('static uint32_t getTIDIndex() {')
1036 header_txt.append(' pthread_t tid = pthread_self();')
1037 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
1038 header_txt.append(' if (tid == tidMapping[i])')
1039 header_txt.append(' return i;')
1040 header_txt.append(' }')
1041 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
1042 header_txt.append(' uint32_t retVal = (uint32_t)maxTID;')
1043 header_txt.append(' tidMapping[maxTID++] = tid;')
1044 header_txt.append(' assert(maxTID < MAX_TID);')
1045 header_txt.append(' return retVal;')
1046 header_txt.append('}')
1047 return "\n".join(header_txt)
1048
1049 def generate_body(self):
1050 body = [self._generate_layer_dispatch_table(),
1051 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "APIDumpCpp", True),
1052 self._generate_layer_gpa_function()]
1053
1054 return "\n\n".join(body)
1055
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001056class ObjectTrackerSubcommand(Subcommand):
1057 def generate_header(self):
1058 header_txt = []
1059 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 -07001060 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
1061 header_txt.append('static pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\nstatic long long unsigned int object_track_index = 0;')
1062 header_txt.append('// Ptr to LL of dbg functions')
1063 header_txt.append('static XGL_LAYER_DBG_FUNCTION_NODE *pDbgFunctionHead = NULL;')
1064 header_txt.append('// Utility function to handle reporting')
1065 header_txt.append('// If callbacks are enabled, use them, otherwise use printf')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001066 header_txt.append('static void layerCbMsg(XGL_DBG_MSG_TYPE msgType,')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001067 header_txt.append(' XGL_VALIDATION_LEVEL validationLevel,')
1068 header_txt.append(' XGL_BASE_OBJECT srcObject,')
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001069 header_txt.append(' size_t location,')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001070 header_txt.append(' int32_t msgCode,')
Chia-I Wua837c522014-12-16 10:47:33 +08001071 header_txt.append(' const char* pLayerPrefix,')
1072 header_txt.append(' const char* pMsg)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001073 header_txt.append('{')
1074 header_txt.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = pDbgFunctionHead;')
1075 header_txt.append(' if (pTrav) {')
1076 header_txt.append(' while (pTrav) {')
Chia-I Wu7461fcf2014-12-27 15:16:07 +08001077 header_txt.append(' pTrav->pfnMsgCallback(msgType, validationLevel, srcObject, location, msgCode, pMsg, pTrav->pUserData);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001078 header_txt.append(' pTrav = pTrav->pNext;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001079 header_txt.append(' }')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001080 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001081 header_txt.append(' else {')
1082 header_txt.append(' switch (msgType) {')
1083 header_txt.append(' case XGL_DBG_MSG_ERROR:')
1084 header_txt.append(' printf("{%s}ERROR : %s\\n", pLayerPrefix, pMsg);')
1085 header_txt.append(' break;')
1086 header_txt.append(' case XGL_DBG_MSG_WARNING:')
1087 header_txt.append(' printf("{%s}WARN : %s\\n", pLayerPrefix, pMsg);')
1088 header_txt.append(' break;')
1089 header_txt.append(' case XGL_DBG_MSG_PERF_WARNING:')
1090 header_txt.append(' printf("{%s}PERF_WARN : %s\\n", pLayerPrefix, pMsg);')
1091 header_txt.append(' break;')
1092 header_txt.append(' default:')
1093 header_txt.append(' printf("{%s}INFO : %s\\n", pLayerPrefix, pMsg);')
1094 header_txt.append(' break;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001095 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001096 header_txt.append(' }')
1097 header_txt.append('}')
1098 header_txt.append('// We maintain a "Global" list which links every object and a')
1099 header_txt.append('// per-Object list which just links objects of a given type')
1100 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
1101 header_txt.append('typedef struct _objNode {')
1102 header_txt.append(' OBJTRACK_NODE obj;')
1103 header_txt.append(' struct _objNode *pNextObj;')
1104 header_txt.append(' struct _objNode *pNextGlobal;')
1105 header_txt.append('} objNode;')
1106 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
1107 header_txt.append('static objNode *pGlobalHead = NULL;')
1108 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
1109 header_txt.append('static uint64_t numTotalObjs = 0;')
1110 header_txt.append('// Debug function to print global list and each individual object list')
1111 header_txt.append('static void ll_print_lists()')
1112 header_txt.append('{')
1113 header_txt.append(' objNode* pTrav = pGlobalHead;')
1114 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
1115 header_txt.append(' while (pTrav) {')
1116 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);')
1117 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1118 header_txt.append(' }')
1119 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
1120 header_txt.append(' pTrav = pObjectHead[i];')
1121 header_txt.append(' if (pTrav) {')
1122 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
1123 header_txt.append(' while (pTrav) {')
1124 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);')
1125 header_txt.append(' pTrav = pTrav->pNextObj;')
1126 header_txt.append(' }')
1127 header_txt.append(' }')
1128 header_txt.append(' }')
1129 header_txt.append('}')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001130 header_txt.append('static void ll_insert_obj(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001131 header_txt.append(' char str[1024];')
1132 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1133 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1134 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
1135 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
1136 header_txt.append(' pNewObjNode->obj.objType = objType;')
1137 header_txt.append(' pNewObjNode->obj.numUses = 0;')
1138 header_txt.append(' // insert at front of global list')
1139 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
1140 header_txt.append(' pGlobalHead = pNewObjNode;')
1141 header_txt.append(' // insert at front of object list')
1142 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
1143 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
1144 header_txt.append(' // increment obj counts')
1145 header_txt.append(' numObjs[objType]++;')
1146 header_txt.append(' numTotalObjs++;')
1147 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 +08001148 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001149 header_txt.append('}')
1150 header_txt.append('// Traverse global list and return type for given object')
1151 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
1152 header_txt.append(' objNode *pTrav = pGlobalHead;')
1153 header_txt.append(' while (pTrav) {')
1154 header_txt.append(' if (pTrav->obj.pObj == object)')
1155 header_txt.append(' return pTrav->obj.objType;')
1156 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1157 header_txt.append(' }')
1158 header_txt.append(' char str[1024];')
1159 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
1160 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
1161 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
1162 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001163 header_txt.append('#if 0')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001164 header_txt.append('static uint64_t ll_get_obj_uses(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001165 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1166 header_txt.append(' while (pTrav) {')
1167 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1168 header_txt.append(' return pTrav->obj.numUses;')
1169 header_txt.append(' }')
1170 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001171 header_txt.append(' }')
1172 header_txt.append(' return 0;')
1173 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001174 header_txt.append('#endif')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001175 header_txt.append('static void ll_increment_use_count(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001176 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001177 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001178 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1179 header_txt.append(' pTrav->obj.numUses++;')
1180 header_txt.append(' char str[1024];')
1181 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);')
1182 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1183 header_txt.append(' return;')
1184 header_txt.append(' }')
1185 header_txt.append(' pTrav = pTrav->pNextObj;')
1186 header_txt.append(' }')
1187 header_txt.append(' // If we do not find obj, insert it and then increment count')
1188 header_txt.append(' char str[1024];')
1189 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));')
1190 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1191 header_txt.append('')
1192 header_txt.append(' ll_insert_obj(pObj, objType);')
1193 header_txt.append(' ll_increment_use_count(pObj, objType);')
1194 header_txt.append('}')
1195 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1196 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1197 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001198 header_txt.append('static void ll_remove_obj_type(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001199 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1200 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1201 header_txt.append(' while (pTrav) {')
1202 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1203 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1204 header_txt.append(' // update HEAD of Obj list as needed')
1205 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1206 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1207 header_txt.append(' assert(numObjs[objType] > 0);')
1208 header_txt.append(' numObjs[objType]--;')
1209 header_txt.append(' char str[1024];')
1210 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1211 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 -06001212 header_txt.append(' return;')
1213 header_txt.append(' }')
1214 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001215 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001216 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001217 header_txt.append(' char str[1024];')
1218 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));')
1219 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1220 header_txt.append('}')
1221 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1222 header_txt.append('// remove obj from global list')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001223 header_txt.append('static void ll_destroy_obj(void* pObj) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001224 header_txt.append(' objNode *pTrav = pGlobalHead;')
1225 header_txt.append(' objNode *pPrev = pGlobalHead;')
1226 header_txt.append(' while (pTrav) {')
1227 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1228 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1229 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1230 header_txt.append(' // update HEAD of global list if needed')
1231 header_txt.append(' if (pGlobalHead == pTrav)')
1232 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
1233 header_txt.append(' free(pTrav);')
1234 header_txt.append(' assert(numTotalObjs > 0);')
1235 header_txt.append(' numTotalObjs--;')
1236 header_txt.append(' char str[1024];')
1237 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));')
1238 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1239 header_txt.append(' return;')
1240 header_txt.append(' }')
1241 header_txt.append(' pPrev = pTrav;')
1242 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1243 header_txt.append(' }')
1244 header_txt.append(' char str[1024];')
1245 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1246 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 -06001247 header_txt.append('}')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001248 header_txt.append('// Set selected flag state for an object node')
1249 header_txt.append('static void set_status(void* pObj, XGL_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
1250 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1251 header_txt.append(' while (pTrav) {')
1252 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1253 header_txt.append(' pTrav->obj.status |= status_flag;')
1254 header_txt.append(' return;')
1255 header_txt.append(' }')
1256 header_txt.append(' pTrav = pTrav->pNextObj;')
1257 header_txt.append(' }')
1258 header_txt.append(' // If we do not find it print an error')
1259 header_txt.append(' char str[1024];')
1260 header_txt.append(' sprintf(str, "Unable to set status for non-existent object %p of %s type", pObj, string_XGL_OBJECT_TYPE(objType));')
1261 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1262 header_txt.append('}')
1263 header_txt.append('')
1264 header_txt.append('// Check object status for selected flag state')
1265 header_txt.append('static void validate_status(void* pObj, XGL_OBJECT_TYPE objType, OBJECT_STATUS status_flag, char* fail_msg) {')
1266 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1267 header_txt.append(' while (pTrav) {')
1268 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1269 header_txt.append(' if ((pTrav->obj.status && status_flag) != status_flag) {')
1270 header_txt.append(' char str[1024];')
1271 header_txt.append(' sprintf(str, "OBJECT VALIDATION WARNING: %s object %p: %s", string_XGL_OBJECT_TYPE(objType), (void*)pObj, fail_msg);')
1272 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INVALID_FENCE, "OBJTRACK", str);')
1273 header_txt.append(' }')
1274 header_txt.append(' return;')
1275 header_txt.append(' }')
1276 header_txt.append(' pTrav = pTrav->pNextObj;')
1277 header_txt.append(' }')
1278 header_txt.append(' // If we do not find it print an error')
1279 header_txt.append(' char str[1024];')
1280 header_txt.append(' sprintf(str, "Unable to obtain status for non-existent object %p of %s type", pObj, string_XGL_OBJECT_TYPE(objType));')
1281 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1282 header_txt.append('}')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001283
1284 return "\n".join(header_txt)
1285
1286 def generate_body(self):
1287 body = [self._generate_layer_dispatch_table(),
Tobin Ehlis907a0522014-11-25 16:59:27 -07001288 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ObjectTracker"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001289 self._generate_extensions(),
1290 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001291
1292 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001293
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001294class ParamCheckerSubcommand(Subcommand):
1295 def generate_header(self):
1296 return '#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <assert.h>\n#include <pthread.h>\n#include "xglLayer.h"\n#include "xgl_enum_validate_helper.h"\n#include "xgl_struct_validate_helper.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\nstatic pthread_once_t tabOnce = PTHREAD_ONCE_INIT;\n'
1297
1298 def generate_body(self):
1299 body = [self._gen_layer_dbg_callback_header(),
1300 self._generate_layer_dispatch_table(),
1301 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT", "ParamChecker"),
1302 self._generate_layer_gpa_function()]
1303
1304 return "\n\n".join(body)
1305
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001306def main():
1307 subcommands = {
1308 "layer-funcs" : LayerFuncsSubcommand,
1309 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001310 "Generic" : GenericLayerSubcommand,
1311 "ApiDump" : ApiDumpSubcommand,
1312 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -07001313 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001314 "ApiDumpCpp" : ApiDumpCppSubcommand,
1315 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001316 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001317 "ParamChecker" : ParamCheckerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001318 }
1319
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001320 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1321 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001322 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001323 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001324 exit(1)
1325
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001326 hfp = xgl_helper.HeaderFileParser(sys.argv[2])
1327 hfp.parse()
1328 xgl_helper.enum_val_dict = hfp.get_enum_val_dict()
1329 xgl_helper.enum_type_dict = hfp.get_enum_type_dict()
1330 xgl_helper.struct_dict = hfp.get_struct_dict()
1331 xgl_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1332 xgl_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1333 xgl_helper.types_dict = hfp.get_types_dict()
1334
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001335 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1336 subcmd.run()
1337
1338if __name__ == "__main__":
1339 main()