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.
This commit is contained in:
Matthew Waters 2019-11-06 09:29:51 +11:00 committed by GStreamer Merge Bot
parent 66244e9342
commit 212b313e29
5 changed files with 235 additions and 0 deletions

View file

@ -0,0 +1,111 @@
/*
* GStreamer
* Copyright (C) 2019 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.
*/
/**
* 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);
}

View file

@ -0,0 +1,119 @@
/*
* GStreamer
* Copyright (C) 2019 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_HANDLE_H__
#define __GST_VULKAN_HANDLE_H__
#include <gst/gst.h>
#include <gst/vulkan/vulkan_fwd.h>
#include <gst/vulkan/gstvkapi.h>
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;
/* <protected> */
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_ */

View file

@ -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',

View file

@ -45,6 +45,7 @@
#include <gst/vulkan/gstvkutils.h>
#include <gst/vulkan/gstvkcommandbuffer.h>
#include <gst/vulkan/gstvkcommandpool.h>
#include <gst/vulkan/gstvkhandle.h>
#include <gst/vulkan/gstvktrash.h>
#include <gst/vulkan/gstvkswapper.h>

View file

@ -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;