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.
31 #include <xf86drmMode.h>
35 #include "gstkmsallocator.h"
36 #include "gstkmsutils.h"
38 #define GST_CAT_DEFAULT kmsallocator_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41 #define GST_KMS_MEMORY_TYPE "KMSMemory"
43 struct _GstKMSAllocatorPrivate
46 struct kms_driver *driver;
49 #define parent_class gst_kms_allocator_parent_class
50 G_DEFINE_TYPE_WITH_CODE (GstKMSAllocator, gst_kms_allocator, GST_TYPE_ALLOCATOR,
51 G_ADD_PRIVATE (GstKMSAllocator);
52 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "kmsallocator", 0,
61 static GParamSpec *g_props[PROP_N] = { NULL, };
64 gst_is_kms_memory (GstMemory * mem)
66 return gst_memory_is_type (mem, GST_KMS_MEMORY_TYPE);
70 gst_kms_memory_get_fb_id (GstMemory * mem)
72 if (!gst_is_kms_memory (mem))
74 return ((GstKMSMemory *) mem)->fb_id;
78 ensure_kms_driver (GstKMSAllocator * alloc)
80 GstKMSAllocatorPrivate *priv;
91 err = kms_create (priv->fd, &priv->driver);
93 GST_ERROR_OBJECT (alloc, "Could not create KMS driver: %s",
102 gst_kms_allocator_memory_reset (GstKMSAllocator * allocator, GstKMSMemory * mem)
105 GST_DEBUG_OBJECT (allocator, "removing fb id %d", mem->fb_id);
106 drmModeRmFB (allocator->priv->fd, mem->fb_id);
110 if (!ensure_kms_driver (allocator))
114 kms_bo_destroy (&mem->bo);
120 gst_kms_allocator_memory_create (GstKMSAllocator * allocator,
121 GstKMSMemory * kmsmem, GstVideoInfo * vinfo)
125 KMS_WIDTH, GST_VIDEO_INFO_WIDTH (vinfo),
126 KMS_HEIGHT, GST_VIDEO_INFO_HEIGHT (vinfo),
127 KMS_TERMINATE_PROP_LIST,
133 if (!ensure_kms_driver (allocator))
136 ret = kms_bo_create (allocator->priv->driver, attrs, &kmsmem->bo);
138 GST_ERROR_OBJECT (allocator, "Failed to create buffer object: %s (%d)",
139 strerror (-ret), ret);
147 gst_kms_allocator_free (GstAllocator * allocator, GstMemory * mem)
149 GstKMSAllocator *alloc;
150 GstKMSMemory *kmsmem;
152 alloc = GST_KMS_ALLOCATOR (allocator);
153 kmsmem = (GstKMSMemory *) mem;
155 gst_kms_allocator_memory_reset (alloc, kmsmem);
156 g_slice_free (GstKMSMemory, kmsmem);
160 gst_kms_allocator_set_property (GObject * object, guint prop_id,
161 const GValue * value, GParamSpec * pspec)
163 GstKMSAllocator *alloc;
165 alloc = GST_KMS_ALLOCATOR (object);
169 int fd = g_value_get_int (value);
171 alloc->priv->fd = dup (fd);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 gst_kms_allocator_get_property (GObject * object, guint prop_id,
182 GValue * value, GParamSpec * pspec)
184 GstKMSAllocator *alloc;
186 alloc = GST_KMS_ALLOCATOR (object);
190 g_value_set_int (value, alloc->priv->fd);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gst_kms_allocator_finalize (GObject * obj)
201 GstKMSAllocator *alloc;
203 alloc = GST_KMS_ALLOCATOR (obj);
205 if (alloc->priv->driver)
206 kms_destroy (&alloc->priv->driver);
208 if (alloc->priv->fd > -1)
209 close (alloc->priv->fd);
211 G_OBJECT_CLASS (parent_class)->finalize (obj);
215 gst_kms_allocator_class_init (GstKMSAllocatorClass * klass)
217 GObjectClass *gobject_class;
218 GstAllocatorClass *allocator_class;
220 allocator_class = GST_ALLOCATOR_CLASS (klass);
221 gobject_class = G_OBJECT_CLASS (klass);
223 allocator_class->free = gst_kms_allocator_free;
225 gobject_class->set_property = gst_kms_allocator_set_property;
226 gobject_class->get_property = gst_kms_allocator_get_property;
227 gobject_class->finalize = gst_kms_allocator_finalize;
229 g_props[PROP_DRM_FD] = g_param_spec_int ("drm-fd", "DRM fd",
230 "DRM file descriptor", -1, G_MAXINT, -1,
231 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
233 g_object_class_install_properties (gobject_class, PROP_N, g_props);
237 gst_kms_memory_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
239 GstKMSMemory *kmsmem;
243 if (!ensure_kms_driver ((GstKMSAllocator *) mem->allocator))
246 kmsmem = (GstKMSMemory *) mem;
251 err = kms_bo_map (kmsmem->bo, &out);
253 GST_ERROR ("could not map memory: %s %d", strerror (-err), err);
261 gst_kms_memory_unmap (GstMemory * mem)
263 GstKMSMemory *kmsmem;
265 if (!ensure_kms_driver ((GstKMSAllocator *) mem->allocator))
268 kmsmem = (GstKMSMemory *) mem;
270 kms_bo_unmap (kmsmem->bo);
274 gst_kms_allocator_init (GstKMSAllocator * allocator)
278 alloc = GST_ALLOCATOR_CAST (allocator);
280 allocator->priv = gst_kms_allocator_get_instance_private (allocator);
281 allocator->priv->fd = -1;
283 alloc->mem_type = GST_KMS_MEMORY_TYPE;
284 alloc->mem_map = gst_kms_memory_map;
285 alloc->mem_unmap = gst_kms_memory_unmap;
286 /* Use the default, fallback copy function */
288 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
292 gst_kms_allocator_new (int fd)
294 return g_object_new (GST_TYPE_KMS_ALLOCATOR, "name",
295 "KMSMemory::allocator", "drm-fd", fd, NULL);
298 /* The mem_offsets are relative to the GstMemory start, unlike the vinfo->offset
299 * which are relative to the GstBuffer start. */
301 gst_kms_allocator_add_fb (GstKMSAllocator * alloc, GstKMSMemory * kmsmem,
302 gsize mem_offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
305 gint num_planes = GST_VIDEO_INFO_N_PLANES (vinfo);
306 guint32 w, h, fmt, pitch = 0, bo_handles[4] = { 0, };
307 guint32 offsets[4] = { 0, };
308 guint32 pitches[4] = { 0, };
313 w = GST_VIDEO_INFO_WIDTH (vinfo);
314 h = GST_VIDEO_INFO_HEIGHT (vinfo);
315 fmt = gst_drm_format_from_video (GST_VIDEO_INFO_FORMAT (vinfo));
318 kms_bo_get_prop (kmsmem->bo, KMS_HANDLE, &bo_handles[0]);
319 for (i = 1; i < num_planes; i++)
320 bo_handles[i] = bo_handles[0];
322 /* Get the bo pitch calculated by the kms driver.
323 * If it's defined, it will overwrite the video info's stride */
324 kms_bo_get_prop (kmsmem->bo, KMS_PITCH, &pitch);
326 for (i = 0; i < num_planes; i++)
327 bo_handles[i] = kmsmem->gem_handle[i];
330 GST_DEBUG_OBJECT (alloc, "bo handles: %d, %d, %d, %d", bo_handles[0],
331 bo_handles[1], bo_handles[2], bo_handles[3]);
333 for (i = 0; i < num_planes; i++) {
334 offsets[i] = mem_offsets[i];
336 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = pitch;
337 pitches[i] = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i);
338 GST_DEBUG_OBJECT (alloc, "Create FB plane %i with stride %u and offset %u",
339 i, pitches[i], offsets[i]);
342 ret = drmModeAddFB2 (alloc->priv->fd, w, h, fmt, bo_handles, pitches,
343 offsets, &kmsmem->fb_id, 0);
345 GST_ERROR_OBJECT (alloc, "Failed to bind to framebuffer: %s (%d)",
346 strerror (-ret), ret);
353 gst_kms_allocator_alloc_empty (GstAllocator * allocator, GstVideoInfo * vinfo)
355 GstKMSMemory *kmsmem;
358 kmsmem = g_slice_new0 (GstKMSMemory);
361 mem = GST_MEMORY_CAST (kmsmem);
363 gst_memory_init (mem, GST_MEMORY_FLAG_NO_SHARE, allocator, NULL,
364 GST_VIDEO_INFO_SIZE (vinfo), 0, 0, GST_VIDEO_INFO_SIZE (vinfo));
370 gst_kms_allocator_bo_alloc (GstAllocator * allocator, GstVideoInfo * vinfo)
372 GstKMSAllocator *alloc;
373 GstKMSMemory *kmsmem;
376 mem = gst_kms_allocator_alloc_empty (allocator, vinfo);
380 alloc = GST_KMS_ALLOCATOR (allocator);
381 kmsmem = (GstKMSMemory *) mem;
382 if (!gst_kms_allocator_memory_create (alloc, kmsmem, vinfo))
384 if (!gst_kms_allocator_add_fb (alloc, kmsmem, vinfo->offset, vinfo))
391 gst_memory_unref (mem);
396 gst_kms_allocator_dmabuf_import (GstAllocator * allocator, gint * prime_fds,
397 gint n_planes, gsize offsets[GST_VIDEO_MAX_PLANES], GstVideoInfo * vinfo)
399 GstKMSAllocator *alloc;
404 g_return_val_if_fail (n_planes <= GST_VIDEO_MAX_PLANES, FALSE);
406 mem = gst_kms_allocator_alloc_empty (allocator, vinfo);
410 tmp = (GstKMSMemory *) mem;
411 alloc = GST_KMS_ALLOCATOR (allocator);
412 for (i = 0; i < n_planes; i++) {
413 ret = drmPrimeFDToHandle (alloc->priv->fd, prime_fds[i],
414 &tmp->gem_handle[i]);
416 goto import_fd_failed;
419 if (!gst_kms_allocator_add_fb (alloc, tmp, offsets, vinfo))
427 GST_ERROR_OBJECT (alloc, "Failed to import prime fd %d: %s (%d)",
428 prime_fds[i], strerror (-ret), ret);
434 gst_memory_unref (mem);