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