1 /*
2  * GStreamer
3  * Copyright (C) 2016 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-vulkanupload
23  * @title: vulkanupload
24  *
25  * vulkanupload uploads data into Vulkan memory objects.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <string.h>
33 
34 #include "vkupload.h"
35 
36 GST_DEBUG_CATEGORY (gst_debug_vulkan_upload);
37 #define GST_CAT_DEFAULT gst_debug_vulkan_upload
38 
39 static GstCaps *
_set_caps_features_with_passthrough(const GstCaps * caps,const gchar * feature_name,GstCapsFeatures * passthrough)40 _set_caps_features_with_passthrough (const GstCaps * caps,
41     const gchar * feature_name, GstCapsFeatures * passthrough)
42 {
43   guint i, j, m, n;
44   GstCaps *tmp;
45 
46   tmp = gst_caps_copy (caps);
47 
48   n = gst_caps_get_size (caps);
49   for (i = 0; i < n; i++) {
50     GstCapsFeatures *features, *orig_features;
51 
52     orig_features = gst_caps_get_features (caps, i);
53     features = gst_caps_features_new (feature_name, NULL);
54 
55     m = gst_caps_features_get_size (orig_features);
56     for (j = 0; j < m; j++) {
57       const gchar *feature = gst_caps_features_get_nth (orig_features, j);
58 
59       /* if we already have the features */
60       if (gst_caps_features_contains (features, feature))
61         continue;
62 
63       if (g_strcmp0 (feature, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY) == 0)
64         continue;
65 
66       if (passthrough && gst_caps_features_contains (passthrough, feature)) {
67         gst_caps_features_add (features, feature);
68       }
69     }
70 
71     gst_caps_set_features (tmp, i, features);
72   }
73 
74   return tmp;
75 }
76 
77 struct BufferUpload
78 {
79   GstVulkanUpload *upload;
80 };
81 
82 static gpointer
_buffer_new_impl(GstVulkanUpload * upload)83 _buffer_new_impl (GstVulkanUpload * upload)
84 {
85   struct BufferUpload *raw = g_new0 (struct BufferUpload, 1);
86 
87   raw->upload = upload;
88 
89   return raw;
90 }
91 
92 static GstCaps *
_buffer_transform_caps(gpointer impl,GstPadDirection direction,GstCaps * caps)93 _buffer_transform_caps (gpointer impl, GstPadDirection direction,
94     GstCaps * caps)
95 {
96   return gst_caps_ref (caps);
97 }
98 
99 static gboolean
_buffer_set_caps(gpointer impl,GstCaps * in_caps,GstCaps * out_caps)100 _buffer_set_caps (gpointer impl, GstCaps * in_caps, GstCaps * out_caps)
101 {
102   return TRUE;
103 }
104 
105 static void
_buffer_propose_allocation(gpointer impl,GstQuery * decide_query,GstQuery * query)106 _buffer_propose_allocation (gpointer impl, GstQuery * decide_query,
107     GstQuery * query)
108 {
109   struct BufferUpload *raw = impl;
110   gboolean need_pool;
111   GstCaps *caps;
112   GstVideoInfo info;
113   guint size;
114   GstBufferPool *pool = NULL;
115 
116   gst_query_parse_allocation (query, &caps, &need_pool);
117 
118   if (caps == NULL)
119     return;
120 
121   if (!gst_video_info_from_caps (&info, caps))
122     return;
123 
124   /* the normal size of a frame */
125   size = info.size;
126 
127   if (need_pool) {
128     GstStructure *config;
129 
130     pool = gst_vulkan_buffer_pool_new (raw->upload->device);
131 
132     config = gst_buffer_pool_get_config (pool);
133     gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
134 
135     if (!gst_buffer_pool_set_config (pool, config)) {
136       g_object_unref (pool);
137       return;
138     }
139   }
140 
141   gst_query_add_allocation_pool (query, pool, size, 1, 0);
142   if (pool)
143     g_object_unref (pool);
144 
145   return;
146 }
147 
148 static GstFlowReturn
_buffer_perform(gpointer impl,GstBuffer * inbuf,GstBuffer ** outbuf)149 _buffer_perform (gpointer impl, GstBuffer * inbuf, GstBuffer ** outbuf)
150 {
151   if (!gst_is_vulkan_buffer_memory (gst_buffer_peek_memory (inbuf, 0)))
152     return GST_FLOW_ERROR;
153 
154   *outbuf = inbuf;
155 
156   return GST_FLOW_OK;
157 }
158 
159 static void
_buffer_free(gpointer impl)160 _buffer_free (gpointer impl)
161 {
162   g_free (impl);
163 }
164 
165 static GstStaticCaps _buffer_in_templ =
166     GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ") ;"
167     "video/x-raw");
168 static GstStaticCaps _buffer_out_templ =
169 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ")");
170 
171 static const struct UploadMethod buffer_upload = {
172   "VulkanBuffer",
173   &_buffer_in_templ,
174   &_buffer_out_templ,
175   _buffer_new_impl,
176   _buffer_transform_caps,
177   _buffer_set_caps,
178   _buffer_propose_allocation,
179   _buffer_perform,
180   _buffer_free,
181 };
182 
183 struct RawToBufferUpload
184 {
185   GstVulkanUpload *upload;
186 
187   GstVideoInfo in_info;
188   GstVideoInfo out_info;
189 
190   GstBufferPool *pool;
191   gboolean pool_active;
192 
193   gsize alloc_sizes[GST_VIDEO_MAX_PLANES];
194 };
195 
196 static gpointer
_raw_to_buffer_new_impl(GstVulkanUpload * upload)197 _raw_to_buffer_new_impl (GstVulkanUpload * upload)
198 {
199   struct RawToBufferUpload *raw = g_new0 (struct RawToBufferUpload, 1);
200 
201   raw->upload = upload;
202 
203   return raw;
204 }
205 
206 static GstCaps *
_raw_to_buffer_transform_caps(gpointer impl,GstPadDirection direction,GstCaps * caps)207 _raw_to_buffer_transform_caps (gpointer impl, GstPadDirection direction,
208     GstCaps * caps)
209 {
210   GstCaps *ret;
211 
212   if (direction == GST_PAD_SINK) {
213     ret =
214         _set_caps_features_with_passthrough (caps,
215         GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER, NULL);
216   } else {
217     ret =
218         _set_caps_features_with_passthrough (caps,
219         GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, NULL);
220   }
221 
222   return ret;
223 }
224 
225 static gboolean
_raw_to_buffer_set_caps(gpointer impl,GstCaps * in_caps,GstCaps * out_caps)226 _raw_to_buffer_set_caps (gpointer impl, GstCaps * in_caps, GstCaps * out_caps)
227 {
228   struct RawToBufferUpload *raw = impl;
229   guint out_width, out_height;
230   guint i;
231 
232   if (!gst_video_info_from_caps (&raw->in_info, in_caps))
233     return FALSE;
234 
235   if (!gst_video_info_from_caps (&raw->out_info, out_caps))
236     return FALSE;
237 
238   out_width = GST_VIDEO_INFO_WIDTH (&raw->out_info);
239   out_height = GST_VIDEO_INFO_HEIGHT (&raw->out_info);
240 
241   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&raw->out_info); i++) {
242     GstVideoFormat v_format = GST_VIDEO_INFO_FORMAT (&raw->out_info);
243     GstVulkanImageMemory *img_mem;
244     VkFormat vk_format;
245 
246     vk_format = gst_vulkan_format_from_video_format (v_format, i);
247 
248     img_mem = (GstVulkanImageMemory *)
249         gst_vulkan_image_memory_alloc (raw->upload->device, vk_format,
250         out_width, out_height, VK_IMAGE_TILING_OPTIMAL,
251         VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT,
252         VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
253 
254     raw->alloc_sizes[i] = img_mem->requirements.size;
255 
256     gst_memory_unref (GST_MEMORY_CAST (img_mem));
257   }
258 
259   return TRUE;
260 }
261 
262 static void
_raw_to_buffer_propose_allocation(gpointer impl,GstQuery * decide_query,GstQuery * query)263 _raw_to_buffer_propose_allocation (gpointer impl, GstQuery * decide_query,
264     GstQuery * query)
265 {
266   /* a little trickery with the impl pointer */
267   _buffer_propose_allocation (impl, decide_query, query);
268 }
269 
270 static GstFlowReturn
_raw_to_buffer_perform(gpointer impl,GstBuffer * inbuf,GstBuffer ** outbuf)271 _raw_to_buffer_perform (gpointer impl, GstBuffer * inbuf, GstBuffer ** outbuf)
272 {
273   struct RawToBufferUpload *raw = impl;
274   GstVideoFrame v_frame;
275   GstFlowReturn ret;
276   guint i;
277 
278   if (!raw->pool) {
279     GstStructure *config;
280     guint min = 0, max = 0;
281     gsize size = 1;
282 
283     raw->pool = gst_vulkan_buffer_pool_new (raw->upload->device);
284     config = gst_buffer_pool_get_config (raw->pool);
285     gst_buffer_pool_config_set_params (config, raw->upload->out_caps, size, min,
286         max);
287     gst_buffer_pool_set_config (raw->pool, config);
288   }
289   if (!raw->pool_active) {
290     gst_buffer_pool_set_active (raw->pool, TRUE);
291     raw->pool_active = TRUE;
292   }
293 
294   if ((ret =
295           gst_buffer_pool_acquire_buffer (raw->pool, outbuf,
296               NULL)) != GST_FLOW_OK)
297     goto out;
298 
299   if (!gst_video_frame_map (&v_frame, &raw->in_info, inbuf, GST_MAP_READ)) {
300     GST_ELEMENT_ERROR (raw->upload, RESOURCE, NOT_FOUND,
301         ("%s", "Failed to map input buffer"), NULL);
302     return GST_FLOW_ERROR;
303   }
304 
305   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&raw->out_info); i++) {
306     GstMapInfo map_info;
307     gsize plane_size;
308     GstMemory *mem;
309 
310     mem = gst_buffer_peek_memory (*outbuf, i);
311     if (!gst_memory_map (GST_MEMORY_CAST (mem), &map_info, GST_MAP_WRITE)) {
312       GST_ELEMENT_ERROR (raw->upload, RESOURCE, NOT_FOUND,
313           ("%s", "Failed to map output memory"), NULL);
314       gst_buffer_unref (*outbuf);
315       *outbuf = NULL;
316       ret = GST_FLOW_ERROR;
317       goto out;
318     }
319 
320     plane_size =
321         GST_VIDEO_INFO_PLANE_STRIDE (&raw->out_info,
322         i) * GST_VIDEO_INFO_COMP_HEIGHT (&raw->out_info, i);
323     g_assert (plane_size < map_info.size);
324     memcpy (map_info.data, v_frame.data[i], plane_size);
325 
326     gst_memory_unmap (GST_MEMORY_CAST (mem), &map_info);
327   }
328 
329   gst_video_frame_unmap (&v_frame);
330 
331   ret = GST_FLOW_OK;
332 
333 out:
334   return ret;
335 }
336 
337 static void
_raw_to_buffer_free(gpointer impl)338 _raw_to_buffer_free (gpointer impl)
339 {
340   struct RawToBufferUpload *raw = impl;
341 
342   if (raw->pool) {
343     if (raw->pool_active) {
344       gst_buffer_pool_set_active (raw->pool, FALSE);
345     }
346     raw->pool_active = FALSE;
347     gst_object_unref (raw->pool);
348     raw->pool = NULL;
349   }
350 
351   g_free (impl);
352 }
353 
354 static GstStaticCaps _raw_to_buffer_in_templ = GST_STATIC_CAPS ("video/x-raw");
355 static GstStaticCaps _raw_to_buffer_out_templ =
356 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_BUFFER ")");
357 
358 static const struct UploadMethod raw_to_buffer_upload = {
359   "RawToVulkanBuffer",
360   &_raw_to_buffer_in_templ,
361   &_raw_to_buffer_out_templ,
362   _raw_to_buffer_new_impl,
363   _raw_to_buffer_transform_caps,
364   _raw_to_buffer_set_caps,
365   _raw_to_buffer_propose_allocation,
366   _raw_to_buffer_perform,
367   _raw_to_buffer_free,
368 };
369 
370 static const struct UploadMethod *upload_methods[] = {
371   &buffer_upload,
372   &raw_to_buffer_upload,
373 };
374 
375 static GstCaps *
_get_input_template_caps(void)376 _get_input_template_caps (void)
377 {
378   GstCaps *ret = NULL;
379   gint i;
380 
381   /* FIXME: cache this and invalidate on changes to upload_methods */
382   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
383     GstCaps *template = gst_static_caps_get (upload_methods[i]->in_template);
384     ret = ret == NULL ? template : gst_caps_merge (ret, template);
385   }
386 
387   ret = gst_caps_simplify (ret);
388 
389   return ret;
390 }
391 
392 static GstCaps *
_get_output_template_caps(void)393 _get_output_template_caps (void)
394 {
395   GstCaps *ret = NULL;
396   gint i;
397 
398   /* FIXME: cache this and invalidate on changes to upload_methods */
399   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
400     GstCaps *template = gst_static_caps_get (upload_methods[i]->out_template);
401     ret = ret == NULL ? template : gst_caps_merge (ret, template);
402   }
403 
404   ret = gst_caps_simplify (ret);
405 
406   return ret;
407 }
408 
409 static void gst_vulkan_upload_finalize (GObject * object);
410 static void gst_vulkan_upload_set_property (GObject * object, guint prop_id,
411     const GValue * value, GParamSpec * param_spec);
412 static void gst_vulkan_upload_get_property (GObject * object, guint prop_id,
413     GValue * value, GParamSpec * param_spec);
414 
415 static gboolean gst_vulkan_upload_query (GstBaseTransform * bt,
416     GstPadDirection direction, GstQuery * query);
417 static void gst_vulkan_upload_set_context (GstElement * element,
418     GstContext * context);
419 static GstStateChangeReturn gst_vulkan_upload_change_state (GstElement *
420     element, GstStateChange transition);
421 
422 static gboolean gst_vulkan_upload_set_caps (GstBaseTransform * bt,
423     GstCaps * in_caps, GstCaps * out_caps);
424 static GstCaps *gst_vulkan_upload_transform_caps (GstBaseTransform * bt,
425     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
426 static gboolean gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
427     GstQuery * decide_query, GstQuery * query);
428 static gboolean gst_vulkan_upload_decide_allocation (GstBaseTransform * bt,
429     GstQuery * query);
430 static GstFlowReturn gst_vulkan_upload_transform (GstBaseTransform * bt,
431     GstBuffer * inbuf, GstBuffer * outbuf);
432 static GstFlowReturn gst_vulkan_upload_prepare_output_buffer (GstBaseTransform *
433     bt, GstBuffer * inbuf, GstBuffer ** outbuf);
434 
435 enum
436 {
437   PROP_0,
438 };
439 
440 enum
441 {
442   SIGNAL_0,
443   LAST_SIGNAL
444 };
445 
446 /* static guint gst_vulkan_upload_signals[LAST_SIGNAL] = { 0 }; */
447 
448 #define gst_vulkan_upload_parent_class parent_class
449 G_DEFINE_TYPE_WITH_CODE (GstVulkanUpload, gst_vulkan_upload,
450     GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_upload,
451         "vulkanupload", 0, "Vulkan Uploader"));
452 
453 static void
gst_vulkan_upload_class_init(GstVulkanUploadClass * klass)454 gst_vulkan_upload_class_init (GstVulkanUploadClass * klass)
455 {
456   GObjectClass *gobject_class;
457   GstElementClass *gstelement_class;
458   GstBaseTransformClass *gstbasetransform_class;
459 
460   gobject_class = (GObjectClass *) klass;
461   gstelement_class = (GstElementClass *) klass;
462   gstbasetransform_class = (GstBaseTransformClass *) klass;
463 
464   gobject_class->set_property = gst_vulkan_upload_set_property;
465   gobject_class->get_property = gst_vulkan_upload_get_property;
466 
467   gst_element_class_set_metadata (gstelement_class, "Vulkan Uploader",
468       "Filter/Video", "A Vulkan data uploader",
469       "Matthew Waters <matthew@centricular.com>");
470 
471   {
472     GstCaps *caps;
473 
474     caps = _get_input_template_caps ();
475     gst_element_class_add_pad_template (gstelement_class,
476         gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
477     gst_caps_unref (caps);
478 
479     caps = _get_output_template_caps ();
480     gst_element_class_add_pad_template (gstelement_class,
481         gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
482     gst_caps_unref (caps);
483   }
484 
485   gobject_class->finalize = gst_vulkan_upload_finalize;
486 
487   gstelement_class->change_state = gst_vulkan_upload_change_state;
488   gstelement_class->set_context = gst_vulkan_upload_set_context;
489   gstbasetransform_class->query = GST_DEBUG_FUNCPTR (gst_vulkan_upload_query);
490   gstbasetransform_class->set_caps = gst_vulkan_upload_set_caps;
491   gstbasetransform_class->transform_caps = gst_vulkan_upload_transform_caps;
492   gstbasetransform_class->propose_allocation =
493       gst_vulkan_upload_propose_allocation;
494   gstbasetransform_class->decide_allocation =
495       gst_vulkan_upload_decide_allocation;
496   gstbasetransform_class->transform = gst_vulkan_upload_transform;
497   gstbasetransform_class->prepare_output_buffer =
498       gst_vulkan_upload_prepare_output_buffer;
499 }
500 
501 static void
gst_vulkan_upload_init(GstVulkanUpload * vk_upload)502 gst_vulkan_upload_init (GstVulkanUpload * vk_upload)
503 {
504   guint i, n;
505 
506   n = G_N_ELEMENTS (upload_methods);
507   vk_upload->upload_impls = g_malloc (sizeof (gpointer) * n);
508   for (i = 0; i < n; i++) {
509     vk_upload->upload_impls[i] = upload_methods[i]->new_impl (vk_upload);
510   }
511 }
512 
513 static void
gst_vulkan_upload_finalize(GObject * object)514 gst_vulkan_upload_finalize (GObject * object)
515 {
516   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
517   guint i;
518 
519   gst_caps_replace (&vk_upload->in_caps, NULL);
520   gst_caps_replace (&vk_upload->out_caps, NULL);
521 
522   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
523     upload_methods[i]->free (vk_upload->upload_impls[i]);
524   }
525   g_free (vk_upload->upload_impls);
526   vk_upload->upload_impls = NULL;
527 
528   G_OBJECT_CLASS (parent_class)->finalize (object);
529 }
530 
531 static void
gst_vulkan_upload_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)532 gst_vulkan_upload_set_property (GObject * object, guint prop_id,
533     const GValue * value, GParamSpec * pspec)
534 {
535 //  GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
536 
537   switch (prop_id) {
538     default:
539       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
540       break;
541   }
542 }
543 
544 static void
gst_vulkan_upload_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)545 gst_vulkan_upload_get_property (GObject * object, guint prop_id,
546     GValue * value, GParamSpec * pspec)
547 {
548 //  GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (object);
549 
550   switch (prop_id) {
551     default:
552       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
553       break;
554   }
555 }
556 
557 static gboolean
gst_vulkan_upload_query(GstBaseTransform * bt,GstPadDirection direction,GstQuery * query)558 gst_vulkan_upload_query (GstBaseTransform * bt, GstPadDirection direction,
559     GstQuery * query)
560 {
561   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
562   gboolean res = FALSE;
563 
564   switch (GST_QUERY_TYPE (query)) {
565     case GST_QUERY_CONTEXT:{
566       res = gst_vulkan_handle_context_query (GST_ELEMENT (vk_upload), query,
567           &vk_upload->display, &vk_upload->instance, &vk_upload->device);
568 
569       if (res)
570         return res;
571       break;
572     }
573     default:
574       break;
575   }
576 
577   return GST_BASE_TRANSFORM_CLASS (parent_class)->query (bt, direction, query);
578 }
579 
580 static void
gst_vulkan_upload_set_context(GstElement * element,GstContext * context)581 gst_vulkan_upload_set_context (GstElement * element, GstContext * context)
582 {
583   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (element);
584 
585   gst_vulkan_handle_set_context (element, context, &vk_upload->display,
586       &vk_upload->instance);
587 
588   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
589 }
590 
591 static GstStateChangeReturn
gst_vulkan_upload_change_state(GstElement * element,GstStateChange transition)592 gst_vulkan_upload_change_state (GstElement * element, GstStateChange transition)
593 {
594   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (element);
595   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
596 
597   GST_DEBUG ("changing state: %s => %s",
598       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
599       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
600 
601   switch (transition) {
602     case GST_STATE_CHANGE_NULL_TO_READY:
603       break;
604     case GST_STATE_CHANGE_READY_TO_PAUSED:
605       if (!gst_vulkan_ensure_element_data (element, &vk_upload->display,
606               &vk_upload->instance)) {
607         GST_ELEMENT_ERROR (vk_upload, RESOURCE, NOT_FOUND,
608             ("Failed to retreive vulkan instance/display"), (NULL));
609         return GST_STATE_CHANGE_FAILURE;
610       }
611       if (!gst_vulkan_device_run_context_query (GST_ELEMENT (vk_upload),
612               &vk_upload->device)) {
613         GST_ELEMENT_ERROR (vk_upload, RESOURCE, NOT_FOUND,
614             ("Failed to retreive vulkan device"), (NULL));
615         return GST_STATE_CHANGE_FAILURE;
616       }
617       break;
618     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
619       break;
620     default:
621       break;
622   }
623 
624   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
625   if (ret == GST_STATE_CHANGE_FAILURE)
626     return ret;
627 
628   switch (transition) {
629     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
630       break;
631     case GST_STATE_CHANGE_PAUSED_TO_READY:
632       if (vk_upload->display)
633         gst_object_unref (vk_upload->display);
634       vk_upload->display = NULL;
635       if (vk_upload->device)
636         gst_object_unref (vk_upload->device);
637       vk_upload->device = NULL;
638       if (vk_upload->instance)
639         gst_object_unref (vk_upload->instance);
640       vk_upload->instance = NULL;
641       break;
642     case GST_STATE_CHANGE_READY_TO_NULL:
643       break;
644     default:
645       break;
646   }
647 
648   return ret;
649 }
650 
651 static GstCaps *
gst_vulkan_upload_transform_caps(GstBaseTransform * bt,GstPadDirection direction,GstCaps * caps,GstCaps * filter)652 gst_vulkan_upload_transform_caps (GstBaseTransform * bt,
653     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
654 {
655   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
656   GstCaps *result, *tmp;
657   gint i;
658 
659   tmp = gst_caps_new_empty ();
660 
661   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
662     GstCaps *tmp2;
663     GstCaps *templ;
664 
665     if (direction == GST_PAD_SINK) {
666       templ = gst_static_caps_get (upload_methods[i]->in_template);
667     } else {
668       templ = gst_static_caps_get (upload_methods[i]->out_template);
669     }
670     if (!gst_caps_can_intersect (caps, templ)) {
671       gst_caps_unref (templ);
672       continue;
673     }
674     gst_caps_unref (templ);
675 
676     tmp2 = upload_methods[i]->transform_caps (vk_upload->upload_impls[i],
677         direction, caps);
678 
679     if (tmp2)
680       tmp = gst_caps_merge (tmp, tmp2);
681   }
682 
683   if (filter) {
684     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
685     gst_caps_unref (tmp);
686   } else {
687     result = tmp;
688   }
689 
690   return result;
691 }
692 
693 static gboolean
gst_vulkan_upload_set_caps(GstBaseTransform * bt,GstCaps * in_caps,GstCaps * out_caps)694 gst_vulkan_upload_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
695     GstCaps * out_caps)
696 {
697   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
698   gboolean found_method = FALSE;
699   guint i;
700 
701   gst_caps_replace (&vk_upload->in_caps, in_caps);
702   gst_caps_replace (&vk_upload->out_caps, out_caps);
703 
704   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
705     GstCaps *templ;
706 
707     templ = gst_static_caps_get (upload_methods[i]->in_template);
708     if (!gst_caps_can_intersect (in_caps, templ)) {
709       gst_caps_unref (templ);
710       continue;
711     }
712     gst_caps_unref (templ);
713 
714     templ = gst_static_caps_get (upload_methods[i]->out_template);
715     if (!gst_caps_can_intersect (out_caps, templ)) {
716       gst_caps_unref (templ);
717       continue;
718     }
719     gst_caps_unref (templ);
720 
721     if (!upload_methods[i]->set_caps (vk_upload->upload_impls[i], in_caps,
722             out_caps))
723       continue;
724 
725     GST_LOG_OBJECT (bt, "uploader %s accepted caps in: %" GST_PTR_FORMAT
726         " out: %" GST_PTR_FORMAT, upload_methods[i]->name, in_caps, out_caps);
727 
728     vk_upload->current_impl = i;
729     found_method = TRUE;
730     break;
731   }
732 
733   GST_DEBUG_OBJECT (bt,
734       "set caps in: %" GST_PTR_FORMAT " out: %" GST_PTR_FORMAT, in_caps,
735       out_caps);
736 
737   return found_method;
738 }
739 
740 static gboolean
gst_vulkan_upload_propose_allocation(GstBaseTransform * bt,GstQuery * decide_query,GstQuery * query)741 gst_vulkan_upload_propose_allocation (GstBaseTransform * bt,
742     GstQuery * decide_query, GstQuery * query)
743 {
744   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
745   guint i;
746 
747   for (i = 0; i < G_N_ELEMENTS (upload_methods); i++) {
748     GstCaps *templ;
749 
750     templ = gst_static_caps_get (upload_methods[i]->in_template);
751     if (!gst_caps_can_intersect (vk_upload->in_caps, templ)) {
752       gst_caps_unref (templ);
753       continue;
754     }
755     gst_caps_unref (templ);
756 
757     templ = gst_static_caps_get (upload_methods[i]->out_template);
758     if (!gst_caps_can_intersect (vk_upload->out_caps, templ)) {
759       gst_caps_unref (templ);
760       continue;
761     }
762     gst_caps_unref (templ);
763 
764     upload_methods[i]->propose_allocation (vk_upload->upload_impls[i],
765         decide_query, query);
766   }
767 
768   return TRUE;
769 }
770 
771 static gboolean
gst_vulkan_upload_decide_allocation(GstBaseTransform * bt,GstQuery * query)772 gst_vulkan_upload_decide_allocation (GstBaseTransform * bt, GstQuery * query)
773 {
774   return TRUE;
775 }
776 
777 static gboolean
_upload_find_method(GstVulkanUpload * vk_upload)778 _upload_find_method (GstVulkanUpload * vk_upload)
779 {
780   vk_upload->current_impl++;
781 
782   if (vk_upload->current_impl >= G_N_ELEMENTS (upload_methods))
783     return FALSE;
784 
785   GST_DEBUG_OBJECT (vk_upload, "attempting upload with uploader %s",
786       upload_methods[vk_upload->current_impl]->name);
787 
788   return TRUE;
789 }
790 
791 static GstFlowReturn
gst_vulkan_upload_prepare_output_buffer(GstBaseTransform * bt,GstBuffer * inbuf,GstBuffer ** outbuf)792 gst_vulkan_upload_prepare_output_buffer (GstBaseTransform * bt,
793     GstBuffer * inbuf, GstBuffer ** outbuf)
794 {
795   GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
796   GstVulkanUpload *vk_upload = GST_VULKAN_UPLOAD (bt);
797   GstFlowReturn ret;
798 
799 restart:
800   {
801     gpointer method_impl;
802     const struct UploadMethod *method;
803 
804     method = upload_methods[vk_upload->current_impl];
805     method_impl = vk_upload->upload_impls[vk_upload->current_impl];
806 
807     ret = method->perform (method_impl, inbuf, outbuf);
808     if (ret != GST_FLOW_OK) {
809     next_method:
810       if (!_upload_find_method (vk_upload)) {
811         GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
812             ("Could not find suitable uploader"), (NULL));
813         return GST_FLOW_ERROR;
814       }
815 
816       method = upload_methods[vk_upload->current_impl];
817       method_impl = vk_upload->upload_impls[vk_upload->current_impl];
818       if (!method->set_caps (method_impl, vk_upload->in_caps,
819               vk_upload->out_caps))
820         /* try the next method */
821         goto next_method;
822 
823       /* try the uploading with the next method */
824       goto restart;
825     }
826   }
827 
828   if (ret == GST_FLOW_OK) {
829     /* basetransform doesn't unref if they're the same */
830     if (inbuf != *outbuf)
831       bclass->copy_metadata (bt, inbuf, *outbuf);
832   }
833 
834   return ret;
835 }
836 
837 static GstFlowReturn
gst_vulkan_upload_transform(GstBaseTransform * bt,GstBuffer * inbuf,GstBuffer * outbuf)838 gst_vulkan_upload_transform (GstBaseTransform * bt, GstBuffer * inbuf,
839     GstBuffer * outbuf)
840 {
841   return GST_FLOW_OK;
842 }
843