2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
29 #include "util/os_file.h"
30 #include "util/u_inlines.h"
32 #include "freedreno_fence.h"
33 #include "freedreno_context.h"
34 #include "freedreno_util.h"
35 /* TODO: Use the interface drm/freedreno_drmif.h instead of calling directly */
38 struct pipe_fence_handle
{
39 struct pipe_reference reference
;
40 /* fence holds a weak reference to the batch until the batch is flushed,
41 * at which point fd_fence_populate() is called and timestamp and possibly
42 * fence_fd become valid and the week reference is dropped.
44 struct fd_batch
*batch
;
46 struct fd_screen
*screen
;
52 static void fence_flush(struct pipe_fence_handle
*fence
)
55 fd_batch_flush(fence
->batch
);
56 debug_assert(!fence
->batch
);
59 void fd_fence_populate(struct pipe_fence_handle
*fence
,
60 uint32_t timestamp
, int fence_fd
)
64 fence
->timestamp
= timestamp
;
65 fence
->fence_fd
= fence_fd
;
69 static void fd_fence_destroy(struct pipe_fence_handle
*fence
)
71 if (fence
->fence_fd
!= -1)
72 close(fence
->fence_fd
);
74 drmSyncobjDestroy(fd_device_fd(fence
->screen
->dev
), fence
->syncobj
);
75 fd_pipe_del(fence
->pipe
);
79 void fd_fence_ref(struct pipe_fence_handle
**ptr
,
80 struct pipe_fence_handle
*pfence
)
82 if (pipe_reference(&(*ptr
)->reference
, &pfence
->reference
))
83 fd_fence_destroy(*ptr
);
88 bool fd_fence_finish(struct pipe_screen
*pscreen
,
89 struct pipe_context
*ctx
,
90 struct pipe_fence_handle
*fence
,
95 if (fence
->fence_fd
!= -1) {
96 int ret
= sync_wait(fence
->fence_fd
, timeout
/ 1000000);
100 if (fd_pipe_wait_timeout(fence
->pipe
, fence
->timestamp
, timeout
))
106 static struct pipe_fence_handle
* fence_create(struct fd_context
*ctx
,
107 struct fd_batch
*batch
, uint32_t timestamp
, int fence_fd
, int syncobj
)
109 struct pipe_fence_handle
*fence
;
111 fence
= CALLOC_STRUCT(pipe_fence_handle
);
115 pipe_reference_init(&fence
->reference
, 1);
117 fence
->batch
= batch
;
118 fence
->pipe
= fd_pipe_ref(ctx
->pipe
);
119 fence
->screen
= ctx
->screen
;
120 fence
->timestamp
= timestamp
;
121 fence
->fence_fd
= fence_fd
;
122 fence
->syncobj
= syncobj
;
127 void fd_create_fence_fd(struct pipe_context
*pctx
,
128 struct pipe_fence_handle
**pfence
, int fd
,
129 enum pipe_fd_type type
)
131 struct fd_context
*ctx
= fd_context(pctx
);
134 case PIPE_FD_TYPE_NATIVE_SYNC
:
135 *pfence
= fence_create(fd_context(pctx
), NULL
, 0, os_dupfd_cloexec(fd
), 0);
137 case PIPE_FD_TYPE_SYNCOBJ
: {
141 assert(ctx
->screen
->has_syncobj
);
142 ret
= drmSyncobjFDToHandle(fd_device_fd(ctx
->screen
->dev
), fd
, &syncobj
);
146 *pfence
= fence_create(fd_context(pctx
), NULL
, 0, -1, syncobj
);
150 unreachable("Unhandled fence type");
154 void fd_fence_server_sync(struct pipe_context
*pctx
,
155 struct pipe_fence_handle
*fence
)
157 struct fd_context
*ctx
= fd_context(pctx
);
161 /* if not an external fence, then nothing more to do without preemption: */
162 if (fence
->fence_fd
== -1)
165 if (sync_accumulate("freedreno", &ctx
->in_fence_fd
, fence
->fence_fd
)) {
170 void fd_fence_server_signal(struct pipe_context
*pctx
,
171 struct pipe_fence_handle
*fence
)
173 struct fd_context
*ctx
= fd_context(pctx
);
175 if (fence
->syncobj
) {
176 drmSyncobjSignal(fd_device_fd(ctx
->screen
->dev
), &fence
->syncobj
, 1);
180 int fd_fence_get_fd(struct pipe_screen
*pscreen
,
181 struct pipe_fence_handle
*fence
)
184 return os_dupfd_cloexec(fence
->fence_fd
);
187 bool fd_fence_is_fd(struct pipe_fence_handle
*fence
)
189 return fence
->fence_fd
!= -1;
192 struct pipe_fence_handle
* fd_fence_create(struct fd_batch
*batch
)
194 return fence_create(batch
->ctx
, batch
, 0, -1, 0);