xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_fence.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © Microsoft Corporation
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 #ifndef D3D12_FENCE_H
25 #define D3D12_FENCE_H
26 
27 #include "util/u_inlines.h"
28 
29 #include "d3d12_common.h"
30 
31 
32 constexpr uint64_t NsPerMs = 1000000;
33 constexpr uint64_t MaxTimeoutInNs = (uint64_t)UINT_MAX * NsPerMs;
34 
35 #ifdef _WIN32
36 inline void
d3d12_fence_close_event(HANDLE event,int fd)37 d3d12_fence_close_event(HANDLE event, int fd)
38 {
39    if (event)
40       CloseHandle(event);
41 }
42 
43 inline HANDLE
d3d12_fence_create_event(int * fd)44 d3d12_fence_create_event(int *fd)
45 {
46    *fd = -1;
47    return CreateEvent(NULL, false, false, NULL);
48 }
49 
50 inline bool
d3d12_fence_wait_event(HANDLE event,int event_fd,uint64_t timeout_ns)51 d3d12_fence_wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)
52 {
53    DWORD timeout_ms = (timeout_ns == OS_TIMEOUT_INFINITE || timeout_ns > MaxTimeoutInNs) ? INFINITE : timeout_ns / NsPerMs;
54    return WaitForSingleObject(event, timeout_ms) == WAIT_OBJECT_0;
55 }
56 #else
57 #include <sys/eventfd.h>
58 #include <poll.h>
59 #include <util/libsync.h>
60 
61 inline void
d3d12_fence_close_event(HANDLE event,int fd)62 d3d12_fence_close_event(HANDLE event, int fd)
63 {
64    if (fd != -1)
65       close(fd);
66 }
67 
68 inline HANDLE
d3d12_fence_create_event(int * fd)69 d3d12_fence_create_event(int *fd)
70 {
71    *fd = eventfd(0, 0);
72    return (HANDLE)(size_t)*fd;
73 }
74 
75 inline bool
d3d12_fence_wait_event(HANDLE event,int event_fd,uint64_t timeout_ns)76 d3d12_fence_wait_event(HANDLE event, int event_fd, uint64_t timeout_ns)
77 {
78    int timeout_ms = (timeout_ns == OS_TIMEOUT_INFINITE || timeout_ns > MaxTimeoutInNs) ? -1 : timeout_ns / NsPerMs;
79    return sync_wait(event_fd, timeout_ms) == 0;
80 }
81 #endif
82 
83 struct pipe_screen;
84 struct d3d12_screen;
85 
86 struct d3d12_fence {
87    struct pipe_reference reference;
88    ID3D12Fence *cmdqueue_fence;
89    HANDLE event;
90    int event_fd;
91    uint64_t value;
92    bool signaled;
93 };
94 
95 static inline struct d3d12_fence *
d3d12_fence(struct pipe_fence_handle * pfence)96 d3d12_fence(struct pipe_fence_handle *pfence)
97 {
98    return (struct d3d12_fence *)pfence;
99 }
100 
101 struct d3d12_fence *
102 d3d12_create_fence(struct d3d12_screen *screen);
103 
104 struct d3d12_fence *
105 d3d12_open_fence(struct d3d12_screen *screen, HANDLE handle, const void *name);
106 
107 void
108 d3d12_fence_reference(struct d3d12_fence **ptr, struct d3d12_fence *fence);
109 
110 bool
111 d3d12_fence_finish(struct d3d12_fence *fence, uint64_t timeout_ns);
112 
113 void
114 d3d12_screen_fence_init(struct pipe_screen *pscreen);
115 
116 #endif
117