OSDN Git Service

egl: Add support for EGL_KHR_image_base.
[android-x86/external-mesa.git] / src / egl / main / egldriver.c
index 6d53362..89e04a7 100644 (file)
@@ -4,10 +4,9 @@
 
 
 #include <assert.h>
-#include <dlfcn.h>
+#include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include "eglconfig.h"
 #include "eglcontext.h"
 #include "egldefines.h"
 #include "egldriver.h"
 #include "eglglobals.h"
 #include "egllog.h"
+#include "eglmisc.h"
 #include "eglmode.h"
 #include "eglscreen.h"
 #include "eglstring.h"
 #include "eglsurface.h"
+#include "eglimage.h"
 
 #if defined(_EGL_PLATFORM_X)
-#include "eglx.h"
+#include <dlfcn.h>
 #elif defined(_EGL_PLATFORM_WINDOWS)
-/* XXX to do */
-#elif defined(_EGL_PLATFORM_WINCE)
-/* XXX to do */
+/* Use static linking on Windows for now */
+#define WINDOWS_STATIC_LINK
 #endif
 
+/**
+ * Wrappers for dlopen/dlclose()
+ */
+#if defined(_EGL_PLATFORM_WINDOWS)
+#ifdef WINDOWS_STATIC_LINK
+   static const char *DefaultDriverName = "Windows EGL Static Library";
+#else
+   /* XXX Need to decide how to do dynamic name lookup on Windows */
+   static const char *DefaultDriverName = "TBD";
+#endif
+   typedef HMODULE lib_handle;
 
-const char *DefaultDriverName = ":0";
-const char *SysFS = "/sys/class";
+   static HMODULE
+   open_library(const char *filename)
+   {
+#ifdef WINDOWS_STATIC_LINK
+      return 0;
+#else
+      return LoadLibrary(filename);
+#endif
+   }
+
+   static void
+   close_library(HMODULE lib)
+   {
+#ifdef WINDOWS_STATIC_LINK
+#else
+      FreeLibrary(lib);
+#endif
+   }
 
+#elif defined(_EGL_PLATFORM_X)
+   static const char *DefaultDriverName = "egl_softpipe";
 
+   typedef void * lib_handle;
+
+   static void *
+   open_library(const char *filename)
+   {
+      return dlopen(filename, RTLD_LAZY);
+   }
+
+   static void
+   close_library(void *lib)
+   {
+      dlclose(lib);
+   }
+   
+#endif
 
 
 /**
- * Given a card number, use sysfs to determine the DRI driver name.
+ * Choose a driver for a given display.
+ * The caller may free() the returned strings.
  */
