Move files from gst-plugins-bad into the "subprojects/gst-plugins-bad/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / 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: #GstVulkanDevice, #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 gboolean
159 gst_vulkan_image_memory_init (GstVulkanImageMemory * mem,
160     GstAllocator * allocator, GstMemory * parent, GstVulkanDevice * device,
161     VkImageUsageFlags usage, GstAllocationParams * params, gsize size,
162     gpointer user_data, 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   return TRUE;
205 }
206
207 static GstVulkanImageMemory *
208 _vk_image_mem_new_alloc (GstAllocator * allocator, GstMemory * parent,
209     GstVulkanDevice * device, VkFormat format, gsize width, gsize height,
210     VkImageTiling tiling, VkImageUsageFlags usage,
211     VkMemoryPropertyFlags mem_prop_flags, gpointer user_data,
212     GDestroyNotify notify)
213 {
214   GstVulkanImageMemory *mem = NULL;
215   GstAllocationParams params = { 0, };
216   VkImageCreateInfo image_info;
217   VkPhysicalDevice gpu;
218   GError *error = NULL;
219   guint32 type_idx;
220   VkImage image;
221   VkResult err;
222
223   gpu = gst_vulkan_device_get_physical_device (device);
224   if (!_create_info_from_args (&image_info, format, width, height, tiling,
225           usage)) {
226     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
227     goto error;
228   }
229
230   err = vkCreateImage (device->device, &image_info, NULL, &image);
231   if (gst_vulkan_error_to_g_error (err, &error, "vkCreateImage") < 0)
232     goto vk_error;
233
234   mem = g_new0 (GstVulkanImageMemory, 1);
235
236   vkGetImageMemoryRequirements (device->device, image, &mem->requirements);
237
238   gst_vulkan_image_memory_init (mem, allocator, parent, device, usage, &params,
239       mem->requirements.size, user_data, notify);
240   mem->create_info = image_info;
241   mem->image = image;
242
243   err = vkGetPhysicalDeviceImageFormatProperties (gpu, format, VK_IMAGE_TYPE_2D,
244       tiling, usage, 0, &mem->format_properties);
245   if (gst_vulkan_error_to_g_error (err, &error,
246           "vkGetPhysicalDeviceImageFormatProperties") < 0)
247     goto vk_error;
248
249   if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device,
250           mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx))
251     goto error;
252
253   if ((mem->requirements.alignment & (mem->requirements.alignment - 1)) != 0) {
254     g_set_error_literal (&error, GST_VULKAN_ERROR, GST_VULKAN_FAILED,
255         "Vulkan implementation requires unsupported non-power-of 2 memory alignment");
256     goto error;
257   }
258   params.align = mem->requirements.alignment - 1;
259   mem->vk_mem = (GstVulkanMemory *) gst_vulkan_memory_alloc (device, type_idx,
260       &params, mem->requirements.size, mem_prop_flags);
261   if (!mem->vk_mem)
262     goto error;
263
264   err = vkBindImageMemory (device->device, image, mem->vk_mem->mem_ptr, 0);
265   if (gst_vulkan_error_to_g_error (err, &error, "vkBindImageMemory") < 0)
266     goto vk_error;
267
268   return mem;
269
270 vk_error:
271   {
272     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
273         "Failed to allocate image memory %s", error->message);
274     g_clear_error (&error);
275     goto error;
276   }
277
278 error:
279   {
280     if (mem)
281       gst_memory_unref ((GstMemory *) mem);
282     return NULL;
283   }
284 }
285
286 static GstVulkanImageMemory *
287 _vk_image_mem_new_wrapped (GstAllocator * allocator, GstMemory * parent,
288     GstVulkanDevice * device, VkImage image, VkFormat format, gsize width,
289     gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
290     gpointer user_data, GDestroyNotify notify)
291 {
292   GstVulkanImageMemory *mem = g_new0 (GstVulkanImageMemory, 1);
293   GstAllocationParams params = { 0, };
294   VkPhysicalDevice gpu;
295   GError *error = NULL;
296   VkResult err;
297
298   gpu = gst_vulkan_device_get_physical_device (device);
299   mem->image = image;
300
301   vkGetImageMemoryRequirements (device->device, mem->image, &mem->requirements);
302
303   /* XXX: assumes alignment is a power of 2 */
304   params.align = mem->requirements.alignment - 1;
305   params.flags = GST_MEMORY_FLAG_NOT_MAPPABLE;
306   gst_vulkan_image_memory_init (mem, allocator, parent, device, usage, &params,
307       mem->requirements.size, user_data, notify);
308   mem->wrapped = TRUE;
309
310   if (!_create_info_from_args (&mem->create_info, format, width, height, tiling,
311           usage)) {
312     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
313     goto error;
314   }
315
316   err = vkGetPhysicalDeviceImageFormatProperties (gpu, format, VK_IMAGE_TYPE_2D,
317       tiling, usage, 0, &mem->format_properties);
318   if (gst_vulkan_error_to_g_error (err, &error,
319           "vkGetPhysicalDeviceImageFormatProperties") < 0)
320     goto vk_error;
321
322   return mem;
323
324 vk_error:
325   {
326     GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
327         "Failed to allocate image memory %s", error->message);
328     g_clear_error (&error);
329     goto error;
330   }
331
332 error:
333   {
334     gst_memory_unref ((GstMemory *) mem);
335     return NULL;
336   }
337 }
338
339 static gpointer
340 _vk_image_mem_map_full (GstVulkanImageMemory * mem, GstMapInfo * info,
341     gsize size)
342 {
343   GstMapInfo *vk_map_info;
344
345   /* FIXME: possible layout transformation needed */
346   g_mutex_lock (&mem->lock);
347
348   if (!mem->vk_mem) {
349     g_mutex_unlock (&mem->lock);
350     return NULL;
351   }
352
353   vk_map_info = g_new0 (GstMapInfo, 1);
354   info->user_data[0] = vk_map_info;
355   if (!gst_memory_map ((GstMemory *) mem->vk_mem, vk_map_info, info->flags)) {
356     g_free (vk_map_info);
357     g_mutex_unlock (&mem->lock);
358     return NULL;
359   }
360   g_mutex_unlock (&mem->lock);
361
362   return vk_map_info->data;
363 }
364
365 static void
366 _vk_image_mem_unmap_full (GstVulkanImageMemory * mem, GstMapInfo * info)
367 {
368   g_mutex_lock (&mem->lock);
369   gst_memory_unmap ((GstMemory *) mem->vk_mem, info->user_data[0]);
370   g_mutex_unlock (&mem->lock);
371
372   g_free (info->user_data[0]);
373 }
374
375 static GstMemory *
376 _vk_image_mem_copy (GstVulkanImageMemory * src, gssize offset, gssize size)
377 {
378   return NULL;
379 }
380
381 static GstMemory *
382 _vk_image_mem_share (GstVulkanImageMemory * mem, gssize offset, gssize size)
383 {
384   return NULL;
385 }
386
387 static gboolean
388 _vk_image_mem_is_span (GstVulkanImageMemory * mem1, GstVulkanImageMemory * mem2,
389     gsize * offset)
390 {
391   return FALSE;
392 }
393
394 static GstMemory *
395 _vk_image_mem_alloc (GstAllocator * allocator, gsize size,
396     GstAllocationParams * params)
397 {
398   g_critical ("Subclass should override GstAllocatorClass::alloc() function");
399
400   return NULL;
401 }
402
403 static void
404 _free_view (GstVulkanImageView * view, gpointer unused)
405 {
406   gst_vulkan_image_view_unref (view);
407 }
408
409 static void
410 _vk_image_mem_free (GstAllocator * allocator, GstMemory * memory)
411 {
412   GstVulkanImageMemory *mem = (GstVulkanImageMemory *) memory;
413
414   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "freeing image memory:%p "
415       "id:%" G_GUINT64_FORMAT, mem, (guint64) mem->image);
416
417   g_warn_if_fail (mem->outstanding_views->len == 0);
418   g_ptr_array_unref (mem->outstanding_views);
419
420   g_ptr_array_foreach (mem->views, (GFunc) _free_view, NULL);
421   g_ptr_array_unref (mem->views);
422
423   if (mem->image && !mem->wrapped)
424     vkDestroyImage (mem->device->device, mem->image, NULL);
425
426   if (mem->vk_mem)
427     gst_memory_unref ((GstMemory *) mem->vk_mem);
428
429   if (mem->notify)
430     mem->notify (mem->user_data);
431
432   gst_object_unref (mem->device);
433
434   g_free (mem);
435 }
436
437 /**
438  * gst_vulkan_image_memory_alloc:
439  * @device: a #GstVulkanDevice
440  * @format: the VkFormat for the new image
441  * @width: width for the new image
442  * @height: height for the new image
443  * @tiling: tiling for the new image
444  * @usage: usage flags for the new image
445  * @mem_prop_flags: VkDeviceMemory property flags for the new image
446  *
447  * Allocated a new #GstVulkanImageMemory.
448  *
449  * Returns: a #GstMemory object backed by a vulkan device memory
450  *
451  * Since: 1.18
452  */
453 GstMemory *
454 gst_vulkan_image_memory_alloc (GstVulkanDevice * device, VkFormat format,
455     gsize width, gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
456     VkMemoryPropertyFlags mem_prop_flags)
457 {
458   GstVulkanImageMemory *mem;
459
460   mem = _vk_image_mem_new_alloc (_vulkan_image_memory_allocator, NULL, device,
461       format, width, height, tiling, usage, mem_prop_flags, NULL, NULL);
462
463   return (GstMemory *) mem;
464 }
465
466 /**
467  * gst_vulkan_image_memory_wrapped:
468  * @device: a #GstVulkanDevice
469  * @image: a VkImage
470  * @format: the VkFormat for @image
471  * @width: width of @image
472  * @height: height of @image
473  * @tiling: tiling of @image
474  * @usage: usage flags of @image
475  * @user_data: (nullable): user data for @notify
476  * @notify: a #GDestroyNotify when @image is no longer needed
477  *
478  * Return: a new #GstVulkanImageMemory wrapping @image
479  *
480  * Since: 1.18
481  */
482 GstMemory *
483 gst_vulkan_image_memory_wrapped (GstVulkanDevice * device, VkImage image,
484     VkFormat format, gsize width, gsize height, VkImageTiling tiling,
485     VkImageUsageFlags usage, gpointer user_data, GDestroyNotify notify)
486 {
487   GstVulkanImageMemory *mem;
488
489   mem = _vk_image_mem_new_wrapped (_vulkan_image_memory_allocator, NULL, device,
490       image, format, width, height, tiling, usage, user_data, notify);
491
492   return (GstMemory *) mem;
493 }
494
495 /**
496  * gst_vulkan_image_memory_get_width:
497  * @image: a #GstVulkanImageMemory
498  *
499  * Return: the width of @image
500  *
501  * Since: 1.18
502  */
503 guint32
504 gst_vulkan_image_memory_get_width (GstVulkanImageMemory * image)
505 {
506   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
507       0);
508
509   return image->create_info.extent.width;
510 }
511
512 /**
513  * gst_vulkan_image_memory_get_height:
514  * @image: a #GstVulkanImageMemory
515  *
516  * Return: the height of @image
517  *
518  * Since: 1.18
519  */
520 guint32
521 gst_vulkan_image_memory_get_height (GstVulkanImageMemory * image)
522 {
523   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
524       0);
525
526   return image->create_info.extent.height;
527 }
528
529 static gint
530 find_view_index_unlocked (GstVulkanImageMemory * image,
531     GstVulkanImageView * view)
532 {
533   guint index;
534
535   if (!g_ptr_array_find (image->views, view, &index))
536     return -1;
537
538   return (gint) index;
539 }
540
541 extern void
542 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
543     GstVulkanImageView * view);
544
545 void
546 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
547     GstVulkanImageView * view)
548 {
549   guint index;
550
551   g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
552   g_return_if_fail (image == view->image);
553
554   g_mutex_lock (&image->lock);
555   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "image %p removing view %p",
556       image, view);
557   if (g_ptr_array_find (image->outstanding_views, view, &index)) {
558     /* really g_ptr_array_steal_index_fast() but that requires glib 2.58 */
559     g_ptr_array_remove_index_fast (image->outstanding_views, index);
560     g_ptr_array_add (image->views, view);
561   } else {
562     g_warning ("GstVulkanImageMemory:%p attempt to remove a view %p "
563         "that we do not own", image, view);
564   }
565   view->image = NULL;
566   g_mutex_unlock (&image->lock);
567   gst_memory_unref ((GstMemory *) image);
568 }
569
570 /**
571  * gst_vulkan_image_memory_add_view:
572  * @image: a #GstVulkanImageMemory
573  * @view: a #GstVulkanImageView
574  *
575  * Since: 1.18
576  */
577 void
578 gst_vulkan_image_memory_add_view (GstVulkanImageMemory * image,
579     GstVulkanImageView * view)
580 {
581   g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
582   g_return_if_fail (view != NULL);
583   g_return_if_fail (image == view->image);
584
585   g_mutex_lock (&image->lock);
586   if (find_view_index_unlocked (image, view) != -1) {
587     g_warn_if_reached ();
588     g_mutex_unlock (&image->lock);
589     return;
590   }
591   g_ptr_array_add (image->outstanding_views, view);
592   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p adding view %p",
593       image, view);
594
595   g_mutex_unlock (&image->lock);
596 }
597
598 struct view_data
599 {
600   GstVulkanImageMemory *img;
601   GstVulkanImageMemoryFindViewFunc find_func;
602   gpointer find_data;
603 };
604
605 static gboolean
606 find_view_func (GstVulkanImageView * view, gpointer user_data)
607 {
608   struct view_data *data = user_data;
609   GstVulkanImageMemory *previous;
610   gboolean ret;
611
612   previous = view->image;
613   view->image = data->img;
614
615   ret = data->find_func (view, data->find_data);
616
617   view->image = previous;
618
619   return ret;
620 }
621
622 /**
623  * gst_vulkan_image_memory_find_view:
624  * @image: a #GstVulkanImageMemory
625  * @find_func: (scope call): #GstVulkanImageMemoryFindViewFunc to search with
626  * @user_data: user data to call @finc_func with
627  *
628  * Return: (transfer full): the first #GstVulkanImageView that @find_func
629  * returns %TRUE for, or %NULL
630  *
631  * Since: 1.18
632  */
633 GstVulkanImageView *
634 gst_vulkan_image_memory_find_view (GstVulkanImageMemory * image,
635     GstVulkanImageMemoryFindViewFunc find_func, gpointer user_data)
636 {
637   GstVulkanImageView *ret = NULL;
638   struct view_data view;
639   guint index;
640
641   g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
642       NULL);
643   g_return_val_if_fail (find_func != NULL, NULL);
644
645   g_mutex_lock (&image->lock);
646   view.img = image;
647   view.find_func = find_func;
648   view.find_data = user_data;
649
650   if (g_ptr_array_find_with_equal_func (image->outstanding_views, &view,
651           (GEqualFunc) find_view_func, &index)) {
652     ret =
653         gst_vulkan_image_view_ref (g_ptr_array_index (image->outstanding_views,
654             index));
655   } else if (g_ptr_array_find_with_equal_func (image->views, &view,
656           (GEqualFunc) find_view_func, &index)) {
657     /* really g_ptr_array_steal_index_fast() but that requires glib 2.58 */
658     ret = g_ptr_array_remove_index_fast (image->views, index);
659     g_ptr_array_add (image->outstanding_views, ret);
660     ret->image = (GstVulkanImageMemory *) gst_memory_ref ((GstMemory *) image);
661   }
662
663   GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p found view %p",
664       image, ret);
665   g_mutex_unlock (&image->lock);
666
667   return ret;
668 }
669
670 G_DEFINE_TYPE (GstVulkanImageMemoryAllocator, gst_vulkan_image_memory_allocator,
671     GST_TYPE_ALLOCATOR);
672
673 static void
674 gst_vulkan_image_memory_allocator_class_init (GstVulkanImageMemoryAllocatorClass
675     * klass)
676 {
677   GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
678
679   allocator_class->alloc = _vk_image_mem_alloc;
680   allocator_class->free = _vk_image_mem_free;
681 }
682
683 static void
684 gst_vulkan_image_memory_allocator_init (GstVulkanImageMemoryAllocator *
685     allocator)
686 {
687   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
688
689   alloc->mem_type = GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME;
690   alloc->mem_map_full = (GstMemoryMapFullFunction) _vk_image_mem_map_full;
691   alloc->mem_unmap_full = (GstMemoryUnmapFullFunction) _vk_image_mem_unmap_full;
692   alloc->mem_copy = (GstMemoryCopyFunction) _vk_image_mem_copy;
693   alloc->mem_share = (GstMemoryShareFunction) _vk_image_mem_share;
694   alloc->mem_is_span = (GstMemoryIsSpanFunction) _vk_image_mem_is_span;
695 }
696
697 /**
698  * gst_vulkan_image_memory_init_once:
699  *
700  * Initializes the Vulkan image memory allocator. It is safe to call this function
701  * multiple times.  This must be called before any other #GstVulkanImageMemory operation.
702  *
703  * Since: 1.18
704  */
705 void
706 gst_vulkan_image_memory_init_once (void)
707 {
708   static gsize _init = 0;
709
710   if (g_once_init_enter (&_init)) {
711     GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_MEMORY, "vulkanimagememory",
712         0, "Vulkan Image Memory");
713
714     _vulkan_image_memory_allocator =
715         g_object_new (gst_vulkan_image_memory_allocator_get_type (), NULL);
716     gst_object_ref_sink (_vulkan_image_memory_allocator);
717
718     gst_allocator_register (GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME,
719         gst_object_ref (_vulkan_image_memory_allocator));
720     g_once_init_leave (&_init, 1);
721   }
722 }
723
724 /**
725  * gst_is_vulkan_image_memory:
726  * @mem: a #GstMemory
727  *
728  * Returns: whether the memory at @mem is a #GstVulkanImageMemory
729  *
730  * Since: 1.18
731  */
732 gboolean
733 gst_is_vulkan_image_memory (GstMemory * mem)
734 {
735   return mem != NULL && mem->allocator != NULL &&
736       g_type_is_a (G_OBJECT_TYPE (mem->allocator),
737       GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR);
738 }