1 /*
2 * GStreamer
3 * Copyright (C) 2016 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 "vkbufferpool.h"
26
27 /**
28 * SECTION:vkbufferpool
29 * @title: GstVulkanBufferPool
30 * @short_description: buffer pool for #GstVulkanBufferMemory objects
31 * @see_also: #GstBufferPool, #GstVulkanBufferMemory
32 *
33 * a #GstVulkanBufferPool is an object that allocates buffers with #GstVulkanBufferMemory
34 *
35 * A #GstVulkanBufferPool is created with gst_vulkan_buffer_pool_new()
36 *
37 * #GstVulkanBufferPool implements the VideoMeta buffer pool option
38 * #GST_BUFFER_POOL_OPTION_VIDEO_META
39 */
40
41 /* bufferpool */
42 struct _GstVulkanBufferPoolPrivate
43 {
44 GstCaps *caps;
45 GstVideoInfo v_info;
46 gboolean add_videometa;
47 gsize alloc_sizes[GST_VIDEO_MAX_PLANES];
48 };
49
50 static void gst_vulkan_buffer_pool_finalize (GObject * object);
51
52 GST_DEBUG_CATEGORY_STATIC (GST_CAT_VULKAN_BUFFER_POOL);
53 #define GST_CAT_DEFAULT GST_CAT_VULKAN_BUFFER_POOL
54
55 #define gst_vulkan_buffer_pool_parent_class parent_class
56 G_DEFINE_TYPE_WITH_CODE (GstVulkanBufferPool, gst_vulkan_buffer_pool,
57 GST_TYPE_BUFFER_POOL, G_ADD_PRIVATE (GstVulkanBufferPool)
58 GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_BUFFER_POOL,
59 "vulkanbufferpool", 0, "Vulkan Buffer Pool"));
60
61 static const gchar **
gst_vulkan_buffer_pool_get_options(GstBufferPool * pool)62 gst_vulkan_buffer_pool_get_options (GstBufferPool * pool)
63 {
64 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
65 NULL
66 };
67
68 return options;
69 }
70
71 static gboolean
gst_vulkan_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)72 gst_vulkan_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
73 {
74 GstVulkanBufferPool *vk_pool = GST_VULKAN_BUFFER_POOL_CAST (pool);
75 GstVulkanBufferPoolPrivate *priv = vk_pool->priv;
76 guint min_buffers, max_buffers;
77 GstCaps *caps = NULL;
78 gboolean ret = TRUE;
79 guint i;
80
81 if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
82 &max_buffers))
83 goto wrong_config;
84
85 if (caps == NULL)
86 goto no_caps;
87
88 /* now parse the caps from the config */
89 if (!gst_video_info_from_caps (&priv->v_info, caps))
90 goto wrong_caps;
91
92 GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, priv->v_info.width,
93 priv->v_info.height, caps);
94
95 gst_caps_replace (&priv->caps, caps);
96
97 /* get the size of the buffer to allocate */
98 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
99 GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&priv->v_info);
100 GstVulkanImageMemory *img_mem;
101 guint width, height;
102 VkFormat vk_format;
103
104 vk_format = gst_vulkan_format_from_video_format (v_format, i);
105 width = GST_VIDEO_INFO_WIDTH (&priv->v_info);
106 height = GST_VIDEO_INFO_HEIGHT (&priv->v_info);
107
108 img_mem = (GstVulkanImageMemory *)
109 gst_vulkan_image_memory_alloc (vk_pool->device, vk_format, width,
110 height, VK_IMAGE_TILING_OPTIMAL,
111 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
112 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
113
114 priv->alloc_sizes[i] = img_mem->requirements.size;
115
116 gst_memory_unref (GST_MEMORY_CAST (img_mem));
117 }
118
119 priv->add_videometa = gst_buffer_pool_config_has_option (config,
120 GST_BUFFER_POOL_OPTION_VIDEO_META);
121
122 gst_buffer_pool_config_set_params (config, caps,
123 priv->v_info.size, min_buffers, max_buffers);
124
125 return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
126
127 /* ERRORS */
128 wrong_config:
129 {
130 GST_WARNING_OBJECT (pool, "invalid config");
131 return FALSE;
132 }
133 no_caps:
134 {
135 GST_WARNING_OBJECT (pool, "no caps in config");
136 return FALSE;
137 }
138 wrong_caps:
139 {
140 GST_WARNING_OBJECT (pool,
141 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
142 return FALSE;
143 }
144 }
145
146 /* This function handles GstBuffer creation */
147 static GstFlowReturn
gst_vulkan_buffer_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)148 gst_vulkan_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
149 GstBufferPoolAcquireParams * params)
150 {
151 GstVulkanBufferPool *vk_pool = GST_VULKAN_BUFFER_POOL_CAST (pool);
152 GstVulkanBufferPoolPrivate *priv = vk_pool->priv;
153 GstBuffer *buf;
154 guint i;
155
156 if (!(buf = gst_buffer_new ())) {
157 goto no_buffer;
158 }
159
160 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
161 GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&priv->v_info);
162 VkFormat vk_format;
163 GstMemory *mem;
164
165 vk_format = gst_vulkan_format_from_video_format (v_format, i);
166
167 mem = gst_vulkan_buffer_memory_alloc (vk_pool->device,
168 vk_format, priv->alloc_sizes[i],
169 VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
170 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
171 if (!mem) {
172 gst_buffer_unref (buf);
173 goto mem_create_failed;
174 }
175
176 gst_buffer_append_memory (buf, mem);
177 }
178
179 *buffer = buf;
180
181 return GST_FLOW_OK;
182
183 /* ERROR */
184 no_buffer:
185 {
186 GST_WARNING_OBJECT (pool, "can't create image");
187 return GST_FLOW_ERROR;
188 }
189 mem_create_failed:
190 {
191 GST_WARNING_OBJECT (pool, "Could not create Vulkan Memory");
192 return GST_FLOW_ERROR;
193 }
194 }
195
196 /**
197 * gst_vulkan_buffer_pool_new:
198 * @context: the #GstGLContext to use
199 *
200 * Returns: a #GstBufferPool that allocates buffers with #GstGLMemory
201 */
202 GstBufferPool *
gst_vulkan_buffer_pool_new(GstVulkanDevice * device)203 gst_vulkan_buffer_pool_new (GstVulkanDevice * device)
204 {
205 GstVulkanBufferPool *pool;
206
207 pool = g_object_new (GST_TYPE_VULKAN_BUFFER_POOL, NULL);
208 g_object_ref_sink (pool);
209 pool->device = gst_object_ref (device);
210
211 GST_LOG_OBJECT (pool, "new Vulkan buffer pool for device %" GST_PTR_FORMAT,
212 device);
213
214 return GST_BUFFER_POOL_CAST (pool);
215 }
216
217 static void
gst_vulkan_buffer_pool_class_init(GstVulkanBufferPoolClass * klass)218 gst_vulkan_buffer_pool_class_init (GstVulkanBufferPoolClass * klass)
219 {
220 GObjectClass *gobject_class = (GObjectClass *) klass;
221 GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
222
223 gobject_class->finalize = gst_vulkan_buffer_pool_finalize;
224
225 gstbufferpool_class->get_options = gst_vulkan_buffer_pool_get_options;
226 gstbufferpool_class->set_config = gst_vulkan_buffer_pool_set_config;
227 gstbufferpool_class->alloc_buffer = gst_vulkan_buffer_pool_alloc;
228 }
229
230 static void
gst_vulkan_buffer_pool_init(GstVulkanBufferPool * pool)231 gst_vulkan_buffer_pool_init (GstVulkanBufferPool * pool)
232 {
233 pool->priv = gst_vulkan_buffer_pool_get_instance_private (pool);
234 }
235
236 static void
gst_vulkan_buffer_pool_finalize(GObject * object)237 gst_vulkan_buffer_pool_finalize (GObject * object)
238 {
239 GstVulkanBufferPool *pool = GST_VULKAN_BUFFER_POOL_CAST (object);
240 GstVulkanBufferPoolPrivate *priv = pool->priv;
241
242 GST_LOG_OBJECT (pool, "finalize Vulkan buffer pool %p", pool);
243
244 if (priv->caps)
245 gst_caps_unref (priv->caps);
246
247 G_OBJECT_CLASS (gst_vulkan_buffer_pool_parent_class)->finalize (object);
248
249 /* only release the context once all our memory have been deleted */
250 if (pool->device) {
251 gst_object_unref (pool->device);
252 pool->device = NULL;
253 }
254 }
255