]> git.cworth.org Git - apitrace/blob - wrappers/glxtrace.py
glxtrace: Basic EXT_texture_from_pixmap support.
[apitrace] / wrappers / glxtrace.py
1 ##########################################################################
2 #
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2010 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 """GLX tracing generator."""
29
30
31 from gltrace import GlTracer
32 from specs.stdapi import Module, API
33 from specs.glapi import glapi
34 from specs.glxapi import glxapi
35
36
37 class GlxTracer(GlTracer):
38
39     def isFunctionPublic(self, function):
40         # The symbols visible in libGL.so can vary, so expose them all
41         return True
42
43     getProcAddressFunctionNames = [
44         "glXGetProcAddress",
45         "glXGetProcAddressARB",
46     ]
47
48     createContextFunctionNames = [
49         'glXCreateContext',
50         'glXCreateContextAttribsARB',
51         'glXCreateContextWithConfigSGIX',
52         'glXCreateNewContext',
53     ]
54
55     destroyContextFunctionNames = [
56         'glXDestroyContext',
57     ]
58
59     makeCurrentFunctionNames = [
60         'glXMakeCurrent',
61         'glXMakeContextCurrent',
62         'glXMakeCurrentReadSGI',
63     ]
64
65     def traceFunctionImplBody(self, function):
66         if function.name in self.destroyContextFunctionNames:
67             print '    gltrace::releaseContext((uintptr_t)ctx);'
68
69         GlTracer.traceFunctionImplBody(self, function)
70
71         if function.name in self.createContextFunctionNames:
72             print '    if (_result != NULL)'
73             print '        gltrace::createContext((uintptr_t)_result);'
74
75         if function.name in self.makeCurrentFunctionNames:
76             print '    if (_result) {'
77             print '        if (ctx != NULL)'
78             print '            gltrace::setContext((uintptr_t)ctx);'
79             print '        else'
80             print '            gltrace::clearContext();'
81             print '    }'
82
83         if function.name == 'glXBindTexImageEXT':
84             # FIXME: glXBindTexImageEXT gets called frequently, so we should
85             # avoid recording the same data over and over again somehow, e.g.:
86             # - get the pixels before and after glXBindTexImageEXT, and only
87             #   emit emitFakeTexture2D when it changes
88             # - keep a global hash of the pixels
89             # FIXME: Handle mipmaps
90             print r'''
91                 unsigned glx_target = 0;
92                 _glXQueryDrawable(display, drawable, GLX_TEXTURE_TARGET_EXT, &glx_target);
93                 GLenum target;
94                 switch (glx_target) {
95                 // FIXME
96                 //case GLX_TEXTURE_1D_EXT:
97                 //    target = GL_TEXTURE_1D;
98                 //    break;
99                 case GLX_TEXTURE_2D_EXT:
100                     target = GL_TEXTURE_2D;
101                     break;
102                 case GLX_TEXTURE_RECTANGLE_EXT:
103                     target = GL_TEXTURE_RECTANGLE;
104                     break;
105                 default:
106                     os::log("apitrace: warning: %s: unsupported GLX_TEXTURE_TARGET_EXT 0x%u\n", __FUNCTION__, glx_target);
107                     target = GL_NONE;
108                     break;
109                 }
110                 GLint level = 0;
111                 GLint internalformat = GL_NONE;
112                 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
113                 GLint width = 0;
114                 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
115                 GLint height = 0;
116                 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
117                 GLint border = 0;
118                 unsigned glx_format = 0;
119                 _glXQueryDrawable(display, drawable, GLX_TEXTURE_FORMAT_EXT, &glx_format);
120                 GLenum format;
121                 switch (glx_format) {
122                 case GLX_TEXTURE_FORMAT_RGB_EXT:
123                     format = GL_RGB;
124                     break;
125                 case GLX_TEXTURE_FORMAT_RGBA_EXT:
126                     format = GL_RGBA;
127                     break;
128                 case GLX_TEXTURE_FORMAT_NONE_EXT:
129                     // XXX: This really shouldn't happen but some
130                     // implementations (Mesa) appear return bogus results to
131                     // the GLX_TEXTURE_FORMAT_EXT query
132                 default:
133                     //os::log("apitrace: warning: %s: unexpected GLX_TEXTURE_FORMAT_EXT 0x%u\n", __FUNCTION__, glx_format);
134                     format = GL_RGBA;
135                     break;
136                 }
137                 GLenum type = GL_UNSIGNED_BYTE;
138                 if (target && internalformat && height && width && format) {
139                     GLint channels = _gl_format_channels(format);
140                     GLvoid * pixels = malloc(height * width * channels);
141                     _glGetTexImage(target, level, format, type, pixels);
142             '''
143             self.emitFakeTexture2D()
144             print r'''
145                     free(pixels);
146                 }
147             '''
148
149
150 if __name__ == '__main__':
151     print
152     print '#include <stdlib.h>'
153     print '#include <string.h>'
154     print
155     print '#include <dlfcn.h>'
156     print
157     print '#include "trace_writer_local.hpp"'
158     print
159     print '// To validate our prototypes'
160     print '#define GL_GLEXT_PROTOTYPES'
161     print '#define GLX_GLXEXT_PROTOTYPES'
162     print
163     print '#include "glproc.hpp"'
164     print '#include "glsize.hpp"'
165     print
166
167     module = Module()
168     module.mergeModule(glxapi)
169     module.mergeModule(glapi)
170     api = API()
171     api.addModule(module)
172     tracer = GlxTracer()
173     tracer.traceApi(api)
174
175     print r'''
176
177
178 /*
179  * Invoke the true dlopen() function.
180  */
181 static void *_dlopen(const char *filename, int flag)
182 {
183     typedef void * (*PFN_DLOPEN)(const char *, int);
184     static PFN_DLOPEN dlopen_ptr = NULL;
185
186     if (!dlopen_ptr) {
187         dlopen_ptr = (PFN_DLOPEN)dlsym(RTLD_NEXT, "dlopen");
188         if (!dlopen_ptr) {
189             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
190             return NULL;
191         }
192     }
193
194     return dlopen_ptr(filename, flag);
195 }
196
197
198 /*
199  * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
200  * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
201  * we need to intercept the dlopen() call here, and redirect to our wrapper
202  * shared object.
203  */
204 extern "C" PUBLIC
205 void * dlopen(const char *filename, int flag)
206 {
207     void *handle;
208
209     handle = _dlopen(filename, flag);
210
211     const char * libgl_filename = getenv("TRACE_LIBGL");
212
213     if (filename && handle && !libgl_filename) {
214         if (0) {
215             os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
216         }
217
218         // FIXME: handle absolute paths and other versions
219         if (strcmp(filename, "libGL.so") == 0 ||
220             strcmp(filename, "libGL.so.1") == 0) {
221
222             // Use the true libGL.so handle instead of RTLD_NEXT from now on
223             _libGlHandle = handle;
224
225             // Get the file path for our shared object, and use it instead
226             static int dummy = 0xdeedbeef;
227             Dl_info info;
228             if (dladdr(&dummy, &info)) {
229                 os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
230                 handle = _dlopen(info.dli_fname, flag);
231             } else {
232                 os::log("apitrace: warning: dladdr() failed\n");
233             }
234         }
235     }
236
237     return handle;
238 }
239
240
241
242 '''