nouveau: create only 1 shared screen between vdpau and opengl
authorMaarten Lankhorst <maarten.lankhorst@canonical.com>
Wed, 12 Feb 2014 13:56:53 +0000 (14:56 +0100)
committerMaarten Lankhorst <maarten.lankhorst@canonical.com>
Wed, 12 Feb 2014 13:57:25 +0000 (14:57 +0100)
This fixes bug 73200 "vdpau-GL interop fails due to different screen
objects" in the same way radeon does.

Signed-off-by: Maarten Lankhorst <maarten.lankhorst@canonical.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
src/gallium/drivers/nouveau/nouveau_screen.c
src/gallium/drivers/nouveau/nouveau_screen.h
src/gallium/drivers/nouveau/nv30/nv30_screen.c
src/gallium/drivers/nouveau/nv50/nv50_screen.c
src/gallium/drivers/nouveau/nvc0/nvc0_screen.c
src/gallium/targets/dri-nouveau/Makefile.am
src/gallium/targets/dri-nouveau/nouveau_dri.link [new file with mode: 0644]
src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c

index 21b31e04f0a79b912494962a2d20254f1ad77744..f742a9483b192f2dc3f74258b8f54a1571a7bb6d 100644 (file)
@@ -144,6 +144,12 @@ nouveau_screen_init(struct nouveau_screen *screen, struct nouveau_device *dev)
        if (nv_dbg)
           nouveau_mesa_debug = atoi(nv_dbg);
 
