1 From c5d115bf88f0d2fb64f58b21b72a3195eae98d0a Mon Sep 17 00:00:00 2001
2 From: Pooja Prajod <a0132412@ti.com>
3 Date: Fri, 20 Jan 2017 14:41:45 +0530
4 Subject: [PATCH 1/5] gstdrmallocator: Add DRM allocator support
6 Add DRM based allocator support.
8 The following changes are included :
9 1. Use DRM dumb buffers and associated APIs for
11 2. Have DRM device fd a member of allocator object
12 3. Allocate GstMemory objects with mem_type as 'dmabuf'
14 Signed-off-by: Pooja Prajod <a0132412@ti.com>
17 gst-libs/gst/Makefile.am | 4 +-
18 gst-libs/gst/drm/Makefile.am | 33 +++++
19 gst-libs/gst/drm/gstdrmallocator.c | 206 ++++++++++++++++++++++++++++++
20 gst-libs/gst/drm/gstdrmallocator.h | 77 +++++++++++
21 pkgconfig/Makefile.am | 3 +
22 pkgconfig/gstreamer-drm-uninstalled.pc.in | 11 ++
23 pkgconfig/gstreamer-drm.pc.in | 12 ++
24 8 files changed, 349 insertions(+), 2 deletions(-)
25 create mode 100644 gst-libs/gst/drm/Makefile.am
26 create mode 100644 gst-libs/gst/drm/gstdrmallocator.c
27 create mode 100644 gst-libs/gst/drm/gstdrmallocator.h
28 create mode 100644 pkgconfig/gstreamer-drm-uninstalled.pc.in
29 create mode 100644 pkgconfig/gstreamer-drm.pc.in
31 diff --git a/configure.ac b/configure.ac
32 index 5e85e56..e254605 100644
35 @@ -621,6 +621,8 @@ if test x$with_egl_module_name != x; then
36 AC_DEFINE_UNQUOTED(GST_GL_LIBEGL_MODULE_NAME, "$with_egl_module_name", [EGL module name])
39 +PKG_CHECK_MODULES([DRM], [libdrm libdrm_omap], HAVE_KMS=yes, HAVE_KMS=no)
42 [ --enable-wgl Enable WGL support @<:@default=auto@:>@],
43 [case "${enableval}" in
44 @@ -3590,6 +3592,7 @@ gst-libs/Makefile
46 gst-libs/gst/adaptivedemux/Makefile
47 gst-libs/gst/basecamerabinsrc/Makefile
48 +gst-libs/gst/drm/Makefile
49 gst-libs/gst/gl/Makefile
50 gst-libs/gst/gl/android/Makefile
51 gst-libs/gst/gl/cocoa/Makefile
52 @@ -3749,6 +3752,8 @@ pkgconfig/gstreamer-plugins-bad.pc
53 pkgconfig/gstreamer-plugins-bad-uninstalled.pc
54 pkgconfig/gstreamer-codecparsers.pc
55 pkgconfig/gstreamer-codecparsers-uninstalled.pc
56 +pkgconfig/gstreamer-drm.pc
57 +pkgconfig/gstreamer-drm-uninstalled.pc
58 pkgconfig/gstreamer-insertbin.pc
59 pkgconfig/gstreamer-insertbin-uninstalled.pc
60 pkgconfig/gstreamer-gl.pc
61 diff --git a/gst-libs/gst/Makefile.am b/gst-libs/gst/Makefile.am
62 index 7d0b309..5ec3967 100644
63 --- a/gst-libs/gst/Makefile.am
64 +++ b/gst-libs/gst/Makefile.am
65 @@ -11,7 +11,7 @@ WAYLAND_DIR=wayland
68 SUBDIRS = uridownloader adaptivedemux interfaces basecamerabinsrc codecparsers \
69 - insertbin mpegts base video audio player $(GL_DIR) $(WAYLAND_DIR)
70 + insertbin mpegts base video audio player drm $(GL_DIR) $(WAYLAND_DIR)
72 noinst_HEADERS = gst-i18n-plugin.h gettext.h glib-compat-private.h
73 DIST_SUBDIRS = uridownloader adaptivedemux interfaces gl basecamerabinsrc \
74 @@ -24,7 +24,7 @@ adaptivedemux: uridownloader
76 INDEPENDENT_SUBDIRS = \
77 interfaces basecamerabinsrc codecparsers insertbin uridownloader \
78 - mpegts base player $(GL_DIR) $(WAYLAND_DIR)
79 + mpegts base player drm $(GL_DIR) $(WAYLAND_DIR)
81 .PHONY: independent-subdirs $(INDEPENDENT_SUBDIRS)
83 diff --git a/gst-libs/gst/drm/Makefile.am b/gst-libs/gst/drm/Makefile.am
85 index 0000000..9a45dfb
87 +++ b/gst-libs/gst/drm/Makefile.am
89 +lib_LTLIBRARIES = libgstdrm-@GST_API_VERSION@.la
91 +CLEANFILES = $(BUILT_SOURCES)
93 +libgstdrm_@GST_API_VERSION@_la_SOURCES = \
96 +libgstdrm_@GST_API_VERSION@includedir = \
97 + $(includedir)/gstreamer-@GST_API_VERSION@/gst/drm
99 +libgstdrm_@GST_API_VERSION@include_HEADERS = \
102 +libgstdrm_@GST_API_VERSION@_la_CFLAGS = \
104 + $(OMAPDRM_CFLAGS) \
105 + $(GST_PLUGINS_BAD_CFLAGS) \
106 + $(GST_PLUGINS_BASE_CFLAGS) \
107 + -DGST_USE_UNSTABLE_API \
110 +libgstdrm_@GST_API_VERSION@_la_LIBADD = \
112 + $(GST_PLUGINS_BASE_LIBS) \
114 + -lgstallocators-$(GST_API_VERSION) \
117 +libgstdrm_@GST_API_VERSION@_la_LDFLAGS = \
119 + $(GST_LIB_LDFLAGS) \
120 + $(GST_ALL_LDFLAGS) \
122 diff --git a/gst-libs/gst/drm/gstdrmallocator.c b/gst-libs/gst/drm/gstdrmallocator.c
124 index 0000000..b557ad2
126 +++ b/gst-libs/gst/drm/gstdrmallocator.c
131 + * Copyright (C) 2012 Texas Instruments
134 + * Pooja Prajod <poojaprajod@ti.com>
136 + * This library is free software; you can redistribute it and/or
137 + * modify it under the terms of the GNU Lesser General Public
138 + * License as published by the Free Software Foundation
139 + * version 2.1 of the License.
141 + * This library is distributed in the hope that it will be useful,
142 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
143 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
144 + * Lesser General Public License for more details.
146 + * You should have received a copy of the GNU Lesser General Public
147 + * License along with this library; if not, write to the Free Software
148 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
152 + * SECTION:GstDRMAllocator
153 + * @short_description: GStreamer DRM allocator support
159 +#include "gstdrmallocator.h"
165 +#include <sys/mman.h>
166 +#include <sys/types.h>
168 +#define INVALID_DRM_FD (-1)
170 +GST_DEBUG_CATEGORY (drmallocator_debug);
171 +#define GST_CAT_DEFAULT drmallocator_debug
173 +#define gst_drm_allocator_parent_class parent_class
174 +G_DEFINE_TYPE (GstDRMAllocator, gst_drm_allocator, GST_TYPE_FD_ALLOCATOR);
177 +gst_drm_allocator_alloc (GstAllocator * allocator, gsize size,
178 + GstAllocationParams * params)
180 + GstDRMAllocator *self = GST_DRM_ALLOCATOR (allocator);
182 + int DrmDeviceFD = self->DrmDeviceFD;
184 + /* Variable for DRM Dumb Buffers */
186 + struct drm_mode_create_dumb creq;
187 + struct drm_mode_destroy_dumb dreq;
190 + GST_LOG_OBJECT (self, "DRM Memory alloc");
192 + memset(&creq, 0, sizeof(struct drm_mode_create_dumb));
194 + We have only total size as argument to _allocator_alloc.
195 + Since the DDR storage is linear, it is as good as saying
196 + the buffer is of width = size and height = 1
202 + /* Create a DRM dumb buffer */
203 + ret = drmIoctl (DrmDeviceFD, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
205 + GST_ERROR_OBJECT (self, "Create DRM dumb buffer failed");
208 + /* Get a dmabuf fd from the dumb buffer handle */
209 + drmPrimeHandleToFD (DrmDeviceFD, creq.handle, DRM_CLOEXEC | O_RDWR, &fd);
212 + GST_ERROR_OBJECT (self, "Invalid fd returned: %d", fd);
216 + /* Get a dmabuf gstmemory with the fd */
217 + mem = gst_fd_allocator_alloc (allocator, fd, size, 0);
219 + if (G_UNLIKELY (!mem)) {
220 + GST_ERROR_OBJECT (self, "GstDmaBufMemory allocation failed");
228 + memset(&dreq, 0, sizeof(struct drm_mode_destroy_dumb));
229 + dreq.handle = creq.handle;
230 + drmIoctl (DrmDeviceFD, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
235 +gst_drm_allocator_free (GstAllocator * allocator, GstMemory * mem)
237 + GstDRMAllocator *self = GST_DRM_ALLOCATOR (allocator);
238 + uint32_t handle = 0;
239 + int DrmDeviceFD = self->DrmDeviceFD;
242 + GST_LOG_OBJECT (self, "DRM Memory free");
244 + g_return_if_fail (GST_IS_ALLOCATOR (allocator));
245 + g_return_if_fail (mem != NULL);
246 + g_return_if_fail (gst_is_drm_memory (mem));
248 + fd = gst_fd_memory_get_fd (mem);
249 + drmPrimeFDToHandle(DrmDeviceFD, fd, &handle);
251 + /* Incase there are some mapped memory, we unmap and ready it to be cleaned*/
252 + GST_ALLOCATOR_CLASS (parent_class)->free (allocator, mem);
255 + struct drm_mode_destroy_dumb dreq;
256 + memset(&dreq, 0, sizeof(struct drm_mode_destroy_dumb));
257 + dreq.handle = handle;
258 + drmIoctl (DrmDeviceFD, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
265 +gst_drm_allocator_finalize (GObject * obj)
267 + GstDRMAllocator *self = GST_DRM_ALLOCATOR (obj);
268 + GST_LOG_OBJECT (obj, "DRM Allocator finalize");
270 + close (self->DrmDeviceFD);
271 + self->DrmDeviceFD = INVALID_DRM_FD;
273 + G_OBJECT_CLASS (parent_class)->finalize (obj);
277 +gst_drm_allocator_class_init (GstDRMAllocatorClass * klass)
279 + GstAllocatorClass *drm_alloc = (GstAllocatorClass *) klass;
281 + drm_alloc->alloc = GST_DEBUG_FUNCPTR (gst_drm_allocator_alloc);
282 + drm_alloc->free = GST_DEBUG_FUNCPTR (gst_drm_allocator_free);
283 + GST_DEBUG_CATEGORY_INIT (drmallocator_debug, "drmallocator", 0,
284 + "GstDRMAllocator debug");
289 +gst_drm_allocator_init (GstDRMAllocator * self)
291 + GstAllocator *alloc = GST_ALLOCATOR_CAST (self);
292 + GObjectClass *object_class = G_OBJECT_CLASS (GST_DRM_ALLOCATOR_GET_CLASS(self));
294 + if (self->DrmDeviceFD <= 0) {
295 + self->DrmDeviceFD = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
296 + if (self->DrmDeviceFD < 0) {
297 + GST_ERROR_OBJECT (self, "Failed to open DRM device");
299 + drmDropMaster (self->DrmDeviceFD);
303 + alloc->mem_type = GST_ALLOCATOR_DMABUF;
305 + object_class->finalize = gst_drm_allocator_finalize;
307 + GST_OBJECT_FLAG_UNSET (self, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
311 +gst_drm_allocator_register (void)
313 + gst_allocator_register (GST_ALLOCATOR_DRM,
314 + g_object_new (GST_TYPE_DRM_ALLOCATOR, NULL));
318 +gst_drm_allocator_get (void)
320 + GstAllocator *alloc;
321 + alloc = gst_allocator_find (GST_ALLOCATOR_DRM);
323 + gst_drm_allocator_register();
324 + alloc = gst_allocator_find (GST_ALLOCATOR_DRM);
330 +gst_is_drm_memory (GstMemory * mem)
332 + return gst_memory_is_type (mem, GST_ALLOCATOR_DMABUF);
334 diff --git a/gst-libs/gst/drm/gstdrmallocator.h b/gst-libs/gst/drm/gstdrmallocator.h
336 index 0000000..1ca93ba
338 +++ b/gst-libs/gst/drm/gstdrmallocator.h
343 + * Copyright (C) 2012 Texas Instruments
346 + * Pooja Prajod <poojaprajod@ti.com>
348 + * This library is free software; you can redistribute it and/or
349 + * modify it under the terms of the GNU Lesser General Public
350 + * License as published by the Free Software Foundation
351 + * version 2.1 of the License.
353 + * This library is distributed in the hope that it will be useful,
354 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
355 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
356 + * Lesser General Public License for more details.
358 + * You should have received a copy of the GNU Lesser General Public
359 + * License along with this library; if not, write to the Free Software
360 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
364 + * SECTION:GstDRMAllocator
365 + * @short_description: GStreamer DRM allocator support
370 +#ifndef __GSTDRMALLOCATOR_H__
371 +#define __GSTDRMALLOCATOR_H__
373 +#include <gst/gst.h>
374 +#include <gst/video/video.h>
375 +#include <gst/allocators/allocators.h>
378 +#include <xf86drm.h>
379 +#include <xf86drmMode.h>
384 +#define GST_TYPE_DRM_ALLOCATOR (gst_drm_allocator_get_type ())
385 +#define GST_DRM_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_DRM_ALLOCATOR, GstDRMAllocator))
386 +#define GST_IS_DRM_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_DRM_ALLOCATOR))
387 +#define GST_DRM_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_DRM_ALLOCATOR, GstDRMAllocatorClass))
388 +#define GST_IS_DRM_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_DRM_ALLOCATOR))
389 +#define GST_DRM_ALLOCATOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_DRM_ALLOCATOR, GstDRMAllocatorClass))
391 +#define GST_ALLOCATOR_DRM "DRM"
393 +typedef struct _GstDRMAllocator GstDRMAllocator;
394 +typedef struct _GstDRMAllocatorClass GstDRMAllocatorClass;
396 +struct _GstDRMAllocator
398 + GstFdAllocator parent;
402 +struct _GstDRMAllocatorClass
404 + GstFdAllocatorClass parent_class;
407 +void gst_drm_allocator_register (void);
408 +GstAllocator * gst_drm_allocator_get (void);
410 +gboolean gst_is_drm_memory (GstMemory * mem);
412 +GType gst_drm_allocator_get_type (void);
416 +#endif /* __GSTDRMALLOCATOR_H__ */
417 diff --git a/pkgconfig/Makefile.am b/pkgconfig/Makefile.am
418 index 88c7e52..5fdccbf 100644
419 --- a/pkgconfig/Makefile.am
420 +++ b/pkgconfig/Makefile.am
422 ### all of the standard pc files we need to generate
424 gstreamer-plugins-bad-@GST_API_VERSION@.pc \
425 + gstreamer-drm-@GST_API_VERSION@.pc \
426 gstreamer-codecparsers-@GST_API_VERSION@.pc \
427 gstreamer-insertbin-@GST_API_VERSION@.pc \
428 gstreamer-mpegts-@GST_API_VERSION@.pc \
429 @@ -12,6 +13,7 @@ pcverfiles = \
431 pcverfiles_uninstalled = \
432 gstreamer-plugins-bad-@GST_API_VERSION@-uninstalled.pc \
433 + gstreamer-drm-@GST_API_VERSION@-uninstalled.pc \
434 gstreamer-codecparsers-@GST_API_VERSION@-uninstalled.pc \
435 gstreamer-insertbin-@GST_API_VERSION@-uninstalled.pc \
436 gstreamer-mpegts-@GST_API_VERSION@-uninstalled.pc \
437 @@ -43,6 +45,7 @@ pkgconfig_DATA = $(pcverfiles)
438 CLEANFILES = $(pcverfiles) $(pcverfiles_uninstalled)
440 gstreamer-plugins-bad.pc.in gstreamer-plugins-bad-uninstalled.pc.in \
441 + gstreamer-drm.pc.in gstreamer-drm-uninstalled.pc.in \
442 gstreamer-codecparsers.pc.in gstreamer-codecparsers-uninstalled.pc.in \
443 gstreamer-gl.pc.in gstreamer-gl-uninstalled.pc.in \
444 gstreamer-insertbin.pc.in gstreamer-insertbin-uninstalled.pc.in \
445 diff --git a/pkgconfig/gstreamer-drm-uninstalled.pc.in b/pkgconfig/gstreamer-drm-uninstalled.pc.in
447 index 0000000..9dcf978
449 +++ b/pkgconfig/gstreamer-drm-uninstalled.pc.in
453 +libdir=${pcfiledir}/../gst-libs/gst/drm
454 +includedir=${pcfiledir}/../gst-libs
456 +Name: GStreamer DRM Allocator, Uninstalled
457 +Description: DRM Allocator for GStreamer elements, uninstalled
458 +Requires: gstreamer-@GST_MAJORMINOR@ gstreamer-base-@GST_MAJORMINOR@
460 +Libs: -L${libdir} ${libdir}/libgstdrm-@GST_MAJORMINOR@.la
461 +Cflags: -I${includedir}
462 diff --git a/pkgconfig/gstreamer-drm.pc.in b/pkgconfig/gstreamer-drm.pc.in
464 index 0000000..d155e80
466 +++ b/pkgconfig/gstreamer-drm.pc.in
469 +exec_prefix=@exec_prefix@
471 +includedir=@includedir@/gstreamer-@GST_MAJORMINOR@
473 +Name: GStreamer DRM Allocator
474 +Description: DRM Allocator for GStreamer elements
475 +Requires: gstreamer-@GST_MAJORMINOR@ gstreamer-base-@GST_MAJORMINOR@
477 +Libs: -L${libdir} -lgstdrm-@GST_MAJORMINOR@
478 +Cflags: -I${includedir}