aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/state_trackers/egl/common/egl_g3d_sync.c
blob: dd07af140a7af28efcd2deac8455b74c30f5c719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Mesa 3-D graphics library
 * Version:  7.9
 *
 * Copyright (C) 2010 LunarG Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "util/u_memory.h"
#include "util/u_atomic.h"
#include "os/os_thread.h"
#include "eglsync.h"
#include "eglcurrent.h"

#include "egl_g3d.h"
#include "egl_g3d_sync.h"

#ifdef EGL_KHR_reusable_sync

/**
 * Wait for the conditional variable.
 */
static EGLint
egl_g3d_wait_sync_condvar(struct egl_g3d_sync *gsync, EGLTimeKHR timeout)
{
   _EGLDisplay *dpy = gsync->base.Resource.Display;

   pipe_mutex_lock(gsync->mutex);

   /* unlock display lock just before waiting */
   _eglUnlockMutex(&dpy->Mutex);

   /* No timed wait.  Always treat timeout as EGL_FOREVER_KHR */
   pipe_condvar_wait(gsync->condvar, gsync->mutex);

   _eglLockMutex(&dpy->Mutex);

   pipe_mutex_unlock(gsync->mutex);

   return EGL_CONDITION_SATISFIED_KHR;
}

/**
 * Signal the conditional variable.
 */
static void
egl_g3d_signal_sync_condvar(struct egl_g3d_sync *gsync)
{
   pipe_mutex_lock(gsync->mutex);
   pipe_condvar_broadcast(gsync->condvar);
   pipe_mutex_unlock(gsync->mutex);
}

/**
 * Insert a fence command to the command stream of the current context.
 */
static EGLint
egl_g3d_insert_fence_sync(struct egl_g3d_sync *gsync)
{
   _EGLContext *ctx = _eglGetCurrentContext();
   struct egl_g3d_context *gctx = egl_g3d_context(ctx);

   /* already checked in egl_g3d_create_sync */
   assert(gctx);

   /* insert the fence command */
   gctx->stctxi->flush(gctx->stctxi, 0x0, &gsync->fence);
   if (!gsync->fence)
      gsync->base.SyncStatus = EGL_SIGNALED_KHR;

   return EGL_SUCCESS;
}

/**
 * Wait for the fence sync to be signaled.
 */
static EGLint
egl_g3d_wait_fence_sync(struct egl_g3d_sync *gsync, EGLTimeKHR timeout)
{
   EGLint ret;

   if (gsync->fence) {
      _EGLDisplay *dpy = gsync->base.Resource.Display;
      struct egl_g3d_display *gdpy = egl_g3d_display(dpy);
      struct pipe_screen *screen = gdpy->native->screen;
      struct pipe_fence_handle *fence = gsync->fence;

      gsync->fence = NULL;

      _eglUnlockMutex(&dpy->Mutex);
      /* no timed finish? */
      screen->fence_finish(screen, fence, PIPE_TIMEOUT_INFINITE);
      ret = EGL_CONDITION_SATISFIED_KHR;
      _eglLockMutex(&dpy->Mutex);

      gsync->base.SyncStatus = EGL_SIGNALED_KHR;

      screen->fence_reference(screen, &fence, NULL);
      egl_g3d_signal_sync_condvar(gsync);
   }
   else {
      ret = egl_g3d_wait_sync_condvar(gsync, timeout);
   }

   return ret;
}

static INLINE void
egl_g3d_ref_sync(struct egl_g3d_sync *gsync)
{
   _eglGetSync(&gsync->base);
}

static INLINE void
egl_g3d_unref_sync(struct egl_g3d_sync *gsync)
{
   if (_eglPutSync(&gsync->base)) {
      pipe_condvar_destroy(gsync->condvar);
      pipe_mutex_destroy(gsync->mutex);

      if (gsync->fence) {
         struct egl_g3d_display *gdpy =
            egl_g3d_display(gsync->base.Resource.Display);
         struct pipe_screen *screen = gdpy->native->screen;

         screen->fence_reference(screen, &gsync->fence, NULL);
      }

      FREE(gsync);
   }
}

