f52b9b7b810499512ab76e8d2e7e1654a5057814
3 # Copyright (C) 2012 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
37 #include "marshal_generated.h"
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 '#ifdef HAVE_PTHREAD'
71 print 'static inline int safe_mul(int a, int b)'
73 print ' if (a < 0 || b < 0) return -1;'
74 print ' if (a == 0 || b == 0) return 0;'
75 print ' if (a > INT_MAX / b) return -1;'
76 print ' return a * b;'
80 def printRealFooter(self
):
84 def print_sync_call(self
, func
):
85 call
= 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
86 func
.name
, func
.get_called_parameter_string())
87 if func
.return_type
== 'void':
88 out('{0};'.format(call
))
90 out('return {0};'.format(call
))
92 def print_sync_dispatch(self
, func
):
93 out('debug_print_sync_fallback("{0}");'.format(func
.name
))
94 self
.print_sync_call(func
)
96 def print_sync_body(self
, func
):
97 out('/* {0}: marshalled synchronously */'.format(func
.name
))
98 out('static {0} GLAPIENTRY'.format(func
.return_type
))
99 out('_mesa_marshal_{0}({1})'.format(func
.name
, func
.get_parameter_string()))
102 out('GET_CURRENT_CONTEXT(ctx);')
103 out('_mesa_glthread_finish(ctx);')
104 out('debug_print_sync("{0}");'.format(func
.name
))
105 self
.print_sync_call(func
)
110 def print_async_dispatch(self
, func
):
111 out('cmd = _mesa_glthread_allocate_command(ctx, '
112 'DISPATCH_CMD_{0}, cmd_size);'.format(func
.name
))
113 for p
in func
.fixed_params
:
115 out('memcpy(cmd->{0}, {0}, {1});'.format(
116 p
.name
, p
.size_string()))
118 out('cmd->{0} = {0};'.format(p
.name
))
119 if func
.variable_params
:
120 out('char *variable_data = (char *) (cmd + 1);')
121 for p
in func
.variable_params
:
123 out('cmd->{0}_null = !{0};'.format(p
.name
))
124 out('if (!cmd->{0}_null) {{'.format(p
.name
))
126 out(('memcpy(variable_data, {0}, {1});').format(
127 p
.name
, p
.size_string(False)))
128 out('variable_data += {0};'.format(
129 p
.size_string(False)))
132 out(('memcpy(variable_data, {0}, {1});').format(
133 p
.name
, p
.size_string(False)))
134 out('variable_data += {0};'.format(
135 p
.size_string(False)))
137 if not func
.fixed_params
and not func
.variable_params
:
139 out('_mesa_post_marshal_hook(ctx);')
141 def print_async_struct(self
, func
):
142 out('struct marshal_cmd_{0}'.format(func
.name
))
145 out('struct marshal_cmd_base cmd_base;')
146 for p
in func
.fixed_params
:
148 out('{0} {1}[{2}];'.format(
149 p
.get_base_type_string(), p
.name
, p
.count
))
151 out('{0} {1};'.format(p
.type_string(), p
.name
))
153 for p
in func
.variable_params
:
155 out('bool {0}_null; /* If set, no data follows '
156 'for "{0}" */'.format(p
.name
))
158 for p
in func
.variable_params
:
159 if p
.count_scale
!= 1:
160 out(('/* Next {0} bytes are '
161 '{1} {2}[{3}][{4}] */').format(
162 p
.size_string(), p
.get_base_type_string(),
163 p
.name
, p
.counter
, p
.count_scale
))
165 out(('/* Next {0} bytes are '
166 '{1} {2}[{3}] */').format(
167 p
.size_string(), p
.get_base_type_string(),
171 def print_async_unmarshal(self
, func
):
172 out('static inline void')
173 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
174 'const struct marshal_cmd_{0} *cmd)').format(func
.name
))
177 for p
in func
.fixed_params
:
179 out('const {0} * {1} = cmd->{1};'.format(
180 p
.get_base_type_string(), p
.name
))
182 out('const {0} {1} = cmd->{1};'.format(
183 p
.type_string(), p
.name
))
184 if func
.variable_params
:
185 for p
in func
.variable_params
:
186 out('const {0} * {1};'.format(
187 p
.get_base_type_string(), p
.name
))
188 out('const char *variable_data = (const char *) (cmd + 1);')
189 for p
in func
.variable_params
:
190 out('{0} = (const {1} *) variable_data;'.format(
191 p
.name
, p
.get_base_type_string()))
194 out('if (cmd->{0}_null)'.format(p
.name
))
196 out('{0} = NULL;'.format(p
.name
))
199 out('variable_data += {0};'.format(p
.size_string(False)))
201 out('variable_data += {0};'.format(p
.size_string(False)))
203 self
.print_sync_call(func
)
206 def validate_count_or_fallback(self
, func
):
207 # Check that any counts for variable-length arguments might be < 0, in
208 # which case the command alloc or the memcpy would blow up before we
209 # get to the validation in Mesa core.
210 for p
in func
.parameters
:
211 if p
.is_variable_length():
212 out('if (unlikely({0} < 0)) {{'.format(p
.size_string()))
214 out('goto fallback_to_sync;')
220 def print_async_marshal(self
, func
):
221 need_fallback_sync
= False
222 out('static void GLAPIENTRY')
223 out('_mesa_marshal_{0}({1})'.format(
224 func
.name
, func
.get_parameter_string()))
227 out('GET_CURRENT_CONTEXT(ctx);')
228 struct
= 'struct marshal_cmd_{0}'.format(func
.name
)
229 size_terms
= ['sizeof({0})'.format(struct
)]
230 for p
in func
.variable_params
:
231 size
= p
.size_string()
233 size
= '({0} ? {1} : 0)'.format(p
.name
, size
)
234 size_terms
.append(size
)
235 out('size_t cmd_size = {0};'.format(' + '.join(size_terms
)))
236 out('{0} *cmd;'.format(struct
))
238 out('debug_print_marshal("{0}");'.format(func
.name
))
240 need_fallback_sync
= self
.validate_count_or_fallback(func
)
242 if func
.marshal_fail
:
243 out('if ({0}) {{'.format(func
.marshal_fail
))
245 out('_mesa_glthread_finish(ctx);')
246 out('_mesa_glthread_restore_dispatch(ctx);')
247 self
.print_sync_dispatch(func
)
251 out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
253 self
.print_async_dispatch(func
)
258 if need_fallback_sync
:
259 out('fallback_to_sync:')
261 out('_mesa_glthread_finish(ctx);')
262 self
.print_sync_dispatch(func
)
266 def print_async_body(self
, func
):
267 out('/* {0}: marshalled asynchronously */'.format(func
.name
))
268 self
.print_async_struct(func
)
269 self
.print_async_unmarshal(func
)
270 self
.print_async_marshal(func
)
274 def print_unmarshal_dispatch_cmd(self
, api
):
276 out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, '
280 out('const struct marshal_cmd_base *cmd_base = cmd;')
281 out('switch (cmd_base->cmd_id) {')
282 for func
in api
.functionIterateAll():
283 flavor
= func
.marshal_flavor()
284 if flavor
in ('skip', 'sync'):
286 out('case DISPATCH_CMD_{0}:'.format(func
.name
))
288 out('debug_print_unmarshal("{0}");'.format(func
.name
))
289 out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)'
290 ' cmd);').format(func
.name
))
294 out('assert(!"Unrecognized command ID");')
298 out('return cmd_base->cmd_size;')
303 def print_create_marshal_table(self
, api
):
304 out('struct _glapi_table *')
305 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
308 out('struct _glapi_table *table;')
310 out('table = _mesa_alloc_dispatch_table();')
311 out('if (table == NULL)')
315 for func
in api
.functionIterateAll():
316 if func
.marshal_flavor() == 'skip':
318 out('SET_{0}(table, _mesa_marshal_{0});'.format(func
.name
))
325 def printBody(self
, api
):
327 for func
in api
.functionIterateAll():
328 flavor
= func
.marshal_flavor()
329 if flavor
in ('skip', 'custom'):
331 elif flavor
== 'async':
332 self
.print_async_body(func
)
333 async_funcs
.append(func
)
334 elif flavor
== 'sync':
335 self
.print_sync_body(func
)
336 self
.print_unmarshal_dispatch_cmd(api
)
337 self
.print_create_marshal_table(api
)
341 print 'Usage: %s [-f input_file_name]' % sys
.argv
[0]
345 if __name__
== '__main__':
346 file_name
= 'gl_API.xml'
349 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], 'm:f:')
353 for (arg
,val
) in args
:
357 printer
= PrintCode()
359 api
= gl_XML
.parse_GL_API(file_name
, marshal_XML
.marshal_item_factory())