653a59f26aa0298dea4463f73ee778c449f64021
[platform/upstream/gstreamer.git] / gst-libs / gst / vulkan / gstvkimagememory.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstvkimagememory.h"
26
27 /**
28  * SECTION:vkimagememory
29  * @title: GstVulkanImageMemory
30  * @short_description: memory subclass for Vulkan image memory
31  * @see_also: #GstMemory, #GstAllocator
32  *
33  * GstVulkanImageMemory is a #GstMemory subclass providing support for the
34  * mapping of Vulkan device memory.
35  */
36
37 #define GST_CAT_DEFUALT GST_CAT_VULKAN_IMAGE_MEMORY
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFUALT);
39
40 static GstAllocator *_vulkan_image_memory_allocator;
41
42 /**
43  * gst_vulkan_format_from_video_info: (skip)
44  * @v_info: the #GstVideoInfo
45  * @plane: the plane
46  *
47  * Returns: the VkFormat to use for @v_format and @plane
48  *
49  * Since: 1.18
50  */
51 VkFormat
52 gst_vulkan_format_from_video_info (GstVideoInfo * v_info, guint plane)
53 {
54   guint n_plane_components;
55
56   switch (GST_VIDEO_INFO_FORMAT (v_info)) {
57     case GST_VIDEO_FORMAT_RGBx:
58     case GST_VIDEO_FORMAT_RGBA:
59       if (GST_VIDEO_INFO_COLORIMETRY (v_info).transfer ==
60           GST_VIDEO_TRANSFER_SRGB)
61         return VK_FORMAT_R8G8B8A8_UNORM;
62       else
63         return VK_FORMAT_R8G8B8A8_SRGB;
64     case GST_VIDEO_FORMAT_BGRx:
65     case GST_VIDEO_FORMAT_BGRA:
66       if (GST_VIDEO_INFO_COLORIMETRY (v_info).transfer ==
67           GST_VIDEO_TRANSFER_SRGB)
68         return VK_FORMAT_B8G8R8A8_UNORM;
69       else
70         return VK_FORMAT_B8G8R8A8_SRGB;
71     case GST_VIDEO_FORMAT_xRGB:
72     case GST_VIDEO_FORMAT_ARGB:
73     case GST_VIDEO_FORMAT_xBGR:
74     case GST_VIDEO_FORMAT_ABGR:
75       if (GST_VIDEO_INFO_COLORIMETRY (v_info).transfer ==
76           GST_VIDEO_TRANSFER_SRGB)
77         n_plane_components = 4;
78       else
79         return VK_FORMAT_UNDEFINED;
80     case GST_VIDEO_FORMAT_AYUV:
81       n_plane_components = 4;
82       break;
83     case GST_VIDEO_FORMAT_RGB:
84       return VK_FORMAT_R8G8B8_UNORM;
85     case GST_VIDEO_FORMAT_BGR:
86       return VK_FORMAT_B8G8R8_UNORM;
87       break;
88     case GST_VIDEO_FORMAT_RGB16:
89       return VK_FORMAT_R5G6B5_UNORM_PACK16;
90     case GST_VIDEO_FORMAT_BGR16:
91       return VK_FORMAT_B5G6R5_UNORM_PACK16;
92     case GST_VIDEO_FORMAT_GRAY16_BE:
93     case GST_VIDEO_FORMAT_GRAY16_LE:
94     case GST_VIDEO_FORMAT_YUY2:
95     case GST_VIDEO_FORMAT_UYVY:
96       n_plane_components = 2;
97       break;
98     case GST_VIDEO_FORMAT_NV12:
99     case GST_VIDEO_FORMAT_NV21:
100       n_plane_components = plane == 0 ? 1 : 2;
101       break;
102     case GST_VIDEO_FORMAT_GRAY8:
103     case GST_VIDEO_FORMAT_Y444:
104     case GST_VIDEO_FORMAT_Y42B:
105     case GST_VIDEO_FORMAT_Y41B:
106     case GST_VIDEO_FORMAT_I420:
107     case GST_VIDEO_FORMAT_YV12:
108       n_plane_components = 1;
109       break;
110     default:
111       n_plane_components = 4;
112       g_assert_not_reached ();
113       break;
114   }
115
116   switch (n_plane_components) {
117     case 4:
118       return VK_FORMAT_R8G8B8A8_UNORM;
119     case 2:
120       return VK_FORMAT_R8G8_UNORM;
121     case 1:
122       return VK_FORMAT_R8_UNORM;
123     default:
124       g_assert_not_reached ();
125       return VK_FORMAT_R8G8B8A8_UNORM;
126   }
127 }
128
129 static gboolean
130 _create_info_from_args (VkImageCreateInfo * info, VkFormat format, gsize width,
131     gsize height, VkImageTiling tiling, VkImageUsageFlags usage)
132 {
133   /* FIXME: validate these */
134
135   /* *INDENT-OFF* */
136   *info = (VkImageCreateInfo) {
137       .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
138       .pNext = NULL,
139       .flags = 0,
140       .imageType = VK_IMAGE_TYPE_2D,
141       .format = format,
142       .extent = (VkExtent3D) { width, height, 1 },
143       .mipLevels = 1,
144       .arrayLayers = 1,
145       .samples = VK_SAMPLE_COUNT_1_BIT,
146       .tiling = tiling,
147       .usage = usage,
148       .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
149       .queueFamilyIndexCount = 0,
150       .pQueueFamilyIndices = NULL,
151       .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
152   };
153   /* *INDENT-ON* */
154
155   return TRUE;
156 }
157
158 static void
159 _vk_image_mem_init (GstVulkanImageMemory * mem, GstAllocator * allocator,
160     GstMemory * parent, GstVulkanDevice * device, VkImageUsageFlags usage,
161     GstAllocationParams * params, gsize size, gpointer user_data,
162     GDestroyNotify notify)
163 {
164   gsize align = gst_memory_alignment, offset = 0, maxsize = size;
165   GstMemoryFlags flags = 0;
166
167   if (params) {
168     flags = params->flags;
169     align |= params->align;
170     offset = params->prefix;
171     maxsize += params->prefix + params->padding + align;
172   }
173
174   gst_memory_init (GST_MEMORY_CAST (mem), flags, allocator, parent, maxsize,
175       align, offset, size);
176
177   mem->device = gst_object_ref (device);
178   mem->barrier.parent.type = GST_VULKAN_BARRIER_TYPE_IMAGE;
179   mem->barrier.parent.pipeline_stages = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
180   mem->barrier.parent.access_flags = 0;
181   mem->barrier.image_layout = VK_IMAGE_LAYOUT_UNDEFINED;
182   /* *INDENT-OFF* */
183   mem->barrier.subresource_range = (VkImageSubresourceRange) {
184           .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
185           .baseMipLevel = 0,
186           .levelCount = 1,
187           .baseArrayLayer = 0,
188           .layerCount = 1,
189   };
190   /* *INDENT-ON* */
191   mem->usage = usage;
192   mem->wrapped = FALSE;
193   mem->notify = notify;
194   mem->user_data = user_data;
195
196   g_mutex_init (&mem->lock);
197
198   mem->views = g_ptr_array_new ();
199   mem->outstanding_views = g_ptr_array_new ();
200
201   GST_CAT_DEBUG (GST_CAT_VULKAN_IMAGE_MEMORY,
202       "new Vulkan Image memory:%p size:%" G_GSIZE_FORMAT, mem, maxsize);
203 }
204
205 static GstVulkanImageMemory *
206 _vk_image_mem_new_alloc (GstAllocator * allocator, GstMemory * parent,
207     GstVulkanDevice * device, VkFormat format, gsize width, gsize height,
208     VkImageTiling tiling, VkImageUsageFlags usage,
209     VkMemoryPropertyFlags mem_prop_flags, gpointer user_data,
210     GDestroyNotify notify)
211 {
212   GstVulkanImageMemory *mem = NULL;
213   GstAllocationParams params = { 0, };
214   VkImageCreateInfo image_info;
215   VkPhysicalDevice gpu;
216   GError *error = NULL;
217   guint32 type_idx;
218   VkImage image;
219   VkResult err;
220
221   gpu = gst_vulkan_device_get_physical_device (device);
222   if (!_create_info_from_args (&image_info, format, width, height, tiling,
223           usage)) {
224     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
225     goto error;
226   }
227
228   err = vkCreateImage (device->device, &image_info, NULL, &image);
229   if (gst_vulkan_error_to_g_error (err, &error, "vkCreateImage") < 0)
230     goto vk_error;
231
232   mem = g_new0 (GstVulkanImageMemory, 1);
233
234   vkGetImageMemoryRequirements (device->device, image, &mem->requirements);
235
236   _vk_image_mem_init (mem, allocator, parent, device, usage, &params,
237       mem->requirements.size, user_data, notify);
238   mem->create_info = image_info;
239   mem->image = image;
240
241   err = vkGetPhysicalDeviceImageFormatProperties (gpu, format, VK_IMAGE_TYPE_2D,
242       tiling, usage, 0, &mem->format_properties);
243   if (gst_vulkan_error_to_g_error (err, &error,
244           "vkGetPhysicalDeviceImageFormatProperties") < 0)
245     goto vk_error;
246
247   if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device,
248           mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx))
249     goto error;
250
251   if ((mem->requirements.alignment & (mem->requirements.alignment - 1)) != 0) {
252     g_set_error_literal (&error, GST_VULKAN_ERROR, GST_VULKAN_FAILED,
253         "Vulkan implementation requires unsupported non-power-of 2 memory alignment");
254     goto error;
255   }
256   params.align = mem->requirements.alignment - 1;
257   mem->vk_mem = (GstVulkanMemory *) gst_vulkan_memory_alloc (device, type_idx,
258       &params, mem->requirements.size, mem_prop_flags);
259   if (!mem->vk_mem)
260     goto error;
261
262   err = vkBindImageMemory (device->device, image, mem->vk_mem->mem_ptr, 0);
263   if (gst_vulkan_error_to_g_error (err, &error, "vkBindImageMemory") < 0)
264     goto vk_error;
265
266   return mem;
267
268 vk_error:
269   {
270     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
271         "Failed to allocate image memory %s", error->message);
272     g_clear_error (&error);
273     goto error;
274   }
275
276 error:
277   {
278     if (mem)
279       gst_memory_unref ((GstMemory *) mem);
280     return NULL;
281   }
282 }
283
284 static GstVulkanImageMemory *
285 _vk_image_mem_new_wrapped (GstAllocator * allocator, GstMemory * parent,
286     GstVulkanDevice * device, VkImage image, VkFormat format, gsize width,
287     gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
288     gpointer user_data, GDestroyNotify notify)
289 {
290   GstVulkanImageMemory *mem = g_new0 (GstVulkanImageMemory, 1);
291   GstAllocationParams params = { 0, };
292   VkPhysicalDevice gpu;
293   GError *error = NULL;
294   VkResult err;
295
296   gpu = gst_vulkan_device_get_physical_device (device);
297   mem->image = image;
298
299   vkGetImageMemoryRequirements (device->device, mem->image, &mem->requirements);
300
301   /* XXX: assumes alignment is a power of 2 */
302   params.align = mem->requirements.alignment - 1;
303   params.flags = GST_MEMORY_FLAG_NOT_MAPPABLE;
304   _vk_image_mem_init (mem, allocator, parent, device, usage, &params,
305       mem->requirements.size, user_data, notify);
306   mem->wrapped = TRUE;
307
308   if (!_create_info_from_args (&mem->create_info, format, width, height, tiling,
309           usage)) {
310     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
311     goto error;
312   }
313
314   err = vkGetPhysicalDeviceImageFormatProperties (gpu, format, VK_IMAGE_TYPE_2D,
315       tiling, usage, 0, &mem->format_properties);
316   if (gst_vulkan_error_to_g_error (err, &error,
317           "vkGetPhysicalDeviceImageFormatProperties") < 0)
318     goto vk_error;
319
320   return mem;
321
322 vk_error:
323   {
324     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
325         "Failed to allocate image memory %s", error->message);
326     g_clear_error (&error);
327     goto error;
328   }
329
330 error:
331   {
332     gst_memory_unref ((GstMemory *) mem);
333     return NULL;
334   }
335 }
336
337 static gpointer
338 _vk_image_mem_map_full (GstVulkanImageMemory * mem, GstMapInfo * info,
339     gsize size)
340 {
341   GstMapInfo *vk_map_info;
342
343   /* FIXME: possible layout transformation needed */
344   g_mutex_lock (&mem->lock);
345
346   if (!mem->vk_mem) {
347     g_mutex_unlock (&mem->lock);
348     return NULL;
349   }
350
351   vk_map_info = g_new0 (GstMapInfo, 1);
352   info->user_data[0] = vk_map_info;
353   if (!gst_memory_map ((GstMemory *) mem->vk_mem, vk_map_info, info->flags)) {
354     g_free (vk_map_info);
355     g_mutex_unlock (&mem->lock);
356     return NULL;
357   }
358   g_mutex_unlock (&mem->lock);
359
360   return vk_map_info->data;
361 }
362
363 static void
364 _vk_image_mem_unmap_full (GstVulkanImageMemory * mem, GstMapInfo * info)
365 {
366   g_mutex_lock (&mem->lock);
367   gst_memory_unmap ((GstMemory *) mem->vk_mem, info->user_data[0]);
368   g_mutex_unlock (&mem->lock);
369
370   g_free (info->user_data[0]);
371 }
372
373 static GstMemory *
374 _vk_image_mem_copy (GstVulkanImageMemory * src, gssize offset, gssize size)
375 {
376   return NULL;
377 }
378
379 static GstMemory *
380 _vk_image_mem_share (GstVulkanImageMemory * mem, gssize offset, gssize size)
381 {
382   return NULL;
383 }
384
385 static gboolean
386 _vk_image_mem_is_span (GstVulkanImageMemory * mem1, GstVulkanImageMemory * mem2,
387     gsize * offset)
388 {
389   return FALSE;
390 }
391
392 static GstMemory *
393 _vk_image_mem_alloc (GstAllocator * allocator, gsize size,
394     GstAllocationParams * params)
395 {
396   g_critical ("Subclass should override GstAllocatorClass::alloc() function");
397
398   return NULL;
399 }
400
401 static void
402 _free_view (GstVulkanImageView * view, gpointer unused)
403 {
404   gst_vulkan_image_view_unref (view);
405 }
406
407 static void
408 _vk_image_mem_free (GstAllocator * allocator, GstMemory * memory)
409 {
410   GstVulkanImageMemory *mem = (GstVulkanImageMemory *) memory;
411
412   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "freeing image memory:%p "
413       "id:%" G_GUINT64_FORMAT, mem, (guint64) mem->image);
414
415   g_warn_if_fail (mem->outstanding_views > 0);
416   g_ptr_array_unref (mem->outstanding_views);
417
418   g_ptr_array_foreach (mem->views, (GFunc) _free_view, NULL);
419   g_ptr_array_unref (mem->views);
420
421   if (mem->image && !mem->wrapped)
422     vkDestroyImage (mem->device->device, mem->image, NULL);
423
424   if (mem->vk_mem)
425     gst_memory_unref ((GstMemory *) mem->vk_mem);
426
427   if (mem->notify)
428     mem->notify (mem->user_data);
429
430   gst_object_unref (mem->device);
431
432   g_free (mem);
433 }
434
435 /**
436  * gst_vulkan_image_memory_alloc:
437  * @device: a #GstVulkanDevice
438  * @format: the VkFormat for the new image
439  * @width: width for the new image
440  * @height: height for the new image
441  * @tiling: tiling for the new image
442  * @usage: usage flags for the new image
443  * @mem_prop_flags: VkDeviceMemory property flags for the new image
444  *
445  * Allocated a new #GstVulkanImageMemory.
446  *
447  * Returns: a #GstMemory object backed by a vulkan device memory
448  *
449  * Since: 1.18
450  */
451 GstMemory *
452 gst_vulkan_image_memory_alloc (GstVulkanDevice * device, VkFormat format,
453     gsize width, gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
454     VkMemoryPropertyFlags mem_prop_flags)
455 {
456   GstVulkanImageMemory *mem;
457
458   mem = _vk_image_mem_new_alloc (_vulkan_image_memory_allocator, NULL, device,
459       format, width, height, tiling, usage, mem_prop_flags, NULL, NULL);
460
461   return (GstMemory *) mem;
462 }
463
464 /**
465  * gst_vulkan_image_memory_wrapped:
466  * @device: a #GstVulkanDevice
467  * @image: a VkImage
468  * @format: the VkFormat for @image
469  * @width: width of @image
470  * @height: height of @image
471  * @tiling: tiling of @image
472  * @usage: usage flags of @image
473  * @user_data: (nullable): user data for @notify
474  * @notify: a #DestroyNotify when @image is no longer needed
475  *
476  * Return: a new #GstVulkanImageMemory wrapping @image
477  *
478  * Since: 1.18
479  */
480 GstMemory *
481 gst_vulkan_image_memory_wrapped (GstVulkanDevice * device, VkImage image,
482     VkFormat format, gsize width, gsize height, VkImageTiling tiling,
483     VkImageUsageFlags usage, gpointer user_data, GDestroyNotify notify)
484 {
485   GstVulkanImageMemory *mem;
486
487   mem = _vk_image_mem_new_wrapped (_vulkan_image_memory_allocator, NULL, device,
488       image, format, width, height, tiling, usage, user_data, notify);
489
490   return (GstMemory *) mem;
491 }
492
493 /**
494  * gst_vulkan_image_memory_get_width:
495  * @image: a #GstVulkanImageMemory
496  *
497  * Return: the width of @image
498  *
499  * Since: 1.18
500  */
501 guint32
502 gst_vulkan_image_memory_get_width (GstVulkanImageMemory * image)
503 {
504   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
505       0);
506
507   return image->create_info.extent.width;
508 }
509
510 /**
511  * gst_vulkan_image_memory_get_height:
512  * @image: a #GstVulkanImageMemory
513  *
514  * Return: the height of @image
515  *
516  * Since: 1.18
517  */
518 guint32
519 gst_vulkan_image_memory_get_height (GstVulkanImageMemory * image)
520 {
521   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
522       0);
523
524   return image->create_info.extent.height;
525 }
526
527 static gint
528 find_view_index_unlocked (GstVulkanImageMemory * image,
529     GstVulkanImageView * view)
530 {
531   guint index;
532
533   if (!g_ptr_array_find (image->views, view, &index))
534     return -1;
535
536   return (gint) index;
537 }
538
539 extern void
540 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
541     GstVulkanImageView * view);
542
543 void
544 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
545     GstVulkanImageView * view)
546 {
547   guint index;
548
549   g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
550
551   g_mutex_lock (&image->lock);
552   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "image %p removing view %p",
553       image, view);
554   if (g_ptr_array_find (image->outstanding_views, view, &index)) {
555     /* really g_ptr_array_steal_index_fast() but that requires glib 2.58 */
556     g_ptr_array_remove_index_fast (image->outstanding_views, index);
557     g_ptr_array_add (image->views, view);
558   } else {
559     g_warning ("GstVulkanImageMemory:%p attempt to remove a view %p "
560         "that we do not own", image, view);
561   }
562   gst_clear_mini_object ((GstMiniObject **) & view->image);
563   g_mutex_unlock (&image->lock);
564 }
565
566 /**
567  * gst_vulkan_image_memory_add_view:
568  * @image: a #GstVulkanImageMemory
569  * @view: a #GstVulkanImageView
570  *
571  * Return: the height of @image
572  *
573  * Since: 1.18
574  */
575 void
576 gst_vulkan_image_memory_add_view (GstVulkanImageMemory * image,
577     GstVulkanImageView * view)
578 {
579   g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
580   g_return_if_fail (view != NULL);
581   g_return_if_fail (image == view->image);
582
583   g_mutex_lock (&image->lock);
584   if (find_view_index_unlocked (image, view) != -1) {
585     g_warn_if_reached ();
586     g_mutex_unlock (&image->lock);
587     return;
588   }
589   g_ptr_array_add (image->outstanding_views, view);
590   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p adding view %p",
591       image, view);
592
593   g_mutex_unlock (&image->lock);
594 }
595
596 struct view_data
597 {
598   GstVulkanImageMemory *img;
599   GstVulkanImageMemoryFindViewFunc find_func;
600   gpointer find_data;
601 };
602
603 static gboolean
604 find_view_func (GstVulkanImageView * view, gpointer user_data)
605 {
606   struct view_data *data = user_data;
607   GstVulkanImageMemory *previous;
608   gboolean ret;
609
610   previous = view->image;
611   view->image = data->img;
612
613   ret = data->find_func (view, data->find_data);
614
615   view->image = previous;
616
617   return ret;
618 }
619
620 /**
621  * gst_vulkan_image_memory_find_view:
622  * @image: a #GstVulkanImageMemory
623  * @find_func: #GstVulkanImageMemoryFindViewFunc to search with
624  * @data: user data to call @finc_func with
625  *
626  * Return: (transfer full): the first #GstVulkanImageView that @find_func
627  * returns %TRUE for, or %NULL
628  *
629  * Since: 1.18
630  */
631 GstVulkanImageView *
632 gst_vulkan_image_memory_find_view (GstVulkanImageMemory * image,
633     GstVulkanImageMemoryFindViewFunc find_func, gpointer data)
634 {
635   GstVulkanImageView *ret = NULL;
636   struct view_data view;
637   guint index;
638
639   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
640       NULL);
641   g_return_val_if_fail (find_func != NULL, NULL);
642
643   g_mutex_lock (&image->lock);
644   view.img = image;
645   view.find_func = find_func;
646   view.find_data = data;
647
648   if (g_ptr_array_find_with_equal_func (image->outstanding_views, &view,
649           (GEqualFunc) find_view_func, &index)) {
650     ret =
651         gst_vulkan_image_view_ref (g_ptr_array_index (image->outstanding_views,
652             index));
653   } else if (g_ptr_array_find_with_equal_func (image->views, &view,
654           (GEqualFunc) find_view_func, &index)) {
655     /* really g_ptr_array_steal_index_fast() but that requires glib 2.58 */
656     ret = g_ptr_array_remove_index_fast (image->views, index);
657     g_ptr_array_add (image->outstanding_views, ret);
658     ret->image = (GstVulkanImageMemory *) gst_memory_ref ((GstMemory *) image);
659   }
660
661   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p found view %p",
662       image, ret);
663   g_mutex_unlock (&image->lock);
664
665   return ret;
666 }
667
668 G_DEFINE_TYPE (GstVulkanImageMemoryAllocator, gst_vulkan_image_memory_allocator,
669     GST_TYPE_ALLOCATOR);
670
671 static void
672 gst_vulkan_image_memory_allocator_class_init (GstVulkanImageMemoryAllocatorClass
673     * klass)
674 {
675   GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
676
677   allocator_class->alloc = _vk_image_mem_alloc;
678   allocator_class->free = _vk_image_mem_free;
679 }
680
681 static void
682 gst_vulkan_image_memory_allocator_init (GstVulkanImageMemoryAllocator *
683     allocator)
684 {
685   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
686
687   alloc->mem_type = GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME;
688   alloc->mem_map_full = (GstMemoryMapFullFunction) _vk_image_mem_map_full;
689   alloc->mem_unmap_full = (GstMemoryUnmapFullFunction) _vk_image_mem_unmap_full;
690   alloc->mem_copy = (GstMemoryCopyFunction) _vk_image_mem_copy;
691   alloc->mem_share = (GstMemoryShareFunction) _vk_image_mem_share;
692   alloc->mem_is_span = (GstMemoryIsSpanFunction) _vk_image_mem_is_span;
693 }
694
695 /**
696  * gst_vulkan_image_memory_init_once:
697  *
698  * Initializes the Vulkan image memory allocator. It is safe to call this function
699  * multiple times.  This must be called before any other #GstVulkanImageMemory operation.
700  *
701  * Since: 1.18
702  */
703 void
704 gst_vulkan_image_memory_init_once (void)
705 {
706   static volatile gsize _init = 0;
707
708   if (g_once_init_enter (&_init)) {
709     GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_MEMORY, "vulkanimagememory",
710         0, "Vulkan Image Memory");
711
712     _vulkan_image_memory_allocator =
713         g_object_new (gst_vulkan_image_memory_allocator_get_type (), NULL);
714     gst_object_ref_sink (_vulkan_image_memory_allocator);
715
716     gst_allocator_register (GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME,
717         gst_object_ref (_vulkan_image_memory_allocator));
718     g_once_init_leave (&_init, 1);
719   }
720 }
721
722 /**
723  * gst_is_vulkan_image_memory:
724  * @mem: a #GstMemory
725  *
726  * Returns: whether the memory at @mem is a #GstVulkanImageMemory
727  *
728  * Since: 1.18
729  */
730 gboolean
731 gst_is_vulkan_image_memory (GstMemory * mem)
732 {
733   return mem != NULL && mem->allocator != NULL &&
734       g_type_is_a (G_OBJECT_TYPE (mem->allocator),
735       GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR);
736 }