1 /*
2 * GStreamer
3 * Copyright (C) 2016 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 "gstvkfence.h"
26 #include "gstvkdevice.h"
27
28 /**
29 * SECTION:vkfence
30 * @title: GstVulkanFence
31 * @short_description: Vulkan fences
32 * @see_also: #GstVulkanDevice
33 *
34 * A #GstVulkanFence encapsulates a VkFence
35 */
36
37 GST_DEBUG_CATEGORY (gst_debug_vulkan_fence);
38 #define GST_CAT_DEFAULT gst_debug_vulkan_fence
39
40 #define gst_vulkan_fence_cache_release(c,f) gst_vulkan_handle_pool_release(GST_VULKAN_HANDLE_POOL (c), f)
41
42 static void
_init_debug(void)43 _init_debug (void)
44 {
45 static gsize init;
46
47 if (g_once_init_enter (&init)) {
48 GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_fence,
49 "vulkanfence", 0, "Vulkan Fence");
50 g_once_init_leave (&init, 1);
51 }
52 }
53
54 static gboolean
gst_vulkan_fence_dispose(GstVulkanFence * fence)55 gst_vulkan_fence_dispose (GstVulkanFence * fence)
56 {
57 GstVulkanFenceCache *cache;
58
59 /* no pool, do free */
60 if ((cache = fence->cache) == NULL)
61 return TRUE;
62
63 /* keep the buffer alive */
64 gst_vulkan_fence_ref (fence);
65 /* return the buffer to the cache */
66 gst_vulkan_fence_cache_release (cache, fence);
67
68 return FALSE;
69 }
70
71 static void
gst_vulkan_fence_free(GstVulkanFence * fence)72 gst_vulkan_fence_free (GstVulkanFence * fence)
73 {
74 if (!fence)
75 return;
76
77 GST_TRACE ("Freeing fence %p", fence);
78
79 if (fence->fence)
80 vkDestroyFence (fence->device->device, fence->fence, NULL);
81
82 gst_clear_object (&fence->device);
83
84 g_free (fence);
85 }
86
87 /**
88 * gst_vulkan_fence_new:
89 * @device: the parent #GstVulkanDevice
90 * @error: a #GError for the failure condition
91 *
92 * Returns: whether a new #GstVulkanFence or %NULL on error
93 *
94 * Since: 1.18
95 */
96 GstVulkanFence *
gst_vulkan_fence_new(GstVulkanDevice * device,GError ** error)97 gst_vulkan_fence_new (GstVulkanDevice * device, GError ** error)
98 {
99 VkFenceCreateInfo fence_info = { 0, };
100 GstVulkanFence *fence;
101 VkResult err;
102
103 _init_debug ();
104
105 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
106
107 fence = g_new0 (GstVulkanFence, 1);
108 GST_TRACE ("Creating fence %p with device %" GST_PTR_FORMAT, fence, device);
109 fence->device = gst_object_ref (device);
110
111 fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
112 fence_info.pNext = NULL;
113 fence_info.flags = 0;
114
115 err = vkCreateFence (device->device, &fence_info, NULL, &fence->fence);
116 if (gst_vulkan_error_to_g_error (err, error, "vkCreateFence") < 0) {
117 gst_clear_object (&fence->device);
118 g_free (fence);
119 return NULL;
120 }
121
122 gst_mini_object_init (GST_MINI_OBJECT_CAST (fence), 0, GST_TYPE_VULKAN_FENCE,
123 NULL, (GstMiniObjectDisposeFunction) gst_vulkan_fence_dispose,
124 (GstMiniObjectFreeFunction) gst_vulkan_fence_free);
125
126 return fence;
127 }
128
129 /**
130 * gst_vulkan_fence_new_always_signalled:
131 *
132 * Returns: a new #GstVulkanFence that is always in the signalled state
133 *
134 * Since: 1.18
135 */
136 GstVulkanFence *
gst_vulkan_fence_new_always_signalled(GstVulkanDevice * device)137 gst_vulkan_fence_new_always_signalled (GstVulkanDevice * device)
138 {
139 GstVulkanFence *fence;
140
141 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
142
143 _init_debug ();
144
145 fence = g_new0 (GstVulkanFence, 1);
146 GST_TRACE ("Creating always-signalled fence %p with device %" GST_PTR_FORMAT,
147 fence, device);
148 fence->device = gst_object_ref (device);
149 fence->fence = VK_NULL_HANDLE;
150
151 gst_mini_object_init (GST_MINI_OBJECT_CAST (fence), 0, GST_TYPE_VULKAN_FENCE,
152 NULL, NULL, (GstMiniObjectFreeFunction) gst_vulkan_fence_free);
153
154 return fence;
155 }
156
157 /**
158 * gst_vulkan_fence_is_signaled:
159 * @fence: a #GstVulkanFence
160 *
161 * Returns: whether @fence has been signalled
162 *
163 * Since: 1.18
164 */
165 gboolean
gst_vulkan_fence_is_signaled(GstVulkanFence * fence)166 gst_vulkan_fence_is_signaled (GstVulkanFence * fence)
167 {
168 g_return_val_if_fail (fence != NULL, FALSE);
169
170 if (!fence->fence)
171 return TRUE;
172
173 return vkGetFenceStatus (fence->device->device, fence->fence) == VK_SUCCESS;
174 }
175
176 void
gst_vulkan_fence_reset(GstVulkanFence * fence)177 gst_vulkan_fence_reset (GstVulkanFence * fence)
178 {
179 g_return_if_fail (fence != NULL);
180
181 if (!fence->fence)
182 return;
183
184 GST_TRACE ("resetting fence %p", fence);
185 vkResetFences (fence->device->device, 1, &fence->fence);
186 }
187
188 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanFence, gst_vulkan_fence);
189
190 #define parent_class gst_vulkan_fence_cache_parent_class
191 G_DEFINE_TYPE_WITH_CODE (GstVulkanFenceCache, gst_vulkan_fence_cache,
192 GST_TYPE_VULKAN_HANDLE_POOL, _init_debug ());
193
194 GstVulkanFenceCache *
gst_vulkan_fence_cache_new(GstVulkanDevice * device)195 gst_vulkan_fence_cache_new (GstVulkanDevice * device)
196 {
197 GstVulkanFenceCache *ret;
198 GstVulkanHandlePool *pool;
199
200 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
201
202 ret = g_object_new (GST_TYPE_VULKAN_FENCE_CACHE, NULL);
203
204 pool = GST_VULKAN_HANDLE_POOL (ret);
205 pool->device = gst_object_ref (device);
206
207 gst_object_ref_sink (ret);
208
209 return ret;
210 }
211
212 static gpointer
gst_vulkan_fence_cache_alloc(GstVulkanHandlePool * pool,GError ** error)213 gst_vulkan_fence_cache_alloc (GstVulkanHandlePool * pool, GError ** error)
214 {
215 return gst_vulkan_fence_new (pool->device, error);
216 }
217
218 static gpointer
gst_vulkan_fence_cache_acquire_impl(GstVulkanHandlePool * pool,GError ** error)219 gst_vulkan_fence_cache_acquire_impl (GstVulkanHandlePool * pool,
220 GError ** error)
221 {
222 GstVulkanFence *fence;
223
224 if ((fence =
225 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->acquire (pool, error))) {
226 fence->cache = gst_object_ref (pool);
227 if (!fence->device)
228 fence->device = gst_object_ref (pool->device);
229 }
230
231 return fence;
232 }
233
234 static void
gst_vulkan_fence_cache_release_impl(GstVulkanHandlePool * pool,gpointer handle)235 gst_vulkan_fence_cache_release_impl (GstVulkanHandlePool * pool,
236 gpointer handle)
237 {
238 GstVulkanFence *fence = handle;
239
240 gst_vulkan_fence_reset (fence);
241
242 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->release (pool, handle);
243
244 if (fence) {
245 gst_clear_object (&fence->cache);
246 gst_clear_object (&fence->device);
247 }
248 }
249
250 static void
gst_vulkan_fence_cache_free(GstVulkanHandlePool * pool,gpointer handle)251 gst_vulkan_fence_cache_free (GstVulkanHandlePool * pool, gpointer handle)
252 {
253 GstVulkanFence *fence = handle;
254
255 if (!fence->device)
256 fence->device = gst_object_ref (pool->device);
257
258 gst_vulkan_fence_unref (handle);
259 }
260
261 static void
gst_vulkan_fence_cache_init(GstVulkanFenceCache * cache)262 gst_vulkan_fence_cache_init (GstVulkanFenceCache * cache)
263 {
264 }
265
266 static void
gst_vulkan_fence_cache_class_init(GstVulkanFenceCacheClass * klass)267 gst_vulkan_fence_cache_class_init (GstVulkanFenceCacheClass * klass)
268 {
269 GstVulkanHandlePoolClass *handle_class = (GstVulkanHandlePoolClass *) klass;
270
271 handle_class->acquire = gst_vulkan_fence_cache_acquire_impl;
272 handle_class->alloc = gst_vulkan_fence_cache_alloc;
273 handle_class->release = gst_vulkan_fence_cache_release_impl;
274 handle_class->free = gst_vulkan_fence_cache_free;
275 }
276