scons: Updates for targets/egl-static.
[mesa.git] / src / egl / main / egldriver.c
index 8fc9e792b061f533c0591967c2689103f7515e56..b7c3de3e38e2c7ffe14c0fc5df90fa9357d48857 100644 (file)
@@ -9,18 +9,10 @@
 #include <stdlib.h>
 
 #include "eglstring.h"
-#include "eglconfig.h"
-#include "eglcontext.h"
 #include "egldefines.h"
 #include "egldisplay.h"
 #include "egldriver.h"
 #include "egllog.h"
-#include "eglmisc.h"
-#include "eglmode.h"
-#include "eglscreen.h"
-#include "eglstring.h"
-#include "eglsurface.h"
-#include "eglimage.h"
 #include "eglmutex.h"
 
 #if defined(_EGL_OS_UNIX)
@@ -33,6 +25,7 @@
 
 typedef struct _egl_module {
    char *Path;
+   _EGLMain_t BuiltIn;
    void *Handle;
    _EGLDriver *Driver;
 } _EGLModule;
@@ -40,6 +33,21 @@ typedef struct _egl_module {
 static _EGL_DECLARE_MUTEX(_eglModuleMutex);
 static _EGLArray *_eglModules;
 
+const struct {
+   const char *name;
+   _EGLMain_t main;
+} _eglBuiltInDrivers[] = {
+#ifdef _EGL_BUILT_IN_DRIVER_GALLIUM
+   { "egl_gallium", _eglBuiltInDriverGALLIUM },
+#endif
+#ifdef _EGL_BUILT_IN_DRIVER_DRI2
+   { "egl_dri2", _eglBuiltInDriverDRI2 },
+#endif
+#ifdef _EGL_BUILT_IN_DRIVER_GLX
+   { "egl_glx", _eglBuiltInDriverGLX },
+#endif
+   { NULL, NULL }
+};
 
 /**
  * Wrappers for dlopen/dlclose()
@@ -136,9 +144,6 @@ _eglOpenLibrary(const char *driverPath, lib_handle *handle)
    if (!lib) {
       _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
               driverPath, error);
-      if (!getenv("EGL_DRIVER"))
-         _eglLog(_EGL_WARNING,
-                 "The driver can be overridden by setting EGL_DRIVER");
       return NULL;
    }
 
@@ -165,9 +170,18 @@ _eglLoadModule(_EGLModule *mod)
    lib_handle lib;
    _EGLDriver *drv;
 
-   mainFunc = _eglOpenLibrary(mod->Path, &lib);
-   if (!mainFunc)
-      return EGL_FALSE;
+   if (mod->Driver)
+      return EGL_TRUE;
+
+   if (mod->BuiltIn) {
+      lib = (lib_handle) NULL;
+      mainFunc = mod->BuiltIn;
+   }
+   else {
+      mainFunc = _eglOpenLibrary(mod->Path, &lib);
+      if (!mainFunc)
+         return EGL_FALSE;
+   }
 
    drv = mainFunc(NULL);
    if (!drv) {
@@ -194,11 +208,22 @@ _eglLoadModule(_EGLModule *mod)
 static void
 _eglUnloadModule(_EGLModule *mod)
 {
+#if defined(_EGL_OS_UNIX)
    /* destroy the driver */
    if (mod->Driver && mod->Driver->Unload)
       mod->Driver->Unload(mod->Driver);
+
+   /*
+    * XXX At this point (atexit), the module might be the last reference to
+    * libEGL.  Closing the module might unmap libEGL and give problems.
+    */
+#if 0
    if (mod->Handle)
       close_library(mod->Handle);
+#endif
+#elif defined(_EGL_OS_WINDOWS)
+   /* XXX Windows unloads DLLs before atexit */
+#endif
 
    mod->Driver = NULL;
    mod->Handle = NULL;
@@ -309,68 +334,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_OS_UNIX)
-   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))) {
-      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 add it to the module array */
-      if (len + dirent_len + 1 <= sizeof(path)) {
-         strcpy(path + len, dirent->d_name);
-         _eglAddModule(path);
-      }
-   }
-
-   closedir(dirp);
-
-   return EGL_TRUE;
-#else /* _EGL_OS_UNIX */
-   /* stop immediately */
-   return EGL_FALSE;
-#endif
-}
-
-
 /**
  * Run the callback function on each driver directory.
  *
@@ -403,35 +366,62 @@ _eglPreloadForEach(const char *search_path,
 static const char *
 _eglGetSearchPath(void)
 {
-   static const char *search_path;
+   static char search_path[1024];
 
 #if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
-   if (!search_path) {
-      static char buffer[1024];
-      const char *p;
+   if (search_path[0] == '\0') {
+      char *buf = search_path;
+      size_t len = sizeof(search_path);
+      EGLBoolean use_env;
+      char dir_sep;
       int ret;
 
-      p = getenv("EGL_DRIVERS_PATH");
 #if defined(_EGL_OS_UNIX)
-      if (p && (geteuid() != getuid() || getegid() != getgid())) {
+      use_env = (geteuid() == getuid() && getegid() == getgid());
+      dir_sep = '/';
+#else
+      use_env = EGL_TRUE;
+      dir_sep = '\\';
+#endif
+
+      if (use_env) {
+         char *p;
+
+         /* extract the dirname from EGL_DRIVER */
+         p = getenv("EGL_DRIVER");
+         if (p && strchr(p, dir_sep)) {
+            ret = _eglsnprintf(buf, len, "%s", p);
+            if (ret > 0 && ret < len) {
+               p = strrchr(buf, dir_sep);
+               *p++ = ':';
+
+               len -= p - buf;
+               buf = p;
+            }
+         }
+
+         /* append EGL_DRIVERS_PATH */
+         p = getenv("EGL_DRIVERS_PATH");
+         if (p) {
+            ret = _eglsnprintf(buf, len, "%s:", p);
+            if (ret > 0 && ret < len) {
+               buf += ret;
+               len -= ret;
+            }
+         }
+      }
+      else {
          _eglLog(_EGL_DEBUG,
                "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
-         p = NULL;
       }
-#endif /* _EGL_OS_UNIX */
 
-      if (p) {
-         ret = _eglsnprintf(buffer, sizeof(buffer),
-               "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
-         if (ret > 0 && ret < sizeof(buffer))
-            search_path = buffer;
-      }
+      ret = _eglsnprintf(buf, len, "%s", _EGL_DRIVER_SEARCH_DIR);
+      if (ret < 0 || ret >= len)
+         search_path[0] = '\0';
+
+      _eglLog(_EGL_DEBUG, "EGL search path is %s", search_path);
    }
-   if (!search_path)
-      search_path = _EGL_DRIVER_SEARCH_DIR;
-#else
-   search_path = "";
-#endif
+#endif /* defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS) */
 
    return search_path;
 }
