• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstvkimagebufferpool.h"
26 
27 /**
28  * SECTION:vkimagebufferpool
29  * @title: GstVulkanImageBufferPool
30  * @short_description: buffer pool for #GstVulkanImageMemory objects
31  * @see_also: #GstBufferPool, #GstVulkanImageMemory
32  *
33  * a #GstVulkanImageBufferPool is an object that allocates buffers with #GstVulkanImageMemory
34  *
35  * A #GstVulkanImageBufferPool is created with gst_vulkan_image_buffer_pool_new()
36  *
37  * #GstVulkanImageBufferPool implements the VideoMeta buffer pool option
38  * #GST_BUFFER_POOL_OPTION_VIDEO_META
39  */
40 
41 /* bufferpool */
42 struct _GstVulkanImageBufferPoolPrivate
43 {
44   GstCaps *caps;
45   gboolean raw_caps;
46   GstVideoInfo v_info;
47   gboolean add_videometa;
48 };
49 
50 static void gst_vulkan_image_buffer_pool_finalize (GObject * object);
51 
52 GST_DEBUG_CATEGORY_STATIC (GST_CAT_VULKAN_IMAGE_BUFFER_POOL);
53 #define GST_CAT_DEFAULT GST_CAT_VULKAN_IMAGE_BUFFER_POOL
54 
55 #define GET_PRIV(pool) gst_vulkan_image_buffer_pool_get_instance_private (pool)
56 
57 #define gst_vulkan_image_buffer_pool_parent_class parent_class
58 G_DEFINE_TYPE_WITH_CODE (GstVulkanImageBufferPool, gst_vulkan_image_buffer_pool,
59     GST_TYPE_BUFFER_POOL, G_ADD_PRIVATE (GstVulkanImageBufferPool)
60     GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_BUFFER_POOL,
61         "vulkanimagebufferpool", 0, "Vulkan Image Buffer Pool"));
62 
63 static const gchar **
gst_vulkan_image_buffer_pool_get_options(GstBufferPool * pool)64 gst_vulkan_image_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_image_buffer_pool_set_config(GstBufferPool * pool,GstStructure * config)74 gst_vulkan_image_buffer_pool_set_config (GstBufferPool * pool,
75     GstStructure * config)
76 {
77   GstVulkanImageBufferPool *vk_pool = GST_VULKAN_IMAGE_BUFFER_POOL_CAST (pool);
78   GstVulkanImageBufferPoolPrivate *priv = GET_PRIV (vk_pool);
79   guint min_buffers, max_buffers;
80   GstCaps *caps = NULL;
81   GstCapsFeatures *features;
82   gboolean ret = TRUE;
83   guint i;
84 
85   if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
86           &max_buffers))
87     goto wrong_config;
88 
89   if (caps == NULL)
90     goto no_caps;
91 
92   /* now parse the caps from the config */
93   if (!gst_video_info_from_caps (&priv->v_info, caps))
94     goto wrong_caps;
95 
96   GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, priv->v_info.width,
97       priv->v_info.height, caps);
98 
99   gst_caps_replace (&priv->caps, caps);
100 
101   features = gst_caps_get_features (caps, 0);
102   priv->raw_caps = features == NULL || gst_caps_features_is_equal (features,
103       GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY);
104 
105   /* get the size of the buffer to allocate */
106   priv->v_info.size = 0;
107   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
108     GstVulkanImageMemory *img_mem;
109     VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
110     guint width, height;
111     VkFormat vk_format;
112 
113     vk_format = gst_vulkan_format_from_video_info (&priv->v_info, i);
114     width = GST_VIDEO_INFO_COMP_WIDTH (&priv->v_info, i);
115     height = GST_VIDEO_INFO_COMP_HEIGHT (&priv->v_info, i);
116     if (priv->raw_caps)
117       tiling = VK_IMAGE_TILING_LINEAR;
118 
119     img_mem = (GstVulkanImageMemory *)
120         gst_vulkan_image_memory_alloc (vk_pool->device, vk_format, width,
121         height, tiling,
122         VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
123         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
124         VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
125         VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
126 
127     priv->v_info.offset[i] = priv->v_info.size;
128     priv->v_info.size += img_mem->requirements.size;
129 
130     gst_memory_unref (GST_MEMORY_CAST (img_mem));
131   }
132 
133   priv->add_videometa = gst_buffer_pool_config_has_option (config,
134       GST_BUFFER_POOL_OPTION_VIDEO_META);
135 
136   gst_buffer_pool_config_set_params (config, caps,
137       priv->v_info.size, min_buffers, max_buffers);
138 
139   return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
140 
141   /* ERRORS */
142 wrong_config:
143   {
144     GST_WARNING_OBJECT (pool, "invalid config");
145     return FALSE;
146   }
147 no_caps:
148   {
149     GST_WARNING_OBJECT (pool, "no caps in config");
150     return FALSE;
151   }
152 wrong_caps:
153   {
154     GST_WARNING_OBJECT (pool,
155         "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
156     return FALSE;
157   }
158 }
159 
160 /* This function handles GstBuffer creation */
161 static GstFlowReturn
gst_vulkan_image_buffer_pool_alloc(GstBufferPool * pool,GstBuffer ** buffer,GstBufferPoolAcquireParams * params)162 gst_vulkan_image_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
163     GstBufferPoolAcquireParams * params)
164 {
165   GstVulkanImageBufferPool *vk_pool = GST_VULKAN_IMAGE_BUFFER_POOL_CAST (pool);
166   GstVulkanImageBufferPoolPrivate *priv = GET_PRIV (vk_pool);
167   GstBuffer *buf;
168   guint i;
169 
170   if (!(buf = gst_buffer_new ())) {
171     goto no_buffer;
172   }
173 
174   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
175     VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
176     VkFormat vk_format;
177     GstMemory *mem;
178 
179     vk_format = gst_vulkan_format_from_video_info (&priv->v_info, i);
180     if (priv->raw_caps)
181       tiling = VK_IMAGE_TILING_LINEAR;
182 
183     mem = gst_vulkan_image_memory_alloc (vk_pool->device,
184         vk_format, GST_VIDEO_INFO_COMP_WIDTH (&priv->v_info, i),
185         GST_VIDEO_INFO_COMP_HEIGHT (&priv->v_info, i), tiling,
186         /* FIXME: choose from outside */
187         VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
188         VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT |
189         VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
190         /* FIXME: choose from outside */
191         VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
192     if (!mem) {
193       gst_buffer_unref (buf);
194       goto mem_create_failed;
195     }
196 
197     gst_buffer_append_memory (buf, mem);
198   }
199 
200   *buffer = buf;
201 
202   return GST_FLOW_OK;
203 
204   /* ERROR */
205 no_buffer:
206   {
207     GST_WARNING_OBJECT (pool, "can't create image");
208     return GST_FLOW_ERROR;
209   }
210 mem_create_failed:
211   {
212     GST_WARNING_OBJECT (pool, "Could not create Vulkan Memory");
213     return GST_FLOW_ERROR;
214   }
215 }
216 
217 /**
218  * gst_vulkan_image_buffer_pool_new:
219  * @device: the #GstVulkanDevice to use
220  *
221  * Returns: (transfer full): a #GstBufferPool that allocates buffers with #GstGLMemory
222  *
223  * Since: 1.18
224  */
225 GstBufferPool *
gst_vulkan_image_buffer_pool_new(GstVulkanDevice * device)226 gst_vulkan_image_buffer_pool_new (GstVulkanDevice * device)
227 {
228   GstVulkanImageBufferPool *pool;
229 
230   pool = g_object_new (GST_TYPE_VULKAN_IMAGE_BUFFER_POOL, NULL);
231   g_object_ref_sink (pool);
232   pool->device = gst_object_ref (device);
233 
234   GST_LOG_OBJECT (pool, "new Vulkan buffer pool for device %" GST_PTR_FORMAT,
235       device);
236 
237   return GST_BUFFER_POOL_CAST (pool);
238 }
239 
240 static void
gst_vulkan_image_buffer_pool_class_init(GstVulkanImageBufferPoolClass * klass)241 gst_vulkan_image_buffer_pool_class_init (GstVulkanImageBufferPoolClass * klass)
242 {
243   GObjectClass *gobject_class = (GObjectClass *) klass;
244   GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
245 
246   gobject_class->finalize = gst_vulkan_image_buffer_pool_finalize;
247 
248   gstbufferpool_class->get_options = gst_vulkan_image_buffer_pool_get_options;
249   gstbufferpool_class->set_config = gst_vulkan_image_buffer_pool_set_config;
250   gstbufferpool_class->alloc_buffer = gst_vulkan_image_buffer_pool_alloc;
251 }
252 
253 static void
gst_vulkan_image_buffer_pool_init(GstVulkanImageBufferPool * pool)254 gst_vulkan_image_buffer_pool_init (GstVulkanImageBufferPool * pool)
255 {
256 }
257 
258 static void
gst_vulkan_image_buffer_pool_finalize(GObject * object)259 gst_vulkan_image_buffer_pool_finalize (GObject * object)
260 {
261   GstVulkanImageBufferPool *pool = GST_VULKAN_IMAGE_BUFFER_POOL_CAST (object);
262   GstVulkanImageBufferPoolPrivate *priv = GET_PRIV (pool);
263 
264   GST_LOG_OBJECT (pool, "finalize Vulkan buffer pool %p", pool);
265 
266   if (priv->caps)
267     gst_caps_unref (priv->caps);
268 
269   G_OBJECT_CLASS (gst_vulkan_image_buffer_pool_parent_class)->finalize (object);
270 
271   /* only release the context once all our memory have been deleted */
272   if (pool->device) {
273     gst_object_unref (pool->device);
274     pool->device = NULL;
275   }
276 }
277