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:vkhandle
23 * @title: GstVulkanHandle
24 * @short_description: Vulkan handles
25 * @see_also: #GstVulkanHandlePool, #GstVulkanDevice
26 *
27 * #GstVulkanHandle holds information about a vulkan handle.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstvkhandle.h"
35 #include "gstvkdevice.h"
36
37 #define GST_CAT_DEFAULT gst_debug_vulkan_handle
38 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
39
40 static void
init_debug(void)41 init_debug (void)
42 {
43 static gsize _init = 0;
44
45 if (g_once_init_enter (&_init)) {
46 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanhandle", 0,
47 "Vulkan handle");
48 g_once_init_leave (&_init, 1);
49 }
50 }
51
52 static void
gst_vulkan_handle_free(GstVulkanHandle * handle)53 gst_vulkan_handle_free (GstVulkanHandle * handle)
54 {
55 GST_TRACE ("Freeing %p", handle);
56
57 if (handle->notify)
58 handle->notify (handle, handle->user_data);
59
60 gst_clear_object (&handle->device);
61
62 g_free (handle);
63 }
64
65 static void
gst_vulkan_handle_init(GstVulkanHandle * handle,GstVulkanDevice * device,GstVulkanHandleType type,GstVulkanHandleTypedef handle_val,GstVulkanHandleDestroyNotify notify,gpointer user_data)66 gst_vulkan_handle_init (GstVulkanHandle * handle, GstVulkanDevice * device,
67 GstVulkanHandleType type, GstVulkanHandleTypedef handle_val,
68 GstVulkanHandleDestroyNotify notify, gpointer user_data)
69 {
70 handle->device = gst_object_ref (device);
71 handle->type = type;
72 handle->handle = handle_val;
73 handle->notify = notify;
74 handle->user_data = user_data;
75
76 init_debug ();
77
78 GST_TRACE ("new %p", handle);
79
80 gst_mini_object_init (&handle->parent, 0, GST_TYPE_VULKAN_HANDLE, NULL, NULL,
81 (GstMiniObjectFreeFunction) gst_vulkan_handle_free);
82 }
83
84 /**
85 * gst_vulkan_handle_new_wrapped:
86 * @handle: a Vulkan handle
87 * @notify: (scope call): a #GDestroyNotify
88 * @user_data: data to pass to @notify
89 *
90 * Returns: (transfer full): a new #GstVulkanHandle wrapping @handle
91 *
92 * Since: 1.18
93 */
94 GstVulkanHandle *
gst_vulkan_handle_new_wrapped(GstVulkanDevice * device,GstVulkanHandleType type,GstVulkanHandleTypedef handle,GstVulkanHandleDestroyNotify notify,gpointer user_data)95 gst_vulkan_handle_new_wrapped (GstVulkanDevice * device,
96 GstVulkanHandleType type, GstVulkanHandleTypedef handle,
97 GstVulkanHandleDestroyNotify notify, gpointer user_data)
98 {
99 GstVulkanHandle *ret;
100
101 ret = g_new0 (GstVulkanHandle, 1);
102 gst_vulkan_handle_init (ret, device, type, handle, notify, user_data);
103
104 return ret;
105 }
106
107 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanHandle, gst_vulkan_handle);
108
109 /**
110 * gst_vulkan_handle_free_descriptor_set_layout:
111 * @handle: a #GstVulkanHandle containing a vulkan `VkDescriptorSetLayout`
112 * @user_data: callback user data
113 *
114 * Frees the descriptor set layout in @handle
115 *
116 * Since: 1.18
117 */
118 void
gst_vulkan_handle_free_descriptor_set_layout(GstVulkanHandle * handle,gpointer user_data)119 gst_vulkan_handle_free_descriptor_set_layout (GstVulkanHandle * handle,
120 gpointer user_data)
121 {
122 g_return_if_fail (handle != NULL);
123 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
124 g_return_if_fail (handle->type ==
125 GST_VULKAN_HANDLE_TYPE_DESCRIPTOR_SET_LAYOUT);
126
127 vkDestroyDescriptorSetLayout (handle->device->device,
128 (VkDescriptorSetLayout) handle->handle, NULL);
129 }
130
131 /**
132 * gst_vulkan_handle_free_pipeline:
133 * @handle: a #GstVulkanHandle containing a vulkan `VkPipeline`
134 * @user_data: callback user data
135 *
136 * Frees the pipeline in @handle
137 *
138 * Since: 1.18
139 */
140 void
gst_vulkan_handle_free_pipeline(GstVulkanHandle * handle,gpointer user_data)141 gst_vulkan_handle_free_pipeline (GstVulkanHandle * handle, gpointer user_data)
142 {
143 g_return_if_fail (handle != NULL);
144 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
145 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_PIPELINE);
146
147 vkDestroyPipeline (handle->device->device, (VkPipeline) handle->handle, NULL);
148 }
149
150 /**
151 * gst_vulkan_handle_free_pipeline_layout:
152 * @handle: a #GstVulkanHandle containing a vulkan `VkPipelineLayout`
153 * @user_data: callback user data
154 *
155 * Frees the pipeline layout in @handle
156 *
157 * Since: 1.18
158 */
159 void
gst_vulkan_handle_free_pipeline_layout(GstVulkanHandle * handle,gpointer user_data)160 gst_vulkan_handle_free_pipeline_layout (GstVulkanHandle * handle,
161 gpointer user_data)
162 {
163 g_return_if_fail (handle != NULL);
164 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
165 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_PIPELINE_LAYOUT);
166
167 vkDestroyPipelineLayout (handle->device->device,
168 (VkPipelineLayout) handle->handle, NULL);
169 }
170
171 /**
172 * gst_vulkan_handle_free_render_pass:
173 * @handle: a #GstVulkanHandle containing a vulkan `VkRenderPass`
174 * @user_data: callback user data
175 *
176 * Frees the render pass in @handle
177 *
178 * Since: 1.18
179 */
180 void
gst_vulkan_handle_free_render_pass(GstVulkanHandle * handle,gpointer user_data)181 gst_vulkan_handle_free_render_pass (GstVulkanHandle * handle,
182 gpointer user_data)
183 {
184 g_return_if_fail (handle != NULL);
185 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
186 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_RENDER_PASS);
187
188 vkDestroyRenderPass (handle->device->device,
189 (VkRenderPass) handle->handle, NULL);
190 }
191
192 /**
193 * gst_vulkan_handle_free_sampler:
194 * @handle: a #GstVulkanHandle containing a vulkan `VkSampler`
195 * @user_data: callback user data
196 *
197 * Frees the sampler in @handle
198 *
199 * Since: 1.18
200 */
201 void
gst_vulkan_handle_free_sampler(GstVulkanHandle * handle,gpointer user_data)202 gst_vulkan_handle_free_sampler (GstVulkanHandle * handle, gpointer user_data)
203 {
204 g_return_if_fail (handle != NULL);
205 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
206 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_SAMPLER);
207
208 vkDestroySampler (handle->device->device, (VkSampler) handle->handle, NULL);
209 }
210
211 /**
212 * gst_vulkan_handle_free_framebuffer:
213 * @handle: a #GstVulkanHandle containing a vulkan `VkFramebuffer`
214 * @user_data: callback user data
215 *
216 * Frees the framebuffer in @handle
217 *
218 * Since: 1.18
219 */
220 void
gst_vulkan_handle_free_framebuffer(GstVulkanHandle * handle,gpointer user_data)221 gst_vulkan_handle_free_framebuffer (GstVulkanHandle * handle,
222 gpointer user_data)
223 {
224 g_return_if_fail (handle != NULL);
225 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
226 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_FRAMEBUFFER);
227
228 vkDestroyFramebuffer (handle->device->device, (VkFramebuffer) handle->handle,
229 NULL);
230 }
231
232 /**
233 * gst_vulkan_handle_free_shader:
234 * @handle: a #GstVulkanHandle containing a vulkan `VkFramebuffer`
235 * @user_data: callback user data
236 *
237 * Frees the shader in @handle
238 *
239 * Since: 1.18
240 */
241 void
gst_vulkan_handle_free_shader(GstVulkanHandle * handle,gpointer user_data)242 gst_vulkan_handle_free_shader (GstVulkanHandle * handle, gpointer user_data)
243 {
244 g_return_if_fail (handle != NULL);
245 g_return_if_fail (handle->handle != VK_NULL_HANDLE);
246 g_return_if_fail (handle->type == GST_VULKAN_HANDLE_TYPE_SHADER);
247
248 vkDestroyShaderModule (handle->device->device,
249 (VkShaderModule) handle->handle, NULL);
250 }
251