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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstvkdescriptorcache.h"
26
27 /**
28 * SECTION:vkdescriptorcache
29 * @title: GstVulkanDescriptorCache
30 * @short_description: Vulkan descriptor cache
31 * @see_also: #GstVulkanDescriptorSet, #GstVulkanDescriptorPool, #GstVulkanDevice
32 */
33
34 #define GET_PRIV(cache) gst_vulkan_descriptor_cache_get_instance_private (cache)
35
36 #define GST_CAT_DEFAULT gst_vulkan_descriptor_cache_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38
39 struct _GstVulkanDescriptorCachePrivate
40 {
41 guint n_layouts;
42 GstVulkanHandle **layouts;
43 };
44
45 #define parent_class gst_vulkan_descriptor_cache_parent_class
46 G_DEFINE_TYPE_WITH_CODE (GstVulkanDescriptorCache, gst_vulkan_descriptor_cache,
47 GST_TYPE_VULKAN_HANDLE_POOL, G_ADD_PRIVATE (GstVulkanDescriptorCache);
48 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT,
49 "vulkancommandcache", 0, "Vulkan Command Cache"));
50
51 static void
gst_vulkan_descriptor_cache_finalize(GObject * object)52 gst_vulkan_descriptor_cache_finalize (GObject * object)
53 {
54 GstVulkanDescriptorCache *cache = GST_VULKAN_DESCRIPTOR_CACHE (object);
55 GstVulkanDescriptorCachePrivate *priv = GET_PRIV (cache);
56 guint i;
57
58 for (i = 0; i < priv->n_layouts; i++)
59 gst_vulkan_handle_unref (priv->layouts[i]);
60 g_free (priv->layouts);
61
62 gst_clear_object (&cache->pool);
63
64 G_OBJECT_CLASS (parent_class)->finalize (object);
65 }
66
67 /**
68 * gst_vulkan_descriptor_cache_new:
69 * @pool: a #GstVulkanDescriptorPool
70 * @n_layouts: number of @layouts
71 * @layouts: list of #GstVulkanHandle containing descriptor set layouts
72 *
73 * Returns: (transfer full): a new #GstVulkanDescriptorCache
74 *
75 * Since: 1.18
76 */
77 GstVulkanDescriptorCache *
gst_vulkan_descriptor_cache_new(GstVulkanDescriptorPool * pool,guint n_layouts,GstVulkanHandle ** layouts)78 gst_vulkan_descriptor_cache_new (GstVulkanDescriptorPool * pool,
79 guint n_layouts, GstVulkanHandle ** layouts)
80 {
81 GstVulkanHandlePool *handle_pool;
82 GstVulkanDescriptorCache *ret;
83 GstVulkanDescriptorCachePrivate *priv;
84 guint i;
85
86 g_return_val_if_fail (GST_IS_VULKAN_DESCRIPTOR_POOL (pool), NULL);
87
88 ret = g_object_new (GST_TYPE_VULKAN_DESCRIPTOR_CACHE, NULL);
89 ret->pool = gst_object_ref (pool);
90
91 priv = GET_PRIV (ret);
92 priv->n_layouts = n_layouts;
93 priv->layouts = g_new0 (GstVulkanHandle *, n_layouts);
94 for (i = 0; i < n_layouts; i++)
95 priv->layouts[i] = gst_vulkan_handle_ref (layouts[i]);
96
97 handle_pool = GST_VULKAN_HANDLE_POOL (ret);
98 handle_pool->device = gst_object_ref (pool->device);
99
100 gst_object_ref_sink (ret);
101
102 return ret;
103 }
104
105 static gpointer
gst_vulkan_descriptor_cache_acquire_impl(GstVulkanHandlePool * pool,GError ** error)106 gst_vulkan_descriptor_cache_acquire_impl (GstVulkanHandlePool * pool,
107 GError ** error)
108 {
109 GstVulkanDescriptorSet *set;
110
111 if ((set =
112 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->acquire (pool, error)))
113 set->cache = gst_object_ref (pool);
114
115 return set;
116 }
117
118 static gpointer
gst_vulkan_descriptor_cache_alloc_impl(GstVulkanHandlePool * pool,GError ** error)119 gst_vulkan_descriptor_cache_alloc_impl (GstVulkanHandlePool * pool,
120 GError ** error)
121 {
122 GstVulkanDescriptorCache *desc = GST_VULKAN_DESCRIPTOR_CACHE (pool);
123 GstVulkanDescriptorCachePrivate *priv = GET_PRIV (desc);
124
125 return gst_vulkan_descriptor_pool_create (desc->pool, priv->n_layouts,
126 priv->layouts, error);
127 }
128
129 static void
gst_vulkan_descriptor_cache_release_impl(GstVulkanHandlePool * pool,gpointer handle)130 gst_vulkan_descriptor_cache_release_impl (GstVulkanHandlePool * pool,
131 gpointer handle)
132 {
133 GstVulkanDescriptorSet *set = handle;
134
135 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->release (pool, handle);
136
137 /* decrease the refcount that the set had to us */
138 gst_clear_object (&set->cache);
139 }
140
141 static void
gst_vulkan_descriptor_cache_free_impl(GstVulkanHandlePool * pool,gpointer handle)142 gst_vulkan_descriptor_cache_free_impl (GstVulkanHandlePool * pool,
143 gpointer handle)
144 {
145 GstVulkanDescriptorSet *set = handle;
146
147 GST_VULKAN_HANDLE_POOL_CLASS (parent_class)->free (pool, handle);
148
149 gst_vulkan_descriptor_set_unref (set);
150 }
151
152 /**
153 * gst_vulkan_descriptor_cache_acquire:
154 * @cache: a #GstVulkanDescriptorCache
155 * @error: a #GError
156 *
157 * Returns: a new #GstVulkanDescriptorSet
158 *
159 * Since: 1.18
160 */
161 GstVulkanDescriptorSet *
gst_vulkan_descriptor_cache_acquire(GstVulkanDescriptorCache * cache,GError ** error)162 gst_vulkan_descriptor_cache_acquire (GstVulkanDescriptorCache * cache,
163 GError ** error)
164 {
165 return gst_vulkan_handle_pool_acquire (GST_VULKAN_HANDLE_POOL_CAST (cache),
166 error);
167 }
168
169 static void
gst_vulkan_descriptor_cache_init(GstVulkanDescriptorCache * cache)170 gst_vulkan_descriptor_cache_init (GstVulkanDescriptorCache * cache)
171 {
172 }
173
174 static void
gst_vulkan_descriptor_cache_class_init(GstVulkanDescriptorCacheClass * klass)175 gst_vulkan_descriptor_cache_class_init (GstVulkanDescriptorCacheClass * klass)
176 {
177 GObjectClass *gobject_class = (GObjectClass *) klass;
178 GstVulkanHandlePoolClass *handle_class = (GstVulkanHandlePoolClass *) klass;
179
180 gobject_class->finalize = gst_vulkan_descriptor_cache_finalize;
181
182 handle_class->acquire = gst_vulkan_descriptor_cache_acquire_impl;
183 handle_class->alloc = gst_vulkan_descriptor_cache_alloc_impl;
184 handle_class->release = gst_vulkan_descriptor_cache_release_impl;
185 handle_class->free = gst_vulkan_descriptor_cache_free_impl;
186 }
187