freedreno: one screen to rule them all
authorRob Clark <robclark@freedesktop.org>
Fri, 4 Sep 2015 15:35:33 +0000 (11:35 -0400)
committerRob Clark <robclark@freedesktop.org>
Wed, 16 Sep 2015 13:14:39 +0000 (09:14 -0400)
Similar to fee0686c21c631d96d6042741267a3c218c23ffc, but in this case to
ensure that drm_gralloc and libGLES_mesa are sharing a single screen.

Bumps libdrm_freedreno version dependency, as it requires the new
fd_device_fd() API.

Signed-off-by: Rob Clark <robclark@freedesktop.org>
configure.ac
src/gallium/drivers/freedreno/freedreno_screen.c
src/gallium/drivers/freedreno/freedreno_screen.h
src/gallium/targets/dri/dri.sym
src/gallium/winsys/freedreno/drm/freedreno_drm_winsys.c

index d8e56439a54a815fec8988cc6010c7232ba534ea..71c9b6c97fa7f7a2e67e0062bc26dfe5925837b8 100644 (file)
@@ -74,7 +74,7 @@ LIBDRM_AMDGPU_REQUIRED=2.4.63
 LIBDRM_INTEL_REQUIRED=2.4.61
 LIBDRM_NVVIEUX_REQUIRED=2.4.33
 LIBDRM_NOUVEAU_REQUIRED=2.4.62
-LIBDRM_FREEDRENO_REQUIRED=2.4.64
+LIBDRM_FREEDRENO_REQUIRED=2.4.65
 DRI2PROTO_REQUIRED=2.6
 DRI3PROTO_REQUIRED=1.0
 PRESENTPROTO_REQUIRED=1.0
index 8000279ae806fa5b5a55c68d557fd5ff9111dbd8..a3dede2500e20ae260a18438a324915813feb3dc 100644 (file)
@@ -483,6 +483,7 @@ fd_screen_create(struct fd_device *dev)
        pscreen = &screen->base;
 
        screen->dev = dev;
+       screen->refcnt = 1;
 
        // maybe this should be in context?
        screen->pipe = fd_pipe_new(screen->dev, FD_PIPE_3D);
index 4e5c3a619580bb2143648d7421f9505fef14bd20..8fb096a10ddf48071e32ef32c960bf58d5ac4e1a 100644 (file)
@@ -42,6 +42,16 @@ struct fd_bo;
 struct fd_screen {
        struct pipe_screen base;
 
+       /* it would be tempting to use pipe_reference here, but that
+        * really doesn't work well if it isn't the first member of
+        * the struct, so not quite so awesome to be adding refcnting
+        * further down the inheritance hierarchy:
+        */
+       int refcnt;
+
+       /* place for winsys to stash it's own stuff: */
+       void *winsys_priv;
+
        uint32_t gmemsize_bytes;
        uint32_t device_id;
        uint32_t gpu_id;         /* 220, 305, etc */
index 8e26fb960b7a820db0fe219cd6acfcd193c3fae6..1fdf18beee76734152c6cec3d48f758941482682 100644 (file)
@@ -5,6 +5,7 @@
                nouveau_drm_screen_create;
                radeon_drm_winsys_create;
                amdgpu_winsys_create;
+               fd_drm_screen_create;
        local:
                *;
 };
index 9eb974451d1244cfec117c37b19aa998b82ca45d..e4785f83d96316d21c968baabaff4547c55f0c0a 100644 (file)
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors:
+ *    Rob Clark <robclark@freedesktop.org>
+ */
+
+#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 "freedreno_drm_public.h"
 
 #include "freedreno/freedreno_screen.h"
 
+static struct util_hash_table *fd_tab = NULL;
+
+pipe_static_mutex(fd_screen_mutex);
+
+static void
+fd_drm_screen_destroy(struct pipe_screen *pscreen)
+{
+       struct fd_screen *screen = fd_screen(pscreen);
+       boolean destroy;
+
+       pipe_mutex_lock(fd_screen_mutex);
+       destroy = --screen->refcnt == 0;
+       if (destroy) {
+               int fd = fd_device_fd(screen->dev);
+               util_hash_table_remove(fd_tab, intptr_to_pointer(fd));
+       }
+       pipe_mutex_unlock(fd_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 *
 fd_drm_screen_create(int fd)
 {
-       struct fd_device *dev = fd_device_new_dup(fd);
-       if (!dev)
-               return NULL;
-       return fd_screen_create(dev);
+       struct pipe_screen *pscreen = NULL;
+
+       pipe_mutex_lock(fd_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) {
+               fd_screen(pscreen)->refcnt++;
+       } else {
+               struct fd_device *dev = fd_device_new_dup(fd);
+               if (!dev)
+                       goto unlock;
+
+               pscreen = fd_screen_create(dev);
+               if (pscreen) {
+                       int fd = fd_device_fd(dev);
+
+                       util_hash_table_set(fd_tab, intptr_to_pointer(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():
+                        */
+                       fd_screen(pscreen)->winsys_priv = pscreen->destroy;
+                       pscreen->destroy = fd_drm_screen_destroy;
+               }
+       }
+
+unlock:
+       pipe_mutex_unlock(fd_screen_mutex);
+       return pscreen;
 }