base:tizenmemory: Add new interface for fd 26/283826/6 accepted/tizen/unified/20221109.171118
authorJeongmo Yang <jm80.yang@samsung.com>
Thu, 3 Nov 2022 11:18:29 +0000 (20:18 +0900)
committerJeongmo Yang <jm80.yang@samsung.com>
Mon, 7 Nov 2022 23:39:31 +0000 (08:39 +0900)
[Version] 1.20.0-40
[Issue Type] New feature

Change-Id: I4930955c18d8814e8ca6e54deab06b4f2d39b187
Signed-off-by: Jeongmo Yang <jm80.yang@samsung.com>
packaging/gstreamer.spec
subprojects/gst-plugins-base/gst-libs/gst/allocators/gsttizenmemory.c
subprojects/gst-plugins-base/gst-libs/gst/allocators/gsttizenmemory.h

index 4197cd4..bb74324 100644 (file)
@@ -62,7 +62,7 @@
 
 Name:           %{_name}
 Version:        1.20.0
-Release:        39
+Release:        40
 Summary:        Streaming-Media Framework Runtime
 License:        LGPL-2.0+
 Group:          Multimedia/Framework
index 0ce0ca5..f108292 100644 (file)
@@ -176,6 +176,75 @@ _tizen_video_mem_new2 (GstAllocator * allocator, GstMemory * parent, GstVideoInf
   return tmem;
 }
 
+static GstTizenMemory *
+_tizen_video_mem_new3 (GstAllocator * allocator, GstMemory * parent, GstVideoInfo * vinfo,
+    tbm_surface_info_s * tsinfo, int fd[GST_TIZEN_MEMORY_MAX_FD], int fd_count,
+    gpointer user_data, GDestroyNotify notify)
+{
+  int i = 0;
+  tbm_bo bos[GST_TIZEN_MEMORY_MAX_FD] = {NULL, };
+  tbm_surface_h surface = NULL;
+  GstTizenMemory *tmem;
+  GstTizenAllocator *tallocator = GST_TIZEN_ALLOCATOR (allocator);
+
+  if (!vinfo || !tsinfo || fd_count < 1 || fd_count > GST_TIZEN_MEMORY_MAX_FD) {
+    GST_ERROR ("invalid param[vinfo:%p,tsinfo:%p, fd_count:%d]", vinfo, tsinfo, fd_count);
+    return NULL;
+  }
+
+  if (!tallocator->bufmgr) {
+    GST_ERROR ("No TBM bufmgr");
+    return NULL;
+  }
+
+  for (i = 0 ; i < fd_count ; i++) {
+    bos[i] = tbm_bo_import_fd (tallocator->bufmgr, fd[i]);
+    if (!bos[i]) {
+      GST_ERROR ("failed to import fd[%d]", fd[i]);
+      goto _TIZEN_VIDEO_MEM_NEW3_FAILED;
+    }
+  }
+
+  surface = tbm_surface_internal_create_with_bos (tsinfo, bos, fd_count);
+  if (!surface) {
+    GST_ERROR ("failed to create surface");
+    goto _TIZEN_VIDEO_MEM_NEW3_FAILED;
+  }
+
+  tmem = g_slice_new0 (GstTizenMemory);
+
+  gst_memory_init (GST_MEMORY_CAST (tmem), 0,
+    allocator, parent, tsinfo->size, 0, 0, tsinfo->size);
+
+  /* bos[] will be kept in tbm surface and released when surface is released finally. */
+  for (i = 0 ; i < fd_count ; i++) {
+    tbm_bo_unref (bos[i]);
+    bos[i] = NULL;
+    tmem->fd[i] = fd[i];
+  }
+
+  tmem->surface = surface;
+  tmem->fd_count = fd_count;
+  tmem->info = gst_video_info_copy (vinfo);
+  tmem->notify = notify;
+  tmem->user_data = user_data;
+
+  GST_VIDEO_INFO_SIZE (tmem->info) = tsinfo->size;
+
+  g_mutex_init (&tmem->lock);
+
+  GST_DEBUG ("mem[%p], fd[0][%d], fd_count[%d], size[%" G_GSIZE_FORMAT"], max[%" G_GSIZE_FORMAT"]",
+    tmem, tmem->fd[0], tmem->fd_count, tmem->mem.size, tmem->mem.maxsize);
+
+  return tmem;
+
+_TIZEN_VIDEO_MEM_NEW3_FAILED:
+  for (i = 0 ; i < fd_count && bos[i] ; i++)
+    tbm_bo_unref (bos[i]);
+
+  return NULL;
+}
+
 static void
 gst_tizen_mem_free (GstAllocator * allocator, GstMemory * mem)
 {
@@ -296,6 +365,9 @@ gst_tizen_allocator_finalize (GObject *obj)
   g_list_free (allocator->priv->mem_cache);
   allocator->priv->mem_cache = NULL;
 
+  tbm_bufmgr_deinit (allocator->bufmgr);
+  allocator->bufmgr = NULL;
+
   G_OBJECT_CLASS (parent_class)->finalize (obj);
 }
 
