vulkan: add a vulkanbuffer bufferpool

This commit is contained in:
Matthew Waters 2016-02-09 00:07:38 +11:00
parent 86e18d6b8f
commit 0dd1154b73
6 changed files with 424 additions and 25 deletions

View file

@ -11,6 +11,7 @@ libgstvulkan_la_SOURCES = \
vkerror.c \
vkbuffermemory.c \
vkimagememory.c \
vkbufferpool.c \
vkinstance.c \
vkmemory.c \
vkqueue.c \
@ -28,6 +29,7 @@ noinst_HEADERS = \
vkerror.h \
vkbuffermemory.h \
vkimagememory.h \
vkbufferpool.h \
vkinstance.h \
vkmemory.h \
vkqueue.h \

View file

@ -35,6 +35,7 @@
#include "vkmemory.h"
#include "vkbuffermemory.h"
#include "vkimagememory.h"
#include "vkbufferpool.h"
#include "vkutils.h"
#endif /* _VK_H_ */

View file

@ -62,6 +62,10 @@ typedef struct _GstVulkanImageMemory GstVulkanImageMemory;
typedef struct _GstVulkanImageMemoryAllocator GstVulkanImageMemoryAllocator;
typedef struct _GstVulkanImageMemoryAllocatorClass GstVulkanImageMemoryAllocatorClass;
typedef struct _GstVulkanBufferPool GstVulkanBufferPool;
typedef struct _GstVulkanBufferPoolClass GstVulkanBufferPoolClass;
typedef struct _GstVulkanBufferPoolPrivate GstVulkanBufferPoolPrivate;
G_END_DECLS
#endif /* _VK_FWD_H_ */

256
ext/vulkan/vkbufferpool.c Normal file
View file

