blob: afeb55b495ca241ea01aa74f60a7dc3728c017dc [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
Mike Stroyan7c2efaa2015-04-03 13:58:35 -060034def generate_get_proc_addr_check(name):
35 return " if (!%s || %s[0] != 'x' || %s[1] != 'g' || %s[2] != 'l')\n" \
36 " return NULL;" % ((name,) * 4)
37
Tobin Ehlis92dbf802014-10-22 09:06:33 -060038class Subcommand(object):
39 def __init__(self, argv):
40 self.argv = argv
Chia-I Wuec30dcb2015-01-01 08:46:31 +080041 self.headers = xgl.headers
42 self.protos = xgl.protos
Mike Stroyan3aecdb42015-04-03 17:13:23 -060043 self.no_addr = False
44 self.layer_name = ""
Tobin Ehlis92dbf802014-10-22 09:06:33 -060045
46 def run(self):
Tobin Ehlis92dbf802014-10-22 09:06:33 -060047 print(self.generate())
48
49 def generate(self):
50 copyright = self.generate_copyright()
51 header = self.generate_header()
52 body = self.generate_body()
53 footer = self.generate_footer()
54
55 contents = []
56 if copyright:
57 contents.append(copyright)
58 if header:
59 contents.append(header)
60 if body:
61 contents.append(body)
62 if footer:
63 contents.append(footer)
64
65 return "\n\n".join(contents)
66
67 def generate_copyright(self):
68 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
69
70/*
71 * XGL
72 *
73 * Copyright (C) 2014 LunarG, Inc.
74 *
75 * Permission is hereby granted, free of charge, to any person obtaining a
76 * copy of this software and associated documentation files (the "Software"),
77 * to deal in the Software without restriction, including without limitation
78 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
79 * and/or sell copies of the Software, and to permit persons to whom the
80 * Software is furnished to do so, subject to the following conditions:
81 *
82 * The above copyright notice and this permission notice shall be included
83 * in all copies or substantial portions of the Software.
84 *
85 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
86 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
87 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
88 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
89 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
90 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
91 * DEALINGS IN THE SOFTWARE.
92 */"""
93
94 def generate_header(self):
95 return "\n".join(["#include <" + h + ">" for h in self.headers])
96
97 def generate_body(self):
98 pass
99
100 def generate_footer(self):
101 pass
102
103 # Return set of printf '%' qualifier and input to that qualifier
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700104 def _get_printf_params(self, xgl_type, name, output_param, cpp=False):
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600105 # TODO : Need ENUM and STRUCT checks here
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700106 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 -0600107 return ("%s", "string_%s(%s)" % (xgl_type.strip('const ').strip('*'), name))
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600108 if "char*" == xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600109 return ("%s", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700110 if "uint64" in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600111 if '*' in xgl_type:
112 return ("%lu", "*%s" % name)
113 return ("%lu", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700114 if "size" in xgl_type:
Chia-I Wu54ed0792014-12-27 14:14:50 +0800115 if '*' in xgl_type:
116 return ("%zu", "*%s" % name)
117 return ("%zu", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700118 if "float" in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600119 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700120 if cpp:
121 return ("[%i, %i, %i, %i]", '"[" << %s[0] << "," << %s[1] << "," << %s[2] << "," << %s[3] << "]"' % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600122 return ("[%f, %f, %f, %f]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
123 return ("%f", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700124 if "bool" in xgl_type or 'xcb_randr_crtc_t' in xgl_type:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600125 return ("%u", name)
Tobin Ehlis2f3726c2015-01-15 17:51:52 -0700126 if True in [t in xgl_type for t in ["int", "FLAGS", "MASK", "xcb_window_t"]]:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600127 if '[' in xgl_type: # handle array, current hard-coded to 4 (TODO: Make this dynamic)
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700128 if cpp:
129 return ("[%i, %i, %i, %i]", "%s[0] << %s[1] << %s[2] << %s[3]" % (name, name, name, name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600130 return ("[%i, %i, %i, %i]", "%s[0], %s[1], %s[2], %s[3]" % (name, name, name, name))
131 if '*' in xgl_type:
Tobin Ehlis1336c8d2015-02-04 15:15:11 -0700132 if 'pUserData' == name:
133 return ("%i", "((pUserData == 0) ? 0 : *(pUserData))")
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700134 return ("%i", "*(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600135 return ("%i", name)
Tobin Ehlis0a1e06d2014-11-11 17:28:22 -0700136 # 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 -0700137 if "XGL_FORMAT" == xgl_type:
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700138 if cpp:
139 return ("%p", "&%s" % name)
140 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 -0700141 if output_param:
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600142 return ("%p", "(void*)*%s" % name)
Courtney Goeltzenleuchter9a1ded82015-04-03 16:35:32 -0600143 if xgl_helper.is_type(xgl_type, 'struct') and '*' not in xgl_type:
144 return ("%p", "(void*)(&%s)" % name)
Jon Ashburn1f7e2d72014-12-12 16:10:45 -0700145 return ("%p", "(void*)(%s)" % name)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600146
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700147 def _gen_layer_dbg_callback_register(self):
148 r_body = []
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -0600149 r_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgRegisterMsgCallback(XGL_INSTANCE instance, XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback, void* pUserData)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700150 r_body.append('{')
151 r_body.append(' // This layer intercepts callbacks')
152 r_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pNewDbgFuncNode = (XGL_LAYER_DBG_FUNCTION_NODE*)malloc(sizeof(XGL_LAYER_DBG_FUNCTION_NODE));')
153 r_body.append(' if (!pNewDbgFuncNode)')
154 r_body.append(' return XGL_ERROR_OUT_OF_MEMORY;')
155 r_body.append(' pNewDbgFuncNode->pfnMsgCallback = pfnMsgCallback;')
156 r_body.append(' pNewDbgFuncNode->pUserData = pUserData;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700157 r_body.append(' pNewDbgFuncNode->pNext = g_pDbgFunctionHead;')
158 r_body.append(' g_pDbgFunctionHead = pNewDbgFuncNode;')
Jon Ashburne4722392015-03-03 15:07:15 -0700159 r_body.append(' // force callbacks if DebugAction hasn\'t been set already other than initial value')
Ian Elliottc9473d92015-03-05 12:28:53 -0700160 r_body.append(' if (g_actionIsDefault) {')
Jon Ashburne4722392015-03-03 15:07:15 -0700161 r_body.append(' g_debugAction = XGL_DBG_LAYER_ACTION_CALLBACK;')
Ian Elliottc9473d92015-03-05 12:28:53 -0700162 r_body.append(' }')
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -0600163 r_body.append(' XGL_RESULT result = nextTable.DbgRegisterMsgCallback(instance, pfnMsgCallback, pUserData);')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700164 r_body.append(' return result;')
165 r_body.append('}')
166 return "\n".join(r_body)
167
168 def _gen_layer_dbg_callback_unregister(self):
169 ur_body = []
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -0600170 ur_body.append('XGL_LAYER_EXPORT XGL_RESULT XGLAPI xglDbgUnregisterMsgCallback(XGL_INSTANCE instance, XGL_DBG_MSG_CALLBACK_FUNCTION pfnMsgCallback)')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700171 ur_body.append('{')
Jon Ashburn21001f62015-02-16 08:26:50 -0700172 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pTrav = g_pDbgFunctionHead;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700173 ur_body.append(' XGL_LAYER_DBG_FUNCTION_NODE *pPrev = pTrav;')
174 ur_body.append(' while (pTrav) {')
175 ur_body.append(' if (pTrav->pfnMsgCallback == pfnMsgCallback) {')
176 ur_body.append(' pPrev->pNext = pTrav->pNext;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700177 ur_body.append(' if (g_pDbgFunctionHead == pTrav)')
178 ur_body.append(' g_pDbgFunctionHead = pTrav->pNext;')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700179 ur_body.append(' free(pTrav);')
180 ur_body.append(' break;')
181 ur_body.append(' }')
182 ur_body.append(' pPrev = pTrav;')
183 ur_body.append(' pTrav = pTrav->pNext;')
184 ur_body.append(' }')
Jon Ashburne4722392015-03-03 15:07:15 -0700185 ur_body.append(' if (g_pDbgFunctionHead == NULL)')
186 ur_body.append(' {')
187 ur_body.append(' if (g_actionIsDefault)')
188 ur_body.append(' g_debugAction = XGL_DBG_LAYER_ACTION_LOG_MSG;')
189 ur_body.append(' else')
190 ur_body.append(' g_debugAction &= ~XGL_DBG_LAYER_ACTION_CALLBACK;')
191 ur_body.append(' }')
Courtney Goeltzenleuchter9d36ef42015-04-13 14:10:06 -0600192 ur_body.append(' XGL_RESULT result = nextTable.DbgUnregisterMsgCallback(instance, pfnMsgCallback);')
Tobin Ehlisc54139f2014-12-17 08:01:59 -0700193 ur_body.append(' return result;')
194 ur_body.append('}')
195 return "\n".join(ur_body)
196
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600197 def _generate_dispatch_entrypoints(self, qual=""):
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600198 if qual:
199 qual += " "
200
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600201 funcs = []
202 intercepted = []
203 for proto in self.protos:
204 if proto.name != "GetProcAddr" and proto.name != "InitAndEnumerateGpus":
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600205 intercept = self.generate_intercept(proto, qual)
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600206 if intercept is None:
207 # fill in default intercept for certain entrypoints
208 if 'DbgRegisterMsgCallback' == proto.name:
209 intercept = self._gen_layer_dbg_callback_register()
210 if 'DbgUnregisterMsgCallback' == proto.name:
211 intercept = self._gen_layer_dbg_callback_unregister()
212 if intercept is not None:
213 funcs.append(intercept)
214 intercepted.append(proto)
215
216 prefix="xgl"
217 lookups = []
218 for proto in intercepted:
219 if 'WsiX11' in proto.name:
220 lookups.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
221 lookups.append("if (!strcmp(name, \"%s\"))" % proto.name)
222 lookups.append(" return (void*) %s%s;" %
223 (prefix, proto.name))
224 if 'WsiX11' in proto.name:
225 lookups.append("#endif")
226
227 # add customized layer_intercept_proc
228 body = []
229 body.append("static inline void* layer_intercept_proc(const char *name)")
230 body.append("{")
231 body.append(generate_get_proc_addr_check("name"))
232 body.append("")
233 body.append(" name += 3;")
234 body.append(" %s" % "\n ".join(lookups))
235 body.append("")
236 body.append(" return NULL;")
237 body.append("}")
238 funcs.append("\n".join(body))
239
240 return "\n\n".join(funcs)
241
242
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700243 def _generate_extensions(self):
244 exts = []
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600245 exts.append('uint64_t objTrackGetObjectCount(XGL_OBJECT_TYPE type)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700246 exts.append('{')
247 exts.append(' return (type == XGL_OBJECT_TYPE_ANY) ? numTotalObjs : numObjs[type];')
248 exts.append('}')
249 exts.append('')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600250 exts.append('XGL_RESULT objTrackGetObjects(XGL_OBJECT_TYPE type, uint64_t objCount, OBJTRACK_NODE* pObjNodeArray)')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700251 exts.append('{')
252 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 -0600253 exts.append(' bool32_t bAllObjs = (type == XGL_OBJECT_TYPE_ANY);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700254 exts.append(' // Check the count first thing')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600255 exts.append(' uint64_t maxObjCount = (bAllObjs) ? numTotalObjs : numObjs[type];')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700256 exts.append(' if (objCount > maxObjCount) {')
257 exts.append(' char str[1024];')
258 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));')
259 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_OBJCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
260 exts.append(' return XGL_ERROR_INVALID_VALUE;')
261 exts.append(' }')
262 exts.append(' objNode* pTrav = (bAllObjs) ? pGlobalHead : pObjectHead[type];')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600263 exts.append(' for (uint64_t i = 0; i < objCount; i++) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700264 exts.append(' if (!pTrav) {')
265 exts.append(' char str[1024];')
266 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);')
267 exts.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, 0, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
268 exts.append(' return XGL_ERROR_UNKNOWN;')
269 exts.append(' }')
270 exts.append(' memcpy(&pObjNodeArray[i], pTrav, sizeof(OBJTRACK_NODE));')
271 exts.append(' pTrav = (bAllObjs) ? pTrav->pNextGlobal : pTrav->pNextObj;')
272 exts.append(' }')
273 exts.append(' return XGL_SUCCESS;')
274 exts.append('}')
275
276 return "\n".join(exts)
277
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600278 def _generate_layer_gpa_function(self, extensions=[]):
279 func_body = []
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -0600280 func_body.append("XGL_LAYER_EXPORT void* XGLAPI xglGetProcAddr(XGL_PHYSICAL_GPU gpu, const char* funcName)\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600281 "{\n"
282 " XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) gpu;\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800283 " void* addr;\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600284 " if (gpu == NULL)\n"
285 " return NULL;\n"
286 " pCurObj = gpuw;\n"
Jon Ashburn21001f62015-02-16 08:26:50 -0700287 " loader_platform_thread_once(&tabOnce, init%s);\n\n"
Chia-I Wu706533e2015-01-05 13:18:57 +0800288 " addr = layer_intercept_proc(funcName);\n"
289 " if (addr)\n"
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600290 " return addr;" % self.layer_name)
Chia-I Wu706533e2015-01-05 13:18:57 +0800291
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700292 if 0 != len(extensions):
293 for ext_name in extensions:
Chia-I Wu7461fcf2014-12-27 15:16:07 +0800294 func_body.append(' else if (!strncmp("%s", funcName, sizeof("%s")))\n'
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700295 ' return %s;' % (ext_name, ext_name, ext_name))
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600296 func_body.append(" else {\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600297 " if (gpuw->pGPA == NULL)\n"
298 " return NULL;\n"
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700299 " return gpuw->pGPA((XGL_PHYSICAL_GPU)gpuw->nextObject, funcName);\n"
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600300 " }\n"
301 "}\n")
302 return "\n".join(func_body)
303
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600304 def _generate_layer_initialization(self, init_opts=False, prefix='xgl', lockname=None):
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800305 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700306 func_body.append('static void init%s(void)\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600307 '{\n' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700308 if init_opts:
309 func_body.append(' const char *strOpt;')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600310 func_body.append(' // initialize %s options' % self.layer_name)
311 func_body.append(' getLayerOptionEnum("%sReportLevel", (uint32_t *) &g_reportingLevel);' % self.layer_name)
312 func_body.append(' g_actionIsDefault = getLayerOptionEnum("%sDebugAction", (uint32_t *) &g_debugAction);' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700313 func_body.append('')
314 func_body.append(' if (g_debugAction & XGL_DBG_LAYER_ACTION_LOG_MSG)')
315 func_body.append(' {')
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600316 func_body.append(' strOpt = getLayerOption("%sLogFilename");' % self.layer_name)
Jon Ashburn21001f62015-02-16 08:26:50 -0700317 func_body.append(' if (strOpt)')
318 func_body.append(' {')
319 func_body.append(' g_logFile = fopen(strOpt, "w");')
320 func_body.append(' }')
321 func_body.append(' if (g_logFile == NULL)')
322 func_body.append(' g_logFile = stdout;')
323 func_body.append(' }')
324 func_body.append('')
325 func_body.append(' xglGetProcAddrType fpNextGPA;\n'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600326 ' fpNextGPA = pCurObj->pGPA;\n'
Jon Ashburn21001f62015-02-16 08:26:50 -0700327 ' assert(fpNextGPA);\n')
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600328
Chia-I Wu0f65b1e2015-01-04 23:11:43 +0800329 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);")
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700330 if lockname is not None:
331 func_body.append(" if (!%sLockInitialized)" % lockname)
332 func_body.append(" {")
333 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
334 func_body.append(" loader_platform_thread_create_mutex(&%sLock);" % lockname)
335 func_body.append(" %sLockInitialized = 1;" % lockname)
336 func_body.append(" }")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600337 func_body.append("}\n")
338 return "\n".join(func_body)
339
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600340 def _generate_layer_initialization_with_lock(self, prefix='xgl'):
Ian Elliott81ac44c2015-01-13 17:52:38 -0700341 func_body = ["#include \"xgl_dispatch_table_helper.h\""]
Jon Ashburn21001f62015-02-16 08:26:50 -0700342 func_body.append('static void init%s(void)\n'
Ian Elliott81ac44c2015-01-13 17:52:38 -0700343 '{\n'
344 ' xglGetProcAddrType fpNextGPA;\n'
345 ' fpNextGPA = pCurObj->pGPA;\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600346 ' assert(fpNextGPA);\n' % self.layer_name);
Ian Elliott81ac44c2015-01-13 17:52:38 -0700347
348 func_body.append(" layer_initialize_dispatch_table(&nextTable, fpNextGPA, (XGL_PHYSICAL_GPU) pCurObj->nextObject);\n")
349 func_body.append(" if (!printLockInitialized)")
350 func_body.append(" {")
351 func_body.append(" // TODO/TBD: Need to delete this mutex sometime. How???")
352 func_body.append(" loader_platform_thread_create_mutex(&printLock);")
353 func_body.append(" printLockInitialized = 1;")
354 func_body.append(" }")
355 func_body.append("}\n")
356 return "\n".join(func_body)
357
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600358class LayerFuncsSubcommand(Subcommand):
359 def generate_header(self):
360 return '#include <xglLayer.h>\n#include "loader.h"'
361
362 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600363 return self._generate_dispatch_entrypoints("static")
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600364
365class LayerDispatchSubcommand(Subcommand):
366 def generate_header(self):
367 return '#include "layer_wrappers.h"'
368
369 def generate_body(self):
Jon Ashburn21001f62015-02-16 08:26:50 -0700370 return self._generate_layer_initialization()
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600371
372class GenericLayerSubcommand(Subcommand):
373 def generate_header(self):
Jon Ashburn7a2da4f2015-02-17 11:03:12 -0700374 return '#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "loader_platform.h"\n#include "xglLayer.h"\n//The following is #included again to catch certain OS-specific functions being used:\n#include "loader_platform.h"\n\n#include "layers_config.h"\n#include "layers_msg.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;\n\nstatic LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);'
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600375
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600376 def generate_intercept(self, proto, qual):
Mike Stroyan723913e2015-04-03 14:39:16 -0600377 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' ]:
378 # use default version
379 return None
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600380 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
381 param0_name = proto.params[0].name
382 ret_val = ''
383 stmt = ''
384 funcs = []
385 if proto.ret != "void":
386 ret_val = "XGL_RESULT result = "
387 stmt = " return result;\n"
388 if 'WsiX11AssociateConnection' == proto.name:
389 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
390 if proto.name == "EnumerateLayers":
391 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
392 funcs.append('%s%s\n'
393 '{\n'
394 ' char str[1024];\n'
395 ' if (gpu != NULL) {\n'
396 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
397 ' sprintf(str, "At start of layered %s\\n");\n'
398 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
399 ' pCurObj = gpuw;\n'
400 ' loader_platform_thread_once(&tabOnce, init%s);\n'
401 ' %snextTable.%s;\n'
402 ' sprintf(str, "Completed layered %s\\n");\n'
403 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpu, 0, 0, (char *) "GENERIC", (char *) str);\n'
404 ' fflush(stdout);\n'
405 ' %s'
406 ' } else {\n'
407 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
408 ' return XGL_ERROR_INVALID_POINTER;\n'
409 ' // This layer compatible with all GPUs\n'
410 ' *pOutLayerCount = 1;\n'
411 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
412 ' return XGL_SUCCESS;\n'
413 ' }\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600414 '}' % (qual, decl, proto.params[0].name, proto.name, self.layer_name, ret_val, c_call, proto.name, stmt, self.layer_name))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600415 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
416 funcs.append('%s%s\n'
417 '{\n'
418 ' %snextTable.%s;\n'
419 '%s'
420 '}' % (qual, decl, ret_val, proto.c_call(), stmt))
421 else:
422 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
423 funcs.append('%s%s\n'
424 '{\n'
425 ' char str[1024];'
426 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
427 ' sprintf(str, "At start of layered %s\\n");\n'
428 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
429 ' pCurObj = gpuw;\n'
430 ' loader_platform_thread_once(&tabOnce, init%s);\n'
431 ' %snextTable.%s;\n'
432 ' sprintf(str, "Completed layered %s\\n");\n'
433 ' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, gpuw, 0, 0, (char *) "GENERIC", (char *) str);\n'
434 ' fflush(stdout);\n'
435 '%s'
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600436 '}' % (qual, decl, proto.params[0].name, proto.name, self.layer_name, ret_val, c_call, proto.name, stmt))
Mike Stroyan7c2efaa2015-04-03 13:58:35 -0600437 if 'WsiX11QueuePresent' == proto.name:
438 funcs.append("#endif")
439 return "\n\n".join(funcs)
440
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600441 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600442 self.layer_name = "Generic"
443 body = [self._generate_layer_initialization(True),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600444 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
445 self._generate_layer_gpa_function()]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600446
447 return "\n\n".join(body)
448
449class ApiDumpSubcommand(Subcommand):
450 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700451 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700452 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
453 header_txt.append('#include "loader_platform.h"')
454 header_txt.append('#include "xglLayer.h"\n#include "xgl_struct_string_helper.h"\n')
Ian Elliott20f06872015-02-12 17:08:34 -0700455 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
456 header_txt.append('#include "loader_platform.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700457 header_txt.append('static XGL_LAYER_DISPATCH_TABLE nextTable;')
458 header_txt.append('static XGL_BASE_LAYER_OBJECT *pCurObj;\n')
459 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
460 header_txt.append('static int printLockInitialized = 0;')
461 header_txt.append('static loader_platform_thread_mutex printLock;\n')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700462 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700463 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700464 header_txt.append('static uint32_t maxTID = 0;')
465 header_txt.append('// Map actual TID to an index value and return that index')
466 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
467 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700468 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700469 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
470 header_txt.append(' if (tid == tidMapping[i])')
471 header_txt.append(' return i;')
472 header_txt.append(' }')
473 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700474 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700475 header_txt.append(' tidMapping[maxTID++] = tid;')
476 header_txt.append(' assert(maxTID < MAX_TID);')
477 header_txt.append(' return retVal;')
478 header_txt.append('}')
479 return "\n".join(header_txt)
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600480
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600481 def generate_intercept(self, proto, qual):
482 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
483 param0_name = proto.params[0].name
484 ret_val = ''
485 stmt = ''
486 funcs = []
487 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
488 create_params = 0 # Num of params at end of function that are created and returned as output values
489 if 'WsiX11CreatePresentableImage' in proto.name:
490 create_params = -2
491 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
492 create_params = -1
493 if proto.ret != "void":
494 ret_val = "XGL_RESULT result = "
495 stmt = " return result;\n"
496 f_open = ''
497 f_close = ''
498 if "File" in self.layer_name:
499 file_mode = "a"
500 if 'CreateDevice' in proto.name:
501 file_mode = "w"
502 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
503 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
504 f_close = '\n fclose(pOutFile);\n loader_platform_thread_unlock_mutex(&printLock);'
505 else:
506 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n '
507 log_func = 'printf("t{%%u} xgl%s(' % proto.name
508 f_close = '\n loader_platform_thread_unlock_mutex(&printLock);'
509 print_vals = ', getTIDIndex()'
510 pindex = 0
511 prev_count_name = ''
512 for p in proto.params:
513 cp = False
514 if 0 != create_params:
515 # If this is any of the N last params of the func, treat as output
516 for y in range(-1, create_params-1, -1):
517 if p.name == proto.params[y].name:
518 cp = True
519 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp)
520 if self.no_addr and "%p" == pft:
521 (pft, pfi) = ("%s", '"addr"')
522 log_func += '%s = %s, ' % (p.name, pft)
523 print_vals += ', %s' % (pfi)
524 # Catch array inputs that are bound by a "Count" param
525 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
526 sp_param_dict[pindex] = prev_count_name
527 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
528 sp_param_dict[pindex] = '*pCount'
529 elif 'Wsi' not in proto.name and xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct'):
530 sp_param_dict[pindex] = 'index'
531 pindex += 1
532 if p.name.endswith('Count'):
533 if '*' in p.ty:
534 prev_count_name = "*%s" % p.name
535 else:
536 prev_count_name = p.name
537 else:
538 prev_count_name = ''
539 log_func = log_func.strip(', ')
540 if proto.ret != "void":
541 log_func += ') = %s\\n"'
542 print_vals += ', string_XGL_RESULT(result)'
543 else:
544 log_func += ')\\n"'
545 log_func = '%s%s);' % (log_func, print_vals)
546 if len(sp_param_dict) > 0:
547 i_decl = False
548 log_func += '\n char *pTmpStr = "";'
549 for sp_index in sorted(sp_param_dict):
550 # TODO : Clean this if/else block up, too much duplicated code
551 if 'index' == sp_param_dict[sp_index]:
552 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
553 var_name = proto.params[sp_index].name
554 if proto.params[sp_index].name != 'color':
555 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
556 else:
557 var_name = "&%s" % proto.params[sp_index].name
558 log_func += '\n pTmpStr = %s(%s, " ");' % (cis_print_func, var_name)
559 if "File" in self.layer_name:
560 if self.no_addr:
561 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (var_name)
562 else:
563 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (var_name, var_name)
564 else:
565 if self.no_addr:
566 log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
567 else:
568 log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, var_name)
569 log_func += '\n fflush(stdout);'
570 log_func += '\n free(pTmpStr);'
571 if proto.params[sp_index].name != 'color':
572 log_func += '\n }'
573 else: # should have a count value stored to iterate over array
574 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
575 cis_print_func = 'pTmpStr = xgl_print_%s(&%s[i], " ");' % (proto.params[sp_index].ty.strip('const ').strip('*').lower(), proto.params[sp_index].name)
576 else:
577 cis_print_func = 'pTmpStr = (char*)malloc(32);\n sprintf(pTmpStr, " %%p", %s[i]);' % proto.params[sp_index].name
578 if not i_decl:
579 log_func += '\n uint32_t i;'
580 i_decl = True
581 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
582 log_func += '\n %s' % (cis_print_func)
583 if "File" in self.layer_name:
584 if self.no_addr:
585 log_func += '\n fprintf(pOutFile, " %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
586 else:
587 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)
588 else:
589 if self.no_addr:
590 log_func += '\n printf(" %s[%%i] (addr)\\n%%s\\n", i, pTmpStr);' % (proto.params[sp_index].name)
591 else:
592 log_func += '\n printf(" %s[%%i] (%%p)\\n%%s\\n", i, (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
593 log_func += '\n fflush(stdout);'
594 log_func += '\n free(pTmpStr);\n }'
595 if 'WsiX11AssociateConnection' == proto.name:
596 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
597 if proto.name == "EnumerateLayers":
598 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
599 funcs.append('%s%s\n'
600 '{\n'
601 ' if (gpu != NULL) {\n'
602 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
603 ' pCurObj = gpuw;\n'
604 ' loader_platform_thread_once(&tabOnce, init%s);\n'
605 ' %snextTable.%s;\n'
606 ' %s %s %s\n'
607 ' %s'
608 ' } else {\n'
609 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
610 ' return XGL_ERROR_INVALID_POINTER;\n'
611 ' // This layer compatible with all GPUs\n'
612 ' *pOutLayerCount = 1;\n'
613 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
614 ' return XGL_SUCCESS;\n'
615 ' }\n'
616 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call,f_open, log_func, f_close, stmt, self.layer_name))
617 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
618 funcs.append('%s%s\n'
619 '{\n'
620 ' %snextTable.%s;\n'
621 ' %s%s%s\n'
622 '%s'
623 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
624 else:
625 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
626 funcs.append('%s%s\n'
627 '{\n'
628 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
629 ' pCurObj = gpuw;\n'
630 ' loader_platform_thread_once(&tabOnce, init%s);\n'
631 ' %snextTable.%s;\n'
632 ' %s%s%s\n'
633 '%s'
634 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call, f_open, log_func, f_close, stmt))
635 if 'WsiX11QueuePresent' == proto.name:
636 funcs.append("#endif")
637 return "\n\n".join(funcs)
638
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600639 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600640 self.layer_name = "APIDump"
641 body = [self._generate_layer_initialization_with_lock(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600642 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600643 self._generate_layer_gpa_function()]
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600644
645 return "\n\n".join(body)
646
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700647class ApiDumpCppSubcommand(Subcommand):
648 def generate_header(self):
649 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700650 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
651 header_txt.append('#include "loader_platform.h"')
652 header_txt.append('#include "xglLayer.h"\n#include "xgl_struct_string_helper_cpp.h"\n')
Ian Elliott20f06872015-02-12 17:08:34 -0700653 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
654 header_txt.append('#include "loader_platform.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700655 header_txt.append('static XGL_LAYER_DISPATCH_TABLE nextTable;')
656 header_txt.append('static XGL_BASE_LAYER_OBJECT *pCurObj;\n')
657 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
658 header_txt.append('static int printLockInitialized = 0;')
659 header_txt.append('static loader_platform_thread_mutex printLock;\n')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700660 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700661 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700662 header_txt.append('static uint32_t maxTID = 0;')
663 header_txt.append('// Map actual TID to an index value and return that index')
664 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
665 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700666 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700667 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
668 header_txt.append(' if (tid == tidMapping[i])')
669 header_txt.append(' return i;')
670 header_txt.append(' }')
671 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700672 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700673 header_txt.append(' tidMapping[maxTID++] = tid;')
674 header_txt.append(' assert(maxTID < MAX_TID);')
675 header_txt.append(' return retVal;')
676 header_txt.append('}')
677 return "\n".join(header_txt)
678
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600679 def generate_intercept(self, proto, qual):
680 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
681 param0_name = proto.params[0].name
682 ret_val = ''
683 stmt = ''
684 funcs = []
685 sp_param_dict = {} # Store 'index' for struct param to print, or an name of binding "Count" param for array to print
686 create_params = 0 # Num of params at end of function that are created and returned as output values
687 if 'WsiX11CreatePresentableImage' in proto.name or 'AllocDescriptorSets' in proto.name:
688 create_params = -2
689 elif 'Create' in proto.name or 'Alloc' in proto.name or 'MapMemory' in proto.name:
690 create_params = -1
691 if proto.ret != "void":
692 ret_val = "XGL_RESULT result = "
693 stmt = " return result;\n"
694 f_open = ''
695 f_close = ''
696 if "File" in self.layer_name:
697 file_mode = "a"
698 if 'CreateDevice' in proto.name:
699 file_mode = "w"
700 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n pOutFile = fopen(outFileName, "%s");\n ' % (file_mode)
701 log_func = 'fprintf(pOutFile, "t{%%u} xgl%s(' % proto.name
702 f_close = '\n fclose(pOutFile);\n loader_platform_thread_unlock_mutex(&printLock);'
703 else:
704 f_open = 'loader_platform_thread_lock_mutex(&printLock);\n '
705 log_func = 'cout << "t{" << getTIDIndex() << "} xgl%s(' % proto.name
706 f_close = '\n loader_platform_thread_unlock_mutex(&printLock);'
707 pindex = 0
708 prev_count_name = ''
709 for p in proto.params:
710 cp = False
711 if 0 != create_params:
712 # If this is any of the N last params of the func, treat as output
713 for y in range(-1, create_params-1, -1):
714 if p.name == proto.params[y].name:
715 cp = True
716 (pft, pfi) = self._get_printf_params(p.ty, p.name, cp, cpp=True)
717 if self.no_addr and "%p" == pft:
718 (pft, pfi) = ("%s", '"addr"')
719 log_func += '%s = " << %s << ", ' % (p.name, pfi)
720 #print_vals += ', %s' % (pfi)
721 if prev_count_name != '' and (prev_count_name.strip('Count')[1:] in p.name or 'slotCount' == prev_count_name):
722 sp_param_dict[pindex] = prev_count_name
723 elif 'pDescriptorSets' == p.name and proto.params[-1].name == 'pCount':
724 sp_param_dict[pindex] = '*pCount'
725 elif 'Wsi' not in proto.name and xgl_helper.is_type(p.ty.strip('*').strip('const '), 'struct'):
726 sp_param_dict[pindex] = 'index'
727 pindex += 1
728 if p.name.endswith('Count'):
729 if '*' in p.ty:
730 prev_count_name = "*%s" % p.name
731 else:
732 prev_count_name = p.name
733 else:
734 prev_count_name = ''
735 log_func = log_func.strip(', ')
736 if proto.ret != "void":
737 log_func += ') = " << string_XGL_RESULT((XGL_RESULT)result) << endl'
738 #print_vals += ', string_XGL_RESULT_CODE(result)'
739 else:
740 log_func += ')\\n"'
741 log_func += ';'
742 if len(sp_param_dict) > 0:
743 i_decl = False
744 log_func += '\n string tmp_str;'
745 for sp_index in sp_param_dict:
746 if 'index' == sp_param_dict[sp_index]:
747 cis_print_func = 'xgl_print_%s' % (proto.params[sp_index].ty.strip('const ').strip('*').lower())
748 var_name = proto.params[sp_index].name
749 if proto.params[sp_index].name != 'color':
750 log_func += '\n if (%s) {' % (proto.params[sp_index].name)
751 else:
752 var_name = '&%s' % (proto.params[sp_index].name)
753 log_func += '\n tmp_str = %s(%s, " ");' % (cis_print_func, var_name)
754 if "File" in self.layer_name:
755 if self.no_addr:
756 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
757 else:
758 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, var_name)
759 else:
760 if self.no_addr:
761 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
762 log_func += '\n cout << " %s (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
763 else:
764 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
765 log_func += '\n cout << " %s (" << %s << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, var_name)
766 #log_func += '\n fflush(stdout);'
767 if proto.params[sp_index].name != 'color':
768 log_func += '\n }'
769 else: # We have a count value stored to iterate over an array
770 print_cast = ''
771 print_func = ''
772 if xgl_helper.is_type(proto.params[sp_index].ty.strip('*').strip('const '), 'struct'):
773 print_cast = '&'
774 print_func = 'xgl_print_%s' % proto.params[sp_index].ty.strip('const ').strip('*').lower()
775 #cis_print_func = 'tmp_str = xgl_print_%s(&%s[i], " ");' % (proto.params[sp_index].ty.strip('const ').strip('*').lower(), proto.params[sp_index].name)
776# TODO : Need to display this address as a string
777 else:
778 print_cast = '(void*)'
779 print_func = 'string_convert_helper'
780 #cis_print_func = 'tmp_str = string_convert_helper((void*)%s[i], " ");' % proto.params[sp_index].name
781 cis_print_func = 'tmp_str = %s(%s%s[i], " ");' % (print_func, print_cast, proto.params[sp_index].name)
782# else:
783# cis_print_func = ''
784 if not i_decl:
785 log_func += '\n uint32_t i;'
786 i_decl = True
787 log_func += '\n for (i = 0; i < %s; i++) {' % (sp_param_dict[sp_index])
788 log_func += '\n %s' % (cis_print_func)
789 if "File" in self.layer_name:
790 if self.no_addr:
791 log_func += '\n fprintf(pOutFile, " %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
792 else:
793 log_func += '\n fprintf(pOutFile, " %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
794 else:
795 if self.no_addr:
796 #log_func += '\n printf(" %s (addr)\\n%%s\\n", pTmpStr);' % (proto.params[sp_index].name)
797 log_func += '\n cout << " %s[" << (uint32_t)i << "] (addr)" << endl << tmp_str << endl;' % (proto.params[sp_index].name)
798 else:
799 #log_func += '\n printf(" %s (%%p)\\n%%s\\n", (void*)%s, pTmpStr);' % (proto.params[sp_index].name, proto.params[sp_index].name)
800 #log_func += '\n cout << " %s[" << (uint32_t)i << "] (" << %s[i] << ")" << endl << tmp_str << endl;' % (proto.params[sp_index].name, proto.params[sp_index].name)
801 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)
802 #log_func += '\n fflush(stdout);'
803 log_func += '\n }'
804 if 'WsiX11AssociateConnection' == proto.name:
805 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
806 if proto.name == "EnumerateLayers":
807 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
808 funcs.append('%s%s\n'
809 '{\n'
810 ' if (gpu != NULL) {\n'
811 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
812 ' pCurObj = gpuw;\n'
813 ' loader_platform_thread_once(&tabOnce, init%s);\n'
814 ' %snextTable.%s;\n'
815 ' %s %s %s\n'
816 ' %s'
817 ' } else {\n'
818 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
819 ' return XGL_ERROR_INVALID_POINTER;\n'
820 ' // This layer compatible with all GPUs\n'
821 ' *pOutLayerCount = 1;\n'
822 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
823 ' return XGL_SUCCESS;\n'
824 ' }\n'
825 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call,f_open, log_func, f_close, stmt, self.layer_name))
826 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
827 funcs.append('%s%s\n'
828 '{\n'
829 ' %snextTable.%s;\n'
830 ' %s%s%s\n'
831 '%s'
832 '}' % (qual, decl, ret_val, proto.c_call(), f_open, log_func, f_close, stmt))
833 else:
834 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
835 funcs.append('%s%s\n'
836 '{\n'
837 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
838 ' pCurObj = gpuw;\n'
839 ' loader_platform_thread_once(&tabOnce, init%s);\n'
840 ' %snextTable.%s;\n'
841 ' %s%s%s\n'
842 '%s'
843 '}' % (qual, decl, proto.params[0].name, self.layer_name, ret_val, c_call, f_open, log_func, f_close, stmt))
844 if 'WsiX11QueuePresent' == proto.name:
845 funcs.append("#endif")
846 return "\n\n".join(funcs)
847
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700848 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600849 self.layer_name = "APIDumpCpp"
850 body = [self._generate_layer_initialization_with_lock(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600851 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600852 self._generate_layer_gpa_function()]
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700853
854 return "\n\n".join(body)
855
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600856# subclass from ApiDumpSubcommand instead of Subcommand
857class ApiDumpFileSubcommand(ApiDumpSubcommand):
Tobin Ehlis574b0142014-11-12 13:11:15 -0700858 def generate_header(self):
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700859 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700860 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
861 header_txt.append('#include "loader_platform.h"')
862 header_txt.append('#include "xglLayer.h"\n#include "xgl_struct_string_helper.h"\n')
Ian Elliott20f06872015-02-12 17:08:34 -0700863 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
864 header_txt.append('#include "loader_platform.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700865 header_txt.append('static XGL_LAYER_DISPATCH_TABLE nextTable;')
866 header_txt.append('static XGL_BASE_LAYER_OBJECT *pCurObj;\n')
867 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
868 header_txt.append('static int printLockInitialized = 0;')
869 header_txt.append('static loader_platform_thread_mutex printLock;\n')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700870 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700871 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700872 header_txt.append('static uint32_t maxTID = 0;')
873 header_txt.append('// Map actual TID to an index value and return that index')
874 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
875 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700876 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700877 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
878 header_txt.append(' if (tid == tidMapping[i])')
879 header_txt.append(' return i;')
880 header_txt.append(' }')
881 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700882 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700883 header_txt.append(' tidMapping[maxTID++] = tid;')
884 header_txt.append(' assert(maxTID < MAX_TID);')
885 header_txt.append(' return retVal;')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700886 header_txt.append('}\n')
887 header_txt.append('static FILE* pOutFile;\nstatic char* outFileName = "xgl_apidump.txt";')
Tobin Ehlis0c68c9c2014-11-24 15:46:55 -0700888 return "\n".join(header_txt)
Tobin Ehlis574b0142014-11-12 13:11:15 -0700889
890 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600891 self.layer_name = "APIDumpFile"
892 body = [self._generate_layer_initialization_with_lock(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600893 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600894 self._generate_layer_gpa_function()]
Tobin Ehlis574b0142014-11-12 13:11:15 -0700895
896 return "\n\n".join(body)
897
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600898# subclass from ApiDumpSubcommand instead of Subcommand
899class ApiDumpNoAddrSubcommand(ApiDumpSubcommand):
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700900 def generate_header(self):
901 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700902 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
903 header_txt.append('#include "loader_platform.h"')
904 header_txt.append('#include "xglLayer.h"\n#include "xgl_struct_string_helper_no_addr.h"\n')
Ian Elliott20f06872015-02-12 17:08:34 -0700905 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
906 header_txt.append('#include "loader_platform.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700907 header_txt.append('static XGL_LAYER_DISPATCH_TABLE nextTable;')
908 header_txt.append('static XGL_BASE_LAYER_OBJECT *pCurObj;\n')
909 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
910 header_txt.append('static int printLockInitialized = 0;')
911 header_txt.append('static loader_platform_thread_mutex printLock;\n')
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700912 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700913 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700914 header_txt.append('static uint32_t maxTID = 0;')
915 header_txt.append('// Map actual TID to an index value and return that index')
916 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
917 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700918 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700919 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
920 header_txt.append(' if (tid == tidMapping[i])')
921 header_txt.append(' return i;')
922 header_txt.append(' }')
923 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700924 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700925 header_txt.append(' tidMapping[maxTID++] = tid;')
926 header_txt.append(' assert(maxTID < MAX_TID);')
927 header_txt.append(' return retVal;')
928 header_txt.append('}')
929 return "\n".join(header_txt)
930
931 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600932 self.layer_name = "APIDumpNoAddr"
933 self.no_addr = True
934 body = [self._generate_layer_initialization_with_lock(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600935 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600936 self._generate_layer_gpa_function()]
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -0700937
938 return "\n\n".join(body)
939
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600940# subclass from ApiDumpCppSubcommand instead of Subcommand
941class ApiDumpNoAddrCppSubcommand(ApiDumpCppSubcommand):
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700942 def generate_header(self):
943 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700944 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>')
945 header_txt.append('#include "loader_platform.h"')
946 header_txt.append('#include "xglLayer.h"\n#include "xgl_struct_string_helper_no_addr_cpp.h"\n')
Ian Elliott20f06872015-02-12 17:08:34 -0700947 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
948 header_txt.append('#include "loader_platform.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700949 header_txt.append('static XGL_LAYER_DISPATCH_TABLE nextTable;')
950 header_txt.append('static XGL_BASE_LAYER_OBJECT *pCurObj;\n')
951 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
952 header_txt.append('static int printLockInitialized = 0;')
953 header_txt.append('static loader_platform_thread_mutex printLock;\n')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700954 header_txt.append('#define MAX_TID 513')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700955 header_txt.append('static loader_platform_thread_id tidMapping[MAX_TID] = {0};')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700956 header_txt.append('static uint32_t maxTID = 0;')
957 header_txt.append('// Map actual TID to an index value and return that index')
958 header_txt.append('// This keeps TIDs in range from 0-MAX_TID and simplifies compares between runs')
959 header_txt.append('static uint32_t getTIDIndex() {')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700960 header_txt.append(' loader_platform_thread_id tid = loader_platform_get_thread_id();')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700961 header_txt.append(' for (uint32_t i = 0; i < maxTID; i++) {')
962 header_txt.append(' if (tid == tidMapping[i])')
963 header_txt.append(' return i;')
964 header_txt.append(' }')
965 header_txt.append(" // Don't yet have mapping, set it and return newly set index")
Ian Elliott81ac44c2015-01-13 17:52:38 -0700966 header_txt.append(' uint32_t retVal = (uint32_t) maxTID;')
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700967 header_txt.append(' tidMapping[maxTID++] = tid;')
968 header_txt.append(' assert(maxTID < MAX_TID);')
969 header_txt.append(' return retVal;')
970 header_txt.append('}')
971 return "\n".join(header_txt)
972
973 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600974 self.layer_name = "APIDumpNoAddrCpp"
975 self.no_addr = True
976 body = [self._generate_layer_initialization_with_lock(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -0600977 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Mike Stroyan3aecdb42015-04-03 17:13:23 -0600978 self._generate_layer_gpa_function()]
Tobin Ehlis434db7c2015-01-10 12:42:41 -0700979
980 return "\n\n".join(body)
981
Tobin Ehlis92dbf802014-10-22 09:06:33 -0600982class ObjectTrackerSubcommand(Subcommand):
983 def generate_header(self):
984 header_txt = []
Ian Elliott81ac44c2015-01-13 17:52:38 -0700985 header_txt.append('#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "loader_platform.h"')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700986 header_txt.append('#include "object_track.h"\n\nstatic XGL_LAYER_DISPATCH_TABLE nextTable;\nstatic XGL_BASE_LAYER_OBJECT *pCurObj;')
Ian Elliott20f06872015-02-12 17:08:34 -0700987 header_txt.append('// The following is #included again to catch certain OS-specific functions being used:')
988 header_txt.append('#include "loader_platform.h"')
Jon Ashburn7a2da4f2015-02-17 11:03:12 -0700989 header_txt.append('#include "layers_config.h"')
Jon Ashburn21001f62015-02-16 08:26:50 -0700990 header_txt.append('#include "layers_msg.h"')
Ian Elliott81ac44c2015-01-13 17:52:38 -0700991 header_txt.append('static LOADER_PLATFORM_THREAD_ONCE_DECLARATION(tabOnce);')
992 header_txt.append('static long long unsigned int object_track_index = 0;')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -0700993 header_txt.append('static int objLockInitialized = 0;')
994 header_txt.append('static loader_platform_thread_mutex objLock;')
Jon Ashburn21001f62015-02-16 08:26:50 -0700995 header_txt.append('')
Tobin Ehlis3c26a542014-11-18 11:28:33 -0700996 header_txt.append('// We maintain a "Global" list which links every object and a')
997 header_txt.append('// per-Object list which just links objects of a given type')
998 header_txt.append('// The object node has both pointers so the actual nodes are shared between the two lists')
999 header_txt.append('typedef struct _objNode {')
1000 header_txt.append(' OBJTRACK_NODE obj;')
1001 header_txt.append(' struct _objNode *pNextObj;')
1002 header_txt.append(' struct _objNode *pNextGlobal;')
1003 header_txt.append('} objNode;')
1004 header_txt.append('static objNode *pObjectHead[XGL_NUM_OBJECT_TYPE] = {0};')
1005 header_txt.append('static objNode *pGlobalHead = NULL;')
1006 header_txt.append('static uint64_t numObjs[XGL_NUM_OBJECT_TYPE] = {0};')
1007 header_txt.append('static uint64_t numTotalObjs = 0;')
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -06001008 header_txt.append('static uint32_t maxMemRefsPerSubmission = 0;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001009 header_txt.append('// Debug function to print global list and each individual object list')
1010 header_txt.append('static void ll_print_lists()')
1011 header_txt.append('{')
1012 header_txt.append(' objNode* pTrav = pGlobalHead;')
1013 header_txt.append(' printf("=====GLOBAL OBJECT LIST (%lu total objs):\\n", numTotalObjs);')
1014 header_txt.append(' while (pTrav) {')
1015 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);')
1016 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1017 header_txt.append(' }')
1018 header_txt.append(' for (uint32_t i = 0; i < XGL_NUM_OBJECT_TYPE; i++) {')
1019 header_txt.append(' pTrav = pObjectHead[i];')
1020 header_txt.append(' if (pTrav) {')
1021 header_txt.append(' printf("=====%s OBJECT LIST (%lu objs):\\n", string_XGL_OBJECT_TYPE(pTrav->obj.objType), numObjs[i]);')
1022 header_txt.append(' while (pTrav) {')
1023 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);')
1024 header_txt.append(' pTrav = pTrav->pNextObj;')
1025 header_txt.append(' }')
1026 header_txt.append(' }')
1027 header_txt.append(' }')
1028 header_txt.append('}')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001029 header_txt.append('static void ll_insert_obj(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001030 header_txt.append(' char str[1024];')
1031 header_txt.append(' sprintf(str, "OBJ[%llu] : CREATE %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1032 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1033 header_txt.append(' objNode* pNewObjNode = (objNode*)malloc(sizeof(objNode));')
1034 header_txt.append(' pNewObjNode->obj.pObj = pObj;')
1035 header_txt.append(' pNewObjNode->obj.objType = objType;')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001036 header_txt.append(' pNewObjNode->obj.status = OBJSTATUS_NONE;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001037 header_txt.append(' pNewObjNode->obj.numUses = 0;')
1038 header_txt.append(' // insert at front of global list')
1039 header_txt.append(' pNewObjNode->pNextGlobal = pGlobalHead;')
1040 header_txt.append(' pGlobalHead = pNewObjNode;')
1041 header_txt.append(' // insert at front of object list')
1042 header_txt.append(' pNewObjNode->pNextObj = pObjectHead[objType];')
1043 header_txt.append(' pObjectHead[objType] = pNewObjNode;')
1044 header_txt.append(' // increment obj counts')
1045 header_txt.append(' numObjs[objType]++;')
1046 header_txt.append(' numTotalObjs++;')
1047 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 +08001048 header_txt.append(' if (0) ll_print_lists();')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001049 header_txt.append('}')
1050 header_txt.append('// Traverse global list and return type for given object')
1051 header_txt.append('static XGL_OBJECT_TYPE ll_get_obj_type(XGL_OBJECT object) {')
1052 header_txt.append(' objNode *pTrav = pGlobalHead;')
1053 header_txt.append(' while (pTrav) {')
1054 header_txt.append(' if (pTrav->obj.pObj == object)')
1055 header_txt.append(' return pTrav->obj.objType;')
1056 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1057 header_txt.append(' }')
1058 header_txt.append(' char str[1024];')
1059 header_txt.append(' sprintf(str, "Attempting look-up on obj %p but it is NOT in the global list!", (void*)object);')
1060 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, object, 0, OBJTRACK_MISSING_OBJECT, "OBJTRACK", str);')
1061 header_txt.append(' return XGL_OBJECT_TYPE_UNKNOWN;')
1062 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001063 header_txt.append('#if 0')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001064 header_txt.append('static uint64_t ll_get_obj_uses(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001065 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1066 header_txt.append(' while (pTrav) {')
1067 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1068 header_txt.append(' return pTrav->obj.numUses;')
1069 header_txt.append(' }')
1070 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001071 header_txt.append(' }')
1072 header_txt.append(' return 0;')
1073 header_txt.append('}')
Chia-I Wudf142a32014-12-16 11:02:06 +08001074 header_txt.append('#endif')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001075 header_txt.append('static void ll_increment_use_count(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001076 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001077 header_txt.append(' while (pTrav) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001078 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1079 header_txt.append(' pTrav->obj.numUses++;')
1080 header_txt.append(' char str[1024];')
1081 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);')
1082 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
1083 header_txt.append(' return;')
1084 header_txt.append(' }')
1085 header_txt.append(' pTrav = pTrav->pNextObj;')
1086 header_txt.append(' }')
1087 header_txt.append(' // If we do not find obj, insert it and then increment count')
1088 header_txt.append(' char str[1024];')
1089 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));')
1090 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1091 header_txt.append('')
1092 header_txt.append(' ll_insert_obj(pObj, objType);')
1093 header_txt.append(' ll_increment_use_count(pObj, objType);')
1094 header_txt.append('}')
1095 header_txt.append('// We usually do not know Obj type when we destroy it so have to fetch')
1096 header_txt.append('// Type from global list w/ ll_destroy_obj()')
1097 header_txt.append('// and then do the full removal from both lists w/ ll_remove_obj_type()')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001098 header_txt.append('static void ll_remove_obj_type(void* pObj, XGL_OBJECT_TYPE objType) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001099 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1100 header_txt.append(' objNode *pPrev = pObjectHead[objType];')
1101 header_txt.append(' while (pTrav) {')
1102 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1103 header_txt.append(' pPrev->pNextObj = pTrav->pNextObj;')
1104 header_txt.append(' // update HEAD of Obj list as needed')
1105 header_txt.append(' if (pObjectHead[objType] == pTrav)')
1106 header_txt.append(' pObjectHead[objType] = pTrav->pNextObj;')
1107 header_txt.append(' assert(numObjs[objType] > 0);')
1108 header_txt.append(' numObjs[objType]--;')
1109 header_txt.append(' char str[1024];')
1110 header_txt.append(' sprintf(str, "OBJ[%llu] : DESTROY %s object %p", object_track_index++, string_XGL_OBJECT_TYPE(objType), (void*)pObj);')
1111 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 -06001112 header_txt.append(' return;')
1113 header_txt.append(' }')
1114 header_txt.append(' pPrev = pTrav;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001115 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001116 header_txt.append(' }')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001117 header_txt.append(' char str[1024];')
1118 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));')
1119 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_INTERNAL_ERROR, "OBJTRACK", str);')
1120 header_txt.append('}')
1121 header_txt.append('// Parse global list to find obj type, then remove obj from obj type list, finally')
1122 header_txt.append('// remove obj from global list')
Mark Lobodzinskie2d07a52015-01-29 08:55:56 -06001123 header_txt.append('static void ll_destroy_obj(void* pObj) {')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001124 header_txt.append(' objNode *pTrav = pGlobalHead;')
1125 header_txt.append(' objNode *pPrev = pGlobalHead;')
1126 header_txt.append(' while (pTrav) {')
1127 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1128 header_txt.append(' ll_remove_obj_type(pObj, pTrav->obj.objType);')
1129 header_txt.append(' pPrev->pNextGlobal = pTrav->pNextGlobal;')
1130 header_txt.append(' // update HEAD of global list if needed')
1131 header_txt.append(' if (pGlobalHead == pTrav)')
1132 header_txt.append(' pGlobalHead = pTrav->pNextGlobal;')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001133 header_txt.append(' assert(numTotalObjs > 0);')
1134 header_txt.append(' numTotalObjs--;')
1135 header_txt.append(' char str[1024];')
Courtney Goeltzenleuchterf6adc302015-03-26 16:16:16 -06001136 header_txt.append(' sprintf(str, "OBJ_STAT Removed %s obj %p that was used %lu times (%lu total objs remain & %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));')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001137 header_txt.append(' layerCbMsg(XGL_DBG_MSG_UNKNOWN, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_NONE, "OBJTRACK", str);')
Tobin Ehlis84a8a9b2015-02-23 14:09:16 -07001138 header_txt.append(' free(pTrav);')
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001139 header_txt.append(' return;')
1140 header_txt.append(' }')
1141 header_txt.append(' pPrev = pTrav;')
1142 header_txt.append(' pTrav = pTrav->pNextGlobal;')
1143 header_txt.append(' }')
1144 header_txt.append(' char str[1024];')
1145 header_txt.append(' sprintf(str, "Unable to remove obj %p. Was it created? Has it already been destroyed?", pObj);')
1146 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 -06001147 header_txt.append('}')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001148 header_txt.append('// Set selected flag state for an object node')
1149 header_txt.append('static void set_status(void* pObj, XGL_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -06001150 header_txt.append(' if (pObj != NULL) {')
1151 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1152 header_txt.append(' while (pTrav) {')
1153 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1154 header_txt.append(' pTrav->obj.status |= status_flag;')
1155 header_txt.append(' return;')
1156 header_txt.append(' }')
1157 header_txt.append(' pTrav = pTrav->pNextObj;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001158 header_txt.append(' }')
Mark Lobodzinskid11fcca2015-02-09 10:16:20 -06001159 header_txt.append(' // If we do not find it print an error')
1160 header_txt.append(' char str[1024];')
1161 header_txt.append(' sprintf(str, "Unable to set status for non-existent object %p of %s type", pObj, string_XGL_OBJECT_TYPE(objType));')
1162 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1163 header_txt.append(' }');
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001164 header_txt.append('}')
1165 header_txt.append('')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001166 header_txt.append('// Track selected state for an object node')
1167 header_txt.append('static void track_object_status(void* pObj, XGL_STATE_BIND_POINT stateBindPoint) {')
1168 header_txt.append(' objNode *pTrav = pObjectHead[XGL_OBJECT_TYPE_CMD_BUFFER];')
1169 header_txt.append('')
1170 header_txt.append(' while (pTrav) {')
1171 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
1172 header_txt.append(' if (stateBindPoint == XGL_STATE_BIND_VIEWPORT) {')
1173 header_txt.append(' pTrav->obj.status |= OBJSTATUS_VIEWPORT_BOUND;')
1174 header_txt.append(' } else if (stateBindPoint == XGL_STATE_BIND_RASTER) {')
1175 header_txt.append(' pTrav->obj.status |= OBJSTATUS_RASTER_BOUND;')
1176 header_txt.append(' } else if (stateBindPoint == XGL_STATE_BIND_COLOR_BLEND) {')
1177 header_txt.append(' pTrav->obj.status |= OBJSTATUS_COLOR_BLEND_BOUND;')
1178 header_txt.append(' } else if (stateBindPoint == XGL_STATE_BIND_DEPTH_STENCIL) {')
1179 header_txt.append(' pTrav->obj.status |= OBJSTATUS_DEPTH_STENCIL_BOUND;')
1180 header_txt.append(' }')
1181 header_txt.append(' return;')
1182 header_txt.append(' }')
1183 header_txt.append(' pTrav = pTrav->pNextObj;')
1184 header_txt.append(' }')
1185 header_txt.append(' // If we do not find it print an error')
1186 header_txt.append(' char str[1024];')
1187 header_txt.append(' sprintf(str, "Unable to track status for non-existent Command Buffer object %p", pObj);')
1188 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1189 header_txt.append('}')
1190 header_txt.append('')
1191 header_txt.append('// Reset selected flag state for an object node')
1192 header_txt.append('static void reset_status(void* pObj, XGL_OBJECT_TYPE objType, OBJECT_STATUS status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001193 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1194 header_txt.append(' while (pTrav) {')
1195 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001196 header_txt.append(' pTrav->obj.status &= ~status_flag;')
1197 header_txt.append(' return;')
1198 header_txt.append(' }')
1199 header_txt.append(' pTrav = pTrav->pNextObj;')
1200 header_txt.append(' }')
1201 header_txt.append(' // If we do not find it print an error')
1202 header_txt.append(' char str[1024];')
1203 header_txt.append(' sprintf(str, "Unable to reset status for non-existent object %p of %s type", pObj, string_XGL_OBJECT_TYPE(objType));')
1204 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1205 header_txt.append('}')
1206 header_txt.append('')
1207 header_txt.append('// Check object status for selected flag state')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001208 header_txt.append('static bool32_t validate_status(void* pObj, XGL_OBJECT_TYPE objType, OBJECT_STATUS status_mask, OBJECT_STATUS status_flag, XGL_DBG_MSG_TYPE error_level, OBJECT_TRACK_ERROR error_code, char* fail_msg) {')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001209 header_txt.append(' objNode *pTrav = pObjectHead[objType];')
1210 header_txt.append(' while (pTrav) {')
1211 header_txt.append(' if (pTrav->obj.pObj == pObj) {')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001212 header_txt.append(' if ((pTrav->obj.status & status_mask) != status_flag) {')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001213 header_txt.append(' char str[1024];')
1214 header_txt.append(' sprintf(str, "OBJECT VALIDATION WARNING: %s object %p: %s", string_XGL_OBJECT_TYPE(objType), (void*)pObj, fail_msg);')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001215 header_txt.append(' layerCbMsg(error_level, XGL_VALIDATION_LEVEL_0, pObj, 0, error_code, "OBJTRACK", str);')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001216 header_txt.append(' return XGL_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001217 header_txt.append(' }')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001218 header_txt.append(' return XGL_TRUE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001219 header_txt.append(' }')
1220 header_txt.append(' pTrav = pTrav->pNextObj;')
1221 header_txt.append(' }')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001222 header_txt.append(' if (objType != XGL_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY) {')
1223 header_txt.append(' // If we do not find it print an error')
1224 header_txt.append(' char str[1024];')
1225 header_txt.append(' sprintf(str, "Unable to obtain status for non-existent object %p of %s type", pObj, string_XGL_OBJECT_TYPE(objType));')
1226 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, pObj, 0, OBJTRACK_UNKNOWN_OBJECT, "OBJTRACK", str);')
1227 header_txt.append(' }')
1228 header_txt.append(' return XGL_FALSE;')
Tobin Ehlis235c20e2015-01-16 08:56:30 -07001229 header_txt.append('}')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001230 header_txt.append('')
1231 header_txt.append('static void validate_draw_state_flags(void* pObj) {')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001232 header_txt.append(' validate_status((void*)pObj, XGL_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_VIEWPORT_BOUND, OBJSTATUS_VIEWPORT_BOUND, XGL_DBG_MSG_ERROR, OBJTRACK_VIEWPORT_NOT_BOUND, "Viewport object not bound to this command buffer");')
1233 header_txt.append(' validate_status((void*)pObj, XGL_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_RASTER_BOUND, OBJSTATUS_RASTER_BOUND, XGL_DBG_MSG_ERROR, OBJTRACK_RASTER_NOT_BOUND, "Raster object not bound to this command buffer");')
1234 header_txt.append(' validate_status((void*)pObj, XGL_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_COLOR_BLEND_BOUND, OBJSTATUS_COLOR_BLEND_BOUND, XGL_DBG_MSG_UNKNOWN, OBJTRACK_COLOR_BLEND_NOT_BOUND, "Color-blend object not bound to this command buffer");')
1235 header_txt.append(' validate_status((void*)pObj, XGL_OBJECT_TYPE_CMD_BUFFER, OBJSTATUS_DEPTH_STENCIL_BOUND, OBJSTATUS_DEPTH_STENCIL_BOUND, XGL_DBG_MSG_UNKNOWN, OBJTRACK_DEPTH_STENCIL_NOT_BOUND, "Depth-stencil object not bound to this command buffer");')
1236 header_txt.append('}')
1237 header_txt.append('')
1238 header_txt.append('static void validate_memory_mapping_status(const XGL_MEMORY_REF* pMemRefs, uint32_t numRefs) {')
Ian Elliotteac469b2015-02-04 12:15:12 -07001239 header_txt.append(' uint32_t i;')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001240 header_txt.append(' for (i = 0; i < numRefs; i++) {')
Mark Lobodzinskiacb93682015-03-05 12:39:33 -06001241 header_txt.append(' if(pMemRefs[i].mem) {')
1242 header_txt.append(' // If mem reference is in presentable image memory list, skip the check of the GPU_MEMORY list')
1243 header_txt.append(' if (!validate_status((void *)pMemRefs[i].mem, XGL_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY, OBJSTATUS_NONE, OBJSTATUS_NONE, XGL_DBG_MSG_UNKNOWN, OBJTRACK_NONE, NULL) == XGL_TRUE)')
1244 header_txt.append(' {')
1245 header_txt.append(' validate_status((void *)pMemRefs[i].mem, XGL_OBJECT_TYPE_GPU_MEMORY, OBJSTATUS_GPU_MEM_MAPPED, OBJSTATUS_NONE, XGL_DBG_MSG_ERROR, OBJTRACK_GPU_MEM_MAPPED, "A Mapped Memory Object was referenced in a command buffer");')
1246 header_txt.append(' }')
1247 header_txt.append(' }')
Mark Lobodzinski4186e712015-02-03 11:52:26 -06001248 header_txt.append(' }')
Mark Lobodzinski01552702015-02-03 10:06:31 -06001249 header_txt.append('}')
Mark Lobodzinskie1d3f0c2015-02-09 10:20:53 -06001250 header_txt.append('')
1251 header_txt.append('static void validate_mem_ref_count(uint32_t numRefs) {')
1252 header_txt.append(' if (maxMemRefsPerSubmission == 0) {')
1253 header_txt.append(' char str[1024];')
1254 header_txt.append(' sprintf(str, "xglQueueSubmit called before calling xglGetGpuInfo");')
1255 header_txt.append(' layerCbMsg(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, NULL, 0, OBJTRACK_GETGPUINFO_NOT_CALLED, "OBJTRACK", str);')
1256 header_txt.append(' } else {')
1257 header_txt.append(' if (numRefs > maxMemRefsPerSubmission) {')
1258 header_txt.append(' char str[1024];')
1259 header_txt.append(' sprintf(str, "xglQueueSubmit Memory reference count (%d) exceeds allowable GPU limit (%d)", numRefs, maxMemRefsPerSubmission);')
1260 header_txt.append(' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0, OBJTRACK_MEMREFCOUNT_MAX_EXCEEDED, "OBJTRACK", str);')
1261 header_txt.append(' }')
1262 header_txt.append(' }')
1263 header_txt.append('}')
1264 header_txt.append('')
1265 header_txt.append('static void setGpuInfoState(void *pData) {')
1266 header_txt.append(' maxMemRefsPerSubmission = ((XGL_PHYSICAL_GPU_PROPERTIES *)pData)->maxMemRefsPerSubmission;')
1267 header_txt.append('}')
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001268 return "\n".join(header_txt)
1269
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001270 def generate_intercept(self, proto, qual):
Mike Stroyan723913e2015-04-03 14:39:16 -06001271 if proto.name in [ 'DbgRegisterMsgCallback', 'DbgUnregisterMsgCallback' ]:
1272 # use default version
1273 return None
1274 obj_type_mapping = {base_t : base_t.replace("XGL_", "XGL_OBJECT_TYPE_") for base_t in xgl.object_type_list}
1275 # For the various "super-types" we have to use function to distinguish sub type
1276 for obj_type in ["XGL_BASE_OBJECT", "XGL_OBJECT", "XGL_DYNAMIC_STATE_OBJECT"]:
1277 obj_type_mapping[obj_type] = "ll_get_obj_type(object)"
1278
1279 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
1280 param0_name = proto.params[0].name
1281 p0_type = proto.params[0].ty.strip('*').strip('const ')
1282 create_line = ''
1283 destroy_line = ''
1284 funcs = []
1285 # Special cases for API funcs that don't use an object as first arg
1286 if True in [no_use_proto in proto.name for no_use_proto in ['GlobalOption', 'CreateInstance', 'QueueSubmit', 'QueueSetGlobalMemReferences', 'QueueWaitIdle', 'CreateDevice', 'GetGpuInfo', 'QueueSignalSemaphore', 'QueueWaitSemaphore', 'WsiX11QueuePresent']]:
1287 using_line = ''
1288 else:
1289 using_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1290 using_line += ' ll_increment_use_count((void*)%s, %s);\n' % (param0_name, obj_type_mapping[p0_type])
1291 using_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1292 if 'QueueSubmit' in proto.name:
1293 using_line += ' set_status((void*)fence, XGL_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED);\n'
1294 using_line += ' validate_memory_mapping_status(pMemRefs, memRefCount);\n'
1295 using_line += ' validate_mem_ref_count(memRefCount);\n'
1296 elif 'GetFenceStatus' in proto.name:
1297 using_line += ' // Warn if submitted_flag is not set\n'
1298 using_line += ' validate_status((void*)fence, XGL_OBJECT_TYPE_FENCE, OBJSTATUS_FENCE_IS_SUBMITTED, OBJSTATUS_FENCE_IS_SUBMITTED, XGL_DBG_MSG_ERROR, OBJTRACK_INVALID_FENCE, "Status Requested for Unsubmitted Fence");\n'
1299 elif 'EndCommandBuffer' in proto.name:
1300 using_line += ' reset_status((void*)cmdBuffer, XGL_OBJECT_TYPE_CMD_BUFFER, (OBJSTATUS_VIEWPORT_BOUND |\n'
1301 using_line += ' OBJSTATUS_RASTER_BOUND |\n'
1302 using_line += ' OBJSTATUS_COLOR_BLEND_BOUND |\n'
1303 using_line += ' OBJSTATUS_DEPTH_STENCIL_BOUND));\n'
1304 elif 'CmdBindDynamicStateObject' in proto.name:
1305 using_line += ' track_object_status((void*)cmdBuffer, stateBindPoint);\n'
1306 elif 'CmdDraw' in proto.name:
1307 using_line += ' validate_draw_state_flags((void *)cmdBuffer);\n'
1308 elif 'MapMemory' in proto.name:
1309 using_line += ' set_status((void*)mem, XGL_OBJECT_TYPE_GPU_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);\n'
1310 elif 'UnmapMemory' in proto.name:
1311 using_line += ' reset_status((void*)mem, XGL_OBJECT_TYPE_GPU_MEMORY, OBJSTATUS_GPU_MEM_MAPPED);\n'
1312 if 'AllocDescriptor' in proto.name: # Allocates array of DSs
1313 create_line = ' for (uint32_t i = 0; i < *pCount; i++) {\n'
1314 create_line += ' loader_platform_thread_lock_mutex(&objLock);\n'
1315 create_line += ' ll_insert_obj((void*)pDescriptorSets[i], XGL_OBJECT_TYPE_DESCRIPTOR_SET);\n'
1316 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1317 create_line += ' }\n'
1318 elif 'CreatePresentableImage' in proto.name:
1319 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1320 create_line += ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-2].name, obj_type_mapping[proto.params[-2].ty.strip('*').strip('const ')])
1321 create_line += ' ll_insert_obj((void*)*pMem, XGL_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY);\n'
1322 # create_line += ' ll_insert_obj((void*)*%s, XGL_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY);\n' % (obj_type_mapping[proto.params[-1].ty.strip('*').strip('const ')])
1323 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1324 elif 'Create' in proto.name or 'Alloc' in proto.name:
1325 create_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1326 create_line += ' ll_insert_obj((void*)*%s, %s);\n' % (proto.params[-1].name, obj_type_mapping[proto.params[-1].ty.strip('*').strip('const ')])
1327 create_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1328 if 'DestroyObject' in proto.name:
1329 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1330 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1331 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1332 using_line = ''
1333 else:
1334 if 'Destroy' in proto.name or 'Free' in proto.name:
1335 destroy_line = ' loader_platform_thread_lock_mutex(&objLock);\n'
1336 destroy_line += ' ll_destroy_obj((void*)%s);\n' % (param0_name)
1337 destroy_line += ' loader_platform_thread_unlock_mutex(&objLock);\n'
1338 using_line = ''
1339 if 'DestroyDevice' in proto.name:
1340 destroy_line += ' // Report any remaining objects in LL\n objNode *pTrav = pGlobalHead;\n while (pTrav) {\n'
1341 destroy_line += ' if (pTrav->obj.objType == XGL_OBJECT_TYPE_PRESENTABLE_IMAGE_MEMORY) {\n'
1342 destroy_line += ' objNode *pDel = pTrav;\n'
1343 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1344 destroy_line += ' ll_destroy_obj((void*)(pDel->obj.pObj));\n'
1345 destroy_line += ' } else {\n'
1346 destroy_line += ' char str[1024];\n'
1347 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'
1348 destroy_line += ' layerCbMsg(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, device, 0, OBJTRACK_OBJECT_LEAK, "OBJTRACK", str);\n'
1349 destroy_line += ' pTrav = pTrav->pNextGlobal;\n'
1350 destroy_line += ' }\n'
1351 destroy_line += ' }\n'
1352 ret_val = ''
1353 stmt = ''
1354 if proto.ret != "void":
1355 ret_val = "XGL_RESULT result = "
1356 stmt = " return result;\n"
1357 if 'WsiX11AssociateConnection' == proto.name:
1358 funcs.append("#if defined(__linux__) || defined(XCB_NVIDIA)")
1359 if proto.name == "EnumerateLayers":
1360 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
1361 funcs.append('%s%s\n'
1362 '{\n'
1363 ' if (gpu != NULL) {\n'
1364 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
1365 ' %s'
1366 ' pCurObj = gpuw;\n'
1367 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1368 ' %snextTable.%s;\n'
1369 ' %s%s'
1370 ' %s'
1371 ' } else {\n'
1372 ' if (pOutLayerCount == NULL || pOutLayers == NULL || pOutLayers[0] == NULL)\n'
1373 ' return XGL_ERROR_INVALID_POINTER;\n'
1374 ' // This layer compatible with all GPUs\n'
1375 ' *pOutLayerCount = 1;\n'
1376 ' strncpy((char *) pOutLayers[0], "%s", maxStringSize);\n'
1377 ' return XGL_SUCCESS;\n'
1378 ' }\n'
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001379 '}' % (qual, decl, proto.params[0].name, using_line, self.layer_name, ret_val, c_call, create_line, destroy_line, stmt, self.layer_name))
Mike Stroyan723913e2015-04-03 14:39:16 -06001380 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
1381 funcs.append('%s%s\n'
1382 '{\n'
1383 '%s'
1384 ' %snextTable.%s;\n'
1385 '%s%s'
1386 '%s'
1387 '}' % (qual, decl, using_line, ret_val, proto.c_call(), create_line, destroy_line, stmt))
1388 else:
1389 c_call = proto.c_call().replace("(" + proto.params[0].name, "((XGL_PHYSICAL_GPU)gpuw->nextObject", 1)
1390 gpu_state = ''
1391 if 'GetGpuInfo' in proto.name:
1392 gpu_state = ' if (infoType == XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES) {\n'
1393 gpu_state += ' if (pData != NULL) {\n'
1394 gpu_state += ' setGpuInfoState(pData);\n'
1395 gpu_state += ' }\n'
1396 gpu_state += ' }\n'
1397 funcs.append('%s%s\n'
1398 '{\n'
1399 ' XGL_BASE_LAYER_OBJECT* gpuw = (XGL_BASE_LAYER_OBJECT *) %s;\n'
1400 '%s'
1401 ' pCurObj = gpuw;\n'
1402 ' loader_platform_thread_once(&tabOnce, init%s);\n'
1403 ' %snextTable.%s;\n'
1404 '%s%s'
1405 '%s'
1406 '%s'
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001407 '}' % (qual, decl, proto.params[0].name, using_line, self.layer_name, ret_val, c_call, create_line, destroy_line, gpu_state, stmt))
Mike Stroyan723913e2015-04-03 14:39:16 -06001408 if 'WsiX11QueuePresent' == proto.name:
1409 funcs.append("#endif")
1410 return "\n\n".join(funcs)
1411
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001412 def generate_body(self):
Mike Stroyan3aecdb42015-04-03 17:13:23 -06001413 self.layer_name = "ObjectTracker"
1414 body = [self._generate_layer_initialization(True, lockname='obj'),
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001415 self._generate_dispatch_entrypoints("XGL_LAYER_EXPORT"),
Tobin Ehlis3c26a542014-11-18 11:28:33 -07001416 self._generate_extensions(),
Mike Stroyan2ad66f12015-04-03 17:45:53 -06001417 self._generate_layer_gpa_function(extensions=['objTrackGetObjectCount', 'objTrackGetObjects'])]
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001418
1419 return "\n\n".join(body)
Courtney Goeltzenleuchtere6094fc2014-11-18 10:40:29 -07001420
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001421def main():
1422 subcommands = {
1423 "layer-funcs" : LayerFuncsSubcommand,
1424 "layer-dispatch" : LayerDispatchSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001425 "Generic" : GenericLayerSubcommand,
1426 "ApiDump" : ApiDumpSubcommand,
1427 "ApiDumpFile" : ApiDumpFileSubcommand,
Tobin Ehlis07fe9ab2014-11-25 17:43:26 -07001428 "ApiDumpNoAddr" : ApiDumpNoAddrSubcommand,
Tobin Ehlis434db7c2015-01-10 12:42:41 -07001429 "ApiDumpCpp" : ApiDumpCppSubcommand,
1430 "ApiDumpNoAddrCpp" : ApiDumpNoAddrCppSubcommand,
Tobin Ehlis907a0522014-11-25 16:59:27 -07001431 "ObjectTracker" : ObjectTrackerSubcommand,
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001432 }
1433
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001434 if len(sys.argv) < 3 or sys.argv[1] not in subcommands or not os.path.exists(sys.argv[2]):
1435 print("Usage: %s <subcommand> <input_header> [options]" % sys.argv[0])
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001436 print
Tobin Ehlis2f3726c2015-01-15 17:51:52 -07001437 print("Available subcommands are: %s" % " ".join(subcommands))
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001438 exit(1)
1439
Tobin Ehlis6cd06372014-12-17 17:44:50 -07001440 hfp = xgl_helper.HeaderFileParser(sys.argv[2])
1441 hfp.parse()
1442 xgl_helper.enum_val_dict = hfp.get_enum_val_dict()
1443 xgl_helper.enum_type_dict = hfp.get_enum_type_dict()
1444 xgl_helper.struct_dict = hfp.get_struct_dict()
1445 xgl_helper.typedef_fwd_dict = hfp.get_typedef_fwd_dict()
1446 xgl_helper.typedef_rev_dict = hfp.get_typedef_rev_dict()
1447 xgl_helper.types_dict = hfp.get_types_dict()
1448
Tobin Ehlis92dbf802014-10-22 09:06:33 -06001449 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
1450 subcmd.run()
1451
1452if __name__ == "__main__":
1453 main()