@@ -419,6 +491,10 @@ gst_tizen_allocator_init (GstTizenAllocator * allocator)
   alloc->mem_copy = (GstMemoryCopyFunction) gst_tizen_mem_copy;
 
   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
+
+  allocator->bufmgr = tbm_bufmgr_init (-1);
+  if (!allocator->bufmgr)
+    GST_ERROR ("TBM bufmgr failed");
 }
 
 /**
@@ -491,6 +567,28 @@ gst_tizen_allocator_alloc_bo (GstAllocator * allocator, GstVideoInfo * vinfo,
   return (GstMemory *)_tizen_video_mem_new2 (allocator, NULL, vinfo, bo, size, user_data, notify);
 }
 
+/**
+ * gst_tizen_allocator_alloc_fd: (skip)
+ * @allocator: a #GstAllocator to use
+ * @vinfo: a #GstVideoInfo to be referred for memory size and type
+ * @tsinfo: a #tbm_surface_info_s to be referred for information of TBM surface
+ * @fd: fd list to be used for allocated memory
+ * @fd_count: a count of fd in list
+ * @user_data: (allow-none): user data pointer
+ * @notify: (allow-none) (closure user_data): called with @user_data when the memory is freed
+ *
+ * Returns: (transfer full) (nullable): a new #GstMemory.
+ */
+GstMemory *
+gst_tizen_allocator_alloc_fd (GstAllocator * allocator, GstVideoInfo * vinfo,
+    tbm_surface_info_s * tsinfo, int fd[GST_TIZEN_MEMORY_MAX_FD], int fd_count,
+    gpointer user_data, GDestroyNotify notify)
+{
+  g_return_val_if_fail (GST_IS_TIZEN_ALLOCATOR (allocator), NULL);
+
+  return (GstMemory *)_tizen_video_mem_new3 (allocator, NULL, vinfo, tsinfo, fd, fd_count, user_data, notify);
+}
+
 gboolean
 gst_is_tizen_memory (GstMemory * mem)
 {
@@ -556,6 +654,35 @@ gst_tizen_memory_get_surface (GstMemory * mem)
   return tmem->surface;
 }
 
