1 /*
2 * GStreamer
3 * Copyright (C) 2019 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 <gst/vulkan/vulkan.h>
26
27 /**
28 * SECTION:vkimageview
29 * @title: GstVulkanImageView
30 * @short_description: wrapper for `VkImageView`'s
31 * @see_also: #GstVulkanImageMemory, #GstVulkanDevice
32 *
33 * #GstVulkanImageView is a wrapper around a `VkImageView` mostly for
34 * usage across element boundaries with #GstVulkanImageMemory
35 */
36
37 #define GST_CAT_DEFUALT GST_CAT_VULKAN_IMAGE_VIEW
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFUALT);
39
40 static void
init_debug(void)41 init_debug (void)
42 {
43 static gsize _init = 0;
44
45 if (g_once_init_enter (&_init)) {
46 GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_VIEW, "vulkanimageview",
47 0, "Vulkan Image View");
48 g_once_init_leave (&_init, 1);
49 }
50 }
51
52 extern void
53 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
54 GstVulkanImageView * view);
55
56 static gboolean
gst_vulkan_image_view_dispose(GstVulkanImageView * view)57 gst_vulkan_image_view_dispose (GstVulkanImageView * view)
58 {
59 GstVulkanImageMemory *image;
60
61 if ((image = view->image) == NULL)
62 return TRUE;
63
64 gst_vulkan_image_view_ref (view);
65 gst_vulkan_image_memory_release_view (image, view);
66
67 return FALSE;
68 }
69
70 static void
gst_vulkan_image_view_free(GstVulkanImageView * view)71 gst_vulkan_image_view_free (GstVulkanImageView * view)
72 {
73 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_VIEW, "freeing image view:%p ", view);
74
75 if (view->view)
76 vkDestroyImageView (view->device->device, view->view, NULL);
77
78 if (view->image) {
79 gst_memory_unref (GST_MEMORY_CAST (view->image));
80 }
81 view->image = NULL;
82 gst_clear_object (&view->device);
83
84 g_free (view);
85 }
86
87 /**
88 * gst_vulkan_image_view_new:
89 * @image: a #GstVulkanImageMemory to create the new view from
90 * @create_info: the creation information to create the view from
91 *
92 * Returns: (transfer full): A new #GstVulkanImageView from @image and
93 * @create_info
94 *
95 * Since: 1.18
96 */
97 GstVulkanImageView *
gst_vulkan_image_view_new(GstVulkanImageMemory * image,VkImageViewCreateInfo * create_info)98 gst_vulkan_image_view_new (GstVulkanImageMemory * image,
99 VkImageViewCreateInfo * create_info)
100 {
101 GstVulkanImageView *view;
102 GError *error = NULL;
103 VkResult err;
104
105 g_return_val_if_fail (create_info != NULL, NULL);
106 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
107 NULL);
108
109 init_debug ();
110
111 view = g_new0 (GstVulkanImageView, 1);
112
113 gst_mini_object_init ((GstMiniObject *) view, 0,
114 gst_vulkan_image_view_get_type (), NULL,
115 (GstMiniObjectDisposeFunction) gst_vulkan_image_view_dispose,
116 (GstMiniObjectFreeFunction) gst_vulkan_image_view_free);
117
118 err =
119 vkCreateImageView (image->device->device, create_info, NULL, &view->view);
120 if (gst_vulkan_error_to_g_error (err, &error, "vkImageCreateView") < 0)
121 goto vk_error;
122
123 view->image =
124 (GstVulkanImageMemory *) gst_memory_ref (GST_MEMORY_CAST (image));
125 view->device = gst_object_ref (image->device);
126 view->create_info = *create_info;
127 /* we cannot keep this as it may point to stack allocated memory */
128 view->create_info.pNext = NULL;
129
130 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_VIEW, "new image view for image: %p",
131 image);
132
133 return view;
134
135 vk_error:
136 {
137 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_VIEW,
138 "Failed to allocate image memory %s", error->message);
139 g_clear_error (&error);
140 goto error;
141 }
142
143 error:
144 {
145 g_free (view);
146 return NULL;
147 }
148 }
149
150 GST_DEFINE_MINI_OBJECT_TYPE (GstVulkanImageView, gst_vulkan_image_view);
151