1 /*
2 ** Copyright 2007, The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17 #include <ctype.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <dlfcn.h>
23 #include <limits.h>
24
25 #include <cutils/log.h>
26
27 #include <EGL/egl.h>
28
29 #include "hooks.h"
30 #include "egl_impl.h"
31
32 #include "Loader.h"
33
34 // ----------------------------------------------------------------------------
35 namespace android {
36 // ----------------------------------------------------------------------------
37
38
39 /*
40 * EGL drivers are called
41 *
42 * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
43 *
44 */
45
ANDROID_SINGLETON_STATIC_INSTANCE(Loader)46 ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
47
48 // ----------------------------------------------------------------------------
49
50 Loader::driver_t::driver_t(void* gles)
51 {
52 dso[0] = gles;
53 for (size_t i=1 ; i<NELEM(dso) ; i++)
54 dso[i] = 0;
55 }
56
~driver_t()57 Loader::driver_t::~driver_t()
58 {
59 for (size_t i=0 ; i<NELEM(dso) ; i++) {
60 if (dso[i]) {
61 dlclose(dso[i]);
62 dso[i] = 0;
63 }
64 }
65 }
66
set(void * hnd,int32_t api)67 status_t Loader::driver_t::set(void* hnd, int32_t api)
68 {
69 switch (api) {
70 case EGL:
71 dso[0] = hnd;
72 break;
73 case GLESv1_CM:
74 dso[1] = hnd;
75 break;
76 case GLESv2:
77 dso[2] = hnd;
78 break;
79 default:
80 return BAD_INDEX;
81 }
82 return NO_ERROR;
83 }
84
85 // ----------------------------------------------------------------------------
86
entry_t(int dpy,int impl,const char * tag)87 Loader::entry_t::entry_t(int dpy, int impl, const char* tag)
88 : dpy(dpy), impl(impl), tag(tag) {
89 }
90
91 // ----------------------------------------------------------------------------
92
Loader()93 Loader::Loader()
94 {
95 char line[256];
96 char tag[256];
97 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
98 if (cfg == NULL) {
99 // default config
100 LOGD("egl.cfg not found, using default config");
101 gConfig.add( entry_t(0, 0, "android") );
102 } else {
103 while (fgets(line, 256, cfg)) {
104 int dpy;
105 int impl;
106 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
107 //LOGD(">>> %u %u %s", dpy, impl, tag);
108 gConfig.add( entry_t(dpy, impl, tag) );
109 }
110 }
111 fclose(cfg);
112 }
113 }
114
~Loader()115 Loader::~Loader()
116 {
117 }
118
getTag(int dpy,int impl)119 const char* Loader::getTag(int dpy, int impl)
120 {
121 const Vector<entry_t>& cfgs(gConfig);
122 const size_t c = cfgs.size();
123 for (size_t i=0 ; i<c ; i++) {
124 if (dpy == cfgs[i].dpy)
125 if (impl == cfgs[i].impl)
126 return cfgs[i].tag.string();
127 }
128 return 0;
129 }
130
open(EGLNativeDisplayType display,int impl,egl_connection_t * cnx)131 void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
132 {
133 /*
134 * TODO: if we don't find display/0, then use 0/0
135 * (0/0 should always work)
136 */
137
138 void* dso;
139 char path[PATH_MAX];
140 int index = int(display);
141 driver_t* hnd = 0;
142 const char* const format = "/system/lib/egl/lib%s_%s.so";
143
144 char const* tag = getTag(index, impl);
145 if (tag) {
146 snprintf(path, PATH_MAX, format, "GLES", tag);
147 dso = load_driver(path, cnx, EGL | GLESv1_CM | GLESv2);
148 if (dso) {
149 hnd = new driver_t(dso);
150 } else {
151 // Always load EGL first
152 snprintf(path, PATH_MAX, format, "EGL", tag);
153 dso = load_driver(path, cnx, EGL);
154 if (dso) {
155 hnd = new driver_t(dso);
156
157 // TODO: make this more automated
158 snprintf(path, PATH_MAX, format, "GLESv1_CM", tag);
159 hnd->set( load_driver(path, cnx, GLESv1_CM), GLESv1_CM );
160
161 snprintf(path, PATH_MAX, format, "GLESv2", tag);
162 hnd->set( load_driver(path, cnx, GLESv2), GLESv2 );
163 }
164 }
165 }
166
167 LOG_FATAL_IF(!index && !impl && !hnd,
168 "couldn't find the default OpenGL ES implementation "
169 "for default display");
170
171 return (void*)hnd;
172 }
173
close(void * driver)174 status_t Loader::close(void* driver)
175 {
176 driver_t* hnd = (driver_t*)driver;
177 delete hnd;
178 return NO_ERROR;
179 }
180
init_api(void * dso,char const * const * api,__eglMustCastToProperFunctionPointerType * curr,getProcAddressType getProcAddress)181 void Loader::init_api(void* dso,
182 char const * const * api,
183 __eglMustCastToProperFunctionPointerType* curr,
184 getProcAddressType getProcAddress)
185 {
186 char scrap[256];
187 while (*api) {
188 char const * name = *api;
189 __eglMustCastToProperFunctionPointerType f =
190 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
191 if (f == NULL) {
192 // couldn't find the entry-point, use eglGetProcAddress()
193 f = getProcAddress(name);
194 }
195 if (f == NULL) {
196 // Try without the OES postfix
197 ssize_t index = ssize_t(strlen(name)) - 3;
198 if ((index>0 && (index<255)) && (!strcmp(name+index, "OES"))) {
199 strncpy(scrap, name, index);
200 scrap[index] = 0;
201 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
202 //LOGD_IF(f, "found <%s> instead", scrap);
203 }
204 }
205 if (f == NULL) {
206 // Try with the OES postfix
207 ssize_t index = ssize_t(strlen(name)) - 3;
208 if ((index>0 && (index<252)) && (strcmp(name+index, "OES"))) {
209 strncpy(scrap, name, index);
210 scrap[index] = 0;
211 strcat(scrap, "OES");
212 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
213 //LOGD_IF(f, "found <%s> instead", scrap);
214 }
215 }
216 if (f == NULL) {
217 //LOGD("%s", name);
218 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
219 }
220 *curr++ = f;
221 api++;
222 }
223 }
224
load_driver(const char * driver_absolute_path,egl_connection_t * cnx,uint32_t mask)225 void *Loader::load_driver(const char* driver_absolute_path,
226 egl_connection_t* cnx, uint32_t mask)
227 {
228 if (access(driver_absolute_path, R_OK)) {
229 // this happens often, we don't want to log an error
230 return 0;
231 }
232
233 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
234 if (dso == 0) {
235 const char* err = dlerror();
236 LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
237 return 0;
238 }
239
240 LOGD("loaded %s", driver_absolute_path);
241
242 if (mask & EGL) {
243 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
244
245 LOGE_IF(!getProcAddress,
246 "can't find eglGetProcAddress() in %s", driver_absolute_path);
247
248 egl_t* egl = &cnx->egl;
249 __eglMustCastToProperFunctionPointerType* curr =
250 (__eglMustCastToProperFunctionPointerType*)egl;
251 char const * const * api = egl_names;
252 while (*api) {
253 char const * name = *api;
254 __eglMustCastToProperFunctionPointerType f =
255 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
256 if (f == NULL) {
257 // couldn't find the entry-point, use eglGetProcAddress()
258 f = getProcAddress(name);
259 if (f == NULL) {
260 f = (__eglMustCastToProperFunctionPointerType)0;
261 }
262 }
263 *curr++ = f;
264 api++;
265 }
266 }
267
268 if (mask & GLESv1_CM) {
269 init_api(dso, gl_names,
270 (__eglMustCastToProperFunctionPointerType*)
271 &cnx->hooks[GLESv1_INDEX]->gl,
272 getProcAddress);
273 }
274
275 if (mask & GLESv2) {
276 init_api(dso, gl_names,
277 (__eglMustCastToProperFunctionPointerType*)
278 &cnx->hooks[GLESv2_INDEX]->gl,
279 getProcAddress);
280 }
281
282 return dso;
283 }
284
285 // ----------------------------------------------------------------------------
286 }; // namespace android
287 // ----------------------------------------------------------------------------
288