3 * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstglsyncmeta.h"
28 #define GST_CAT_DEFAULT gst_gl_sync_meta_debug
29 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
31 #ifndef GL_SYNC_GPU_COMMANDS_COMPLETE
32 #define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117
34 #ifndef GL_SYNC_FLUSH_COMMANDS_BIT
35 #define GL_SYNC_FLUSH_COMMANDS_BIT 0x00000001
37 #ifndef GL_TIMEOUT_EXPIRED
38 #define GL_TIMEOUT_EXPIRED 0x911B
42 gst_buffer_add_gl_sync_meta (GstGLContext * context, GstBuffer * buffer)
46 g_return_val_if_fail (GST_GL_IS_CONTEXT (context), NULL);
49 (GstGLSyncMeta *) gst_buffer_add_meta ((buffer), GST_GL_SYNC_META_INFO,
55 meta->context = gst_object_ref (context);
62 _set_sync_point (GstGLContext * context, GstGLSyncMeta * sync_meta)
64 const GstGLFuncs *gl = context->gl_vtable;
67 if (sync_meta->glsync)
68 gl->DeleteSync (sync_meta->glsync);
69 sync_meta->glsync = gl->FenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
70 GST_LOG ("setting sync object %p", sync_meta->glsync);
77 gst_gl_sync_meta_set_sync_point (GstGLSyncMeta * sync_meta,
78 GstGLContext * context)
80 gst_gl_context_thread_add (context,
81 (GstGLContextThreadFunc) _set_sync_point, sync_meta);
85 _wait (GstGLContext * context, GstGLSyncMeta * sync_meta)
87 const GstGLFuncs *gl = context->gl_vtable;
90 if (gl->ClientWaitSync) {
92 GST_LOG ("waiting on sync object %p", sync_meta->glsync);
94 gl->ClientWaitSync (sync_meta->glsync, GL_SYNC_FLUSH_COMMANDS_BIT,
95 1000000000 /* 1s */ );
96 } while (res == GL_TIMEOUT_EXPIRED);
101 gst_gl_sync_meta_wait (GstGLSyncMeta * sync_meta, GstGLContext * context)
103 if (sync_meta->context == context)
106 if (sync_meta->glsync) {
107 gst_gl_context_thread_add (context,
108 (GstGLContextThreadFunc) _wait, sync_meta);
113 _gst_gl_sync_meta_transform (GstBuffer * dest, GstMeta * meta,
114 GstBuffer * buffer, GQuark type, gpointer data)
116 GstGLSyncMeta *dmeta, *smeta;
118 smeta = (GstGLSyncMeta *) meta;
120 if (GST_META_TRANSFORM_IS_COPY (type)) {
121 GstMetaTransformCopy *copy = data;
124 /* only copy if the complete data is copied as well */
125 dmeta = gst_buffer_add_gl_sync_meta (smeta->context, dest);
130 GST_DEBUG ("copy gl sync metadata");
132 dmeta->glsync = smeta->glsync;
139 _free_gl_sync_meta (GstGLContext * context, GstGLSyncMeta * sync_meta)
141 const GstGLFuncs *gl = context->gl_vtable;
143 if (sync_meta->glsync)
144 gl->DeleteSync (sync_meta->glsync);
145 sync_meta->glsync = NULL;
149 _gst_gl_sync_meta_free (GstGLSyncMeta * sync_meta, GstBuffer * buffer)
151 if (sync_meta->glsync) {
152 gst_gl_context_thread_add (sync_meta->context,
153 (GstGLContextThreadFunc) _free_gl_sync_meta, sync_meta);
155 gst_object_unref (sync_meta->context);
159 _gst_gl_sync_meta_init (GstGLSyncMeta * sync_meta, gpointer params,
162 static volatile gsize _init;
164 if (g_once_init_enter (&_init)) {
165 GST_DEBUG_CATEGORY_INIT (gst_gl_sync_meta_debug, "glsyncmeta", 0,
167 g_once_init_leave (&_init, 1);
170 sync_meta->context = NULL;
171 sync_meta->glsync = NULL;
177 gst_gl_sync_meta_api_get_type (void)
179 static volatile GType type = 0;
180 static const gchar *tags[] = { NULL };
182 if (g_once_init_enter (&type)) {
183 GType _type = gst_meta_api_type_register ("GstGLSyncMetaAPI", tags);
184 g_once_init_leave (&type, _type);
191 gst_gl_sync_meta_get_info (void)
193 static const GstMetaInfo *meta_info = NULL;
195 if (g_once_init_enter (&meta_info)) {
196 const GstMetaInfo *meta =
197 gst_meta_register (GST_GL_SYNC_META_API_TYPE, "GstGLSyncMeta",
198 sizeof (GstVideoMeta), (GstMetaInitFunction) _gst_gl_sync_meta_init,
199 (GstMetaFreeFunction) _gst_gl_sync_meta_free,
200 _gst_gl_sync_meta_transform);
201 g_once_init_leave (&meta_info, meta);