2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Chia-I Wu <olv@lunarg.com>
29 #include "util/u_memory.h"
30 #include "util/u_atomic.h"
31 #include "os/os_thread.h"
33 #include "eglcurrent.h"
36 #include "egl_g3d_sync.h"
38 #ifdef EGL_KHR_reusable_sync
41 * Wait for the conditional variable.
44 egl_g3d_wait_sync_condvar(struct egl_g3d_sync
*gsync
, EGLTimeKHR timeout
)
46 _EGLDisplay
*dpy
= gsync
->base
.Resource
.Display
;
48 pipe_mutex_lock(gsync
->mutex
);
50 /* unlock display lock just before waiting */
51 _eglUnlockMutex(&dpy
->Mutex
);
53 /* No timed wait. Always treat timeout as EGL_FOREVER_KHR */
54 pipe_condvar_wait(gsync
->condvar
, gsync
->mutex
);
56 _eglLockMutex(&dpy
->Mutex
);
58 pipe_mutex_unlock(gsync
->mutex
);
60 return EGL_CONDITION_SATISFIED_KHR
;
64 * Signal the conditional variable.
67 egl_g3d_signal_sync_condvar(struct egl_g3d_sync
*gsync
)
69 pipe_mutex_lock(gsync
->mutex
);
70 pipe_condvar_broadcast(gsync
->condvar
);
71 pipe_mutex_unlock(gsync
->mutex
);
75 * Insert a fence command to the command stream of the current context.
78 egl_g3d_insert_fence_sync(struct egl_g3d_sync
*gsync
)
80 _EGLContext
*ctx
= _eglGetCurrentContext();
81 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
83 /* already checked in egl_g3d_create_sync */
86 /* insert the fence command */
87 gctx
->stctxi
->flush(gctx
->stctxi
, 0x0, &gsync
->fence
);
89 gsync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
95 * Wait for the fence sync to be signaled.
98 egl_g3d_wait_fence_sync(struct egl_g3d_sync
*gsync
, EGLTimeKHR timeout
)
103 _EGLDisplay
*dpy
= gsync
->base
.Resource
.Display
;
104 struct egl_g3d_display
*gdpy
= egl_g3d_display(dpy
);
105 struct pipe_screen
*screen
= gdpy
->native
->screen
;
106 struct pipe_fence_handle
*fence
= gsync
->fence
;
110 _eglUnlockMutex(&dpy
->Mutex
);
111 /* no timed finish? */
112 screen
->fence_finish(screen
, fence
, 0x0, PIPE_TIMEOUT_INFINITE
);
113 ret
= EGL_CONDITION_SATISFIED_KHR
;
114 _eglLockMutex(&dpy
->Mutex
);
116 gsync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
118 screen
->fence_reference(screen
, &fence
, NULL
);
119 egl_g3d_signal_sync_condvar(gsync
);
122 ret
= egl_g3d_wait_sync_condvar(gsync
, timeout
);
129 egl_g3d_ref_sync(struct egl_g3d_sync
*gsync
)
131 _eglGetSync(&gsync
->base
);
135 egl_g3d_unref_sync(struct egl_g3d_sync
*gsync
)
137 if (_eglPutSync(&gsync
->base
)) {
138 pipe_condvar_destroy(gsync
->condvar
);
139 pipe_mutex_destroy(gsync
->mutex
);
142 struct egl_g3d_display
*gdpy
=
143 egl_g3d_display(gsync
->base
.Resource
.Display
);
144 struct pipe_screen
*screen
= gdpy
->native
->screen
;
146 screen
->fence_reference(screen
, &gsync
->fence
, NULL
);
154 egl_g3d_create_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
,
155 EGLenum type
, const EGLint
*attrib_list
)
157 _EGLContext
*ctx
= _eglGetCurrentContext();
158 struct egl_g3d_sync
*gsync
;
161 if (!ctx
|| ctx
->Resource
.Display
!= dpy
) {
162 _eglError(EGL_BAD_MATCH
, "eglCreateSyncKHR");
166 gsync
= CALLOC_STRUCT(egl_g3d_sync
);
168 _eglError(EGL_BAD_ALLOC
, "eglCreateSyncKHR");
172 if (!_eglInitSync(&gsync
->base
, dpy
, type
, attrib_list
)) {
178 case EGL_SYNC_REUSABLE_KHR
:
181 case EGL_SYNC_FENCE_KHR
:
182 err
= egl_g3d_insert_fence_sync(gsync
);
185 err
= EGL_BAD_ATTRIBUTE
;
189 if (err
!= EGL_SUCCESS
) {
190 _eglError(err
, "eglCreateSyncKHR");
195 pipe_mutex_init(gsync
->mutex
);
196 pipe_condvar_init(gsync
->condvar
);
202 egl_g3d_destroy_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
)
204 struct egl_g3d_sync
*gsync
= egl_g3d_sync(sync
);
206 switch (gsync
->base
.Type
) {
207 case EGL_SYNC_REUSABLE_KHR
:
208 /* signal the waiters */
209 if (gsync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
210 gsync
->base
.SyncStatus
= EGL_SIGNALED_KHR
;
211 egl_g3d_signal_sync_condvar(gsync
);
218 egl_g3d_unref_sync(gsync
);
224 egl_g3d_client_wait_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
225 EGLint flags
, EGLTimeKHR timeout
)
227 struct egl_g3d_sync
*gsync
= egl_g3d_sync(sync
);
228 EGLint ret
= EGL_CONDITION_SATISFIED_KHR
;
230 if (gsync
->base
.SyncStatus
!= EGL_SIGNALED_KHR
) {
231 /* flush if there is a current context */
232 if (flags
& EGL_SYNC_FLUSH_COMMANDS_BIT_KHR
) {
233 _EGLContext
*ctx
= _eglGetCurrentContext();
234 struct egl_g3d_context
*gctx
= egl_g3d_context(ctx
);
237 gctx
->stctxi
->flush(gctx
->stctxi
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
241 /* reference the sync object in case it is destroyed while waiting */
242 egl_g3d_ref_sync(gsync
);
244 switch (gsync
->base
.Type
) {
245 case EGL_SYNC_REUSABLE_KHR
:
246 ret
= egl_g3d_wait_sync_condvar(gsync
, timeout
);
248 case EGL_SYNC_FENCE_KHR
:
249 ret
= egl_g3d_wait_fence_sync(gsync
, timeout
);
254 egl_g3d_unref_sync(gsync
);
257 ret
= EGL_TIMEOUT_EXPIRED_KHR
;
265 egl_g3d_signal_sync(_EGLDriver
*drv
, _EGLDisplay
*dpy
, _EGLSync
*sync
,
268 struct egl_g3d_sync
*gsync
= egl_g3d_sync(sync
);
270 /* only for reusable sync */
271 if (sync
->Type
!= EGL_SYNC_REUSABLE_KHR
)
272 return _eglError(EGL_BAD_MATCH
, "eglSignalSyncKHR");
274 if (gsync
->base
.SyncStatus
!= mode
) {
275 gsync
->base
.SyncStatus
= mode
;
276 if (mode
== EGL_SIGNALED_KHR
)
277 egl_g3d_signal_sync_condvar(gsync
);
283 #endif /* EGL_KHR_reusable_sync */