ad2280d20e82ba227e370947f63c39487501709f
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / gstvkdescriptorset.c
1 /*
2  * GStreamer
3  * Copyright (C) 2019 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 /**
22  * SECTION:vkdescriptorset
23  * @title: GstVulkanDescriptorSet
24  * @short_description: Vulkan descriptor set
25  * @see_also: #GstVulkanDescriptorPool, #GstVulkanDescriptorCache, #GstVulkanDevice
26  *
27  * vulkandescriptorset holds information about a descriptor set.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstvkdescriptorset.h"
35 #include "gstvkdescriptorpool.h"
36 #include "gstvkdescriptorcache.h"
37
38 #define GST_CAT_DEFAULT gst_debug_vulkan_descriptor_set
39 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
40
41 #define gst_vulkan_descriptor_cache_release_set(c,s) \
42     gst_vulkan_handle_pool_release (GST_VULKAN_HANDLE_POOL_CAST (c), s);
43
44 static void
45 init_debug (void)
46 {
47   static volatile gsize _init = 0;
48
49   if (g_once_init_enter (&_init)) {
50     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkandescriptorset", 0,
51         "Vulkan descriptor set");
52     g_once_init_leave (&_init, 1);
53   }
54 }
55
56 static gboolean
57 gst_vulkan_descriptor_set_dispose (GstVulkanDescriptorSet * set)
58 {
59   GstVulkanDescriptorCache *cache;
60
61   /* no pool, do free */
62   if ((cache = set->cache) == NULL)
63     return TRUE;
64
65   /* keep the buffer alive */
66   gst_vulkan_descriptor_set_ref (set);
67   /* return the buffer to the pool */
68   gst_vulkan_descriptor_cache_release_set (cache, set);
69
70   return FALSE;
71 }
72
73 static void
74 gst_vulkan_descriptor_set_free (GstVulkanDescriptorSet * set)
75 {
76   guint i;
77
78   g_assert (set->cache == NULL);
79
80   GST_TRACE ("Freeing %p", set);
81
82   for (i = 0; i < set->n_layouts; i++)
83     gst_vulkan_handle_unref (set->layouts[i]);
84   g_free (set->layouts);
85
86   vkFreeDescriptorSets (set->pool->device->device, set->pool->pool, 1,
87       &set->set);
88
89   gst_clear_object (&set->pool);
90
91   g_free (set);
92 }
93
94 static void
95 gst_vulkan_descriptor_set_init (GstVulkanDescriptorSet * set,
96     GstVulkanDescriptorPool * pool, VkDescriptorSet desc_set, guint n_layouts,
97     GstVulkanHandle ** layouts)
98 {
99   guint i;
100
101   set->pool = gst_object_ref (pool);
102   set->set = desc_set;
103   set->n_layouts = n_layouts;
104   set->layouts = g_new0 (GstVulkanHandle *, n_layouts);
105   for (i = 0; i < n_layouts; i++)
106     set->layouts[i] = gst_vulkan_handle_ref (layouts[i]);
107
108   init_debug ();
109
110   GST_TRACE ("new %p", set);
111
112   gst_mini_object_init (&set->parent, 0, GST_TYPE_VULKAN_DESCRIPTOR_SET,
113       NULL, (GstMiniObjectDisposeFunction) gst_vulkan_descriptor_set_dispose,
114       (GstMiniObjectFreeFunction) gst_vulkan_descriptor_set_free);
115 }
116
117 /**
118  * gst_vulkan_descriptor_set_new_wrapped:
119  * @set: a VkDescriptorSet
120  *
121  * Returns: (transfer full): a new #GstVulkanDescriptorSet
122  *
123  * Since: 1.18
124  */
125 GstVulkanDescriptorSet *
126 gst_vulkan_descriptor_set_new_wrapped (GstVulkanDescriptorPool * pool,
127     VkDescriptorSet set, guint n_layouts, GstVulkanHandle ** layouts)
128 {
129   GstVulkanDescriptorSet *ret;
130
131   g_return_val_if_fail (GST_IS_VULKAN_DESCRIPTOR_POOL (pool), NULL);
132   g_return_val_if_fail (set != VK_NULL_HANDLE, NULL);
133   g_return_val_if_fail (n_layouts > 0, NULL);
134   g_return_val_if_fail (layouts != NULL, NULL);
135
136   ret = g_new0 (GstVulkanDescriptorSet, 1);
137   gst_vulkan_descriptor_set_init (ret, pool, set, n_layouts, layouts);
138
139   return ret;
140 }
141
142 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanDescriptorSet, gst_vulkan_descriptor_set);