3 #include "pipe/p_context.h"
4 #include "pipe/p_state.h"
5 #include "util/u_format.h"
6 #include "util/u_memory.h"
7 #include "util/u_inlines.h"
8 #include "util/u_hash_table.h"
9 #include "os/os_thread.h"
11 #include "nouveau_drm_public.h"
13 #include "nouveau/nouveau_winsys.h"
14 #include "nouveau/nouveau_screen.h"
16 static struct util_hash_table
*fd_tab
= NULL
;
18 pipe_static_mutex(nouveau_screen_mutex
);
20 bool nouveau_drm_screen_unref(struct nouveau_screen
*screen
)
23 if (screen
->refcount
== -1)
26 pipe_mutex_lock(nouveau_screen_mutex
);
27 ret
= --screen
->refcount
;
30 util_hash_table_remove(fd_tab
, intptr_to_pointer(screen
->device
->fd
));
31 pipe_mutex_unlock(nouveau_screen_mutex
);
35 static unsigned hash_fd(void *key
)
37 int fd
= pointer_to_intptr(key
);
41 return stat
.st_dev
^ stat
.st_ino
^ stat
.st_rdev
;
44 static int compare_fd(void *key1
, void *key2
)
46 int fd1
= pointer_to_intptr(key1
);
47 int fd2
= pointer_to_intptr(key2
);
48 struct stat stat1
, stat2
;
52 return stat1
.st_dev
!= stat2
.st_dev
||
53 stat1
.st_ino
!= stat2
.st_ino
||
54 stat1
.st_rdev
!= stat2
.st_rdev
;
57 PUBLIC
struct pipe_screen
*
58 nouveau_drm_screen_create(int fd
)
60 struct nouveau_device
*dev
= NULL
;
61 struct pipe_screen
*(*init
)(struct nouveau_device
*);
62 struct nouveau_screen
*screen
;
65 pipe_mutex_lock(nouveau_screen_mutex
);
67 fd_tab
= util_hash_table_create(hash_fd
, compare_fd
);
72 screen
= util_hash_table_get(fd_tab
, intptr_to_pointer(fd
));
75 pipe_mutex_unlock(nouveau_screen_mutex
);
79 /* Since the screen re-use is based on the device node and not the fd,
80 * create a copy of the fd to be owned by the device. Otherwise a
81 * scenario could occur where two screens are created, and the first
82 * one is shut down, along with the fd being closed. The second
83 * (identical) screen would now have a reference to the closed fd. We
84 * avoid this by duplicating the original fd. Note that
85 * nouveau_device_wrap does not close the fd in case of a device
89 ret
= nouveau_device_wrap(dupfd
, 1, &dev
);
93 switch (dev
->chipset
& ~0xf) {
97 init
= nv30_screen_create
;
103 init
= nv50_screen_create
;
111 init
= nvc0_screen_create
;
114 debug_printf("%s: unknown chipset nv%02x\n", __func__
,
119 screen
= (struct nouveau_screen
*)init(dev
);
123 /* Use dupfd in hash table, to avoid errors if the original fd gets
124 * closed by its owner. The hash key needs to live at least as long as
127 util_hash_table_set(fd_tab
, intptr_to_pointer(dupfd
), screen
);
128 screen
->refcount
= 1;
129 pipe_mutex_unlock(nouveau_screen_mutex
);
130 return &screen
->base
;
134 nouveau_device_del(&dev
);
137 pipe_mutex_unlock(nouveau_screen_mutex
);