]> git.cworth.org Git - apitrace/blob - wrappers/egltrace.py
f40b8a06a863e2875d8704d6c8f64d4f03d1e82c
[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 '    if (_result) {'
57             print '        // update the profile'
58             print '        if (ctx != EGL_NO_CONTEXT) {'
59             print '            EGLint api = EGL_OPENGL_ES_API, version = 1;'
60             print '            gltrace::Context *tr = gltrace::getContext();'
61             print '            _eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_TYPE, &api);'
62             print '            _eglQueryContext(dpy, ctx, EGL_CONTEXT_CLIENT_VERSION, &version);'
63             print '            if (api == EGL_OPENGL_API)'
64             print '                tr->profile = gltrace::PROFILE_COMPAT;'
65             print '            else if (version == 1)'
66             print '                tr->profile = gltrace::PROFILE_ES1;'
67             print '            else'
68             print '                tr->profile = gltrace::PROFILE_ES2;'
69             print '        }'
70             print '    }'
71
72
73 if __name__ == '__main__':
74     print '#include <stdlib.h>'
75     print '#include <string.h>'
76     print '#include <dlfcn.h>'
77     print
78     print '#include "trace_writer_local.hpp"'
79     print
80     print '// To validate our prototypes'
81     print '#define GL_GLEXT_PROTOTYPES'
82     print '#define EGL_EGLEXT_PROTOTYPES'
83     print
84     print '#include "glproc.hpp"'
85     print '#include "glsize.hpp"'
86     print
87     
88     api = API()
89     api.addApi(eglapi)
90     api.addApi(glapi)
91     api.addApi(glesapi)
92     tracer = EglTracer()
93     tracer.traceApi(api)
94
95     print r'''
96
97
98 /*
99  * Android does not support LD_PRELOAD.
100  */
101 #if !defined(ANDROID)
102
103
104 /*
105  * Invoke the true dlopen() function.
106  */
107 static void *_dlopen(const char *filename, int flag)
108 {
109     typedef void * (*PFN_DLOPEN)(const char *, int);
110     static PFN_DLOPEN dlopen_ptr = NULL;
111
112     if (!dlopen_ptr) {
113         dlopen_ptr = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
114         if (!dlopen_ptr) {
115             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
116             return NULL;
117         }
118     }
119
120     return dlopen_ptr(filename, flag);
121 }
122
123
124 /*
125  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
126  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
127  * we need to intercept the dlopen() call here, and redirect to our wrapper
128  * shared object.
129  */
130 extern "C" PUBLIC
131 void * dlopen(const char *filename, int flag)
132 {
133     bool intercept = false;
134
135     if (filename) {
136         intercept =
137             strcmp(filename, "libEGL.so") == 0 ||
138             strcmp(filename, "libEGL.so.1") == 0 ||
139             strcmp(filename, "libGLESv1_CM.so") == 0 ||
140             strcmp(filename, "libGLESv1_CM.so.1") == 0 ||
141             strcmp(filename, "libGLESv2.so") == 0 ||
142             strcmp(filename, "libGLESv2.so.2") == 0 ||
143             strcmp(filename, "libGL.so") == 0 ||
144             strcmp(filename, "libGL.so.1") == 0;
145
146         if (intercept) {
147             os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
148
149             /* The current dispatch implementation relies on core entry-points to be globally available, so force this.
150              *
151              * TODO: A better approach would be note down the entry points here and
152              * use them latter. Another alternative would be to reopen the library
153              * with RTLD_NOLOAD | RTLD_GLOBAL.
154              */
155             flag &= ~RTLD_LOCAL;
156             flag |= RTLD_GLOBAL;
157         }
158     }
159
160     void *handle = _dlopen(filename, flag);
161
162     if (intercept) {
163         // Get the file path for our shared object, and use it instead
164         static int dummy = 0xdeedbeef;
165         Dl_info info;
166         if (dladdr(&dummy, &info)) {
167             handle = _dlopen(info.dli_fname, flag);
168         } else {
169             os::log("apitrace: warning: dladdr() failed\n");
170         }
171     }
172
173     return handle;
174 }
175
176
177 #endif /* !ANDROID */
178
179
180 #if defined(ANDROID)
181
182 /*
183  * Undocumented Android extensions used by Dalvik which have bound information
184  * passed to it, but is currently ignored, so probably unreliable.
185  *
186  * See:
187  * https://github.com/android/platform_frameworks_base/blob/master/opengl/libs/GLES_CM/gl.cpp
188  */
189
190 extern "C" PUBLIC
191 void APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
192     (void)count;
193     glColorPointer(size, type, stride, pointer);
194 }
195
196 extern "C" PUBLIC
197 void APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
198     (void)count;
199     glNormalPointer(type, stride, pointer);
200 }
201
202 extern "C" PUBLIC
203 void APIENTRY glTexCoordPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
204     (void)count;
205     glTexCoordPointer(size, type, stride, pointer);
206 }
207
208 extern "C" PUBLIC
209 void APIENTRY glVertexPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
210     (void)count;
211     glVertexPointer(size, type, stride, pointer);
212 }
213
214 extern "C" PUBLIC
215 void GL_APIENTRY glPointSizePointerOESBounds(GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
216     (void)count;
217     glPointSizePointerOES(type, stride, pointer);
218 }
219
220 extern "C" PUBLIC
221 void APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
222     (void)count;
223     glMatrixIndexPointerOES(size, type, stride, pointer);
224 }
225
226 extern "C" PUBLIC
227 void APIENTRY glWeightPointerOESBounds(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
228     (void)count;
229     glWeightPointerOES(size, type, stride, pointer);
230 }
231
232 /*
233  * There is also a glVertexAttribPointerBounds in
234  * https://github.com/android/platform_frameworks_base/blob/master/opengl/tools/glgen/stubs/gles11/GLES20cHeader.cpp
235  * but is it not exported.
236  */
237
238 #endif /* ANDROID */
239
240
241 '''