[Beignet] [PATCH v2 1/3] Add extension clCloseMemObjectFdIntel().
Chuanbo Weng
chuanbo.weng at intel.com
Fri Mar 27 09:34:01 PDT 2015
We have added extension clGetMemObjectFdIntel to export fd of memory
object,so we have to added corresponding extension to close the 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 | 32 ++++++++++++++++++++++++++++++--
src/cl_mem.h | 4 ++++
src/intel/intel_driver.c | 1 +
7 files changed, 64 insertions(+), 3 deletions(-)
diff --git a/include/CL/cl_intel.h b/include/CL/cl_intel.h
index 28bcb62..2ab79ad 100644
--- a/include/CL/cl_intel.h
+++ b/include/CL/cl_intel.h
@@ -122,7 +122,7 @@ 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 */
+/*Export memory object's fd*/
extern CL_API_ENTRY cl_int CL_API_CALL
clGetMemObjectFdIntel(cl_context /* context */,
cl_mem /* Memory Obejct */,
@@ -133,6 +133,15 @@ typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetMemObjectFdIntel_fn)(
cl_mem /* Memory Obejct */,
int* /* returned fd */);
+/*Close memory object's fd*/
+extern CL_API_ENTRY cl_int CL_API_CALL
+clCloseMemObjectFdIntel(cl_context /* context */,
+ cl_mem /* Memory Obejct */);
+
+typedef CL_API_ENTRY cl_int (CL_API_CALL *clCloseMemObjectFdIntel_fn)(
+ cl_context /* context */,
+ cl_mem /* Memory Obejct */);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/cl_api.c b/src/cl_api.c
index 3e72deb..f9efac7 100644
--- a/src/cl_api.c
+++ b/src/cl_api.c
@@ -3180,6 +3180,7 @@ internal_clGetExtensionFunctionAddress(const char *func_name)
EXTFUNC(clCreateBufferFromLibvaIntel)
EXTFUNC(clCreateImageFromLibvaIntel)
EXTFUNC(clGetMemObjectFdIntel)
+ EXTFUNC(clCloseMemObjectFdIntel)
return NULL;
}
@@ -3348,3 +3349,17 @@ clGetMemObjectFdIntel(cl_context context,
error:
return err;
}
+
+extern CL_API_ENTRY cl_int CL_API_CALL
+clCloseMemObjectFdIntel(cl_context context,
+ cl_mem memobj)
+{
+ cl_int err = CL_SUCCESS;
+ CHECK_CONTEXT (context);
+ CHECK_MEM (memobj);
+
+ err = cl_mem_close_fd(memobj);
+
+error:
+ return err;
+}
diff --git a/src/cl_driver.h b/src/cl_driver.h
index 3f54a27..dd028c0 100644
--- a/src/cl_driver.h
+++ b/src/cl_driver.h
@@ -369,6 +369,9 @@ 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;
+typedef int (cl_buffer_close_fd_cb)(int fd);
+extern cl_buffer_close_fd_cb *cl_buffer_close_fd;
+
typedef int (cl_buffer_get_tiling_align_cb)(cl_context ctx, uint32_t tiling_mode, uint32_t dim);
extern cl_buffer_get_tiling_align_cb *cl_buffer_get_tiling_align;
diff --git a/src/cl_driver_defs.c b/src/cl_driver_defs.c
index 9a47210..8018747 100644
--- a/src/cl_driver_defs.c
+++ b/src/cl_driver_defs.c
@@ -51,6 +51,7 @@ 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;
+LOCAL cl_buffer_close_fd_cb *cl_buffer_close_fd = NULL;
LOCAL cl_buffer_get_tiling_align_cb *cl_buffer_get_tiling_align = NULL;
/* cl_khr_gl_sharing */
diff --git a/src/cl_mem.c b/src/cl_mem.c
index b41ec14..08fb239 100644
--- a/src/cl_mem.c
+++ b/src/cl_mem.c
@@ -267,6 +267,8 @@ cl_mem_allocate(enum cl_mem_type type,
mem->flags = flags;
mem->is_userptr = 0;
mem->offset = 0;
+ mem->export_ref = 0;
+ pthread_mutex_init(&mem->export_lock, NULL);
if (sz != 0) {
/* Pinning will require stricter alignment rules */
@@ -2051,7 +2053,33 @@ 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;
+
+ pthread_mutex_lock(&mem->export_lock);
+ if(mem->export_ref == 0){
+ if(cl_buffer_get_fd(mem->bo, fd))
+ err = CL_INVALID_OPERATION;
+ mem->export_fd = *fd;
+ }
+ else{
+ *fd = mem->export_fd;
+ }
+ mem->export_ref++;
+ pthread_mutex_unlock(&mem->export_lock);
+
+ return err;
+}
+
+LOCAL cl_int
+cl_mem_close_fd(cl_mem mem)
+{
+ cl_int err = CL_SUCCESS;
+
+ if(mem->export_ref == 0)
+ return CL_INVALID_MEM_OBJECT;
+ if (atomic_dec(&mem->export_ref) > 1)
+ return CL_SUCCESS;
+ if(cl_buffer_close_fd(mem->export_fd))
+ err = CL_INVALID_OPERATION;
+
return err;
}
diff --git a/src/cl_mem.h b/src/cl_mem.h
index e027f15..aea2ade 100644
--- a/src/cl_mem.h
+++ b/src/cl_mem.h
@@ -95,6 +95,9 @@ typedef struct _cl_mem {
cl_mem_dstr_cb *dstr_cb; /* The destroy callback. */
uint8_t is_userptr; /* CL_MEM_USE_HOST_PTR is enabled*/
size_t offset; /* offset of host_ptr to the page beginning, only for CL_MEM_USE_HOST_PTR*/
+ pthread_mutex_t export_lock; /* To export fd */
+ volatile int export_ref; /* The exported count */
+ int export_fd; /* The exported fd of this memory object */
} _cl_mem;
struct _cl_mem_image {
@@ -294,6 +297,7 @@ extern cl_mem cl_mem_new_libva_image(cl_context ctx,
size_t row_pitch,
cl_int *errcode);
extern cl_int cl_mem_get_fd(cl_mem mem, int* fd);
+extern cl_int cl_mem_close_fd(cl_mem mem);
#endif /* __CL_MEM_H__ */
diff --git a/src/intel/intel_driver.c b/src/intel/intel_driver.c
index 755ab6b..37fb262 100644
--- a/src/intel/intel_driver.c
+++ b/src/intel/intel_driver.c
@@ -840,6 +840,7 @@ intel_setup_callbacks(void)
cl_buffer_get_subdata = (cl_buffer_get_subdata_cb *) drm_intel_bo_get_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;
+ cl_buffer_close_fd = (cl_buffer_close_fd_cb *) close;
cl_buffer_get_tiling_align = (cl_buffer_get_tiling_align_cb *)intel_buffer_get_tiling_align;
intel_set_gpgpu_callbacks(intel_get_device_id());
}
--
1.9.1
More information about the Beignet
mailing list