@ -0,0 +1,256 @@
/*
* GStreamer
* Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "vkbufferpool.h"
/**
* SECTION:vkbufferpool
* @short_description: buffer pool for #GstVulkanBufferMemory objects
* @see_also: #GstBufferPool, #GstVulkanBufferMemory
*
* a #GstVulkanBufferPool is an object that allocates buffers with #GstVulkanBufferMemory
*
* A #GstVulkanBufferPool is created with gst_vulkan_buffer_pool_new()
*
* #GstVulkanBufferPool implements the VideoMeta buffer pool option
* #GST_BUFFER_POOL_OPTION_VIDEO_META
*/
/* bufferpool */
struct _GstVulkanBufferPoolPrivate
{
GstCaps *caps;
GstVideoInfo v_info;
gboolean add_videometa;
gsize alloc_sizes[GST_VIDEO_MAX_PLANES];
};
static void gst_vulkan_buffer_pool_finalize (GObject * object);
GST_DEBUG_CATEGORY_STATIC (GST_CAT_VULKAN_BUFFER_POOL);
#define GST_CAT_DEFAULT GST_CAT_VULKAN_BUFFER_POOL
#define GST_VULKAN_BUFFER_POOL_GET_PRIVATE(obj) \
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_VULKAN_BUFFER_POOL, GstVulkanBufferPoolPrivate))
#define gst_vulkan_buffer_pool_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstVulkanBufferPool, gst_vulkan_buffer_pool,
GST_TYPE_BUFFER_POOL, GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_BUFFER_POOL,
"vulkanbufferpool", 0, "Vulkan Buffer Pool"));
static const gchar **
gst_vulkan_buffer_pool_get_options (GstBufferPool * pool)
{
static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META,
NULL
};
return options;
}
static gboolean
gst_vulkan_buffer_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
GstVulkanBufferPool *vk_pool = GST_VULKAN_BUFFER_POOL_CAST (pool);
GstVulkanBufferPoolPrivate *priv = vk_pool->priv;
guint min_buffers, max_buffers;
GstCaps *caps = NULL;
gboolean ret = TRUE;
guint i;
if (!gst_buffer_pool_config_get_params (config, &caps, NULL, &min_buffers,
&max_buffers))
goto wrong_config;
if (caps == NULL)
goto no_caps;
/* now parse the caps from the config */
if (!gst_video_info_from_caps (&priv->v_info, caps))
goto wrong_caps;
GST_LOG_OBJECT (pool, "%dx%d, caps %" GST_PTR_FORMAT, priv->v_info.width,
priv->v_info.height, caps);
gst_caps_replace (&priv->caps, caps);
/* get the size of the buffer to allocate */
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&priv->v_info);
GstVulkanImageMemory *img_mem;
guint width, height;
VkFormat vk_format;
vk_format = gst_vulkan_format_from_video_format (v_format, i);
width = GST_VIDEO_INFO_WIDTH (&priv->v_info);
height = GST_VIDEO_INFO_HEIGHT (&priv->v_info);
img_mem = (GstVulkanImageMemory *)
gst_vulkan_image_memory_alloc (vk_pool->device, vk_format, width,
height, VK_IMAGE_TILING_OPTIMAL,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
priv->alloc_sizes[i] = img_mem->requirements.size;
gst_memory_unref (GST_MEMORY_CAST (img_mem));
}
priv->add_videometa = gst_buffer_pool_config_has_option (config,
GST_BUFFER_POOL_OPTION_VIDEO_META);
gst_buffer_pool_config_set_params (config, caps,
priv->v_info.size, min_buffers, max_buffers);
return GST_BUFFER_POOL_CLASS (parent_class)->set_config (pool, config) && ret;
/* ERRORS */
wrong_config:
{
GST_WARNING_OBJECT (pool, "invalid config");
return FALSE;
}
no_caps:
{
GST_WARNING_OBJECT (pool, "no caps in config");
return FALSE;
}
wrong_caps:
{
GST_WARNING_OBJECT (pool,
"failed getting geometry from caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
}
/* This function handles GstBuffer creation */
static GstFlowReturn
gst_vulkan_buffer_pool_alloc (GstBufferPool * pool, GstBuffer ** buffer,
GstBufferPoolAcquireParams * params)
{
GstVulkanBufferPool *vk_pool = GST_VULKAN_BUFFER_POOL_CAST (pool);
GstVulkanBufferPoolPrivate *priv = vk_pool->priv;
GstBuffer *buf;
guint i;
if (!(buf = gst_buffer_new ())) {
goto no_buffer;
}
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&priv->v_info); i++) {
GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&priv->v_info);
VkFormat vk_format;
GstMemory *mem;
vk_format = gst_vulkan_format_from_video_format (v_format, i);
mem = gst_vulkan_buffer_memory_alloc_bind (vk_pool->device,
vk_format, priv->alloc_sizes[i],
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
if (!mem) {
gst_buffer_unref (buf);
goto mem_create_failed;
}
gst_buffer_append_memory (buf, mem);
}
*buffer = buf;
return GST_FLOW_OK;
/* ERROR */
no_buffer:
{
GST_WARNING_OBJECT (pool, "can't create image");
return GST_FLOW_ERROR;
}
mem_create_failed:
{
GST_WARNING_OBJECT (pool, "Could not create Vulkan Memory");
return GST_FLOW_ERROR;
}
}
/**
* gst_vulkan_buffer_pool_new:
* @context: the #GstGLContext to use
*
* Returns: a #GstBufferPool that allocates buffers with #GstGLMemory
*/
GstBufferPool *
gst_vulkan_buffer_pool_new (GstVulkanDevice * device)
{
GstVulkanBufferPool *pool;
pool = g_object_new (GST_TYPE_VULKAN_BUFFER_POOL, NULL);
pool->device = gst_object_ref (device);
GST_LOG_OBJECT (pool, "new Vulkan buffer pool for device %" GST_PTR_FORMAT,
device);
return GST_BUFFER_POOL_CAST (pool);
}
static void
gst_vulkan_buffer_pool_class_init (GstVulkanBufferPoolClass * klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
GstBufferPoolClass *gstbufferpool_class = (GstBufferPoolClass *) klass;
g_type_class_add_private (klass, sizeof (GstVulkanBufferPoolPrivate));
gobject_class->finalize = gst_vulkan_buffer_pool_finalize;
gstbufferpool_class->get_options = gst_vulkan_buffer_pool_get_options;
gstbufferpool_class->set_config = gst_vulkan_buffer_pool_set_config;
gstbufferpool_class->alloc_buffer = gst_vulkan_buffer_pool_alloc;
}
static void
gst_vulkan_buffer_pool_init (GstVulkanBufferPool * pool)
{
pool->priv = GST_VULKAN_BUFFER_POOL_GET_PRIVATE (pool);
}
static void
gst_vulkan_buffer_pool_finalize (GObject * object)
{
GstVulkanBufferPool *pool = GST_VULKAN_BUFFER_POOL_CAST (object);
GstVulkanBufferPoolPrivate *priv = pool->priv;
GST_LOG_OBJECT (pool, "finalize Vulkan buffer pool %p", pool);
if (priv->caps)
gst_caps_unref (priv->caps);
G_OBJECT_CLASS (gst_vulkan_buffer_pool_parent_class)->finalize (object);
/* only release the context once all our memory have been deleted */
if (pool->device) {
gst_object_unref (pool->device);
pool->device = NULL;
}
}

