2013-07-11 16:40:35 +00:00
|
|
|
/*
|
2009-05-29 00:39:03 +00:00
|
|
|
** Copyright 2007, The Android Open Source Project
|
|
|
|
**
|
2013-07-11 16:40:35 +00:00
|
|
|
** 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
|
2009-05-29 00:39:03 +00:00
|
|
|
**
|
2013-07-11 16:40:35 +00:00
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
2009-05-29 00:39:03 +00:00
|
|
|
**
|
2013-07-11 16:40:35 +00:00
|
|
|
** 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
|
2009-05-29 00:39:03 +00:00
|
|
|
** limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <limits.h>
|
2013-04-23 18:52:29 +00:00
|
|
|
#include <dirent.h>
|
2009-05-29 00:39:03 +00:00
|
|
|
|
|
|
|
#include <cutils/log.h>
|
2011-08-26 15:38:47 +00:00
|
|
|
#include <cutils/properties.h>
|
2009-05-29 00:39:03 +00:00
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
|
2013-04-05 06:17:56 +00:00
|
|
|
#include "../glestrace.h"
|
|
|
|
|
2011-05-24 00:26:14 +00:00
|
|
|
#include "egldefs.h"
|
2009-05-29 00:39:03 +00:00
|
|
|
#include "Loader.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2013-04-23 18:52:29 +00:00
|
|
|
* 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:
|
2013-07-11 16:40:35 +00:00
|
|
|
*
|
2013-04-23 18:52:29 +00:00
|
|
|
* /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
|
2013-07-11 16:40:35 +00:00
|
|
|
*
|
2009-05-29 00:39:03 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
|
|
|
|
|
2011-08-26 15:38:47 +00:00
|
|
|
/* 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=<number> -> tells us the GLES GPU emulation status
|
|
|
|
*
|
|
|
|
* Note that we will return <number> if we find it. This let us support
|
|
|
|
* more additionnal emulation modes in the future.
|
|
|
|
*/
|
|
|
|
char prop[PROPERTY_VALUE_MAX];
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
/* First, 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);
|
|
|
|
}
|
|
|
|
|
2009-05-29 00:39:03 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2012-06-06 04:44:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2013-07-11 16:40:35 +00:00
|
|
|
Loader::driver_t::driver_t(void* gles)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
|
|
|
dso[0] = gles;
|
|
|
|
for (size_t i=1 ; i<NELEM(dso) ; i++)
|
|
|
|
dso[i] = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:40:35 +00:00
|
|
|
Loader::driver_t::~driver_t()
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<NELEM(dso) ; i++) {
|
|
|
|
if (dso[i]) {
|
|
|
|
dlclose(dso[i]);
|
|
|
|
dso[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Loader::driver_t::set(void* hnd, int32_t api)
|
|
|
|
{
|
|
|
|
switch (api) {
|
|
|
|
case EGL:
|
|
|
|
dso[0] = hnd;
|
|
|
|
break;
|
|
|
|
case GLESv1_CM:
|
|
|
|
dso[1] = hnd;
|
|
|
|
break;
|
|
|
|
case GLESv2:
|
|
|
|
dso[2] = hnd;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return BAD_INDEX;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Loader::Loader()
|
2013-04-23 18:52:29 +00:00
|
|
|
: getProcAddress(NULL) {
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
2013-04-23 18:52:29 +00:00
|
|
|
Loader::~Loader() {
|
2011-11-30 23:05:37 +00:00
|
|
|
GLTrace_stop();
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 19:08:16 +00:00
|
|
|
static void* load_wrapper(const char* path) {
|
|
|
|
void* so = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
ALOGE_IF(!so, "dlopen(\"%s\") failed: %s", path, dlerror());
|
|
|
|
return so;
|
|
|
|
}
|
|
|
|
|
2012-02-14 01:09:30 +00:00
|
|
|
void* Loader::open(egl_connection_t* cnx)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
|
|
|
void* dso;
|
|
|
|
driver_t* hnd = 0;
|
2013-07-11 16:40:35 +00:00
|
|
|
|
2013-04-23 18:52:29 +00:00
|
|
|
dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
|
|
|
|
if (dso) {
|
|
|
|
hnd = new driver_t(dso);
|
|
|
|
} else {
|
|
|
|
// Always load EGL first
|
|
|
|
dso = load_driver("EGL", cnx, EGL);
|
2009-05-29 00:39:03 +00:00
|
|
|
if (dso) {
|
|
|
|
hnd = new driver_t(dso);
|
2013-04-23 18:52:29 +00:00
|
|
|
hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
|
|
|
|
hnd->set( load_driver("GLESv2", cnx, GLESv2), GLESv2 );
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:52:29 +00:00
|
|
|
LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
|
2013-07-04 19:08:16 +00:00
|
|
|
|
2013-07-11 16:40:35 +00:00
|
|
|
cnx->libGles2 = load_wrapper("/system/lib/libGLESv2.so");
|
|
|
|
cnx->libGles1 = load_wrapper("/system/lib/libGLESv1_CM.so");
|
2013-07-04 19:08:16 +00:00
|
|
|
LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
|
|
|
|
"couldn't load system OpenGL ES wrapper libraries");
|
|
|
|
|
2009-05-29 00:39:03 +00:00
|
|
|
return (void*)hnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t Loader::close(void* driver)
|
|
|
|
{
|
|
|
|
driver_t* hnd = (driver_t*)driver;
|
|
|
|
delete hnd;
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-07-11 16:40:35 +00:00
|
|
|
void Loader::init_api(void* dso,
|
|
|
|
char const * const * api,
|
|
|
|
__eglMustCastToProperFunctionPointerType* curr,
|
|
|
|
getProcAddressType getProcAddress)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
2012-02-14 04:06:08 +00:00
|
|
|
const ssize_t SIZE = 256;
|
2011-05-12 03:37:47 +00:00
|
|
|
char scrap[SIZE];
|
2009-05-29 00:39:03 +00:00
|
|
|
while (*api) {
|
|
|
|
char const * name = *api;
|
2013-07-11 16:40:35 +00:00
|
|
|
__eglMustCastToProperFunctionPointerType f =
|
2009-05-29 00:39:03 +00:00
|
|
|
(__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;
|
2011-05-12 03:37:47 +00:00
|
|
|
if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
|
2009-05-29 00:39:03 +00:00
|
|
|
strncpy(scrap, name, index);
|
|
|
|
scrap[index] = 0;
|
|
|
|
f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD_IF(f, "found <%s> instead", scrap);
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f == NULL) {
|
|
|
|
// Try with the OES postfix
|
2011-05-12 03:37:47 +00:00
|
|
|
ssize_t index = ssize_t(strlen(name)) - 3;
|
|
|
|
if (index>0 && strcmp(name+index, "OES")) {
|
|
|
|
snprintf(scrap, SIZE, "%sOES", name);
|
2009-05-29 00:39:03 +00:00
|
|
|
f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD_IF(f, "found <%s> instead", scrap);
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f == NULL) {
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD("%s", name);
|
2009-05-29 00:39:03 +00:00
|
|
|
f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
|
2012-01-29 05:44:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
*curr++ = f;
|
|
|
|
api++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:52:29 +00:00
|
|
|
void *Loader::load_driver(const char* kind,
|
2009-10-14 09:06:37 +00:00
|
|
|
egl_connection_t* cnx, uint32_t mask)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
2013-04-23 18:52:29 +00:00
|
|
|
class MatchFile {
|
|
|
|
public:
|
|
|
|
static String8 find(const char* kind) {
|
|
|
|
String8 result;
|
|
|
|
String8 pattern;
|
|
|
|
pattern.appendFormat("lib%s", kind);
|
|
|
|
const char* const searchPaths[] = {
|
|
|
|
"/vendor/lib/egl",
|
|
|
|
"/system/lib/egl"
|
|
|
|
};
|
|
|
|
|
|
|
|
// first, we search for the exact name of the GLES userspace
|
|
|
|
// driver in both locations.
|
|
|
|
// i.e.:
|
|
|
|
// libGLES.so, or:
|
|
|
|
// libEGL.so, libGLESv1_CM.so, libGLESv2.so
|
|
|
|
|
|
|
|
for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
|
|
|
|
if (find(result, pattern, searchPaths[i], true)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for compatibility with the old "egl.cfg" naming convention
|
|
|
|
// we look for files that match:
|
|
|
|
// libGLES_*.so, or:
|
|
|
|
// libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
|
|
|
|
|
|
|
|
pattern.append("_");
|
|
|
|
for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
|
|
|
|
if (find(result, pattern, searchPaths[i], false)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we didn't find the driver. gah.
|
|
|
|
result.clear();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool find(String8& result,
|
|
|
|
const String8& pattern, const char* const search, bool exact) {
|
|
|
|
|
|
|
|
// 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.");
|
|
|
|
result.setTo("/system/lib/egl/libGLES_android.so");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exact) {
|
|
|
|
String8 absolutePath;
|
|
|
|
absolutePath.appendFormat("%s/%s.so", search, pattern.string());
|
|
|
|
if (!access(absolutePath.string(), R_OK)) {
|
|
|
|
result = absolutePath;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR* d = opendir(search);
|
|
|
|
if (d != NULL) {
|
|
|
|
struct dirent cur;
|
|
|
|
struct dirent* e;
|
|
|
|
while (readdir_r(d, &cur, &e) == 0 && e) {
|
|
|
|
if (e->d_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;
|
2010-09-20 19:58:15 +00:00
|
|
|
}
|
2013-04-23 18:52:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
String8 absolutePath = MatchFile::find(kind);
|
|
|
|
if (absolutePath.isEmpty()) {
|
|
|
|
// this happens often, we don't want to log an error
|
|
|
|
return 0;
|
2009-09-20 23:01:02 +00:00
|
|
|
}
|
2013-04-23 18:52:29 +00:00
|
|
|
const char* const driver_absolute_path = absolutePath.string();
|
2009-09-20 23:01:02 +00:00
|
|
|
|
|
|
|
void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
if (dso == 0) {
|
|
|
|
const char* err = dlerror();
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
|
2009-09-20 23:01:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-05-29 00:39:03 +00:00
|
|
|
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("loaded %s", driver_absolute_path);
|
2009-08-21 02:09:34 +00:00
|
|
|
|
2009-05-29 00:39:03 +00:00
|
|
|
if (mask & EGL) {
|
|
|
|
getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
|
|
|
|
|
2013-07-11 16:40:35 +00:00
|
|
|
ALOGE_IF(!getProcAddress,
|
2009-09-20 23:01:02 +00:00
|
|
|
"can't find eglGetProcAddress() in %s", driver_absolute_path);
|
2009-05-29 00:39:03 +00:00
|
|
|
|
2009-10-14 09:06:37 +00:00
|
|
|
egl_t* egl = &cnx->egl;
|
2009-05-29 00:39:03 +00:00
|
|
|
__eglMustCastToProperFunctionPointerType* curr =
|
|
|
|
(__eglMustCastToProperFunctionPointerType*)egl;
|
|
|
|
char const * const * api = egl_names;
|
|
|
|
while (*api) {
|
|
|
|
char const * name = *api;
|
2013-07-11 16:40:35 +00:00
|
|
|
__eglMustCastToProperFunctionPointerType f =
|
2009-05-29 00:39:03 +00:00
|
|
|
(__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++;
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 16:40:35 +00:00
|
|
|
|
2009-05-29 00:39:03 +00:00
|
|
|
if (mask & GLESv1_CM) {
|
2009-10-14 09:06:37 +00:00
|
|
|
init_api(dso, gl_names,
|
|
|
|
(__eglMustCastToProperFunctionPointerType*)
|
2012-02-14 04:06:08 +00:00
|
|
|
&cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
|
2009-10-14 09:06:37 +00:00
|
|
|
getProcAddress);
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mask & GLESv2) {
|
2009-10-14 09:06:37 +00:00
|
|
|
init_api(dso, gl_names,
|
|
|
|
(__eglMustCastToProperFunctionPointerType*)
|
2012-02-14 04:06:08 +00:00
|
|
|
&cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
|
2009-05-29 00:39:03 +00:00
|
|
|
getProcAddress);
|
|
|
|
}
|
2013-07-11 16:40:35 +00:00
|
|
|
|
2009-05-29 00:39:03 +00:00
|
|
|
return dso;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ----------------------------------------------------------------------------
|