summaryrefslogtreecommitdiff
path: root/gst-libs/gst/gl/gstglsyncmeta.c
blob: 31fa6796c56be68a2df32acd21e1674e365c9f4a (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * GStreamer
 * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gstglsyncmeta
 * @title: GstGLSyncMeta
 * @short_description: synchronization primitives
 * @see_also: #GstGLBaseMemory, #GstGLContext
 *
 * #GstGLSyncMeta provides the ability to synchronize the OpenGL command stream
 * with the CPU or with other OpenGL contexts.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstglsyncmeta.h"

#include "gstglcontext.h"
#include "gstglfuncs.h"

GST_DEBUG_CATEGORY_STATIC (gst_gl_sync_meta_debug);
#define GST_CAT_DEFAULT gst_gl_sync_meta_debug

#ifndef GL_SYNC_GPU_COMMANDS_COMPLETE
#define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117
#endif
#ifndef GL_SYNC_FLUSH_COMMANDS_BIT
#define GL_SYNC_FLUSH_COMMANDS_BIT        0x00000001
#endif
#ifndef GL_TIMEOUT_EXPIRED
#define GL_TIMEOUT_EXPIRED 0x911B
#endif
#ifndef GL_TIMEOUT_IGNORED
#define GL_TIMEOUT_IGNORED G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF)
#endif

static void
_default_set_sync_gl (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  const GstGLFuncs *gl = context->gl_vtable;

  if (gl->FenceSync) {
    if (sync_meta->data) {
      GST_LOG ("deleting sync object %p", sync_meta->data);
      gl->DeleteSync ((GLsync) sync_meta->data);
    }
    sync_meta->data =
        (gpointer) gl->FenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
    GST_LOG ("setting sync object %p", sync_meta->data);
  }

  if (gst_gl_context_is_shared (context))
    gl->Flush ();
}

static void
_default_wait_gl (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  const GstGLFuncs *gl = context->gl_vtable;

  if (sync_meta->data && gl->WaitSync) {
    GST_LOG ("waiting on sync object %p", sync_meta->data);
    gl->WaitSync ((GLsync) sync_meta->data, 0, GL_TIMEOUT_IGNORED);
  }
}

static void
_default_wait_cpu_gl (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  const GstGLFuncs *gl = context->gl_vtable;
  GLenum res;

  if (sync_meta->data && gl->ClientWaitSync) {
    do {
      GST_LOG ("waiting on sync object %p", sync_meta->data);
      res =
          gl->ClientWaitSync ((GLsync) sync_meta->data,
          GL_SYNC_FLUSH_COMMANDS_BIT, 1000000000 /* 1s */ );
    } while (res == GL_TIMEOUT_EXPIRED);
  } else {
    gl->Finish ();
  }
}

static void
_default_copy (GstGLSyncMeta * src, GstBuffer * sbuffer, GstGLSyncMeta * dest,
    GstBuffer * dbuffer)
{
  GST_LOG ("copy sync object %p from meta %p to %p", src->data, src, dest);

  /* Setting a sync point here relies on GstBuffer copying
   * metas after data */
  gst_gl_sync_meta_set_sync_point (src, src->context);
}

static void
_default_free_gl (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  const GstGLFuncs *gl = context->gl_vtable;

  if (sync_meta->data) {
    GST_LOG ("deleting sync object %p", sync_meta->data);
    gl->DeleteSync ((GLsync) sync_meta->data);
    sync_meta->data = NULL;
  }
}

/**
 * gst_buffer_add_gl_sync_meta_full:
 * @context: a #GstGLContext
 * @buffer: a #GstBuffer
 * @data: sync data to hold
 *
 * Returns: (transfer none): the #GstGLSyncMeta added to #GstBuffer
 *
 * Since: 1.8
 */
GstGLSyncMeta *
gst_buffer_add_gl_sync_meta_full (GstGLContext * context, GstBuffer * buffer,
    gpointer data)
{
  GstGLSyncMeta *meta;

  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);

  meta =
      (GstGLSyncMeta *) gst_buffer_add_meta ((buffer), GST_GL_SYNC_META_INFO,
      NULL);

  if (!meta)
    return NULL;

  meta->context = gst_object_ref (context);
  meta->data = data;

  return meta;
}

