Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / vulkan / vkdownload.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-vulkandownload
23  * @title: vulkandownload
24  *
25  * vulkandownload downloads 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 "gstvulkanelements.h"
35 #include "vkdownload.h"
36
37 GST_DEBUG_CATEGORY (gst_debug_vulkan_download);
38 #define GST_CAT_DEFAULT gst_debug_vulkan_download
39
40 static GstCaps *
41 _set_caps_features_with_passthrough (const GstCaps * caps,
42     const gchar * feature_name, GstCapsFeatures * passthrough)
43 {
44   guint i, j, m, n;
45   GstCaps *tmp;
46
47   tmp = gst_caps_copy (caps);
48
49   n = gst_caps_get_size (caps);
50   for (i = 0; i < n; i++) {
51     GstCapsFeatures *features, *orig_features;
52
53     orig_features = gst_caps_get_features (caps, i);
54     features = gst_caps_features_new (feature_name, NULL);
55
56     m = gst_caps_features_get_size (orig_features);
57     for (j = 0; j < m; j++) {
58       const gchar *feature = gst_caps_features_get_nth (orig_features, j);
59
60       /* if we already have the features */
61       if (gst_caps_features_contains (features, feature))
62         continue;
63
64       if (g_strcmp0 (feature, GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY) == 0)
65         continue;
66
67       if (passthrough && gst_caps_features_contains (passthrough, feature)) {
68         gst_caps_features_add (features, feature);
69       }
70     }
71
72     gst_caps_set_features (tmp, i, features);
73   }
74
75   return tmp;
76 }
77
78 struct ImageToRawDownload
79 {
80   GstVulkanDownload *download;
81
82   GstVideoInfo in_info;
83   GstVideoInfo out_info;
84
85   GstBufferPool *pool;
86   gboolean pool_active;
87
88   GstVulkanCommandPool *cmd_pool;
89   GstVulkanTrashList *trash_list;
90 };
91
92 static gpointer
93 _image_to_raw_new_impl (GstVulkanDownload * download)
94 {
95   struct ImageToRawDownload *raw = g_new0 (struct ImageToRawDownload, 1);
96
97   raw->download = download;
98   raw->trash_list = gst_vulkan_trash_fence_list_new ();
99
100   return raw;
101 }
102
103 static GstCaps *
104 _image_to_raw_transform_caps (gpointer impl, GstPadDirection direction,
105     GstCaps * caps)
106 {
107   GstCaps *ret;
108
109   if (direction == GST_PAD_SINK) {
110     ret =
111         _set_caps_features_with_passthrough (caps,
112         GST_CAPS_FEATURE_MEMORY_SYSTEM_MEMORY, NULL);
113   } else {
114     ret =
115         _set_caps_features_with_passthrough (caps,
116         GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE, NULL);
117   }
118
119   return ret;
120 }
121
122 static gboolean
123 _image_to_raw_set_caps (gpointer impl, GstCaps * in_caps, GstCaps * out_caps)
124 {
125   struct ImageToRawDownload *raw = impl;
126
127   if (!gst_video_info_from_caps (&raw->in_info, in_caps))
128     return FALSE;
129
130   if (!gst_video_info_from_caps (&raw->out_info, out_caps))
131     return FALSE;
132
133   return TRUE;
134 }
135
136 static void
137 _image_to_raw_propose_allocation (gpointer impl, GstQuery * decide_query,
138     GstQuery * query)
139 {
140   /* FIXME: implement */
141 }
142
143 static GstFlowReturn
144 _image_to_raw_perform (gpointer impl, GstBuffer * inbuf, GstBuffer ** outbuf)
145 {
146   struct ImageToRawDownload *raw = impl;
147   GstVulkanCommandBuffer *cmd_buf;
148   GError *error = NULL;
149   GstFlowReturn ret;
150   VkResult err;
151   int i;
152
153   if (!raw->cmd_pool) {
154     if (!(raw->cmd_pool =
155             gst_vulkan_queue_create_command_pool (raw->download->queue,
156                 &error))) {
157       goto error;
158     }
159   }
160
161   if (!(cmd_buf = gst_vulkan_command_pool_create (raw->cmd_pool, &error)))
162     goto error;
163
164   if (!raw->pool) {
165     GstStructure *config;
166     guint min = 0, max = 0;
167     gsize size = 1;
168
169     raw->pool = gst_vulkan_buffer_pool_new (raw->download->device);
170     config = gst_buffer_pool_get_config (raw->pool);
171     gst_buffer_pool_config_set_params (config, raw->download->out_caps, size,
172         min, max);
173     gst_buffer_pool_set_config (raw->pool, config);
174   }
175   if (!raw->pool_active) {
176     gst_buffer_pool_set_active (raw->pool, TRUE);
177     raw->pool_active = TRUE;
178   }
179
180   if ((ret =
181           gst_buffer_pool_acquire_buffer (raw->pool, outbuf,
182               NULL)) != GST_FLOW_OK)
183     goto out;
184
185   {
186     /* *INDENT-OFF* */
187     VkCommandBufferBeginInfo cmd_buf_info = {
188         .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
189         .pNext = NULL,
190         .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
191         .pInheritanceInfo = NULL
192     };
193     /* *INDENT-ON* */
194
195     gst_vulkan_command_buffer_lock (cmd_buf);
196     err = vkBeginCommandBuffer (cmd_buf->cmd, &cmd_buf_info);
197     if (gst_vulkan_error_to_g_error (err, &error, "vkBeginCommandBuffer") < 0)
198       goto unlock_error;
199   }
200
201   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (&raw->out_info); i++) {
202     VkBufferImageCopy region;
203     GstMemory *in_mem, *out_mem;
204     GstVulkanBufferMemory *buf_mem;
205     GstVulkanImageMemory *img_mem;
206     VkImageMemoryBarrier image_memory_barrier;
207     VkBufferMemoryBarrier buffer_memory_barrier;
208
209     in_mem = gst_buffer_peek_memory (inbuf, i);
210     if (!gst_is_vulkan_image_memory (in_mem)) {
211       GST_WARNING_OBJECT (raw->download, "Input is not a GstVulkanImageMemory");
212       goto unlock_error;
213     }
214     img_mem = (GstVulkanImageMemory *) in_mem;
215
216     out_mem = gst_buffer_peek_memory (*outbuf, i);
217     if (!gst_is_vulkan_buffer_memory (out_mem)) {
218       GST_WARNING_OBJECT (raw->download,
219           "Output is not a GstVulkanBufferMemory");
220       goto unlock_error;
221     }
222     buf_mem = (GstVulkanBufferMemory *) out_mem;
223
224     /* *INDENT-OFF* */
225     region = (VkBufferImageCopy) {
226         .bufferOffset = 0,
227         .bufferRowLength = GST_VIDEO_INFO_COMP_WIDTH (&raw->in_info, i),
228         .bufferImageHeight = GST_VIDEO_INFO_COMP_HEIGHT (&raw->in_info, i),
229         .imageSubresource = {
230             .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
231             .mipLevel = 0,
232             .baseArrayLayer = 0,
233             .layerCount = 1,
234         },
235         .imageOffset = { .x = 0, .y = 0, .z = 0, },
236         .imageExtent = {
237             .width = GST_VIDEO_INFO_COMP_WIDTH (&raw->out_info, i),
238             .height = GST_VIDEO_INFO_COMP_HEIGHT (&raw->out_info, i),
239             .depth = 1,
240         }
241     };
242
243     image_memory_barrier = (VkImageMemoryBarrier) {
244         .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
245         .pNext = NULL,
246         .srcAccessMask = img_mem->barrier.parent.access_flags,
247         .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
248         .oldLayout = img_mem->barrier.image_layout,
249         .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
250         /* FIXME: implement exclusive transfers */
251         .srcQueueFamilyIndex = 0,
252         .dstQueueFamilyIndex = 0,
253         .image = img_mem->image,
254         .subresourceRange = img_mem->barrier.subresource_range
255     };
256
257     buffer_memory_barrier = (VkBufferMemoryBarrier) {
258         .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
259         .pNext = NULL,
260         .srcAccessMask = buf_mem->barrier.parent.access_flags,
261         .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
262         /* FIXME: implement exclusive transfers */
263         .srcQueueFamilyIndex = 0,
264         .dstQueueFamilyIndex = 0,
265         .buffer = buf_mem->buffer,
266         .offset = region.bufferOffset,
267         .size = region.bufferRowLength * region.bufferImageHeight
268     };
269     /* *INDENT-ON* */
270
271     vkCmdPipelineBarrier (cmd_buf->cmd,
272         buf_mem->barrier.parent.pipeline_stages | img_mem->barrier.
273         parent.pipeline_stages, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1,
274         &buffer_memory_barrier, 1, &image_memory_barrier);
275
276     buf_mem->barrier.parent.pipeline_stages = VK_PIPELINE_STAGE_TRANSFER_BIT;
277     buf_mem->barrier.parent.access_flags = buffer_memory_barrier.dstAccessMask;
278
279     img_mem->barrier.parent.pipeline_stages = VK_PIPELINE_STAGE_TRANSFER_BIT;
280     img_mem->barrier.parent.access_flags = image_memory_barrier.dstAccessMask;
281     img_mem->barrier.image_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
282
283     vkCmdCopyImageToBuffer (cmd_buf->cmd, img_mem->image,
284         img_mem->barrier.image_layout, buf_mem->buffer, 1, &region);
285   }
286
287   err = vkEndCommandBuffer (cmd_buf->cmd);
288   gst_vulkan_command_buffer_unlock (cmd_buf);
289   if (gst_vulkan_error_to_g_error (err, &error, "vkEndCommandBuffer") < 0)
290     goto error;
291
292   {
293     VkSubmitInfo submit_info = { 0, };
294     VkPipelineStageFlags stages = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
295     GstVulkanFence *fence;
296
297     /* *INDENT-OFF* */
298     submit_info = (VkSubmitInfo) {
299         .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
300         .pNext = NULL,
301         .waitSemaphoreCount = 0,
302         .pWaitSemaphores = NULL,
303         .pWaitDstStageMask = &stages,
304         .commandBufferCount = 1,
305         .pCommandBuffers = &cmd_buf->cmd,
306         .signalSemaphoreCount = 0,
307         .pSignalSemaphores = NULL
308     };
309     /* *INDENT-ON* */
310
311     fence = gst_vulkan_device_create_fence (raw->download->device, &error);
312     if (!fence)
313       goto error;
314
315     gst_vulkan_queue_submit_lock (raw->download->queue);
316     err =
317         vkQueueSubmit (raw->download->queue->queue, 1, &submit_info,
318         GST_VULKAN_FENCE_FENCE (fence));
319     gst_vulkan_queue_submit_unlock (raw->download->queue);
320     if (gst_vulkan_error_to_g_error (err, &error, "vkQueueSubmit") < 0)
321       goto error;
322
323     gst_vulkan_trash_list_add (raw->trash_list,
324         gst_vulkan_trash_list_acquire (raw->trash_list, fence,
325             gst_vulkan_trash_mini_object_unref,
326             GST_MINI_OBJECT_CAST (cmd_buf)));
327     gst_vulkan_fence_unref (fence);
328   }
329
330   /* XXX: STALL!
331    * Need to have the buffer gst_memory_map() wait for this fence before
332    * allowing access */
333   gst_vulkan_trash_list_wait (raw->trash_list, -1);
334   gst_vulkan_trash_list_gc (raw->trash_list);
335
336   ret = GST_FLOW_OK;
337
338 out:
339   return ret;
340
341 unlock_error:
342   if (cmd_buf) {
343     gst_vulkan_command_buffer_unlock (cmd_buf);
344     gst_vulkan_command_buffer_unref (cmd_buf);
345   }
346 error:
347   if (error) {
348     GST_WARNING_OBJECT (raw->download, "Error: %s", error->message);
349     g_clear_error (&error);
350   }
351   gst_clear_buffer (outbuf);
352   ret = GST_FLOW_ERROR;
353   goto out;
354 }
355
356 static void
357 _image_to_raw_free (gpointer impl)
358 {
359   struct ImageToRawDownload *raw = impl;
360
361   if (raw->pool) {
362     if (raw->pool_active) {
363       gst_buffer_pool_set_active (raw->pool, FALSE);
364     }
365     raw->pool_active = FALSE;
366     gst_object_unref (raw->pool);
367     raw->pool = NULL;
368   }
369
370   if (raw->cmd_pool) {
371     gst_object_unref (raw->cmd_pool);
372     raw->cmd_pool = NULL;
373   }
374
375   gst_object_unref (raw->trash_list);
376   raw->trash_list = NULL;
377
378   g_free (impl);
379 }
380
381 static GstStaticCaps _image_to_raw_in_templ =
382 GST_STATIC_CAPS ("video/x-raw(" GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE ")");
383 static GstStaticCaps _image_to_raw_out_templ = GST_STATIC_CAPS ("video/x-raw");
384
385 static const struct DownloadMethod image_to_raw_download = {
386   "VulkanImageToRaw",
387   &_image_to_raw_in_templ,
388   &_image_to_raw_out_templ,
389   _image_to_raw_new_impl,
390   _image_to_raw_transform_caps,
391   _image_to_raw_set_caps,
392   _image_to_raw_propose_allocation,
393   _image_to_raw_perform,
394   _image_to_raw_free,
395 };
396
397 static const struct DownloadMethod *download_methods[] = {
398   &image_to_raw_download,
399 };
400
401 static GstCaps *
402 _get_input_template_caps (void)
403 {
404   GstCaps *ret = NULL;
405   gint i;
406
407   /* FIXME: cache this and invalidate on changes to download_methods */
408   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
409     GstCaps *template = gst_static_caps_get (download_methods[i]->in_template);
410     ret = ret == NULL ? template : gst_caps_merge (ret, template);
411   }
412
413   ret = gst_caps_simplify (ret);
414
415   return ret;
416 }
417
418 static GstCaps *
419 _get_output_template_caps (void)
420 {
421   GstCaps *ret = NULL;
422   gint i;
423
424   /* FIXME: cache this and invalidate on changes to download_methods */
425   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
426     GstCaps *template = gst_static_caps_get (download_methods[i]->out_template);
427     ret = ret == NULL ? template : gst_caps_merge (ret, template);
428   }
429
430   ret = gst_caps_simplify (ret);
431
432   return ret;
433 }
434
435 static void gst_vulkan_download_finalize (GObject * object);
436
437 static gboolean gst_vulkan_download_query (GstBaseTransform * bt,
438     GstPadDirection direction, GstQuery * query);
439 static void gst_vulkan_download_set_context (GstElement * element,
440     GstContext * context);
441 static GstStateChangeReturn gst_vulkan_download_change_state (GstElement *
442     element, GstStateChange transition);
443
444 static gboolean gst_vulkan_download_set_caps (GstBaseTransform * bt,
445     GstCaps * in_caps, GstCaps * out_caps);
446 static GstCaps *gst_vulkan_download_transform_caps (GstBaseTransform * bt,
447     GstPadDirection direction, GstCaps * caps, GstCaps * filter);
448 static gboolean gst_vulkan_download_propose_allocation (GstBaseTransform * bt,
449     GstQuery * decide_query, GstQuery * query);
450 static gboolean gst_vulkan_download_decide_allocation (GstBaseTransform * bt,
451     GstQuery * query);
452 static GstFlowReturn gst_vulkan_download_transform (GstBaseTransform * bt,
453     GstBuffer * inbuf, GstBuffer * outbuf);
454 static GstFlowReturn gst_vulkan_download_prepare_output_buffer (GstBaseTransform
455     * bt, GstBuffer * inbuf, GstBuffer ** outbuf);
456
457 enum
458 {
459   PROP_0,
460 };
461
462 enum
463 {
464   SIGNAL_0,
465   LAST_SIGNAL
466 };
467
468 /* static guint gst_vulkan_download_signals[LAST_SIGNAL] = { 0 }; */
469
470 #define gst_vulkan_download_parent_class parent_class
471 G_DEFINE_TYPE_WITH_CODE (GstVulkanDownload, gst_vulkan_download,
472     GST_TYPE_BASE_TRANSFORM, GST_DEBUG_CATEGORY_INIT (gst_debug_vulkan_download,
473         "vulkandownload", 0, "Vulkan Downloader"));
474 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (vulkandownload, "vulkandownload",
475     GST_RANK_NONE, GST_TYPE_VULKAN_DOWNLOAD, vulkan_element_init (plugin));
476
477 static void
478 gst_vulkan_download_class_init (GstVulkanDownloadClass * klass)
479 {
480   GObjectClass *gobject_class;
481   GstElementClass *gstelement_class;
482   GstBaseTransformClass *gstbasetransform_class;
483
484   gobject_class = (GObjectClass *) klass;
485   gstelement_class = (GstElementClass *) klass;
486   gstbasetransform_class = (GstBaseTransformClass *) klass;
487
488   gst_element_class_set_metadata (gstelement_class, "Vulkan Downloader",
489       "Filter/Video", "A Vulkan data downloader",
490       "Matthew Waters <matthew@centricular.com>");
491
492   {
493     GstCaps *caps;
494
495     caps = _get_input_template_caps ();
496     gst_element_class_add_pad_template (gstelement_class,
497         gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
498     gst_caps_unref (caps);
499
500     caps = _get_output_template_caps ();
501     gst_element_class_add_pad_template (gstelement_class,
502         gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps));
503     gst_caps_unref (caps);
504   }
505
506   gobject_class->finalize = gst_vulkan_download_finalize;
507
508   gstelement_class->change_state = gst_vulkan_download_change_state;
509   gstelement_class->set_context = gst_vulkan_download_set_context;
510   gstbasetransform_class->query = GST_DEBUG_FUNCPTR (gst_vulkan_download_query);
511   gstbasetransform_class->set_caps = gst_vulkan_download_set_caps;
512   gstbasetransform_class->transform_caps = gst_vulkan_download_transform_caps;
513   gstbasetransform_class->propose_allocation =
514       gst_vulkan_download_propose_allocation;
515   gstbasetransform_class->decide_allocation =
516       gst_vulkan_download_decide_allocation;
517   gstbasetransform_class->transform = gst_vulkan_download_transform;
518   gstbasetransform_class->prepare_output_buffer =
519       gst_vulkan_download_prepare_output_buffer;
520 }
521
522 static void
523 gst_vulkan_download_init (GstVulkanDownload * vk_download)
524 {
525   guint i, n;
526
527   n = G_N_ELEMENTS (download_methods);
528   vk_download->download_impls = g_malloc (sizeof (gpointer) * n);
529   for (i = 0; i < n; i++) {
530     vk_download->download_impls[i] =
531         download_methods[i]->new_impl (vk_download);
532   }
533 }
534
535 static void
536 gst_vulkan_download_finalize (GObject * object)
537 {
538   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (object);
539   guint i;
540
541   gst_caps_replace (&vk_download->in_caps, NULL);
542   gst_caps_replace (&vk_download->out_caps, NULL);
543
544   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
545     download_methods[i]->free (vk_download->download_impls[i]);
546   }
547   g_free (vk_download->download_impls);
548   vk_download->download_impls = NULL;
549
550   G_OBJECT_CLASS (parent_class)->finalize (object);
551 }
552
553 static gboolean
554 gst_vulkan_download_query (GstBaseTransform * bt, GstPadDirection direction,
555     GstQuery * query)
556 {
557   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (bt);
558
559   switch (GST_QUERY_TYPE (query)) {
560     case GST_QUERY_CONTEXT:{
561       if (gst_vulkan_handle_context_query (GST_ELEMENT (vk_download), query,
562               NULL, vk_download->instance, vk_download->device))
563         return TRUE;
564
565       if (gst_vulkan_queue_handle_context_query (GST_ELEMENT (vk_download),
566               query, vk_download->queue))
567         return TRUE;
568
569       break;
570     }
571     default:
572       break;
573   }
574
575   return GST_BASE_TRANSFORM_CLASS (parent_class)->query (bt, direction, query);
576 }
577
578 static void
579 gst_vulkan_download_set_context (GstElement * element, GstContext * context)
580 {
581   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (element);
582
583   gst_vulkan_handle_set_context (element, context, NULL,
584       &vk_download->instance);
585
586   GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
587 }
588
589 struct choose_data
590 {
591   GstVulkanDownload *download;
592   GstVulkanQueue *queue;
593 };
594
595 static gboolean
596 _choose_queue (GstVulkanDevice * device, GstVulkanQueue * queue,
597     struct choose_data *data)
598 {
599   guint flags =
600       device->physical_device->queue_family_props[queue->family].queueFlags;
601
602   if ((flags & VK_QUEUE_GRAPHICS_BIT) != 0) {
603     if (data->queue)
604       gst_object_unref (data->queue);
605     data->queue = gst_object_ref (queue);
606     return FALSE;
607   }
608
609   return TRUE;
610 }
611
612 static GstVulkanQueue *
613 _find_graphics_queue (GstVulkanDownload * download)
614 {
615   struct choose_data data;
616
617   data.download = download;
618   data.queue = NULL;
619
620   gst_vulkan_device_foreach_queue (download->device,
621       (GstVulkanDeviceForEachQueueFunc) _choose_queue, &data);
622
623   return data.queue;
624 }
625
626 static GstStateChangeReturn
627 gst_vulkan_download_change_state (GstElement * element,
628     GstStateChange transition)
629 {
630   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (element);
631   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
632
633   GST_DEBUG ("changing state: %s => %s",
634       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
635       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
636
637   switch (transition) {
638     case GST_STATE_CHANGE_NULL_TO_READY:
639       break;
640     case GST_STATE_CHANGE_READY_TO_PAUSED:
641       if (!gst_vulkan_ensure_element_data (element, NULL,
642               &vk_download->instance)) {
643         GST_ELEMENT_ERROR (vk_download, RESOURCE, NOT_FOUND,
644             ("Failed to retrieve vulkan instance"), (NULL));
645         return GST_STATE_CHANGE_FAILURE;
646       }
647       if (!gst_vulkan_device_run_context_query (GST_ELEMENT (vk_download),
648               &vk_download->device)) {
649         GError *error = NULL;
650         GST_DEBUG_OBJECT (vk_download,
651             "No device retrieved from peer elements");
652         if (!(vk_download->device =
653                 gst_vulkan_instance_create_device (vk_download->instance,
654                     &error))) {
655           GST_ELEMENT_ERROR (vk_download, RESOURCE, NOT_FOUND,
656               ("Failed to create vulkan device"), ("%s",
657                   error ? error->message : ""));
658           g_clear_error (&error);
659           return GST_STATE_CHANGE_FAILURE;
660         }
661       }
662
663       if (!gst_vulkan_queue_run_context_query (GST_ELEMENT (vk_download),
664               &vk_download->queue)) {
665         GST_DEBUG_OBJECT (vk_download, "No queue retrieved from peer elements");
666         vk_download->queue = _find_graphics_queue (vk_download);
667       }
668       if (!vk_download->queue) {
669         GST_ELEMENT_ERROR (vk_download, RESOURCE, NOT_FOUND,
670             ("Failed to create/retrieve vulkan queue"), (NULL));
671         return GST_STATE_CHANGE_FAILURE;
672       }
673       break;
674     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
675       break;
676     default:
677       break;
678   }
679
680   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
681   if (ret == GST_STATE_CHANGE_FAILURE)
682     return ret;
683
684   switch (transition) {
685     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
686       break;
687     case GST_STATE_CHANGE_PAUSED_TO_READY:
688       if (vk_download->queue)
689         gst_object_unref (vk_download->queue);
690       vk_download->queue = NULL;
691       if (vk_download->device)
692         gst_object_unref (vk_download->device);
693       vk_download->device = NULL;
694       if (vk_download->instance)
695         gst_object_unref (vk_download->instance);
696       vk_download->instance = NULL;
697       break;
698     case GST_STATE_CHANGE_READY_TO_NULL:
699       break;
700     default:
701       break;
702   }
703
704   return ret;
705 }
706
707 static GstCaps *
708 gst_vulkan_download_transform_caps (GstBaseTransform * bt,
709     GstPadDirection direction, GstCaps * caps, GstCaps * filter)
710 {
711   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (bt);
712   GstCaps *result, *tmp;
713   gint i;
714
715   tmp = gst_caps_new_empty ();
716
717   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
718     GstCaps *tmp2;
719     GstCaps *templ;
720
721     if (direction == GST_PAD_SINK) {
722       templ = gst_static_caps_get (download_methods[i]->in_template);
723     } else {
724       templ = gst_static_caps_get (download_methods[i]->out_template);
725     }
726     if (!gst_caps_can_intersect (caps, templ)) {
727       gst_caps_unref (templ);
728       continue;
729     }
730     gst_caps_unref (templ);
731
732     tmp2 = download_methods[i]->transform_caps (vk_download->download_impls[i],
733         direction, caps);
734
735     if (tmp2)
736       tmp = gst_caps_merge (tmp, tmp2);
737   }
738
739   if (filter) {
740     result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
741     gst_caps_unref (tmp);
742   } else {
743     result = tmp;
744   }
745
746   return result;
747 }
748
749 static gboolean
750 gst_vulkan_download_set_caps (GstBaseTransform * bt, GstCaps * in_caps,
751     GstCaps * out_caps)
752 {
753   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (bt);
754   gboolean found_method = FALSE;
755   guint i;
756
757   gst_caps_replace (&vk_download->in_caps, in_caps);
758   gst_caps_replace (&vk_download->out_caps, out_caps);
759
760   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
761     GstCaps *templ;
762
763     templ = gst_static_caps_get (download_methods[i]->in_template);
764     if (!gst_caps_can_intersect (in_caps, templ)) {
765       gst_caps_unref (templ);
766       continue;
767     }
768     gst_caps_unref (templ);
769
770     templ = gst_static_caps_get (download_methods[i]->out_template);
771     if (!gst_caps_can_intersect (out_caps, templ)) {
772       gst_caps_unref (templ);
773       continue;
774     }
775     gst_caps_unref (templ);
776
777     if (!download_methods[i]->set_caps (vk_download->download_impls[i], in_caps,
778             out_caps))
779       continue;
780
781     GST_LOG_OBJECT (bt, "downloader %s accepted caps in: %" GST_PTR_FORMAT
782         " out: %" GST_PTR_FORMAT, download_methods[i]->name, in_caps, out_caps);
783
784     vk_download->current_impl = i;
785     found_method = TRUE;
786     break;
787   }
788
789   GST_DEBUG_OBJECT (bt,
790       "set caps in: %" GST_PTR_FORMAT " out: %" GST_PTR_FORMAT, in_caps,
791       out_caps);
792
793   return found_method;
794 }
795
796 static gboolean
797 gst_vulkan_download_propose_allocation (GstBaseTransform * bt,
798     GstQuery * decide_query, GstQuery * query)
799 {
800   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (bt);
801   guint i;
802
803   for (i = 0; i < G_N_ELEMENTS (download_methods); i++) {
804     GstCaps *templ;
805
806     templ = gst_static_caps_get (download_methods[i]->in_template);
807     if (!gst_caps_can_intersect (vk_download->in_caps, templ)) {
808       gst_caps_unref (templ);
809       continue;
810     }
811     gst_caps_unref (templ);
812
813     templ = gst_static_caps_get (download_methods[i]->out_template);
814     if (!gst_caps_can_intersect (vk_download->out_caps, templ)) {
815       gst_caps_unref (templ);
816       continue;
817     }
818     gst_caps_unref (templ);
819
820     download_methods[i]->propose_allocation (vk_download->download_impls[i],
821         decide_query, query);
822   }
823
824   return TRUE;
825 }
826
827 static gboolean
828 gst_vulkan_download_decide_allocation (GstBaseTransform * bt, GstQuery * query)
829 {
830   return TRUE;
831 }
832
833 static gboolean
834 _download_find_method (GstVulkanDownload * vk_download)
835 {
836   vk_download->current_impl++;
837
838   if (vk_download->current_impl >= G_N_ELEMENTS (download_methods))
839     return FALSE;
840
841   GST_DEBUG_OBJECT (vk_download, "attempting download with downloader %s",
842       download_methods[vk_download->current_impl]->name);
843
844   return TRUE;
845 }
846
847 static GstFlowReturn
848 gst_vulkan_download_prepare_output_buffer (GstBaseTransform * bt,
849     GstBuffer * inbuf, GstBuffer ** outbuf)
850 {
851   GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (bt);
852   GstVulkanDownload *vk_download = GST_VULKAN_DOWNLOAD (bt);
853   GstFlowReturn ret;
854
855 restart:
856   {
857     gpointer method_impl;
858     const struct DownloadMethod *method;
859
860     method = download_methods[vk_download->current_impl];
861     method_impl = vk_download->download_impls[vk_download->current_impl];
862
863     ret = method->perform (method_impl, inbuf, outbuf);
864     if (ret != GST_FLOW_OK) {
865     next_method:
866       if (!_download_find_method (vk_download)) {
867         GST_ELEMENT_ERROR (bt, RESOURCE, NOT_FOUND,
868             ("Could not find suitable downloader"), (NULL));
869         return GST_FLOW_ERROR;
870       }
871
872       method = download_methods[vk_download->current_impl];
873       method_impl = vk_download->download_impls[vk_download->current_impl];
874       if (!method->set_caps (method_impl, vk_download->in_caps,
875               vk_download->out_caps))
876         /* try the next method */
877         goto next_method;
878
879       /* try the downloading with the next method */
880       goto restart;
881     }
882   }
883
884   if (ret == GST_FLOW_OK) {
885     /* basetransform doesn't unref if they're the same */
886     if (inbuf != *outbuf)
887       bclass->copy_metadata (bt, inbuf, *outbuf);
888   }
889
890   return ret;
891 }
892
893 static GstFlowReturn
894 gst_vulkan_download_transform (GstBaseTransform * bt, GstBuffer * inbuf,
895     GstBuffer * outbuf)
896 {
897   return GST_FLOW_OK;
898 }