2009-05-29 00:39:03 +00:00
|
|
|
/*
|
|
|
|
** 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 <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <cutils/log.h>
|
|
|
|
|
|
|
|
#include <EGL/egl.h>
|
|
|
|
|
|
|
|
#include "hooks.h"
|
|
|
|
#include "egl_impl.h"
|
|
|
|
|
|
|
|
#include "Loader.h"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EGL drivers are called
|
|
|
|
*
|
|
|
|
* /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Loader::driver_t::driver_t(void* gles)
|
|
|
|
{
|
|
|
|
dso[0] = gles;
|
|
|
|
for (size_t i=1 ; i<NELEM(dso) ; i++)
|
|
|
|
dso[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::driver_t::~driver_t()
|
|
|
|
{
|
|
|
|
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::entry_t::entry_t(int dpy, int impl, const char* tag)
|
|
|
|
: dpy(dpy), impl(impl), tag(tag) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Loader::Loader()
|
|
|
|
{
|
|
|
|
char line[256];
|
|
|
|
char tag[256];
|
|
|
|
FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
|
|
|
|
if (cfg == NULL) {
|
|
|
|
// default config
|
|
|
|
LOGD("egl.cfg not found, using default config");
|
|
|
|
gConfig.add( entry_t(0, 0, "android") );
|
|
|
|
} else {
|
|
|
|
while (fgets(line, 256, cfg)) {
|
|
|
|
int dpy;
|
|
|
|
int impl;
|
|
|
|
if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
|
2009-06-04 01:30:22 +00:00
|
|
|
//LOGD(">>> %u %u %s", dpy, impl, tag);
|
2009-05-29 00:39:03 +00:00
|
|
|
gConfig.add( entry_t(dpy, impl, tag) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(cfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Loader::~Loader()
|
|
|
|
{
|
2011-03-02 00:08:10 +00:00
|
|
|
extern void StopDebugServer();
|
|
|
|
StopDebugServer();
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* Loader::getTag(int dpy, int impl)
|
|
|
|
{
|
|
|
|
const Vector<entry_t>& cfgs(gConfig);
|
|
|
|
const size_t c = cfgs.size();
|
|
|
|
for (size_t i=0 ; i<c ; i++) {
|
|
|
|
if (dpy == cfgs[i].dpy)
|
|
|
|
if (impl == cfgs[i].impl)
|
|
|
|
return cfgs[i].tag.string();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-14 09:06:37 +00:00
|
|
|
void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* TODO: if we don't find display/0, then use 0/0
|
|
|
|
* (0/0 should always work)
|
|
|
|
*/
|
|
|
|
|
|
|
|
void* dso;
|
|
|
|
int index = int(display);
|
|
|
|
driver_t* hnd = 0;
|
|
|
|
|
|
|
|
char const* tag = getTag(index, impl);
|
|
|
|
if (tag) {
|
2010-09-20 19:58:15 +00:00
|
|
|
dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
|
2009-05-29 00:39:03 +00:00
|
|
|
if (dso) {
|
|
|
|
hnd = new driver_t(dso);
|
|
|
|
} else {
|
|
|
|
// Always load EGL first
|
2010-09-20 19:58:15 +00:00
|
|
|
dso = load_driver("EGL", tag, cnx, EGL);
|
2009-05-29 00:39:03 +00:00
|
|
|
if (dso) {
|
|
|
|
hnd = new driver_t(dso);
|
|
|
|
|
|
|
|
// TODO: make this more automated
|
2010-09-20 19:58:15 +00:00
|
|
|
hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
|
2009-05-29 00:39:03 +00:00
|
|
|
|
2010-09-20 19:58:15 +00:00
|
|
|
hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
|
2009-05-29 00:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_FATAL_IF(!index && !impl && !hnd,
|
2009-06-04 01:26:58 +00:00
|
|
|
"couldn't find the default OpenGL ES implementation "
|
2009-05-29 00:39:03 +00:00
|
|
|
"for default display");
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char scrap[256];
|
|
|
|
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<255)) && (!strcmp(name+index, "OES"))) {
|
|
|
|
strncpy(scrap, name, index);
|
|
|
|
scrap[index] = 0;
|
|
|
|
f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
|
|
|
|
//LOGD_IF(f, "found <%s> instead", scrap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f == NULL) {
|
|
|
|
// Try with the OES postfix
|
|
|
|
ssize_t index = ssize_t(strlen(name)) - 3;
|
|
|
|
if ((index>0 && (index<252)) && (strcmp(name+index, "OES"))) {
|
|
|
|
strncpy(scrap, name, index);
|
|
|
|
scrap[index] = 0;
|
|
|
|
strcat(scrap, "OES");
|
|
|
|
f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
|
|
|
|
//LOGD_IF(f, "found <%s> instead", scrap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (f == NULL) {
|
|
|
|
//LOGD("%s", name);
|
|
|
|
f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
|
|
|
|
}
|
|
|
|
*curr++ = f;
|
|
|
|
api++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-20 19:58:15 +00:00
|
|
|
void *Loader::load_driver(const char* kind, const char *tag,
|
2009-10-14 09:06:37 +00:00
|
|
|
egl_connection_t* cnx, uint32_t mask)
|
2009-05-29 00:39:03 +00:00
|
|
|
{
|
2010-09-20 19:58:15 +00:00
|
|
|
char driver_absolute_path[PATH_MAX];
|
|
|
|
const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
|
|
|
|
const char* const search2 = "/system/lib/egl/lib%s_%s.so";
|
|
|
|
|
|
|
|
snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
|
2009-09-20 23:01:02 +00:00
|
|
|
if (access(driver_absolute_path, R_OK)) {
|
2010-09-20 19:58:15 +00:00
|
|
|
snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
|
|
|
|
if (access(driver_absolute_path, R_OK)) {
|
|
|
|
// this happens often, we don't want to log an error
|
|
|
|
return 0;
|
|
|
|
}
|
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();
|
|
|
|
LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-05-29 00:39:03 +00:00
|
|
|
|
2009-09-20 23:01:02 +00:00
|
|
|
LOGD("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");
|
|
|
|
|
|
|
|
LOGE_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;
|
|
|
|
__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) {
|
2009-10-14 09:06:37 +00:00
|
|
|
init_api(dso, gl_names,
|
|
|
|
(__eglMustCastToProperFunctionPointerType*)
|
|
|
|
&cnx->hooks[GLESv1_INDEX]->gl,
|
|
|
|
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*)
|
|
|
|
&cnx->hooks[GLESv2_INDEX]->gl,
|
2009-05-29 00:39:03 +00:00
|
|
|
getProcAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dso;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|
|
|
|
// ----------------------------------------------------------------------------
|