/**
 * gst_buffer_add_gl_sync_meta:
 * @context: a #GstGLContext
 * @buffer: a #GstBuffer
 *
 * Returns: (transfer none): the #GstGLSyncMeta added to #GstBuffer
 *
 * Since: 1.6
 */
GstGLSyncMeta *
gst_buffer_add_gl_sync_meta (GstGLContext * context, GstBuffer * buffer)
{
  GstGLSyncMeta *ret = gst_buffer_add_gl_sync_meta_full (context, buffer, NULL);
  if (!ret)
    return NULL;

  ret->set_sync_gl = _default_set_sync_gl;
  ret->wait_gl = _default_wait_gl;
  ret->wait_cpu_gl = _default_wait_cpu_gl;
  ret->copy = _default_copy;
  ret->free_gl = _default_free_gl;

  return ret;
}

static void
_set_sync_point (GstGLContext * context, GstGLSyncMeta * sync_meta)
{
  g_assert (sync_meta->set_sync_gl != NULL);

  GST_LOG ("setting sync point %p", sync_meta);
  sync_meta->set_sync_gl (sync_meta, context);
}

/**
 * gst_gl_sync_meta_set_sync_point:
 * @sync_meta: a #GstGLSyncMeta
 * @context: a #GstGLContext
 *
 * Set a sync point to possibly wait on at a later time.
 *
 * Since: 1.6
 */
void
gst_gl_sync_meta_set_sync_point (GstGLSyncMeta * sync_meta,
    GstGLContext * context)
{
  if (sync_meta->set_sync)
    sync_meta->set_sync (sync_meta, context);
  else
    gst_gl_context_thread_add (context,
        (GstGLContextThreadFunc) _set_sync_point, sync_meta);
}

static void
_wait (GstGLContext * context, GstGLSyncMeta * sync_meta)
{
  g_assert (sync_meta->wait_gl != NULL);

  GST_LOG ("waiting %p", sync_meta);
  sync_meta->wait_gl (sync_meta, context);
}

/**
 * gst_gl_sync_meta_wait:
 * @sync_meta: a #GstGLSyncMeta
 * @context: a #GstGLContext
 *
 * Insert a wait into @context's command stream ensuring all previous OpenGL
 * commands before @sync_meta have completed.
 *
 * Since: 1.6
 */
void
gst_gl_sync_meta_wait (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  if (sync_meta->wait)
    sync_meta->wait (sync_meta, context);
  else
    gst_gl_context_thread_add (context,
        (GstGLContextThreadFunc) _wait, sync_meta);
}

static void
_wait_cpu (GstGLContext * context, GstGLSyncMeta * sync_meta)
{
  g_assert (sync_meta->wait_cpu_gl != NULL);

  GST_LOG ("waiting %p", sync_meta);
  sync_meta->wait_cpu_gl (sync_meta, context);
}

/**
 * gst_gl_sync_meta_wait_cpu:
 * @sync_meta: a #GstGLSyncMeta
 * @context: a #GstGLContext
 *
 * Perform a wait so that the sync point has passed from the CPU's perspective
 * What that means, is that all GL operations changing CPU-visible data before
 * the sync point are now visible.
 *
 * Since: 1.8
 */
void
gst_gl_sync_meta_wait_cpu (GstGLSyncMeta * sync_meta, GstGLContext * context)
{
  if (sync_meta->wait_cpu)
    sync_meta->wait_cpu (sync_meta, context);
  else
    gst_gl_context_thread_add (context,
        (GstGLContextThreadFunc) _wait_cpu, sync_meta);
}

