amlvideosink: CB0 add dv flow [1/1]

PD#SWPL-195249

Problem:
add dv flow

Solution:
add dv flow

Verify:
t5w

Change-Id: Ie3405640478558cfbee708e231eb6422f8a1395b
Signed-off-by: hanghang.luo <hanghang.luo@amlogic.com>
diff --git a/src/ge2drotation.c b/src/ge2drotation.c
index 43195f6..c6e3964 100644
--- a/src/ge2drotation.c
+++ b/src/ge2drotation.c
@@ -34,8 +34,17 @@
 #include <gst/allocators/gstdmabuf.h>
 
 #define DUMP_ROTATION_DATA 0
-
+#define UVM_IOC_MAGIC 'U'
+#define UVM_IOC_SYNC_INFO _IOWR(UVM_IOC_MAGIC, 14, struct uvm_sync_info)
+#define DEFAULT_UVM_NAME "/dev/uvm"
+#define DEFAULT_DRM_NAME "/dev/dri/renderD128"
+static int uvmFd = -1;
 static int mdrmFd = -1;
+struct uvm_sync_info {
+    int src_fd;
+    int dst_fd;
+};
+
 
 static void destroy_gem_buffer(int drmFd, GemBuffer *gemBuf)
 {
@@ -75,6 +84,13 @@
    {
      destroy_gem_buffer(mdrmFd, &ROTbuf[i].gemBuf);
    }
+
+   if (ROTbuf->src_buf)
+   {
+     gst_buffer_unref(ROTbuf->src_buf);
+     ROTbuf->src_buf = NULL;
+   }
+
    free(ROTbuf);
    ROTbuf = NULL;
 
@@ -84,9 +100,14 @@
      mdrmFd= -1;
    }
 
+   if (uvmFd >= 0)
+   {
+     close(uvmFd);
+     uvmFd= -1;
+   }
+
 }
 
-#define DEFAULT_DRM_NAME "/dev/dri/renderD128"
 static bool creat_gem_buffer(int drmFd, GemBuffer *gemBuf)
 {
    bool result= false;
@@ -166,12 +187,20 @@
      goto exit;
    }
 
+   uvmFd = open(DEFAULT_UVM_NAME, O_RDONLY | O_CLOEXEC);
+   if (uvmFd < 0)
+   {
+     GST_ERROR("Failed to open uvm node: %d", errno);
+     goto exit;
+   }
+
    for (int i= 0; i < numBuffer; ++i)
    {
      ROTbuf[i].used = FALSE;
      ROTbuf[i].gemBuf.planeCount= numPlanes;
      ROTbuf[i].gemBuf.width= width;
      ROTbuf[i].gemBuf.height= height;
+     ROTbuf[i].src_buf = NULL;
      for (int j= 0; j < MAX_PLANES; ++j)
      {
        ROTbuf[i].gemBuf.fd[j]= -1;
@@ -293,6 +322,17 @@
   return TRUE;
 }
 
+static int uvm_bind_vframe(int fd, int src_fd, int dst_fd)
+{
+  int rc;
+  struct uvm_sync_info sync_info;
+  sync_info.src_fd = src_fd;
+  sync_info.dst_fd = dst_fd;
+  rc = ioctl (fd, UVM_IOC_SYNC_INFO, &sync_info);
+  return rc;
+}
+
+
 GstFlowReturn rotation_transform(aml_ge2d_info_t *pge2dinfo, GstBuffer *inbuf, ROTBuffer *ROTbuf, ROTATION_DEGREE ROTDegree)
 {
     GstFlowReturn ret = GST_FLOW_ERROR;
@@ -314,6 +354,13 @@
         g_return_val_if_fail(gst_is_drm_memory(mem), ret);
         pge2dinfo->src_info[0].shared_fd[i] = gst_fd_memory_get_fd(mem);
         pge2dinfo->dst_info.shared_fd[i] = ROTbuf->gemBuf.fd[i];
+
+        // bind vframe
+        int rc = uvm_bind_vframe(uvmFd, pge2dinfo->src_info[0].shared_fd[i], pge2dinfo->dst_info.shared_fd[i]);
+        if (rc) {
+          GST_ERROR ("bind vframe, UVM_IOC_SYNC_INFO error %d", rc);
+        }
+        ROTbuf->src_buf = inbuf;
     }
 
     meta_data = gst_buffer_get_video_meta(inbuf);
diff --git a/src/ge2drotation.h b/src/ge2drotation.h
index 3a18b09..4a61fe8 100644
--- a/src/ge2drotation.h
+++ b/src/ge2drotation.h
@@ -38,6 +38,7 @@
    gint index;
    gboolean used;
    GemBuffer gemBuf;
+   GstBuffer *src_buf;
 }ROTBuffer;
 
 typedef enum
diff --git a/src/gstamlvideosink.c b/src/gstamlvideosink.c
index fcc890d..8246fec 100644
--- a/src/gstamlvideosink.c
+++ b/src/gstamlvideosink.c
@@ -2243,6 +2243,12 @@
           {
             ROTBuffer* rotation_buffer = (ROTBuffer*)tunnel_lib_buf_wrap->priv;
             rotation_buffer->used = FALSE;
+            if (rotation_buffer->src_buf)
+            {
+                gst_buffer_unref(rotation_buffer->src_buf);
+                rotation_buffer->src_buf = NULL;
+            }
+
             /*printf("release [%d][%d][%d][%d][%d]\n", sink_priv->rot_buffer[0].used, sink_priv->rot_buffer[1].used,
             sink_priv->rot_buffer[2].used, sink_priv->rot_buffer[3].used, sink_priv->rot_buffer[4].used);*/
           }
@@ -2454,8 +2460,6 @@
       tunnel_lib_buf_wrap->flag = BUFFER_FLAG_DMA_BUFFER;
       tunnel_lib_buf_wrap->priv = (void *)&sink_priv->rot_buffer[index];
       tunnel_lib_buf_wrap->pts = GST_BUFFER_PTS(gst_buf);
-      // unref gstbuf
-      gst_buffer_unref(gst_buf);
     }
     else
     {