@@ -459,34 +449,50 @@ _eglAddUserDriver(void)
       }
    }
 #endif /* _EGL_OS_UNIX */
-   if (env)
+   if (env) {
+      _EGLModule *mod;
+      EGLint i;
+
+      /* env can be a path */
       _eglPreloadForEach(search_path, _eglLoaderFile, (void *) env);
+      /* or the name of a built-in driver */
+      for (i = 0; _eglBuiltInDrivers[i].name; i++) {
+         if (!strcmp(_eglBuiltInDrivers[i].name, env)) {
+            mod = _eglAddModule(env);
+            if (mod)
+               mod->BuiltIn = _eglBuiltInDrivers[i].main;
+         }
+      }
+   }
 }
 
 
 /**
- * Add default drivers to the module array.
+ * Add egl_gallium to the module array.
  */
 static void
-_eglAddDefaultDrivers(void)
+_eglAddGalliumDriver(void)
 {
-   const char *search_path = _eglGetSearchPath();
-   EGLint i;
-#if defined(_EGL_OS_WINDOWS)
-   const char *DefaultDriverNames[] = {
-      "egl_gallium"
-   };
-#elif defined(_EGL_OS_UNIX)
-   const char *DefaultDriverNames[] = {
-      "egl_gallium",
-      "egl_dri2",
-      "egl_glx"
-   };
+#ifndef _EGL_BUILT_IN_DRIVER_GALLIUM
+   void *external = (void *) "egl_gallium";
+   _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderFile, external);
 #endif
