3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
25 #include "gstvkqueue.h"
29 * @title: GstVulkanQueue
30 * @short_description: Vulkan command queue
31 * @see_also: #GstVulkanDevice
33 * GstVulkanQueue encapsulates the vulkan command queue.
36 #define GST_CAT_DEFAULT gst_vulkan_queue_debug
37 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
43 static volatile gsize init;
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);
52 struct _GstVulkanQueuePrivate
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 ());
61 #define GET_PRIV(queue) gst_vulkan_queue_get_instance_private (queue)
63 static void gst_vulkan_queue_dispose (GObject * object);
66 gst_vulkan_queue_init (GstVulkanQueue * queue)
68 GstVulkanQueuePrivate *priv = GET_PRIV (queue);
70 g_mutex_init (&priv->submit_lock);
74 gst_vulkan_queue_class_init (GstVulkanQueueClass * queue_class)
76 GObjectClass *gobject_class = (GObjectClass *) queue_class;
78 gobject_class->dispose = gst_vulkan_queue_dispose;
82 gst_vulkan_queue_dispose (GObject * object)
84 GstVulkanQueue *queue = GST_VULKAN_QUEUE (object);
85 GstVulkanQueuePrivate *priv = GET_PRIV (queue);
88 gst_object_unref (queue->device);
91 g_mutex_clear (&priv->submit_lock);
93 G_OBJECT_CLASS (parent_class)->dispose (object);
97 * gst_vulkan_queue_get_device
98 * @queue: a #GstVulkanQueue
100 * Returns: (transfer full): the #GstVulkanDevice for @queue
105 gst_vulkan_queue_get_device (GstVulkanQueue * queue)
107 g_return_val_if_fail (GST_IS_VULKAN_QUEUE (queue), NULL);
109 return queue->device ? gst_object_ref (queue->device) : NULL;
113 * gst_vulkan_queue_create_command_pool:
114 * @queue: a #GstVulkanQueue
117 * Returns: (transfer full): a new #GstVUlkanCommandPool or %NULL
121 GstVulkanCommandPool *
122 gst_vulkan_queue_create_command_pool (GstVulkanQueue * queue, GError ** error)
124 GstVulkanCommandPool *pool;
125 VkCommandPoolCreateInfo cmd_pool_info = { 0, };
126 VkCommandPool vk_pool;
129 g_return_val_if_fail (GST_IS_VULKAN_QUEUE (queue), NULL);
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;
136 GST_OBJECT_LOCK (queue->device);
138 vkCreateCommandPool (queue->device->device, &cmd_pool_info, NULL,
140 if (gst_vulkan_error_to_g_error (err, error, "vkCreateCommandPool") < 0) {
141 GST_OBJECT_LOCK (queue->device);
144 GST_OBJECT_UNLOCK (queue->device);
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;
158 * gst_context_set_vulkan_queue:
159 * @context: a #GstContext
160 * @queue: a #GstVulkanQueue
162 * Sets @queue on @context
167 gst_context_set_vulkan_queue (GstContext * context, GstVulkanQueue * queue)
171 g_return_if_fail (context != NULL);
172 g_return_if_fail (gst_context_is_writable (context));
175 GST_CAT_LOG (GST_CAT_CONTEXT,
176 "setting GstVulkanQueue(%" GST_PTR_FORMAT ") on context(%"
177 GST_PTR_FORMAT ")", queue, context);
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);
185 * gst_context_get_vulkan_queue:
186 * @context: a #GstContext
187 * @queue: resulting #GstVulkanQueue
189 * Returns: Whether @queue was in @context
194 gst_context_get_vulkan_queue (GstContext * context, GstVulkanQueue ** queue)
196 const GstStructure *s;
199 g_return_val_if_fail (queue != NULL, FALSE);
200 g_return_val_if_fail (context != NULL, FALSE);
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);
206 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVulkanQueue(%" GST_PTR_FORMAT
207 ") from context(%" GST_PTR_FORMAT ")", *queue, context);
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
218 * If a #GstVulkanQueue is requested in @query, sets @queue as the reply.
220 * Intended for use with element query handlers to respond to #GST_QUERY_CONTEXT
221 * for a #GstVulkanQueue.
223 * Returns: whether @query was responded to with @queue
228 gst_vulkan_queue_handle_context_query (GstElement * element, GstQuery * query,
229 GstVulkanQueue * queue)
231 gboolean res = FALSE;
232 const gchar *context_type;
233 GstContext *context, *old_context;
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);
242 gst_query_parse_context_type (query, &context_type);
244 if (g_strcmp0 (context_type, GST_VULKAN_QUEUE_CONTEXT_TYPE_STR) == 0) {
245 gst_query_parse_context (query, &old_context);
248 context = gst_context_copy (old_context);
250 context = gst_context_new (GST_VULKAN_QUEUE_CONTEXT_TYPE_STR, TRUE);
252 gst_context_set_vulkan_queue (context, queue);
253 gst_query_set_context (query, context);
254 gst_context_unref (context);
263 * gst_vulkan_queue_run_context_query:
264 * @element: a #GstElement
265 * @queue: (inout): a #GstVulkanQueue
267 * Attempt to retrieve a #GstVulkanQueue using #GST_QUERY_CONTEXT from the
268 * surrounding elements of @element.
270 * Returns: whether @queue contains a valid #GstVulkanQueue
275 gst_vulkan_queue_run_context_query (GstElement * element,
276 GstVulkanQueue ** queue)
280 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
281 g_return_val_if_fail (queue != NULL, FALSE);
285 if (*queue && GST_IS_VULKAN_QUEUE (*queue))
289 gst_vulkan_local_context_query (element,
290 GST_VULKAN_QUEUE_CONTEXT_TYPE_STR))) {
293 gst_query_parse_context (query, &context);
295 gst_context_get_vulkan_queue (context, queue);
297 gst_query_unref (query);
300 GST_DEBUG_OBJECT (element, "found queue %p", *queue);
309 * gst_vulkan_queue_submit_lock:
310 * @queue: a #GstVulkanQueue
312 * Locks the queue for command submission using `vkQueueSubmit()` to meet the
313 * Vulkan requirements for externally synchronised resources.
318 gst_vulkan_queue_submit_lock (GstVulkanQueue * queue)
320 GstVulkanQueuePrivate *priv = GET_PRIV (queue);
322 g_mutex_lock (&priv->submit_lock);
326 * gst_vulkan_queue_submit_unlock:
327 * @queue: a #GstVulkanQueue
329 * Unlocks the queue for command submission using `vkQueueSubmit()`.
331 * See gst_vulkan_queue_submit_lock() for details on when this call is needed.
336 gst_vulkan_queue_submit_unlock (GstVulkanQueue * queue)
338 GstVulkanQueuePrivate *priv = GET_PRIV (queue);
340 g_mutex_unlock (&priv->submit_lock);