]> git.cworth.org Git - apitrace/blob - dispatch/glproc_gl.cpp
Move dispatch to its own subdirectory.
[apitrace] / dispatch / glproc_gl.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include "glproc.hpp"
28
29
30 #if !defined(_WIN32)
31 #include <unistd.h> // for symlink
32 #include <dlfcn.h>
33 #endif
34
35
36 /*
37  * Handle to the true OpenGL library.
38  */
39 #if defined(_WIN32)
40 HINSTANCE __libGlHandle = NULL;
41 #else
42 void *__libGlHandle = NULL;
43 #endif
44
45
46
47 #if defined(_WIN32)
48
49 void *
50 __getPublicProcAddress(const char *procName)
51 {
52     if (!__libGlHandle) {
53         char szDll[MAX_PATH] = {0};
54         
55         if (!GetSystemDirectoryA(szDll, MAX_PATH)) {
56             return NULL;
57         }
58         
59         strcat(szDll, "\\\\opengl32.dll");
60         
61         __libGlHandle = LoadLibraryA(szDll);
62         if (!__libGlHandle) {
63             os::log("apitrace: error: couldn't load %s\n", szDll);
64             return NULL;
65         }
66     }
67         
68     return (void *)GetProcAddress(__libGlHandle, procName);
69 }
70
71
72 void *
73 __getPrivateProcAddress(const char *procName) {
74     return (void *)__wglGetProcAddress(procName);
75 }
76
77
78 #elif defined(__APPLE__)
79
80
81 /*
82  * Path to the true OpenGL framework
83  */
84 static const char *libgl_filename = "/System/Library/Frameworks/OpenGL.framework/OpenGL";
85
86
87 /*
88  * Lookup a libGL symbol
89  */
90 void * __libgl_sym(const char *symbol)
91 {
92     void *result;
93
94     if (!__libGlHandle) {
95         /* 
96          * Unfortunately we can't just dlopen the true dynamic library because
97          * DYLD_LIBRARY_PATH/DYLD_FRAMEWORK_PATH take precedence, even for
98          * absolute paths.  So we create a temporary symlink, and dlopen that
99          * instead.
100          */
101
102         char temp_filename[] = "/tmp/tmp.XXXXXX";
103
104         if (mktemp(temp_filename) != NULL) {
105             if (symlink(libgl_filename, temp_filename) == 0) {
106                 __libGlHandle = dlopen(temp_filename, RTLD_LOCAL | RTLD_NOW | RTLD_FIRST);
107                 remove(temp_filename);
108             }
109         }
110
111         if (!__libGlHandle) {
112             os::log("apitrace: error: couldn't load %s\n", libgl_filename);
113             os::abort();
114             return NULL;
115         }
116     }
117
118     result = dlsym(__libGlHandle, symbol);
119
120     if (resullt && result == dlsym(RTLD_SELF, symbol)) {
121         os::log("apitrace: error: symbol lookup recursion\n");
122         os::abort();
123         return NULL;
124     }
125
126     return result;
127 }
128
129
130 void *
131 __getPublicProcAddress(const char *procName)
132 {
133     return __libgl_sym(procName);
134 }
135
136 void *
137 __getPrivateProcAddress(const char *procName)
138 {
139     return __libgl_sym(procName);
140 }
141
142
143 #else
144
145
146 /*
147  * Invoke the true dlopen() function.
148  */
149 static void *
150 __dlopen(const char *filename, int flag)
151 {
152     typedef void * (*PFNDLOPEN)(const char *, int);
153     static PFNDLOPEN dlopen_ptr = NULL;
154
155     if (!dlopen_ptr) {
156         dlopen_ptr = (PFNDLOPEN)dlsym(RTLD_NEXT, "dlopen");
157         if (!dlopen_ptr) {
158             os::log("apitrace: error: dlsym(RTLD_NEXT, \"dlopen\") failed\n");
159             return NULL;
160         }
161     }
162
163     return dlopen_ptr(filename, flag);
164 }
165
166
167 /*
168  * Lookup a libGL symbol
169  */
170 void * __libgl_sym(const char *symbol)
171 {
172     void *result;
173
174     if (!__libGlHandle) {
175         /*
176          * The app doesn't directly link against libGL.so, nor does it directly
177          * dlopen it.  So we have to load it ourselves.
178          */
179
180         const char * libgl_filename = getenv("TRACE_LIBGL");
181
182         if (!libgl_filename) {
183             /*
184              * Try to use whatever libGL.so the library is linked against.
185              */
186
187             result = dlsym(RTLD_NEXT, symbol);
188             if (result) {
189                 __libGlHandle = RTLD_NEXT;
190                 return result;
191             }
192
193             libgl_filename = "libGL.so.1";
194         }
195
196         /*
197          * It would have been preferable to use RTLD_LOCAL to ensure that the
198          * application can never access libGL.so symbols directly, but this
199          * won't work, given libGL.so often loads a driver specific SO and
200          * exposes symbols to it.
201          */
202
203         __libGlHandle = __dlopen(libgl_filename, RTLD_GLOBAL | RTLD_LAZY);
204         if (!__libGlHandle) {
205             os::log("apitrace: error: couldn't find libGL.so\n");
206             return NULL;
207         }
208     }
209
210     return dlsym(__libGlHandle, symbol);
211 }
212
213
214 void *
215 __getPublicProcAddress(const char *procName)
216 {
217     return __libgl_sym(procName);
218 }
219
220 void *
221 __getPrivateProcAddress(const char *procName)
222 {
223     return (void *)__glXGetProcAddressARB((const GLubyte *)procName);
224 }
225
226
227 #endif 
228