67
ext/vulkan/vkbufferpool.h Normal file
View file

@ -0,0 +1,67 @@
/*
* GStreamer
* Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _GST_VULKAN_BUFFER_POOL_H_
#define _GST_VULKAN_BUFFER_POOL_H_
#include <gst/video/video.h>
#include <gst/video/gstvideometa.h>
#include <gst/video/gstvideopool.h>
#include <vk.h>
G_BEGIN_DECLS
/* buffer pool functions */
GType gst_vulkan_buffer_pool_get_type (void);
#define GST_TYPE_VULKAN_BUFFER_POOL (gst_vulkan_buffer_pool_get_type())
#define GST_IS_VULKAN_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_VULKAN_BUFFER_POOL))
#define GST_VULKAN_BUFFER_POOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_VULKAN_BUFFER_POOL, GstVulkanBufferPool))
#define GST_VULKAN_BUFFER_POOL_CAST(obj) ((GstVulkanBufferPool*)(obj))
/**
* GstVulkanBufferPool:
*
* Opaque GstVulkanBufferPool struct
*/
struct _GstVulkanBufferPool
{
GstBufferPool bufferpool;
GstVulkanDevice *device;
GstVulkanBufferPoolPrivate *priv;
};
/**
* GstVulkanBufferPoolClass:
*
* The #GstVulkanBufferPoolClass structure contains only private data
*/
struct _GstVulkanBufferPoolClass
{
GstBufferPoolClass parent_class;
};
GstBufferPool *gst_vulkan_buffer_pool_new (GstVulkanDevice * device);
G_END_DECLS
#endif /* _GST_VULKAN_BUFFER_POOL_H_ */

View file

