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 /**
22 * SECTION:element-vulkanimageidentity
23 * @title: vulkanimgeidentity
24 *
25 * vulkanimageidentity produces a vulkan image that is a copy of the input image.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <string.h>
33
34 #include "gstvulkanelements.h"
35 #include "vkimageidentity.h"
36
37 #include "shaders/identity.vert.h"
38 #include "shaders/identity.frag.h"
39
40 GST_DEBUG_CATEGORY (gst_debug_vulkan_image_identity);
41 #define GST_CAT_DEFAULT gst_debug_vulkan_image_identity
42
43 static gboolean gst_vulkan_image_identity_start (GstBaseTransform * bt);
44 static gboolean gst_vulkan_image_identity_stop (GstBaseTransform * bt);
45
46 static GstFlowReturn gst_vulkan_image_identity_transform (GstBaseTransform * bt,
47 GstBuffer * inbuf, GstBuffer * outbuf);
48 static gboolean gst_vulkan_image_identity_set_caps (GstBaseTransform * bt,
49 GstCaps * in_caps, GstCaps * out_caps);
50
51 #define IMAGE_FORMATS " { BGRA }"
52
53 static GstStaticPadTemplate gst_vulkan_sink_template =
54 GST_STATIC_PAD_TEMPLATE ("sink",
55 GST_PAD_SINK,
56 GST_PAD_ALWAYS,
57 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
58 (GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE,
59 IMAGE_FORMATS)));
60
61 static GstStaticPadTemplate gst_vulkan_src_template =
62 GST_STATIC_PAD_TEMPLATE ("src",
63 GST_PAD_SRC,
64 GST_PAD_ALWAYS,
65 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
66 (GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE,
67 IMAGE_FORMATS)));
68
69 enum
70 {
71 PROP_0,
72 };
73
74 enum
75 {
76 SIGNAL_0,
77 LAST_SIGNAL
78 };
79
80 /* static guint gst_vulkan_image_identity_signals[LAST_SIGNAL] = { 0 }; */
81
82 #define gst_vulkan_image_identity_parent_class parent_class
83 G_DEFINE_TYPE_WITH_CODE (GstVulkanImageIdentity, gst_vulkan_image_identity,
84 GST_TYPE_VULKAN_VIDEO_FILTER,
85 GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_image_identity,
86 "vulkanimageidentity", 0, "Vulkan Image identity"));
87 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (vulkanimageidentity,
88 "vulkanimageidentity", GST_RANK_NONE, GST_TYPE_VULKAN_IMAGE_IDENTITY,
89 vulkan_element_init (plugin));
90
91 static void
gst_vulkan_image_identity_class_init(GstVulkanImageIdentityClass * klass)92 gst_vulkan_image_identity_class_init (GstVulkanImageIdentityClass * klass)
93 {
94 GstElementClass *gstelement_class;
95 GstBaseTransformClass *gstbasetransform_class;
96
97 gstelement_class = (GstElementClass *) klass;
98 gstbasetransform_class = (GstBaseTransformClass *) klass;
99
100 gst_element_class_set_metadata (gstelement_class, "Vulkan Image Identity",
101 "Filter/Video", "A Vulkan image copier",
102 "Matthew Waters <matthew@centricular.com>");
103
104 gst_element_class_add_static_pad_template (gstelement_class,
105 &gst_vulkan_sink_template);
106 gst_element_class_add_static_pad_template (gstelement_class,
107 &gst_vulkan_src_template);
108
109 gstbasetransform_class->start =
110 GST_DEBUG_FUNCPTR (gst_vulkan_image_identity_start);
111 gstbasetransform_class->stop =
112 GST_DEBUG_FUNCPTR (gst_vulkan_image_identity_stop);
113 gstbasetransform_class->set_caps = gst_vulkan_image_identity_set_caps;
114 gstbasetransform_class->transform = gst_vulkan_image_identity_transform;
115 }
116
117 static void
gst_vulkan_image_identity_init(GstVulkanImageIdentity * vk_identity)118 gst_vulkan_image_identity_init (GstVulkanImageIdentity * vk_identity)
119 {
120 }
121
122 static gboolean
gst_vulkan_image_identity_set_caps(GstBaseTransform * bt,GstCaps * in_caps,GstCaps * out_caps)123 gst_vulkan_image_identity_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
124 GstCaps * out_caps)
125 {
126 GstVulkanVideoFilter *vfilter = GST_VULKAN_VIDEO_FILTER (bt);
127 GstVulkanImageIdentity *vk_identity = GST_VULKAN_IMAGE_IDENTITY (bt);
128
129 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->set_caps (bt, in_caps,
130 out_caps))
131 return FALSE;
132
133 if (!gst_vulkan_full_screen_quad_set_info (vk_identity->quad,
134 &vfilter->in_info, &vfilter->out_info))
135 return FALSE;
136
137 return TRUE;
138 }
139
140 static gboolean
gst_vulkan_image_identity_start(GstBaseTransform * bt)141 gst_vulkan_image_identity_start (GstBaseTransform * bt)
142 {
143 GstVulkanImageIdentity *vk_identity = GST_VULKAN_IMAGE_IDENTITY (bt);
144 GstVulkanVideoFilter *vfilter = GST_VULKAN_VIDEO_FILTER (vk_identity);
145 GstVulkanHandle *vert, *frag;
146 GError *error = NULL;
147
148 if (!GST_BASE_TRANSFORM_CLASS (parent_class)->start (bt))
149 return FALSE;
150
151 vk_identity->quad = gst_vulkan_full_screen_quad_new (vfilter->queue);
152
153 if (!(vert = gst_vulkan_create_shader (vfilter->device, identity_vert,
154 identity_vert_size, &error)))
155 goto error;
156 if (!(frag = gst_vulkan_create_shader (vfilter->device, identity_frag,
157 identity_frag_size, &error))) {
158 gst_vulkan_handle_unref (vert);
159 goto error;
160 }
161 gst_vulkan_full_screen_quad_set_shaders (vk_identity->quad, vert, frag);
162
163 gst_vulkan_handle_unref (vert);
164 gst_vulkan_handle_unref (frag);
165
166 return TRUE;
167
168 error:
169 GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND, ("%s", error->message), (NULL));
170 return FALSE;
171 }
172
173 static gboolean
gst_vulkan_image_identity_stop(GstBaseTransform * bt)174 gst_vulkan_image_identity_stop (GstBaseTransform * bt)
175 {
176 GstVulkanImageIdentity *vk_identity = GST_VULKAN_IMAGE_IDENTITY (bt);
177
178 gst_clear_object (&vk_identity->quad);
179
180 return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (bt);
181 }
182
183 static GstFlowReturn
gst_vulkan_image_identity_transform(GstBaseTransform * bt,GstBuffer * inbuf,GstBuffer * outbuf)184 gst_vulkan_image_identity_transform (GstBaseTransform * bt, GstBuffer * inbuf,
185 GstBuffer * outbuf)
186 {
187 GstVulkanImageIdentity *vk_identity = GST_VULKAN_IMAGE_IDENTITY (bt);
188 GError *error = NULL;
189
190 if (!gst_vulkan_full_screen_quad_set_input_buffer (vk_identity->quad, inbuf,
191 &error))
192 goto error;
193 if (!gst_vulkan_full_screen_quad_set_output_buffer (vk_identity->quad, outbuf,
194 &error))
195 goto error;
196
197 if (!gst_vulkan_full_screen_quad_draw (vk_identity->quad, &error))
198 goto error;
199
200 return GST_FLOW_OK;
201
202 error:
203 GST_ELEMENT_ERROR (bt, LIBRARY, FAILED, ("%s", error->message), (NULL));
204 g_clear_error (&error);
205 return GST_FLOW_ERROR;
206 }
207