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"
45 #include "intel_reg.h"
48 /** The fence waits for completion of this batch. */
49 drm_intel_bo
*batch_bo
;
54 struct intel_gl_sync_object
{
55 struct gl_sync_object Base
;
56 struct brw_fence fence
;
60 brw_fence_finish(struct brw_fence
*fence
)
63 drm_intel_bo_unreference(fence
->batch_bo
);
67 brw_fence_insert(struct brw_context
*brw
, struct brw_fence
*fence
)
69 assert(!fence
->batch_bo
);
70 assert(!fence
->signalled
);
72 brw_emit_mi_flush(brw
);
73 fence
->batch_bo
= brw
->batch
.bo
;
74 drm_intel_bo_reference(fence
->batch_bo
);
75 intel_batchbuffer_flush(brw
);
79 brw_fence_has_completed(struct brw_fence
*fence
)
84 if (fence
->batch_bo
&& !drm_intel_bo_busy(fence
->batch_bo
)) {
85 drm_intel_bo_unreference(fence
->batch_bo
);
86 fence
->batch_bo
= NULL
;
87 fence
->signalled
= true;
95 * Return true if the function successfully signals or has already signalled.
96 * (This matches the behavior expected from __DRI2fence::client_wait_sync).
99 brw_fence_client_wait(struct brw_context
*brw
, struct brw_fence
*fence
,
102 if (fence
->signalled
)
105 assert(fence
->batch_bo
);
107 /* DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and returns
108 * immediately for timeouts <= 0. The best we can do is to clamp the
109 * timeout to INT64_MAX. This limits the maximum timeout from 584 years to
110 * 292 years - likely not a big deal.
112 if (timeout
> INT64_MAX
)
115 if (drm_intel_gem_bo_wait(fence
->batch_bo
, timeout
) != 0)
118 fence
->signalled
= true;
119 drm_intel_bo_unreference(fence
->batch_bo
);
120 fence
->batch_bo
= NULL
;
126 brw_fence_server_wait(struct brw_context
*brw
, struct brw_fence
*fence
)
128 /* We have nothing to do for WaitSync. Our GL command stream is sequential,
129 * so given that the sync object has already flushed the batchbuffer, any
130 * batchbuffers coming after this waitsync will naturally not occur until
131 * the previous one is done.
135 static struct gl_sync_object
*
136 intel_gl_new_sync_object(struct gl_context
*ctx
, GLuint id
)
138 struct intel_gl_sync_object
*sync
;
140 sync
= calloc(1, sizeof(*sync
));
148 intel_gl_delete_sync_object(struct gl_context
*ctx
, struct gl_sync_object
*s
)
150 struct intel_gl_sync_object
*sync
= (struct intel_gl_sync_object
*)s
;
152 brw_fence_finish(&sync
->fence
);
157 intel_gl_fence_sync(struct gl_context
*ctx
, struct gl_sync_object
*s
,
158 GLenum condition
, GLbitfield flags
)
160 struct brw_context
*brw
= brw_context(ctx
);
161 struct intel_gl_sync_object
*sync
= (struct intel_gl_sync_object
*)s
;
163 brw_fence_insert(brw
, &sync
->fence
);
167 intel_gl_client_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*s
,
168 GLbitfield flags
, GLuint64 timeout
)
170 struct brw_context
*brw
= brw_context(ctx
);
171 struct intel_gl_sync_object
*sync
= (struct intel_gl_sync_object
*)s
;
173 if (brw_fence_client_wait(brw
, &sync
->fence
, timeout
))
178 intel_gl_server_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*s
,
179 GLbitfield flags
, GLuint64 timeout
)
181 struct brw_context
*brw
= brw_context(ctx
);
182 struct intel_gl_sync_object
*sync
= (struct intel_gl_sync_object
*)s
;
184 brw_fence_server_wait(brw
, &sync
->fence
);
188 intel_gl_check_sync(struct gl_context
*ctx
, struct gl_sync_object
*s
)
190 struct intel_gl_sync_object
*sync
= (struct intel_gl_sync_object
*)s
;
192 if (brw_fence_has_completed(&sync
->fence
))
197 intel_init_syncobj_functions(struct dd_function_table
*functions
)
199 functions
->NewSyncObject
= intel_gl_new_sync_object
;
200 functions
->DeleteSyncObject
= intel_gl_delete_sync_object
;
201 functions
->FenceSync
= intel_gl_fence_sync
;
202 functions
->CheckSync
= intel_gl_check_sync
;
203 functions
->ClientWaitSync
= intel_gl_client_wait_sync
;
204 functions
->ServerWaitSync
= intel_gl_server_wait_sync
;
208 intel_dri_create_fence(__DRIcontext
*ctx
)
210 struct brw_context
*brw
= ctx
->driverPrivate
;
211 struct brw_fence
*fence
;
213 fence
= calloc(1, sizeof(*fence
));
217 brw_fence_insert(brw
, fence
);
223 intel_dri_destroy_fence(__DRIscreen
*screen
, void *driver_fence
)
225 struct brw_fence
*fence
= driver_fence
;
227 brw_fence_finish(fence
);
232 intel_dri_client_wait_sync(__DRIcontext
*ctx
, void *driver_fence
, unsigned flags
,
235 struct brw_context
*brw
= ctx
->driverPrivate
;
236 struct brw_fence
*fence
= driver_fence
;
238 return brw_fence_client_wait(brw
, fence
, timeout
);
242 intel_dri_server_wait_sync(__DRIcontext
*ctx
, void *driver_fence
, unsigned flags
)
244 struct brw_context
*brw
= ctx
->driverPrivate
;
245 struct brw_fence
*fence
= driver_fence
;
247 brw_fence_server_wait(brw
, fence
);
250 const __DRI2fenceExtension intelFenceExtension
= {
251 .base
= { __DRI2_FENCE
, 1 },
253 .create_fence
= intel_dri_create_fence
,
254 .destroy_fence
= intel_dri_destroy_fence
,
255 .client_wait_sync
= intel_dri_client_wait_sync
,
256 .server_wait_sync
= intel_dri_server_wait_sync
,
257 .get_fence_from_cl_event
= NULL
,