virgl: reuse screen when fd is already open
authorRob Herring <robh@kernel.org>
Fri, 29 Jan 2016 22:36:28 +0000 (16:36 -0600)
committerDave Airlie <airlied@redhat.com>
Mon, 1 Feb 2016 23:58:29 +0000 (09:58 +1000)
It is necessary to share the screen between mesa and gralloc to
properly ref count resources. This implements a hash lookup on
the file description to re-use an already created screen. This is
a similar implementation as freedreno and radeon.

Signed-off-by: Rob Herring <robh@kernel.org>
Signed-off-by: Dave Airlie <airlied@redhat.com>
src/gallium/auxiliary/target-helpers/drm_helper.h
src/gallium/drivers/virgl/virgl_screen.c
src/gallium/drivers/virgl/virgl_screen.h
src/gallium/winsys/virgl/drm/virgl_drm_public.h
src/gallium/winsys/virgl/drm/virgl_drm_winsys.c

index 332b1cba9849d18c6fe79cc7f59a1cd9630d6f03..90820d3fe9101ea28ae4fa8f693292fb27ba2c13 100644 (file)
@@ -226,14 +226,9 @@ pipe_freedreno_create_screen(int fd)
 struct pipe_screen *
 pipe_virgl_create_screen(int fd)
 {
-   struct virgl_winsys *vws;
    struct pipe_screen *screen;
 
-   vws = virgl_drm_winsys_create(fd);
-   if (!vws)
-      return NULL;
-
-   screen = virgl_create_screen(vws);
+   screen = virgl_drm_screen_create(fd);
    return screen ? debug_screen_wrap(screen) : NULL;
 }
 
index fb2e5670ef0709c39b015ab1e60c6bfaa1451d36..d7d8735bf776f9dfe0ca543cf7928298bddc0a7a 100644 (file)
@@ -557,6 +557,7 @@ virgl_create_screen(struct virgl_winsys *vws)
 
    vws->get_caps(vws, &screen->caps);
 
+   screen->refcnt = 1;
 
    util_format_s3tc_init();
    return &screen->base;
index 52e72ca495823949ce18e2ded1ee865567872080..8cac38d7e96d5903209308c736c0a6e7a84a5747 100644 (file)
 
 struct virgl_screen {
    struct pipe_screen base;
+
+   int refcnt;
+
+   /* place for winsys to stash it's own stuff: */
+   void *winsys_priv;
+
    struct virgl_winsys *vws;
 
    struct virgl_drm_caps caps;
index be01021ca9ad639efa24000f358680f7f9e27aa7..f70f0e50448f1035e1d75edfc7d1cb2199f585f0 100644 (file)
@@ -23,8 +23,8 @@
 #ifndef VIRGL_DRM_PUBLIC_H
 #define VIRGL_DRM_PUBLIC_H
 
-struct virgl_winsys;
+struct pipe_screen;
 
-struct virgl_winsys *virgl_drm_winsys_create(int drmFD);
+struct pipe_screen *virgl_drm_screen_create(int fd);
 
 #endif
index b5d4435e5e6560c5e341dfa0a78d9555c3788197..48a79faa4bb13c82ea1920c432f2d8721942eab2 100644 (file)
@@ -25,6 +25,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <sys/ioctl.h>
+#include <sys/stat.h>
 
 #include "os/os_mman.h"
 #include "os/os_time.h"
@@ -33,6 +34,8 @@
 #include "util/u_hash_table.h"
 #include "util/u_inlines.h"
 #include "state_tracker/drm_driver.h"
+#include "virgl/virgl_screen.h"
+#include "virgl/virgl_public.h"
 
 #include <xf86drm.h>
 #include "virtgpu_drm.h"
@@ -772,3 +775,87 @@ virgl_drm_winsys_create(int drmFD)
    return &qdws->base;
 
 }
+
+static struct util_hash_table *fd_tab = NULL;
+pipe_static_mutex(virgl_screen_mutex);
+
+static void
+virgl_drm_screen_destroy(struct pipe_screen *pscreen)
+{
+   struct virgl_screen *screen = virgl_screen(pscreen);
+   boolean destroy;
+
+   pipe_mutex_lock(virgl_screen_mutex);
+   destroy = --screen->refcnt == 0;
+   if (destroy) {
+      int fd = virgl_drm_winsys(screen->vws)->fd;
+      util_hash_table_remove(fd_tab, intptr_to_pointer(fd));
+   }
+   pipe_mutex_unlock(virgl_screen_mutex);
+
+   if (destroy) {
+      pscreen->destroy = screen->winsys_priv;
+      pscreen->destroy(pscreen);
+   }
+}
+
+static unsigned hash_fd(void *key)
+{
+   int fd = pointer_to_intptr(key);
+   struct stat stat;
+   fstat(fd, &stat);
+
+   return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
+}
+
+static int compare_fd(void *key1, void *key2)
+{
+   int fd1 = pointer_to_intptr(key1);
+   int fd2 = pointer_to_intptr(key2);
+   struct stat stat1, stat2;
+   fstat(fd1, &stat1);
+   fstat(fd2, &stat2);
+
+   return stat1.st_dev != stat2.st_dev ||
+         stat1.st_ino != stat2.st_ino ||
+         stat1.st_rdev != stat2.st_rdev;
+}
+
+struct pipe_screen *
+virgl_drm_screen_create(int fd)
+{
+   struct pipe_screen *pscreen = NULL;
+
+   pipe_mutex_lock(virgl_screen_mutex);
+   if (!fd_tab) {
+      fd_tab = util_hash_table_create(hash_fd, compare_fd);
+      if (!fd_tab)
+         goto unlock;
+   }
+
+   pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
+   if (pscreen) {
+      virgl_screen(pscreen)->refcnt++;
+   } else {
+      struct virgl_winsys *vws;
+      int dup_fd = dup(fd);
+
+      vws = virgl_drm_winsys_create(dup_fd);
+
+      pscreen = virgl_create_screen(vws);
+      if (pscreen) {
+         util_hash_table_set(fd_tab, intptr_to_pointer(dup_fd), pscreen);
+
+         /* Bit of a hack, to avoid circular linkage dependency,
+          * ie. pipe driver having to call in to winsys, we
+          * override the pipe drivers screen->destroy():
+          */
+         virgl_screen(pscreen)->winsys_priv = pscreen->destroy;
+         pscreen->destroy = virgl_drm_screen_destroy;
+      }
+   }
+
+unlock:
+   pipe_mutex_unlock(virgl_screen_mutex);
+   return pscreen;
+}