egl: Introduce platform displays internally.
[mesa.git] / src / egl / main / egldriver.c
index 139f83968531770c69e804c7bd7a1dfcc5b66b29..db7b4a7471e32b641249a8350964514b28d59019 100644 (file)
@@ -7,6 +7,8 @@
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+
+#include "eglstring.h"
 #include "eglconfig.h"
 #include "eglcontext.h"
 #include "egldefines.h"
@@ -21,7 +23,7 @@
 #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>
 /**
  * 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_gdi_swrast"
+};
 
 typedef HMODULE lib_handle;
 
@@ -60,10 +64,13 @@ library_suffix(void)
 }
 
 
-#elif defined(_EGL_PLATFORM_POSIX)
+#elif defined(_EGL_OS_UNIX)
 
 
-static const char DefaultDriverName[] = "egl_glx";
+static const char *DefaultDriverNames[] = {
+   "egl_dri2",
+   "egl_glx"
+};
 
 typedef void * lib_handle;
 
@@ -87,32 +94,6 @@ library_suffix(void)
 }
 
 
-#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;
-}
-
-
 #endif
 
 
@@ -138,25 +119,25 @@ _eglOpenLibrary(const char *driverPath, lib_handle *handle)
    _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) {
@@ -237,6 +218,10 @@ _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;
@@ -261,15 +246,15 @@ _eglMatchDriver(_EGLDisplay *dpy)
 
 
 /**
- * A preload function for use with _eglPreloadForEach.  The preload data is the
+ * 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
-_eglPreloadFile(const char *dir, size_t len, void *preload_data)
+_eglLoaderFile(const char *dir, size_t len, void *loader_data)
 {
    _EGLDriver *drv;
    char path[1024];
-   const char *filename = (const char *) preload_data;
+   const char *filename = (const char *) loader_data;
    size_t flen = strlen(filename);
 
    /* make a full path */
@@ -283,9 +268,9 @@ _eglPreloadFile(const char *dir, size_t len, void *preload_data)
    len += flen;
    path[len] = '\0';
 
-   drv = _eglLoadDriver(path, NULL);
-   /* fix the path and load again */
-   if (!drv && library_suffix()) {
+   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;
@@ -296,6 +281,8 @@ _eglPreloadFile(const char *dir, size_t len, void *preload_data)
       if (need_suffix && len + slen + 1 <= sizeof(path)) {
          strcpy(path + len, suffix);
          drv = _eglLoadDriver(path, NULL);
+      } else {
+         drv = NULL;
       }
    }
    if (!drv)
@@ -308,13 +295,13 @@ _eglPreloadFile(const char *dir, size_t len, void *preload_data)
 
 
 /**
- * A preload function for use with _eglPreloadForEach.  The preload data is the
+ * A loader function for use with _eglPreloadForEach.  The loader data is the
  * pattern (prefix) of the files to look for.
  */
 static EGLBoolean
