3 * Copyright (C) 2016 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.
25 #include "gstvkfence.h"
26 #include "gstvkdevice.h"
30 * @title: GstVulkanFence
31 * @short_description: Vulkan fences
32 * @see_also: #GstVulkanDevice
34 * A #GstVulkanFence encapsulates a VkFence
37 GST_DEBUG_CATEGORY (gst_debug_vulkan_fence);
38 #define GST_CAT_DEFAULT gst_debug_vulkan_fence
40 #define gst_vulkan_fence_cache_release(c,f) gst_vulkan_handle_pool_release(GST_VULKAN_HANDLE_POOL (c), f)
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);
55 gst_vulkan_fence_dispose (GstVulkanFence * fence)
57 GstVulkanFenceCache *cache;
59 /* no pool, do free */
60 if ((cache = fence->cache) == NULL)
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);
72 gst_vulkan_fence_free (GstVulkanFence * fence)
77 GST_TRACE ("Freeing fence %p", fence);
80 vkDestroyFence (fence->device->device, fence->fence, NULL);
82 gst_clear_object (&fence->device);
88 * gst_vulkan_fence_new:
89 * @device: the parent #GstVulkanDevice
90 * @error: a #GError for the failure condition
92 * Returns: whether a new #GstVulkanFence or %NULL on error
97 gst_vulkan_fence_new (GstVulkanDevice * device, GError ** error)
99 VkFenceCreateInfo fence_info = { 0, };
100 GstVulkanFence *fence;
105 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
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);
111 fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
112 fence_info.pNext = NULL;
113 fence_info.flags = 0;
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);
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);
130 * gst_vulkan_fence_new_always_signalled:
132 * Returns: a new #GstVulkanFence that is always in the signalled state
137 gst_vulkan_fence_new_always_signalled (GstVulkanDevice * device)
139 GstVulkanFence *fence;
141 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
145 fence = g_new0 (GstVulkanFence, 1);
146 GST_TRACE ("Creating always-signalled fence %p with device %" GST_PTR_FORMAT,
148 fence->device = gst_object_ref (device);
149 fence->fence = VK_NULL_HANDLE;
151 gst_mini_object_init (GST_MINI_OBJECT_CAST (fence), 0, GST_TYPE_VULKAN_FENCE,
152 NULL, NULL, (GstMiniObjectFreeFunction) gst_vulkan_fence_free);
158 * gst_vulkan_fence_is_signaled:
159 * @fence: a #GstVulkanFence
161 * Returns: whether @fence has been signalled
166 gst_vulkan_fence_is_signaled (GstVulkanFence * fence)
168 g_return_val_if_fail (fence != NULL, FALSE);
173 return vkGetFenceStatus (fence->device->device, fence->fence) == VK_SUCCESS;
177 gst_vulkan_fence_reset (GstVulkanFence * fence)
179 g_return_if_fail (fence != NULL);
184 GST_TRACE ("resetting fence %p", fence);
185 vkResetFences (fence->device->device, 1, &fence->fence);
188 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanFence, gst_vulkan_fence);
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 ());
194 GstVulkanFenceCache *
195 gst_vulkan_fence_cache_new (GstVulkanDevice * device)
197 GstVulkanFenceCache *ret;
198 GstVulkanHandlePool *pool;
200 g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), NULL);
202 ret = g_object_new (GST_TYPE_VULKAN_FENCE_CACHE, NULL);
204 pool = GST_VULKAN_HANDLE_POOL (ret);
205 pool->device = gst_object_ref (device);
207 gst_object_ref_sink (ret);
213 gst_vulkan_fence_cache_alloc (GstVulkanHandlePool * pool, GError ** error)
215 return gst_vulkan_fence_new (pool->device, error);
219 gst_vulkan_fence_cache_acquire_impl (GstVulkanHandlePool * pool,
222 GstVulkanFence *fence;
225 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->acquire (pool, error))) {
226 fence->cache = gst_object_ref (pool);
228 fence->device = gst_object_ref (pool->device);
235 gst_vulkan_fence_cache_release_impl (GstVulkanHandlePool * pool,
238 GstVulkanFence *fence = handle;
240 gst_vulkan_fence_reset (fence);
242 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->release (pool, handle);
245 gst_clear_object (&fence->cache);
246 gst_clear_object (&fence->device);
251 gst_vulkan_fence_cache_free (GstVulkanHandlePool * pool, gpointer handle)
253 GstVulkanFence *fence = handle;
256 fence->device = gst_object_ref (pool->device);
258 gst_vulkan_fence_unref (handle);
262 gst_vulkan_fence_cache_init (GstVulkanFenceCache * cache)
267 gst_vulkan_fence_cache_class_init (GstVulkanFenceCacheClass * klass)
269 GstVulkanHandlePoolClass *handle_class = (GstVulkanHandlePoolClass *) klass;
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;