Merge branch 'mesa_7_5_branch'
[mesa.git] / src / egl / drivers / dri / egldri.c
index 79a2eda3aefd4b3e406a80fe03fe9f341b9430f9..9e400be6248b805b0f445703a88953eb79cc3b00 100644 (file)
@@ -1,5 +1,6 @@
 /**
- * Generic EGL driver for DRI.
+ * Generic EGL driver for DRI.  This is basically an "adaptor" driver
+ * that allows libEGL to load/use regular DRI drivers.
  *
  * This file contains all the code needed to interface DRI-based drivers
  * with libEGL.
@@ -37,14 +38,68 @@ const char *sysfs = "/sys/class";
 static const int empty_attribute_list[1] = { None };
 
 
+
+/**
+ * Given a card number, return the name of the DRI driver to use.
+ * This generally means reading the contents of
+ * /sys/class/drm/cardX/dri_library_name, where X is the card number
+ */
+static EGLBoolean
+driver_name_from_card_number(int card, char *driverName, int maxDriverName)
+{
+   char path[2000];
+   FILE *f;
+   int length;
+
+   snprintf(path, sizeof(path), "%s/drm/card%d/dri_library_name", sysfs, card);
+
+   f = fopen(path, "r");
+   if (!f)
+      return EGL_FALSE;
+
+   fgets(driverName, maxDriverName, f);
+   fclose(f);
+
+   if ((length = strlen(driverName)) > 1) {
+      /* remove the trailing newline from sysfs */
+      driverName[length - 1] = '\0';
+      strncat(driverName, "_dri", maxDriverName);
+      return EGL_TRUE;
+   }
+   else {
+      return EGL_FALSE;
+   }   
+}
+
+
+
 /**
  * The bootstrap function.
  * Return a new driDriver object and plug in API functions.
  * This function, in turn, loads a specific DRI driver (ex: r200_dri.so).
  */
 _EGLDriver *
-_eglMain(_EGLDisplay *dpy)
+_eglMain(_EGLDisplay *dpy, const char *args)
 {
+#if 1
+   const int card = args ? atoi(args) : 0;
+   _EGLDriver *driver = NULL;
+   char driverName[1000];
+
+   if (!driver_name_from_card_number(card, driverName, sizeof(driverName))) {
+      _eglLog(_EGL_WARNING,
+              "Unable to determine driver name for card %d\n", card);
+      return NULL;
+   }
+
+   _eglLog(_EGL_DEBUG, "Driver name: %s\n", driverName);
+
+   driver = _eglOpenDriver(dpy, driverName, args);
+
+   return driver;
+
+#else
+
    int length;
    char path[NAME_MAX];
    struct dirent *dirent;
@@ -59,15 +114,19 @@ _eglMain(_EGLDisplay *dpy)
       _eglLog(_EGL_WARNING, "%s DRM devices not found.", path);
       return EGL_FALSE;
    }
+
+   /* loop over dir entries looking for cardX where "X" is in the
+    * dpy->DriverName ":X" string.
+    */
    while ((dirent = readdir(dir))) {
 
       if (strncmp(&dirent->d_name[0], "card", 4) != 0)
          continue;
-      if (strcmp(&dirent->d_name[4], &dpy->DriverName[1]) != 0)
+      if (strcmp(&dirent->d_name[4], &driverName[1]) != 0)
          continue;
 
       snprintf(path, sizeof(path), "%s/drm/card%s/dri_library_name",
-               sysfs, &dpy->DriverName[1]);
+               sysfs, &driverName[1]);
       _eglLog(_EGL_INFO, "Opening %s", path);
 #if 1
       file = fopen(path, "r");
@@ -91,6 +150,7 @@ _eglMain(_EGLDisplay *dpy)
    closedir(dir);
 
    return driver;
+#endif
 }
 
 
@@ -111,7 +171,10 @@ _eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
    if (!c)
       return EGL_NO_CONTEXT;
 
-   if (!_eglInitContext(drv, dpy, &c->Base, config, attrib_list)) {
+   conf = _eglLookupConfig(drv, dpy, config);
+   assert(conf);
+
+   if (!_eglInitContext(drv, &c->Base, conf, attrib_list)) {
       free(c);
       return EGL_NO_CONTEXT;
    }
@@ -129,8 +192,6 @@ _eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
    else
       sharePriv = NULL;
 
-   conf = _eglLookupConfig(drv, dpy, config);
-   assert(conf);
    _eglConfigToContextModesRec(conf, &visMode);
 
    c->driContext.private = disp->driScreen.createNewContext(disp, &visMode,
@@ -140,8 +201,8 @@ _eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
       return EGL_FALSE;
    }
 
-   /* generate handle and insert into hash table */
-   _eglSaveContext(&c->Base);
+   /* link to display */
+   _eglLinkContext(&c->Base, &disp->Base);
 
    return _eglGetContextHandle(&c->Base);
 }
