#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+
+#include "eglstring.h"
#include "eglconfig.h"
#include "eglcontext.h"
#include "egldefines.h"
#include "eglscreen.h"
#include "eglstring.h"
#include "eglsurface.h"
+#include "eglimage.h"
-#if defined(_EGL_PLATFORM_POSIX)
+#if defined(_EGL_OS_UNIX)
#include <dlfcn.h>
#include <sys/types.h>
#include <dirent.h>
+#include <unistd.h>
#endif
/**
* Wrappers for dlopen/dlclose()
*/
-#if defined(_EGL_PLATFORM_WINDOWS)
+#if defined(_EGL_OS_WINDOWS)
/* XXX Need to decide how to do dynamic name lookup on Windows */
-static const char DefaultDriverName[] = "TBD";
+static const char *DefaultDriverNames[] = {
+ "egl_gallium"
+};
typedef HMODULE lib_handle;
static const char *
library_suffix(void)
{
- return "dll";
+ return ".dll";
}
-#elif defined(_EGL_PLATFORM_POSIX)
+#elif defined(_EGL_OS_UNIX)
-static const char DefaultDriverName[] = "egl_glx";
+static const char *DefaultDriverNames[] = {
+ "egl_gallium",
+ "egl_dri2",
+ "egl_glx"
+};
typedef void * lib_handle;
static const char *
library_suffix(void)
{
- return "so";
-}
-
-
-#else /* _EGL_PLATFORM_NO_OS */
-
-static const char DefaultDriverName[] = "builtin";
-
-typedef void *lib_handle;
-
-static INLINE void *
-open_library(const char *filename)
-{
- return (void *) filename;
-}
-
-static INLINE void
-close_library(void *lib)
-{
-}
-
-
-static const char *
-library_suffix(void)
-{
- return NULL;
+ return ".so";
}
#endif
-#define NUM_PROBE_CACHE_SLOTS 8
-static struct {
- EGLint keys[NUM_PROBE_CACHE_SLOTS];
- const void *values[NUM_PROBE_CACHE_SLOTS];
-} _eglProbeCache;
-
-
/**
* Open the named driver and find its bootstrap function: _eglMain().
*/
_eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
lib = open_library(driverPath);
-#if defined(_EGL_PLATFORM_WINDOWS)
+#if defined(_EGL_OS_WINDOWS)
/* XXX untested */
if (lib)
mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
-#elif defined(_EGL_PLATFORM_POSIX)
+#elif defined(_EGL_OS_UNIX)
if (lib) {
- mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
+ union {
+ _EGLMain_t func;
+ void *ptr;
+ } tmp = { NULL };
+ /* direct cast gives a warning when compiled with -pedantic */
+ tmp.ptr = dlsym(lib, "_eglMain");
+ mainFunc = tmp.func;
if (!mainFunc)
error = dlerror();
}
else {
error = dlerror();
}
-#else /* _EGL_PLATFORM_NO_OS */
- /* must be the default driver name */
- if (strcmp(driverPath, DefaultDriverName) == 0)
- mainFunc = (_EGLMain_t) _eglMain;
- else
- error = "not builtin driver";
#endif
if (!lib) {
*
* The matching is done by finding the driver with the highest score.
*/
-static _EGLDriver *
+_EGLDriver *
_eglMatchDriver(_EGLDisplay *dpy)
{
_EGLDriver *best_drv = NULL;
EGLint best_score = -1, i;
+ /*
+ * this function is called after preloading and the drivers never change
+ * after preloading.
+ */
for (i = 0; i < _eglGlobal.NumDrivers; i++) {
_EGLDriver *drv = _eglGlobal.Drivers[i];
EGLint score;
/**
- * Open a preloaded driver.
- */
-_EGLDriver *
-_eglOpenDriver(_EGLDisplay *dpy)
-{
- _EGLDriver *drv = _eglMatchDriver(dpy);
- return drv;
-}
-
-
-/**
- * Close a preloaded driver.
- */
-EGLBoolean
-_eglCloseDriver(_EGLDriver *drv, _EGLDisplay *dpy)
-{
- return EGL_TRUE;
-}
-
-
-/**
- * Preload a user driver.
- *
- * A user driver can be specified by EGL_DRIVER.
+ * A loader function for use with _eglPreloadForEach. The loader data is the
+ * filename of the driver. This function stops on the first valid driver.
*/
static EGLBoolean
-_eglPreloadUserDriver(void)
+_eglLoaderFile(const char *dir, size_t len, void *loader_data)
{
-#if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
_EGLDriver *drv;
- char *env, *path;
- const char *suffix, *p;
+ char path[1024];
+ const char *filename = (const char *) loader_data;
+ size_t flen = strlen(filename);
- env = getenv("EGL_DRIVER");
- if (!env)
- return EGL_FALSE;
+ /* make a full path */
+ if (len + flen + 2 > sizeof(path))
+ return EGL_TRUE;
+ if (len) {
+ memcpy(path, dir, len);
+ path[len++] = '/';
+ }
+ memcpy(path + len, filename, flen);
+ len += flen;
+ path[len] = '\0';
- path = env;
- suffix = library_suffix();
-
- /* append suffix if there isn't */
- p = strrchr(path, '.');
- if (!p && suffix) {
- size_t len = strlen(path);
- char *tmp = malloc(len + strlen(suffix) + 2);
- if (tmp) {
- memcpy(tmp, path, len);
- tmp[len++] = '.';
- tmp[len] = '\0';
- strcat(tmp + len, suffix);
-
- path = tmp;
+ if (library_suffix() == NULL || strstr(path, library_suffix()))
+ drv = _eglLoadDriver(path, NULL);
+ else {
+ const char *suffix = library_suffix();
+ size_t slen = strlen(suffix);
+ const char *p;
+ EGLBoolean need_suffix;
+
+ p = filename + flen - slen;
+ need_suffix = (p < filename || strcmp(p, suffix) != 0);
+ if (need_suffix && len + slen + 1 <= sizeof(path)) {
+ strcpy(path + len, suffix);
+ drv = _eglLoadDriver(path, NULL);
+ } else {
+ drv = NULL;
}
}
-
- drv = _eglLoadDriver(path, NULL);
- if (path != env)
- free(path);
if (!drv)
- return EGL_FALSE;
+ return EGL_TRUE;
+ /* remember the driver and stop */
_eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
-
- return EGL_TRUE;
-#else /* _EGL_PLATFORM_POSIX || _EGL_PLATFORM_WINDOWS */
return EGL_FALSE;
-#endif
}
/**
- * Preload display drivers.
- *
- * Display drivers are a set of drivers that support a certain display system.
- * The display system may be specified by EGL_DISPLAY.
+ * Run the preload function on each driver directory and return the number of
+ * drivers loaded.
*
- * FIXME This makes libEGL a memory hog if an user driver is not specified and
- * there are many display drivers.
+ * The process may end prematurely if the callback function returns false.
*/
-static EGLBoolean
-_eglPreloadDisplayDrivers(void)
+static EGLint
+_eglPreloadForEach(const char *search_path,
+ EGLBoolean (*loader)(const char *, size_t, void *),
+ void *loader_data)
{
-#if defined(_EGL_PLATFORM_POSIX)
- const char *dpy, *suffix;
- char path[1024], prefix[32];
- DIR *dirp;
- struct dirent *dirent;
-
- dpy = getenv("EGL_DISPLAY");
- if (!dpy || !dpy[0])
- dpy = _EGL_DEFAULT_DISPLAY;
- if (!dpy || !dpy[0])
- return EGL_FALSE;
+ const char *cur, *next;
+ size_t len;
+ EGLint num_drivers = _eglGlobal.NumDrivers;
- snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
- suffix = library_suffix();
+ cur = search_path;
+ while (cur) {
+ next = strchr(cur, ':');
+ len = (next) ? next - cur : strlen(cur);
- dirp = opendir(_EGL_DRIVER_SEARCH_DIR);
- if (!dirp)
- return EGL_FALSE;
-
- while ((dirent = readdir(dirp))) {
- _EGLDriver *drv;
- const char *p;
+ if (!loader(cur, len, loader_data))
+ break;
- /* match the prefix */
- if (strncmp(dirent->d_name, prefix, strlen(prefix)) != 0)
- continue;
+ cur = (next) ? next + 1 : NULL;
+ }
- /* match the suffix */
- p = strrchr(dirent->d_name, '.');
- if ((p && !suffix) || (!p && suffix))
- continue;
- else if (p && suffix && strcmp(p + 1, suffix) != 0)
- continue;
+ return (_eglGlobal.NumDrivers - num_drivers);
+}
- snprintf(path, sizeof(path),
- _EGL_DRIVER_SEARCH_DIR"/%s", dirent->d_name);
- drv = _eglLoadDriver(path, NULL);
- if (drv)
- _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
- }
+/**
+ * Return a list of colon-separated driver directories.
+ */
+static const char *
+_eglGetSearchPath(void)
+{
+ static const char *search_path;
- closedir(dirp);
+#if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
+ if (!search_path) {
+ static char buffer[1024];
+ const char *p;
+ int ret;
+
+ p = getenv("EGL_DRIVERS_PATH");
+#if defined(_EGL_OS_UNIX)
+ if (p && (geteuid() != getuid() || getegid() != getgid())) {
+ _eglLog(_EGL_DEBUG,
+ "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
+ p = NULL;
+ }
+#endif /* _EGL_OS_UNIX */
- return (_eglGlobal.NumDrivers > 0);
-#else /* _EGL_PLATFORM_POSIX */
- return EGL_FALSE;
+ if (p) {
+ ret = _eglsnprintf(buffer, sizeof(buffer),
+ "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
+ if (ret > 0 && ret < sizeof(buffer))
+ search_path = buffer;
+ }
+ }
+ if (!search_path)
+ search_path = _EGL_DRIVER_SEARCH_DIR;
+#else
+ search_path = "";
#endif
+
+ return search_path;
}
/**
- * Preload the default driver.
+ * Preload a user driver.
+ *
+ * A user driver can be specified by EGL_DRIVER.
*/
static EGLBoolean
-_eglPreloadDefaultDriver(void)
+_eglPreloadUserDriver(void)
{
- _EGLDriver *drv;
- char path[1024];
- const char *suffix = library_suffix();
+ const char *search_path = _eglGetSearchPath();
+ char *env;
- if (suffix)
- snprintf(path, sizeof(path), "%s.%s", DefaultDriverName, suffix);
- else
- snprintf(path, sizeof(path), DefaultDriverName);
-
- drv = _eglLoadDriver(path, NULL);
- if (!drv)
+ env = getenv("EGL_DRIVER");
+#if defined(_EGL_OS_UNIX)
+ if (env && strchr(env, '/')) {
+ search_path = "";
+ if ((geteuid() != getuid() || getegid() != getgid())) {
+ _eglLog(_EGL_DEBUG,
+ "ignore EGL_DRIVER for setuid/setgid binaries");
+ env = NULL;
+ }
+ }
+#endif /* _EGL_OS_UNIX */
+ if (!env)
return EGL_FALSE;
- _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
+ if (!_eglPreloadForEach(search_path, _eglLoaderFile, (void *) env)) {
+ _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
+ return EGL_FALSE;
+ }
return EGL_TRUE;
}
{
EGLBoolean loaded;
+ /* protect the preloading process */
+ _eglLockMutex(_eglGlobal.Mutex);
+
/* already preloaded */
- if (_eglGlobal.NumDrivers)
+ if (_eglGlobal.NumDrivers) {
+ _eglUnlockMutex(_eglGlobal.Mutex);
return EGL_TRUE;
+ }
- loaded = (_eglPreloadUserDriver() ||
- _eglPreloadDisplayDrivers() ||
- _eglPreloadDefaultDriver());
+ loaded = _eglPreloadUserDriver();
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
return loaded;
}
-
/**
* Unload preloaded drivers.
*/
_eglUnloadDrivers(void)
{
EGLint i;
+
+ /* this is called at atexit time */
for (i = 0; i < _eglGlobal.NumDrivers; i++) {
_EGLDriver *drv = _eglGlobal.Drivers[i];
lib_handle handle = drv->LibHandle;
_eglGlobal.NumDrivers = 0;
}
+_EGLDriver *
+_eglLoadDefaultDriver(EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ _EGLDriver *drv = NULL;
+ EGLBoolean ok;
+ int i;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ for (i = 0; i < ARRAY_SIZE(DefaultDriverNames); i++) {
+ _eglPreloadForEach(_eglGetSearchPath(),
+ _eglLoaderFile, (void *) DefaultDriverNames[i]);
+ if (_eglGlobal.NumDrivers == 0)
+ continue;
+ drv = _eglGlobal.Drivers[0];
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+ ok = drv->API.Initialize(drv, dpy, major, minor);
+ _eglLockMutex(_eglGlobal.Mutex);
+ if (ok)
+ break;
+
+ _eglUnloadDrivers();
+ }
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+
+ return _eglGlobal.NumDrivers > 0 ? drv : NULL;
+}
+
/**
* Plug all the available fallback routines into the given driver's
#ifdef EGL_VERSION_1_2
drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
#endif /* EGL_VERSION_1_2 */
-}
-
-
-
-/**
- * Try to determine which EGL APIs (OpenGL, OpenGL ES, OpenVG, etc)
- * are supported on the system by looking for standard library names.
- */
-EGLint
-_eglFindAPIs(void)
-{
- EGLint mask = 0x0;
- lib_handle lib;
-#if defined(_EGL_PLATFORM_WINDOWS)
- /* XXX not sure about these names */
- const char *es1_libname = "libGLESv1_CM.dll";
- const char *es2_libname = "libGLESv2.dll";
- const char *gl_libname = "OpenGL32.dll";
- const char *vg_libname = "libOpenVG.dll";
-#elif defined(_EGL_PLATFORM_POSIX)
- const char *es1_libname = "libGLESv1_CM.so";
- const char *es2_libname = "libGLESv2.so";
- const char *gl_libname = "libGL.so";
- const char *vg_libname = "libOpenVG.so";
-#else /* _EGL_PLATFORM_NO_OS */
- const char *es1_libname = NULL;
- const char *es2_libname = NULL;
- const char *gl_libname = NULL;
- const char *vg_libname = NULL;
-#endif
- if ((lib = open_library(es1_libname))) {
- close_library(lib);
- mask |= EGL_OPENGL_ES_BIT;
- }
-
- if ((lib = open_library(es2_libname))) {
- close_library(lib);
- mask |= EGL_OPENGL_ES2_BIT;
- }
-
- if ((lib = open_library(gl_libname))) {
- close_library(lib);
- mask |= EGL_OPENGL_BIT;
- }
-
- if ((lib = open_library(vg_libname))) {
- close_library(lib);
- mask |= EGL_OPENVG_BIT;
- }
-
- return mask;
+#ifdef EGL_KHR_image_base
+ drv->API.CreateImageKHR = _eglCreateImageKHR;
+ drv->API.DestroyImageKHR = _eglDestroyImageKHR;
+#endif /* EGL_KHR_image_base */
}
/**
- * Set the probe cache at the given key.
+ * Invoke a callback function on each EGL search path.
*
- * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
- * by multiple drivers.
+ * The first argument of the callback function is the name of the search path.
+ * The second argument is the length of the name.
*/
void
-_eglSetProbeCache(EGLint key, const void *val)
+_eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
+ void *callback_data)
{
- EGLint idx;
-
- for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
- if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
- break;
- }
- assert(key > 0);
- assert(idx < NUM_PROBE_CACHE_SLOTS);
-
- _eglProbeCache.keys[idx] = key;
- _eglProbeCache.values[idx] = val;
-}
-
-
-/**
- * Return the probe cache at the given key.
- */
-const void *
-_eglGetProbeCache(EGLint key)
-{
- EGLint idx;
-
- for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
- if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
- break;
- }
-
- return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
- _eglProbeCache.values[idx] : NULL;
+ const char *search_path = _eglGetSearchPath();
+ _eglPreloadForEach(search_path, callback, callback_data);
}