+gint
+gst_tizen_memory_get_num_fd (GstMemory * mem)
+{
+  GstTizenMemory *tmem;
+
+  g_return_val_if_fail (mem != NULL, -1);
+  g_return_val_if_fail (GST_IS_TIZEN_ALLOCATOR (mem->allocator), -1);
+
+  tmem = (GstTizenMemory *)mem;
+
+  GST_DEBUG ("fd count[%d] in mem[%p]", tmem->fd_count, tmem);
+
+  return tmem->fd_count;
+}
+
+gint
+gst_tizen_memory_get_fd (GstMemory * mem, gint index)
+{
+  GstTizenMemory *tmem = (GstTizenMemory *)mem;
+
+  g_return_val_if_fail (mem != NULL, -1);
+  g_return_val_if_fail (GST_IS_TIZEN_ALLOCATOR (mem->allocator), -1);
+  g_return_val_if_fail (index >= 0 && index < tmem->fd_count, -1);
+
+  GST_DEBUG ("fd[%d] %d in mem[%p]", index, tmem->fd[index], tmem);
+
+  return tmem->fd[index];
+}
+
 static void
 cached_tizen_disposed_cb (GstTizenAllocator * allocator, GstMiniObject *obj)
 {
index 0c90e8d..8cadb6a 100644 (file)
@@ -49,7 +49,8 @@ GType gst_tizen_allocator_get_type(void);
 #define GST_TIZEN_ALLOCATOR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_TIZEN_ALLOCATOR, GstTizenAllocatorClass))
 #define GST_TIZEN_ALLOCATOR_CAST(obj)            ((GstTizenAllocator *)(obj))
 
-#define GST_TIZEN_MEMORY_TYPE "TizenVideoMemory"
+#define GST_TIZEN_MEMORY_TYPE       "TizenVideoMemory"
+#define GST_TIZEN_MEMORY_MAX_FD     4
 
 struct _GstTizenMemory
 {
@@ -71,6 +72,10 @@ struct _GstTizenMemory
   /* for encoded format */
   tbm_bo                    bo;
   tbm_bo_handle             bo_handle;
+
+  /* for fd */
+  int fd[GST_TIZEN_MEMORY_MAX_FD];
+  int fd_count;
 };
 
 struct _GstTizenAllocatorPrivate
@@ -88,6 +93,7 @@ struct _GstTizenAllocator
 {
   GstAllocator parent;
   GstTizenAllocatorPrivate *priv;
+  tbm_bufmgr bufmgr;
 };
 
 
@@ -97,10 +103,10 @@ struct _GstTizenAllocatorClass
 };
 
 GST_ALLOCATORS_API
-GstAllocator *  gst_tizen_allocator_new    (void);
+GstAllocator *  gst_tizen_allocator_new (void);
 
 GST_ALLOCATORS_API
-GstMemory *     gst_tizen_allocator_alloc  (GstAllocator * allocator, GstVideoInfo * vinfo);
+GstMemory *     gst_tizen_allocator_alloc (GstAllocator * allocator, GstVideoInfo * vinfo);
 
 GST_ALLOCATORS_API
 GstMemory *     gst_tizen_allocator_alloc_surface (GstAllocator * allocator, GstVideoInfo * vinfo,
@@ -111,19 +117,30 @@ GstMemory *     gst_tizen_allocator_alloc_bo (GstAllocator * allocator, GstVideo
                                                    tbm_bo bo, gsize size, gpointer user_data, GDestroyNotify notify);
 
 GST_ALLOCATORS_API
-gboolean        gst_is_tizen_memory        (GstMemory *mem);
+GstMemory *     gst_tizen_allocator_alloc_fd (GstAllocator * allocator, GstVideoInfo * vinfo,
+                                                   tbm_surface_info_s * tsinfo, int fd[GST_TIZEN_MEMORY_MAX_FD], int fd_count,
+                                                   gpointer user_data, GDestroyNotify notify);
+
+GST_ALLOCATORS_API
+gboolean        gst_is_tizen_memory (GstMemory * mem);
+
+GST_ALLOCATORS_API
+gint            gst_tizen_memory_get_num_bos (GstMemory * mem);
+
+GST_ALLOCATORS_API
+void *          gst_tizen_memory_get_bos (GstMemory * mem, gint index);
 
 GST_ALLOCATORS_API
-gint            gst_tizen_memory_get_num_bos (GstMemory *mem);
+void *          gst_tizen_memory_get_surface (GstMemory * mem);
 
 GST_ALLOCATORS_API
-void *          gst_tizen_memory_get_bos   (GstMemory *mem, gint index);
+gint            gst_tizen_memory_get_num_fd (GstMemory * mem);
 
 GST_ALLOCATORS_API
-void *          gst_tizen_memory_get_surface (GstMemory *mem);
+gint            gst_tizen_memory_get_fd (GstMemory * mem, gint index);
 
 GST_ALLOCATORS_API
-GstMemory *     gst_tizen_allocator_dmabuf_export (GstAllocator * allocator, GstMemory *tmem, int bo_idx);
+GstMemory *     gst_tizen_allocator_dmabuf_export (GstAllocator * allocator, GstMemory * tmem, int bo_idx);
 
 GST_ALLOCATORS_API
 GstTizenMemory *gst_tizen_allocator_dmabuf_import (GstAllocator * allocator, gint * fds, gint planes, gsize offsets[4], GstVideoInfo * vinfo);