Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / gstvkcommandbuffer.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:vulkancommandbuffer
23  * @title: GstVulkanCommandBuffer
24  * @short_description: Vulkan command buffer
25  * @see_also: #GstVulkanCommandPool
26  *
27  * vulkancommandbuffer holds information about a command buffer.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstvkcommandbuffer.h"
35 #include "gstvkcommandpool.h"
36 #include "gstvkcommandpool-private.h"
37
38 #define GST_CAT_DEFAULT gst_debug_vulkan_command_buffer
39 GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
40
41 static void
42 init_debug (void)
43 {
44   static gsize _init = 0;
45
46   if (g_once_init_enter (&_init)) {
47     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkancommandbuffer", 0,
48         "Vulkan command buffer");
49     g_once_init_leave (&_init, 1);
50   }
51 }
52
53 static gboolean
54 gst_vulkan_command_buffer_dispose (GstVulkanCommandBuffer * cmd)
55 {
56   GstVulkanCommandPool *pool;
57
58   /* no pool, do free */
59   if ((pool = cmd->pool) == NULL)
60     return TRUE;
61
62   /* keep the buffer alive */
63   gst_vulkan_command_buffer_ref (cmd);
64   /* return the buffer to the pool */
65   gst_vulkan_command_pool_release_buffer (pool, cmd);
66
67   return FALSE;
68 }
69
70 static void
71 gst_vulkan_command_buffer_free (GstVulkanCommandBuffer * cmd)
72 {
73   g_assert (cmd->pool == NULL);
74
75   GST_TRACE ("Freeing %p", cmd);
76
77   g_free (cmd);
78 }
79
80 static void
81 gst_vulkan_command_buffer_init (GstVulkanCommandBuffer * cmd,
82     VkCommandBuffer cmd_buf, VkCommandBufferLevel level)
83 {
84   cmd->cmd = cmd_buf;
85   cmd->level = level;
86
87   init_debug ();
88
89   GST_TRACE ("new %p", cmd);
90
91   gst_mini_object_init (&cmd->parent, 0, GST_TYPE_VULKAN_COMMAND_BUFFER,
92       NULL, (GstMiniObjectDisposeFunction) gst_vulkan_command_buffer_dispose,
93       (GstMiniObjectFreeFunction) gst_vulkan_command_buffer_free);
94 }
95
96 /**
97  * gst_vulkan_command_buffer_new_wrapped:
98  * @cmd: a VkCommandBuffer
99  * @level: the VkCommandBufferLevel for @cmd
100  *
101  * Returns: (transfer full): a new #GstVulkanCommandBuffer
102  *
103  * Since: 1.18
104  */
105 GstVulkanCommandBuffer *
106 gst_vulkan_command_buffer_new_wrapped (VkCommandBuffer cmd,
107     VkCommandBufferLevel level)
108 {
109   GstVulkanCommandBuffer *ret;
110
111   ret = g_new0 (GstVulkanCommandBuffer, 1);
112   gst_vulkan_command_buffer_init (ret, cmd, level);
113
114   return ret;
115 }
116
117 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanCommandBuffer, gst_vulkan_command_buffer);