+}
+
+
+/**
+ * Add built-in drivers to the module array.
+ */
+static void
+_eglAddBuiltInDrivers(void)
+{
+   _EGLModule *mod;
+   EGLint i;
 
-   for (i = 0; i < ARRAY_SIZE(DefaultDriverNames); i++) {
-      void *name = (void *) DefaultDriverNames[i];
-      _eglPreloadForEach(search_path, _eglLoaderFile, name);
+   for (i = 0; _eglBuiltInDrivers[i].name; i++) {
+      mod = _eglAddModule(_eglBuiltInDrivers[i].name);
+      if (mod)
+         mod->BuiltIn = _eglBuiltInDrivers[i].main;
    }
 }
 
@@ -503,8 +509,8 @@ _eglAddDrivers(void)
 
    /* the order here decides the priorities of the drivers */
    _eglAddUserDriver();
-   _eglAddDefaultDrivers();
-   _eglPreloadForEach(_eglGetSearchPath(), _eglLoaderPattern, (void *) "egl_");
+   _eglAddGalliumDriver();
+   _eglAddBuiltInDrivers();
 
    return (_eglModules != NULL);
 }
@@ -651,80 +657,12 @@ _eglUnloadDrivers(void)
 {
    /* this is called at atexit time */
    if (_eglModules) {
-#if defined(_EGL_OS_UNIX)
       _eglDestroyArray(_eglModules, _eglFreeModule);
-#elif defined(_EGL_OS_WINDOWS)
-      /* XXX Windows unloads DLLs before atexit */
-      _eglDestroyArray(_eglModules, NULL);
-#endif
       _eglModules = NULL;
    }
 }
 
 
-/**
- * Plug all the available fallback routines into the given driver's
- * dispatch table.
- */
-void
-_eglInitDriverFallbacks(_EGLDriver *drv)
-{
-   /* If a pointer is set to NULL, then the device driver _really_ has
-    * to implement it.
-    */
-   drv->API.Initialize = NULL;
-   drv->API.Terminate = NULL;
-
-   drv->API.GetConfigs = _eglGetConfigs;
-   drv->API.ChooseConfig = _eglChooseConfig;
-   drv->API.GetConfigAttrib = _eglGetConfigAttrib;
-
-   drv->API.CreateContext = _eglCreateContext;
-   drv->API.DestroyContext = _eglDestroyContext;
-   drv->API.MakeCurrent = _eglMakeCurrent;
-   drv->API.QueryContext = _eglQueryContext;
-
-   drv->API.CreateWindowSurface = _eglCreateWindowSurface;
-   drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
-   drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
-   drv->API.DestroySurface = _eglDestroySurface;
-   drv->API.QuerySurface = _eglQuerySurface;
-   drv->API.SurfaceAttrib = _eglSurfaceAttrib;
-   drv->API.BindTexImage = _eglBindTexImage;
-   drv->API.ReleaseTexImage = _eglReleaseTexImage;
-   drv->API.SwapInterval = _eglSwapInterval;
-   drv->API.SwapBuffers = _eglSwapBuffers;
-   drv->API.CopyBuffers = _eglCopyBuffers;
-
-   drv->API.QueryString = _eglQueryString;
-   drv->API.WaitClient = _eglWaitClient;
-   drv->API.WaitNative = _eglWaitNative;
-
-#ifdef EGL_MESA_screen_surface
-   drv->API.ChooseModeMESA = _eglChooseModeMESA; 
-   drv->API.GetModesMESA = _eglGetModesMESA;
-   drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
-   drv->API.GetScreensMESA = _eglGetScreensMESA;
-   drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
-   drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
-   drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
-   drv->API.QueryScreenMESA = _eglQueryScreenMESA;
-   drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
-   drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
-   drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
-#endif /* EGL_MESA_screen_surface */
-
-#ifdef EGL_VERSION_1_2
-   drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
-#endif /* EGL_VERSION_1_2 */
-
-#ifdef EGL_KHR_image_base
-   drv->API.CreateImageKHR = _eglCreateImageKHR;
-   drv->API.DestroyImageKHR = _eglDestroyImageKHR;
-#endif /* EGL_KHR_image_base */
-}
-
-
 /**
  * Invoke a callback function on each EGL search path.
  *