-_eglPreloadPattern(const char *dir, size_t len, void *preload_data)
+_eglLoaderPattern(const char *dir, size_t len, void *loader_data)
 {
-#if defined(_EGL_PLATFORM_POSIX)
+#if defined(_EGL_OS_UNIX)
    const char *prefix, *suffix;
    size_t prefix_len, suffix_len;
    DIR *dirp;
@@ -333,7 +320,7 @@ _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
    if (!dirp)
       return EGL_TRUE;
 
-   prefix = (const char *) preload_data;
+   prefix = (const char *) loader_data;
    prefix_len = strlen(prefix);
    suffix = library_suffix();
    suffix_len = (suffix) ? strlen(suffix) : 0;
@@ -347,9 +334,11 @@ _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
       if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
          continue;
       /* match the suffix */
-      p = dirent->d_name + dirent_len - suffix_len;
-      if (p < dirent->d_name || strcmp(p, suffix) != 0)
-         continue;
+      if (suffix) {
+         p = dirent->d_name + dirent_len - suffix_len;
+         if (p < dirent->d_name || strcmp(p, suffix) != 0)
+            continue;
+      }
 
       /* make a full path and load the driver */
       if (len + dirent_len + 1 <= sizeof(path)) {
@@ -363,7 +352,7 @@ _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
    closedir(dirp);
 
    return EGL_TRUE;
-#else /* _EGL_PLATFORM_POSIX */
+#else /* _EGL_OS_UNIX */
    /* stop immediately */
    return EGL_FALSE;
 #endif
@@ -378,8 +367,8 @@ _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
  */
 static EGLint
 _eglPreloadForEach(const char *search_path,
-                   EGLBoolean (*preload)(const char *, size_t, void *),
-                   void *preload_data)
+                   EGLBoolean (*loader)(const char *, size_t, void *),
+                   void *loader_data)
 {
    const char *cur, *next;
    size_t len;
@@ -390,7 +379,7 @@ _eglPreloadForEach(const char *search_path,
       next = strchr(cur, ':');
       len = (next) ? next - cur : strlen(cur);
 
-      if (!preload(cur, len, preload_data))
+      if (!loader(cur, len, loader_data))
          break;
 
       cur = (next) ? next + 1 : NULL;
@@ -408,15 +397,23 @@ _eglGetSearchPath(void)
 {
    static const char *search_path;
 
-#if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
+#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 */
+
       if (p) {
-         ret = snprintf(buffer, sizeof(buffer),
+         ret = _eglsnprintf(buffer, sizeof(buffer),
                "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
          if (ret > 0 && ret < sizeof(buffer))
             search_path = buffer;
@@ -440,73 +437,63 @@ _eglGetSearchPath(void)
 static EGLBoolean
 _eglPreloadUserDriver(void)
 {
-#if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
    const char *search_path = _eglGetSearchPath();
    char *env;
 
    env = getenv("EGL_DRIVER");
-#if defined(_EGL_PLATFORM_POSIX)
-   if (env && strchr(env, '/'))
+#if defined(_EGL_OS_UNIX)
+   if (env && strchr(env, '/')) {
       search_path = "";
-#endif
+      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;
 
-   if (!_eglPreloadForEach(search_path, _eglPreloadFile, (void *) env)) {
+   if (!_eglPreloadForEach(search_path, _eglLoaderFile, (void *) env)) {
       _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
       return EGL_FALSE;
    }
 
    return EGL_TRUE;
-#else /* _EGL_PLATFORM_POSIX || _EGL_PLATFORM_WINDOWS */
-   return EGL_FALSE;
-#endif
 }
 
 
 /**
- * Preload display drivers.
+ * Preload platform drivers.
  *
- * Display drivers are a set of drivers that support a certain display system.
- * The display system may be specified by EGL_DISPLAY.
+ * Platform drivers are a set of drivers that support a certain window system.
+ * The window system may be specified by EGL_PLATFORM.
  *
  * FIXME This makes libEGL a memory hog if an user driver is not specified and
- * there are many display drivers.
+ * there are many platform drivers.
  */
 static EGLBoolean
-_eglPreloadDisplayDrivers(void)
+_eglPreloadPlatformDrivers(void)
 {
-#if defined(_EGL_PLATFORM_POSIX)
    const char *dpy;
    char prefix[32];
    int ret;
 
-   dpy = getenv("EGL_DISPLAY");
+   dpy = getenv("EGL_PLATFORM");
+   /* try deprecated env variable */
    if (!dpy || !dpy[0])
-      dpy = _EGL_DEFAULT_DISPLAY;
+      dpy = getenv("EGL_DISPLAY");
+   if (!dpy || !dpy[0])
+      dpy = _EGL_DEFAULT_PLATFORM;
    if (!dpy || !dpy[0])
       return EGL_FALSE;
 
-   ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
+   ret = _eglsnprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
    if (ret < 0 || ret >= sizeof(prefix))
       return EGL_FALSE;
 
    return (_eglPreloadForEach(_eglGetSearchPath(),
-            _eglPreloadPattern, (void *) prefix) > 0);
-#else /* _EGL_PLATFORM_POSIX */
-   return EGL_FALSE;
-#endif
-}
-
-
-/**
- * Preload the default driver.
- */
-static EGLBoolean
-_eglPreloadDefaultDriver(void)
-{
-   return (_eglPreloadForEach(_eglGetSearchPath(),
-            _eglPreloadFile, (void *) DefaultDriverName) > 0);
+            _eglLoaderPattern, (void *) prefix) > 0);
 }
 
 
@@ -521,18 +508,23 @@ _eglPreloadDrivers(void)
 {
    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());
+             _eglPreloadPlatformDrivers());
+
+   _eglUnlockMutex(_eglGlobal.Mutex);
 
    return loaded;
 }
 
-
 /**
  * Unload preloaded drivers.
  */
@@ -540,6 +532,8 @@ void
 _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;
@@ -561,6 +555,40 @@ _eglUnloadDrivers(void)
    _eglGlobal.NumDrivers = 0;
 }
 
+_EGLDriver *
+_eglLoadDefaultDriver(EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+   _EGLDriver *drv = NULL;
+   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];
+      if (drv->API.Initialize(drv, dpy, major, minor))
+        break;
+      _eglUnloadDrivers();
+   }      
+
+   _eglUnlockMutex(_eglGlobal.Mutex);
+
+   return _eglGlobal.NumDrivers > 0 ? drv : NULL;
+}
+
+
+/**
+ * Return the native platform.  It is the platform of the EGL native types.
+ */
+_EGLPlatformType
+_eglGetNativePlatform(void)
+{
+   return _EGL_NATIVE_PLATFORM;
+}
+
 
 /**
  * Plug all the available fallback routines into the given driver's
@@ -625,6 +653,21 @@ _eglInitDriverFallbacks(_EGLDriver *drv)
 }
 
 
+/**
+ * Invoke a callback function on each EGL search path.
+ *
+ * The first argument of the callback function is the name of the search path.
+ * The second argument is the length of the name.
+ */
+void
+_eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
+                      void *callback_data)
+{
+   const char *search_path = _eglGetSearchPath();
+   _eglPreloadForEach(search_path, callback, callback_data);
+}
+
+
 /**
  * Set the probe cache at the given key.
  *