]> git.cworth.org Git - apitrace/blob - wrappers/egltrace.py
Don't abuse double-underscore prefix.
[apitrace] / wrappers / egltrace.py
1 ##########################################################################
2 #
3 # Copyright 2011 LunarG, Inc.
4 # All Rights Reserved.
5 #
6 # Based on glxtrace.py, which has
7 #
8 #   Copyright 2011 Jose Fonseca
9 #   Copyright 2008-2010 VMware, Inc.
10 #
11 # Permission is hereby granted, free of charge, to any person obtaining a copy
12 # of this software and associated documentation files (the "Software"), to deal
13 # in the Software without restriction, including without limitation the rights
14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 # copies of the Software, and to permit persons to whom the Software is
16 # furnished to do so, subject to the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included in
19 # all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 # THE SOFTWARE.
28 #
29 ##########################################################################/
30
31
32 """EGL tracing generator."""
33
34
35 from gltrace import GlTracer
36 from specs.stdapi import API
37 from specs.glapi import glapi
38 from specs.eglapi import eglapi
39 from specs.glesapi import glesapi
40
41
42 class EglTracer(GlTracer):
43
44     def isFunctionPublic(self, function):
45         # The symbols visible in libEGL.so can vary, so expose them all
46         return True
47
48     getProcAddressFunctionNames = [
49         "eglGetProcAddress",
50     ]
51
52     def traceFunctionImplBody(self, function):
53         GlTracer.traceFunctionImplBody(self, function)
54
55         if function.name == 'eglMakeCurrent':
56             print '    // update the profile'
57             print '    if (ctx != EGL_NO_CONTEXT) {'
58             print '        EGLint api = EGL_OPENGL_ES_API, version = 1;'
59             print '        gltrace::Context *tr = gltrace::getContext();'
60             print '        _eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_TYPE, &api);'
61             print '        _eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &version);'
62             print '        if (api == EGL_OPENGL_API)'
63             print '            tr->profile = gltrace::PROFILE_COMPAT;'
64             print '        else if (version == 1)'
65             print '            tr->profile = gltrace::PROFILE_ES1;'
66             print '        else'
67             print '            tr->profile = gltrace::PROFILE_ES2;'
68             print '    }'
69
70
71 if __name__ == '__main__':
72     print '#include <stdlib.h>'
73     print '#include <string.h>'
74     print '#include <dlfcn.h>'
75     print
76     print '#include "trace_writer_local.hpp"'
77     print
78     print '// To validate our prototypes'
79     print '#define GL_GLEXT_PROTOTYPES'
80     print '#define EGL_EGLEXT_PROTOTYPES'
81     print
82     print '#include "glproc.hpp"'
83     print '#include "glsize.hpp"'
84     print
85     
86     api = API()
87     api.addApi(eglapi)
88     api.addApi(glapi)
89     api.addApi(glesapi)
90     tracer = EglTracer()
91     tracer.traceApi(api)
92
93     print r'''
94
95
96 /*
97  * Android does not support LD_PRELOAD.
98  */
99 #if !defined(ANDROID)
100
101
102 /*
103  * Invoke the true dlopen() function.
104  */
105 static void *_dlopen(const char *filename, int flag)
106 {
107     typedef void * (*PFN_DLOPEN)(const char *, int);
108     static PFN_DLOPEN dlopen_ptr = NULL;
109
110     if (!dlopen_ptr) {
111         dlopen_ptr = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
112         if (!dlopen_ptr) {
113             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
114             return NULL;
115         }
116     }
117
118     return dlopen_ptr(filename, flag);
119 }
120
121
122 /*
123  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
124  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
125  * we need to intercept the dlopen() call here, and redirect to our wrapper
126  * shared object.
127  */
128 extern "C" PUBLIC
129 void * dlopen(const char *filename, int flag)
130 {
131     bool intercept = false;
132
133     if (filename) {
134         intercept =
135             strcmp(filename, "libEGL.so") == 0 ||
136             strcmp(filename, "libEGL.so.1") == 0 ||
137             strcmp(filename, "libGLESv1_CM.so") == 0 ||
138             strcmp(filename, "libGLESv1_CM.so.1") == 0 ||
139             strcmp(filename, "libGLESv2.so") == 0 ||
140             strcmp(filename, "libGLESv2.so.2") == 0 ||
141             strcmp(filename, "libGL.so") == 0 ||
142             strcmp(filename, "libGL.so.1") == 0;
143
144         if (intercept) {
145             os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
146
147             /* The current dispatch implementation relies on core entry-points to be globally available, so force this.
148              *
149              * TODO: A better approach would be note down the entry points here and
150              * use them latter. Another alternative would be to reopen the library
151              * with RTLD_NOLOAD | RTLD_GLOBAL.
152              */
153             flag &= ~RTLD_LOCAL;
154             flag |= RTLD_GLOBAL;
155         }
156     }
157
158     void *handle = _dlopen(filename, flag);
159
160     if (intercept) {
161         // Get the file path for our shared object, and use it instead
162         static int dummy = 0xdeedbeef;
163         Dl_info info;
164         if (dladdr(&dummy, &info)) {
165             handle = _dlopen(info.dli_fname, flag);
166         } else {
167             os::log("apitrace: warning: dladdr() failed\n");
168         }
169     }
170
171     return handle;
172 }
173
174
175 #endif /* !ANDROID */
176
177
178
179 '''