From 212b313e2960bd81c81742406356b51531ef515a Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Wed, 6 Nov 2019 09:29:51 +1100 Subject: [PATCH] vulkan: add handle type for arbitrary vulkan handles Serve two purposes: 1. refcounting of vulkan handles with associated destruction. When combined with the trash list, the user can ensure destruction at the correct time according to the vulkan rules. 2. avoids polluting our API with 32-bit vs 64-bit integer/pointers differences as exposed through the vulkan API. on 32-bit, vulkan non-dispatchable handles are 64-bit integers and on 64-bit, they are pointers. --- gst-libs/gst/vulkan/gstvkhandle.c | 111 ++++++++++++++++++++++++++++ gst-libs/gst/vulkan/gstvkhandle.h | 119 ++++++++++++++++++++++++++++++ gst-libs/gst/vulkan/meson.build | 2 + gst-libs/gst/vulkan/vulkan.h | 1 + gst-libs/gst/vulkan/vulkan_fwd.h | 2 + 5 files changed, 235 insertions(+) create mode 100644 gst-libs/gst/vulkan/gstvkhandle.c create mode 100644 gst-libs/gst/vulkan/gstvkhandle.h diff --git a/gst-libs/gst/vulkan/gstvkhandle.c b/gst-libs/gst/vulkan/gstvkhandle.c new file mode 100644 index 0000000000..80600bdfa6 --- /dev/null +++ b/gst-libs/gst/vulkan/gstvkhandle.c @@ -0,0 +1,111 @@ +/* + * GStreamer + * Copyright (C) 2019 Matthew Waters + * + * 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. + */ + +/** + * SECTION:vulkancommandbuffer + * @title: vulkancommandbuffer + * + * vulkancommandbuffer holds information about a command buffer. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "gstvkhandle.h" +#include "gstvkdevice.h" + +#define GST_CAT_DEFAULT gst_debug_vulkan_handle +GST_DEBUG_CATEGORY (GST_CAT_DEFAULT); + +static void +init_debug (void) +{ + static volatile gsize _init = 0; + + if (g_once_init_enter (&_init)) { + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "vulkanhandle", 0, + "Vulkan handle"); + g_once_init_leave (&_init, 1); + } +} + +static void +gst_vulkan_handle_free (GstVulkanHandle * handle) +{ + GST_TRACE ("Freeing %p", handle); + + if (handle->notify) + handle->notify (handle, handle->user_data); + + gst_clear_object (&handle->device); + + g_free (handle); +} + +static void +gst_vulkan_handle_init (GstVulkanHandle * handle, GstVulkanDevice * device, + GstVulkanHandleType type, GstVulkanHandleTypedef handle_val, + GstVulkanHandleDestroyNotify notify, gpointer user_data) +{ + handle->device = gst_object_ref (device); + handle->type = type; + handle->handle = handle_val; + handle->notify = notify; + handle->user_data = user_data; + + init_debug (); + + GST_TRACE ("new %p", handle); + + gst_mini_object_init (&handle->parent, 0, GST_TYPE_VULKAN_HANDLE, NULL, NULL, + (GstMiniObjectFreeFunction) gst_vulkan_handle_free); +} + +/** + * gst_vulkan_handle_new_wrapped: + * @handle: a Vulkan handle + * @destroy: a #GDestroyNotify + * @user_data: data to pass to @notify + * + * Returns: (transfer full): a new #GstVulkanHandle wrapping @handle + */ +GstVulkanHandle * +gst_vulkan_handle_new_wrapped (GstVulkanDevice * device, + GstVulkanHandleType type, GstVulkanHandleTypedef handle, + GstVulkanHandleDestroyNotify notify, gpointer user_data) +{ + GstVulkanHandle *ret; + + ret = g_new0 (GstVulkanHandle, 1); + gst_vulkan_handle_init (ret, device, type, handle, notify, user_data); + + return ret; +} + +GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanHandle, gst_vulkan_handle); + +void +gst_vulkan_handle_free_descriptor_set_layout (GstVulkanHandle * handle, + gpointer user_data) +{ + vkDestroyDescriptorSetLayout (handle->device->device, + (VkDescriptorSetLayout) handle->handle, NULL); +} diff --git a/gst-libs/gst/vulkan/gstvkhandle.h b/gst-libs/gst/vulkan/gstvkhandle.h new file mode 100644 index 0000000000..331e3f54b7 --- /dev/null +++ b/gst-libs/gst/vulkan/gstvkhandle.h @@ -0,0 +1,119 @@ +/* + * GStreamer + * Copyright (C) 2019 Matthew Waters + * + * 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_HANDLE_H__ +#define __GST_VULKAN_HANDLE_H__ + +#include + +#include +#include + +G_BEGIN_DECLS + +GST_VULKAN_API +GType gst_vulkan_handle_get_type (void); +#define GST_TYPE_VULKAN_HANDLE (gst_vulkan_handle_get_type ()) + +VK_DEFINE_NON_DISPATCHABLE_HANDLE(GstVulkanHandleTypedef) + +typedef void (*GstVulkanHandleDestroyNotify) (GstVulkanHandle * handle, gpointer user_data); + +typedef enum +{ + GST_VULKAN_HANDLE_TYPE_DESCRIPTOR_SET_LAYOUT = 1, +} GstVulkanHandleType; + +struct _GstVulkanHandle +{ + GstMiniObject parent; + + GstVulkanDevice *device; + + GstVulkanHandleType type; + GstVulkanHandleTypedef handle; + + /* */ + GstVulkanHandleDestroyNotify notify; + gpointer user_data; +}; + +/** + * gst_vulkan_handle_ref: (skip) + * @cmd: a #GstVulkanHandle. + * + * Increases the refcount of the given handle by one. + * + * Returns: (transfer full): @buf + */ +static inline GstVulkanHandle* gst_vulkan_handle_ref(GstVulkanHandle* handle); +static inline GstVulkanHandle * +gst_vulkan_handle_ref (GstVulkanHandle * handle) +{ + return (GstVulkanHandle *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (handle)); +} + +/** + * gst_vulkan_handle_unref: (skip) + * @cmd: (transfer full): a #GstVulkanHandle. + * + * Decreases the refcount of the buffer. If the refcount reaches 0, the buffer + * will be freed. + */ +static inline void gst_vulkan_handle_unref(GstVulkanHandle* handle); +static inline void +gst_vulkan_handle_unref (GstVulkanHandle * handle) +{ + gst_mini_object_unref (GST_MINI_OBJECT_CAST (handle)); +} + +/** + * gst_clear_vulkan_handle: (skip) + * @handle_ptr: a pointer to a #GstVulkanHandle reference + * + * Clears a reference to a #GstVulkanHandle. + * + * @handle_ptr must not be %NULL. + * + * If the reference is %NULL then this function does nothing. Otherwise, the + * reference count of the handle is decreased and the pointer is set to %NULL. + * + * Since: 1.16 + */ +static inline void +gst_clear_vulkan_handle (GstVulkanHandle ** handle_ptr) +{ + gst_clear_mini_object ((GstMiniObject **) handle_ptr); +} + +GST_VULKAN_API +GstVulkanHandle * gst_vulkan_handle_new_wrapped (GstVulkanDevice *device, + GstVulkanHandleType type, + GstVulkanHandleTypedef handle, + GstVulkanHandleDestroyNotify notify, + gpointer user_data); + +GST_VULKAN_API +void gst_vulkan_handle_free_descriptor_set_layout (GstVulkanHandle * handle, + gpointer user_data); + +G_END_DECLS + +#endif /* _GST_VULKAN_HANDLE_H_ */ diff --git a/gst-libs/gst/vulkan/meson.build b/gst-libs/gst/vulkan/meson.build index 0cff5e9242..22906b442f 100644 --- a/gst-libs/gst/vulkan/meson.build +++ b/gst-libs/gst/vulkan/meson.build @@ -14,6 +14,7 @@ vulkan_sources = [ 'gstvkerror.c', 'gstvkfence.c', 'gstvkformat.c', + 'gstvkhandle.c', 'gstvkimagememory.c', 'gstvkimagebufferpool.c', 'gstvkimageview.c', @@ -40,6 +41,7 @@ vulkan_headers = [ 'gstvkerror.h', 'gstvkfence.h', 'gstvkformat.h', + 'gstvkhandle.h', 'gstvkimagememory.h', 'gstvkimagebufferpool.h', 'gstvkimageview.h', diff --git a/gst-libs/gst/vulkan/vulkan.h b/gst-libs/gst/vulkan/vulkan.h index db3f633382..30fec89bb1 100644 --- a/gst-libs/gst/vulkan/vulkan.h +++ b/gst-libs/gst/vulkan/vulkan.h @@ -45,6 +45,7 @@ #include #include #include +#include #include #include diff --git a/gst-libs/gst/vulkan/vulkan_fwd.h b/gst-libs/gst/vulkan/vulkan_fwd.h index 5f9b6e85aa..e4926bf827 100644 --- a/gst-libs/gst/vulkan/vulkan_fwd.h +++ b/gst-libs/gst/vulkan/vulkan_fwd.h @@ -68,6 +68,8 @@ typedef struct _GstVulkanBufferPool GstVulkanBufferPool; typedef struct _GstVulkanBufferPoolClass GstVulkanBufferPoolClass; typedef struct _GstVulkanBufferPoolPrivate GstVulkanBufferPoolPrivate; +typedef struct _GstVulkanHandle GstVulkanHandle; + typedef struct _GstVulkanImageMemory GstVulkanImageMemory; typedef struct _GstVulkanImageMemoryAllocator GstVulkanImageMemoryAllocator; typedef struct _GstVulkanImageMemoryAllocatorClass GstVulkanImageMemoryAllocatorClass;