]> git.cworth.org Git - apitrace/blob - wrappers/egltrace.py
505ca797442c4f354847d0394ef47ec7bf4c4a0e
[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 == '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 == 'eglCreateImageKHR':
85             print '    _eglCreateImageKHR_epilog(dpy, ctx, target, buffer,'
86             print '                              attrib_list, _result);'
87
88         if function.name == 'eglDestroyImageKHR':
89             print '    _eglDestroyImageKHR_epilog(image);'
90
91     def serializeArgValue(self, function, arg):
92         if function.name == 'glEGLImageTargetTexture2DOES' and \
93           arg.name == 'image':
94             print '    GLsizei blob_size;'
95             print '    GLvoid *blob_ptr;'
96             print '    blob_size = _glEGLImageTargetTexture2DOES_size(target, image);'
97             print '    blob_ptr = _glEGLImageTargetTexture2DOES_get_ptr(target, image);'
98             print '    trace::localWriter.writeBlob(blob_ptr, blob_size);'
99             print '    _glEGLImageTargetTexture2DOES_put_ptr(blob_ptr);'
100
101             return
102
103         GlTracer.serializeArgValue(self, function, arg)
104
105 if __name__ == '__main__':
106     print '#include <stdlib.h>'
107     print '#include <string.h>'
108     print '#include <dlfcn.h>'
109     print
110     print '#include "trace_writer_local.hpp"'
111     print
112     print '// To validate our prototypes'
113     print '#define GL_GLEXT_PROTOTYPES'
114     print '#define EGL_EGLEXT_PROTOTYPES'
115     print
116     print '#include "glproc.hpp"'
117     print '#include "glsize.hpp"'
118     print '#include "eglsize.hpp"'
119     print
120     
121     api = API()
122     api.addApi(eglapi)
123     api.addApi(glapi)
124     api.addApi(glesapi)
125     tracer = EglTracer()
126     tracer.traceApi(api)
127
128     print r'''
129
130
131 /*
132  * Android does not support LD_PRELOAD.
133  */
134 #if !defined(ANDROID)
135
136
137 /*
138  * Invoke the true dlopen() function.
139  */
140 static void *_dlopen(const char *filename, int flag)
141 {
142     typedef void * (*PFN_DLOPEN)(const char *, int);
143     static PFN_DLOPEN dlopen_ptr = NULL;
144
145     if (!dlopen_ptr) {
146         dlopen_ptr = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
147         if (!dlopen_ptr) {
148             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
149             return NULL;
150         }
151     }
152
153     return dlopen_ptr(filename, flag);
154 }
155
156
157 /*
158  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
159  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
160  * we need to intercept the dlopen() call here, and redirect to our wrapper
161  * shared object.
162  */
163 extern "C" PUBLIC
164 void * dlopen(const char *filename, int flag)
165 {
166     bool intercept = false;
167
168     if (filename) {
169         intercept =
170             strcmp(filename, "libEGL.so") == 0 ||
171             strcmp(filename, "libEGL.so.1") == 0 ||
172             strcmp(filename, "libGLESv1_CM.so") == 0 ||
173             strcmp(filename, "libGLESv1_CM.so.1") == 0 ||
174             strcmp(filename, "libGLESv2.so") == 0 ||
175             strcmp(filename, "libGLESv2.so.2") == 0 ||
176             strcmp(filename, "libGL.so") == 0 ||
177             strcmp(filename, "libGL.so.1") == 0;
178
179         if (intercept) {
180             os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
181
182             /* The current dispatch implementation relies on core entry-points to be globally available, so force this.
183              *
184              * TODO: A better approach would be note down the entry points here and
185              * use them latter. Another alternative would be to reopen the library
186              * with RTLD_NOLOAD | RTLD_GLOBAL.
187              */
188             flag &= ~RTLD_LOCAL;
189             flag |= RTLD_GLOBAL;
190         }
191     }
192
193     void *handle = _dlopen(filename, flag);
194
195     if (intercept) {
196         // Get the file path for our shared object, and use it instead
197         static int dummy = 0xdeedbeef;
198         Dl_info info;
199         if (dladdr(&dummy, &info)) {
200             handle = _dlopen(info.dli_fname, flag);
201         } else {
202             os::log("apitrace: warning: dladdr() failed\n");
203         }
204     }
205
206     return handle;
207 }
208
209
210 #endif /* !ANDROID */
211
212
213 #if defined(ANDROID)
214
215 /*
216  * Undocumented Android extensions used by Dalvik which have bound information
217  * passed to it, but is currently ignored, so probably unreliable.
218  *
219  * See:
220  * https://github.com/android/platform_frameworks_base/blob/master/opengl/libs/GLES_CM/gl.cpp
221  */
222
223 extern "C" PUBLIC
224 void APIENTRY glColorPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
225     (void)count;
226     glColorPointer(size, type, stride, pointer);
227 }
228
229 extern "C" PUBLIC
230 void APIENTRY glNormalPointerBounds(GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
231     (void)count;
232     glNormalPointer(type, stride, pointer);
233 }
234
235 extern "C" PUBLIC
236 void APIENTRY glTexCoordPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
237     (void)count;
238     glTexCoordPointer(size, type, stride, pointer);
239 }
240
241 extern "C" PUBLIC
242 void APIENTRY glVertexPointerBounds(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer, GLsizei count) {
243     (void)count;
244     glVertexPointer(size, type, stride, pointer);
245 }
246
247 extern "C" PUBLIC
248 void GL_APIENTRY glPointSizePointerOESBounds(GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
249     (void)count;
250     glPointSizePointerOES(type, stride, pointer);
251 }
252
253 extern "C" PUBLIC
254 void APIENTRY glMatrixIndexPointerOESBounds(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
255     (void)count;
256     glMatrixIndexPointerOES(size, type, stride, pointer);
257 }
258
259 extern "C" PUBLIC
260 void APIENTRY glWeightPointerOESBounds(GLint size, GLenum type, GLsizei stride, const GLvoid *pointer, GLsizei count) {
261     (void)count;
262     glWeightPointerOES(size, type, stride, pointer);
263 }
264
265 /*
266  * There is also a glVertexAttribPointerBounds in
267  * https://github.com/android/platform_frameworks_base/blob/master/opengl/tools/glgen/stubs/gles11/GLES20cHeader.cpp
268  * but is it not exported.
269  */
270
271 #endif /* ANDROID */
272
273
274 '''