mesa/glthread: remove HAVE_PTHREAD guards
[mesa.git] / src / mapi / glapi / gen / gl_marshal.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2012 Intel Corporation
4 #
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:
11 #
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
14 # Software.
15 #
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
22 # IN THE SOFTWARE.
23
24 import contextlib
25 import getopt
26 import gl_XML
27 import license
28 import marshal_XML
29 import sys
30
31 header = """
32 #include "api_exec.h"
33 #include "context.h"
34 #include "dispatch.h"
35 #include "glthread.h"
36 #include "marshal.h"
37 #include "marshal_generated.h"
38 """
39
40
41 current_indent = 0
42
43
44 def out(str):
45 if str:
46 print ' '*current_indent + str
47 else:
48 print ''
49
50
51 @contextlib.contextmanager
52 def indent(delta = 3):
53 global current_indent
54 current_indent += delta
55 yield
56 current_indent -= delta
57
58
59 class PrintCode(gl_XML.gl_print_base):
60 def __init__(self):
61 super(PrintCode, self).__init__()
62
63 self.name = 'gl_marshal.py'
64 self.license = license.bsd_license_template % (
65 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
66
67 def printRealHeader(self):
68 print header
69 print 'static inline int safe_mul(int a, int b)'
70 print '{'
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;'
75 print '}'
76 print
77
78 def printRealFooter(self):
79 pass
80
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))
86 else:
87 out('return {0};'.format(call))
88
89 def print_sync_dispatch(self, func):
90 out('debug_print_sync_fallback("{0}");'.format(func.name))
91 self.print_sync_call(func)
92
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()))
97 out('{')
98 with indent():
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)
103 out('}')
104 out('')
105 out('')
106
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:
111 if p.count:
112 out('memcpy(cmd->{0}, {0}, {1});'.format(
113 p.name, p.size_string()))
114 else:
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:
119 if p.img_null_flag:
120 out('cmd->{0}_null = !{0};'.format(p.name))
121 out('if (!cmd->{0}_null) {{'.format(p.name))
122 with indent():
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)))
127 out('}')
128 else:
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)))
133
134 if not func.fixed_params and not func.variable_params:
135 out('(void) cmd;\n')
136 out('_mesa_post_marshal_hook(ctx);')
137
138 def print_async_struct(self, func):
139 out('struct marshal_cmd_{0}'.format(func.name))
140 out('{')
141 with indent():
142 out('struct marshal_cmd_base cmd_base;')
143 for p in func.fixed_params:
144 if p.count:
145 out('{0} {1}[{2}];'.format(
146 p.get_base_type_string(), p.name, p.count))
147 else:
148 out('{0} {1};'.format(p.type_string(), p.name))
149
150 for p in func.variable_params:
151 if p.img_null_flag:
152 out('bool {0}_null; /* If set, no data follows '
153 'for "{0}" */'.format(p.name))
154
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))
161 else:
162 out(('/* Next {0} bytes are '
163 '{1} {2}[{3}] */').format(
164 p.size_string(), p.get_base_type_string(),
165 p.name, p.counter))
166 out('};')
167
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))
172 out('{')
173 with indent():
174 for p in func.fixed_params:
175 if p.count:
176 out('const {0} * {1} = cmd->{1};'.format(
177 p.get_base_type_string(), p.name))
178 else:
179 out('const {0} {1} = cmd->{1};'.format(
180 p.type_string(), p.name))
181 if func.variable_params:
182 for p in func.variable_params:
183 out('const {0} * {1};'.format(
184 p.get_base_type_string(), p.name))
185 out('const char *variable_data = (const char *) (cmd + 1);')
186 for p in func.variable_params:
187 out('{0} = (const {1} *) variable_data;'.format(
188 p.name, p.get_base_type_string()))
189
190 if p.img_null_flag:
191 out('if (cmd->{0}_null)'.format(p.name))
192 with indent():
193 out('{0} = NULL;'.format(p.name))
194 out('else')
195 with indent():
196 out('variable_data += {0};'.format(p.size_string(False)))
197 else:
198 out('variable_data += {0};'.format(p.size_string(False)))
199
200 self.print_sync_call(func)
201 out('}')
202
203 def validate_count_or_fallback(self, func):
204 # Check that any counts for variable-length arguments might be < 0, in
205 # which case the command alloc or the memcpy would blow up before we
206 # get to the validation in Mesa core.
207 for p in func.parameters:
208 if p.is_variable_length():
209 out('if (unlikely({0} < 0)) {{'.format(p.size_string()))
210 with indent():
211 out('goto fallback_to_sync;')
212 out('}')
213 return True
214 return False
215
216
217 def print_async_marshal(self, func):
218 need_fallback_sync = False
219 out('static void GLAPIENTRY')
220 out('_mesa_marshal_{0}({1})'.format(
221 func.name, func.get_parameter_string()))
222 out('{')
223 with indent():
224 out('GET_CURRENT_CONTEXT(ctx);')
225 struct = 'struct marshal_cmd_{0}'.format(func.name)
226 size_terms = ['sizeof({0})'.format(struct)]
227 for p in func.variable_params:
228 size = p.size_string()
229 if p.img_null_flag:
230 size = '({0} ? {1} : 0)'.format(p.name, size)
231 size_terms.append(size)
232 out('size_t cmd_size = {0};'.format(' + '.join(size_terms)))
233 out('{0} *cmd;'.format(struct))
234
235 out('debug_print_marshal("{0}");'.format(func.name))
236
237 need_fallback_sync = self.validate_count_or_fallback(func)
238
239 if func.marshal_fail:
240 out('if ({0}) {{'.format(func.marshal_fail))
241 with indent():
242 out('_mesa_glthread_finish(ctx);')
243 out('_mesa_glthread_restore_dispatch(ctx);')
244 self.print_sync_dispatch(func)
245 out('return;')
246 out('}')
247
248 out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
249 with indent():
250 self.print_async_dispatch(func)
251 out('return;')
252 out('}')
253
254 out('')
255 if need_fallback_sync:
256 out('fallback_to_sync:')
257 with indent():
258 out('_mesa_glthread_finish(ctx);')
259 self.print_sync_dispatch(func)
260
261 out('}')
262
263 def print_async_body(self, func):
264 out('/* {0}: marshalled asynchronously */'.format(func.name))
265 self.print_async_struct(func)
266 self.print_async_unmarshal(func)
267 self.print_async_marshal(func)
268 out('')
269 out('')
270
271 def print_unmarshal_dispatch_cmd(self, api):
272 out('size_t')
273 out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, '
274 'const void *cmd)')
275 out('{')
276 with indent():
277 out('const struct marshal_cmd_base *cmd_base = cmd;')
278 out('switch (cmd_base->cmd_id) {')
279 for func in api.functionIterateAll():
280 flavor = func.marshal_flavor()
281 if flavor in ('skip', 'sync'):
282 continue
283 out('case DISPATCH_CMD_{0}:'.format(func.name))
284 with indent():
285 out('debug_print_unmarshal("{0}");'.format(func.name))
286 out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)'
287 ' cmd);').format(func.name))
288 out('break;')
289 out('default:')
290 with indent():
291 out('assert(!"Unrecognized command ID");')
292 out('break;')
293 out('}')
294 out('')
295 out('return cmd_base->cmd_size;')
296 out('}')
297 out('')
298 out('')
299
300 def print_create_marshal_table(self, api):
301 out('struct _glapi_table *')
302 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
303 out('{')
304 with indent():
305 out('struct _glapi_table *table;')
306 out('')
307 out('table = _mesa_alloc_dispatch_table();')
308 out('if (table == NULL)')
309 with indent():
310 out('return NULL;')
311 out('')
312 for func in api.functionIterateAll():
313 if func.marshal_flavor() == 'skip':
314 continue
315 out('SET_{0}(table, _mesa_marshal_{0});'.format(func.name))
316 out('')
317 out('return table;')
318 out('}')
319 out('')
320 out('')
321
322 def printBody(self, api):
323 async_funcs = []
324 for func in api.functionIterateAll():
325 flavor = func.marshal_flavor()
326 if flavor in ('skip', 'custom'):
327 continue
328 elif flavor == 'async':
329 self.print_async_body(func)
330 async_funcs.append(func)
331 elif flavor == 'sync':
332 self.print_sync_body(func)
333 self.print_unmarshal_dispatch_cmd(api)
334 self.print_create_marshal_table(api)
335
336
337 def show_usage():
338 print 'Usage: %s [-f input_file_name]' % sys.argv[0]
339 sys.exit(1)
340
341
342 if __name__ == '__main__':
343 file_name = 'gl_API.xml'
344
345 try:
346 (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:')
347 except Exception,e:
348 show_usage()
349
350 for (arg,val) in args:
351 if arg == '-f':
352 file_name = val
353
354 printer = PrintCode()
355
356 api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())
357 printer.Print(api)