[Beignet] [PATCH V2] Add clGetMemObjectFdIntel() api

Chuanbo Weng chuanbo.weng at intel.com
Wed Mar 5 08:08:15 PST 2014


Use this api to share buffer between OpenCL and v4l2. After import
the fd of OpenCL memory object to v4l2, v4l2 can directly read frame
into this memory object by the way of DMABUF, without memory-copy.

v2:
Check return value of cl_buffer_get_fd

Signed-off-by: Chuanbo Weng <chuanbo.weng at intel.com>
---
 include/CL/cl_intel.h    |   11 +++++++++++
 src/cl_api.c             |   15 +++++++++++++++
 src/cl_driver.h          |    3 +++
 src/cl_driver_defs.c     |    1 +
 src/cl_mem.c             |   10 ++++++++++
 src/intel/intel_driver.c |    1 +
 6 files changed, 41 insertions(+)

diff --git a/include/CL/cl_intel.h b/include/CL/cl_intel.h
index 3fd73da..f2fe9d4 100644
--- a/include/CL/cl_intel.h
+++ b/include/CL/cl_intel.h
@@ -122,6 +122,17 @@ typedef CL_API_ENTRY cl_mem (CL_API_CALL *clCreateImageFromLibvaIntel_fn)(
                              const cl_libva_image * /* info */,
                              cl_int *               /* errcode_ret */);
 
+/* Create buffer from libva's buffer object */
+extern CL_API_ENTRY cl_int CL_API_CALL
+clGetMemObjectFdIntel(cl_context   /* context */,
+                      cl_mem       /* Memory Obejct */,
+                      int*         /* returned fd */);
+
+typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetMemObjectFdIntel_fn)(
+                             cl_context   /* context */,
+                             cl_mem       /* Memory Obejct */,
+                             int*         /* returned fd */);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/cl_api.c b/src/cl_api.c
index 2a6f8ce..9638994 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -2675,6 +2675,7 @@ clGetExtensionFunctionAddress(const char *func_name)
   EXTFUNC(clReportUnfreedIntel)
   EXTFUNC(clCreateBufferFromLibvaIntel)
   EXTFUNC(clCreateImageFromLibvaIntel)
+  EXTFUNC(clGetMemObjectFdIntel)
   return NULL;
 }
 
@@ -2814,3 +2815,17 @@ error:
   return mem;
 }
 
+extern CL_API_ENTRY cl_int CL_API_CALL
+clGetMemObjectFdIntel(cl_context context,
+                      cl_mem memobj,
+                      int* fd)
+{
+  cl_int err = CL_SUCCESS;
+  CHECK_CONTEXT (context);
+  CHECK_MEM (memobj);
+
+  err = cl_mem_get_fd(memobj, fd);
+
+error:
+  return err;
+}
diff --git a/src/cl_driver.h b/src/cl_driver.h
index 96fc377..9dc2330 100644
--- a/src/cl_driver.h
+++ b/src/cl_driver.h
@@ -297,6 +297,9 @@ extern cl_buffer_subdata_cb *cl_buffer_subdata;
 typedef int (cl_buffer_wait_rendering_cb) (cl_buffer);
 extern cl_buffer_wait_rendering_cb *cl_buffer_wait_rendering;
 
+typedef int (cl_buffer_get_fd_cb)(cl_buffer, int *fd);
+extern cl_buffer_get_fd_cb *cl_buffer_get_fd;
+
 /* Get the device id */
 typedef int (cl_driver_get_device_id_cb)(void);
 extern cl_driver_get_device_id_cb *cl_driver_get_device_id;
diff --git a/src/cl_driver_defs.c b/src/cl_driver_defs.c
index 0a9012c..95a1a03 100644
--- a/src/cl_driver_defs.c
+++ b/src/cl_driver_defs.c
@@ -47,6 +47,7 @@ LOCAL cl_buffer_subdata_cb *cl_buffer_subdata = NULL;
 LOCAL cl_buffer_wait_rendering_cb *cl_buffer_wait_rendering = NULL;
 LOCAL cl_buffer_get_buffer_from_libva_cb *cl_buffer_get_buffer_from_libva = NULL;
 LOCAL cl_buffer_get_image_from_libva_cb *cl_buffer_get_image_from_libva = NULL;
+LOCAL cl_buffer_get_fd_cb *cl_buffer_get_fd = NULL;
 
 /* cl_khr_gl_sharing */
 LOCAL cl_gl_acquire_texture_cb *cl_gl_acquire_texture = NULL;
diff --git a/src/cl_mem.c b/src/cl_mem.c
index 40e0a99..9e0d334 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -1337,3 +1337,13 @@ error:
   mem = NULL;
   goto exit;
 }
+
+LOCAL cl_int
+cl_mem_get_fd(cl_mem mem,
+              int* fd)
+{
+  cl_int err = CL_SUCCESS;
+  if(cl_buffer_get_fd(mem->bo, fd))
+	err = CL_INVALID_OPERATION;
+  return err;
+}
diff --git a/src/intel/intel_driver.c b/src/intel/intel_driver.c
index f88a105..5e474de 100644
--- a/src/intel/intel_driver.c
+++ b/src/intel/intel_driver.c
@@ -682,5 +682,6 @@ intel_setup_callbacks(void)
   cl_buffer_unpin = (cl_buffer_unpin_cb *) drm_intel_bo_unpin;
   cl_buffer_subdata = (cl_buffer_subdata_cb *) drm_intel_bo_subdata;
   cl_buffer_wait_rendering = (cl_buffer_wait_rendering_cb *) drm_intel_bo_wait_rendering;
+  cl_buffer_get_fd = (cl_buffer_get_fd_cb *) drm_intel_bo_gem_export_to_prime;
   intel_set_gpgpu_callbacks();
 }
-- 
1.7.9.5



More information about the Beignet mailing list