3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
25 #include "gstvkimagememory.h"
28 * SECTION:vkimagememory
29 * @title: GstVulkanImageMemory
30 * @short_description: memory subclass for Vulkan image memory
31 * @see_also: #GstVulkanDevice, #GstMemory, #GstAllocator
33 * GstVulkanImageMemory is a #GstMemory subclass providing support for the
34 * mapping of Vulkan device memory.
37 #define GST_CAT_DEFUALT GST_CAT_VULKAN_IMAGE_MEMORY
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFUALT);
40 static GstAllocator *_vulkan_image_memory_allocator;
43 * gst_vulkan_format_from_video_info: (skip)
44 * @v_info: the #GstVideoInfo
47 * Returns: the VkFormat to use for @v_format and @plane
52 gst_vulkan_format_from_video_info (GstVideoInfo * v_info, guint plane)
54 guint n_plane_components;
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;
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;
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;
79 return VK_FORMAT_UNDEFINED;
80 case GST_VIDEO_FORMAT_AYUV:
81 n_plane_components = 4;
83 case GST_VIDEO_FORMAT_RGB:
84 return VK_FORMAT_R8G8B8_UNORM;
85 case GST_VIDEO_FORMAT_BGR:
86 return VK_FORMAT_B8G8R8_UNORM;
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;
98 case GST_VIDEO_FORMAT_NV12:
99 case GST_VIDEO_FORMAT_NV21:
100 n_plane_components = plane == 0 ? 1 : 2;
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;
111 n_plane_components = 4;
112 g_assert_not_reached ();
116 switch (n_plane_components) {
118 return VK_FORMAT_R8G8B8A8_UNORM;
120 return VK_FORMAT_R8G8_UNORM;
122 return VK_FORMAT_R8_UNORM;
124 g_assert_not_reached ();
125 return VK_FORMAT_R8G8B8A8_UNORM;
130 _create_info_from_args (VkImageCreateInfo * info, VkFormat format, gsize width,
131 gsize height, VkImageTiling tiling, VkImageUsageFlags usage)
133 /* FIXME: validate these */
136 *info = (VkImageCreateInfo) {
137 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
140 .imageType = VK_IMAGE_TYPE_2D,
142 .extent = (VkExtent3D) { width, height, 1 },
145 .samples = VK_SAMPLE_COUNT_1_BIT,
148 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
149 .queueFamilyIndexCount = 0,
150 .pQueueFamilyIndices = NULL,
151 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
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)
164 gsize align = gst_memory_alignment, offset = 0, maxsize = size;
165 GstMemoryFlags flags = 0;
168 flags = params->flags;
169 align |= params->align;
170 offset = params->prefix;
171 maxsize += params->prefix + params->padding + align;
174 gst_memory_init (GST_MEMORY_CAST (mem), flags, allocator, parent, maxsize,
175 align, offset, size);
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;
183 mem->barrier.subresource_range = (VkImageSubresourceRange) {
184 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
192 mem->wrapped = FALSE;
193 mem->notify = notify;
194 mem->user_data = user_data;
196 g_mutex_init (&mem->lock);
198 mem->views = g_ptr_array_new ();
199 mem->outstanding_views = g_ptr_array_new ();
201 GST_CAT_DEBUG (GST_CAT_VULKAN_IMAGE_MEMORY,
202 "new Vulkan Image memory:%p size:%" G_GSIZE_FORMAT, mem, maxsize);
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)
214 GstVulkanImageMemory *mem = NULL;
215 GstAllocationParams params = { 0, };
216 VkImageCreateInfo image_info;
217 VkPhysicalDevice gpu;
218 GError *error = NULL;
223 gpu = gst_vulkan_device_get_physical_device (device);
224 if (!_create_info_from_args (&image_info, format, width, height, tiling,
226 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
230 err = vkCreateImage (device->device, &image_info, NULL, &image);
231 if (gst_vulkan_error_to_g_error (err, &error, "vkCreateImage") < 0)
234 mem = g_new0 (GstVulkanImageMemory, 1);
236 vkGetImageMemoryRequirements (device->device, image, &mem->requirements);
238 gst_vulkan_image_memory_init (mem, allocator, parent, device, usage, ¶ms,
239 mem->requirements.size, user_data, notify);
240 mem->create_info = image_info;
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)
249 if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device,
250 mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx))
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");
258 params.align = mem->requirements.alignment - 1;
259 mem->vk_mem = (GstVulkanMemory *) gst_vulkan_memory_alloc (device, type_idx,
260 ¶ms, mem->requirements.size, mem_prop_flags);
264 err = vkBindImageMemory (device->device, image, mem->vk_mem->mem_ptr, 0);
265 if (gst_vulkan_error_to_g_error (err, &error, "vkBindImageMemory") < 0)
272 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
273 "Failed to allocate image memory %s", error->message);
274 g_clear_error (&error);
281 gst_memory_unref ((GstMemory *) mem);
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)
292 GstVulkanImageMemory *mem = g_new0 (GstVulkanImageMemory, 1);
293 GstAllocationParams params = { 0, };
294 VkPhysicalDevice gpu;
295 GError *error = NULL;
298 gpu = gst_vulkan_device_get_physical_device (device);
301 vkGetImageMemoryRequirements (device->device, mem->image, &mem->requirements);
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, ¶ms,
307 mem->requirements.size, user_data, notify);
310 if (!_create_info_from_args (&mem->create_info, format, width, height, tiling,
312 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
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)
326 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
327 "Failed to allocate image memory %s", error->message);
328 g_clear_error (&error);
334 gst_memory_unref ((GstMemory *) mem);
340 _vk_image_mem_map_full (GstVulkanImageMemory * mem, GstMapInfo * info,
343 GstMapInfo *vk_map_info;
345 /* FIXME: possible layout transformation needed */
346 g_mutex_lock (&mem->lock);
349 g_mutex_unlock (&mem->lock);
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);
360 g_mutex_unlock (&mem->lock);
362 return vk_map_info->data;
366 _vk_image_mem_unmap_full (GstVulkanImageMemory * mem, GstMapInfo * info)
368 g_mutex_lock (&mem->lock);
369 gst_memory_unmap ((GstMemory *) mem->vk_mem, info->user_data[0]);
370 g_mutex_unlock (&mem->lock);
372 g_free (info->user_data[0]);
376 _vk_image_mem_copy (GstVulkanImageMemory * src, gssize offset, gssize size)
382 _vk_image_mem_share (GstVulkanImageMemory * mem, gssize offset, gssize size)
388 _vk_image_mem_is_span (GstVulkanImageMemory * mem1, GstVulkanImageMemory * mem2,
395 _vk_image_mem_alloc (GstAllocator * allocator, gsize size,
396 GstAllocationParams * params)
398 g_critical ("Subclass should override GstAllocatorClass::alloc() function");
404 _free_view (GstVulkanImageView * view, gpointer unused)
406 gst_vulkan_image_view_unref (view);
410 _vk_image_mem_free (GstAllocator * allocator, GstMemory * memory)
412 GstVulkanImageMemory *mem = (GstVulkanImageMemory *) memory;
414 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "freeing image memory:%p "
415 "id:%" G_GUINT64_FORMAT, mem, (guint64) mem->image);
417 g_warn_if_fail (mem->outstanding_views->len == 0);
418 g_ptr_array_unref (mem->outstanding_views);
420 g_ptr_array_foreach (mem->views, (GFunc) _free_view, NULL);
421 g_ptr_array_unref (mem->views);
423 if (mem->image && !mem->wrapped)
424 vkDestroyImage (mem->device->device, mem->image, NULL);
427 gst_memory_unref ((GstMemory *) mem->vk_mem);
430 mem->notify (mem->user_data);
432 gst_object_unref (mem->device);
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
447 * Allocated a new #GstVulkanImageMemory.
449 * Returns: a #GstMemory object backed by a vulkan device memory
454 gst_vulkan_image_memory_alloc (GstVulkanDevice * device, VkFormat format,
455 gsize width, gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
456 VkMemoryPropertyFlags mem_prop_flags)
458 GstVulkanImageMemory *mem;
460 mem = _vk_image_mem_new_alloc (_vulkan_image_memory_allocator, NULL, device,
461 format, width, height, tiling, usage, mem_prop_flags, NULL, NULL);
463 return (GstMemory *) mem;
467 * gst_vulkan_image_memory_wrapped:
468 * @device: a #GstVulkanDevice
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
478 * Return: a new #GstVulkanImageMemory wrapping @image
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)
487 GstVulkanImageMemory *mem;
489 mem = _vk_image_mem_new_wrapped (_vulkan_image_memory_allocator, NULL, device,
490 image, format, width, height, tiling, usage, user_data, notify);
492 return (GstMemory *) mem;
496 * gst_vulkan_image_memory_get_width:
497 * @image: a #GstVulkanImageMemory
499 * Return: the width of @image
504 gst_vulkan_image_memory_get_width (GstVulkanImageMemory * image)
506 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
509 return image->create_info.extent.width;
513 * gst_vulkan_image_memory_get_height:
514 * @image: a #GstVulkanImageMemory
516 * Return: the height of @image
521 gst_vulkan_image_memory_get_height (GstVulkanImageMemory * image)
523 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
526 return image->create_info.extent.height;
530 find_view_index_unlocked (GstVulkanImageMemory * image,
531 GstVulkanImageView * view)
535 if (!g_ptr_array_find (image->views, view, &index))
542 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
543 GstVulkanImageView * view);
546 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
547 GstVulkanImageView * view)
551 g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
552 g_return_if_fail (image == view->image);
554 g_mutex_lock (&image->lock);
555 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "image %p removing view %p",
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);
562 g_warning ("GstVulkanImageMemory:%p attempt to remove a view %p "
563 "that we do not own", image, view);
566 g_mutex_unlock (&image->lock);
567 gst_memory_unref ((GstMemory *) image);
571 * gst_vulkan_image_memory_add_view:
572 * @image: a #GstVulkanImageMemory
573 * @view: a #GstVulkanImageView
578 gst_vulkan_image_memory_add_view (GstVulkanImageMemory * image,
579 GstVulkanImageView * view)
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);
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);
591 g_ptr_array_add (image->outstanding_views, view);
592 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p adding view %p",
595 g_mutex_unlock (&image->lock);
600 GstVulkanImageMemory *img;
601 GstVulkanImageMemoryFindViewFunc find_func;
606 find_view_func (GstVulkanImageView * view, gpointer user_data)
608 struct view_data *data = user_data;
609 GstVulkanImageMemory *previous;
612 previous = view->image;
613 view->image = data->img;
615 ret = data->find_func (view, data->find_data);
617 view->image = previous;
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
628 * Return: (transfer full): the first #GstVulkanImageView that @find_func
629 * returns %TRUE for, or %NULL
634 gst_vulkan_image_memory_find_view (GstVulkanImageMemory * image,
635 GstVulkanImageMemoryFindViewFunc find_func, gpointer user_data)
637 GstVulkanImageView *ret = NULL;
638 struct view_data view;
641 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
643 g_return_val_if_fail (find_func != NULL, NULL);
645 g_mutex_lock (&image->lock);
647 view.find_func = find_func;
648 view.find_data = user_data;
650 if (g_ptr_array_find_with_equal_func (image->outstanding_views, &view,
651 (GEqualFunc) find_view_func, &index)) {
653 gst_vulkan_image_view_ref (g_ptr_array_index (image->outstanding_views,
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);
663 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p found view %p",
665 g_mutex_unlock (&image->lock);
670 G_DEFINE_TYPE (GstVulkanImageMemoryAllocator, gst_vulkan_image_memory_allocator,
674 gst_vulkan_image_memory_allocator_class_init (GstVulkanImageMemoryAllocatorClass
677 GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
679 allocator_class->alloc = _vk_image_mem_alloc;
680 allocator_class->free = _vk_image_mem_free;
684 gst_vulkan_image_memory_allocator_init (GstVulkanImageMemoryAllocator *
687 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
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;
698 * gst_vulkan_image_memory_init_once:
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.
706 gst_vulkan_image_memory_init_once (void)
708 static gsize _init = 0;
710 if (g_once_init_enter (&_init)) {
711 GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_MEMORY, "vulkanimagememory",
712 0, "Vulkan Image Memory");
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);
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);
725 * gst_is_vulkan_image_memory:
728 * Returns: whether the memory at @mem is a #GstVulkanImageMemory
733 gst_is_vulkan_image_memory (GstMemory * mem)
735 return mem != NULL && mem->allocator != NULL &&
736 g_type_is_a (G_OBJECT_TYPE (mem->allocator),
737 GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR);