2 * Copyright © 2008 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
30 * \brief Support for GL_ARB_sync and EGL_KHR_fence_sync.
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.
41 #include "main/imports.h"
43 #include "brw_context.h"
44 #include "intel_batchbuffer.h"
47 struct brw_context
*brw
;
50 BRW_FENCE_TYPE_BO_WAIT
,
53 /** The fence waits for completion of this batch. */
54 drm_intel_bo
*batch_bo
;
61 struct gl_sync_object gl
;
62 struct brw_fence fence
;
66 brw_fence_init(struct brw_context
*brw
, struct brw_fence
*fence
,
67 enum brw_fence_type type
)
71 mtx_init(&fence
->mutex
, mtx_plain
);
74 case BRW_FENCE_TYPE_BO_WAIT
:
75 fence
->batch_bo
= NULL
;
81 brw_fence_finish(struct brw_fence
*fence
)
83 switch (fence
->type
) {
84 case BRW_FENCE_TYPE_BO_WAIT
:
86 drm_intel_bo_unreference(fence
->batch_bo
);
90 mtx_destroy(&fence
->mutex
);
93 static bool MUST_CHECK
94 brw_fence_insert_locked(struct brw_context
*brw
, struct brw_fence
*fence
)
96 brw_emit_mi_flush(brw
);
98 switch (fence
->type
) {
99 case BRW_FENCE_TYPE_BO_WAIT
:
100 assert(!fence
->batch_bo
);
101 assert(!fence
->signalled
);
103 fence
->batch_bo
= brw
->batch
.bo
;
104 drm_intel_bo_reference(fence
->batch_bo
);
106 if (intel_batchbuffer_flush(brw
) < 0) {
107 drm_intel_bo_unreference(fence
->batch_bo
);
108 fence
->batch_bo
= NULL
;
118 brw_fence_has_completed_locked(struct brw_fence
*fence
)
120 if (fence
->signalled
)
123 switch (fence
->type
) {
124 case BRW_FENCE_TYPE_BO_WAIT
:
125 if (!fence
->batch_bo
) {
126 /* There may be no batch if intel_batchbuffer_flush() failed. */
130 if (drm_intel_bo_busy(fence
->batch_bo
))
133 drm_intel_bo_unreference(fence
->batch_bo
);
134 fence
->batch_bo
= NULL
;
135 fence
->signalled
= true;
144 brw_fence_has_completed(struct brw_fence
*fence
)
148 mtx_lock(&fence
->mutex
);
149 ret
= brw_fence_has_completed_locked(fence
);
150 mtx_unlock(&fence
->mutex
);
156 brw_fence_client_wait_locked(struct brw_context
*brw
, struct brw_fence
*fence
,
159 if (fence
->signalled
)
162 switch (fence
->type
) {
163 case BRW_FENCE_TYPE_BO_WAIT
:
164 if (!fence
->batch_bo
) {
165 /* There may be no batch if intel_batchbuffer_flush() failed. */
169 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and returns
170 * immediately for timeouts <= 0. The best we can do is to clamp the
171 * timeout to INT64_MAX. This limits the maximum timeout from 584 years to
172 * 292 years - likely not a big deal.
174 if (timeout
> INT64_MAX
)
177 if (drm_intel_gem_bo_wait(fence
->batch_bo
, timeout
) != 0)
180 fence
->signalled
= true;
181 drm_intel_bo_unreference(fence
->batch_bo
);
182 fence
->batch_bo
= NULL
;
187 assert(!"bad enum brw_fence_type");
192 * Return true if the function successfully signals or has already signalled.
193 * (This matches the behavior expected from __DRI2fence::client_wait_sync).
196 brw_fence_client_wait(struct brw_context
*brw
, struct brw_fence
*fence
,
201 mtx_lock(&fence
->mutex
);
202 ret
= brw_fence_client_wait_locked(brw
, fence
, timeout
);
203 mtx_unlock(&fence
->mutex
);
209 brw_fence_server_wait(struct brw_context
*brw
, struct brw_fence
*fence
)
211 switch (fence
->type
) {
212 case BRW_FENCE_TYPE_BO_WAIT
:
213 /* We have nothing to do for WaitSync. Our GL command stream is sequential,
214 * so given that the sync object has already flushed the batchbuffer, any
215 * batchbuffers coming after this waitsync will naturally not occur until
216 * the previous one is done.
222 static struct gl_sync_object
*
223 brw_gl_new_sync(struct gl_context
*ctx
, GLuint id
)
225 struct brw_gl_sync
*sync
;
227 sync
= calloc(1, sizeof(*sync
));
235 brw_gl_delete_sync(struct gl_context
*ctx
, struct gl_sync_object
*_sync
)
237 struct brw_gl_sync
*sync
= (struct brw_gl_sync
*) _sync
;
239 brw_fence_finish(&sync
->fence
);
244 brw_gl_fence_sync(struct gl_context
*ctx
, struct gl_sync_object
*_sync
,
245 GLenum condition
, GLbitfield flags
)
247 struct brw_context
*brw
= brw_context(ctx
);
248 struct brw_gl_sync
*sync
= (struct brw_gl_sync
*) _sync
;
250 brw_fence_init(brw
, &sync
->fence
, BRW_FENCE_TYPE_BO_WAIT
);
252 if (!brw_fence_insert_locked(brw
, &sync
->fence
)) {
253 /* FIXME: There exists no way to report a GL error here. If an error
254 * occurs, continue silently and hope for the best.
260 brw_gl_client_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*_sync
,
261 GLbitfield flags
, GLuint64 timeout
)
263 struct brw_context
*brw
= brw_context(ctx
);
264 struct brw_gl_sync
*sync
= (struct brw_gl_sync
*) _sync
;
266 if (brw_fence_client_wait(brw
, &sync
->fence
, timeout
))
267 sync
->gl
.StatusFlag
= 1;
271 brw_gl_server_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*_sync
,
272 GLbitfield flags
, GLuint64 timeout
)
274 struct brw_context
*brw
= brw_context(ctx
);
275 struct brw_gl_sync
*sync
= (struct brw_gl_sync
*) _sync
;
277 brw_fence_server_wait(brw
, &sync
->fence
);
281 brw_gl_check_sync(struct gl_context
*ctx
, struct gl_sync_object
*_sync
)
283 struct brw_gl_sync
*sync
= (struct brw_gl_sync
*) _sync
;
285 if (brw_fence_has_completed(&sync
->fence
))
286 sync
->gl
.StatusFlag
= 1;
290 brw_init_syncobj_functions(struct dd_function_table
*functions
)
292 functions
->NewSyncObject
= brw_gl_new_sync
;
293 functions
->DeleteSyncObject
= brw_gl_delete_sync
;
294 functions
->FenceSync
= brw_gl_fence_sync
;
295 functions
->CheckSync
= brw_gl_check_sync
;
296 functions
->ClientWaitSync
= brw_gl_client_wait_sync
;
297 functions
->ServerWaitSync
= brw_gl_server_wait_sync
;
301 brw_dri_create_fence(__DRIcontext
*ctx
)
303 struct brw_context
*brw
= ctx
->driverPrivate
;
304 struct brw_fence
*fence
;
306 fence
= calloc(1, sizeof(*fence
));
310 brw_fence_init(brw
, fence
, BRW_FENCE_TYPE_BO_WAIT
);
312 if (!brw_fence_insert_locked(brw
, fence
)) {
313 brw_fence_finish(fence
);
322 brw_dri_destroy_fence(__DRIscreen
*dri_screen
, void *_fence
)
324 struct brw_fence
*fence
= _fence
;
326 brw_fence_finish(fence
);
331 brw_dri_client_wait_sync(__DRIcontext
*ctx
, void *_fence
, unsigned flags
,
334 struct brw_fence
*fence
= _fence
;
336 return brw_fence_client_wait(fence
->brw
, fence
, timeout
);
340 brw_dri_server_wait_sync(__DRIcontext
*ctx
, void *_fence
, unsigned flags
)
342 struct brw_fence
*fence
= _fence
;
344 /* We might be called here with a NULL fence as a result of WaitSyncKHR
345 * on a EGL_KHR_reusable_sync fence. Nothing to do here in such case.
350 brw_fence_server_wait(fence
->brw
, fence
);
353 const __DRI2fenceExtension intelFenceExtension
= {
354 .base
= { __DRI2_FENCE
, 1 },
356 .create_fence
= brw_dri_create_fence
,
357 .destroy_fence
= brw_dri_destroy_fence
,
358 .client_wait_sync
= brw_dri_client_wait_sync
,
359 .server_wait_sync
= brw_dri_server_wait_sync
,
360 .get_fence_from_cl_event
= NULL
,