@@ -177,14 +238,18 @@ _eglDRICreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
                             const EGLint *attrib_list)
 {
    driSurface *surf;
+   _EGLConfig *conf;
+
+   conf = _eglLookupConfig(drv, dpy, config);
+   assert(conf);
 
    surf = (driSurface *) calloc(1, sizeof(*surf));
    if (!surf) {
       return EGL_NO_SURFACE;
    }
 
-   if (!_eglInitSurface(drv, dpy, &surf->Base, EGL_PBUFFER_BIT,
-                        config, attrib_list)) {
+   if (!_eglInitSurface(drv, &surf->Base, EGL_PBUFFER_BIT,
+                        conf, attrib_list)) {
       free(surf);
       return EGL_NO_SURFACE;
    }
@@ -215,7 +280,7 @@ _eglDRICreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
 #endif
    }
 
-   _eglSaveSurface(&surf->Base);
+   _eglLinkSurface(&surf->Base, _eglLookupDisplay(dpy));
 
    return surf->Base.Handle;
 }
@@ -227,16 +292,12 @@ _eglDRIDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
    driDisplay *disp = Lookup_driDisplay(dpy);
    driSurface *fs = Lookup_driSurface(surface);
 
-   _eglRemoveSurface(&fs->Base);
+   _eglUnlinkSurface(&fs->Base);
 
    fs->drawable.destroyDrawable(disp, fs->drawable.private);
 
-   if (fs->Base.IsBound) {
-      fs->Base.DeletePending = EGL_TRUE;
-   }
-   else {
+   if (!_eglIsSurfaceBound(&fs->Base))
       free(fs);
-   }
    return EGL_TRUE;
 }
 
@@ -247,16 +308,12 @@ _eglDRIDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
    driDisplay *disp = Lookup_driDisplay(dpy);
    driContext *fc = Lookup_driContext(context);
 
-   _eglRemoveContext(&fc->Base);
+   _eglUnlinkContext(&fc->Base);
 
    fc->driContext.destroyContext(disp, 0, fc->driContext.private);
 
-   if (fc->Base.IsBound) {
-      fc->Base.DeletePending = EGL_TRUE;
-   }
-   else {
+   if (!_eglIsContextBound(&fc->Base))
       free(fc);
-   }
    return EGL_TRUE;
 }
 
@@ -280,13 +337,13 @@ _eglDRICreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
    }
 
    /* init base class, do error checking, etc. */
-   if (!_eglInitSurface(drv, dpy, &surface->Base, EGL_SCREEN_BIT_MESA,
-                        cfg, attrib_list)) {
+   if (!_eglInitSurface(drv, &surface->Base, EGL_SCREEN_BIT_MESA,
+                        config, attrib_list)) {
       free(surface);
       return EGL_NO_SURFACE;
    }
 
-   _eglSaveSurface(&surface->Base);
+   _eglLinkSurface(&surface->Base &disp->Base);
 
 
    /*
@@ -303,7 +360,7 @@ _eglDRICreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
    if (!disp->driScreen.createNewDrawable(disp, &visMode, drawBuf,
                                           &surface->drawable, GLX_WINDOW_BIT,
                                           empty_attribute_list)) {
-      _eglRemoveSurface(&surface->Base);
+      _eglUnlinkSurface(&surface->Base);
       free(surface);
       return EGL_NO_SURFACE;
    }
@@ -1026,8 +1083,10 @@ _eglDRICreateDisplay(driDisplay *dpy, __DRIframebuffer *framebuffer)
                             api_ver,
                             & interface_methods,
                             NULL);
-   if (!dpy->driScreen.private)
+   if (!dpy->driScreen.private) {
+      _eglLog(_EGL_WARNING, "egldri.c: DRI create new screen failed");
       return EGL_FALSE;
+   }
 
    DRM_UNLOCK( dpy->drmFD, dpy->pSAREA, dpy->serverContext );
 
@@ -1087,6 +1146,7 @@ _eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy,
 {
    _EGLDisplay *disp = _eglLookupDisplay(dpy);
    driDisplay *display;
+   const char *driverName = (const char *) disp->NativeDisplay;
 
    assert(disp);
 
@@ -1101,7 +1161,7 @@ _eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy,
    *major = 1;
    *minor = 0;
 
-   sscanf(&disp->DriverName[1], "%d", &display->minor);
+   sscanf(driverName + 1, "%d", &display->minor);
 
    drv->Initialized = EGL_TRUE;
    return EGL_TRUE;