util: Move gallium's PIPE_FORMAT utils to /util/format/
[mesa.git] / src / gallium / winsys / freedreno / drm / freedreno_drm_winsys.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <sys/stat.h>
28
29 #include "pipe/p_context.h"
30 #include "pipe/p_state.h"
31 #include "util/format/u_format.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
34 #include "util/u_hash_table.h"
35 #include "os/os_thread.h"
36
37 #include "freedreno_drm_public.h"
38
39 #include "freedreno/freedreno_screen.h"
40
41 static struct util_hash_table *fd_tab = NULL;
42
43 static mtx_t fd_screen_mutex = _MTX_INITIALIZER_NP;
44
45 static void
46 fd_drm_screen_destroy(struct pipe_screen *pscreen)
47 {
48 struct fd_screen *screen = fd_screen(pscreen);
49 boolean destroy;
50
51 mtx_lock(&fd_screen_mutex);
52 destroy = --screen->refcnt == 0;
53 if (destroy) {
54 int fd = fd_device_fd(screen->dev);
55 util_hash_table_remove(fd_tab, intptr_to_pointer(fd));
56 }
57 mtx_unlock(&fd_screen_mutex);
58
59 if (destroy) {
60 pscreen->destroy = screen->winsys_priv;
61 pscreen->destroy(pscreen);
62 }
63 }
64
65 static unsigned hash_fd(void *key)
66 {
67 int fd = pointer_to_intptr(key);
68 struct stat stat;
69 fstat(fd, &stat);
70
71 return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
72 }
73
74 static int compare_fd(void *key1, void *key2)
75 {
76 int fd1 = pointer_to_intptr(key1);
77 int fd2 = pointer_to_intptr(key2);
78 struct stat stat1, stat2;
79 fstat(fd1, &stat1);
80 fstat(fd2, &stat2);
81
82 return stat1.st_dev != stat2.st_dev ||
83 stat1.st_ino != stat2.st_ino ||
84 stat1.st_rdev != stat2.st_rdev;
85 }
86
87 struct pipe_screen *
88 fd_drm_screen_create(int fd, struct renderonly *ro)
89 {
90 struct pipe_screen *pscreen = NULL;
91
92 mtx_lock(&fd_screen_mutex);
93 if (!fd_tab) {
94 fd_tab = util_hash_table_create(hash_fd, compare_fd);
95 if (!fd_tab)
96 goto unlock;
97 }
98
99 pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
100 if (pscreen) {
101 fd_screen(pscreen)->refcnt++;
102 } else {
103 struct fd_device *dev = fd_device_new_dup(fd);
104 if (!dev)
105 goto unlock;
106
107 pscreen = fd_screen_create(dev, ro);
108 if (pscreen) {
109 int fd = fd_device_fd(dev);
110
111 util_hash_table_set(fd_tab, intptr_to_pointer(fd), pscreen);
112
113 /* Bit of a hack, to avoid circular linkage dependency,
114 * ie. pipe driver having to call in to winsys, we
115 * override the pipe drivers screen->destroy():
116 */
117 fd_screen(pscreen)->winsys_priv = pscreen->destroy;
118 pscreen->destroy = fd_drm_screen_destroy;
119 }
120 }
121
122 unlock:
123 mtx_unlock(&fd_screen_mutex);
124 return pscreen;
125 }