]> git.cworth.org Git - apitrace/blob - glretrace.py
Make better use of C++ implicit casts. Support arrays.
[apitrace] / glretrace.py
1 ##########################################################################
2 #
3 # Copyright 2010 VMware, Inc.
4 # All Rights Reserved.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the 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 THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24 ##########################################################################/
25
26
27 import base
28 from glx import libgl
29
30
31
32 class ConstRemover(base.Rebuilder):
33
34     def visit_const(self, const):
35         return const.type
36
37
38 class ValueExtractor(base.Visitor):
39
40     def visit_literal(self, type, lvalue, rvalue):
41         print '    %s = %s;' % (lvalue, rvalue)
42
43     def visit_alias(self, type, lvalue, rvalue):
44         self.visit(type.type, lvalue, rvalue)
45     
46     def visit_enum(self, type, lvalue, rvalue):
47         print '    %s = %s;' % (lvalue, rvalue)
48
49     def visit_bitmask(self, type, lvalue, rvalue):
50         self.visit(type.type, lvalue, rvalue)
51
52     def visit_array(self, array, lvalue, rvalue):
53         print '    %s = new %s[%s];' % (lvalue, array.type, array.length)
54         index = '__i' + array.id
55         print '    for(size_t {i} = 0; {i} < {length}; ++{i}) {{'.format(i = index, length = array.length)
56         self.visit(array.type, '%s[%s]' % (lvalue, index), '%s[%s]' % (rvalue, index))
57         print '    }'
58
59
60
61 def retrace_function(function):
62     print 'static void retrace_%s(Trace::Call &call) {' % function.name
63     if not function.name.startswith('glX'):
64         success = True
65         for arg_type, arg_name in function.args:
66             arg_type = ConstRemover().visit(arg_type)
67             print '    %s %s;' % (arg_type, arg_name)
68             rvalue = 'call.arg("%s")' % (arg_name,)
69             lvalue = arg_name
70             try:
71                 ValueExtractor().visit(arg_type, lvalue, rvalue)
72             except NotImplementedError:
73                 success = False
74                 print '    %s = 0; // FIXME' % arg_name
75         if not success:
76             print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
77             print '    return;'
78         arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
79         print '    %s(%s);' % (function.name, arg_names)
80     print '}'
81     print
82
83
84 if __name__ == '__main__':
85     print
86     print '#include <stdlib.h>'
87     print '#include <string.h>'
88     print '#include <GL/glew.h>'
89     print '#include <GL/glut.h>'
90     print
91     print '#include "trace_parser.hpp"'
92     print
93
94     for function in libgl.functions:
95         retrace_function(function)
96
97     print 'static bool retrace_call(Trace::Call &call) {'
98     for function in libgl.functions:
99         print '    if (call.name == "%s") {' % function.name
100         print '        retrace_%s(call);' % function.name
101         print '        return true;'
102         print '    }'
103     print '    std::cerr << "warning: unsupported call " << call.name << "\\n";'
104     print '    return false;'
105     print '}'
106     print '''
107
108 class Retracer : public Trace::Parser
109 {
110     void handle_call(Trace::Call &call) {
111         std::cout << call;
112         std::cout.flush();
113         retrace_call(call);
114     }
115 };
116
117 int main(int argc, char **argv)
118 {
119    glutInit(&argc, argv);
120    glutInitWindowPosition( 0, 0 );
121    glutInitWindowSize( 800, 600 );
122    glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
123    glutCreateWindow(argv[0]);
124    glewInit();
125    for (int i = 1; i < argc; ++i) {
126       Retracer p;
127       p.parse(argv[i]);
128       glutMainLoop();
129    }
130    return 0;
131 }
132
133 '''