1 ##########################################################################
3 # Copyright 2011 Jose Fonseca
4 # Copyright 2008-2010 VMware, Inc.
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:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
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
25 ##########################################################################/
28 """GLX tracing generator."""
31 from gltrace import GlTracer
32 from specs.stdapi import Module, API
33 from specs.glapi import glapi
34 from specs.glxapi import glxapi
37 class GlxTracer(GlTracer):
39 def isFunctionPublic(self, function):
40 # The symbols visible in libGL.so can vary, so expose them all
43 getProcAddressFunctionNames = [
45 "glXGetProcAddressARB",
48 createContextFunctionNames = [
50 'glXCreateContextAttribsARB',
51 'glXCreateContextWithConfigSGIX',
52 'glXCreateNewContext',
55 destroyContextFunctionNames = [
59 makeCurrentFunctionNames = [
61 'glXMakeContextCurrent',
62 'glXMakeCurrentReadSGI',
65 def traceFunctionImplBody(self, function):
66 if function.name in self.destroyContextFunctionNames:
67 print ' gltrace::releaseContext((uintptr_t)ctx);'
69 GlTracer.traceFunctionImplBody(self, function)
71 if function.name in self.createContextFunctionNames:
72 print ' if (_result != NULL)'
73 print ' gltrace::createContext((uintptr_t)_result);'
75 if function.name in self.makeCurrentFunctionNames:
76 print ' if (_result) {'
77 print ' if (ctx != NULL)'
78 print ' gltrace::setContext((uintptr_t)ctx);'
80 print ' gltrace::clearContext();'
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
91 unsigned glx_target = 0;
92 _glXQueryDrawable(display, drawable, GLX_TEXTURE_TARGET_EXT, &glx_target);
96 //case GLX_TEXTURE_1D_EXT:
97 // target = GL_TEXTURE_1D;
99 case GLX_TEXTURE_2D_EXT:
100 target = GL_TEXTURE_2D;
102 case GLX_TEXTURE_RECTANGLE_EXT:
103 target = GL_TEXTURE_RECTANGLE;
106 os::log("apitrace: warning: %s: unsupported GLX_TEXTURE_TARGET_EXT 0x%u\n", __FUNCTION__, glx_target);
111 GLint internalformat = GL_NONE;
112 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
113 // XXX: GL_TEXTURE_INTERNAL_FORMAT cannot be trusted on NVIDIA
114 // -- it sometimes returns GL_BGRA, even though GL_BGR/BGRA is
115 // not a valid internal format.
116 switch (internalformat) {
118 internalformat = GL_RGB;
121 internalformat = GL_RGBA;
125 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_WIDTH, &width);
127 _glGetTexLevelParameteriv(target, level, GL_TEXTURE_HEIGHT, &height);
129 // XXX: We always use GL_RGBA format to read the pixels because:
130 // - some implementations (Mesa) seem to return bogus results
131 // for GLX_TEXTURE_FORMAT_EXT
132 // - hardware usually stores GL_RGB with 32bpp, so it should be
133 // faster to read/write
134 // - it is more robust against GL_(UN)PACK_ALIGNMENT state
136 // The drawback is that traces will be slightly bigger.
137 GLenum format = GL_RGBA;
138 GLenum type = GL_UNSIGNED_BYTE;
139 if (target && internalformat && height && width) {
140 // FIXME: This assumes (UN)PACK state (in particular
141 // GL_(UN)PACK_ROW_LENGTH) is set to its defaults. We
142 // really should temporarily reset the state here (and emit
143 // according fake calls) to cope when its not. At very
144 // least we need a heads up warning that this will cause
147 GLint row_stride = _align(width * 4, alignment);
148 GLvoid * pixels = malloc(height * row_stride);
149 _glGetTexImage(target, level, format, type, pixels);
151 self.emitFakeTexture2D()
158 if __name__ == '__main__':
160 print '#include <stdlib.h>'
161 print '#include <string.h>'
163 print '#include "trace_writer_local.hpp"'
165 print '// To validate our prototypes'
166 print '#define GL_GLEXT_PROTOTYPES'
167 print '#define GLX_GLXEXT_PROTOTYPES'
169 print '#include "dlopen.hpp"'
170 print '#include "glproc.hpp"'
171 print '#include "glsize.hpp"'
175 module.mergeModule(glxapi)
176 module.mergeModule(glapi)
178 api.addModule(module)
186 * Several applications, such as Quake3, use dlopen("libGL.so.1"), but
187 * LD_PRELOAD does not intercept symbols obtained via dlopen/dlsym, therefore
188 * we need to intercept the dlopen() call here, and redirect to our wrapper
192 void * dlopen(const char *filename, int flag)
196 handle = _dlopen(filename, flag);
198 const char * libgl_filename = getenv("TRACE_LIBGL");
200 if (filename && handle && !libgl_filename) {
202 os::log("apitrace: warning: dlopen(\"%s\", 0x%x)\n", filename, flag);
205 // FIXME: handle absolute paths and other versions
206 if (strcmp(filename, "libGL.so") == 0 ||
207 strcmp(filename, "libGL.so.1") == 0) {
209 // Use the true libGL.so handle instead of RTLD_NEXT from now on
210 _libGlHandle = handle;
212 // Get the file path for our shared object, and use it instead
213 static int dummy = 0xdeedbeef;
215 if (dladdr(&dummy, &info)) {
216 os::log("apitrace: redirecting dlopen(\"%s\", 0x%x)\n", filename, flag);
217 handle = _dlopen(info.dli_fname, flag);
219 os::log("apitrace: warning: dladdr() failed\n");