xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/freedreno_fence.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #ifndef FREEDRENO_FENCE_H_
10 #define FREEDRENO_FENCE_H_
11 
12 #include "pipe/p_context.h"
13 #include "util/u_queue.h"
14 
15 #include "common/freedreno_common.h"
16 #include "drm/freedreno_drmif.h"
17 
18 BEGINC;
19 
20 struct pipe_fence_handle {
21    struct pipe_reference reference;
22 
23    /* When a pre-created unflushed fence has no actual rendering to flush, and
24     * the last_fence optimization is used, this will be a reference to the
25     * *actualy* fence which needs to be flushed before waiting.
26     */
27    struct pipe_fence_handle *last_fence;
28 
29    /* fence holds a reference to the batch until the batch is flushed, to
30     * accommodate PIPE_FLUSH_DEFERRED.  When the batch is actually flushed, it
31     * is cleared (before the batch reference is dropped).  If we need to wait
32     * on a fence, and the batch is not NULL, we need to flush it.
33     *
34     * Note that with u_threaded_context async flushes, if a fence is requested
35     * by the frontend, the fence is initially created without a reference
36     * to the batch, which is filled in later when fd_context_flush() is called
37     * from the driver thread.  In this case tc_token will be non-null, in
38     * which case threaded_context_flush() should be called in fd_fence_finish()
39     */
40    struct fd_batch *batch;
41 
42    struct tc_unflushed_batch_token *tc_token;
43    bool needs_signal;
44 
45    /* For threaded_context async flushes, we must wait on the fence, signaled
46     * when fence->batch is cleared, to know that the rendering has been actually
47     * flushed from the driver thread.
48     *
49     * The ready fence is created signaled for non-async-flush fences, and only
50     * transitions once from unsignalled->signalled for async-flush fences
51     */
52    struct util_queue_fence ready;
53 
54    /* Note that a fence can outlive the ctx, so we can only assume this is a
55     * valid ptr for unflushed fences.  However we hold a reference to the
56     * fence->pipe so that is safe to use after flushing.
57     */
58    struct fd_context *ctx;
59    struct fd_pipe *pipe;
60    struct fd_screen *screen;
61    struct fd_fence *fence;
62 
63    bool use_fence_fd;
64    bool flushed;
65    uint32_t syncobj;
66 };
67 
68 void fd_pipe_fence_repopulate(struct pipe_fence_handle *fence,
69                               struct pipe_fence_handle *last_fence);
70 void fd_pipe_fence_ref(struct pipe_fence_handle **ptr,
71                        struct pipe_fence_handle *pfence);
72 bool fd_pipe_fence_finish(struct pipe_screen *pscreen, struct pipe_context *ctx,
73                           struct pipe_fence_handle *pfence, uint64_t timeout);
74 void fd_create_pipe_fence_fd(struct pipe_context *pctx,
75                              struct pipe_fence_handle **pfence, int fd,
76                              enum pipe_fd_type type);
77 void fd_pipe_fence_server_sync(struct pipe_context *pctx,
78                                struct pipe_fence_handle *fence);
79 void fd_pipe_fence_server_signal(struct pipe_context *ctx,
80                                  struct pipe_fence_handle *fence);
81 int fd_pipe_fence_get_fd(struct pipe_screen *pscreen,
82                          struct pipe_fence_handle *pfence);
83 bool fd_pipe_fence_is_fd(struct pipe_fence_handle *fence);
84 
85 struct fd_batch;
86 struct pipe_fence_handle *fd_pipe_fence_create(struct fd_batch *batch);
87 
88 void fd_pipe_fence_set_batch(struct pipe_fence_handle *fence,
89                              struct fd_batch *batch);
90 void fd_pipe_fence_set_submit_fence(struct pipe_fence_handle *fence,
91                         struct fd_fence *submit_fence);
92 
93 struct tc_unflushed_batch_token;
94 struct pipe_fence_handle *
95 fd_pipe_fence_create_unflushed(struct pipe_context *pctx,
96                                struct tc_unflushed_batch_token *tc_token);
97 
98 ENDC;
99 
100 #endif /* FREEDRENO_FENCE_H_ */
101