_EGLSync *
egl_g3d_create_sync(_EGLDriver *drv, _EGLDisplay *dpy,
                    EGLenum type, const EGLint *attrib_list)
{
   _EGLContext *ctx = _eglGetCurrentContext();
   struct egl_g3d_sync *gsync;
   EGLint err;

   if (!ctx || ctx->Resource.Display != dpy) {
      _eglError(EGL_BAD_MATCH, "eglCreateSyncKHR");
      return NULL;
   }

   gsync = CALLOC_STRUCT(egl_g3d_sync);
   if (!gsync) {
      _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
      return NULL;
   }

   if (!_eglInitSync(&gsync->base, dpy, type, attrib_list)) {
      FREE(gsync);
      return NULL;
   }

   switch (type) {
   case EGL_SYNC_REUSABLE_KHR:
      err = EGL_SUCCESS;
      break;
   case EGL_SYNC_FENCE_KHR:
      err = egl_g3d_insert_fence_sync(gsync);
      break;
   default:
      err = EGL_BAD_ATTRIBUTE;
      break;
   }

   if (err != EGL_SUCCESS) {
      _eglError(err, "eglCreateSyncKHR");
      FREE(gsync);
      return NULL;
   }

   pipe_mutex_init(gsync->mutex);
   pipe_condvar_init(gsync->condvar);

   return &gsync->base;
}

EGLBoolean
egl_g3d_destroy_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync)
{
   struct egl_g3d_sync *gsync = egl_g3d_sync(sync);

   switch (gsync->base.Type) {
   case EGL_SYNC_REUSABLE_KHR:
      /* signal the waiters */
      if (gsync->base.SyncStatus != EGL_SIGNALED_KHR) {
         gsync->base.SyncStatus = EGL_SIGNALED_KHR;
         egl_g3d_signal_sync_condvar(gsync);
      }
      break;
   default:
      break;
   }

   egl_g3d_unref_sync(gsync);

   return EGL_TRUE;
}

EGLint
egl_g3d_client_wait_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
                         EGLint flags, EGLTimeKHR timeout)
{
   struct egl_g3d_sync *gsync = egl_g3d_sync(sync);
   EGLint ret = EGL_CONDITION_SATISFIED_KHR;

   if (gsync->base.SyncStatus != EGL_SIGNALED_KHR) {
      /* flush if there is a current context */
      if (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR) {
         _EGLContext *ctx = _eglGetCurrentContext();
         struct egl_g3d_context *gctx = egl_g3d_context(ctx);

         if (gctx)
            gctx->stctxi->flush(gctx->stctxi, ST_FLUSH_FRONT, NULL);
      }

      if (timeout) {
         /* reference the sync object in case it is destroyed while waiting */
         egl_g3d_ref_sync(gsync);

         switch (gsync->base.Type) {
         case EGL_SYNC_REUSABLE_KHR:
            ret = egl_g3d_wait_sync_condvar(gsync, timeout);
            break;
         case EGL_SYNC_FENCE_KHR:
            ret = egl_g3d_wait_fence_sync(gsync, timeout);
         default:
            break;
         }

         egl_g3d_unref_sync(gsync);
      }
      else {
         ret = EGL_TIMEOUT_EXPIRED_KHR;
      }
   }

   return ret;
}

EGLBoolean
egl_g3d_signal_sync(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSync *sync,
                    EGLenum mode)
{
   struct egl_g3d_sync *gsync = egl_g3d_sync(sync);

   /* only for reusable sync */
   if (sync->Type != EGL_SYNC_REUSABLE_KHR)
      return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");

   if (gsync->base.SyncStatus != mode) {
      gsync->base.SyncStatus = mode;
      if (mode == EGL_SIGNALED_KHR)
         egl_g3d_signal_sync_condvar(gsync);
   }

   return EGL_TRUE;
}

#endif /* EGL_KHR_reusable_sync */