static gboolean
_gst_gl_sync_meta_transform (GstBuffer * dest, GstMeta * meta,
    GstBuffer * buffer, GQuark type, gpointer data)
{
  GstGLSyncMeta *dmeta, *smeta;

  smeta = (GstGLSyncMeta *) meta;

  if (GST_META_TRANSFORM_IS_COPY (type)) {
    GstMetaTransformCopy *copy = data;

    g_assert (smeta->copy != NULL);

    if (!copy->region) {
      /* only copy if the complete data is copied as well */
      dmeta = gst_buffer_add_gl_sync_meta_full (smeta->context, dest, NULL);
      if (!dmeta)
        return FALSE;

      dmeta->set_sync = smeta->set_sync;
      dmeta->set_sync_gl = smeta->set_sync_gl;
      dmeta->wait = smeta->wait;
      dmeta->wait_gl = smeta->wait_gl;
      dmeta->wait_cpu = smeta->wait_cpu;
      dmeta->wait_cpu_gl = smeta->wait_cpu_gl;
      dmeta->copy = smeta->copy;
      dmeta->free = smeta->free;
      dmeta->free_gl = smeta->free_gl;

      GST_LOG ("copying sync meta %p into %p", smeta, dmeta);
      smeta->copy (smeta, buffer, dmeta, dest);
    }
  } else {
    /* return FALSE, if transform type is not supported */
    return FALSE;
  }

  return TRUE;
}

static void
_free_gl_sync_meta (GstGLContext * context, GstGLSyncMeta * sync_meta)
{
  g_assert (sync_meta->free_gl != NULL);

  GST_LOG ("free sync meta %p", sync_meta);
  sync_meta->free_gl (sync_meta, context);
}

static void
_gst_gl_sync_meta_free (GstGLSyncMeta * sync_meta, GstBuffer * buffer)
{
  if (sync_meta->free)
    sync_meta->free (sync_meta, sync_meta->context);
  else
    gst_gl_context_thread_add (sync_meta->context,
        (GstGLContextThreadFunc) _free_gl_sync_meta, sync_meta);

  gst_object_unref (sync_meta->context);
}

static gboolean
_gst_gl_sync_meta_init (GstGLSyncMeta * sync_meta, gpointer params,
    GstBuffer * buffer)
{
  static gsize _init;

  if (g_once_init_enter (&_init)) {
    GST_DEBUG_CATEGORY_INIT (gst_gl_sync_meta_debug, "glsyncmeta", 0,
        "glsyncmeta");
    g_once_init_leave (&_init, 1);
  }

  sync_meta->context = NULL;
  sync_meta->data = NULL;
  sync_meta->set_sync = NULL;
  sync_meta->set_sync_gl = NULL;
  sync_meta->wait = NULL;
  sync_meta->wait_gl = NULL;
  sync_meta->wait_cpu = NULL;
  sync_meta->wait_cpu_gl = NULL;
  sync_meta->copy = NULL;
  sync_meta->free = NULL;
  sync_meta->free_gl = NULL;

  return TRUE;
}

GType
gst_gl_sync_meta_api_get_type (void)
{
  static GType type = 0;
  static const gchar *tags[] = { NULL };

  if (g_once_init_enter (&type)) {
    GType _type = gst_meta_api_type_register ("GstGLSyncMetaAPI", tags);
    g_once_init_leave (&type, _type);
  }

  return type;
}

const GstMetaInfo *
gst_gl_sync_meta_get_info (void)
{
  static const GstMetaInfo *meta_info = NULL;

  if (g_once_init_enter (&meta_info)) {
    const GstMetaInfo *meta =
        gst_meta_register (GST_GL_SYNC_META_API_TYPE, "GstGLSyncMeta",
        sizeof (GstGLSyncMeta), (GstMetaInitFunction) _gst_gl_sync_meta_init,
        (GstMetaFreeFunction) _gst_gl_sync_meta_free,
        _gst_gl_sync_meta_transform);
    g_once_init_leave (&meta_info, meta);
  }

  return meta_info;
}