/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 | | |
39 | | #include <gst/allocators/gstudmabufallocator.h> |
40 | | |
41 | | #ifdef HAVE_LINUX_DMA_BUF_H |
42 | | #include <linux/dma-buf.h> |
43 | | #include <sys/ioctl.h> |
44 | | #include <unistd.h> |
45 | | #endif |
46 | | |
47 | | /* This alignment is needed on many AMD GPUs and is known to work well across |
48 | | * many vendors/GPUs, see e.g. GstGLDMABufBufferPool. */ |
49 | 0 | #define UDMABUF_ALIGNMENT_MASK (256 - 1) |
50 | | |
51 | | struct _GstVideoDmabufPool |
52 | | { |
53 | | GstVideoBufferPool parent; |
54 | | GMainContext *context; |
55 | | GMainLoop *loop; |
56 | | GThread *thread; |
57 | | }; |
58 | | |
59 | | GST_DEBUG_CATEGORY_STATIC (gst_video_dmabuf_pool_debug); |
60 | | #define GST_CAT_DEFAULT gst_video_dmabuf_pool_debug |
61 | | |
62 | 0 | G_DEFINE_TYPE_WITH_CODE (GstVideoDmabufPool, gst_video_dmabuf_pool, |
63 | 0 | GST_TYPE_VIDEO_BUFFER_POOL, |
64 | 0 | GST_DEBUG_CATEGORY_INIT (gst_video_dmabuf_pool_debug, "video-dmabuf-pool", |
65 | 0 | 0, "video dmabuf pool"); |
66 | 0 | ); |
67 | 0 |
|
68 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
69 | 0 | #ifdef DMA_BUF_IOCTL_EXPORT_SYNC_FILE |
70 | 0 | typedef struct _DmaBufSource |
71 | 0 | { |
72 | 0 | GSource base; |
73 | 0 |
|
74 | 0 | GstVideoDmabufPool *pool; |
75 | 0 | GstBuffer *buffer; |
76 | 0 |
|
77 | 0 | gint mem_fds[GST_VIDEO_MAX_PLANES]; |
78 | 0 | gpointer fd_tags[GST_VIDEO_MAX_PLANES]; |
79 | 0 | } DmaBufSource; |
80 | 0 |
|
81 | 0 | static gboolean |
82 | 0 | dma_buf_fd_readable (gint fd) |
83 | 0 | { |
84 | 0 | GPollFD poll_fd; |
85 | 0 |
|
86 | 0 | poll_fd.fd = fd; |
87 | 0 | poll_fd.events = G_IO_IN; |
88 | 0 | poll_fd.revents = 0; |
89 | 0 |
|
90 | 0 | if (!g_poll (&poll_fd, 1, 0)) |
91 | 0 | return FALSE; |
92 | 0 |
|
93 | 0 | return (poll_fd.revents & (G_IO_IN | G_IO_NVAL)) != 0; |
94 | 0 | } |
95 | 0 |
|
96 | 0 | static int |
97 | 0 | get_sync_file (gint fd) |
98 | 0 | { |
99 | 0 | struct dma_buf_export_sync_file sync_file_in_out = { |
100 | 0 | .flags = DMA_BUF_SYNC_WRITE, |
101 | 0 | .fd = -1 |
102 | 0 | }; |
103 | 0 | gint ret; |
104 | 0 |
|
105 | 0 | do { |
106 | 0 | ret = ioctl (fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &sync_file_in_out); |
107 | 0 | } while (ret == -1 && errno == EINTR); |
108 | 0 |
|
109 | 0 | if (ret == 0) |
110 | 0 | return sync_file_in_out.fd; |
111 | 0 |
|
112 | 0 | return -1; |
113 | 0 | } |
114 | 0 |
|
115 | 0 | static gboolean |
116 | 0 | dma_buf_source_dispatch (GSource * base, |
117 | 0 | GSourceFunc callback, gpointer user_data) |
118 | 0 | { |
119 | 0 | DmaBufSource *source = (DmaBufSource *) base; |
120 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (source->pool); |
121 | 0 | gboolean ready; |
122 | 0 |
|
123 | 0 | GST_DEBUG_OBJECT (self, "Dispatch source for buffer %p", source->buffer); |
124 | 0 |
|
125 | 0 | ready = TRUE; |
126 | 0 |
|
127 | 0 | for (gint i = 0; i < GST_VIDEO_MAX_PLANES; i++) { |
128 | 0 | if (!source->fd_tags[i]) |
129 | 0 | continue; |
130 | 0 |
|
131 | 0 | if (!dma_buf_fd_readable (source->mem_fds[i])) { |
132 | 0 | GST_DEBUG_OBJECT (self, "Buffer %p not ready, sync file: %d", |
133 | 0 | source->buffer, source->mem_fds[i]); |
134 | 0 | ready = FALSE; |
135 | 0 | continue; |
136 | 0 | } |
137 | 0 |
|
138 | 0 | close (source->mem_fds[i]); |
139 | 0 | g_source_remove_unix_fd (base, source->fd_tags[i]); |
140 | 0 | source->fd_tags[i] = NULL; |
141 | 0 | } |
142 | 0 |
|
143 | 0 | if (!ready) |
144 | 0 | return G_SOURCE_CONTINUE; |
145 | 0 |
|
146 | 0 | GST_DEBUG_OBJECT (self, "Releasing buffer %p from source, pool %p", |
147 | 0 | source->buffer, self); |
148 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->release_buffer |
149 | 0 | (GST_BUFFER_POOL_CAST (self), source->buffer); |
150 | 0 | g_source_unref (base); |
151 | 0 |
|
152 | 0 | return G_SOURCE_REMOVE; |
153 | 0 | } |
154 | 0 |
|
155 | 0 | static void |
156 | 0 | dma_buf_source_finalize (GSource * base) |
157 | 0 | { |
158 | 0 | DmaBufSource *source = (DmaBufSource *) base; |
159 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (source->pool); |
160 | 0 | gboolean need_buffer_release = FALSE; |
161 | 0 |
|
162 | 0 | for (gint i = 0; i < GST_VIDEO_MAX_PLANES; i++) { |
163 | 0 | if (!source->fd_tags[i]) |
164 | 0 | continue; |
165 | 0 |
|
166 | 0 | close (source->mem_fds[i]); |
167 | 0 | g_source_remove_unix_fd (base, source->fd_tags[i]); |
168 | 0 | source->fd_tags[i] = NULL; |
169 | 0 | need_buffer_release = TRUE; |
170 | 0 | } |
171 | 0 |
|
172 | 0 | if (need_buffer_release) { |
173 | 0 | GST_DEBUG_OBJECT (self, "Releasing buffer %p from source, pool %p", |
174 | 0 | source->buffer, self); |
175 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->release_buffer |
176 | 0 | (GST_BUFFER_POOL_CAST (self), source->buffer); |
177 | 0 | } |
178 | 0 | } |
179 | 0 |
|
180 | 0 | static GSourceFuncs dma_buf_source_funcs = { |
181 | 0 | .dispatch = dma_buf_source_dispatch, |
182 | 0 | .finalize = dma_buf_source_finalize, |
183 | 0 | }; |
184 | 0 |
|
185 | 0 | static gpointer |
186 | 0 | dmabuf_source_thread (gpointer data) |
187 | 0 | { |
188 | 0 | GstVideoDmabufPool *self = data; |
189 | 0 | GMainContext *context = NULL; |
190 | 0 | GMainLoop *loop = NULL; |
191 | 0 |
|
192 | 0 | GST_OBJECT_LOCK (self); |
193 | 0 | if (self->context) |
194 | 0 | context = g_main_context_ref (self->context); |
195 | 0 | if (self->loop) |
196 | 0 | loop = g_main_loop_ref (self->loop); |
197 | 0 |
|
198 | 0 | if (context == NULL || loop == NULL) { |
199 | 0 | g_clear_pointer (&loop, g_main_loop_unref); |
200 | 0 | g_clear_pointer (&context, g_main_context_unref); |
201 | 0 | GST_OBJECT_UNLOCK (self); |
202 | 0 | return NULL; |
203 | 0 | } |
204 | 0 | GST_OBJECT_UNLOCK (self); |
205 | 0 |
|
206 | 0 | g_main_context_push_thread_default (context); |
207 | 0 |
|
208 | 0 | GST_DEBUG_OBJECT (self, "Running main loop"); |
209 | 0 | g_main_loop_run (loop); |
210 | 0 | g_main_loop_unref (loop); |
211 | 0 | g_main_context_unref (context); |
212 | 0 |
|
213 | 0 | return NULL; |
214 | 0 | } |
215 | 0 |
|
216 | 0 | static gboolean |
217 | 0 | gst_video_dmabuf_pool_start (GstBufferPool * pool) |
218 | 0 | { |
219 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
220 | 0 |
|
221 | 0 | GST_OBJECT_LOCK (self); |
222 | 0 | GST_DEBUG_OBJECT (self, "Starting main loop"); |
223 | 0 | g_assert (self->context == NULL); |
224 | 0 |
|
225 | 0 | self->context = g_main_context_new (); |
226 | 0 | self->loop = g_main_loop_new (self->context, FALSE); |
227 | 0 |
|
228 | 0 | self->thread = |
229 | 0 | g_thread_new ("video-dmabuf-pool-source-loop", dmabuf_source_thread, |
230 | 0 | self); |
231 | 0 |
|
232 | 0 | GST_OBJECT_UNLOCK (self); |
233 | 0 | return |
234 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->start (pool); |
235 | 0 | } |
236 | 0 |
|
237 | 0 | static gboolean |
238 | 0 | gst_video_dmabuf_pool_stop (GstBufferPool * pool) |
239 | 0 | { |
240 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
241 | 0 | GMainContext *context; |
242 | 0 | GMainLoop *loop; |
243 | 0 | GSource *idle_stop_source; |
244 | 0 |
|
245 | 0 | GST_OBJECT_LOCK (self); |
246 | 0 | GST_DEBUG_OBJECT (self, "Stopping main loop"); |
247 | 0 | context = self->context; |
248 | 0 | loop = self->loop; |
249 | 0 | self->context = NULL; |
250 | 0 | self->loop = NULL; |
251 | 0 | GST_OBJECT_UNLOCK (self); |
252 | 0 |
|
253 | 0 | if (!context || !loop) { |
254 | 0 | g_clear_pointer (&loop, g_main_loop_unref); |
255 | 0 | g_clear_pointer (&context, g_main_context_unref); |
256 | 0 | goto out; |
257 | 0 | } |
258 | 0 |
|
259 | 0 | idle_stop_source = g_idle_source_new (); |
260 | 0 | g_source_set_callback (idle_stop_source, (GSourceFunc) g_main_loop_quit, loop, |
261 | 0 | NULL); |
262 | 0 | g_source_attach (idle_stop_source, context); |
263 | 0 | g_source_unref (idle_stop_source); |
264 | 0 |
|
265 | 0 | g_thread_join (self->thread); |
266 | 0 | self->thread = NULL; |
267 | 0 |
|
268 | 0 | g_main_loop_unref (loop); |
269 | 0 | g_main_context_unref (context); |
270 | 0 |
|
271 | 0 | out: |
272 | 0 | return |
273 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->stop (pool); |
274 | 0 | } |
275 | 0 |
|
276 | 0 | static void |
277 | 0 | gst_video_dmabuf_pool_release_buffer (GstBufferPool * pool, GstBuffer * buffer) |
278 | 0 | { |
279 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
280 | 0 | DmaBufSource *source = NULL; |
281 | 0 | guint n_mem; |
282 | 0 |
|
283 | 0 | GST_DEBUG_OBJECT (self, "Buffer: %p", buffer); |
284 | 0 |
|
285 | 0 | n_mem = gst_buffer_n_memory (buffer); |
286 | 0 | for (gint i = 0; i < n_mem; i++) { |
287 | 0 | GstMemory *mem; |
288 | 0 | gint mem_fd, sync_file; |
289 | 0 |
|
290 | 0 | mem = gst_buffer_peek_memory (buffer, i); |
291 | 0 | if (!gst_is_dmabuf_memory (mem)) |
292 | 0 | continue; |
293 | 0 |
|
294 | 0 | mem_fd = gst_dmabuf_memory_get_fd (mem); |
295 | 0 | sync_file = get_sync_file (mem_fd); |
296 | 0 | if (sync_file == -1) { |
297 | 0 | GST_ERROR_OBJECT (self, "Exporting sync file failed"); |
298 | 0 | continue; |
299 | 0 | } |
300 | 0 |
|
301 | 0 | if (dma_buf_fd_readable (sync_file)) { |
302 | 0 | GST_DEBUG_OBJECT (self, "Sync file readable"); |
303 | 0 | close (sync_file); |
304 | 0 | continue; |
305 | 0 | } |
306 | 0 |
|
307 | 0 | if (!source) { |
308 | 0 | GST_DEBUG_OBJECT (self, "Creating source for buffer %p, pool %p", buffer, |
309 | 0 | self); |
310 | 0 | source = |
311 | 0 | (DmaBufSource *) g_source_new (&dma_buf_source_funcs, |
312 | 0 | sizeof (*source)); |
313 | 0 | source->pool = self; |
314 | 0 | source->buffer = buffer; |
315 | 0 | } |
316 | 0 |
|
317 | 0 | GST_DEBUG_OBJECT (self, "Adding sync file to source"); |
318 | 0 | source->mem_fds[i] = sync_file; |
319 | 0 | source->fd_tags[i] = |
320 | 0 | g_source_add_unix_fd (&source->base, sync_file, G_IO_IN); |
321 | 0 | } |
322 | 0 |
|
323 | 0 | if (source) { |
324 | 0 | g_assert (self->context); |
325 | 0 | g_source_attach ((GSource *) source, self->context); |
326 | 0 | return; |
327 | 0 | } |
328 | 0 |
|
329 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->release_buffer |
330 | 0 | (pool, buffer); |
331 | 0 | } |
332 | 0 | #endif /* DMA_BUF_IOCTL_EXPORT_SYNC_FILE */ |
333 | 0 |
|
334 | 0 | static gboolean |
335 | 0 | gst_video_dmabuf_pool_set_config (GstBufferPool * pool, GstStructure * config) |
336 | 0 | { |
337 | 0 | GstVideoDmabufPool *self = GST_VIDEO_DMABUF_POOL (pool); |
338 | 0 | GstAllocator *allocator; |
339 | 0 | GstAllocationParams params; |
340 | 0 | GstVideoAlignment video_align = { 0 }; |
341 | 0 | gboolean config_updated = FALSE; |
342 | 0 | gboolean alignment_updated = FALSE; |
343 | 0 | gboolean res; |
344 | |
|
345 | 0 | gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms); |
346 | 0 | if (!GST_IS_DMABUF_ALLOCATOR (allocator) || |
347 | 0 | GST_OBJECT_FLAG_IS_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC)) { |
348 | 0 | GST_DEBUG_OBJECT (self, |
349 | 0 | "Allocator not a dmabuf allocator or having the CUSTOM_ALLOC flag set, trying to update to udmabuf"); |
350 | |
|
351 | 0 | allocator = gst_udmabuf_allocator_get (); |
352 | 0 | if (allocator) { |
353 | 0 | params.align |= UDMABUF_ALIGNMENT_MASK; |
354 | 0 | gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); |
355 | 0 | gst_object_unref (allocator); |
356 | 0 | config_updated = TRUE; |
357 | 0 | } else { |
358 | 0 | GST_ERROR_OBJECT (self, "udmabuf allocator not available"); |
359 | 0 | return FALSE; |
360 | 0 | } |
361 | 0 | } else if (params.align < UDMABUF_ALIGNMENT_MASK) { |
362 | 0 | GST_DEBUG_OBJECT (self, "updating allocator params"); |
363 | 0 | params.align |= UDMABUF_ALIGNMENT_MASK; |
364 | 0 | gst_buffer_pool_config_set_allocator (config, allocator, ¶ms); |
365 | 0 | config_updated = TRUE; |
366 | 0 | } |
367 | | |
368 | 0 | if (!gst_buffer_pool_config_has_option (config, |
369 | 0 | GST_BUFFER_POOL_OPTION_VIDEO_META)) { |
370 | 0 | GST_DEBUG_OBJECT (self, "missing video meta option"); |
371 | 0 | return FALSE; |
372 | 0 | } |
373 | 0 | if (!gst_buffer_pool_config_has_option (config, |
374 | 0 | GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT)) { |
375 | 0 | GST_DEBUG_OBJECT (self, "missing video alignment option"); |
376 | 0 | return FALSE; |
377 | 0 | } |
378 | | |
379 | 0 | gst_buffer_pool_config_get_video_alignment (config, &video_align); |
380 | 0 | for (int i = 0; i != GST_VIDEO_MAX_PLANES; ++i) { |
381 | 0 | if (video_align.stride_align[i] < UDMABUF_ALIGNMENT_MASK) { |
382 | 0 | video_align.stride_align[i] |= UDMABUF_ALIGNMENT_MASK; |
383 | 0 | alignment_updated = TRUE; |
384 | 0 | } |
385 | 0 | } |
386 | 0 | if (alignment_updated) { |
387 | 0 | GST_DEBUG_OBJECT (self, "updating video alignment"); |
388 | 0 | gst_buffer_pool_config_set_video_alignment (config, &video_align); |
389 | 0 | config_updated = TRUE; |
390 | 0 | } |
391 | |
|
392 | 0 | res = |
393 | 0 | GST_BUFFER_POOL_CLASS (gst_video_dmabuf_pool_parent_class)->set_config |
394 | 0 | (pool, config); |
395 | |
|
396 | 0 | if (config_updated) |
397 | 0 | return FALSE; |
398 | | |
399 | 0 | return res; |
400 | 0 | } |
401 | | #endif /* HAVE_LINUX_DMA_BUF_H */ |
402 | | |
403 | | static void |
404 | | gst_video_dmabuf_pool_init (GstVideoDmabufPool * self) |
405 | 0 | { |
406 | 0 | } |
407 | | |
408 | | static void |
409 | | gst_video_dmabuf_pool_class_init (GstVideoDmabufPoolClass * klass) |
410 | 0 | { |
411 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
412 | 0 | GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass); |
413 | |
|
414 | | #ifdef DMA_BUF_IOCTL_EXPORT_SYNC_FILE |
415 | | pool_class->start = gst_video_dmabuf_pool_start; |
416 | | pool_class->stop = gst_video_dmabuf_pool_stop; |
417 | | pool_class->release_buffer = gst_video_dmabuf_pool_release_buffer; |
418 | | #endif /* DMA_BUF_IOCTL_EXPORT_SYNC_FILE */ |
419 | 0 | pool_class->set_config = gst_video_dmabuf_pool_set_config; |
420 | 0 | #endif |
421 | 0 | } |
422 | | |
423 | | /** |
424 | | * gst_video_dmabuf_pool_new: |
425 | | * |
426 | | * Create a new #GstVideoDmabufPool instance. |
427 | | * |
428 | | * Returns: (transfer full) (nullable): a #GstVideoDmabufPool or %NULL |
429 | | * if dmabufs are not supported. |
430 | | * |
431 | | * Since: 1.28 |
432 | | */ |
433 | | GstBufferPool * |
434 | | gst_video_dmabuf_pool_new (void) |
435 | 0 | { |
436 | 0 | #ifdef HAVE_LINUX_DMA_BUF_H |
437 | 0 | return g_object_new (GST_TYPE_VIDEO_DMABUF_POOL, NULL); |
438 | | #else |
439 | | return NULL; |
440 | | #endif |
441 | 0 | } |