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 #ifndef _VK_IMAGE_MEMORY_H_
22 #define _VK_IMAGE_MEMORY_H_
23 
24 #include <gst/gst.h>
25 #include <gst/gstallocator.h>
26 #include <gst/gstmemory.h>
27 
28 #include <gst/video/video.h>
29 
30 #include <vk.h>
31 
32 G_BEGIN_DECLS
33 
34 #define GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR (gst_vulkan_image_memory_allocator_get_type())
35 GType gst_vulkan_image_memory_allocator_get_type(void);
36 
37 #define GST_IS_VULKAN_IMAGE_MEMORY_ALLOCATOR(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR))
38 #define GST_IS_VULKAN_IMAGE_MEMORY_ALLOCATOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_VULKAN_IMAGE_MEMORY_ALLOCATOR))
39 #define GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_VULKAN_MEMORY_ALLOCATOR, GstVulkanImageMemoryAllocatorClass))
40 #define GST_VULKAN_IMAGE_MEMORY_ALLOCATOR(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_VULKAN_MEMORY_ALLOCATOR, GstVulkanImageMemoryAllocator))
41 #define GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_VULKAN_MEMORY_ALLOCATOR, GstVulkanImageMemoryAllocatorClass))
42 #define GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_CAST(obj)            ((GstVulkanImageMemoryAllocator *)(obj))
43 
44 #define GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME "VulkanImage"
45 #define GST_CAPS_FEATURE_MEMORY_VULKAN_IMAGE "memory:" GST_VULKAN_IMAGE_MEMORY_ALLOCATOR_NAME
46 
47 struct _GstVulkanImageMemory
48 {
49   GstMemory parent;
50 
51   GstVulkanDevice * device;
52 
53   VkImage image;
54   VkImageLayout image_layout;
55   VkImageView view;
56   GstVulkanMemory *vk_mem;
57 
58   VkImageCreateInfo create_info;
59   VkMemoryRequirements requirements;
60   VkImageFormatProperties format_properties;
61   VkImageUsageFlags usage;
62 
63   GMutex lock;
64   gboolean wrapped;
65   GDestroyNotify notify;
66   gpointer user_data;
67 };
68 
69 /**
70  * GstVulkanImageMemoryAllocator
71  *
72  * Opaque #GstVulkanImageMemoryAllocator struct
73  */
74 struct _GstVulkanImageMemoryAllocator
75 {
76   GstAllocator parent;
77 };
78 
79 /**
80  * GstVulkanImageMemoryAllocatorClass:
81  *
82  * The #GstVulkanImageMemoryAllocatorClass only contains private data
83  */
84 struct _GstVulkanImageMemoryAllocatorClass
85 {
86   GstAllocatorClass parent_class;
87 };
88 
89 void            gst_vulkan_image_memory_init_once       (void);
90 gboolean        gst_is_vulkan_image_memory              (GstMemory * mem);
91 
92 GstMemory *     gst_vulkan_image_memory_alloc           (GstVulkanDevice * device,
93                                                          VkFormat format,
94                                                          gsize width,
95                                                          gsize height,
96                                                          VkImageTiling tiling,
97                                                          VkImageUsageFlags usage,
98                                                          VkMemoryPropertyFlags mem_prop_flags);
99 
100 GstMemory *     gst_vulkan_image_memory_wrapped         (GstVulkanDevice * device,
101                                                          VkImage image,
102                                                          VkFormat format,
103                                                          gsize width,
104                                                          gsize height,
105                                                          VkImageTiling tiling,
106                                                          VkImageUsageFlags usage,
107                                                          gpointer user_data,
108                                                          GDestroyNotify notify);
109 
110 gboolean        gst_vulkan_image_memory_set_layout      (GstVulkanImageMemory * vk_mem,
111                                                          VkImageLayout,
112                                                          VkImageMemoryBarrier * barrier);
113 
114 guint32         gst_vulkan_image_memory_get_width       (GstVulkanImageMemory * image);
115 guint32         gst_vulkan_image_memory_get_height      (GstVulkanImageMemory * image);
116 
117 VkFormat gst_vulkan_format_from_video_format (GstVideoFormat v_format,
118                                               guint plane);
119 
120 G_END_DECLS
121 
122 #endif /* _VK_IMAGE_MEMORY_H_ */
123