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)
45 (GstGLSyncMeta *) gst_buffer_add_meta ((buffer), GST_GL_SYNC_META_INFO,
51 meta->context = gst_object_ref (context);
58 _set_sync_point (GstGLContext * context, GstGLSyncMeta * sync_meta)
60 const GstGLFuncs *gl = context->gl_vtable;
63 if (sync_meta->glsync)
64 gl->DeleteSync (sync_meta->glsync);
65 sync_meta->glsync = gl->FenceSync (GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
66 GST_LOG ("setting sync object %p", sync_meta->glsync);
73 gst_gl_sync_meta_set_sync_point (GstGLSyncMeta * sync_meta,
74 GstGLContext * context)
76 gst_gl_context_thread_add (context,
77 (GstGLContextThreadFunc) _set_sync_point, sync_meta);
81 _wait (GstGLContext * context, GstGLSyncMeta * sync_meta)
83 const GstGLFuncs *gl = context->gl_vtable;
86 if (gl->ClientWaitSync) {
88 GST_LOG ("waiting on sync object %p", sync_meta->glsync);
90 gl->ClientWaitSync (sync_meta->glsync, GL_SYNC_FLUSH_COMMANDS_BIT,
91 1000000000 /* 1s */ );
92 } while (res == GL_TIMEOUT_EXPIRED);
97 gst_gl_sync_meta_wait (GstGLSyncMeta * sync_meta)
99 if (sync_meta->glsync) {
100 gst_gl_context_thread_add (sync_meta->context,
101 (GstGLContextThreadFunc) _wait, sync_meta);
106 _gst_gl_sync_meta_transform (GstBuffer * dest, GstMeta * meta,
107 GstBuffer * buffer, GQuark type, gpointer data)
109 GstGLSyncMeta *dmeta, *smeta;
111 smeta = (GstGLSyncMeta *) meta;
113 if (GST_META_TRANSFORM_IS_COPY (type)) {
114 GstMetaTransformCopy *copy = data;
117 /* only copy if the complete data is copied as well */
118 dmeta = gst_buffer_add_gl_sync_meta (smeta->context, dest);
123 GST_DEBUG ("copy gl sync metadata");
125 dmeta->glsync = smeta->glsync;
132 _free_gl_sync_meta (GstGLContext * context, GstGLSyncMeta * sync_meta)
134 const GstGLFuncs *gl = context->gl_vtable;
136 if (sync_meta->glsync)
137 gl->DeleteSync (sync_meta->glsync);
138 sync_meta->glsync = NULL;
142 _gst_gl_sync_meta_free (GstGLSyncMeta * sync_meta, GstBuffer * buffer)
144 if (sync_meta->glsync) {
145 gst_gl_context_thread_add (sync_meta->context,
146 (GstGLContextThreadFunc) _free_gl_sync_meta, sync_meta);
148 gst_object_unref (sync_meta->context);
152 _gst_gl_sync_meta_init (GstGLSyncMeta * sync_meta, gpointer params,
155 static volatile gsize _init;
157 if (g_once_init_enter (&_init)) {
158 GST_DEBUG_CATEGORY_INIT (gst_gl_sync_meta_debug, "glsyncmeta", 0,
160 g_once_init_leave (&_init, 1);
163 sync_meta->context = NULL;
164 sync_meta->glsync = NULL;
170 gst_gl_sync_meta_api_get_type (void)
172 static volatile GType type = 0;
173 static const gchar *tags[] = { NULL };
175 if (g_once_init_enter (&type)) {
176 GType _type = gst_meta_api_type_register ("GstGLSyncMetaAPI", tags);
177 g_once_init_leave (&type, _type);
184 gst_gl_sync_meta_get_info (void)
186 static const GstMetaInfo *meta_info = NULL;
188 if (g_once_init_enter (&meta_info)) {
189 const GstMetaInfo *meta =
190 gst_meta_register (GST_GL_SYNC_META_API_TYPE, "GstGLSyncMeta",
191 sizeof (GstVideoMeta), (GstMetaInitFunction) _gst_gl_sync_meta_init,
192 (GstMetaFreeFunction) _gst_gl_sync_meta_free,
193 _gst_gl_sync_meta_transform);
194 g_once_init_leave (&meta_info, meta);