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