mesa: fix glthread marshal build issues on platforms without PTHREAD
[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 '#include <X11/Xlib-xcb.h>'
70 print
71 print '#ifdef HAVE_PTHREAD'
72 print
73 print 'static _X_INLINE int safe_mul(int a, int b)'
74 print '{'
75 print ' if (a < 0 || b < 0) return -1;'
76 print ' if (a == 0 || b == 0) return 0;'
77 print ' if (a > INT_MAX / b) return -1;'
78 print ' return a * b;'
79 print '}'
80 print
81
82 def printRealFooter(self):
83 print
84 print '#endif'
85
86 def print_sync_call(self, func):
87 call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
88 func.name, func.get_called_parameter_string())
89 if func.return_type == 'void':
90 out('{0};'.format(call))
91 else:
92 out('return {0};'.format(call))
93
94 def print_sync_dispatch(self, func):
95 out('_mesa_glthread_finish(ctx);')
96 self.print_sync_call(func)
97
98 def print_sync_body(self, func):
99 out('/* {0}: marshalled synchronously */'.format(func.name))
100 out('static {0} GLAPIENTRY'.format(func.return_type))
101 out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string()))
102 out('{')
103 with indent():
104 out('GET_CURRENT_CONTEXT(ctx);')
105 out('_mesa_glthread_finish(ctx);')
106 out('debug_print_sync("{0}");'.format(func.name))
107 self.print_sync_call(func)
108 out('}')
109 out('')
110 out('')
111
112 def print_async_dispatch(self, func):
113 out('cmd = _mesa_glthread_allocate_command(ctx, '
114 'DISPATCH_CMD_{0}, cmd_size);'.format(func.name))
115 for p in func.fixed_params:
116 if p.count:
117 out('memcpy(cmd->{0}, {0}, {1});'.format(
118 p.name, p.size_string()))
119 else:
120 out('cmd->{0} = {0};'.format(p.name))
121 if func.variable_params:
122 out('char *variable_data = (char *) (cmd + 1);')
123 for p in func.variable_params:
124 if p.img_null_flag:
125 out('cmd->{0}_null = !{0};'.format(p.name))
126 out('if (!cmd->{0}_null) {{'.format(p.name))
127 with indent():
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 out('}')
133 else:
134 out(('memcpy(variable_data, {0}, {1});').format(
135 p.name, p.size_string(False)))
136 out('variable_data += {0};'.format(
137 p.size_string(False)))
138
139 if not func.fixed_params and not func.variable_params:
140 out('(void) cmd;\n')
141 out('_mesa_post_marshal_hook(ctx);')
142
143 def print_async_struct(self, func):
144 out('struct marshal_cmd_{0}'.format(func.name))
145 out('{')
146 with indent():
147 out('struct marshal_cmd_base cmd_base;')
148 for p in func.fixed_params:
149 if p.count:
150 out('{0} {1}[{2}];'.format(
151 p.get_base_type_string(), p.name, p.count))
152 else:
153 out('{0} {1};'.format(p.type_string(), p.name))
154
155 for p in func.variable_params:
156 if p.img_null_flag:
157 out('bool {0}_null; /* If set, no data follows '
158 'for "{0}" */'.format(p.name))
159
160 for p in func.variable_params:
161 if p.count_scale != 1:
162 out(('/* Next {0} bytes are '
163 '{1} {2}[{3}][{4}] */').format(
164 p.size_string(), p.get_base_type_string(),
165 p.name, p.counter, p.count_scale))
166 else:
167 out(('/* Next {0} bytes are '
168 '{1} {2}[{3}] */').format(
169 p.size_string(), p.get_base_type_string(),
170 p.name, p.counter))
171 out('};')
172
173 def print_async_unmarshal(self, func):
174 out('static inline void')
175 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
176 'const struct marshal_cmd_{0} *cmd)').format(func.name))
177 out('{')
178 with indent():
179 for p in func.fixed_params:
180 if p.count:
181 out('const {0} * {1} = cmd->{1};'.format(
182 p.get_base_type_string(), p.name))
183 else:
184 out('const {0} {1} = cmd->{1};'.format(
185 p.type_string(), p.name))
186 if func.variable_params:
187 for p in func.variable_params:
188 out('const {0} * {1};'.format(
189 p.get_base_type_string(), p.name))
190 out('const char *variable_data = (const char *) (cmd + 1);')
191 for p in func.variable_params:
192 out('{0} = (const {1} *) variable_data;'.format(
193 p.name, p.get_base_type_string()))
194
195 if p.img_null_flag:
196 out('if (cmd->{0}_null)'.format(p.name))
197 with indent():
198 out('{0} = NULL;'.format(p.name))
199 out('else')
200 with indent():
201 out('variable_data += {0};'.format(p.size_string(False)))
202 else:
203 out('variable_data += {0};'.format(p.size_string(False)))
204
205 self.print_sync_call(func)
206 out('}')
207
208 def validate_count_or_return(self, func):
209 # Check that any counts for variable-length arguments might be < 0, in
210 # which case the command alloc or the memcpy would blow up before we
211 # get to the validation in Mesa core.
212 for p in func.parameters:
213 if p.is_variable_length():
214 out('if (unlikely({0} < 0)) {{'.format(p.size_string()))
215 with indent():
216 out('_mesa_glthread_finish(ctx);')
217 out('_mesa_error(ctx, GL_INVALID_VALUE, "{0}({1} < 0)");'.format(func.name, p.size_string()))
218 out('return;')
219 out('}')
220
221 def print_async_marshal(self, func):
222 out('static void GLAPIENTRY')
223 out('_mesa_marshal_{0}({1})'.format(
224 func.name, func.get_parameter_string()))
225 out('{')
226 with indent():
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()
232 if p.img_null_flag:
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))
237
238 out('debug_print_marshal("{0}");'.format(func.name))
239
240 self.validate_count_or_return(func)
241
242 if func.marshal_fail:
243 out('if ({0}) {{'.format(func.marshal_fail))
244 with indent():
245 out('_mesa_glthread_finish(ctx);')
246 out('_mesa_glthread_restore_dispatch(ctx);')
247 self.print_sync_dispatch(func)
248 out('return;')
249 out('}')
250
251 out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
252 with indent():
253 self.print_async_dispatch(func)
254 out('} else {')
255 with indent():
256 self.print_sync_dispatch(func)
257 out('}')
258
259 out('}')
260
261 def print_async_body(self, func):
262 out('/* {0}: marshalled asynchronously */'.format(func.name))
263 self.print_async_struct(func)
264 self.print_async_unmarshal(func)
265 self.print_async_marshal(func)
266 out('')
267 out('')
268
269 def print_unmarshal_dispatch_cmd(self, api):
270 out('size_t')
271 out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, '
272 'const void *cmd)')
273 out('{')
274 with indent():
275 out('const struct marshal_cmd_base *cmd_base = cmd;')
276 out('switch (cmd_base->cmd_id) {')
277 for func in api.functionIterateAll():
278 flavor = func.marshal_flavor()
279 if flavor in ('skip', 'sync'):
280 continue
281 out('case DISPATCH_CMD_{0}:'.format(func.name))
282 with indent():
283 out('debug_print_unmarshal("{0}");'.format(func.name))
284 out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)'
285 ' cmd);').format(func.name))
286 out('break;')
287 out('default:')
288 with indent():
289 out('assert(!"Unrecognized command ID");')
290 out('break;')
291 out('}')
292 out('')
293 out('return cmd_base->cmd_size;')
294 out('}')
295 out('')
296 out('')
297
298 def print_create_marshal_table(self, api):
299 out('struct _glapi_table *')
300 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
301 out('{')
302 with indent():
303 out('struct _glapi_table *table;')
304 out('')
305 out('table = _mesa_alloc_dispatch_table();')
306 out('if (table == NULL)')
307 with indent():
308 out('return NULL;')
309 out('')
310 for func in api.functionIterateAll():
311 if func.marshal_flavor() == 'skip':
312 continue
313 out('SET_{0}(table, _mesa_marshal_{0});'.format(func.name))
314 out('')
315 out('return table;')
316 out('}')
317 out('')
318 out('')
319
320 def printBody(self, api):
321 async_funcs = []
322 for func in api.functionIterateAll():
323 flavor = func.marshal_flavor()
324 if flavor in ('skip', 'custom'):
325 continue
326 elif flavor == 'async':
327 self.print_async_body(func)
328 async_funcs.append(func)
329 elif flavor == 'sync':
330 self.print_sync_body(func)
331 self.print_unmarshal_dispatch_cmd(api)
332 self.print_create_marshal_table(api)
333
334
335 def show_usage():
336 print 'Usage: %s [-f input_file_name]' % sys.argv[0]
337 sys.exit(1)
338
339
340 if __name__ == '__main__':
341 file_name = 'gl_API.xml'
342
343 try:
344 (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:')
345 except Exception,e:
346 show_usage()
347
348 for (arg,val) in args:
349 if arg == '-f':
350 file_name = val
351
352 printer = PrintCode()
353
354 api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())
355 printer.Print(api)