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