1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#!/usr/bin/env python
# Copyright (C) 2012 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import contextlib
import getopt
import gl_XML
import license
import marshal_XML
import sys
header = """
#include "api_exec.h"
#include "context.h"
#include "dispatch.h"
#include "glthread.h"
#include "marshal.h"
#include "marshal_generated.h"
"""
current_indent = 0
def out(str):
if str:
print ' '*current_indent + str
else:
print ''
@contextlib.contextmanager
def indent(delta = 3):
global current_indent
current_indent += delta
yield
current_indent -= delta
class PrintCode(gl_XML.gl_print_base):
def __init__(self):
super(PrintCode, self).__init__()
self.name = 'gl_marshal.py'
self.license = license.bsd_license_template % (
'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
def printRealHeader(self):
print header
print '#include <X11/Xlib-xcb.h>'
print
print 'static _X_INLINE int safe_mul(int a, int b)'
print '{'
print ' if (a < 0 || b < 0) return -1;'
print ' if (a == 0 || b == 0) return 0;'
print ' if (a > INT_MAX / b) return -1;'
print ' return a * b;'
print '}'
print
def printRealFooter(self):
pass
def print_sync_call(self, func):
call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
func.name, func.get_called_parameter_string())
if func.return_type == 'void':
out('{0};'.format(call))
else:
out('return {0};'.format(call))
def print_sync_dispatch(self, func):
out('_mesa_glthread_finish(ctx);')
self.print_sync_call(func)
def print_sync_body(self, func):
out('/* {0}: marshalled synchronously */'.format(func.name))
out('static {0} GLAPIENTRY'.format(func.return_type))
out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string()))
out('{')
with indent():
out('GET_CURRENT_CONTEXT(ctx);')
out('_mesa_glthread_finish(ctx);')
out('debug_print_sync("{0}");'.format(func.name))
self.print_sync_call(func)
out('}')
out('')
out('')
def print_async_dispatch(self, func):
out('cmd = _mesa_glthread_allocate_command(ctx, '
'DISPATCH_CMD_{0}, cmd_size);'.format(func.name))
for p in func.fixed_params:
if p.count:
out('memcpy(cmd->{0}, {0}, {1});'.format(
p.name, p.size_string()))
else:
out('cmd->{0} = {0};'.format(p.name))
if func.variable_params:
out('char *variable_data = (char *) (cmd + 1);')
for p in func.variable_params:
out(('memcpy(variable_data, {0}, {1});').format(
p.name, p.size_string(False)))
out('variable_data += {0};'.format(
p.size_string(False)))
if not func.fixed_params and not func.variable_params:
out('(void) cmd;\n')
out('_mesa_post_marshal_hook(ctx);')
def print_async_struct(self, func):
out('struct marshal_cmd_{0}'.format(func.name))
out('{')
with indent():
out('struct marshal_cmd_base cmd_base;')
for p in func.fixed_params:
if p.count:
out('{0} {1}[{2}];'.format(
p.get_base_type_string(), p.name, p.count))
else:
out('{0} {1};'.format(p.type_string(), p.name))
for p in func.variable_params:
if p.count_scale != 1:
out(('/* Next {0} bytes are '
'{1} {2}[{3}][{4}] */').format(
p.size_string(), p.get_base_type_string(),
p.name, p.counter, p.count_scale))
else:
out(('/* Next {0} bytes are '
'{1} {2}[{3}] */').format(
p.size_string(), p.get_base_type_string(),
p.name, p.counter))
out('};')
def print_async_unmarshal(self, func):
out('static inline void')
out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
'const struct marshal_cmd_{0} *cmd)').format(func.name))
out('{')
with indent():
for p in func.fixed_params:
if p.count:
out('const {0} * {1} = cmd->{1};'.format(
p.get_base_type_string(), p.name))
else:
out('const {0} {1} = cmd->{1};'.format(
p.type_string(), p.name))
if func.variable_params:
for p in func.variable_params:
out('const {0} * {1};'.format(
p.get_base_type_string(), p.name))
out('const char *variable_data = (const char *) (cmd + 1);')
for p in func.variable_params:
out('{0} = (const {1} *) variable_data;'.format(
p.name, p.get_base_type_string()))
out('variable_data += {0};'.format(p.size_string(False)))
self.print_sync_call(func)
out('}')
def print_async_marshal(self, func):
out('static void GLAPIENTRY')
out('_mesa_marshal_{0}({1})'.format(
func.name, func.get_parameter_string()))
out('{')
with indent():
out('GET_CURRENT_CONTEXT(ctx);')
struct = 'struct marshal_cmd_{0}'.format(func.name)
size_terms = ['sizeof({0})'.format(struct)]
for p in func.variable_params:
size_terms.append(p.size_string())
out('size_t cmd_size = {0};'.format(' + '.join(size_terms)))
out('{0} *cmd;'.format(struct))
out('debug_print_marshal("{0}");'.format(func.name))
out('if (cmd_size <= MARSHAL_MAX_CMD_SIZE) {')
with indent():
self.print_async_dispatch(func)
out('} else {')
with indent():
self.print_sync_dispatch(func)
out('}')
out('}')
def print_async_body(self, func):
out('/* {0}: marshalled asynchronously */'.format(func.name))
self.print_async_struct(func)
self.print_async_unmarshal(func)
self.print_async_marshal(func)
out('')
out('')
def print_unmarshal_dispatch_cmd(self, api):
out('size_t')
out('_mesa_unmarshal_dispatch_cmd(struct gl_context *ctx, '
'const void *cmd)')
out('{')
with indent():
out('const struct marshal_cmd_base *cmd_base = cmd;')
out('switch (cmd_base->cmd_id) {')
for func in api.functionIterateAll():
flavor = func.marshal_flavor()
if flavor in ('skip', 'sync'):
continue
out('case DISPATCH_CMD_{0}:'.format(func.name))
with indent():
out('debug_print_unmarshal("{0}");'.format(func.name))
out(('_mesa_unmarshal_{0}(ctx, (const struct marshal_cmd_{0} *)'
' cmd);').format(func.name))
out('break;')
out('default:')
with indent():
out('assert(!"Unrecognized command ID");')
out('break;')
out('}')
out('')
out('return cmd_base->cmd_size;')
out('}')
out('')
out('')
def print_create_marshal_table(self, api):
out('struct _glapi_table *')
out('_mesa_create_marshal_table(const struct gl_context *ctx)')
out('{')
with indent():
out('struct _glapi_table *table;')
out('')
out('table = _mesa_alloc_dispatch_table();')
out('if (table == NULL)')
with indent():
out('return NULL;')
out('')
for func in api.functionIterateAll():
if func.marshal_flavor() == 'skip':
continue
out('SET_{0}(table, _mesa_marshal_{0});'.format(func.name))
out('')
out('return table;')
out('}')
out('')
out('')
def printBody(self, api):
async_funcs = []
for func in api.functionIterateAll():
flavor = func.marshal_flavor()
if flavor in ('skip', 'custom'):
continue
elif flavor == 'async':
self.print_async_body(func)
async_funcs.append(func)
elif flavor == 'sync':
self.print_sync_body(func)
self.print_unmarshal_dispatch_cmd(api)
self.print_create_marshal_table(api)
def show_usage():
print 'Usage: %s [-f input_file_name]' % sys.argv[0]
sys.exit(1)
if __name__ == '__main__':
file_name = 'gl_API.xml'
try:
(args, trail) = getopt.getopt(sys.argv[1:], 'm:f:')
except Exception,e:
show_usage()
for (arg,val) in args:
if arg == '-f':
file_name = val
printer = PrintCode()
api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())
printer.Print(api)
|