]> git.cworth.org Git - apitrace/blob - glretrace.py
49a467a1c6ba1408e0693f7cd08b0f000b6c30b8
[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 def is_arg_supported(arg_type):
32     if isinstance(arg_type, (base.Literal, base._String, base.Enum)):
33         return True
34     if isinstance(arg_type, (base.Alias, base.Flags)):
35         return is_arg_supported(arg_type.type)
36     return False
37
38
39 def is_function_supported(function):
40     for arg_type, arg_name in function.args:
41         if not is_arg_supported(arg_type):
42             return False
43     return True
44
45
46 class ValueExtractor(base.Visitor):
47
48     def visit_literal(self, type, lvalue, rvalue):
49         #print '    %s = static_cast<%s>(Trace::as%s(%s));' % (lvalue, type, type.format, rvalue)
50         print '    %s = Trace::as%s(%s);' % (lvalue, type.format, rvalue)
51
52     def visit_alias(self, type, lvalue, rvalue):
53         self.visit(type.type, lvalue, rvalue)
54     
55     def visit_enum(self, type, lvalue, rvalue):
56         #print '    %s = static_cast<%s>(Trace::as%s(%s));' % (lvalue, type, 'SInt', rvalue)
57         print '    %s = Trace::as%s(%s);' % (lvalue, 'SInt', rvalue)
58
59     def visit_bitmask(self, type, lvalue, rvalue):
60         self.visit(type.type, lvalue, rvalue)
61
62
63
64 def retrace_function(function):
65     print 'static void retrace_%s(Trace::Call &call) {' % function.name
66     if not function.name.startswith('glX'):
67         success = True
68         for arg_type, arg_name in function.args:
69             print '    %s %s;' % (arg_type, arg_name)
70         for arg_type, arg_name in function.args:
71             rvalue = 'call.arg("%s")' % (arg_name,)
72             lvalue = arg_name
73             try:
74                 ValueExtractor().visit(arg_type, lvalue, rvalue)
75             except NotImplementedError:
76                 success = False
77                 print '    %s = 0; // FIXME' % arg_name
78         if not success:
79             print '    std::cerr << "warning: unsupported call %s\\n";' % function.name
80             print '    return;'
81         arg_names = ", ".join([arg_name for arg_type, arg_name in function.args])
82         print '    %s(%s);' % (function.name, arg_names)
83     print '}'
84     print
85
86
87 if __name__ == '__main__':
88     print
89     print '#include <stdlib.h>'
90     print '#include <string.h>'
91     print '#include <GL/glew.h>'
92     print '#include <GL/glut.h>'
93     print
94     print '#include "trace_parser.hpp"'
95     print
96
97     functions = filter(is_function_supported, libgl.functions)
98    
99     for function in libgl.functions:
100         retrace_function(function)
101
102     print 'static bool retrace_call(Trace::Call &call) {'
103     for function in libgl.functions:
104         print '    if (call.name == "%s") {' % function.name
105         print '        retrace_%s(call);' % function.name
106         print '        return true;'
107         print '    }'
108     print '    std::cerr << "warning: unsupported call " << call.name << "\\n";'
109     print '    return false;'
110     print '}'
111     print '''
112
113 class Retracer : public Trace::Parser
114 {
115     void handle_call(Trace::Call &call) {
116         std::cout << call;
117         std::cout.flush();
118         retrace_call(call);
119     }
120 };
121
122 int main(int argc, char **argv)
123 {
124    glutInit(&argc, argv);
125    glutInitWindowPosition( 0, 0 );
126    glutInitWindowSize( 800, 600 );
127    glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE );
128    glutCreateWindow(argv[0]);
129    glewInit();
130    for (int i = 1; i < argc; ++i) {
131       Retracer p;
132       p.parse(argv[i]);
133       glutMainLoop();
134    }
135    return 0;
136 }
137
138 '''