remove final imports.h and imports.c bits
[mesa.git] / src / mesa / drivers / dri / i965 / brw_sync.c
1 /*
2 * Copyright © 2008 Intel 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 /**
29 * \file
30 * \brief Support for GL_ARB_sync and EGL_KHR_fence_sync.
31 *
32 * GL_ARB_sync is implemented by flushing the current batchbuffer and keeping a
33 * reference on it. We can then check for completion or wait for completion
34 * using the normal buffer object mechanisms. This does mean that if an
35 * application is using many sync objects, it will emit small batchbuffers
36 * which may end up being a significant overhead. In other tests of removing
37 * gratuitous batchbuffer syncs in Mesa, it hasn't appeared to be a significant
38 * performance bottleneck, though.
39 */
40
41 #include <libsync.h> /* Requires Android or libdrm-2.4.72 */
42
43 #include "brw_context.h"
44 #include "intel_batchbuffer.h"
45
46 struct brw_fence {
47 struct brw_context *brw;
48
49 enum brw_fence_type {
50 /** The fence waits for completion of brw_fence::batch_bo. */
51 BRW_FENCE_TYPE_BO_WAIT,
52
53 /** The fence waits for brw_fence::sync_fd to signal. */
54 BRW_FENCE_TYPE_SYNC_FD,
55 } type;
56
57 union {
58 struct brw_bo *batch_bo;
59
60 /* This struct owns the fd. */
61 int sync_fd;
62 };
63
64 mtx_t mutex;
65 bool signalled;
66 };
67
68 struct brw_gl_sync {
69 struct gl_sync_object gl;
70 struct brw_fence fence;
71 };
72
73 static void
74 brw_fence_init(struct brw_context *brw, struct brw_fence *fence,
75 enum brw_fence_type type)
76 {
77 fence->brw = brw;
78 fence->type = type;
79 mtx_init(&fence->mutex, mtx_plain);
80
81 switch (type) {
82 case BRW_FENCE_TYPE_BO_WAIT:
83 fence->batch_bo = NULL;
84 break;
85 case BRW_FENCE_TYPE_SYNC_FD:
86 fence->sync_fd = -1;
87 break;
88 }
89 }
90
91 static void
92 brw_fence_finish(struct brw_fence *fence)
93 {
94 switch (fence->type) {
95 case BRW_FENCE_TYPE_BO_WAIT:
96 if (fence->batch_bo)
97 brw_bo_unreference(fence->batch_bo);
98 break;
99 case BRW_FENCE_TYPE_SYNC_FD:
100 if (fence->sync_fd != -1)
101 close(fence->sync_fd);
102 break;
103 }
104
105 mtx_destroy(&fence->mutex);
106 }
107
108 static bool MUST_CHECK
109 brw_fence_insert_locked(struct brw_context *brw, struct brw_fence *fence)
110 {
111 __DRIcontext *driContext = brw->driContext;
112 __DRIdrawable *driDrawable = driContext->driDrawablePriv;
113
114 /*
115 * From KHR_fence_sync:
116 *
117 * When the condition of the sync object is satisfied by the fence
118 * command, the sync is signaled by the associated client API context,
119 * causing any eglClientWaitSyncKHR commands (see below) blocking on
120 * <sync> to unblock. The only condition currently supported is
121 * EGL_SYNC_PRIOR_COMMANDS_COMPLETE_KHR, which is satisfied by
122 * completion of the fence command corresponding to the sync object,
123 * and all preceding commands in the associated client API context's
124 * command stream. The sync object will not be signaled until all
125 * effects from these commands on the client API's internal and
126 * framebuffer state are fully realized. No other state is affected by
127 * execution of the fence command.
128 *
129 * Note the emphasis there on ensuring that the framebuffer is fully
130 * realised before the fence is signaled. We cannot just flush the batch,
131 * but must also resolve the drawable first. The importance of this is,
132 * for example, in creating a fence for a frame to be passed to a
133 * remote compositor. Without us flushing the drawable explicitly, the
134 * resolve will be in a following batch (when the client finally calls
135 * SwapBuffers, or triggers a resolve via some other path) and so the
136 * compositor may read the incomplete framebuffer instead.
137 */
138 if (driDrawable)
139 intel_resolve_for_dri2_flush(brw, driDrawable);
140 brw_emit_mi_flush(brw);
141
142 switch (fence->type) {
143 case BRW_FENCE_TYPE_BO_WAIT:
144 assert(!fence->batch_bo);
145 assert(!fence->signalled);
146
147 fence->batch_bo = brw->batch.batch.bo;
148 brw_bo_reference(fence->batch_bo);
149
150 if (intel_batchbuffer_flush(brw) < 0) {
151 brw_bo_unreference(fence->batch_bo);
152 fence->batch_bo = NULL;
153 return false;
154 }
155 break;
156 case BRW_FENCE_TYPE_SYNC_FD:
157 assert(!fence->signalled);
158
159 if (fence->sync_fd == -1) {
160 /* Create an out-fence that signals after all pending commands
161 * complete.
162 */
163 if (intel_batchbuffer_flush_fence(brw, -1, &fence->sync_fd) < 0)
164 return false;
165 assert(fence->sync_fd != -1);
166 } else {
167 /* Wait on the in-fence before executing any subsequently submitted
168 * commands.
169 */
170 if (intel_batchbuffer_flush(brw) < 0)
171 return false;
172
173 /* Emit a dummy batch just for the fence. */
174 brw_emit_mi_flush(brw);
175 if (intel_batchbuffer_flush_fence(brw, fence->sync_fd, NULL) < 0)
176 return false;
177 }
178 break;
179 }
180
181 return true;
182 }
183
184 static bool MUST_CHECK
185 brw_fence_insert(struct brw_context *brw, struct brw_fence *fence)
186 {
187 bool ret;
188
189 mtx_lock(&fence->mutex);
190 ret = brw_fence_insert_locked(brw, fence);
191 mtx_unlock(&fence->mutex);
192
193 return ret;
194 }
195
196 static bool
197 brw_fence_has_completed_locked(struct brw_fence *fence)
198 {
199 if (fence->signalled)
200 return true;
201
202 switch (fence->type) {
203 case BRW_FENCE_TYPE_BO_WAIT:
204 if (!fence->batch_bo) {
205 /* There may be no batch if intel_batchbuffer_flush() failed. */
206 return false;
207 }
208
209 if (brw_bo_busy(fence->batch_bo))
210 return false;
211
212 brw_bo_unreference(fence->batch_bo);
213 fence->batch_bo = NULL;
214 fence->signalled = true;
215
216 return true;
217
218 case BRW_FENCE_TYPE_SYNC_FD:
219 assert(fence->sync_fd != -1);
220
221 if (sync_wait(fence->sync_fd, 0) == -1)
222 return false;
223
224 fence->signalled = true;
225
226 return true;
227 }
228
229 return false;
230 }
231
232 static bool
233 brw_fence_has_completed(struct brw_fence *fence)
234 {
235 bool ret;
236
237 mtx_lock(&fence->mutex);
238 ret = brw_fence_has_completed_locked(fence);
239 mtx_unlock(&fence->mutex);
240
241 return ret;
242 }
243
244 static bool
245 brw_fence_client_wait_locked(struct brw_context *brw, struct brw_fence *fence,
246 uint64_t timeout)
247 {
248 int32_t timeout_i32;
249
250 if (fence->signalled)
251 return true;
252
253 switch (fence->type) {
254 case BRW_FENCE_TYPE_BO_WAIT:
255 if (!fence->batch_bo) {
256 /* There may be no batch if intel_batchbuffer_flush() failed. */
257 return false;
258 }
259
260 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and returns
261 * immediately for timeouts <= 0. The best we can do is to clamp the
262 * timeout to INT64_MAX. This limits the maximum timeout from 584 years to
263 * 292 years - likely not a big deal.
264 */
265 if (timeout > INT64_MAX)
266 timeout = INT64_MAX;
267
268 if (brw_bo_wait(fence->batch_bo, timeout) != 0)
269 return false;
270
271 fence->signalled = true;
272 brw_bo_unreference(fence->batch_bo);
273 fence->batch_bo = NULL;
274
275 return true;
276 case BRW_FENCE_TYPE_SYNC_FD:
277 if (fence->sync_fd == -1)
278 return false;
279
280 if (timeout > INT32_MAX)
281 timeout_i32 = -1;
282 else
283 timeout_i32 = timeout;
284
285 if (sync_wait(fence->sync_fd, timeout_i32) == -1)
286 return false;
287
288 fence->signalled = true;
289 return true;
290 }
291
292 assert(!"bad enum brw_fence_type");
293 return false;
294 }
295
296 /**
297 * Return true if the function successfully signals or has already signalled.
298 * (This matches the behavior expected from __DRI2fence::client_wait_sync).
299 */
300 static bool
301 brw_fence_client_wait(struct brw_context *brw, struct brw_fence *fence,
302 uint64_t timeout)
303 {
304 bool ret;
305
306 mtx_lock(&fence->mutex);
307 ret = brw_fence_client_wait_locked(brw, fence, timeout);
308 mtx_unlock(&fence->mutex);
309
310 return ret;
311 }
312
313 static void
314 brw_fence_server_wait(struct brw_context *brw, struct brw_fence *fence)
315 {
316 switch (fence->type) {
317 case BRW_FENCE_TYPE_BO_WAIT:
318 /* We have nothing to do for WaitSync. Our GL command stream is sequential,
319 * so given that the sync object has already flushed the batchbuffer, any
320 * batchbuffers coming after this waitsync will naturally not occur until
321 * the previous one is done.
322 */
323 break;
324 case BRW_FENCE_TYPE_SYNC_FD:
325 assert(fence->sync_fd != -1);
326
327 /* The user wants explicit synchronization, so give them what they want. */
328 if (!brw_fence_insert(brw, fence)) {
329 /* FIXME: There exists no way yet to report an error here. If an error
330 * occurs, continue silently and hope for the best.
331 */
332 }
333 break;
334 }
335 }
336
337 static struct gl_sync_object *
338 brw_gl_new_sync(struct gl_context *ctx)
339 {
340 struct brw_gl_sync *sync;
341
342 sync = calloc(1, sizeof(*sync));
343 if (!sync)
344 return NULL;
345
346 return &sync->gl;
347 }
348
349 static void
350 brw_gl_delete_sync(struct gl_context *ctx, struct gl_sync_object *_sync)
351 {
352 struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
353
354 brw_fence_finish(&sync->fence);
355 free(sync->gl.Label);
356 free(sync);
357 }
358
359 static void
360 brw_gl_fence_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
361 GLenum condition, GLbitfield flags)
362 {
363 struct brw_context *brw = brw_context(ctx);
364 struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
365
366 /* brw_fence_insert_locked() assumes it must do a complete flush */
367 assert(condition == GL_SYNC_GPU_COMMANDS_COMPLETE);
368
369 brw_fence_init(brw, &sync->fence, BRW_FENCE_TYPE_BO_WAIT);
370
371 if (!brw_fence_insert_locked(brw, &sync->fence)) {
372 /* FIXME: There exists no way to report a GL error here. If an error
373 * occurs, continue silently and hope for the best.
374 */
375 }
376 }
377
378 static void
379 brw_gl_client_wait_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
380 GLbitfield flags, GLuint64 timeout)
381 {
382 struct brw_context *brw = brw_context(ctx);
383 struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
384
385 if (brw_fence_client_wait(brw, &sync->fence, timeout))
386 sync->gl.StatusFlag = 1;
387 }
388
389 static void
390 brw_gl_server_wait_sync(struct gl_context *ctx, struct gl_sync_object *_sync,
391 GLbitfield flags, GLuint64 timeout)
392 {
393 struct brw_context *brw = brw_context(ctx);
394 struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
395
396 brw_fence_server_wait(brw, &sync->fence);
397 }
398
399 static void
400 brw_gl_check_sync(struct gl_context *ctx, struct gl_sync_object *_sync)
401 {
402 struct brw_gl_sync *sync = (struct brw_gl_sync *) _sync;
403
404 if (brw_fence_has_completed(&sync->fence))
405 sync->gl.StatusFlag = 1;
406 }
407
408 void
409 brw_init_syncobj_functions(struct dd_function_table *functions)
410 {
411 functions->NewSyncObject = brw_gl_new_sync;
412 functions->DeleteSyncObject = brw_gl_delete_sync;
413 functions->FenceSync = brw_gl_fence_sync;
414 functions->CheckSync = brw_gl_check_sync;
415 functions->ClientWaitSync = brw_gl_client_wait_sync;
416 functions->ServerWaitSync = brw_gl_server_wait_sync;
417 }
418
419 static void *
420 brw_dri_create_fence(__DRIcontext *ctx)
421 {
422 struct brw_context *brw = ctx->driverPrivate;
423 struct brw_fence *fence;
424
425 fence = calloc(1, sizeof(*fence));
426 if (!fence)
427 return NULL;
428
429 brw_fence_init(brw, fence, BRW_FENCE_TYPE_BO_WAIT);
430
431 if (!brw_fence_insert_locked(brw, fence)) {
432 brw_fence_finish(fence);
433 free(fence);
434 return NULL;
435 }
436
437 return fence;
438 }
439
440 static void
441 brw_dri_destroy_fence(__DRIscreen *dri_screen, void *_fence)
442 {
443 struct brw_fence *fence = _fence;
444
445 brw_fence_finish(fence);
446 free(fence);
447 }
448
449 static GLboolean
450 brw_dri_client_wait_sync(__DRIcontext *ctx, void *_fence, unsigned flags,
451 uint64_t timeout)
452 {
453 struct brw_fence *fence = _fence;
454
455 return brw_fence_client_wait(fence->brw, fence, timeout);
456 }
457
458 static void
459 brw_dri_server_wait_sync(__DRIcontext *ctx, void *_fence, unsigned flags)
460 {
461 struct brw_fence *fence = _fence;
462
463 /* We might be called here with a NULL fence as a result of WaitSyncKHR
464 * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
465 */
466 if (!fence)
467 return;
468
469 brw_fence_server_wait(fence->brw, fence);
470 }
471
472 static unsigned
473 brw_dri_get_capabilities(__DRIscreen *dri_screen)
474 {
475 struct intel_screen *screen = dri_screen->driverPrivate;
476 unsigned caps = 0;
477
478 if (screen->has_exec_fence)
479 caps |= __DRI_FENCE_CAP_NATIVE_FD;
480
481 return caps;
482 }
483
484 static void *
485 brw_dri_create_fence_fd(__DRIcontext *dri_ctx, int fd)
486 {
487 struct brw_context *brw = dri_ctx->driverPrivate;
488 struct brw_fence *fence;
489
490 assert(brw->screen->has_exec_fence);
491
492 fence = calloc(1, sizeof(*fence));
493 if (!fence)
494 return NULL;
495
496 brw_fence_init(brw, fence, BRW_FENCE_TYPE_SYNC_FD);
497
498 if (fd == -1) {
499 /* Create an out-fence fd */
500 if (!brw_fence_insert_locked(brw, fence))
501 goto fail;
502 } else {
503 /* Import the sync fd as an in-fence. */
504 fence->sync_fd = dup(fd);
505 }
506
507 assert(fence->sync_fd != -1);
508
509 return fence;
510
511 fail:
512 brw_fence_finish(fence);
513 free(fence);
514 return NULL;
515 }
516
517 static int
518 brw_dri_get_fence_fd_locked(struct brw_fence *fence)
519 {
520 assert(fence->type == BRW_FENCE_TYPE_SYNC_FD);
521 return dup(fence->sync_fd);
522 }
523
524 static int
525 brw_dri_get_fence_fd(__DRIscreen *dri_screen, void *_fence)
526 {
527 struct brw_fence *fence = _fence;
528 int fd;
529
530 mtx_lock(&fence->mutex);
531 fd = brw_dri_get_fence_fd_locked(fence);
532 mtx_unlock(&fence->mutex);
533
534 return fd;
535 }
536
537 const __DRI2fenceExtension intelFenceExtension = {
538 .base = { __DRI2_FENCE, 2 },
539
540 .create_fence = brw_dri_create_fence,
541 .destroy_fence = brw_dri_destroy_fence,
542 .client_wait_sync = brw_dri_client_wait_sync,
543 .server_wait_sync = brw_dri_server_wait_sync,
544 .get_fence_from_cl_event = NULL,
545 .get_capabilities = brw_dri_get_capabilities,
546 .create_fence_fd = brw_dri_create_fence_fd,
547 .get_fence_fd = brw_dri_get_fence_fd,
548 };