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