291edc04967178555e84c74e9c5b176d021bfe86
[platform/upstream/gstreamer.git] / gst-libs / gst / gl / gstglsyncmeta.c
1 /*
2  * GStreamer
3  * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gl.h"
26 #include "gstglsyncmeta.h"
27
28 #define GST_CAT_DEFAULT gst_gl_sync_meta_debug
29 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
30
31 #ifndef GL_SYNC_GPU_COMMANDS_COMPLETE
32 #define GL_SYNC_GPU_COMMANDS_COMPLETE 0x9117
33 #endif
34 #ifndef GL_SYNC_FLUSH_COMMANDS_BIT
35 #define GL_SYNC_FLUSH_COMMANDS_BIT        0x00000001
36 #endif
37 #ifndef GL_TIMEOUT_EXPIRED
38 #define GL_TIMEOUT_EXPIRED 0x911B
39 #endif
40
41 GstGLSyncMeta *
42 gst_buffer_add_gl_sync_meta (GstGLContext * context, GstBuffer * buffer)
43 {
44   GstGLSyncMeta *meta =
45       (GstGLSyncMeta *) gst_buffer_add_meta ((buffer), GST_GL_SYNC_META_INFO,
46       NULL);
47
48   if (!meta)
49     return NULL;
50
51   meta->context = gst_object_ref (context);
52   meta->glsync = NULL;
53
54   return meta;
55 }
56
57 static void
58 _set_sync_point (GstGLContext * context, GstGLSyncMeta * sync_meta)
59 {
60   const GstGLFuncs *gl = context->gl_vtable;
61
62   if (gl->FenceSync) {
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);
67   } else {
68     gl->Flush ();
69   }
70 }
71
72 void
73 gst_gl_sync_meta_set_sync_point (GstGLSyncMeta * sync_meta,
74     GstGLContext * context)
75 {
76   gst_gl_context_thread_add (context,
77       (GstGLContextThreadFunc) _set_sync_point, sync_meta);
78 }
79
80 static void
81 _wait (GstGLContext * context, GstGLSyncMeta * sync_meta)
82 {
83   const GstGLFuncs *gl = context->gl_vtable;
84   GLenum res;
85
86   if (gl->ClientWaitSync) {
87     do {
88       GST_LOG ("waiting on sync object %p", sync_meta->glsync);
89       res =
90           gl->ClientWaitSync (sync_meta->glsync, GL_SYNC_FLUSH_COMMANDS_BIT,
91           1000000000 /* 1s */ );
92     } while (res == GL_TIMEOUT_EXPIRED);
93   }
94 }
95
96 void
97 gst_gl_sync_meta_wait (GstGLSyncMeta * sync_meta)
98 {
99   if (sync_meta->glsync) {
100     gst_gl_context_thread_add (sync_meta->context,
101         (GstGLContextThreadFunc) _wait, sync_meta);
102   }
103 }
104
105 static gboolean
106 _gst_gl_sync_meta_transform (GstBuffer * dest, GstMeta * meta,
107     GstBuffer * buffer, GQuark type, gpointer data)
108 {
109   GstGLSyncMeta *dmeta, *smeta;
110
111   smeta = (GstGLSyncMeta *) meta;
112
113   if (GST_META_TRANSFORM_IS_COPY (type)) {
114     GstMetaTransformCopy *copy = data;
115
116     if (!copy->region) {
117       /* only copy if the complete data is copied as well */
118       dmeta = gst_buffer_add_gl_sync_meta (smeta->context, dest);
119
120       if (!dmeta)
121         return FALSE;
122
123       GST_DEBUG ("copy gl sync metadata");
124
125       dmeta->glsync = smeta->glsync;
126     }
127   }
128   return TRUE;
129 }
130
131 static void
132 _free_gl_sync_meta (GstGLContext * context, GstGLSyncMeta * sync_meta)
133 {
134   const GstGLFuncs *gl = context->gl_vtable;
135
136   if (sync_meta->glsync)
137     gl->DeleteSync (sync_meta->glsync);
138   sync_meta->glsync = NULL;
139 }
140
141 static void
142 _gst_gl_sync_meta_free (GstGLSyncMeta * sync_meta, GstBuffer * buffer)
143 {
144   if (sync_meta->glsync) {
145     gst_gl_context_thread_add (sync_meta->context,
146         (GstGLContextThreadFunc) _free_gl_sync_meta, sync_meta);
147   }
148   gst_object_unref (sync_meta->context);
149 }
150
151 static gboolean
152 _gst_gl_sync_meta_init (GstGLSyncMeta * sync_meta, gpointer params,
153     GstBuffer * buffer)
154 {
155   static volatile gsize _init;
156
157   if (g_once_init_enter (&_init)) {
158     GST_DEBUG_CATEGORY_INIT (gst_gl_sync_meta_debug, "glsyncmeta", 0,
159         "glsyncmeta");
160     g_once_init_leave (&_init, 1);
161   }
162
163   sync_meta->context = NULL;
164   sync_meta->glsync = NULL;
165
166   return TRUE;
167 }
168
169 GType
170 gst_gl_sync_meta_api_get_type (void)
171 {
172   static volatile GType type = 0;
173   static const gchar *tags[] = { NULL };
174
175   if (g_once_init_enter (&type)) {
176     GType _type = gst_meta_api_type_register ("GstGLSyncMetaAPI", tags);
177     g_once_init_leave (&type, _type);
178   }
179
180   return type;
181 }
182
183 const GstMetaInfo *
184 gst_gl_sync_meta_get_info (void)
185 {
186   static const GstMetaInfo *meta_info = NULL;
187
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);
195   }
196
197   return meta_info;
198 }