1 /* GStreamer
2 *
3 * Copyright (C) 2016 Igalia
4 *
5 * Authors:
6 * Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7 * Javier Martin <javiermartin@by.com.es>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <fcntl.h>
31 #include <xf86drm.h>
32 #include <xf86drmMode.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/mman.h>
36 #include <unistd.h>
37
38 /* it needs to be below because is internal to libdrm */
39 #include <drm.h>
40 #include <drm_fourcc.h>
41
42 #include <gst/allocators/gstdmabuf.h>
43
44 #include "gstkmsallocator.h"
45 #include "gstkmsutils.h"
46
47 #ifndef DRM_RDWR
48 #define DRM_RDWR O_RDWR
49 #endif
50
51 #define GST_CAT_DEFAULT kmsallocator_debug
52 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53
54 #define GST_KMS_MEMORY_TYPE "KMSMemory"
55
56 struct kms_bo
57 {
58 void *ptr;
59 size_t size;
60 unsigned handle;
61 unsigned int refs;
62 };
63
64 struct _GstKMSAllocatorPrivate
65 {
66 int fd;
67 /* protected by GstKMSAllocator object lock */
68 GList *mem_cache;
69 GstAllocator *dmabuf_alloc;
70 };
71
72 #define parent_class gst_kms_allocator_parent_class
73 G_DEFINE_TYPE_WITH_CODE (GstKMSAllocator, gst_kms_allocator, GST_TYPE_ALLOCATOR,
74 G_ADD_PRIVATE (GstKMSAllocator);
75 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsallocator", 0,
76 "KMS allocator"));
77
78 enum
79 {
80 PROP_DRM_FD = 1,
81 PROP_N,
82 };
83
84 static GParamSpec *g_props[PROP_N] = { NULL, };
85
86 gboolean
gst_is_kms_memory(GstMemory * mem)87 gst_is_kms_memory (GstMemory * mem)
88 {
89 return gst_memory_is_type (mem, GST_KMS_MEMORY_TYPE);
90 }
91
92 guint32
gst_kms_memory_get_fb_id(GstMemory * mem)93 gst_kms_memory_get_fb_id (GstMemory * mem)
94 {
95 if (!gst_is_kms_memory (mem))
96 return 0;
97 return ((GstKMSMemory *) mem)->fb_id;
98 }
99
100 static gboolean
check_fd(GstKMSAllocator * alloc)101 check_fd (GstKMSAllocator * alloc)
102 {
103 return alloc->priv->fd > -1;
104 }
105
106 static void
gst_kms_allocator_memory_reset(GstKMSAllocator * allocator,GstKMSMemory * mem)107 gst_kms_allocator_memory_reset (GstKMSAllocator * allocator, GstKMSMemory * mem)
108 {
109 int err;
110 struct drm_mode_destroy_dumb arg = { 0, };
111
112 if (!check_fd (allocator))
113 return;
114
115 if (mem->fb_id) {
116 GST_DEBUG_OBJECT (allocator, "removing fb id %d", mem->fb_id);
117 drmModeRmFB (allocator->priv->fd, mem->fb_id);
118 mem->fb_id = 0;
119 }
120
121 if (!mem->bo)
122 return;
123
124 if (mem->bo->ptr != NULL) {
125 GST_WARNING_OBJECT (allocator, "destroying mapped bo (refcount=%d)",
126 mem->bo->refs);
127 munmap (mem->bo->ptr, mem->bo->size);
128 mem->bo->ptr = NULL;
129 }
130
131 arg.handle = mem->bo->handle;
132
133 err = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
134 if (err)
135 GST_WARNING_OBJECT (allocator,
136 "Failed to destroy dumb buffer object: %s %d", strerror (errno), errno);
137
138 g_free (mem->bo);
139 mem->bo = NULL;
140 }
141
142 /* Copied from gst_v4l2_object_extrapolate_stride() */
143 static gint
extrapolate_stride(const GstVideoFormatInfo * finfo,gint plane,gint stride)144 extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane, gint stride)
145 {
146 gint estride;
147
148 switch (finfo->format) {
149 case GST_VIDEO_FORMAT_NV12:
150 case GST_VIDEO_FORMAT_NV12_64Z32:
151 #ifdef HAVE_NV12_10LE40
152 case GST_VIDEO_FORMAT_NV12_10LE40:
153 #endif
154 case GST_VIDEO_FORMAT_NV21:
155 case GST_VIDEO_FORMAT_NV16:
156 case GST_VIDEO_FORMAT_NV61:
157 case GST_VIDEO_FORMAT_NV24:
158 estride = (plane == 0 ? 1 : 2) *
159 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
160 break;
161 default:
162 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
163 break;
164 }
165
166 return estride;
167 }
168
169 static gboolean
gst_kms_allocator_memory_create(GstKMSAllocator * allocator,GstKMSMemory * kmsmem,GstVideoInfo * vinfo)170 gst_kms_allocator_memory_create (GstKMSAllocator * allocator,
171 GstKMSMemory * kmsmem, GstVideoInfo * vinfo)
172 {
173 gint i, ret, h;
174 struct drm_mode_create_dumb arg = { 0, };
175 guint32 fmt;
176 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
177 gsize offs = 0;
178
179 if (kmsmem->bo)
180 return TRUE;
181
182 if (!check_fd (allocator))
183 return FALSE;
184
185 kmsmem->bo = g_malloc0 (sizeof (*kmsmem->bo));
186 if (!kmsmem->bo)
187 return FALSE;
188
189 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
190 arg.bpp = gst_drm_bpp_from_drm (fmt);
191 arg.width = GST_VIDEO_INFO_WIDTH (vinfo);
192 h = GST_VIDEO_INFO_HEIGHT (vinfo);
193 arg.height = gst_drm_height_from_drm (fmt, h);
194
195 ret = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
196 if (ret)
197 goto create_failed;
198
199 if (!arg.pitch)
200 goto done;
201
202 for (i = 0; i < num_planes; i++) {
203 guint32 pitch;
204
205 if (!arg.pitch)
206 continue;
207
208 /* Overwrite the video info's stride and offset using the pitch calculcated
209 * by the kms driver. */
210 pitch = extrapolate_stride (vinfo->finfo, i, arg.pitch);
211 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = pitch;
212 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) = offs;
213
214 /* Note that we cannot negotiate special padding betweem each planes,
215 * hence using the display height here. */
216 offs += pitch * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, h);
217
218 GST_DEBUG_OBJECT (allocator, "Created BO plane %i with stride %i and "
219 "offset %" G_GSIZE_FORMAT, i,
220 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i),
221 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i));
222 }
223
224 /* Update with the size use for display, excluding any padding at the end */
225 GST_VIDEO_INFO_SIZE (vinfo) = offs;
226
227 done:
228 kmsmem->bo->handle = arg.handle;
229 /* will be used a memory maxsize */
230 kmsmem->bo->size = arg.size;
231
232 /* Validate the size to prevent overflow */
233 if (kmsmem->bo->size < GST_VIDEO_INFO_SIZE (vinfo)) {
234 GST_ERROR_OBJECT (allocator,
235 "DUMB buffer has a size of %" G_GSIZE_FORMAT
236 " but we require at least %" G_GSIZE_FORMAT " to hold a frame",
237 kmsmem->bo->size, GST_VIDEO_INFO_SIZE (vinfo));
238 return FALSE;
239 }
240
241 return TRUE;
242
243 /* ERRORS */
244 create_failed:
245 {
246 GST_ERROR_OBJECT (allocator, "Failed to create buffer object: %s (%d)",
247 strerror (-ret), ret);
248 g_free (kmsmem->bo);
249 kmsmem->bo = NULL;
250 return FALSE;
251 }
252 }
253
254 static void
gst_kms_allocator_free(GstAllocator * allocator,GstMemory * mem)255 gst_kms_allocator_free (GstAllocator * allocator, GstMemory * mem)
256 {
257 GstKMSAllocator *alloc;
258 GstKMSMemory *kmsmem;
259
260 alloc = GST_KMS_ALLOCATOR (allocator);
261 kmsmem = (GstKMSMemory *) mem;
262
263 gst_kms_allocator_memory_reset (alloc, kmsmem);
264 g_slice_free (GstKMSMemory, kmsmem);
265 }
266
267 static void
gst_kms_allocator_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)268 gst_kms_allocator_set_property (GObject * object, guint prop_id,
269 const GValue * value, GParamSpec * pspec)
270 {
271 GstKMSAllocator *alloc;
272
273 alloc = GST_KMS_ALLOCATOR (object);
274
275 switch (prop_id) {
276 case PROP_DRM_FD:{
277 int fd = g_value_get_int (value);
278 if (fd > -1)
279 alloc->priv->fd = dup (fd);
280 break;
281 }
282 default:
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 break;
285 }
286 }
287
288 static void
gst_kms_allocator_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)289 gst_kms_allocator_get_property (GObject * object, guint prop_id,
290 GValue * value, GParamSpec * pspec)
291 {
292 GstKMSAllocator *alloc;
293
294 alloc = GST_KMS_ALLOCATOR (object);
295
296 switch (prop_id) {
297 case PROP_DRM_FD:
298 g_value_set_int (value, alloc->priv->fd);
299 break;
300 default:
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302 break;
303 }
304 }
305
306 static void
gst_kms_allocator_finalize(GObject * obj)307 gst_kms_allocator_finalize (GObject * obj)
308 {
309 GstKMSAllocator *alloc;
310
311 alloc = GST_KMS_ALLOCATOR (obj);
312
313 gst_kms_allocator_clear_cache (GST_ALLOCATOR (alloc));
314
315 if (alloc->priv->dmabuf_alloc)
316 gst_object_unref (alloc->priv->dmabuf_alloc);
317
318 if (check_fd (alloc))
319 close (alloc->priv->fd);
320
321 G_OBJECT_CLASS (parent_class)->finalize (obj);
322 }
323
324 static void
gst_kms_allocator_class_init(GstKMSAllocatorClass * klass)325 gst_kms_allocator_class_init (GstKMSAllocatorClass * klass)
326 {
327 GObjectClass *gobject_class;
328 GstAllocatorClass *allocator_class;
329
330 allocator_class = GST_ALLOCATOR_CLASS (klass);
331 gobject_class = G_OBJECT_CLASS (klass);
332
333 allocator_class->free = gst_kms_allocator_free;
334
335 gobject_class->set_property = gst_kms_allocator_set_property;
336 gobject_class->get_property = gst_kms_allocator_get_property;
337 gobject_class->finalize = gst_kms_allocator_finalize;
338
339 g_props[PROP_DRM_FD] = g_param_spec_int ("drm-fd", "DRM fd",
340 "DRM file descriptor", -1, G_MAXINT, -1,
341 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
342
343 g_object_class_install_properties (gobject_class, PROP_N, g_props);
344 }
345
346 static gpointer
gst_kms_memory_map(GstMemory * mem,gsize maxsize,GstMapFlags flags)347 gst_kms_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
348 {
349 GstKMSMemory *kmsmem;
350 GstKMSAllocator *alloc;
351 int err;
352 gpointer out;
353 struct drm_mode_map_dumb arg = { 0, };
354
355 alloc = (GstKMSAllocator *) mem->allocator;
356
357 if (!check_fd (alloc))
358 return NULL;
359
360 kmsmem = (GstKMSMemory *) mem;
361 if (!kmsmem->bo)
362 return NULL;
363
364 /* Reuse existing buffer object mapping if possible */
365 if (kmsmem->bo->ptr != NULL) {
366 goto out;
367 }
368
369 arg.handle = kmsmem->bo->handle;
370
371 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
372 if (err) {
373 GST_ERROR_OBJECT (alloc, "Failed to get offset of buffer object: %s %d",
374 strerror (-err), err);
375 return NULL;
376 }
377
378 out = mmap (0, kmsmem->bo->size,
379 PROT_READ | PROT_WRITE, MAP_SHARED, alloc->priv->fd, arg.offset);
380 if (out == MAP_FAILED) {
381 GST_ERROR_OBJECT (alloc, "Failed to map dumb buffer object: %s %d",
382 strerror (errno), errno);
383 return NULL;
384 }
385 kmsmem->bo->ptr = out;
386
387 out:
388 g_atomic_int_inc (&kmsmem->bo->refs);
389 return kmsmem->bo->ptr;
390 }
391
392 static void
gst_kms_memory_unmap(GstMemory * mem)393 gst_kms_memory_unmap (GstMemory * mem)
394 {
395 GstKMSMemory *kmsmem;
396
397 if (!check_fd ((GstKMSAllocator *) mem->allocator))
398 return;
399
400 kmsmem = (GstKMSMemory *) mem;
401 if (!kmsmem->bo)
402 return;
403
404 if (g_atomic_int_dec_and_test (&kmsmem->bo->refs)) {
405 munmap (kmsmem->bo->ptr, kmsmem->bo->size);
406 kmsmem->bo->ptr = NULL;
407 }
408 }
409
410 static void
gst_kms_allocator_init(GstKMSAllocator * allocator)411 gst_kms_allocator_init (GstKMSAllocator * allocator)
412 {
413 GstAllocator *alloc;
414
415 alloc = GST_ALLOCATOR_CAST (allocator);
416
417 allocator->priv = gst_kms_allocator_get_instance_private (allocator);
418 allocator->priv->fd = -1;
419
420 alloc->mem_type = GST_KMS_MEMORY_TYPE;
421 alloc->mem_map = gst_kms_memory_map;
422 alloc->mem_unmap = gst_kms_memory_unmap;
423 /* Use the default, fallback copy function */
424
425 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
426 }
427
428 GstAllocator *
gst_kms_allocator_new(int fd)429 gst_kms_allocator_new (int fd)
430 {
431 GstAllocator *alloc;
432
433 alloc = g_object_new (GST_TYPE_KMS_ALLOCATOR, "name",
434 "KMSMemory::allocator", "drm-fd", fd, NULL);
435 gst_object_ref_sink (alloc);
436
437 return alloc;
438 }
439
440 /* The mem_offsets are relative to the GstMemory start, unlike the vinfo->offset
441 * which are relative to the GstBuffer start. */
442 static gboolean
gst_kms_allocator_add_fb(GstKMSAllocator * alloc,GstKMSMemory * kmsmem,gsize in_offsets[GST_VIDEO_MAX_PLANES],GstVideoInfo * vinfo)443 gst_kms_allocator_add_fb (GstKMSAllocator * alloc, GstKMSMemory * kmsmem,
444 gsize in_offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
445 {
446 gint i, ret = -1;
447 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
448 guint32 w, h, fmt, bo_handles[4] = { 0, };
449 guint32 pitches[4] = { 0, };
450 guint32 offsets[4] = { 0, };
451
452 if (kmsmem->fb_id)
453 return TRUE;
454
455 w = GST_VIDEO_INFO_WIDTH (vinfo);
456 h = GST_VIDEO_INFO_HEIGHT (vinfo);
457 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
458
459 for (i = 0; i < num_planes; i++) {
460 if (kmsmem->bo)
461 bo_handles[i] = kmsmem->bo->handle;
462 else
463 bo_handles[i] = kmsmem->gem_handle[i];
464
465 pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
466 offsets[i] = in_offsets[i];
467 }
468
469 GST_DEBUG_OBJECT (alloc, "bo handles: %d, %d, %d, %d", bo_handles[0],
470 bo_handles[1], bo_handles[2], bo_handles[3]);
471
472 if (GST_VIDEO_INFO_IS_AFBC (vinfo)) {
473 guint64 modifiers[4] = { 0 };
474
475 for (i = 0; i < num_planes; i++)
476 modifiers[i] = DRM_AFBC_MODIFIER;
477
478 if (fmt == DRM_FORMAT_NV12 || fmt == DRM_FORMAT_NV12_10 ||
479 fmt == DRM_FORMAT_NV16) {
480 /* The newer kernel might use new formats instead */
481 guint32 _handles[4] = { bo_handles[0], 0, };
482 guint32 _pitches[4] = { pitches[0], 0, };
483 guint32 _offsets[4] = { offsets[0], 0, };
484 guint64 _modifiers[4] = { modifiers[0], 0, };
485 guint32 _fmt;
486
487 if (fmt == DRM_FORMAT_NV12) {
488 _fmt = DRM_FORMAT_YUV420_8BIT;
489 /* The bpp of YUV420_8BIT is 12 */
490 _pitches[0] *= 1.5;
491 } else if (fmt == DRM_FORMAT_NV12_10) {
492 _fmt = DRM_FORMAT_YUV420_10BIT;
493 /* The bpp of YUV420_10BIT is 15 */
494 _pitches[0] *= 1.5;
495 } else {
496 _fmt = DRM_FORMAT_YUYV;
497 /* The bpp of YUYV (AFBC) is 16 */
498 _pitches[0] *= 2;
499 }
500
501 ret = drmModeAddFB2WithModifiers (alloc->priv->fd, w, h, _fmt, _handles,
502 _pitches, _offsets, _modifiers, &kmsmem->fb_id,
503 DRM_MODE_FB_MODIFIERS);
504 }
505
506 if (ret)
507 ret = drmModeAddFB2WithModifiers (alloc->priv->fd, w, h, fmt, bo_handles,
508 pitches, offsets, modifiers, &kmsmem->fb_id, DRM_MODE_FB_MODIFIERS);
509 } else {
510 ret = drmModeAddFB2 (alloc->priv->fd, w, h, fmt, bo_handles, pitches,
511 offsets, &kmsmem->fb_id, 0);
512 if (ret && fmt == DRM_FORMAT_NV12_10)
513 ret = drmModeAddFB2 (alloc->priv->fd, w, h, DRM_FORMAT_NV15, bo_handles,
514 pitches, offsets, &kmsmem->fb_id, 0);
515 }
516 if (ret) {
517 GST_ERROR_OBJECT (alloc, "Failed to bind to framebuffer: %s (%d)",
518 strerror (-ret), ret);
519 return FALSE;
520 }
521
522 return TRUE;
523 }
524
525 GstMemory *
gst_kms_allocator_bo_alloc(GstAllocator * allocator,GstVideoInfo * vinfo)526 gst_kms_allocator_bo_alloc (GstAllocator * allocator, GstVideoInfo * vinfo)
527 {
528 GstKMSAllocator *alloc;
529 GstKMSMemory *kmsmem;
530 GstMemory *mem;
531
532 kmsmem = g_slice_new0 (GstKMSMemory);
533 if (!kmsmem)
534 return NULL;
535
536 alloc = GST_KMS_ALLOCATOR (allocator);
537
538 mem = GST_MEMORY_CAST (kmsmem);
539
540 if (!gst_kms_allocator_memory_create (alloc, kmsmem, vinfo)) {
541 g_slice_free (GstKMSMemory, kmsmem);
542 return NULL;
543 }
544
545 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
546 kmsmem->bo->size, 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
547
548 if (!gst_kms_allocator_add_fb (alloc, kmsmem, vinfo->offset, vinfo))
549 goto fail;
550
551 return mem;
552
553 /* ERRORS */
554 fail:
555 gst_memory_unref (mem);
556 return NULL;
557 }
558
559 GstKMSMemory *
gst_kms_allocator_dmabuf_import(GstAllocator * allocator,gint * prime_fds,gint n_planes,gsize offsets[GST_VIDEO_MAX_PLANES],GstVideoInfo * vinfo)560 gst_kms_allocator_dmabuf_import (GstAllocator * allocator, gint * prime_fds,
561 gint n_planes, gsize offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
562 {
563 GstKMSAllocator *alloc;
564 GstKMSMemory *kmsmem;
565 GstMemory *mem;
566 gint i, ret;
567 guint32 handle = 0;
568
569 g_return_val_if_fail (n_planes <= GST_VIDEO_MAX_PLANES, FALSE);
570
571 kmsmem = g_slice_new0 (GstKMSMemory);
572 if (!kmsmem)
573 return FALSE;
574
575 mem = GST_MEMORY_CAST (kmsmem);
576 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
577 GST_VIDEO_INFO_SIZE (vinfo), 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
578
579 alloc = GST_KMS_ALLOCATOR (allocator);
580 for (i = 0; i < n_planes; i++) {
581 ret = drmPrimeFDToHandle (alloc->priv->fd, prime_fds[i],
582 &kmsmem->gem_handle[i]);
583 if (ret)
584 goto import_fd_failed;
585 }
586
587 if (!gst_kms_allocator_add_fb (alloc, kmsmem, offsets, vinfo))
588 goto failed;
589
590 for (i = 0; i < n_planes; i++) {
591 struct drm_gem_close arg = { kmsmem->gem_handle[i], };
592 gint err;
593
594 if (handle == arg.handle)
595 break;
596
597 handle = arg.handle;
598
599 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_GEM_CLOSE, &arg);
600 if (err)
601 GST_WARNING_OBJECT (allocator,
602 "Failed to close GEM handle: %s %d", strerror (errno), errno);
603
604 kmsmem->gem_handle[i] = 0;
605 }
606
607 return kmsmem;
608
609 /* ERRORS */
610 import_fd_failed:
611 {
612 GST_ERROR_OBJECT (alloc, "Failed to import prime fd %d: %s (%d)",
613 prime_fds[i], strerror (-ret), ret);
614 /* fallback */
615 }
616
617 failed:
618 {
619 gst_memory_unref (mem);
620 return NULL;
621 }
622 }
623
624 GstMemory *
gst_kms_allocator_dmabuf_export(GstAllocator * allocator,GstMemory * _kmsmem)625 gst_kms_allocator_dmabuf_export (GstAllocator * allocator, GstMemory * _kmsmem)
626 {
627 GstKMSMemory *kmsmem = (GstKMSMemory *) _kmsmem;
628 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
629 GstMemory *mem;
630 gint ret;
631 gint prime_fd;
632
633 /* We can only export DUMB buffers */
634 g_return_val_if_fail (kmsmem->bo, NULL);
635
636
637 ret = drmPrimeHandleToFD (alloc->priv->fd, kmsmem->bo->handle,
638 DRM_CLOEXEC | DRM_RDWR, &prime_fd);
639 if (ret)
640 goto export_fd_failed;
641
642 if (G_UNLIKELY (alloc->priv->dmabuf_alloc == NULL))
643 alloc->priv->dmabuf_alloc = gst_dmabuf_allocator_new ();
644
645 mem = gst_dmabuf_allocator_alloc (alloc->priv->dmabuf_alloc, prime_fd,
646 gst_memory_get_sizes (_kmsmem, NULL, NULL));
647
648 /* Populate the cache so KMSSink can find the kmsmem back when it receives
649 * one of these DMABuf. This call takes ownership of the kmsmem. */
650 gst_kms_allocator_cache (allocator, mem, _kmsmem);
651
652 GST_DEBUG_OBJECT (alloc, "Exported bo handle %d as %d", kmsmem->bo->handle,
653 prime_fd);
654
655 return mem;
656
657 /* ERRORS */
658 export_fd_failed:
659 {
660 GST_ERROR_OBJECT (alloc, "Failed to export bo handle %d: %s (%d)",
661 kmsmem->bo->handle, g_strerror (errno), ret);
662 return NULL;
663 }
664 }
665
666 /* FIXME, using gdata for caching on upstream memory is not tee safe */
667 GstMemory *
gst_kms_allocator_get_cached(GstMemory * mem)668 gst_kms_allocator_get_cached (GstMemory * mem)
669 {
670 return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
671 g_quark_from_static_string ("kmsmem"));
672 }
673
674 static void
cached_kmsmem_disposed_cb(GstKMSAllocator * alloc,GstMiniObject * obj)675 cached_kmsmem_disposed_cb (GstKMSAllocator * alloc, GstMiniObject * obj)
676 {
677 GST_OBJECT_LOCK (alloc);
678 alloc->priv->mem_cache = g_list_remove (alloc->priv->mem_cache, obj);
679 GST_OBJECT_UNLOCK (alloc);
680 }
681
682 void
gst_kms_allocator_clear_cache(GstAllocator * allocator)683 gst_kms_allocator_clear_cache (GstAllocator * allocator)
684 {
685 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
686 GList *iter;
687
688 GST_OBJECT_LOCK (alloc);
689
690 iter = alloc->priv->mem_cache;
691 while (iter) {
692 GstMiniObject *obj = iter->data;
693 gst_mini_object_weak_unref (obj,
694 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
695 gst_mini_object_set_qdata (obj,
696 g_quark_from_static_string ("kmsmem"), NULL, NULL);
697 iter = iter->next;
698 }
699
700 g_list_free (alloc->priv->mem_cache);
701 alloc->priv->mem_cache = NULL;
702
703 GST_OBJECT_UNLOCK (alloc);
704 }
705
706 /* @kmsmem is transfer-full */
707 void
gst_kms_allocator_cache(GstAllocator * allocator,GstMemory * mem,GstMemory * kmsmem)708 gst_kms_allocator_cache (GstAllocator * allocator, GstMemory * mem,
709 GstMemory * kmsmem)
710 {
711 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
712
713 GST_OBJECT_LOCK (alloc);
714 gst_mini_object_weak_ref (GST_MINI_OBJECT (mem),
715 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
716 alloc->priv->mem_cache = g_list_prepend (alloc->priv->mem_cache, mem);
717 GST_OBJECT_UNLOCK (alloc);
718
719 gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
720 g_quark_from_static_string ("kmsmem"), kmsmem,
721 (GDestroyNotify) gst_memory_unref);
722 }
723