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 "vkfence.h"
26 #include "vkutils_private.h"
27 
28 GST_DEBUG_CATEGORY (gst_debug_vulkan_fence);
29 #define GST_CAT_DEFAULT gst_debug_vulkan_fence
30 
31 static void
_init_debug(void)32 _init_debug (void)
33 {
34   static volatile gsize init;
35 
36   if (g_once_init_enter (&init)) {
37     GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_fence,
38         "vulkanfence", 0, "Vulkan Fence");
39     g_once_init_leave (&init, 1);
40   }
41 }
42 
43 static void
gst_vulkan_fence_free(GstVulkanFence * fence)44 gst_vulkan_fence_free (GstVulkanFence * fence)
45 {
46   if (!fence)
47     return;
48 
49   GST_TRACE ("Freeing fence %p", fence);
50 
51   vkDestroyFence (fence->device->device, fence->fence, NULL);
52 
53   gst_object_unref (fence->device);
54 
55   g_free (fence);
56 }
57 
58 GstVulkanFence *
gst_vulkan_fence_new(GstVulkanDevice * device,VkFenceCreateFlags flags,GError ** error)59 gst_vulkan_fence_new (GstVulkanDevice * device, VkFenceCreateFlags flags,
60     GError ** error)
61 {
62   VkFenceCreateInfo fence_info = { 0, };
63   GstVulkanFence *fence;
64   VkResult err;
65 
66   _init_debug ();
67 
68   g_return_val_if_fail (GST_IS_VULKAN_DEVICE (device), FALSE);
69 
70   fence = g_new0 (GstVulkanFence, 1);
71   GST_TRACE ("Creating fence %p with device %" GST_PTR_FORMAT, fence, device);
72   fence->device = gst_object_ref (device);
73 
74   fence_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
75   fence_info.pNext = NULL;
76   fence_info.flags = flags;
77 
78   err = vkCreateFence (device->device, &fence_info, NULL, &fence->fence);
79   if (gst_vulkan_error_to_g_error (err, error, "vkCreateFence") < 0) {
80     g_free (fence);
81     return NULL;
82   }
83 
84   gst_mini_object_init (GST_MINI_OBJECT_CAST (fence), 0, GST_TYPE_VULKAN_FENCE,
85       NULL, NULL, (GstMiniObjectFreeFunction) gst_vulkan_fence_free);
86 
87   return fence;
88 }
89 
90 gboolean
gst_vulkan_fence_is_signaled(GstVulkanFence * fence)91 gst_vulkan_fence_is_signaled (GstVulkanFence * fence)
92 {
93   g_return_val_if_fail (fence != NULL, FALSE);
94 
95   return vkGetFenceStatus (fence->device->device, fence->fence) == VK_SUCCESS;
96 }
97 
98 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanFence, gst_vulkan_fence);
99