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