3 * Copyright (C) 2016 Igalia
6 * Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7 * Javier Martin <javiermartin@by.com.es>
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.
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.
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.
32 #include <xf86drmMode.h>
38 /* it needs to be below because is internal to libdrm */
41 #include <gst/allocators/gstdmabuf.h>
43 #include "gstkmsallocator.h"
44 #include "gstkmsutils.h"
47 #define DRM_RDWR O_RDWR
50 #define GST_CAT_DEFAULT kmsallocator_debug
51 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 #define GST_KMS_MEMORY_TYPE "KMSMemory"
63 struct _GstKMSAllocatorPrivate
66 /* protected by GstKMSAllocator object lock */
68 GstAllocator *dmabuf_alloc;
71 #define parent_class gst_kms_allocator_parent_class
72 G_DEFINE_TYPE_WITH_CODE (GstKMSAllocator, gst_kms_allocator, GST_TYPE_ALLOCATOR,
73 G_ADD_PRIVATE (GstKMSAllocator);
74 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsallocator", 0,
83 static GParamSpec *g_props[PROP_N] = { NULL, };
86 gst_is_kms_memory (GstMemory * mem)
88 return gst_memory_is_type (mem, GST_KMS_MEMORY_TYPE);
92 gst_kms_memory_get_fb_id (GstMemory * mem)
94 if (!gst_is_kms_memory (mem))
96 return ((GstKMSMemory *) mem)->fb_id;
100 check_fd (GstKMSAllocator * alloc)
102 return alloc->priv->fd > -1;
106 gst_kms_allocator_memory_reset (GstKMSAllocator * allocator, GstKMSMemory * mem)
109 struct drm_mode_destroy_dumb arg = { 0, };
111 if (!check_fd (allocator))
115 GST_DEBUG_OBJECT (allocator, "removing fb id %d", mem->fb_id);
116 drmModeRmFB (allocator->priv->fd, mem->fb_id);
123 if (mem->bo->ptr != NULL) {
124 GST_WARNING_OBJECT (allocator, "destroying mapped bo (refcount=%d)",
126 munmap (mem->bo->ptr, mem->bo->size);
130 arg.handle = mem->bo->handle;
132 err = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &arg);
134 GST_WARNING_OBJECT (allocator,
135 "Failed to destroy dumb buffer object: %s %d", strerror (errno), errno);
141 /* Copied from gst_v4l2_object_extrapolate_stride() */
143 extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane, gint stride)
147 switch (finfo->format) {
148 case GST_VIDEO_FORMAT_NV12:
149 case GST_VIDEO_FORMAT_NV12_64Z32:
150 case GST_VIDEO_FORMAT_NV21:
151 case GST_VIDEO_FORMAT_NV16:
152 case GST_VIDEO_FORMAT_NV61:
153 case GST_VIDEO_FORMAT_NV24:
154 estride = (plane == 0 ? 1 : 2) *
155 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
158 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
166 gst_kms_allocator_memory_create (GstKMSAllocator * allocator,
167 GstKMSMemory * kmsmem, GstVideoInfo * vinfo)
170 struct drm_mode_create_dumb arg = { 0, };
172 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
178 if (!check_fd (allocator))
181 kmsmem->bo = g_malloc0 (sizeof (*kmsmem->bo));
185 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
186 arg.bpp = gst_drm_bpp_from_drm (fmt);
187 arg.width = GST_VIDEO_INFO_WIDTH (vinfo);
188 h = GST_VIDEO_INFO_HEIGHT (vinfo);
189 arg.height = gst_drm_height_from_drm (fmt, h);
191 ret = drmIoctl (allocator->priv->fd, DRM_IOCTL_MODE_CREATE_DUMB, &arg);
198 for (i = 0; i < num_planes; i++) {
204 /* Overwrite the video info's stride and offset using the pitch calculcated
205 * by the kms driver. */
206 pitch = extrapolate_stride (vinfo->finfo, i, arg.pitch);
207 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = pitch;
208 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) = offs;
210 /* Note that we cannot negotiate special padding betweem each planes,
211 * hence using the display height here. */
212 offs += pitch * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, h);
214 GST_DEBUG_OBJECT (allocator, "Created BO plane %i with stride %i and "
215 "offset %" G_GSIZE_FORMAT, i,
216 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i),
217 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i));
220 /* Update with the size use for display, excluding any padding at the end */
221 GST_VIDEO_INFO_SIZE (vinfo) = offs;
224 kmsmem->bo->handle = arg.handle;
225 /* will be used a memory maxsize */
226 kmsmem->bo->size = arg.size;
228 /* Validate the size to prevent overflow */
229 if (kmsmem->bo->size < GST_VIDEO_INFO_SIZE (vinfo)) {
230 GST_ERROR_OBJECT (allocator,
231 "DUMB buffer has a size of %" G_GSIZE_FORMAT
232 " but we require at least %" G_GSIZE_FORMAT " to hold a frame",
233 kmsmem->bo->size, GST_VIDEO_INFO_SIZE (vinfo));
242 GST_ERROR_OBJECT (allocator, "Failed to create buffer object: %s (%d)",
243 strerror (-ret), ret);
251 gst_kms_allocator_free (GstAllocator * allocator, GstMemory * mem)
253 GstKMSAllocator *alloc;
254 GstKMSMemory *kmsmem;
256 alloc = GST_KMS_ALLOCATOR (allocator);
257 kmsmem = (GstKMSMemory *) mem;
259 gst_kms_allocator_memory_reset (alloc, kmsmem);
260 g_slice_free (GstKMSMemory, kmsmem);
264 gst_kms_allocator_set_property (GObject * object, guint prop_id,
265 const GValue * value, GParamSpec * pspec)
267 GstKMSAllocator *alloc;
269 alloc = GST_KMS_ALLOCATOR (object);
273 int fd = g_value_get_int (value);
275 alloc->priv->fd = dup (fd);
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285 gst_kms_allocator_get_property (GObject * object, guint prop_id,
286 GValue * value, GParamSpec * pspec)
288 GstKMSAllocator *alloc;
290 alloc = GST_KMS_ALLOCATOR (object);
294 g_value_set_int (value, alloc->priv->fd);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gst_kms_allocator_finalize (GObject * obj)
305 GstKMSAllocator *alloc;
307 alloc = GST_KMS_ALLOCATOR (obj);
309 gst_kms_allocator_clear_cache (GST_ALLOCATOR (alloc));
311 if (alloc->priv->dmabuf_alloc)
312 gst_object_unref (alloc->priv->dmabuf_alloc);
314 if (check_fd (alloc))
315 close (alloc->priv->fd);
317 G_OBJECT_CLASS (parent_class)->finalize (obj);
321 gst_kms_allocator_class_init (GstKMSAllocatorClass * klass)
323 GObjectClass *gobject_class;
324 GstAllocatorClass *allocator_class;
326 allocator_class = GST_ALLOCATOR_CLASS (klass);
327 gobject_class = G_OBJECT_CLASS (klass);
329 allocator_class->free = gst_kms_allocator_free;
331 gobject_class->set_property = gst_kms_allocator_set_property;
332 gobject_class->get_property = gst_kms_allocator_get_property;
333 gobject_class->finalize = gst_kms_allocator_finalize;
335 g_props[PROP_DRM_FD] = g_param_spec_int ("drm-fd", "DRM fd",
336 "DRM file descriptor", -1, G_MAXINT, -1,
337 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
339 g_object_class_install_properties (gobject_class, PROP_N, g_props);
343 gst_kms_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
345 GstKMSMemory *kmsmem;
346 GstKMSAllocator *alloc;
349 struct drm_mode_map_dumb arg = { 0, };
351 alloc = (GstKMSAllocator *) mem->allocator;
353 if (!check_fd (alloc))
356 kmsmem = (GstKMSMemory *) mem;
360 /* Reuse existing buffer object mapping if possible */
361 if (kmsmem->bo->ptr != NULL) {
365 arg.handle = kmsmem->bo->handle;
367 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_MODE_MAP_DUMB, &arg);
369 GST_ERROR_OBJECT (alloc, "Failed to get offset of buffer object: %s %d",
370 strerror (-err), err);
374 out = mmap (0, kmsmem->bo->size,
375 PROT_READ | PROT_WRITE, MAP_SHARED, alloc->priv->fd, arg.offset);
376 if (out == MAP_FAILED) {
377 GST_ERROR_OBJECT (alloc, "Failed to map dumb buffer object: %s %d",
378 strerror (errno), errno);
381 kmsmem->bo->ptr = out;
384 g_atomic_int_inc (&kmsmem->bo->refs);
385 return kmsmem->bo->ptr;
389 gst_kms_memory_unmap (GstMemory * mem)
391 GstKMSMemory *kmsmem;
393 if (!check_fd ((GstKMSAllocator *) mem->allocator))
396 kmsmem = (GstKMSMemory *) mem;
400 if (g_atomic_int_dec_and_test (&kmsmem->bo->refs)) {
401 munmap (kmsmem->bo->ptr, kmsmem->bo->size);
402 kmsmem->bo->ptr = NULL;
407 gst_kms_allocator_init (GstKMSAllocator * allocator)
411 alloc = GST_ALLOCATOR_CAST (allocator);
413 allocator->priv = gst_kms_allocator_get_instance_private (allocator);
414 allocator->priv->fd = -1;
416 alloc->mem_type = GST_KMS_MEMORY_TYPE;
417 alloc->mem_map = gst_kms_memory_map;
418 alloc->mem_unmap = gst_kms_memory_unmap;
419 /* Use the default, fallback copy function */
421 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
425 gst_kms_allocator_new (int fd)
429 alloc = g_object_new (GST_TYPE_KMS_ALLOCATOR, "name",
430 "KMSMemory::allocator", "drm-fd", fd, NULL);
431 gst_object_ref_sink (alloc);
436 /* The mem_offsets are relative to the GstMemory start, unlike the vinfo->offset
437 * which are relative to the GstBuffer start. */
439 gst_kms_allocator_add_fb (GstKMSAllocator * alloc, GstKMSMemory * kmsmem,
440 gsize in_offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
443 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
444 guint32 w, h, fmt, bo_handles[4] = { 0, };
445 guint32 pitches[4] = { 0, };
446 guint32 offsets[4] = { 0, };
451 w = GST_VIDEO_INFO_WIDTH (vinfo);
452 h = GST_VIDEO_INFO_HEIGHT (vinfo);
453 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
455 for (i = 0; i < num_planes; i++) {
457 bo_handles[i] = kmsmem->bo->handle;
459 bo_handles[i] = kmsmem->gem_handle[i];
461 pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
462 offsets[i] = in_offsets[i];
465 GST_DEBUG_OBJECT (alloc, "bo handles: %d, %d, %d, %d", bo_handles[0],
466 bo_handles[1], bo_handles[2], bo_handles[3]);
468 ret = drmModeAddFB2 (alloc->priv->fd, w, h, fmt, bo_handles, pitches,
469 offsets, &kmsmem->fb_id, 0);
471 GST_ERROR_OBJECT (alloc, "Failed to bind to framebuffer: %s (%d)",
472 strerror (-ret), ret);
480 gst_kms_allocator_bo_alloc (GstAllocator * allocator, GstVideoInfo * vinfo)
482 GstKMSAllocator *alloc;
483 GstKMSMemory *kmsmem;
486 kmsmem = g_slice_new0 (GstKMSMemory);
490 alloc = GST_KMS_ALLOCATOR (allocator);
492 mem = GST_MEMORY_CAST (kmsmem);
494 if (!gst_kms_allocator_memory_create (alloc, kmsmem, vinfo)) {
495 g_slice_free (GstKMSMemory, kmsmem);
499 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
500 kmsmem->bo->size, 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
502 if (!gst_kms_allocator_add_fb (alloc, kmsmem, vinfo->offset, vinfo))
509 gst_memory_unref (mem);
514 gst_kms_allocator_dmabuf_import (GstAllocator * allocator, gint * prime_fds,
515 gint n_planes, gsize offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
517 GstKMSAllocator *alloc;
518 GstKMSMemory *kmsmem;
522 g_return_val_if_fail (n_planes <= GST_VIDEO_MAX_PLANES, FALSE);
524 kmsmem = g_slice_new0 (GstKMSMemory);
528 mem = GST_MEMORY_CAST (kmsmem);
529 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
530 GST_VIDEO_INFO_SIZE (vinfo), 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
532 alloc = GST_KMS_ALLOCATOR (allocator);
533 for (i = 0; i < n_planes; i++) {
534 ret = drmPrimeFDToHandle (alloc->priv->fd, prime_fds[i],
535 &kmsmem->gem_handle[i]);
537 goto import_fd_failed;
540 if (!gst_kms_allocator_add_fb (alloc, kmsmem, offsets, vinfo))
543 for (i = 0; i < n_planes; i++) {
544 struct drm_gem_close arg = { kmsmem->gem_handle[i], };
547 err = drmIoctl (alloc->priv->fd, DRM_IOCTL_GEM_CLOSE, &arg);
549 GST_WARNING_OBJECT (allocator,
550 "Failed to close GEM handle: %s %d", strerror (errno), errno);
552 kmsmem->gem_handle[i] = 0;
560 GST_ERROR_OBJECT (alloc, "Failed to import prime fd %d: %s (%d)",
561 prime_fds[i], strerror (-ret), ret);
567 gst_memory_unref (mem);
573 gst_kms_allocator_dmabuf_export (GstAllocator * allocator, GstMemory * _kmsmem)
575 GstKMSMemory *kmsmem = (GstKMSMemory *) _kmsmem;
576 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
581 /* We can only export DUMB buffers */
582 g_return_val_if_fail (kmsmem->bo, NULL);
585 ret = drmPrimeHandleToFD (alloc->priv->fd, kmsmem->bo->handle,
586 DRM_CLOEXEC | DRM_RDWR, &prime_fd);
588 goto export_fd_failed;
590 if (G_UNLIKELY (alloc->priv->dmabuf_alloc == NULL))
591 alloc->priv->dmabuf_alloc = gst_dmabuf_allocator_new ();
593 mem = gst_dmabuf_allocator_alloc (alloc->priv->dmabuf_alloc, prime_fd,
594 gst_memory_get_sizes (_kmsmem, NULL, NULL));
596 /* Populate the cache so KMSSink can find the kmsmem back when it receives
597 * one of these DMABuf. This call takes ownership of the kmsmem. */
598 gst_kms_allocator_cache (allocator, mem, _kmsmem);
600 GST_DEBUG_OBJECT (alloc, "Exported bo handle %d as %d", kmsmem->bo->handle,
608 GST_ERROR_OBJECT (alloc, "Failed to export bo handle %d: %s (%d)",
609 kmsmem->bo->handle, g_strerror (errno), ret);
614 /* FIXME, using gdata for caching on upstream memory is not tee safe */
616 gst_kms_allocator_get_cached (GstMemory * mem)
618 return gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
619 g_quark_from_static_string ("kmsmem"));
623 cached_kmsmem_disposed_cb (GstKMSAllocator * alloc, GstMiniObject * obj)
625 GST_OBJECT_LOCK (alloc);
626 alloc->priv->mem_cache = g_list_remove (alloc->priv->mem_cache, obj);
627 GST_OBJECT_UNLOCK (alloc);
631 gst_kms_allocator_clear_cache (GstAllocator * allocator)
633 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
636 GST_OBJECT_LOCK (alloc);
638 iter = alloc->priv->mem_cache;
640 GstMiniObject *obj = iter->data;
641 gst_mini_object_weak_unref (obj,
642 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
643 gst_mini_object_set_qdata (obj,
644 g_quark_from_static_string ("kmsmem"), NULL, NULL);
648 g_list_free (alloc->priv->mem_cache);
649 alloc->priv->mem_cache = NULL;
651 GST_OBJECT_UNLOCK (alloc);
654 /* @kmsmem is transfer-full */
656 gst_kms_allocator_cache (GstAllocator * allocator, GstMemory * mem,
659 GstKMSAllocator *alloc = GST_KMS_ALLOCATOR (allocator);
661 GST_OBJECT_LOCK (alloc);
662 gst_mini_object_weak_ref (GST_MINI_OBJECT (mem),
663 (GstMiniObjectNotify) cached_kmsmem_disposed_cb, alloc);
664 alloc->priv->mem_cache = g_list_prepend (alloc->priv->mem_cache, mem);
665 GST_OBJECT_UNLOCK (alloc);
667 gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
668 g_quark_from_static_string ("kmsmem"), kmsmem,
669 (GDestroyNotify) gst_memory_unref);