1 2# Copyright (C) 2012 Intel Corporation 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the "Software"), 6# to deal in the Software without restriction, including without limitation 7# the rights to use, copy, modify, merge, publish, distribute, sublicense, 8# and/or sell copies of the Software, and to permit persons to whom the 9# Software is furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice (including the next 12# paragraph) shall be included in all copies or substantial portions of the 13# Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22 23import contextlib 24import getopt 25import gl_XML 26import license 27import marshal_XML 28import sys 29 30header = """ 31#include "api_exec.h" 32#include "context.h" 33#include "dispatch.h" 34#include "glthread.h" 35#include "marshal.h" 36#include "marshal_generated.h" 37""" 38 39 40current_indent = 0 41 42 43def out(str): 44 if str: 45 print ' '*current_indent + str 46 else: 47 print '' 48 49 50@contextlib.contextmanager 51def indent(delta = 3): 52 global current_indent 53 current_indent += delta 54 yield 55 current_indent -= delta 56 57 58class PrintCode(gl_XML.gl_print_base): 59 def __init__(self): 60 super(PrintCode, self).__init__() 61 62 self.name = 'gl_marshal.py' 63 self.license = license.bsd_license_template % ( 64 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION') 65 66 def printRealHeader(self): 67 print header 68 print 'static inline int safe_mul(int a, int b)' 69 print '{' 70 print ' if (a < 0 || b < 0) return -1;' 71 print ' if (a == 0 || b == 0) return 0;' 72 print ' if (a > INT_MAX / b) return -1;' 73 print ' return a * b;' 74 print '}' 75 print 76 77 def printRealFooter(self): 78 pass 79 80 def print_sync_call(self, func): 81 call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format( 82 func.name, func.get_called_parameter_string()) 83 if func.return_type == 'void': 84 out('{0};'.format(call)) 85 else: 86 out('return {0};'.format(call)) 87 88 def print_sync_dispatch(self, func): 89 out('debug_print_sync_fallback("{0}");'.format(func.name)) 90 self.print_sync_call(func) 91 92 def print_sync_body(self, func): 93 out('/* {0}: marshalled synchronously */'.format(func.name)) 94 out('static {0} GLAPIENTRY'.format(func.return_type)) 95 out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string())) 96 out('{') 97 with indent(): 98 out('GET_CURRENT_CONTEXT(ctx);') 99 out('_mesa_glthread_finish(ctx);') 100 out('debug_print_sync("{0}");'.format(func.name)) 101 self.print_sync_call(func) 102 out('}') 103 out('') 104 out('') 105 106 def print_async_dispatch(self, func): 107 out('cmd = _mesa_glthread_allocate_command(ctx, ' 108 'DISPATCH_CMD_{0}, cmd_size);'.format(func.name)) 109 for p in func.fixed_params: 110 if p.count: 111 out('memcpy(cmd->{0}, {0}, {1});'.format( 112 p.name, p.size_string())) 113 else: 114 out('cmd->{0} = {0};'.format(p.name)) 115 if func.variable_params: 116 out('char *variable_data = (char *) (cmd + 1);') 117 for p in func.variable_params: 118 if p.img_null_flag: 119 out('cmd->{0}_null = !{0};'.format(p.name)) 120 out('if (!cmd->{0}_null) {{'.format(p.name)) 121 with indent(): 122 out(('memcpy(variable_data, {0}, {1});').format( 123 p.name, p.size_string(False))) 124 out('variable_data += {0};'.format( 125 p.size_string(False))) 126 out('}') 127 else: 128 out(('memcpy(variable_data, {0}, {1});').format( 129 p.name, p.size_string(False))) 130 out('variable_data += {0};'.format( 131 p.size_string(False))) 132 133 if not func.fixed_params and not func.variable_params: 134 out('(void) cmd;\n') 135 out('_mesa_post_marshal_hook(ctx);') 136 137 def print_async_struct(self, func): 138 out('struct marshal_cmd_{0}'.format(func.name)) 139 out('{') 140 with indent(): 141 out('struct marshal_cmd_base cmd_base;') 142 for p in func.fixed_params: 143 if p.count: 144 out('{0} {1}[{2}];'.format( 145 p.get_base_type_string(), p.name, p.count)) 146 else: 147 out('{0} {1};'.format(p.type_string(), p.name)) 148 149 for p in func.variable_params: 150 if p.img_null_flag: 151 out('bool {0}_null; /* If set, no data follows ' 152 'for "{0}" */'.format(p.name)) 153 154 for p in func.variable_params: 155 if p.count_scale != 1: 156 out(('/* Next {0} bytes are ' 157 '{1} {2}[{3}][{4}] */').format( 158 p.size_string(), p.get_base_type_string(), 159 p.name, p.counter, p.count_scale)) 160 else: 161 out(('/* Next {0} bytes are ' 162 '{1} {2}[{3}] */').format( 163 p.size_string(), p.get_base_type_string(), 164 p.name, p.counter)) 165 out('};') 166 167 def print_async_unmarshal(self, func): 168 out('static inline void') 169 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, ' 170 'const struct marshal_cmd_{0} *cmd)').format(func.name)) 171 out('{') 172 with indent(): 173 for p in func.fixed_params: 174 if p.count: 175 p_decl = '{0} * {1} = cmd->{1};'.format( 176 p.get_base_type_string(), p.name) 177 else: 178 p_decl = '{0} {1} = cmd->{1};'.format( 179 p.type_string(), p.name) 180 181 if not p_decl.startswith('const '): 182 # Declare all local function variables as const, even if 183 # the original parameter is not const. 184 p_decl = 'const ' + p_decl 185 186 out(p_decl) 187 188 if func.variable_params: 189 for p in func.variable_params: 190 out('const {0} * {1};'.format( 191 p.get_base_type_string(), p.name)) 192 out('const char *variable_data = (const char *) (cmd + 1);') 193 for p in func.variable_params: 194 out('{0} = (const {1} *) variable_data;'.format( 195 p.name, p.get_base_type_string())) 196 197 if p.img_null_flag: 198 out('if (cmd->{0}_null)'.format(p.name)) 199 with indent(): 200 out('{0} = NULL;'.format(p.name)) 201 out('else') 202 with indent(): 203 out('variable_data += {0};'.format(p.size_string(False))) 204 else: 205 out('variable_data += {0};'.format(p.size_string(False))) 206 207 self.print_sync_call(func) 208 out('}') 209 210 def validate_count_or_fallback(self, func): 211 # Check that any counts for variable-length arguments might be < 0, in 212 # which case the command alloc or the memcpy would blow up before we 213 # get to the validation in Mesa core. 214 for p in func.parameters: 215 if p.is_variable_length(): 216 out('if (unlikely({0} < 0)) {{'.format(p.size_string())) 217 with indent(): 218 out('goto fallback_to_sync;') 219 out('}') 220 return True 221 return False 222 223 224 def print_async_marshal(self, func): 225 need_fallback_sync = False 226 out('static void GLAPIENTRY') 227 out('_mesa_marshal_{0}({1})'.format( 228 func.name, func.get_parameter_string())) 229 out('{') 230 with indent(): 231 out('GET_CURRENT_CONTEXT(ctx);') 232 struct = 'struct marshal_cmd_{0}'.format(func.name) 233 size_terms = ['sizeof({0})'.format(struct)] 234 for p in func.variable_params: 235 size = p.size_string() 236 if p.img_null_flag: 237 size = '({0} ? {1} : 0)'.format(p.name, size) 238 size_terms.append(size) 239 out('size_t cmd_size = {0};'.format(' + '.join(size_terms))) 240 out('{0} *cmd;'.format(struct)) 241 242 out('debug_print_marshal("{0}");'.format(func.name)) 243 244 need_fallback_sync = self.validate_count_or_fallback(func) 245 246 if func.marshal_fail: 247 out('if ({0}) {{'.format(func.marshal_fail)) 248 with indent(): 249 out('_mesa_glthread_finish(ctx);') 250 out('_mesa_glthread_restore_dispatch(ctx);') 251 self.print_sync_dispatch(func) 252 out('return;') 253 out('}') 254 255 out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {') 256 with indent(): 257 self.print_async_dispatch(func) 258 out('return;') 259 out('}') 260 261 out('') 262 if need_fallback_sync: 263 out('fallback_to_sync:') 264 with indent(): 265 out('_mesa_glthread_finish(ctx);') 266 self.print_sync_dispatch(func) 267 268 out('}') 269 270 def print_async_body(self, func): 271 out('/* {0}: marshalled asynchronously */'.format(func.name)) 272 self.print_async_struct(func) 273 self.print_async_unmarshal(func) 274 self.print_async_marshal(func) 275 out('') 276 out('') 277 278 def print_unmarshal_dispatch_cmd(self, api): 279 out('size_t') 280 out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, ' 281 'const void *cmd)') 282 out('{') 283 with indent(): 284 out('const struct marshal_cmd_base *cmd_base = cmd;') 285 out('switch (cmd_base->cmd_id) {') 286 for func in api.functionIterateAll(): 287 flavor = func.marshal_flavor() 288 if flavor in ('skip', 'sync'): 289 continue 290 out('case DISPATCH_CMD_{0}:'.format(func.name)) 291 with indent(): 292 out('debug_print_unmarshal("{0}");'.format(func.name)) 293 out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)' 294 ' cmd);').format(func.name)) 295 out('break;') 296 out('default:') 297 with indent(): 298 out('assert(!"Unrecognized command ID");') 299 out('break;') 300 out('}') 301 out('') 302 out('return cmd_base->cmd_size;') 303 out('}') 304 out('') 305 out('') 306 307 def print_create_marshal_table(self, api): 308 out('struct _glapi_table *') 309 out('_mesa_create_marshal_table(const struct gl_context *ctx)') 310 out('{') 311 with indent(): 312 out('struct _glapi_table *table;') 313 out('') 314 out('table = _mesa_alloc_dispatch_table();') 315 out('if (table == NULL)') 316 with indent(): 317 out('return NULL;') 318 out('') 319 for func in api.functionIterateAll(): 320 if func.marshal_flavor() == 'skip': 321 continue 322 out('SET_{0}(table, _mesa_marshal_{0});'.format(func.name)) 323 out('') 324 out('return table;') 325 out('}') 326 out('') 327 out('') 328 329 def printBody(self, api): 330 async_funcs = [] 331 for func in api.functionIterateAll(): 332 flavor = func.marshal_flavor() 333 if flavor in ('skip', 'custom'): 334 continue 335 elif flavor == 'async': 336 self.print_async_body(func) 337 async_funcs.append(func) 338 elif flavor == 'sync': 339 self.print_sync_body(func) 340 self.print_unmarshal_dispatch_cmd(api) 341 self.print_create_marshal_table(api) 342 343 344def show_usage(): 345 print 'Usage: %s [-f input_file_name]' % sys.argv[0] 346 sys.exit(1) 347 348 349if __name__ == '__main__': 350 file_name = 'gl_API.xml' 351 352 try: 353 (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:') 354 except Exception,e: 355 show_usage() 356 357 for (arg,val) in args: 358 if arg == '-f': 359 file_name = val 360 361 printer = PrintCode() 362 363 api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory()) 364 printer.Print(api) 365