c12a086942bb93405a3fbf8a80f6b6be609d14ec
2 # Copyright (C) 2012 Intel Corporation
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:
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
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
23 from __future__
import print_function
46 print(' '*current_indent
+ str)
51 @contextlib.contextmanager
52 def indent(delta
= 3):
54 current_indent
+= delta
56 current_indent
-= delta
59 class PrintCode(gl_XML
.gl_print_base
):
61 super(PrintCode
, self
).__init
__()
63 self
.name
= 'gl_marshal.py'
64 self
.license
= license
.bsd_license_template
% (
65 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
67 def printRealHeader(self
):
69 print('static inline int safe_mul(int a, int b)')
71 print(' if (a < 0 || b < 0) return -1;')
72 print(' if (a == 0 || b == 0) return 0;')
73 print(' if (a > INT_MAX / b) return -1;')
74 print(' return a * b;')
78 def printRealFooter(self
):
81 def print_sync_call(self
, func
):
82 call
= 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
83 func
.name
, func
.get_called_parameter_string())
84 if func
.return_type
== 'void':
85 out('{0};'.format(call
))
87 out('return {0};'.format(call
))
89 def print_sync_dispatch(self
, func
):
90 out('debug_print_sync_fallback("{0}");'.format(func
.name
))
91 self
.print_sync_call(func
)
93 def print_sync_body(self
, func
):
94 out('/* {0}: marshalled synchronously */'.format(func
.name
))
95 out('static {0} GLAPIENTRY'.format(func
.return_type
))
96 out('_mesa_marshal_{0}({1})'.format(func
.name
, func
.get_parameter_string()))
99 out('GET_CURRENT_CONTEXT(ctx);')
100 out('_mesa_glthread_finish(ctx);')
101 out('debug_print_sync("{0}");'.format(func
.name
))
102 self
.print_sync_call(func
)
107 def print_async_dispatch(self
, func
):
108 out('cmd = _mesa_glthread_allocate_command(ctx, '
109 'DISPATCH_CMD_{0}, cmd_size);'.format(func
.name
))
110 for p
in func
.fixed_params
:
112 out('memcpy(cmd->{0}, {0}, {1});'.format(
113 p
.name
, p
.size_string()))
115 out('cmd->{0} = {0};'.format(p
.name
))
116 if func
.variable_params
:
117 out('char *variable_data = (char *) (cmd + 1);')
118 for p
in func
.variable_params
:
120 out('cmd->{0}_null = !{0};'.format(p
.name
))
121 out('if (!cmd->{0}_null) {{'.format(p
.name
))
123 out(('memcpy(variable_data, {0}, {1});').format(
124 p
.name
, p
.size_string(False)))
125 out('variable_data += {0};'.format(
126 p
.size_string(False)))
129 out(('memcpy(variable_data, {0}, {1});').format(
130 p
.name
, p
.size_string(False)))
131 out('variable_data += {0};'.format(
132 p
.size_string(False)))
134 if not func
.fixed_params
and not func
.variable_params
:
136 out('_mesa_post_marshal_hook(ctx);')
138 def print_async_struct(self
, func
):
139 out('struct marshal_cmd_{0}'.format(func
.name
))
142 out('struct marshal_cmd_base cmd_base;')
143 for p
in func
.fixed_params
:
145 out('{0} {1}[{2}];'.format(
146 p
.get_base_type_string(), p
.name
, p
.count
))
148 out('{0} {1};'.format(p
.type_string(), p
.name
))
150 for p
in func
.variable_params
:
152 out('bool {0}_null; /* If set, no data follows '
153 'for "{0}" */'.format(p
.name
))
155 for p
in func
.variable_params
:
156 if p
.count_scale
!= 1:
157 out(('/* Next {0} bytes are '
158 '{1} {2}[{3}][{4}] */').format(
159 p
.size_string(), p
.get_base_type_string(),
160 p
.name
, p
.counter
, p
.count_scale
))
162 out(('/* Next {0} bytes are '
163 '{1} {2}[{3}] */').format(
164 p
.size_string(), p
.get_base_type_string(),
168 def print_async_unmarshal(self
, func
):
169 out('static inline void')
170 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
171 'const struct marshal_cmd_{0} *cmd)').format(func
.name
))
174 for p
in func
.fixed_params
:
176 p_decl
= '{0} * {1} = cmd->{1};'.format(
177 p
.get_base_type_string(), p
.name
)
179 p_decl
= '{0} {1} = cmd->{1};'.format(
180 p
.type_string(), p
.name
)
182 if not p_decl
.startswith('const '):
183 # Declare all local function variables as const, even if
184 # the original parameter is not const.
185 p_decl
= 'const ' + p_decl
189 if func
.variable_params
:
190 for p
in func
.variable_params
:
191 out('{0} * {1};'.format(
192 p
.get_base_type_string(), p
.name
))
193 out('const char *variable_data = (const char *) (cmd + 1);')
194 for p
in func
.variable_params
:
195 out('{0} = ({1} *) variable_data;'.format(
196 p
.name
, p
.get_base_type_string()))
199 out('if (cmd->{0}_null)'.format(p
.name
))
201 out('{0} = NULL;'.format(p
.name
))
204 out('variable_data += {0};'.format(p
.size_string(False)))
206 out('variable_data += {0};'.format(p
.size_string(False)))
208 self
.print_sync_call(func
)
211 def validate_count_or_fallback(self
, func
):
212 # Check that any counts for variable-length arguments might be < 0, in
213 # which case the command alloc or the memcpy would blow up before we
214 # get to the validation in Mesa core.
215 for p
in func
.parameters
:
216 if p
.is_variable_length():
217 out('if (unlikely({0} < 0)) {{'.format(p
.size_string()))
219 out('goto fallback_to_sync;')
225 def print_async_marshal(self
, func
):
226 need_fallback_sync
= False
227 out('static void GLAPIENTRY')
228 out('_mesa_marshal_{0}({1})'.format(
229 func
.name
, func
.get_parameter_string()))
232 out('GET_CURRENT_CONTEXT(ctx);')
233 struct
= 'struct marshal_cmd_{0}'.format(func
.name
)
234 size_terms
= ['sizeof({0})'.format(struct
)]
235 for p
in func
.variable_params
:
236 size
= p
.size_string()
238 size
= '({0} ? {1} : 0)'.format(p
.name
, size
)
239 size_terms
.append(size
)
240 out('int cmd_size = {0};'.format(' + '.join(size_terms
)))
241 out('{0} *cmd;'.format(struct
))
243 out('debug_print_marshal("{0}");'.format(func
.name
))
245 need_fallback_sync
= self
.validate_count_or_fallback(func
)
247 if func
.marshal_fail
:
248 out('if ({0}) {{'.format(func
.marshal_fail
))
250 out('_mesa_glthread_finish(ctx);')
251 out('_mesa_glthread_restore_dispatch(ctx, __func__);')
252 self
.print_sync_dispatch(func
)
256 if len(func
.variable_params
) > 0:
258 out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
260 self
.print_async_dispatch(func
)
264 if need_fallback_sync
:
265 out('fallback_to_sync:')
267 out('_mesa_glthread_finish(ctx);')
268 self
.print_sync_dispatch(func
)
271 self
.print_async_dispatch(func
)
272 assert not need_fallback_sync
275 def print_async_body(self
, func
):
276 out('/* {0}: marshalled asynchronously */'.format(func
.name
))
277 self
.print_async_struct(func
)
278 self
.print_async_unmarshal(func
)
279 self
.print_async_marshal(func
)
283 def print_unmarshal_dispatch_cmd(self
, api
):
284 out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
286 for func
in api
.functionIterateAll():
287 flavor
= func
.marshal_flavor()
288 if flavor
in ('skip', 'sync'):
290 out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func
.name
))
295 def print_create_marshal_table(self
, api
):
296 out('struct _glapi_table *')
297 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
300 out('struct _glapi_table *table;')
302 out('table = _mesa_alloc_dispatch_table();')
303 out('if (table == NULL)')
307 for func
in api
.functionIterateAll():
308 if func
.marshal_flavor() == 'skip':
310 out('SET_{0}(table, _mesa_marshal_{0});'.format(func
.name
))
317 def printBody(self
, api
):
319 for func
in api
.functionIterateAll():
320 flavor
= func
.marshal_flavor()
321 if flavor
in ('skip', 'custom'):
323 elif flavor
== 'async':
324 self
.print_async_body(func
)
325 async_funcs
.append(func
)
326 elif flavor
== 'sync':
327 self
.print_sync_body(func
)
328 self
.print_unmarshal_dispatch_cmd(api
)
329 self
.print_create_marshal_table(api
)
333 print('Usage: %s [-f input_file_name]' % sys
.argv
[0])
337 if __name__
== '__main__':
338 file_name
= 'gl_API.xml'
341 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], 'm:f:')
345 for (arg
,val
) in args
:
349 printer
= PrintCode()
351 api
= gl_XML
.parse_GL_API(file_name
, marshal_XML
.marshal_item_factory())