blob: e528831703197f6c8de66418fd8685c0bb86caa6 [file] [log] [blame]
Jesse Hall94cdba92013-07-11 09:40:35 -07001/*
Mathias Agopiande586972009-05-28 17:39:03 -07002 ** Copyright 2007, The Android Open Source Project
3 **
Jesse Hall94cdba92013-07-11 09:40:35 -07004 ** 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
Mathias Agopiande586972009-05-28 17:39:03 -07007 **
Jesse Hall94cdba92013-07-11 09:40:35 -07008 ** http://www.apache.org/licenses/LICENSE-2.0
Mathias Agopiande586972009-05-28 17:39:03 -07009 **
Jesse Hall94cdba92013-07-11 09:40:35 -070010 ** 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
Mathias Agopiande586972009-05-28 17:39:03 -070014 ** 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>
Mathias Agopian99381422013-04-23 20:52:29 +020024#include <dirent.h>
Mathias Agopiande586972009-05-28 17:39:03 -070025
26#include <cutils/log.h>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020027#include <cutils/properties.h>
Mathias Agopiande586972009-05-28 17:39:03 -070028
29#include <EGL/egl.h>
30
Mathias Agopian39c24a22013-04-04 23:17:56 -070031#include "../glestrace.h"
32
Mathias Agopian1cadb252011-05-23 17:26:14 -070033#include "egldefs.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070034#include "Loader.h"
Mathias Agopiande586972009-05-28 17:39:03 -070035
36// ----------------------------------------------------------------------------
37namespace android {
38// ----------------------------------------------------------------------------
39
40
41/*
Mathias Agopian99381422013-04-23 20:52:29 +020042 * EGL userspace drivers must be provided either:
43 * - as a single library:
44 * /vendor/lib/egl/libGLES.so
45 *
46 * - as separate libraries:
47 * /vendor/lib/egl/libEGL.so
48 * /vendor/lib/egl/libGLESv1_CM.so
49 * /vendor/lib/egl/libGLESv2.so
50 *
51 * The software renderer for the emulator must be provided as a single
52 * library at:
53 *
54 * /system/lib/egl/libGLES_android.so
55 *
56 *
57 * For backward compatibility and to facilitate the transition to
58 * this new naming scheme, the loader will additionally look for:
Jesse Hall94cdba92013-07-11 09:40:35 -070059 *
Mathias Agopian99381422013-04-23 20:52:29 +020060 * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
Jesse Hall94cdba92013-07-11 09:40:35 -070061 *
Mathias Agopiande586972009-05-28 17:39:03 -070062 */
63
64ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
65
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020066/* This function is called to check whether we run inside the emulator,
67 * and if this is the case whether GLES GPU emulation is supported.
68 *
69 * Returned values are:
70 * -1 -> not running inside the emulator
71 * 0 -> running inside the emulator, but GPU emulation not supported
72 * 1 -> running inside the emulator, GPU emulation is supported
73 * through the "emulation" config.
74 */
75static int
76checkGlesEmulationStatus(void)
77{
78 /* We're going to check for the following kernel parameters:
79 *
80 * qemu=1 -> tells us that we run inside the emulator
81 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
82 *
83 * Note that we will return <number> if we find it. This let us support
84 * more additionnal emulation modes in the future.
85 */
86 char prop[PROPERTY_VALUE_MAX];
87 int result = -1;
88
89 /* First, check for qemu=1 */
90 property_get("ro.kernel.qemu",prop,"0");
91 if (atoi(prop) != 1)
92 return -1;
93
94 /* We are in the emulator, get GPU status value */
95 property_get("ro.kernel.qemu.gles",prop,"0");
96 return atoi(prop);
97}
98
Mathias Agopiande586972009-05-28 17:39:03 -070099// ----------------------------------------------------------------------------
100
Mathias Agopiand75f84d2012-06-05 21:44:43 -0700101static char const * getProcessCmdline() {
102 long pid = getpid();
103 char procPath[128];
104 snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
105 FILE * file = fopen(procPath, "r");
106 if (file) {
107 static char cmdline[256];
108 char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
109 fclose(file);
110 if (str) {
111 return cmdline;
112 }
113 }
114 return NULL;
115}
116
117// ----------------------------------------------------------------------------
118
Jesse Hall94cdba92013-07-11 09:40:35 -0700119Loader::driver_t::driver_t(void* gles)
Mathias Agopiande586972009-05-28 17:39:03 -0700120{
121 dso[0] = gles;
122 for (size_t i=1 ; i<NELEM(dso) ; i++)
123 dso[i] = 0;
124}
125
Jesse Hall94cdba92013-07-11 09:40:35 -0700126Loader::driver_t::~driver_t()
Mathias Agopiande586972009-05-28 17:39:03 -0700127{
128 for (size_t i=0 ; i<NELEM(dso) ; i++) {
129 if (dso[i]) {
130 dlclose(dso[i]);
131 dso[i] = 0;
132 }
133 }
134}
135
136status_t Loader::driver_t::set(void* hnd, int32_t api)
137{
138 switch (api) {
139 case EGL:
140 dso[0] = hnd;
141 break;
142 case GLESv1_CM:
143 dso[1] = hnd;
144 break;
145 case GLESv2:
146 dso[2] = hnd;
147 break;
148 default:
149 return BAD_INDEX;
150 }
151 return NO_ERROR;
152}
153
154// ----------------------------------------------------------------------------
155
Mathias Agopiande586972009-05-28 17:39:03 -0700156Loader::Loader()
Mathias Agopian99381422013-04-23 20:52:29 +0200157 : getProcAddress(NULL) {
Mathias Agopiande586972009-05-28 17:39:03 -0700158}
159
Mathias Agopian99381422013-04-23 20:52:29 +0200160Loader::~Loader() {
Siva Velusamy0469dd62011-11-30 15:05:37 -0800161 GLTrace_stop();
Mathias Agopiande586972009-05-28 17:39:03 -0700162}
163
Jesse Hallc07b5202013-07-04 12:08:16 -0700164static void* load_wrapper(const char* path) {
165 void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
166 ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
167 return so;
168}
169
Mathias Agopianada798b2012-02-13 17:09:30 -0800170void* Loader::open(egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700171{
Mathias Agopiande586972009-05-28 17:39:03 -0700172 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700173 driver_t* hnd = 0;
Jesse Hall94cdba92013-07-11 09:40:35 -0700174
Mathias Agopian99381422013-04-23 20:52:29 +0200175 dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
176 if (dso) {
177 hnd = new driver_t(dso);
178 } else {
179 // Always load EGL first
180 dso = load_driver("EGL", cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700181 if (dso) {
182 hnd = new driver_t(dso);
Mathias Agopian99381422013-04-23 20:52:29 +0200183 hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
184 hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700185 }
186 }
187
Mathias Agopian99381422013-04-23 20:52:29 +0200188 LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
Jesse Hallc07b5202013-07-04 12:08:16 -0700189
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800190#if defined(__LP64__)
191 cnx->libGles2 = load_wrapper("/system/lib64/libGLESv2.so");
192 cnx->libGles1 = load_wrapper("/system/lib64/libGLESv1_CM.so");
193#else
Jesse Hall94cdba92013-07-11 09:40:35 -0700194 cnx->libGles2 = load_wrapper("/system/lib/libGLESv2.so");
195 cnx->libGles1 = load_wrapper("/system/lib/libGLESv1_CM.so");
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800196#endif
Jesse Hallc07b5202013-07-04 12:08:16 -0700197 LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
198 "couldn't load system OpenGL ES wrapper libraries");
199
Mathias Agopiande586972009-05-28 17:39:03 -0700200 return (void*)hnd;
201}
202
203status_t Loader::close(void* driver)
204{
205 driver_t* hnd = (driver_t*)driver;
206 delete hnd;
207 return NO_ERROR;
208}
209
Jesse Hall94cdba92013-07-11 09:40:35 -0700210void Loader::init_api(void* dso,
211 char const * const * api,
212 __eglMustCastToProperFunctionPointerType* curr,
213 getProcAddressType getProcAddress)
Mathias Agopiande586972009-05-28 17:39:03 -0700214{
Mathias Agopian7773c432012-02-13 20:06:08 -0800215 const ssize_t SIZE = 256;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700216 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700217 while (*api) {
218 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700219 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700220 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
221 if (f == NULL) {
222 // couldn't find the entry-point, use eglGetProcAddress()
223 f = getProcAddress(name);
224 }
225 if (f == NULL) {
226 // Try without the OES postfix
227 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700228 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700229 strncpy(scrap, name, index);
230 scrap[index] = 0;
231 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000232 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700233 }
234 }
235 if (f == NULL) {
236 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700237 ssize_t index = ssize_t(strlen(name)) - 3;
238 if (index>0 && strcmp(name+index, "OES")) {
239 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700240 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000241 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700242 }
243 }
244 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000245 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700246 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
Mathias Agopian48d438d2012-01-28 21:44:00 -0800247
248 /*
249 * GL_EXT_debug_label is special, we always report it as
250 * supported, it's handled by GLES_trace. If GLES_trace is not
251 * enabled, then these are no-ops.
252 */
253 if (!strcmp(name, "glInsertEventMarkerEXT")) {
254 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
255 } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
256 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
257 } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
258 f = (__eglMustCastToProperFunctionPointerType)gl_noop;
259 }
Mathias Agopiande586972009-05-28 17:39:03 -0700260 }
261 *curr++ = f;
262 api++;
263 }
264}
265
Mathias Agopian99381422013-04-23 20:52:29 +0200266void *Loader::load_driver(const char* kind,
Mathias Agopian618fa102009-10-14 02:06:37 -0700267 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700268{
Mathias Agopian99381422013-04-23 20:52:29 +0200269 class MatchFile {
270 public:
271 static String8 find(const char* kind) {
272 String8 result;
273 String8 pattern;
274 pattern.appendFormat("lib%s", kind);
275 const char* const searchPaths[] = {
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800276#if defined(__LP64__)
277 "/vendor/lib64/egl",
278 "/system/lib64/egl"
279#else
Mathias Agopian99381422013-04-23 20:52:29 +0200280 "/vendor/lib/egl",
281 "/system/lib/egl"
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800282#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200283 };
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700284
Mathias Agopian99381422013-04-23 20:52:29 +0200285 // first, we search for the exact name of the GLES userspace
286 // driver in both locations.
287 // i.e.:
288 // libGLES.so, or:
289 // libEGL.so, libGLESv1_CM.so, libGLESv2.so
290
291 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
292 if (find(result, pattern, searchPaths[i], true)) {
293 return result;
294 }
295 }
296
297 // for compatibility with the old "egl.cfg" naming convention
298 // we look for files that match:
299 // libGLES_*.so, or:
300 // libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
301
302 pattern.append("_");
303 for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
304 if (find(result, pattern, searchPaths[i], false)) {
305 return result;
306 }
307 }
308
309 // we didn't find the driver. gah.
310 result.clear();
311 return result;
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700312 }
Mathias Agopian99381422013-04-23 20:52:29 +0200313
314 private:
315 static bool find(String8& result,
316 const String8& pattern, const char* const search, bool exact) {
317
318 // in the emulator case, we just return the hardcoded name
319 // of the software renderer.
320 if (checkGlesEmulationStatus() == 0) {
321 ALOGD("Emulator without GPU support detected. "
322 "Fallback to software renderer.");
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800323#if defined(__LP64__)
324 result.setTo("/system/lib64/egl/libGLES_android.so");
325#else
Mathias Agopian99381422013-04-23 20:52:29 +0200326 result.setTo("/system/lib/egl/libGLES_android.so");
Dan Willemsen8edb8f52014-02-16 10:23:54 -0800327#endif
Mathias Agopian99381422013-04-23 20:52:29 +0200328 return true;
329 }
330
331 if (exact) {
332 String8 absolutePath;
333 absolutePath.appendFormat("%s/%s.so", search, pattern.string());
334 if (!access(absolutePath.string(), R_OK)) {
335 result = absolutePath;
336 return true;
337 }
338 return false;
339 }
340
341 DIR* d = opendir(search);
342 if (d != NULL) {
343 struct dirent cur;
344 struct dirent* e;
345 while (readdir_r(d, &cur, &e) == 0 && e) {
346 if (e->d_type == DT_DIR) {
347 continue;
348 }
349 if (!strcmp(e->d_name, "libGLES_android.so")) {
350 // always skip the software renderer
351 continue;
352 }
353 if (strstr(e->d_name, pattern.string()) == e->d_name) {
354 if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
355 result.clear();
356 result.appendFormat("%s/%s", search, e->d_name);
357 closedir(d);
358 return true;
359 }
360 }
361 }
362 closedir(d);
363 }
364 return false;
365 }
366 };
367
368
369 String8 absolutePath = MatchFile::find(kind);
370 if (absolutePath.isEmpty()) {
371 // this happens often, we don't want to log an error
372 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700373 }
Mathias Agopian99381422013-04-23 20:52:29 +0200374 const char* const driver_absolute_path = absolutePath.string();
Mathias Agopiande586972009-05-28 17:39:03 -0700375
Mathias Agopian8c173842009-09-20 16:01:02 -0700376 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
377 if (dso == 0) {
378 const char* err = dlerror();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000379 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700380 return 0;
381 }
382
Steve Block9d453682011-12-20 16:23:08 +0000383 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700384
Mathias Agopiande586972009-05-28 17:39:03 -0700385 if (mask & EGL) {
386 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
387
Jesse Hall94cdba92013-07-11 09:40:35 -0700388 ALOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700389 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700390
Mathias Agopian618fa102009-10-14 02:06:37 -0700391 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700392 __eglMustCastToProperFunctionPointerType* curr =
393 (__eglMustCastToProperFunctionPointerType*)egl;
394 char const * const * api = egl_names;
395 while (*api) {
396 char const * name = *api;
Jesse Hall94cdba92013-07-11 09:40:35 -0700397 __eglMustCastToProperFunctionPointerType f =
Mathias Agopiande586972009-05-28 17:39:03 -0700398 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
399 if (f == NULL) {
400 // couldn't find the entry-point, use eglGetProcAddress()
401 f = getProcAddress(name);
402 if (f == NULL) {
403 f = (__eglMustCastToProperFunctionPointerType)0;
404 }
405 }
406 *curr++ = f;
407 api++;
408 }
409 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700410
Mathias Agopiande586972009-05-28 17:39:03 -0700411 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700412 init_api(dso, gl_names,
413 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800414 &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
Mathias Agopian618fa102009-10-14 02:06:37 -0700415 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700416 }
417
418 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700419 init_api(dso, gl_names,
420 (__eglMustCastToProperFunctionPointerType*)
Mathias Agopian7773c432012-02-13 20:06:08 -0800421 &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700422 getProcAddress);
423 }
Jesse Hall94cdba92013-07-11 09:40:35 -0700424
Mathias Agopiande586972009-05-28 17:39:03 -0700425 return dso;
426}
427
428// ----------------------------------------------------------------------------
429}; // namespace android
430// ----------------------------------------------------------------------------