1 /*
2 * Copyright © 2017 Lima Project
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27
28 #include "c11/threads.h"
29 #include "util/os_file.h"
30 #include "util/u_hash_table.h"
31 #include "util/u_pointer.h"
32 #include "renderonly/renderonly.h"
33
34 #include "lima_drm_public.h"
35
36 #include "lima/lima_screen.h"
37
38 static struct hash_table *fd_tab = NULL;
39 static mtx_t lima_screen_mutex = _MTX_INITIALIZER_NP;
40
41 static void
lima_drm_screen_destroy(struct pipe_screen * pscreen)42 lima_drm_screen_destroy(struct pipe_screen *pscreen)
43 {
44 struct lima_screen *screen = lima_screen(pscreen);
45 boolean destroy;
46 int fd = screen->fd;
47
48 mtx_lock(&lima_screen_mutex);
49 destroy = --screen->refcnt == 0;
50 if (destroy)
51 _mesa_hash_table_remove_key(fd_tab, intptr_to_pointer(fd));
52 mtx_unlock(&lima_screen_mutex);
53
54 if (destroy) {
55 pscreen->destroy = screen->winsys_priv;
56 pscreen->destroy(pscreen);
57 close(fd);
58 }
59 }
60
61 struct pipe_screen *
lima_drm_screen_create(int fd)62 lima_drm_screen_create(int fd)
63 {
64 struct pipe_screen *pscreen = NULL;
65
66 mtx_lock(&lima_screen_mutex);
67 if (!fd_tab) {
68 fd_tab = util_hash_table_create_fd_keys();
69 if (!fd_tab)
70 goto unlock;
71 }
72
73 pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
74 if (pscreen) {
75 lima_screen(pscreen)->refcnt++;
76 } else {
77 int dup_fd = os_dupfd_cloexec(fd);
78
79 pscreen = lima_screen_create(dup_fd, NULL);
80 if (pscreen) {
81 _mesa_hash_table_insert(fd_tab, intptr_to_pointer(dup_fd), pscreen);
82
83 /* Bit of a hack, to avoid circular linkage dependency,
84 * ie. pipe driver having to call in to winsys, we
85 * override the pipe drivers screen->destroy():
86 */
87 lima_screen(pscreen)->winsys_priv = pscreen->destroy;
88 pscreen->destroy = lima_drm_screen_destroy;
89 }
90 }
91
92 unlock:
93 mtx_unlock(&lima_screen_mutex);
94 return pscreen;
95 }
96
97 struct pipe_screen *
lima_drm_screen_create_renderonly(struct renderonly * ro)98 lima_drm_screen_create_renderonly(struct renderonly *ro)
99 {
100 return lima_screen_create(os_dupfd_cloexec(ro->gpu_fd), ro);
101 }
102