@ -52,6 +52,10 @@ static gboolean gst_vulkan_upload_set_caps (GstBaseTransform * bt,
GstCaps * in_caps, GstCaps * out_caps);
static GstCaps *gst_vulkan_upload_transform_caps (GstBaseTransform * bt,
GstPadDirection direction, GstCaps * caps, GstCaps * filter);
static gboolean gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
GstQuery * decide_query, GstQuery * query);
static gboolean gst_vulkan_upload_decide_allocation (GstBaseTransform * bt,
GstQuery * query);
static GstFlowReturn gst_vulkan_upload_transform (GstBaseTransform * bt,
GstBuffer * inbuf, GstBuffer * outbuf);
static GstFlowReturn gst_vulkan_upload_prepare_output_buffer (GstBaseTransform *
@ -116,6 +120,10 @@ gst_vulkan_upload_class_init (GstVulkanUploadClass * klass)
gstbasetransform_class->query = GST_DEBUG_FUNCPTR (gst_vulkan_upload_query);
gstbasetransform_class->set_caps = gst_vulkan_upload_set_caps;
gstbasetransform_class->transform_caps = gst_vulkan_upload_transform_caps;
gstbasetransform_class->propose_allocation =
gst_vulkan_upload_propose_allocation;
gstbasetransform_class->decide_allocation =
gst_vulkan_upload_decide_allocation;
gstbasetransform_class->transform = gst_vulkan_upload_transform;
gstbasetransform_class->prepare_output_buffer =
gst_vulkan_upload_prepare_output_buffer;
@ -369,16 +377,98 @@ gst_vulkan_upload_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
return TRUE;
}
static gboolean
gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
GstQuery * decide_query, GstQuery * query)
{
GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
gboolean need_pool;
GstCaps *caps;
gboolean ret;
guint size;
gst_query_parse_allocation (query, &caps, &need_pool);
if (caps == NULL)
goto no_caps;
if (need_pool) {
GstBufferPool *pool;
GstStructure *config;
GstVideoInfo info;
if (!gst_video_info_from_caps (&info, caps))
goto invalid_caps;
/* the normal size of a frame */
size = info.size;
GST_DEBUG_OBJECT (bt, "create new pool");
pool = gst_vulkan_buffer_pool_new (vk_upload->device);
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
if (!gst_buffer_pool_set_config (pool, config)) {
g_object_unref (pool);
goto config_failed;
}
gst_query_add_allocation_pool (query, pool, size, 1, 0);
g_object_unref (pool);
}
return TRUE;
/* ERRORS */
no_caps:
{
GST_DEBUG_OBJECT (bt, "no caps specified");
return FALSE;
}
invalid_caps:
{
GST_DEBUG_OBJECT (bt, "invalid caps specified");
return FALSE;
}
config_failed:
{
GST_DEBUG_OBJECT (bt, "failed setting config");
return FALSE;
}
return ret;
}
static gboolean
gst_vulkan_upload_decide_allocation (GstBaseTransform * bt, GstQuery * query)
{
return TRUE;
}
static GstFlowReturn
gst_vulkan_upload_prepare_output_buffer (GstBaseTransform * bt,
GstBuffer * inbuf, GstBuffer ** outbuf)
{
if (gst_is_vulkan_buffer_memory (gst_buffer_peek_memory (inbuf, 0))) {
*outbuf = inbuf;
return GST_FLOW_OK;
}
return GST_BASE_TRANSFORM_CLASS (parent_class)->prepare_output_buffer (bt,
inbuf, outbuf);
}
static GstFlowReturn
gst_vulkan_upload_transform (GstBaseTransform * bt, GstBuffer * inbuf,
GstBuffer * outbuf)
{
GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
GstBaseTransformClass *bclass;
GstVideoFrame v_frame;
guint i;
bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
if (inbuf == outbuf)
return GST_FLOW_OK;
if (!gst_video_frame_map (&v_frame, &vk_upload->in_info, inbuf, GST_MAP_READ)) {
GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
@ -386,27 +476,15 @@ gst_vulkan_upload_prepare_output_buffer (GstBaseTransform * bt,
return GST_FLOW_ERROR;
}
*outbuf = gst_buffer_new ();
for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&vk_upload->out_info); i++) {
GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&vk_upload->out_info);
GstMapInfo map_info;
VkFormat vk_format;
gsize plane_size;
GstMemory *mem;
vk_format = gst_vulkan_format_from_video_format (v_format, i);
mem = gst_vulkan_buffer_memory_alloc_bind (vk_upload->device,
vk_format, vk_upload->alloc_sizes[i],
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
mem = gst_buffer_peek_memory (outbuf, i);
if (!gst_memory_map (GST_MEMORY_CAST (mem), &map_info, GST_MAP_WRITE)) {
GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
("%s", "Failed to map output memory"), NULL);
gst_buffer_unref (*outbuf);
*outbuf = NULL;
return GST_FLOW_ERROR;
}
@ -418,19 +496,10 @@ gst_vulkan_upload_prepare_output_buffer (GstBaseTransform * bt,
gst_memory_unmap (GST_MEMORY_CAST (mem), &map_info);
gst_buffer_append_memory (*outbuf, mem);
gst_buffer_append_memory (outbuf, mem);
}
gst_video_frame_unmap (&v_frame);
bclass->copy_metadata (bt, inbuf, *outbuf);
return GST_FLOW_OK;
}
static GstFlowReturn
gst_vulkan_upload_transform (GstBaseTransform * bt, GstBuffer * inbuf,
GstBuffer * outbuf)
{
return GST_FLOW_OK;
}