-static const char *
-_eglChooseDRMDriver(int card)
+static char *
+_eglChooseDriver(_EGLDisplay *dpy, char **argsRet)
 {
-#if 0
-   return _eglstrdup("libEGLdri");
-#else
-   char path[2000], driverName[2000];
-   FILE *f;
-   int length;
+   char *path = NULL;
+   const char *args = NULL;
+   const char *suffix = NULL;
+   const char *p;
 
-   snprintf(path, sizeof(path), "%s/drm/card%d/dri_library_name", SysFS, card);
+   path = getenv("EGL_DRIVER");
+   if (path)
+      path = _eglstrdup(path);
 
-   f = fopen(path, "r");
-   if (!f)
-      return NULL;
+#if defined(_EGL_PLATFORM_X)
+   if (!path && dpy->NativeDisplay) {
+      /* assume (wrongly!) that the native display is a display string */
+      path = _eglSplitDisplayString((const char *) dpy->NativeDisplay, &args);
+   }
+   suffix = "so";
+#elif defined(_EGL_PLATFORM_WINDOWS)
+   suffix = "dll";
+#endif /* _EGL_PLATFORM_X */
+
+   if (!path)
+      path = _eglstrdup(DefaultDriverName);
+
+   /* 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);
+
+         free(path);
+         path = tmp;
+      }
+   }
+
+   if (argsRet)
+      *argsRet = (args) ? _eglstrdup(args) : NULL;
+
+   return path;
+}
+
+
+/**
+ * Open the named driver and find its bootstrap function: _eglMain().
+ */
+static _EGLMain_t
+_eglOpenLibrary(const char *driverPath, lib_handle *handle)
+{
+   _EGLMain_t mainFunc;
+   lib_handle lib;
 
-   fgets(driverName, sizeof(driverName), f);
-   fclose(f);
+   assert(driverPath);
 
-   if ((length = strlen(driverName)) > 1) {
-      /* remove the trailing newline from sysfs */
-      driverName[length - 1] = '\0';
-      strncat(driverName, "_dri", sizeof(driverName));
-      return _eglstrdup(driverName);
+#if defined(_EGL_PLATFORM_WINDOWS)
+/* Use static linking on Windows for now */
+#ifdef WINDOWS_STATIC_LINK
+   lib = 0;
+   mainFunc = (_EGLMain_t)_eglMain;
+#else
+   /* XXX untested */
+   _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
+   lib = open_library(driverPath);
+   if (!lib) {
+      _eglLog(_EGL_WARNING, "Could not open %s",
+              driverPath);
+      return NULL;
    }
-   else {
+   mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
+#endif
+#elif defined(_EGL_PLATFORM_X)
+   _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
+   lib = open_library(driverPath);
+   if (!lib) {
+      _eglLog(_EGL_WARNING, "Could not open %s (%s)",
+              driverPath, dlerror());
+      if (!getenv("EGL_DRIVER"))
+         _eglLog(_EGL_WARNING,
+                 "The driver can be overridden by setting EGL_DRIVER");
       return NULL;
-   }   
+   }
+   mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
 #endif
+
+   if (!mainFunc) {
+      _eglLog(_EGL_WARNING, "_eglMain not found in %s", driverPath);
+      if (lib)
+         close_library(lib);
+      return NULL;
+   }
+
+   *handle = lib;
+   return mainFunc;
 }
 
 
 /**
- * Determine/return the name of the driver to use for the given _EGLDisplay.
- *
- * Try to be clever and determine if nativeDisplay is an Xlib Display
- * ptr or a string (naming a driver or screen number, etc).
- *
- * If the first character is ':' we interpret it as a screen or card index
- * number (i.e. ":0" or ":1", etc)
- * Else if the first character is '!' we interpret it as specific driver name
- * (i.e. "!r200" or "!i830".
- *
- * Whatever follows ':' is copied and put into dpy->DriverArgs.
- *
- * The caller may free() the returned string.
+ * Load the named driver.  The path and args passed will be
+ * owned by the driver and freed.
  */
-const char *
-_eglChooseDriver(_EGLDisplay *dpy)
+static _EGLDriver *
+_eglLoadDriver(char *path, char *args)
 {
-   const char *displayString = (const char *) dpy->NativeDisplay;
-   const char *driverName = NULL;
+   _EGLMain_t mainFunc;
+   lib_handle lib;
+   _EGLDriver *drv = NULL;
 
-   if (!displayString) {
-      /* choose a default */
-      displayString = DefaultDriverName;
-   }
+   mainFunc = _eglOpenLibrary(path, &lib);
+   if (!mainFunc)
+      return NULL;
 
-   /* extract default DriverArgs = whatever follows ':' */
-   if (displayString[0] == '!' ||
-       displayString[0] == ':') {
-      const char *args = strchr(displayString, ':');
-      if (args)
-         dpy->DriverArgs = _eglstrdup(args + 1);
+   drv = mainFunc(args);
+   if (!drv) {
+      if (lib)
+         close_library(lib);
+      return NULL;
    }
 
-   /* determine driver name now */
-   if (displayString && displayString[0] == ':' &&
-       (displayString[1] >= '0' && displayString[1] <= '9') &&
-       !displayString[2]) {
-      int card = atoi(displayString + 1);
-      driverName = _eglChooseDRMDriver(card);
+   if (!drv->Name) {
+      _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
+      drv->Name = "UNNAMED";
    }
-   else if (displayString && displayString[0] == '!') {
-      /* use user-specified driver name */
-      driverName = _eglstrdup(displayString + 1);
-      /* truncate driverName at ':' if present */
-      {
-         char *args = strchr(driverName, ':');
-         if (args) {
-            *args = 0;
-         }
+
+   drv->Path = path;
+   drv->Args = args;
+   drv->LibHandle = lib;
+
+   return drv;
+}
+
+
+/**
+ * Match a display to a preloaded driver.
+ */
+static _EGLDriver *
+_eglMatchDriver(_EGLDisplay *dpy)
+{
+   _EGLDriver *defaultDriver = NULL;
+   EGLint i;
+
+   for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+      _EGLDriver *drv = _eglGlobal.Drivers[i];
+
+      /* display specifies a driver */
+      if (dpy->DriverName) {
+         if (strcmp(dpy->DriverName, drv->Name) == 0)
+            return drv;
+      }
+      else if (drv->Probe) {
+         if (drv->Probe(drv, dpy))
+            return drv;
+      }
+      else {
+         if (!defaultDriver)
+            defaultDriver = drv;
       }
-   }
-   else {
-      /* NativeDisplay is not a string! */
-#if defined(_EGL_PLATFORM_X)
-      driverName = _xeglChooseDriver(dpy);
-#elif defined(_EGL_PLATFORM_WINDOWS)
-      /* XXX to do */
-      driverName = _weglChooseDriver(dpy);
-#elif defined(_EGL_PLATFORM_WINCE)
-      /* XXX to do */
-#endif
    }
 
-   return driverName;
+   return defaultDriver;
 }
 
 
 /**
- * Open/load the named driver and call its bootstrap function: _eglMain().
- * By the time this function is called, the dpy->DriverName should have
- * been determined.
- *
- * \return  new _EGLDriver object.
+ * Load a driver and save it.
  */
-_EGLDriver *
-_eglOpenDriver(_EGLDisplay *dpy, const char *driverName, const char *args)
+const char *
+_eglPreloadDriver(_EGLDisplay *dpy)
 {
+   char *path, *args;
    _EGLDriver *drv;
-   _EGLMain_t mainFunc;
-   void *lib;
-   char driverFilename[1000];
-
-   assert(driverName);
+   EGLint i;
 
-   /* XXX also prepend a directory path??? */
-   sprintf(driverFilename, "%s.so", driverName);
-
-   _eglLog(_EGL_DEBUG, "dlopen(%s)", driverFilename);
-   lib = dlopen(driverFilename, RTLD_NOW);
-   if (!lib) {
-      _eglLog(_EGL_WARNING, "Could not open %s (%s)",
-              driverFilename, dlerror());
+   path = _eglChooseDriver(dpy, &args);
+   if (!path)
       return NULL;
-   }
 
-   mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
-   if (!mainFunc) {
-      _eglLog(_EGL_WARNING, "_eglMain not found in %s", driverFilename);
-      dlclose(lib);
-      return NULL;
+   for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+      drv = _eglGlobal.Drivers[i];
+      if (strcmp(drv->Path, path) == 0) {
+         _eglLog(_EGL_DEBUG, "Driver %s is already preloaded",
+                 drv->Name);
+         free(path);
+         if (args)
+            free(args);
+         return drv->Name;
+      }
    }
 
-   drv = mainFunc(dpy, args);
-   if (!drv) {
-      dlclose(lib);
+   drv = _eglLoadDriver(path, args);
+   if (!drv)
       return NULL;
-   }
-   /* with a recurvise open you want the inner most handle */
-   if (!drv->LibHandle)
-      drv->LibHandle = lib;
-   else
-      dlclose(lib);
 
-   drv->Display = dpy;
+   _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
+
+   return drv->Name;
+}
+
+
+/**
+ * Open a preloaded driver.
+ */
+_EGLDriver *
+_eglOpenDriver(_EGLDisplay *dpy)
+{
+   _EGLDriver *drv = _eglMatchDriver(dpy);
    return drv;
 }
 
 
+/**
+ * Close a preloaded driver.
+ */
 EGLBoolean
-_eglCloseDriver(_EGLDriver *drv, EGLDisplay dpy)
+_eglCloseDriver(_EGLDriver *drv, _EGLDisplay *dpy)
 {
-   void *handle = drv->LibHandle;
-   EGLBoolean b;
+   return EGL_TRUE;
+}
 
-   _eglLog(_EGL_INFO, "Closing driver");
 
-   /*
-    * XXX check for currently bound context/surfaces and delete them?
-    */
+/**
+ * Unload preloaded drivers.
+ */
+void
+_eglUnloadDrivers(void)
+{
+   EGLint i;
+   for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+      _EGLDriver *drv = _eglGlobal.Drivers[i];
+      lib_handle handle = drv->LibHandle;
+
+      if (drv->Path)
+         free((char *) drv->Path);
+      if (drv->Args)
+         free((char *) drv->Args);
+
+      /* destroy driver */
+      if (drv->Unload)
+         drv->Unload(drv);
+
+      if (handle)
+         close_library(handle);
+      _eglGlobal.Drivers[i] = NULL;
+   }
 
-   b = drv->API.Terminate(drv, dpy);
-   dlclose(handle);
-   return b;
+   _eglGlobal.NumDrivers = 0;
 }
 
 
@@ -276,75 +405,56 @@ _eglInitDriverFallbacks(_EGLDriver *drv)
 #ifdef EGL_VERSION_1_2
    drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
 #endif /* EGL_VERSION_1_2 */
-}
-
-
-/**
- * Examine the individual extension enable/disable flags and recompute
- * the driver's Extensions string.
- */
-static void
-_eglUpdateExtensionsString(_EGLDriver *drv)
-{
-   drv->Extensions.String[0] = 0;
 
-   if (drv->Extensions.MESA_screen_surface)
-      strcat(drv->Extensions.String, "EGL_MESA_screen_surface ");
-   if (drv->Extensions.MESA_copy_context)
-      strcat(drv->Extensions.String, "EGL_MESA_copy_context ");
-   assert(strlen(drv->Extensions.String) < _EGL_MAX_EXTENSIONS_LEN);
+#ifdef EGL_KHR_image_base
+   drv->API.CreateImageKHR = _eglCreateImageKHR;
+   drv->API.DestroyImageKHR = _eglDestroyImageKHR;
+#endif /* EGL_KHR_image_base */
 }
 
 
 
-const char *
-_eglQueryString(_EGLDriver *drv, EGLDisplay dpy, EGLint name)
+/**
+ * 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)
 {
-   (void) drv;
-   (void) dpy;
-   switch (name) {
-   case EGL_VENDOR:
-      return _EGL_VENDOR_STRING;
-   case EGL_VERSION:
-      return _EGL_VERSION_STRING;
-   case EGL_EXTENSIONS:
-      _eglUpdateExtensionsString(drv);
-      return drv->Extensions.String;
-#ifdef EGL_VERSION_1_2
-   case EGL_CLIENT_APIS:
-      /* XXX need to initialize somewhere */
-      return drv->ClientAPIs;
+   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_X)
+   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";
 #endif
-   default:
-      _eglError(EGL_BAD_PARAMETER, "eglQueryString");
-      return NULL;
-   }
-}
 
+   if ((lib = open_library(es1_libname))) {
+      close_library(lib);
+      mask |= EGL_OPENGL_ES_BIT;
+   }
 
-EGLBoolean
-_eglWaitGL(_EGLDriver *drv, EGLDisplay dpy)
-{
-   /* just a placeholder */
-   (void) drv;
-   (void) dpy;
-   return EGL_TRUE;
-}
+   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;
+   }
 
-EGLBoolean
-_eglWaitNative(_EGLDriver *drv, EGLDisplay dpy, EGLint engine)
-{
-   /* just a placeholder */
-   (void) drv;
-   (void) dpy;
-   switch (engine) {
-   case EGL_CORE_NATIVE_ENGINE:
-      break;
-   default:
-      _eglError(EGL_BAD_PARAMETER, "eglWaitNative(engine)");
-      return EGL_FALSE;
+   if ((lib = open_library(vg_libname))) {
+      close_library(lib);
+      mask |= EGL_OPENVG_BIT;
    }
 
-   return EGL_TRUE;
+   return mask;
 }