/src/gstreamer/subprojects/gst-plugins-base/gst-libs/gst/video/gstvideodmabufpool.c
Line | Count | Source |
1 | | /* GStreamer video dmabuf pool |
2 | | * |
3 | | * Copyright (C) 2025 Collabora Ltd. |
4 | | * Author: Robert Mader <robert.mader@collabora.com> |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Library General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Library General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Library General Public |
17 | | * License along with this library; if not, write to the |
18 | | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
19 | | * Boston, MA 02110-1301, USA. |
20 | | */ |
21 | | |
22 | | /** |
23 | | * SECTION:gstvideodmabufpool |
24 | | * @title: GstVideoDmabufPool |
25 | | * @short_description: Pool for virtual memory backed dmabufs |
26 | | * @see_also: #GstUdmabufAllocator |
27 | | * |
28 | | * Using #GstUdmabufAllocator, setting defaults and implementing implicit sync. |
29 | | * |
30 | | * Since: 1.28 |
31 | | */ |
32 | | |
33 | | #ifdef HAVE_CONFIG_H |
34 | | #include "config.h" |
35 | | #endif |
36 | | |
37 | | #include "gstvideodmabufpool.h" |
38 | | #include "gstvideodmabufsynchandler.h" |
39 | | |
40 | | #include <gst/allocators/gstudmabufallocator.h> |
41 | | |
42 | | #ifdef HAVE_LINUX_DMA_BUF_H |
43 | | #include <linux/dma-buf.h> |
44 | | #include <sys/ioctl.h> |
45 | | #include <unistd.h> |
46 | | #endif |
47 | | |
48 | | /* This alignment is needed on many AMD GPUs and is known to work well across |
49 | | * many vendors/GPUs, see e.g. GstGLDMABufBufferPool. */ |
50 | 0 | #define UDMABUF_ALIGNMENT_MASK (256 - 1) |
51 | | |
52 | | struct _GstVideoDmabufPool |
53 | | { |
54 | | GstVideoBufferPool parent; |
55 | | GstVideoDmabufSyncHandler *sync_handler; |
56 | | }; |
57 | | |
58 | | GST_DEBUG_CATEGORY_STATIC (gst_video_dmabuf_pool_debug); |
59 | | #define GST_CAT_DEFAULT gst_video_dmabuf_pool_debug |
60 | | |
61 | 0 | G_DEFINE_TYPE_WITH_CODE (GstVideoDmabufPool, gst_video_dmabuf_pool, |
62 | 0 | GST_TYPE_VIDEO_BUFFER_POOL, |
63 | 0 | GST_DEBUG_CATEGORY_INIT (gst_video_dmabuf_pool_debug, "video-dmabuf-pool", |
64 | 0 | 0, "video dmabuf pool"); |
65 | 0 | ); |
66 | 0 |
|
67 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
68 | 0 |
|
69 | 0 | static gboolean |
70 | 0 | gst_video_dmabuf_pool_start (GstBufferPool * pool) |
71 | 0 | { |
72 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
73 | |
|
74 | 0 | if (!gst_video_dmabuf_sync_handler_start (self->sync_handler)) |
75 | 0 | return FALSE; |
76 | | |
77 | 0 | return |
78 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->start (pool); |
79 | 0 | } |
80 | | |
81 | | static gboolean |
82 | | gst_video_dmabuf_pool_stop (GstBufferPool * pool) |
83 | 0 | { |
84 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
85 | |
|
86 | 0 | if (!gst_video_dmabuf_sync_handler_stop (self->sync_handler)) |
87 | 0 | return FALSE; |
88 | | |
89 | 0 | return |
90 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->stop (pool); |
91 | 0 | } |
92 | | |
93 | | static void |
94 | | gst_video_dmabuf_pool_final_release_buffer (gpointer user_data, gpointer buffer) |
95 | 0 | { |
96 | 0 | GstBufferPool *pool = GST_BUFFER_POOL_CAST (user_data); |
97 | 0 | GstBuffer *buf = GST_BUFFER_CAST (buffer); |
98 | |
|
99 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->release_buffer |
100 | 0 | (pool, buf); |
101 | 0 | } |
102 | | |
103 | | static void |
104 | | gst_video_dmabuf_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer) |
105 | 0 | { |
106 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
107 | 0 | GstMemory *mems[GST_VIDEO_MAX_PLANES]; |
108 | 0 | guint n_mem; |
109 | |
|
110 | 0 | n_mem = gst_buffer_n_memory (buffer); |
111 | 0 | for (gint i = 0; i < n_mem; i++) |
112 | 0 | mems[i] = gst_buffer_peek_memory (buffer, i); |
113 | |
|
114 | 0 | gst_video_dmabuf_sync_handler_release_buffer (self->sync_handler, |
115 | 0 | (gpointer) buffer, mems, n_mem); |
116 | 0 | } |
117 | | |
118 | | static gboolean |
119 | | gst_video_dmabuf_pool_set_config (GstBufferPool * pool, GstStructure * config) |
120 | 0 | { |
121 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
122 | 0 | GstAllocator *allocator; |
123 | 0 | GstAllocationParams params; |
124 | 0 | GstVideoAlignment video_align = { 0 }; |
125 | 0 | gboolean config_updated = FALSE; |
126 | 0 | gboolean alignment_updated = FALSE; |
127 | 0 | gboolean res; |
128 | |
|
129 | 0 | gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms); |
130 | 0 | if (!GST_IS_DMABUF_ALLOCATOR (allocator) || |
131 | 0 | GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC)) { |
132 | 0 | GST_DEBUG_OBJECT (self, |
133 | 0 | "Allocator not a dmabuf allocator or having the CUSTOM_ALLOC flag set, trying to update to udmabuf"); |
134 | |
|
135 | 0 | allocator = gst_udmabuf_allocator_get (); |
136 | 0 | if (allocator) { |
137 | 0 | params.align |= UDMABUF_ALIGNMENT_MASK; |
138 | 0 | gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); |
139 | 0 | gst_object_unref (allocator); |
140 | 0 | config_updated = TRUE; |
141 | 0 | } else { |
142 | 0 | GST_ERROR_OBJECT (self, "udmabuf allocator not available"); |
143 | 0 | return FALSE; |
144 | 0 | } |
145 | 0 | } else if (params.align < UDMABUF_ALIGNMENT_MASK) { |
146 | 0 | GST_DEBUG_OBJECT (self, "updating allocator params"); |
147 | 0 | params.align |= UDMABUF_ALIGNMENT_MASK; |
148 | 0 | gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); |
149 | 0 | config_updated = TRUE; |
150 | 0 | } |
151 | | |
152 | 0 | if (!gst_buffer_pool_config_has_option (config, |
153 | 0 | GST_BUFFER_POOL_OPTION_VIDEO_META)) { |
154 | 0 | GST_DEBUG_OBJECT (self, "missing video meta option"); |
155 | 0 | return FALSE; |
156 | 0 | } |
157 | 0 | if (!gst_buffer_pool_config_has_option (config, |
158 | 0 | GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) { |
159 | 0 | GST_DEBUG_OBJECT (self, "missing video alignment option"); |
160 | 0 | return FALSE; |
161 | 0 | } |
162 | | |
163 | 0 | gst_buffer_pool_config_get_video_alignment (config, &video_align); |
164 | 0 | for (int i = 0; i != GST_VIDEO_MAX_PLANES; ++i) { |
165 | 0 | if (video_align.stride_align[i] < UDMABUF_ALIGNMENT_MASK) { |
166 | 0 | video_align.stride_align[i] |= UDMABUF_ALIGNMENT_MASK; |
167 | 0 | alignment_updated = TRUE; |
168 | 0 | } |
169 | 0 | } |
170 | 0 | if (alignment_updated) { |
171 | 0 | GST_DEBUG_OBJECT (self, "updating video alignment"); |
172 | 0 | gst_buffer_pool_config_set_video_alignment (config, &video_align); |
173 | 0 | config_updated = TRUE; |
174 | 0 | } |
175 | |
|
176 | 0 | res = |
177 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->set_config |
178 | 0 | (pool, config); |
179 | |
|
180 | 0 | if (config_updated) |
181 | 0 | return FALSE; |
182 | | |
183 | 0 | return res; |
184 | 0 | } |
185 | | |
186 | | static void |
187 | | gst_video_dmabuf_pool_finalize (GObject * object) |
188 | 0 | { |
189 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (object); |
190 | |
|
191 | 0 | gst_object_unref (self->sync_handler); |
192 | |
|
193 | 0 | G_OBJECT_CLASS (gst_video_dmabuf_pool_parent_class)->finalize (object); |
194 | 0 | } |
195 | | #endif /* HAVE_LINUX_DMA_BUF_H */ |
196 | | |
197 | | static void |
198 | | gst_video_dmabuf_pool_init (GstVideoDmabufPool * self) |
199 | 0 | { |
200 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
201 | 0 | self->sync_handler = |
202 | 0 | gst_video_dmabuf_sync_handler_new |
203 | 0 | (gst_video_dmabuf_pool_final_release_buffer, (gpointer) self); |
204 | 0 | #endif |
205 | 0 | } |
206 | | |
207 | | static void |
208 | | gst_video_dmabuf_pool_class_init (GstVideoDmabufPoolClass * klass) |
209 | 0 | { |
210 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
211 | 0 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
212 | 0 | GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass); |
213 | |
|
214 | 0 | object_class->finalize = gst_video_dmabuf_pool_finalize; |
215 | |
|
216 | 0 | pool_class->start = gst_video_dmabuf_pool_start; |
217 | 0 | pool_class->stop = gst_video_dmabuf_pool_stop; |
218 | 0 | pool_class->release_buffer = gst_video_dmabuf_pool_release_buffer; |
219 | 0 | pool_class->set_config = gst_video_dmabuf_pool_set_config; |
220 | 0 | #endif |
221 | 0 | } |
222 | | |
223 | | /** |
224 | | * gst_video_dmabuf_pool_new: |
225 | | * |
226 | | * Create a new #GstVideoDmabufPool instance. |
227 | | * |
228 | | * Returns: (transfer full) (nullable): a #GstVideoDmabufPool or %NULL |
229 | | * if dmabufs are not supported. |
230 | | * |
231 | | * Since: 1.28 |
232 | | */ |
233 | | GstBufferPool * |
234 | | gst_video_dmabuf_pool_new (void) |
235 | 0 | { |
236 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
237 | 0 | return g_object_new (GST_TYPE_VIDEO_DMABUF_POOL, NULL); |
238 | | #else |
239 | | return NULL; |
240 | | #endif |
241 | 0 | } |