egl: Remove st/egl probe code.
[mesa.git] / src / egl / main / egldriver.c
index 8748fe5f46758029de9efc0e657b4cc67369c8d3..447f67d88a76eef7b39c475c5ae8934727646f42 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_gallium"
+};
 
 typedef HMODULE lib_handle;
 
@@ -60,10 +64,14 @@ library_suffix(void)
 }
 
 
-#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;
 
@@ -87,42 +95,9 @@ 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
 
 
-#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().
  */
@@ -138,25 +113,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) {
@@ -287,9 +262,9 @@ _eglLoaderFile(const char *dir, size_t len, void *loader_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;
@@ -300,6 +275,8 @@ _eglLoaderFile(const char *dir, size_t len, void *loader_data)
       if (need_suffix && len + slen + 1 <= sizeof(path)) {
          strcpy(path + len, suffix);
          drv = _eglLoadDriver(path, NULL);
+      } else {
+         drv = NULL;
       }
    }
    if (!drv)
@@ -311,71 +288,6 @@ _eglLoaderFile(const char *dir, size_t len, void *loader_data)
 }
 
 
-/**
- * A loader function for use with _eglPreloadForEach.  The loader data is the
- * pattern (prefix) of the files to look for.
- */
-static EGLBoolean
-_eglLoaderPattern(const char *dir, size_t len, void *loader_data)
-{
-#if defined(_EGL_PLATFORM_POSIX)
-   const char *prefix, *suffix;
-   size_t prefix_len, suffix_len;
-   DIR *dirp;
-   struct dirent *dirent;
-   char path[1024];
-
-   if (len + 2 > sizeof(path))
-      return EGL_TRUE;
-   if (len) {
-      memcpy(path, dir, len);
-      path[len++] = '/';
-   }
-   path[len] = '\0';
-
-   dirp = opendir(path);
-   if (!dirp)
-      return EGL_TRUE;
-
-   prefix = (const char *) loader_data;
-   prefix_len = strlen(prefix);
-   suffix = library_suffix();
-   suffix_len = (suffix) ? strlen(suffix) : 0;
-
-   while ((dirent = readdir(dirp))) {
-      _EGLDriver *drv;
-      size_t dirent_len = strlen(dirent->d_name);
-      const char *p;
-
-      /* match the prefix */
-      if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
-         continue;
-      /* match the suffix */
-      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)) {
-         strcpy(path + len, dirent->d_name);
-         drv = _eglLoadDriver(path, NULL);
-         if (drv)
-            _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
-      }
-   }
-
-   closedir(dirp);
-
-   return EGL_TRUE;
-#else /* _EGL_PLATFORM_POSIX */
-   /* stop immediately */
-   return EGL_FALSE;
-#endif
-}
-
-
 /**
  * Run the preload function on each driver directory and return the number of
  * drivers loaded.
@@ -414,23 +326,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_PLATFORM_POSIX)
+#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_PLATFORM_POSIX */
+#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;
@@ -458,7 +370,7 @@ _eglPreloadUserDriver(void)
    char *env;
 
    env = getenv("EGL_DRIVER");
-#if defined(_EGL_PLATFORM_POSIX)
+#if defined(_EGL_OS_UNIX)
    if (env && strchr(env, '/')) {
       search_path = "";
       if ((geteuid() != getuid() || getegid() != getgid())) {
@@ -467,7 +379,7 @@ _eglPreloadUserDriver(void)
          env = NULL;
       }
    }
-#endif /* _EGL_PLATFORM_POSIX */
+#endif /* _EGL_OS_UNIX */
    if (!env)
       return EGL_FALSE;
 
@@ -480,48 +392,6 @@ _eglPreloadUserDriver(void)
 }
 
 
-/**
- * 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.
- *
- * FIXME This makes libEGL a memory hog if an user driver is not specified and
- * there are many display drivers.
- */
-static EGLBoolean
-_eglPreloadDisplayDrivers(void)
-{
-   const char *dpy;
-   char prefix[32];
-   int ret;
-
-   dpy = getenv("EGL_DISPLAY");
-   if (!dpy || !dpy[0])
-      dpy = _EGL_DEFAULT_DISPLAY;
-   if (!dpy || !dpy[0])
-      return EGL_FALSE;
-
-   ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
-   if (ret < 0 || ret >= sizeof(prefix))
-      return EGL_FALSE;
-
-   return (_eglPreloadForEach(_eglGetSearchPath(),
-            _eglLoaderPattern, (void *) prefix) > 0);
-}
-
-
-/**
- * Preload the default driver.
- */
-static EGLBoolean
-_eglPreloadDefaultDriver(void)
-{
-   return (_eglPreloadForEach(_eglGetSearchPath(),
-            _eglLoaderFile, (void *) DefaultDriverName) > 0);
-}
-
-
 /**
  * Preload drivers.
  *
@@ -542,16 +412,13 @@ _eglPreloadDrivers(void)
       return EGL_TRUE;
    }
 
-   loaded = (_eglPreloadUserDriver() ||
-             _eglPreloadDisplayDrivers() ||
-             _eglPreloadDefaultDriver());
+   loaded = _eglPreloadUserDriver();
 
    _eglUnlockMutex(_eglGlobal.Mutex);
 
    return loaded;
 }
 
-
 /**
  * Unload preloaded drivers.
  */
@@ -582,6 +449,36 @@ _eglUnloadDrivers(void)
    _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
@@ -647,41 +544,15 @@ _eglInitDriverFallbacks(_EGLDriver *drv)
 
 
 /**
- * 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);
 }