0763b83d6e69accd7e9ba5afb23be6dda77768ce
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / gstvkqueue.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 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 "gstvkqueue.h"
26
27 /**
28  * SECTION:vkqueue
29  * @title: GstVulkanQueue
30  * @short_description: Vulkan command queue
31  * @see_also: #GstVulkanDevice
32  *
33  * GstVulkanQueue encapsulates the vulkan command queue.
34  */
35
36 #define GST_CAT_DEFAULT gst_vulkan_queue_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
39
40 static void
41 _init_debug (void)
42 {
43   static volatile gsize init;
44
45   if (g_once_init_enter (&init)) {
46     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanqueue", 0, "Vulkan Queue");
47     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
48     g_once_init_leave (&init, 1);
49   }
50 }
51
52 struct _GstVulkanQueuePrivate
53 {
54   GMutex submit_lock;
55 };
56
57 #define parent_class gst_vulkan_queue_parent_class
58 G_DEFINE_TYPE_WITH_CODE (GstVulkanQueue, gst_vulkan_queue, GST_TYPE_OBJECT,
59     G_ADD_PRIVATE (GstVulkanQueue); _init_debug ());
60
61 #define GET_PRIV(queue) gst_vulkan_queue_get_instance_private (queue)
62
63 static void gst_vulkan_queue_dispose (GObject * object);
64
65 static void
66 gst_vulkan_queue_init (GstVulkanQueue * queue)
67 {
68   GstVulkanQueuePrivate *priv = GET_PRIV (queue);
69
70   g_mutex_init (&priv->submit_lock);
71 }
72
73 static void
74 gst_vulkan_queue_class_init (GstVulkanQueueClass * queue_class)
75 {
76   GObjectClass *gobject_class = (GObjectClass *) queue_class;
77
78   gobject_class->dispose = gst_vulkan_queue_dispose;
79 }
80
81 static void
82 gst_vulkan_queue_dispose (GObject * object)
83 {
84   GstVulkanQueue *queue = GST_VULKAN_QUEUE (object);
85   GstVulkanQueuePrivate *priv = GET_PRIV (queue);
86
87   if (queue->device)
88     gst_object_unref (queue->device);
89   queue->device = NULL;
90
91   g_mutex_clear (&priv->submit_lock);
92
93   G_OBJECT_CLASS (parent_class)->dispose (object);
94 }
95
96 /**
97  * gst_vulkan_queue_get_device
98  * @queue: a #GstVulkanQueue
99  *
100  * Returns: (transfer full): the #GstVulkanDevice for @queue
101  *
102  * Since: 1.18
103  */
104 GstVulkanDevice *
105 gst_vulkan_queue_get_device (GstVulkanQueue * queue)
106 {
107   g_return_val_if_fail (GST_IS_VULKAN_QUEUE (queue), NULL);
108
109   return queue->device ? gst_object_ref (queue->device) : NULL;
110 }
111
112 /**
113  * gst_vulkan_queue_create_command_pool:
114  * @queue: a #GstVulkanQueue
115  * @error: a #GError
116  *
117  * Returns: (transfer full): a new #GstVUlkanCommandPool or %NULL
118  *
119  * Since: 1.18
120  */
121 GstVulkanCommandPool *
122 gst_vulkan_queue_create_command_pool (GstVulkanQueue * queue, GError ** error)
123 {
124   GstVulkanCommandPool *pool;
125   VkCommandPoolCreateInfo cmd_pool_info = { 0, };
126   VkCommandPool vk_pool;
127   VkResult err;
128
129   g_return_val_if_fail (GST_IS_VULKAN_QUEUE (queue), NULL);
130
131   cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
132   cmd_pool_info.pNext = NULL;
133   cmd_pool_info.queueFamilyIndex = queue->family;
134   cmd_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
135
136   GST_OBJECT_LOCK (queue->device);
137   err =
138       vkCreateCommandPool (queue->device->device, &cmd_pool_info, NULL,
139       &vk_pool);
140   if (gst_vulkan_error_to_g_error (err, error, "vkCreateCommandPool") < 0) {
141     GST_OBJECT_LOCK (queue->device);
142     goto error;
143   }
144   GST_OBJECT_UNLOCK (queue->device);
145
146   pool = g_object_new (GST_TYPE_VULKAN_COMMAND_POOL, NULL);
147   gst_object_ref_sink (pool);
148   pool->queue = gst_object_ref (queue);
149   pool->pool = vk_pool;
150
151   return pool;
152
153 error:
154   return NULL;
155 }
156
157 /**
158  * gst_context_set_vulkan_queue:
159  * @context: a #GstContext
160  * @queue: a #GstVulkanQueue
161  *
162  * Sets @queue on @context
163  *
164  * Since: 1.18
165  */
166 void
167 gst_context_set_vulkan_queue (GstContext * context, GstVulkanQueue * queue)
168 {
169   GstStructure *s;
170
171   g_return_if_fail (context != NULL);
172   g_return_if_fail (gst_context_is_writable (context));
173
174   if (queue)
175     GST_CAT_LOG (GST_CAT_CONTEXT,
176         "setting GstVulkanQueue(%" GST_PTR_FORMAT ") on context(%"
177         GST_PTR_FORMAT ")", queue, context);
178
179   s = gst_context_writable_structure (context);
180   gst_structure_set (s, GST_VULKAN_QUEUE_CONTEXT_TYPE_STR,
181       GST_TYPE_VULKAN_QUEUE, queue, NULL);
182 }
183
184 /**
185  * gst_context_get_vulkan_queue:
186  * @context: a #GstContext
187  * @queue: resulting #GstVulkanQueue
188  *
189  * Returns: Whether @queue was in @context
190  *
191  * Since: 1.18
192  */
193 gboolean
194 gst_context_get_vulkan_queue (GstContext * context, GstVulkanQueue ** queue)
195 {
196   const GstStructure *s;
197   gboolean ret;
198
199   g_return_val_if_fail (queue != NULL, FALSE);
200   g_return_val_if_fail (context != NULL, FALSE);
201
202   s = gst_context_get_structure (context);
203   ret = gst_structure_get (s, GST_VULKAN_QUEUE_CONTEXT_TYPE_STR,
204       GST_TYPE_VULKAN_QUEUE, queue, NULL);
205
206   GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanQueue(%" GST_PTR_FORMAT
207       ") from context(%" GST_PTR_FORMAT ")", *queue, context);
208
209   return ret;
210 }
211
212 /**
213  * gst_vulkan_queue_handle_context_query:
214  * @element: a #GstElement
215  * @query: a #GstQuery of type #GST_QUERY_CONTEXT
216  * @queue: (nullable): the #GstVulkanQueue
217  *
218  * If a #GstVulkanQueue is requested in @query, sets @queue as the reply.
219  *
220  * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
221  * for a #GstVulkanQueue.
222  *
223  * Returns: whether @query was responded to with @queue
224  *
225  * Since: 1.18
226  */
227 gboolean
228 gst_vulkan_queue_handle_context_query (GstElement * element, GstQuery * query,
229     GstVulkanQueue * queue)
230 {
231   gboolean res = FALSE;
232   const gchar *context_type;
233   GstContext *context, *old_context;
234
235   g_return_val_if_fail (element != NULL, FALSE);
236   g_return_val_if_fail (query != NULL, FALSE);
237   g_return_val_if_fail (GST_QUERY_TYPE (query) == GST_QUERY_CONTEXT, FALSE);
238
239   if (!queue)
240     return FALSE;
241
242   gst_query_parse_context_type (query, &context_type);
243
244   if (g_strcmp0 (context_type, GST_VULKAN_QUEUE_CONTEXT_TYPE_STR) == 0) {
245     gst_query_parse_context (query, &old_context);
246
247     if (old_context)
248       context = gst_context_copy (old_context);
249     else
250       context = gst_context_new (GST_VULKAN_QUEUE_CONTEXT_TYPE_STR, TRUE);
251
252     gst_context_set_vulkan_queue (context, queue);
253     gst_query_set_context (query, context);
254     gst_context_unref (context);
255
256     res = queue != NULL;
257   }
258
259   return res;
260 }
261
262 /**
263  * gst_vulkan_queue_run_context_query:
264  * @element: a #GstElement
265  * @queue: (inout): a #GstVulkanQueue
266  *
267  * Attempt to retrieve a #GstVulkanQueue using #GST_QUERY_CONTEXT from the
268  * surrounding elements of @element.
269  *
270  * Returns: whether @queue contains a valid #GstVulkanQueue
271  *
272  * Since: 1.18
273  */
274 gboolean
275 gst_vulkan_queue_run_context_query (GstElement * element,
276     GstVulkanQueue ** queue)
277 {
278   GstQuery *query;
279
280   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
281   g_return_val_if_fail (queue != NULL, FALSE);
282
283   _init_debug ();
284
285   if (*queue && GST_IS_VULKAN_QUEUE (*queue))
286     return TRUE;
287
288   if ((query =
289           gst_vulkan_local_context_query (element,
290               GST_VULKAN_QUEUE_CONTEXT_TYPE_STR))) {
291     GstContext *context;
292
293     gst_query_parse_context (query, &context);
294     if (context)
295       gst_context_get_vulkan_queue (context, queue);
296
297     gst_query_unref (query);
298   }
299
300   GST_DEBUG_OBJECT (element, "found queue %p", *queue);
301
302   if (*queue)
303     return TRUE;
304
305   return FALSE;
306 }
307
308 /**
309  * gst_vulkan_queue_submit_lock:
310  * @queue: a #GstVulkanQueue
311  *
312  * Locks the queue for command submission using `vkQueueSubmit()` to meet the
313  * Vulkan requirements for externally synchronised resources.
314  *
315  * Since: 1.18
316  */
317 void
318 gst_vulkan_queue_submit_lock (GstVulkanQueue * queue)
319 {
320   GstVulkanQueuePrivate *priv = GET_PRIV (queue);
321
322   g_mutex_lock (&priv->submit_lock);
323 }
324
325 /**
326  * gst_vulkan_queue_submit_unlock:
327  * @queue: a #GstVulkanQueue
328  *
329  * Unlocks the queue for command submission using `vkQueueSubmit()`.
330  *
331  * See gst_vulkan_queue_submit_lock() for details on when this call is needed.
332  *
333  * Since: 1.18
334  */
335 void
336 gst_vulkan_queue_submit_unlock (GstVulkanQueue * queue)
337 {
338   GstVulkanQueuePrivate *priv = GET_PRIV (queue);
339
340   g_mutex_unlock (&priv->submit_lock);
341 }