+       /*
+        * this is initialized to 1 in nouveau_drm_screen_create after screen
+        * is fully constructed and added to the global screen list.
+        */
+       screen->refcount = -1;
+
        if (dev->chipset < 0xc0) {
                data = &nv04_data;
                size = sizeof(nv04_data);
index 51e24fa05a473bd82348139147d289448cb361d5..cf06f7e88aa0b848355d79b820b77b648c62a52c 100644 (file)
@@ -22,6 +22,8 @@ struct nouveau_screen {
        struct nouveau_client *client;
        struct nouveau_pushbuf *pushbuf;
 
+       int refcount;
+
        unsigned vidmem_bindings; /* PIPE_BIND_* where VRAM placement is desired */
        unsigned sysmem_bindings; /* PIPE_BIND_* where GART placement is desired */
        unsigned lowmem_bindings; /* PIPE_BIND_* that require an address < 4 GiB */
@@ -112,6 +114,8 @@ nouveau_screen(struct pipe_screen *pscreen)
        return (struct nouveau_screen *)pscreen;
 }
 
+boolean nouveau_drm_screen_unref(struct nouveau_screen *screen);
+
 boolean
 nouveau_screen_bo_get_handle(struct pipe_screen *pscreen,
                             struct nouveau_bo *bo,
index c027a5f3130434aa928d6195ed66ecfb6136c0dc..9854708b371ac0cd8afbee4f69aaa9da4ec3bb85 100644 (file)
@@ -302,6 +302,9 @@ nv30_screen_destroy(struct pipe_screen *pscreen)
 {
    struct nv30_screen *screen = nv30_screen(pscreen);
 
+   if (!nouveau_drm_screen_unref(&screen->base))
+      return;
+
    if (screen->base.fence.current &&
        screen->base.fence.current->state >= NOUVEAU_FENCE_STATE_EMITTED) {
       nouveau_fence_wait(screen->base.fence.current);
index e636bf8be33d9b35e22a5b4bef3238aa4500fd7f..db3265fb29e95ad5d4f52bc5d6731c8cca094ca7 100644 (file)
@@ -287,6 +287,9 @@ nv50_screen_destroy(struct pipe_screen *pscreen)
 {
    struct nv50_screen *screen = nv50_screen(pscreen);
 
+   if (!nouveau_drm_screen_unref(&screen->base))
+      return;
+
    if (screen->base.fence.current) {
       nouveau_fence_wait(screen->base.fence.current);
       nouveau_fence_ref (NULL, &screen->base.fence.current);
index 28d9be24bbe23e50466ecf5f79651e5ffcbbf45f..f04771de452ebbd16702ec4167dbf1b5243be8ac 100644 (file)
@@ -333,6 +333,9 @@ nvc0_screen_destroy(struct pipe_screen *pscreen)
 {
    struct nvc0_screen *screen = nvc0_screen(pscreen);
 
+   if (!nouveau_drm_screen_unref(&screen->base))
+      return;
+
    if (screen->base.fence.current) {
       nouveau_fence_wait(screen->base.fence.current);
       nouveau_fence_ref(NULL, &screen->base.fence.current);
index 19880679bae0088cd2fc3a2f00aa3f408befd7fb..4bd4e21953c6f562ba4a0ff8f1eff1c4c2c19323 100644 (file)
@@ -20,6 +20,7 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 # DEALINGS IN THE SOFTWARE.
 
+DRI_VERSION_SCRIPT = $(srcdir)/nouveau_dri.link
 include $(top_srcdir)/src/gallium/Automake.inc
 
 AM_CFLAGS = \
diff --git a/src/gallium/targets/dri-nouveau/nouveau_dri.link b/src/gallium/targets/dri-nouveau/nouveau_dri.link
new file mode 100644 (file)
index 0000000..16015aa
--- /dev/null
@@ -0,0 +1,6 @@
+VERSION {
+       global:
+               __driDriverExtensions;
+               nouveau_drm_screen_create;
+       local: *;
+};
index e4f27f6cb249f159b191b942d630bc7f6bd0525d..a077c48c2066e5afc7b3f94cf0891333291bde8d 100644 (file)
@@ -1,24 +1,83 @@
+#include <sys/stat.h>
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
 #include "util/u_format.h"
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
+#include "util/u_hash_table.h"
+#include "os/os_thread.h"
 
 #include "nouveau_drm_public.h"
 
 #include "nouveau/nouveau_winsys.h"
 #include "nouveau/nouveau_screen.h"
 
-struct pipe_screen *
+static struct util_hash_table *fd_tab = NULL;
+
+pipe_static_mutex(nouveau_screen_mutex);
+
+boolean nouveau_drm_screen_unref(struct nouveau_screen *screen)
+{
+       int ret;
+       if (screen->refcount == -1)
+               return true;
+
+       pipe_mutex_lock(nouveau_screen_mutex);
+       ret = --screen->refcount;
+       assert(ret >= 0);
+       if (ret == 0)
+               util_hash_table_remove(fd_tab, intptr_to_pointer(screen->device->fd));
+       pipe_mutex_unlock(nouveau_screen_mutex);
+       return ret == 0;
+}
+
+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;
+}
+
+PUBLIC struct pipe_screen *
 nouveau_drm_screen_create(int fd)
 {
        struct nouveau_device *dev = NULL;
        struct pipe_screen *(*init)(struct nouveau_device *);
+       struct nouveau_screen *screen;
        int ret;
 
+       pipe_mutex_lock(nouveau_screen_mutex);
+       if (!fd_tab) {
+               fd_tab = util_hash_table_create(hash_fd, compare_fd);
+               if (!fd_tab)
+                       goto err;
+       }
+
+       screen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
+       if (screen) {
+               screen->refcount++;
+               pipe_mutex_unlock(nouveau_screen_mutex);
+               return &screen->base;
+       }
+
        ret = nouveau_device_wrap(fd, 0, &dev);
        if (ret)
-               return NULL;
+               goto err;
 
        switch (dev->chipset & ~0xf) {
        case 0x30:
@@ -42,8 +101,19 @@ nouveau_drm_screen_create(int fd)
        default:
                debug_printf("%s: unknown chipset nv%02x\n", __func__,
                             dev->chipset);
-               return NULL;
+               goto err;
        }
 
-       return init(dev);
+       screen = (struct nouveau_screen*)init(dev);
+       if (!screen)
+               goto err;
+
+       util_hash_table_set(fd_tab, intptr_to_pointer(fd), screen);
+       screen->refcount = 1;
+       pipe_mutex_unlock(nouveau_screen_mutex);
+       return &screen->base;
+
+err:
+       pipe_mutex_unlock(nouveau_screen_mutex);
+       return NULL;
 }