]> git.cworth.org Git - apitrace/blob - dispatch/glproc_egl.cpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / dispatch / glproc_egl.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 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE // for dladdr
33 #endif
34 #include <dlfcn.h>
35 #endif
36
37
38 /*
39  * Handle to the true OpenGL library.
40  * XXX: Not really used yet.
41  */
42 #if defined(_WIN32)
43 HMODULE _libGlHandle = NULL;
44 #else
45 void *_libGlHandle = NULL;
46 #endif
47
48
49
50 #if defined(_WIN32)
51
52 #error Unsupported
53
54 #elif defined(__APPLE__)
55
56 #error Unsupported
57
58 #else
59
60 /*
61  * Lookup a public EGL/GL/GLES symbol
62  *
63  * The spec states that eglGetProcAddress should only be used for non-core
64  * (extensions) entry-points.  Core entry-points should be taken directly from
65  * the API specific libraries.
66  *
67  * We cannot tell here which API a symbol is meant for here (as some are
68  * exported by many).  So this code assumes that the appropriate shared
69  * libraries have been loaded previously (either dlopened with RTLD_GLOBAL, or
70  * as part of the executable dependencies), and that their symbols available
71  * for quering via dlsym(RTLD_NEXT, ...).
72  */
73 void *
74 _getPublicProcAddress(const char *procName)
75 {
76 #if defined(ANDROID)
77     /*
78      * Android does not support LD_PRELOAD.  It is assumed that applications
79      * are explicitely loading egltrace.so.
80      */
81
82     if (procName[0] == 'e' && procName[1] == 'g' && procName[2] == 'l') {
83         static void *libEGL = NULL;
84         if (!libEGL) {
85             libEGL = dlopen("libEGL.so", RTLD_LOCAL | RTLD_LAZY);
86             if (!libEGL) {
87                 return NULL;
88             }
89         }
90         return dlsym(libEGL, procName);
91     }
92
93     if (procName[0] == 'g' && procName[1] == 'l') {
94         /* TODO: Use GLESv1/GLESv2 on a per-context basis. */
95         static void *sym = NULL;
96
97         static void *libGLESv2 = NULL;
98         if (!libGLESv2) {
99             libGLESv2 = dlopen("libGLESv2.so", RTLD_LOCAL | RTLD_LAZY);
100         }
101         if (libGLESv2) {
102             sym = dlsym(libGLESv2, procName);
103         }
104         if (sym) {
105             return sym;
106         }
107
108         static void *libGLESv1 = NULL;
109         if (!libGLESv1) {
110             libGLESv1 = dlopen("libGLESv1_CM.so", RTLD_LOCAL | RTLD_LAZY);
111         }
112         if (libGLESv1) {
113             sym = dlsym(libGLESv1, procName);
114         }
115         if (sym) {
116             return sym;
117         }
118     }
119
120     return NULL;
121 #else
122     return dlsym(RTLD_NEXT, procName);
123 #endif
124 }
125
126 /*
127  * Lookup a private EGL/GL/GLES symbol
128  *
129  * Private symbols should always be available through eglGetProcAddress, and
130  * they are guaranteed to work with any context bound (regardless of the API).
131  *
132  * However, per issue#57, eglGetProcAddress returns garbage on some
133  * implementations, and the spec states that implementations may choose to also
134  * export extension functions publicly, so we always attempt dlsym before
135  * eglGetProcAddress to mitigate that.
136  */
137 void *
138 _getPrivateProcAddress(const char *procName)
139 {
140     void *proc;
141     proc = _getPublicProcAddress(procName);
142     if (!proc &&
143         ((procName[0] == 'e' && procName[1] == 'g' && procName[2] == 'l') ||
144          (procName[0] == 'g' && procName[1] == 'l'))) {
145         proc = (void *) _eglGetProcAddress(procName);
146     }
147
148     return proc;
149 }
150
151 #endif