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/u_hash_table.h"
30#include "util/u_pointer.h"
31#include "renderonly/renderonly.h"
32
33#include "lima_drm_public.h"
34
35#include "lima/lima_screen.h"
36
37static struct util_hash_table *fd_tab = NULL;
38static mtx_t lima_screen_mutex = _MTX_INITIALIZER_NP;
39
40static void
41lima_drm_screen_destroy(struct pipe_screen *pscreen)
42{
43   struct lima_screen *screen = lima_screen(pscreen);
44   boolean destroy;
45   int fd = screen->fd;
46
47   mtx_lock(&lima_screen_mutex);
48   destroy = --screen->refcnt == 0;
49   if (destroy)
50      util_hash_table_remove(fd_tab, intptr_to_pointer(fd));
51   mtx_unlock(&lima_screen_mutex);
52
53   if (destroy) {
54      pscreen->destroy = screen->winsys_priv;
55      pscreen->destroy(pscreen);
56      close(fd);
57   }
58}
59
60static unsigned hash_fd(void *key)
61{
62   int fd = pointer_to_intptr(key);
63   struct stat stat;
64
65   fstat(fd, &stat);
66
67   return stat.st_dev ^ stat.st_ino ^ stat.st_rdev;
68}
69
70static int compare_fd(void *key1, void *key2)
71{
72   int fd1 = pointer_to_intptr(key1);
73   int fd2 = pointer_to_intptr(key2);
74   struct stat stat1, stat2;
75
76   fstat(fd1, &stat1);
77   fstat(fd2, &stat2);
78
79   return stat1.st_dev != stat2.st_dev ||
80          stat1.st_ino != stat2.st_ino ||
81          stat1.st_rdev != stat2.st_rdev;
82}
83
84struct pipe_screen *
85lima_drm_screen_create(int fd)
86{
87   struct pipe_screen *pscreen = NULL;
88
89   mtx_lock(&lima_screen_mutex);
90   if (!fd_tab) {
91      fd_tab = util_hash_table_create(hash_fd, compare_fd);
92      if (!fd_tab)
93         goto unlock;
94   }
95
96   pscreen = util_hash_table_get(fd_tab, intptr_to_pointer(fd));
97   if (pscreen) {
98      lima_screen(pscreen)->refcnt++;
99   } else {
100      int dup_fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
101
102      pscreen = lima_screen_create(dup_fd, NULL);
103      if (pscreen) {
104         util_hash_table_set(fd_tab, intptr_to_pointer(dup_fd), pscreen);
105
106         /* Bit of a hack, to avoid circular linkage dependency,
107          * ie. pipe driver having to call in to winsys, we
108          * override the pipe drivers screen->destroy():
109          */
110         lima_screen(pscreen)->winsys_priv = pscreen->destroy;
111         pscreen->destroy = lima_drm_screen_destroy;
112      }
113   }
114
115unlock:
116   mtx_unlock(&lima_screen_mutex);
117   return pscreen;
118}
119
120struct pipe_screen *
121lima_drm_screen_create_renderonly(struct renderonly *ro)
122{
123   return lima_screen_create(fcntl(ro->gpu_fd, F_DUPFD_CLOEXEC, 3), ro);
124}
125