1/*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#ifndef FREEDRENO_FENCE_H_
28#define FREEDRENO_FENCE_H_
29
30#include "pipe/p_context.h"
31#include "util/u_queue.h"
32
33#include "drm/freedreno_ringbuffer.h"
34
35struct pipe_fence_handle {
36   struct pipe_reference reference;
37
38   /* When a pre-created unflushed fence has no actual rendering to flush, and
39    * the last_fence optimization is used, this will be a reference to the
40    * *actualy* fence which needs to be flushed before waiting.
41    */
42   struct pipe_fence_handle *last_fence;
43
44   /* fence holds a weak reference to the batch until the batch is flushed, to
45    * accommodate PIPE_FLUSH_DEFERRED.  When the batch is actually flushed, it
46    * is cleared (before the batch reference is dropped).  If we need to wait
47    * on a fence, and the batch is not NULL, we need to flush it.
48    *
49    * Note that with u_threaded_context async flushes, if a fence is requested
50    * by the frontend, the fence is initially created without a weak reference
51    * to the batch, which is filled in later when fd_context_flush() is called
52    * from the driver thread.  In this case tc_token will be non-null, in
53    * which case threaded_context_flush() should be called in fd_fence_finish()
54    */
55   struct fd_batch *batch;
56
57   struct tc_unflushed_batch_token *tc_token;
58   bool needs_signal;
59
60   /* For threaded_context async flushes, we must wait on the fence, signaled
61    * when fence->batch is cleared, to know that the rendering has been actually
62    * flushed from the driver thread.
63    *
64    * The ready fence is created signaled for non-async-flush fences, and only
65    * transitions once from unsignalled->signalled for async-flush fences
66    */
67   struct util_queue_fence ready;
68
69   /* Note that a fence can outlive the ctx, so we can only assume this is a
70    * valid ptr for unflushed fences.  However we hold a reference to the
71    * fence->pipe so that is safe to use after flushing.
72    */
73   struct fd_context *ctx;
74   struct fd_pipe *pipe;
75   struct fd_screen *screen;
76   struct fd_submit_fence submit_fence;
77   uint32_t syncobj;
78};
79
80void fd_fence_repopulate(struct pipe_fence_handle *fence,
81                         struct pipe_fence_handle *last_fence);
82void fd_fence_ref(struct pipe_fence_handle **ptr,
83                  struct pipe_fence_handle *pfence);
84bool fd_fence_finish(struct pipe_screen *pscreen, struct pipe_context *ctx,
85                     struct pipe_fence_handle *pfence, uint64_t timeout);
86void fd_create_fence_fd(struct pipe_context *pctx,
87                        struct pipe_fence_handle **pfence, int fd,
88                        enum pipe_fd_type type);
89void fd_fence_server_sync(struct pipe_context *pctx,
90                          struct pipe_fence_handle *fence);
91void fd_fence_server_signal(struct pipe_context *ctx,
92                            struct pipe_fence_handle *fence);
93int fd_fence_get_fd(struct pipe_screen *pscreen,
94                    struct pipe_fence_handle *pfence);
95bool fd_fence_is_fd(struct pipe_fence_handle *fence);
96
97struct fd_batch;
98struct pipe_fence_handle *fd_fence_create(struct fd_batch *batch);
99
100void fd_fence_set_batch(struct pipe_fence_handle *fence,
101                        struct fd_batch *batch);
102
103struct tc_unflushed_batch_token;
104struct pipe_fence_handle *
105fd_fence_create_unflushed(struct pipe_context *pctx,
106                          struct tc_unflushed_batch_token *tc_token);
107
108#endif /* FREEDRENO_FENCE_H_ */
109