/* ** Copyright 2007, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** ** http://www.apache.org/licenses/LICENSE-2.0 ** ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "../glestrace.h" #include "egldefs.h" #include "Loader.h" // ---------------------------------------------------------------------------- namespace android { // ---------------------------------------------------------------------------- /* * EGL userspace drivers must be provided either: * - as a single library: * /vendor/lib/egl/libGLES.so * * - as separate libraries: * /vendor/lib/egl/libEGL.so * /vendor/lib/egl/libGLESv1_CM.so * /vendor/lib/egl/libGLESv2.so * * The software renderer for the emulator must be provided as a single * library at: * * /system/lib/egl/libGLES_android.so * * * For backward compatibility and to facilitate the transition to * this new naming scheme, the loader will additionally look for: * * /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so * */ ANDROID_SINGLETON_STATIC_INSTANCE( Loader ) /* This function is called to check whether we run inside the emulator, * and if this is the case whether GLES GPU emulation is supported. * * Returned values are: * -1 -> not running inside the emulator * 0 -> running inside the emulator, but GPU emulation not supported * 1 -> running inside the emulator, GPU emulation is supported * through the "emulation" config. */ static int checkGlesEmulationStatus(void) { /* We're going to check for the following kernel parameters: * * qemu=1 -> tells us that we run inside the emulator * android.qemu.gles= -> tells us the GLES GPU emulation status * * Note that we will return if we find it. This let us support * more additionnal emulation modes in the future. */ char prop[PROPERTY_VALUE_MAX]; int result = -1; /* Check first if the device does not support hardware rendering */ property_get("ro.libagl",prop,"0"); if(atoi(prop) == 1) return 0; /* Then, check for qemu=1 */ property_get("ro.kernel.qemu",prop,"0"); if (atoi(prop) != 1) return -1; /* We are in the emulator, get GPU status value */ property_get("ro.kernel.qemu.gles",prop,"0"); return atoi(prop); } // ---------------------------------------------------------------------------- static char const * getProcessCmdline() { long pid = getpid(); char procPath[128]; snprintf(procPath, 128, "/proc/%ld/cmdline", pid); FILE * file = fopen(procPath, "r"); if (file) { static char cmdline[256]; char *str = fgets(cmdline, sizeof(cmdline) - 1, file); fclose(file); if (str) { return cmdline; } } return NULL; } // ---------------------------------------------------------------------------- /* Gets a system-defined per-application path to a GLES implementation. * Used when running multiple (usually software) renderers on a single device. * Requires ro.zygote.disable_gl_preload to be enabled! * * It looks for libraries in the filesystem in this preferred order: * /data/data//os_override/libGLES_impl.so * /system/etc/os_override//libGLES_impl.so * * Usually, libGLES_impl.so would be a symlink to a GLES implementation. * * MatchFile::find() does the heavy lifting of using the value. * As of writing it searches for the value '/system/lib/libGLES_mesa.so' * in this order: * * /system/lib/libGLES_mesa.so/lib%s.so * /system/lib/libGLES_mesa.so/lib%s_*.so * /system/lib/libGLES_mesa.so * * where %s is determined at runtime to be GLES, EGL, GLESv1_CM, or GLESv2. */ static const char* getOverridePath(void) { ALOGD("UID:%d EUID:%d - %s", getuid(), geteuid(), getProcessCmdline()); // only really useful if ro.zygote.disable_gl_preload is enabled // otherwise overrides only happen once, to zygote if (!property_get_bool("ro.zygote.disable_gl_preload", 0)) { ALOGD("ro.zygote.disable_gl_preload not set," " zygote's libGLES preloaded for every process"); } String8 data_override_path("/data/data/"); data_override_path.appendFormat("%s", getProcessCmdline()); data_override_path.appendFormat("%s", "/libGLES_mesa"); if (!access( data_override_path.string(), F_OK ) ) { ALOGD("override found: %s", data_override_path.string()); return "/system/lib/egl/libGLES_mesa.so"; } data_override_path.setTo("/data/data/"); data_override_path.appendFormat("%s", getProcessCmdline()); data_override_path.appendFormat("%s", "/libGLES_android"); if (!access( data_override_path.string(), F_OK ) ) { ALOGD("override found: %s", data_override_path.string()); return "/system/lib/egl/libGLES_android.so"; } String8 system_override_path("/system/etc/libGLES_mesa/"); system_override_path.appendFormat("%s", getProcessCmdline()); if (!access( system_override_path.string(), F_OK ) ) { ALOGD("override found: %s", system_override_path.string()); return "/system/lib/egl/libGLES_mesa.so"; } system_override_path.setTo("/system/etc/libGLES_android/"); system_override_path.appendFormat("%s", getProcessCmdline()); if (!access( system_override_path.string(), F_OK ) ) { ALOGD("override found: %s", system_override_path.string()); return "/system/lib/egl/libGLES_android.so"; } ALOGD("no EGL override found, default libGLES_mesa.so"); return "/system/lib/egl/libGLES_mesa.so"; } // ---------------------------------------------------------------------------- Loader::driver_t::driver_t(void* gles) { dso[0] = gles; for (size_t i=1 ; iset( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM ); hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 ); } } LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation"); #if defined(__LP64__) cnx->libEgl = load_wrapper("/system/lib64/libEGL.so"); cnx->libGles2 = load_wrapper("/system/lib64/libGLESv2.so"); cnx->libGles1 = load_wrapper("/system/lib64/libGLESv1_CM.so"); #else cnx->libEgl = load_wrapper("/system/lib/libEGL.so"); cnx->libGles2 = load_wrapper("/system/lib/libGLESv2.so"); cnx->libGles1 = load_wrapper("/system/lib/libGLESv1_CM.so"); #endif LOG_ALWAYS_FATAL_IF(!cnx->libEgl, "couldn't load system EGL wrapper libraries"); LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1, "couldn't load system OpenGL ES wrapper libraries"); return (void*)hnd; } status_t Loader::close(void* driver) { driver_t* hnd = (driver_t*)driver; delete hnd; return NO_ERROR; } void Loader::init_api(void* dso, char const * const * api, __eglMustCastToProperFunctionPointerType* curr, getProcAddressType getProcAddress) { const ssize_t SIZE = 256; char scrap[SIZE]; while (*api) { char const * name = *api; __eglMustCastToProperFunctionPointerType f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); if (f == NULL) { // couldn't find the entry-point, use eglGetProcAddress() f = getProcAddress(name); } if (f == NULL) { // Try without the OES postfix ssize_t index = ssize_t(strlen(name)) - 3; if ((index>0 && (index instead", scrap); } } if (f == NULL) { // Try with the OES postfix ssize_t index = ssize_t(strlen(name)) - 3; if (index>0 && strcmp(name+index, "OES")) { snprintf(scrap, SIZE, "%sOES", name); f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap); //ALOGD_IF(f, "found <%s> instead", scrap); } } if (f == NULL) { //ALOGD("%s", name); f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented; /* * GL_EXT_debug_label is special, we always report it as * supported, it's handled by GLES_trace. If GLES_trace is not * enabled, then these are no-ops. */ if (!strcmp(name, "glInsertEventMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } else if (!strcmp(name, "glPushGroupMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } else if (!strcmp(name, "glPopGroupMarkerEXT")) { f = (__eglMustCastToProperFunctionPointerType)gl_noop; } } *curr++ = f; api++; } } void *Loader::load_driver(const char* kind, egl_connection_t* cnx, uint32_t mask) { class MatchFile { public: static String8 find(const char* kind) { String8 result; // check for overrides first const char* overridePath = getOverridePath(); if (overridePath) { result.setTo(overridePath); return result; } // in the emulator case, we just return the hardcoded name // of the software renderer. if (checkGlesEmulationStatus() == 0) { ALOGD("Emulator without GPU support detected. " "Fallback to software renderer."); #if defined(__LP64__) result.setTo("/system/lib64/egl/libGLES_android.so"); #else result.setTo("/system/lib/egl/libGLES_android.so"); #endif return result; } const char* const searchPaths[] = { #if defined(__LP64__) "/vendor/lib64/egl", "/system/lib64/egl" #else "/vendor/lib/egl", "/system/lib/egl" #endif }; for (size_t i=0 ; id_type == DT_DIR) { continue; } if (!strcmp(e->d_name, "libGLES_android.so")) { // always skip the software renderer continue; } if (strstr(e->d_name, pattern.string()) == e->d_name) { if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) { result.clear(); result.appendFormat("%s/%s", search, e->d_name); closedir(d); return true; } } } closedir(d); } return false; } }; String8 absolutePath = MatchFile::find(kind); if (absolutePath.isEmpty()) { // this happens often, we don't want to log an error return 0; } const char* const driver_absolute_path = absolutePath.string(); void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL); if (dso == 0) { const char* err = dlerror(); ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown"); return 0; } ALOGD("loaded %s", driver_absolute_path); if (mask & EGL) { getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress"); ALOGE_IF(!getProcAddress, "can't find eglGetProcAddress() in %s", driver_absolute_path); egl_t* egl = &cnx->egl; __eglMustCastToProperFunctionPointerType* curr = (__eglMustCastToProperFunctionPointerType*)egl; char const * const * api = egl_names; while (*api) { char const * name = *api; __eglMustCastToProperFunctionPointerType f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, name); if (f == NULL) { // couldn't find the entry-point, use eglGetProcAddress() f = getProcAddress(name); if (f == NULL) { f = (__eglMustCastToProperFunctionPointerType)0; } } *curr++ = f; api++; } } if (mask & GLESv1_CM) { init_api(dso, gl_names, (__eglMustCastToProperFunctionPointerType*) &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl, getProcAddress); } if (mask & GLESv2) { init_api(dso, gl_names, (__eglMustCastToProperFunctionPointerType*) &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl, getProcAddress); } return dso; } // ---------------------------------------------------------------------------- }; // namespace android // ----------------------------------------------------------------------------