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: #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 _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)
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);
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)
212 GstVulkanImageMemory *mem = NULL;
213 GstAllocationParams params = { 0, };
214 VkImageCreateInfo image_info;
215 VkPhysicalDevice gpu;
216 GError *error = NULL;
221 gpu = gst_vulkan_device_get_physical_device (device);
222 if (!_create_info_from_args (&image_info, format, width, height, tiling,
224 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
228 err = vkCreateImage (device->device, &image_info, NULL, &image);
229 if (gst_vulkan_error_to_g_error (err, &error, "vkCreateImage") < 0)
232 mem = g_new0 (GstVulkanImageMemory, 1);
234 vkGetImageMemoryRequirements (device->device, image, &mem->requirements);
236 _vk_image_mem_init (mem, allocator, parent, device, usage, ¶ms,
237 mem->requirements.size, user_data, notify);
238 mem->create_info = image_info;
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)
247 if (!gst_vulkan_memory_find_memory_type_index_with_type_properties (device,
248 mem->requirements.memoryTypeBits, mem_prop_flags, &type_idx))
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");
256 params.align = mem->requirements.alignment - 1;
257 mem->vk_mem = (GstVulkanMemory *) gst_vulkan_memory_alloc (device, type_idx,
258 ¶ms, mem->requirements.size, mem_prop_flags);
262 err = vkBindImageMemory (device->device, image, mem->vk_mem->mem_ptr, 0);
263 if (gst_vulkan_error_to_g_error (err, &error, "vkBindImageMemory") < 0)
270 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
271 "Failed to allocate image memory %s", error->message);
272 g_clear_error (&error);
279 gst_memory_unref ((GstMemory *) mem);
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)
290 GstVulkanImageMemory *mem = g_new0 (GstVulkanImageMemory, 1);
291 GstAllocationParams params = { 0, };
292 VkPhysicalDevice gpu;
293 GError *error = NULL;
296 gpu = gst_vulkan_device_get_physical_device (device);
299 vkGetImageMemoryRequirements (device->device, mem->image, &mem->requirements);
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, ¶ms,
305 mem->requirements.size, user_data, notify);
308 if (!_create_info_from_args (&mem->create_info, format, width, height, tiling,
310 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY, "Incorrect image parameters");
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)
324 GST_CAT_ERROR (GST_CAT_VULKAN_IMAGE_MEMORY,
325 "Failed to allocate image memory %s", error->message);
326 g_clear_error (&error);
332 gst_memory_unref ((GstMemory *) mem);
338 _vk_image_mem_map_full (GstVulkanImageMemory * mem, GstMapInfo * info,
341 GstMapInfo *vk_map_info;
343 /* FIXME: possible layout transformation needed */
344 g_mutex_lock (&mem->lock);
347 g_mutex_unlock (&mem->lock);
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);
358 g_mutex_unlock (&mem->lock);
360 return vk_map_info->data;
364 _vk_image_mem_unmap_full (GstVulkanImageMemory * mem, GstMapInfo * info)
366 g_mutex_lock (&mem->lock);
367 gst_memory_unmap ((GstMemory *) mem->vk_mem, info->user_data[0]);
368 g_mutex_unlock (&mem->lock);
370 g_free (info->user_data[0]);
374 _vk_image_mem_copy (GstVulkanImageMemory * src, gssize offset, gssize size)
380 _vk_image_mem_share (GstVulkanImageMemory * mem, gssize offset, gssize size)
386 _vk_image_mem_is_span (GstVulkanImageMemory * mem1, GstVulkanImageMemory * mem2,
393 _vk_image_mem_alloc (GstAllocator * allocator, gsize size,
394 GstAllocationParams * params)
396 g_critical ("Subclass should override GstAllocatorClass::alloc() function");
402 _free_view (GstVulkanImageView * view, gpointer unused)
404 gst_vulkan_image_view_unref (view);
408 _vk_image_mem_free (GstAllocator * allocator, GstMemory * memory)
410 GstVulkanImageMemory *mem = (GstVulkanImageMemory *) memory;
412 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "freeing image memory:%p "
413 "id:%" G_GUINT64_FORMAT, mem, (guint64) mem->image);
415 g_warn_if_fail (mem->outstanding_views > 0);
416 g_ptr_array_unref (mem->outstanding_views);
418 g_ptr_array_foreach (mem->views, (GFunc) _free_view, NULL);
419 g_ptr_array_unref (mem->views);
421 if (mem->image && !mem->wrapped)
422 vkDestroyImage (mem->device->device, mem->image, NULL);
425 gst_memory_unref ((GstMemory *) mem->vk_mem);
428 mem->notify (mem->user_data);
430 gst_object_unref (mem->device);
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
445 * Allocated a new #GstVulkanImageMemory.
447 * Returns: a #GstMemory object backed by a vulkan device memory
452 gst_vulkan_image_memory_alloc (GstVulkanDevice * device, VkFormat format,
453 gsize width, gsize height, VkImageTiling tiling, VkImageUsageFlags usage,
454 VkMemoryPropertyFlags mem_prop_flags)
456 GstVulkanImageMemory *mem;
458 mem = _vk_image_mem_new_alloc (_vulkan_image_memory_allocator, NULL, device,
459 format, width, height, tiling, usage, mem_prop_flags, NULL, NULL);
461 return (GstMemory *) mem;
465 * gst_vulkan_image_memory_wrapped:
466 * @device: a #GstVulkanDevice
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
476 * Return: a new #GstVulkanImageMemory wrapping @image
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)
485 GstVulkanImageMemory *mem;
487 mem = _vk_image_mem_new_wrapped (_vulkan_image_memory_allocator, NULL, device,
488 image, format, width, height, tiling, usage, user_data, notify);
490 return (GstMemory *) mem;
494 * gst_vulkan_image_memory_get_width:
495 * @image: a #GstVulkanImageMemory
497 * Return: the width of @image
502 gst_vulkan_image_memory_get_width (GstVulkanImageMemory * image)
504 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
507 return image->create_info.extent.width;
511 * gst_vulkan_image_memory_get_height:
512 * @image: a #GstVulkanImageMemory
514 * Return: the height of @image
519 gst_vulkan_image_memory_get_height (GstVulkanImageMemory * image)
521 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
524 return image->create_info.extent.height;
528 find_view_index_unlocked (GstVulkanImageMemory * image,
529 GstVulkanImageView * view)
533 if (!g_ptr_array_find (image->views, view, &index))
540 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
541 GstVulkanImageView * view);
544 gst_vulkan_image_memory_release_view (GstVulkanImageMemory * image,
545 GstVulkanImageView * view)
549 g_return_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)));
551 g_mutex_lock (&image->lock);
552 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "image %p removing view %p",
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);
559 g_warning ("GstVulkanImageMemory:%p attempt to remove a view %p "
560 "that we do not own", image, view);
562 gst_clear_mini_object ((GstMiniObject **) & view->image);
563 g_mutex_unlock (&image->lock);
567 * gst_vulkan_image_memory_add_view:
568 * @image: a #GstVulkanImageMemory
569 * @view: a #GstVulkanImageView
571 * Return: the height of @image
576 gst_vulkan_image_memory_add_view (GstVulkanImageMemory * image,
577 GstVulkanImageView * view)
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);
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);
589 g_ptr_array_add (image->outstanding_views, view);
590 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p adding view %p",
593 g_mutex_unlock (&image->lock);
598 GstVulkanImageMemory *img;
599 GstVulkanImageMemoryFindViewFunc find_func;
604 find_view_func (GstVulkanImageView * view, gpointer user_data)
606 struct view_data *data = user_data;
607 GstVulkanImageMemory *previous;
610 previous = view->image;
611 view->image = data->img;
613 ret = data->find_func (view, data->find_data);
615 view->image = previous;
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
626 * Return: (transfer full): the first #GstVulkanImageView that @find_func
627 * returns %TRUE for, or %NULL
632 gst_vulkan_image_memory_find_view (GstVulkanImageMemory * image,
633 GstVulkanImageMemoryFindViewFunc find_func, gpointer data)
635 GstVulkanImageView *ret = NULL;
636 struct view_data view;
639 g_return_val_if_fail (gst_is_vulkan_image_memory (GST_MEMORY_CAST (image)),
641 g_return_val_if_fail (find_func != NULL, NULL);
643 g_mutex_lock (&image->lock);
645 view.find_func = find_func;
646 view.find_data = data;
648 if (g_ptr_array_find_with_equal_func (image->outstanding_views, &view,
649 (GEqualFunc) find_view_func, &index)) {
651 gst_vulkan_image_view_ref (g_ptr_array_index (image->outstanding_views,
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);
661 GST_CAT_TRACE (GST_CAT_VULKAN_IMAGE_MEMORY, "Image %p found view %p",
663 g_mutex_unlock (&image->lock);
668 G_DEFINE_TYPE (GstVulkanImageMemoryAllocator, gst_vulkan_image_memory_allocator,
672 gst_vulkan_image_memory_allocator_class_init (GstVulkanImageMemoryAllocatorClass
675 GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
677 allocator_class->alloc = _vk_image_mem_alloc;
678 allocator_class->free = _vk_image_mem_free;
682 gst_vulkan_image_memory_allocator_init (GstVulkanImageMemoryAllocator *
685 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
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;
696 * gst_vulkan_image_memory_init_once:
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.
704 gst_vulkan_image_memory_init_once (void)
706 static volatile gsize _init = 0;
708 if (g_once_init_enter (&_init)) {
709 GST_DEBUG_CATEGORY_INIT (GST_CAT_VULKAN_IMAGE_MEMORY, "vulkanimagememory",
710 0, "Vulkan Image Memory");
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);
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);
723 * gst_is_vulkan_image_memory:
726 * Returns: whether the memory at @mem is a #GstVulkanImageMemory
731 gst_is_vulkan_image_memory (GstMemory * mem)
733 return mem != NULL && mem->allocator != NULL &&
734 g_type_is_a (G_OBJECT_TYPE (mem->allocator),
735 GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR);