diff --git a/BUILD.gn b/BUILD.gn index 83f27cf..45d9c19 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -11,10 +11,33 @@ # See the License for the specific language governing permissions and # limitations under the License. -config("libOpenCL_public_config") { - include_dirs = [ "./" ] +import("//build/ohos.gni") + +config("cl_config") { + cflags = [ + "-std=c++17", + "-Wno-error=implicit-fallthrough", + "-Wno-deprecated-declarations", + ] } -group("libOpenCL") { - public_configs = [ ":libOpenCL_public_config" ] +config("cl_public_config") { + include_dirs = [ + "./", + "include", + #"//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + ] } + +ohos_shared_library("libcl") { + visibility = [ "*" ] + sources = [ + "src/opencl_wrapper.cpp", + ] + configs = [ ":cl_config" ] + public_configs = [ ":cl_public_config" ] + output_name = "OpenCL" + output_extension = "so" + part_name = "opencl" + subsystem_name = "thirdparty" +} \ No newline at end of file diff --git a/NOTICE b/NOTICE deleted file mode 100644 index 0e5e47a..0000000 --- a/NOTICE +++ /dev/null @@ -1,22 +0,0 @@ -Software: opencl-headers v2020.12.18 - -Copyright (c) 2008-2020 The Khronos Group Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and /or associated documentation files (the "Materials "), to -deal in the Materials without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Materials, and to permit persons to whom the Materials are -furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Materials. - -THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS IN THE -MATERIALS. diff --git a/OAT.xml b/OAT.xml index c29f31c..d6bf6a5 100644 --- a/OAT.xml +++ b/OAT.xml @@ -71,7 +71,7 @@ Note:If the text contains special characters, please escape them according to th - + diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..683c000 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,95 @@ +# OpenCLTM API Headers + +仓库包含C语言的OpenCL API。OpenCL扩展了GPU用于图形渲染之外的能力,将通用计算并行化。OpenCL可用于图像处理、AI、高性能计算等场景的加速。推荐在计算密集型的任务以及可以并行计算的场景使用OpenCL。 + +例如在图像处理场景,CPU进行编解码、滤镜等特效处理较慢,使用OpenCL有十倍的加速效果。 + +例如在AI推理场景,使用OpenCL可以将推理性能提高一倍。 + +OpenHarmony引入后,新增了一层封装层,动态链接查找OpenCL库,以此进行解耦。目前仅支持native层调用OpenCL。 + +## 目录结构 + +``` +README.md 英文说明 +README_zh.md 中文说明 +LICENSE 证书文件 +CL/ 原CL头文件 +include/ 封装层CL头文件 +src/ 封装层CL实现 +``` + +## OpenHarmony如何集成OpenCL +### 1.头文件引入 +```c +#define USE_OPENCL_WRAPPER +#include "opencl_wrapper.h" +``` +### 2.BUILD.gn添加引用 +```c +deps += ["//third_party/openCL:libcl"] +``` +### 3.调用OpenCL函数过程举例 +```c +// 准备OpenCL的kernel程序 +const char* program_source = + "__kernel void test_main(read_only image2d_t inputImage) {\n" + " ...\n" + "}"; +``` + +```c +// 通过wrapper层的函数判断是否cl初始化成功 +// 如果返回false,说明没有实际的驱动 +bool cl_ok = OHOS::InitOpenCL(); + +// 获取设备信息 +cl_int err; +cl_platform_id platform_id; +cl_device_id device_id; +clGetPlatformIDs(1, &platform_id, NULL); +clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL); +// 创建OpenCL上下文 +context_ = clCreateContext(0, 1, &device_id, NULL, NULL, &err); +queue_ = clCreateCommandQueueWithProperties(context_, device_id, 0, &err); +// 使用上述源码创建OpenCL程序 +cl_program p = clCreateProgramWithSource(context, 1, &program_source, nullptr, nullptr); +// 创建kernel程序,对应上述源码中的函数名 +kernel_ = clCreateKernel(program, "test_main", &err); +// 创建OpenCL可以识别的图片 +cl_image_format image_format; +image_format.image_channel_order = CL_RGBA; +image_format.image_channel_data_type = CL_UNORM_INT8; +cl_image_desc desc = {CL_MEM_OBJECT_IMAGE2D, width, height}; +cl_mem input_image = clCreateImage(context_, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, image_format, &desc, const_cast +#include +#define CL_TARGET_OPENCL_VERSION 300 +#include + +#ifdef USE_OPENCL_WRAPPER +#define MS_ASSERT(f) ((void)0) + +namespace OHOS { +// This is a opencl function wrapper. +bool LoadOpenCLLibrary(void **handle_ptr); +bool UnLoadOpenCLLibrary(void *handle); +bool InitOpenCL(); + +// get platform id +using clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *); +// get platform info +using clGetPlatformInfoFunc = cl_int (*)(cl_platform_id, cl_platform_info, size_t, void *, size_t *); +// build program +using clBuildProgramFunc = cl_int (*)(cl_program, cl_uint, const cl_device_id *, const char *, + void (*pfn_notify)(cl_program, void *), void *); +// enqueue run kernel +using clEnqueueNDRangeKernelFunc = cl_int (*)(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *, + const size_t *, cl_uint, const cl_event *, cl_event *); +// set kernel parameter +using clSetKernelArgFunc = cl_int (*)(cl_kernel, cl_uint, size_t, const void *); +using clRetainMemObjectFunc = cl_int (*)(cl_mem); +using clReleaseMemObjectFunc = cl_int (*)(cl_mem); +using clEnqueueUnmapMemObjectFunc = cl_int (*)(cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *); +using clRetainCommandQueueFunc = cl_int (*)(cl_command_queue command_queue); +// create context +using clCreateContextFunc = cl_context (*)(const cl_context_properties *, cl_uint, const cl_device_id *, + void(CL_CALLBACK *)( // NOLINT(readability/casting) + const char *, const void *, size_t, void *), + void *, cl_int *); +using clEnqueueCopyImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *, + const size_t *, cl_uint, const cl_event *, cl_event *); + +using clCreateContextFromTypeFunc = cl_context (*)(const cl_context_properties *, cl_device_type, + void(CL_CALLBACK *)( // NOLINT(readability/casting) + const char *, const void *, size_t, void *), + void *, cl_int *); +using clReleaseContextFunc = cl_int (*)(cl_context); +using clWaitForEventsFunc = cl_int (*)(cl_uint, const cl_event *); +using clReleaseEventFunc = cl_int (*)(cl_event); +using clEnqueueWriteBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint, + const cl_event *, cl_event *); +using clEnqueueWriteImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t, + size_t, const void *, cl_uint, const cl_event *, cl_event *); +using clEnqueueReadImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t, + size_t, void *, cl_uint, const cl_event *, cl_event *); +using clEnqueueReadBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint, + const cl_event *, cl_event *); +using clGetProgramBuildInfoFunc = cl_int (*)(cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *); +using clRetainProgramFunc = cl_int (*)(cl_program program); +using clEnqueueMapBufferFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, size_t, size_t, cl_uint, + const cl_event *, cl_event *, cl_int *); +using clEnqueueMapImageFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, const size_t *, const size_t *, + size_t *, size_t *, cl_uint, const cl_event *, cl_event *, cl_int *); +using clCreateCommandQueueFunc = cl_command_queue (*)(cl_context, cl_device_id, cl_command_queue_properties, cl_int *); +using clGetCommandQueueInfoFunc = cl_int (*)(cl_command_queue, cl_command_queue_info, size_t, void *, size_t *); +using clReleaseCommandQueueFunc = cl_int (*)(cl_command_queue); +using clCreateProgramWithBinaryFunc = cl_program (*)(cl_context, cl_uint, const cl_device_id *, const size_t *, + const unsigned char **, cl_int *, cl_int *); +using clRetainContextFunc = cl_int (*)(cl_context context); +using clGetContextInfoFunc = cl_int (*)(cl_context, cl_context_info, size_t, void *, size_t *); +using clReleaseProgramFunc = cl_int (*)(cl_program program); +using clFlushFunc = cl_int (*)(cl_command_queue command_queue); +using clFinishFunc = cl_int (*)(cl_command_queue command_queue); +using clGetProgramInfoFunc = cl_int (*)(cl_program, cl_program_info, size_t, void *, size_t *); +using clCreateKernelFunc = cl_kernel (*)(cl_program, const char *, cl_int *); +using clRetainKernelFunc = cl_int (*)(cl_kernel kernel); +using clCreateBufferFunc = cl_mem (*)(cl_context, cl_mem_flags, size_t, void *, cl_int *); +using clCreateImage2DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t, + void *, cl_int *); +using clImportMemoryARMFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, void *, ssize_t, cl_int *); +using clCreateImage3DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t, + size_t, size_t, void *, cl_int *); +using clCreateProgramWithSourceFunc = cl_program (*)(cl_context, cl_uint, const char **, const size_t *, cl_int *); +using clReleaseKernelFunc = cl_int (*)(cl_kernel kernel); +using clGetDeviceInfoFunc = cl_int (*)(cl_device_id, cl_device_info, size_t, void *, size_t *); +using clGetDeviceIDsFunc = cl_int (*)(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *); +using clRetainEventFunc = cl_int (*)(cl_event); +using clGetKernelWorkGroupInfoFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_work_group_info, size_t, void *, + size_t *); +using clGetEventInfoFunc = cl_int (*)(cl_event event, cl_event_info param_name, size_t param_value_size, + void *param_value, size_t *param_value_size_ret); +using clGetEventProfilingInfoFunc = cl_int (*)(cl_event event, cl_profiling_info param_name, size_t param_value_size, + void *param_value, size_t *param_value_size_ret); +using clGetImageInfoFunc = cl_int (*)(cl_mem, cl_image_info, size_t, void *, size_t *); +using clEnqueueCopyBufferToImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, size_t, const size_t *, + const size_t *, cl_uint, const cl_event *, cl_event *); +using clEnqueueCopyImageToBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *, + size_t, cl_uint, const cl_event *, cl_event *); +#if CL_TARGET_OPENCL_VERSION >= 120 +using clRetainDeviceFunc = cl_int (*)(cl_device_id); +using clReleaseDeviceFunc = cl_int (*)(cl_device_id); +using clCreateImageFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, const cl_image_desc *, void *, + cl_int *); +using clEnqueueFillImageFunc = cl_int (*)(cl_command_queue, cl_mem, const void *, const size_t *, const size_t *, + cl_uint, const cl_event *, cl_event *); +#endif +#if CL_TARGET_OPENCL_VERSION >= 200 +using clCreateProgramWithILFunc = cl_program (*)(cl_context, const void *, size_t, cl_int *); +using clSVMAllocFunc = void *(*)(cl_context, cl_mem_flags, size_t size, cl_uint); +using clSVMFreeFunc = void (*)(cl_context, void *); +using clEnqueueSVMMapFunc = cl_int (*)(cl_command_queue, cl_bool, cl_map_flags, void *, size_t, cl_uint, + const cl_event *, cl_event *); +using clEnqueueSVMUnmapFunc = cl_int (*)(cl_command_queue, void *, cl_uint, const cl_event *, cl_event *); +using clSetKernelArgSVMPointerFunc = cl_int (*)(cl_kernel, cl_uint, const void *); +// opencl 2.0 can get sub group info and wave size. +using clGetKernelSubGroupInfoKHRFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_sub_group_info, size_t, + const void *, size_t, void *, size_t *); +using clCreateCommandQueueWithPropertiesFunc = cl_command_queue (*)(cl_context, cl_device_id, + const cl_queue_properties *, cl_int *); +using clGetExtensionFunctionAddressFunc = void *(*)(const char *); +#endif + +#define CL_DECLARE_FUNC_PTR(func) extern func##Func func + +CL_DECLARE_FUNC_PTR(clGetPlatformIDs); +CL_DECLARE_FUNC_PTR(clGetPlatformInfo); +CL_DECLARE_FUNC_PTR(clBuildProgram); +CL_DECLARE_FUNC_PTR(clEnqueueNDRangeKernel); +CL_DECLARE_FUNC_PTR(clSetKernelArg); +CL_DECLARE_FUNC_PTR(clReleaseKernel); +CL_DECLARE_FUNC_PTR(clCreateProgramWithSource); +CL_DECLARE_FUNC_PTR(clCreateBuffer); +CL_DECLARE_FUNC_PTR(clCreateImage2D); +CL_DECLARE_FUNC_PTR(clImportMemoryARM); +CL_DECLARE_FUNC_PTR(clCreateImage3D); +CL_DECLARE_FUNC_PTR(clRetainKernel); +CL_DECLARE_FUNC_PTR(clCreateKernel); +CL_DECLARE_FUNC_PTR(clGetProgramInfo); +CL_DECLARE_FUNC_PTR(clFlush); +CL_DECLARE_FUNC_PTR(clFinish); +CL_DECLARE_FUNC_PTR(clReleaseProgram); +CL_DECLARE_FUNC_PTR(clRetainContext); +CL_DECLARE_FUNC_PTR(clGetContextInfo); +CL_DECLARE_FUNC_PTR(clCreateProgramWithBinary); +CL_DECLARE_FUNC_PTR(clCreateCommandQueue); +CL_DECLARE_FUNC_PTR(clGetCommandQueueInfo); +CL_DECLARE_FUNC_PTR(clReleaseCommandQueue); +CL_DECLARE_FUNC_PTR(clEnqueueMapBuffer); +CL_DECLARE_FUNC_PTR(clEnqueueMapImage); +CL_DECLARE_FUNC_PTR(clEnqueueCopyImage); +CL_DECLARE_FUNC_PTR(clRetainProgram); +CL_DECLARE_FUNC_PTR(clGetProgramBuildInfo); +CL_DECLARE_FUNC_PTR(clEnqueueReadBuffer); +CL_DECLARE_FUNC_PTR(clEnqueueWriteBuffer); +CL_DECLARE_FUNC_PTR(clEnqueueWriteImage); +CL_DECLARE_FUNC_PTR(clEnqueueReadImage); +CL_DECLARE_FUNC_PTR(clWaitForEvents); +CL_DECLARE_FUNC_PTR(clReleaseEvent); +CL_DECLARE_FUNC_PTR(clCreateContext); +CL_DECLARE_FUNC_PTR(clCreateContextFromType); +CL_DECLARE_FUNC_PTR(clReleaseContext); +CL_DECLARE_FUNC_PTR(clRetainCommandQueue); +CL_DECLARE_FUNC_PTR(clEnqueueUnmapMemObject); +CL_DECLARE_FUNC_PTR(clRetainMemObject); +CL_DECLARE_FUNC_PTR(clReleaseMemObject); +CL_DECLARE_FUNC_PTR(clGetDeviceInfo); +CL_DECLARE_FUNC_PTR(clGetDeviceIDs); +CL_DECLARE_FUNC_PTR(clRetainEvent); +CL_DECLARE_FUNC_PTR(clGetKernelWorkGroupInfo); +CL_DECLARE_FUNC_PTR(clGetEventInfo); +CL_DECLARE_FUNC_PTR(clGetEventProfilingInfo); +CL_DECLARE_FUNC_PTR(clGetImageInfo); +CL_DECLARE_FUNC_PTR(clEnqueueCopyBufferToImage); +CL_DECLARE_FUNC_PTR(clEnqueueCopyImageToBuffer); +#if CL_TARGET_OPENCL_VERSION >= 120 +CL_DECLARE_FUNC_PTR(clRetainDevice); +CL_DECLARE_FUNC_PTR(clReleaseDevice); +CL_DECLARE_FUNC_PTR(clCreateImage); +CL_DECLARE_FUNC_PTR(clEnqueueFillImage); +#endif +#if CL_TARGET_OPENCL_VERSION >= 200 +CL_DECLARE_FUNC_PTR(clGetKernelSubGroupInfoKHR); +CL_DECLARE_FUNC_PTR(clCreateCommandQueueWithProperties); +CL_DECLARE_FUNC_PTR(clGetExtensionFunctionAddress); +CL_DECLARE_FUNC_PTR(clCreateProgramWithIL); +CL_DECLARE_FUNC_PTR(clSVMAlloc); +CL_DECLARE_FUNC_PTR(clSVMFree); +CL_DECLARE_FUNC_PTR(clEnqueueSVMMap); +CL_DECLARE_FUNC_PTR(clEnqueueSVMUnmap); +CL_DECLARE_FUNC_PTR(clSetKernelArgSVMPointer); +#endif + +#undef CL_DECLARE_FUNC_PTR +} // namespace OHOS +#endif // USE_OPENCL_WRAPPER +#endif // OPENCL_WRAPPER_H_ diff --git a/src/opencl_wrapper.cpp b/src/opencl_wrapper.cpp new file mode 100644 index 0000000..36c34f7 --- /dev/null +++ b/src/opencl_wrapper.cpp @@ -0,0 +1,789 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define USE_OPENCL_WRAPPER +#ifdef USE_OPENCL_WRAPPER + +#include "opencl_wrapper.h" +#include +#include +#include +#include +#include + +namespace OHOS { +// default opencl library path +static const std::vector g_opencl_library_paths = { +#if defined(__APPLE__) || defined(__MACOSX) + "libOpenCL.so", "/System/Library/Frameworks/OpenCL.framework/OpenCL" +#else + "/system/lib64/libGLES_mali.so", + "libGLES_mali.so", +#endif +}; + + +static std::mutex gInitMutex; +bool isInit = false; +void *handle_{nullptr}; + +bool InitOpenCL() { + std::lock_guard lock(gInitMutex); + if (isInit){ + return true; + } + isInit = LoadOpenCLLibrary(&handle_); + return isInit; +} + +bool UnLoadOpenCLLibrary(void *handle) { + if (handle != nullptr) { + if (dlclose(handle) != 0) { + return false; + } + return true; + } + return true; +} + +bool LoadLibraryFromPath(const std::string &library_path, void **handle_ptr) { + if (handle_ptr == nullptr) { + return false; + } + + *handle_ptr = dlopen(library_path.c_str(), RTLD_NOW | RTLD_LOCAL); + if (*handle_ptr == nullptr) { + return false; + } + +// load function ptr use dlopen and dlsym. +#define LOAD_OPENCL_FUNCTION_PTR(func_name) \ + func_name = reinterpret_cast(dlsym(*handle_ptr, #func_name)); \ + if (func_name == nullptr) { \ + UnLoadOpenCLLibrary(*handle_ptr); \ + return false; \ + } + + LOAD_OPENCL_FUNCTION_PTR(clGetPlatformIDs); + LOAD_OPENCL_FUNCTION_PTR(clGetPlatformInfo); + LOAD_OPENCL_FUNCTION_PTR(clBuildProgram); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueNDRangeKernel); + LOAD_OPENCL_FUNCTION_PTR(clSetKernelArg); + LOAD_OPENCL_FUNCTION_PTR(clReleaseKernel); + LOAD_OPENCL_FUNCTION_PTR(clCreateProgramWithSource); + LOAD_OPENCL_FUNCTION_PTR(clCreateBuffer); + LOAD_OPENCL_FUNCTION_PTR(clCreateImage2D); + LOAD_OPENCL_FUNCTION_PTR(clCreateImage3D); + LOAD_OPENCL_FUNCTION_PTR(clRetainKernel); + LOAD_OPENCL_FUNCTION_PTR(clCreateKernel); + LOAD_OPENCL_FUNCTION_PTR(clGetProgramInfo); + LOAD_OPENCL_FUNCTION_PTR(clFlush); + LOAD_OPENCL_FUNCTION_PTR(clFinish); + LOAD_OPENCL_FUNCTION_PTR(clReleaseProgram); + LOAD_OPENCL_FUNCTION_PTR(clRetainContext); + LOAD_OPENCL_FUNCTION_PTR(clGetContextInfo); + LOAD_OPENCL_FUNCTION_PTR(clCreateProgramWithBinary); + LOAD_OPENCL_FUNCTION_PTR(clCreateCommandQueue); + LOAD_OPENCL_FUNCTION_PTR(clGetCommandQueueInfo); + LOAD_OPENCL_FUNCTION_PTR(clReleaseCommandQueue); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueMapBuffer); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueMapImage); + LOAD_OPENCL_FUNCTION_PTR(clRetainProgram); + LOAD_OPENCL_FUNCTION_PTR(clGetProgramBuildInfo); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueReadBuffer); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueWriteBuffer); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueReadImage); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueWriteImage); + LOAD_OPENCL_FUNCTION_PTR(clWaitForEvents); + LOAD_OPENCL_FUNCTION_PTR(clReleaseEvent); + LOAD_OPENCL_FUNCTION_PTR(clCreateContext); + LOAD_OPENCL_FUNCTION_PTR(clCreateContextFromType); + LOAD_OPENCL_FUNCTION_PTR(clReleaseContext); + LOAD_OPENCL_FUNCTION_PTR(clRetainCommandQueue); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueUnmapMemObject); + LOAD_OPENCL_FUNCTION_PTR(clRetainMemObject); + LOAD_OPENCL_FUNCTION_PTR(clReleaseMemObject); + LOAD_OPENCL_FUNCTION_PTR(clGetDeviceInfo); + LOAD_OPENCL_FUNCTION_PTR(clGetDeviceIDs); + LOAD_OPENCL_FUNCTION_PTR(clRetainEvent); + LOAD_OPENCL_FUNCTION_PTR(clGetKernelWorkGroupInfo); + LOAD_OPENCL_FUNCTION_PTR(clGetEventInfo); + LOAD_OPENCL_FUNCTION_PTR(clGetEventProfilingInfo); + LOAD_OPENCL_FUNCTION_PTR(clGetImageInfo); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueCopyImage); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueCopyBufferToImage); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueCopyImageToBuffer); +#if CL_TARGET_OPENCL_VERSION >= 120 + LOAD_OPENCL_FUNCTION_PTR(clRetainDevice); + LOAD_OPENCL_FUNCTION_PTR(clReleaseDevice); + LOAD_OPENCL_FUNCTION_PTR(clCreateImage); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueFillImage); +#endif +#if CL_TARGET_OPENCL_VERSION >= 200 + LOAD_OPENCL_FUNCTION_PTR(clCreateCommandQueueWithProperties); + LOAD_OPENCL_FUNCTION_PTR(clGetExtensionFunctionAddress); + LOAD_OPENCL_FUNCTION_PTR(clSVMAlloc); + LOAD_OPENCL_FUNCTION_PTR(clSVMFree); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueSVMMap); + LOAD_OPENCL_FUNCTION_PTR(clEnqueueSVMUnmap); + LOAD_OPENCL_FUNCTION_PTR(clSetKernelArgSVMPointer); +#endif + + return true; +} +// load default library path +bool LoadOpenCLLibrary(void **handle_ptr) { + if (handle_ptr == nullptr) { + return false; + } + if (*handle_ptr != nullptr) { + return true; + } + auto it = + std::find_if(g_opencl_library_paths.begin(), g_opencl_library_paths.end(), + [&](const std::string &lib_path) { return OHOS::LoadLibraryFromPath(lib_path, handle_ptr); }); + if (it != g_opencl_library_paths.end()) { + return true; + } + return false; +} + +#define CL_DEFINE_FUNC_PTR(func) func##Func func = nullptr + +CL_DEFINE_FUNC_PTR(clGetPlatformIDs); +CL_DEFINE_FUNC_PTR(clGetPlatformInfo); +CL_DEFINE_FUNC_PTR(clBuildProgram); +CL_DEFINE_FUNC_PTR(clEnqueueNDRangeKernel); +CL_DEFINE_FUNC_PTR(clSetKernelArg); +CL_DEFINE_FUNC_PTR(clReleaseKernel); +CL_DEFINE_FUNC_PTR(clCreateProgramWithSource); +CL_DEFINE_FUNC_PTR(clCreateBuffer); +CL_DEFINE_FUNC_PTR(clCreateImage2D); +CL_DEFINE_FUNC_PTR(clImportMemoryARM); +CL_DEFINE_FUNC_PTR(clCreateImage3D); +CL_DEFINE_FUNC_PTR(clRetainKernel); +CL_DEFINE_FUNC_PTR(clCreateKernel); +CL_DEFINE_FUNC_PTR(clGetProgramInfo); +CL_DEFINE_FUNC_PTR(clFlush); +CL_DEFINE_FUNC_PTR(clFinish); +CL_DEFINE_FUNC_PTR(clReleaseProgram); +CL_DEFINE_FUNC_PTR(clRetainContext); +CL_DEFINE_FUNC_PTR(clGetContextInfo); +CL_DEFINE_FUNC_PTR(clCreateProgramWithBinary); +CL_DEFINE_FUNC_PTR(clCreateCommandQueue); +CL_DEFINE_FUNC_PTR(clGetCommandQueueInfo); +CL_DEFINE_FUNC_PTR(clReleaseCommandQueue); +CL_DEFINE_FUNC_PTR(clEnqueueMapBuffer); +CL_DEFINE_FUNC_PTR(clEnqueueMapImage); +CL_DEFINE_FUNC_PTR(clEnqueueCopyImage); +CL_DEFINE_FUNC_PTR(clRetainProgram); +CL_DEFINE_FUNC_PTR(clGetProgramBuildInfo); +CL_DEFINE_FUNC_PTR(clEnqueueReadBuffer); +CL_DEFINE_FUNC_PTR(clEnqueueWriteBuffer); +CL_DEFINE_FUNC_PTR(clEnqueueWriteImage); +CL_DEFINE_FUNC_PTR(clEnqueueReadImage); +CL_DEFINE_FUNC_PTR(clWaitForEvents); +CL_DEFINE_FUNC_PTR(clReleaseEvent); +CL_DEFINE_FUNC_PTR(clCreateContext); +CL_DEFINE_FUNC_PTR(clCreateContextFromType); +CL_DEFINE_FUNC_PTR(clReleaseContext); +CL_DEFINE_FUNC_PTR(clRetainCommandQueue); +CL_DEFINE_FUNC_PTR(clEnqueueUnmapMemObject); +CL_DEFINE_FUNC_PTR(clRetainMemObject); +CL_DEFINE_FUNC_PTR(clReleaseMemObject); +CL_DEFINE_FUNC_PTR(clGetDeviceInfo); +CL_DEFINE_FUNC_PTR(clGetDeviceIDs); +CL_DEFINE_FUNC_PTR(clRetainEvent); +CL_DEFINE_FUNC_PTR(clGetKernelWorkGroupInfo); +CL_DEFINE_FUNC_PTR(clGetEventInfo); +CL_DEFINE_FUNC_PTR(clGetEventProfilingInfo); +CL_DEFINE_FUNC_PTR(clGetImageInfo); +CL_DEFINE_FUNC_PTR(clEnqueueCopyBufferToImage); +CL_DEFINE_FUNC_PTR(clEnqueueCopyImageToBuffer); +#if CL_TARGET_OPENCL_VERSION >= 120 +CL_DEFINE_FUNC_PTR(clRetainDevice); +CL_DEFINE_FUNC_PTR(clReleaseDevice); +CL_DEFINE_FUNC_PTR(clCreateImage); +CL_DEFINE_FUNC_PTR(clEnqueueFillImage); +#endif +#if CL_TARGET_OPENCL_VERSION >= 200 +CL_DEFINE_FUNC_PTR(clGetKernelSubGroupInfoKHR); +CL_DEFINE_FUNC_PTR(clCreateCommandQueueWithProperties); +CL_DEFINE_FUNC_PTR(clGetExtensionFunctionAddress); +CL_DEFINE_FUNC_PTR(clCreateProgramWithIL); +CL_DEFINE_FUNC_PTR(clSVMAlloc); +CL_DEFINE_FUNC_PTR(clSVMFree); +CL_DEFINE_FUNC_PTR(clEnqueueSVMMap); +CL_DEFINE_FUNC_PTR(clEnqueueSVMUnmap); +CL_DEFINE_FUNC_PTR(clSetKernelArgSVMPointer); +#endif +#undef LOAD_OPENCL_FUNCTION_PTR +} // namespace OHOS + +// clGetPlatformIDs wrapper, use OpenCLWrapper function. use OpenCLWrapper function. +cl_int clGetPlatformIDs(cl_uint num_entries, cl_platform_id *platforms, cl_uint *num_platforms) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetPlatformIDs; + MS_ASSERT(func != nullptr); + return func(num_entries, platforms, num_platforms); +} + +// clGetPlatformInfo wrapper, use OpenCLWrapper function. use OpenCLWrapper function. +cl_int clGetPlatformInfo(cl_platform_id platform, cl_platform_info param_name, size_t param_value_size, + void *param_value, size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetPlatformInfo; + MS_ASSERT(func != nullptr); + return func(platform, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clGetDeviceIDs wrapper, use OpenCLWrapper function. +cl_int clGetDeviceIDs(cl_platform_id platform, cl_device_type device_type, cl_uint num_entries, cl_device_id *devices, + cl_uint *num_devices) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetDeviceIDs; + MS_ASSERT(func != nullptr); + return func(platform, device_type, num_entries, devices, num_devices); +} + +// clGetDeviceInfo wrapper, use OpenCLWrapper function. +cl_int clGetDeviceInfo(cl_device_id device, cl_device_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetDeviceInfo; + MS_ASSERT(func != nullptr); + return func(device, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clCreateContext wrapper, use OpenCLWrapper function. +cl_context clCreateContext(const cl_context_properties *properties, cl_uint num_devices, const cl_device_id *devices, + void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), void *user_data, + cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateContext; + MS_ASSERT(func != nullptr); + return func(properties, num_devices, devices, pfn_notify, user_data, errcode_ret); +} + +// clCreateContextFromType wrapper, use OpenCLWrapper function. +cl_context clCreateContextFromType(const cl_context_properties *properties, cl_device_type device_type, + void(CL_CALLBACK *pfn_notify)(const char *, const void *, size_t, void *), + void *user_data, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateContextFromType; + MS_ASSERT(func != nullptr); + return func(properties, device_type, pfn_notify, user_data, errcode_ret); +} + +// clRetainContext wrapper, use OpenCLWrapper function. +cl_int clRetainContext(cl_context context) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainContext; + MS_ASSERT(func != nullptr); + return func(context); +} + +// clReleaseContext wrapper, use OpenCLWrapper function. +cl_int clReleaseContext(cl_context context) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseContext; + MS_ASSERT(func != nullptr); + return func(context); +} + +// clGetContextInfo wrapper, use OpenCLWrapper function. +cl_int clGetContextInfo(cl_context context, cl_context_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetContextInfo; + MS_ASSERT(func != nullptr); + return func(context, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clCreateProgramWithSource wrapper, use OpenCLWrapper function. +cl_program clCreateProgramWithSource(cl_context context, cl_uint count, const char **strings, const size_t *lengths, + cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateProgramWithSource; + MS_ASSERT(func != nullptr); + return func(context, count, strings, lengths, errcode_ret); +} + +// clCreateProgramWithBinary wrapper, use OpenCLWrapper function. +cl_program clCreateProgramWithBinary(cl_context context, cl_uint num_devices, const cl_device_id *devices_list, + const size_t *lengths, const unsigned char **binaries, cl_int *binary_status, + cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateProgramWithBinary; + MS_ASSERT(func != nullptr); + return func(context, num_devices, devices_list, lengths, binaries, binary_status, errcode_ret); +} + +// clGetProgramInfo wrapper, use OpenCLWrapper function. +cl_int clGetProgramInfo(cl_program program, cl_program_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetProgramInfo; + MS_ASSERT(func != nullptr); + return func(program, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clGetProgramBuildInfo wrapper, use OpenCLWrapper function. +cl_int clGetProgramBuildInfo(cl_program program, cl_device_id device, cl_program_build_info param_name, + size_t param_value_size, void *param_value, size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetProgramBuildInfo; + MS_ASSERT(func != nullptr); + return func(program, device, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clRetainProgram wrapper, use OpenCLWrapper function. +cl_int clRetainProgram(cl_program program) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainProgram; + MS_ASSERT(func != nullptr); + return func(program); +} + +// clReleaseProgram wrapper, use OpenCLWrapper function. +cl_int clReleaseProgram(cl_program program) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseProgram; + MS_ASSERT(func != nullptr); + return func(program); +} + +// clBuildProgram wrapper, use OpenCLWrapper function. +cl_int clBuildProgram(cl_program program, cl_uint num_devices, const cl_device_id *device_list, const char *options, + void(CL_CALLBACK *pfn_notify)(cl_program program, void *user_data), void *user_data) { + OHOS::InitOpenCL(); + auto func = OHOS::clBuildProgram; + MS_ASSERT(func != nullptr); + return func(program, num_devices, device_list, options, pfn_notify, user_data); +} + +// clCreateKernel wrapper, use OpenCLWrapper function. +cl_kernel clCreateKernel(cl_program program, const char *kernelName, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateKernel; + MS_ASSERT(func != nullptr); + return func(program, kernelName, errcode_ret); +} + +// clRetainKernel wrapper, use OpenCLWrapper function. +cl_int clRetainKernel(cl_kernel kernel) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainKernel; + MS_ASSERT(func != nullptr); + return func(kernel); +} + +// clReleaseKernel wrapper, use OpenCLWrapper function. +cl_int clReleaseKernel(cl_kernel kernel) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseKernel; + MS_ASSERT(func != nullptr); + return func(kernel); +} + +// clSetKernelArg wrapper, use OpenCLWrapper function. +cl_int clSetKernelArg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value) { + OHOS::InitOpenCL(); + auto func = OHOS::clSetKernelArg; + MS_ASSERT(func != nullptr); + return func(kernel, arg_index, arg_size, arg_value); +} + +// clCreateBuffer wrapper, use OpenCLWrapper function. +cl_mem clCreateBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateBuffer; + MS_ASSERT(func != nullptr); + return func(context, flags, size, host_ptr, errcode_ret); +} + +// clRetainMemObject wrapper, use OpenCLWrapper function. +cl_int clRetainMemObject(cl_mem memobj) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainMemObject; + MS_ASSERT(func != nullptr); + return func(memobj); +} + +// clReleaseMemObject wrapper, use OpenCLWrapper function. +cl_int clReleaseMemObject(cl_mem memobj) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseMemObject; + MS_ASSERT(func != nullptr); + return func(memobj); +} + +// clGetImageInfo wrapper, use OpenCLWrapper function. +cl_int clGetImageInfo(cl_mem image, cl_image_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetImageInfo; + MS_ASSERT(func != nullptr); + return func(image, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clRetainCommandQueue wrapper, use OpenCLWrapper function. +cl_int clRetainCommandQueue(cl_command_queue command_queue) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainCommandQueue; + MS_ASSERT(func != nullptr); + return func(command_queue); +} + +// clReleaseCommandQueue wrapper, use OpenCLWrapper function. +cl_int clReleaseCommandQueue(cl_command_queue command_queue) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseCommandQueue; + MS_ASSERT(func != nullptr); + return func(command_queue); +} + +// clEnqueueReadBuffer wrapper, use OpenCLWrapper function. +cl_int clEnqueueReadBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_read, size_t offset, + size_t size, void *ptr, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, + cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueReadBuffer; + MS_ASSERT(func != nullptr); + return func(command_queue, buffer, blocking_read, offset, size, ptr, num_events_in_wait_list, event_wait_list, event); +} + +// clEnqueueWriteBuffer wrapper, use OpenCLWrapper function. +cl_int clEnqueueWriteBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_write, size_t offset, + size_t size, const void *ptr, cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueWriteBuffer; + MS_ASSERT(func != nullptr); + return func(command_queue, buffer, blocking_write, offset, size, ptr, num_events_in_wait_list, event_wait_list, + event); +} + +// clEnqueueWriteImage wrapper, use OpenCLWrapper function. +cl_int clEnqueueWriteImage(cl_command_queue command_queue, cl_mem image, cl_bool blocking_write, const size_t *origin, + const size_t *region, size_t input_row_pitch, size_t input_slice_pitch, const void *ptr, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueWriteImage; + MS_ASSERT(func != nullptr); + return func(command_queue, image, blocking_write, origin, region, input_row_pitch, input_slice_pitch, ptr, + num_events_in_wait_list, event_wait_list, event); +} + +// clEnqueueReadImage wrapper, use OpenCLWrapper function. +cl_int clEnqueueReadImage(cl_command_queue command_queue, cl_mem image, cl_bool blocking_read, const size_t *origin, + const size_t *region, size_t row_pitch, size_t slice_pitch, void *ptr, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueReadImage; + MS_ASSERT(func != nullptr); + return func(command_queue, image, blocking_read, origin, region, row_pitch, slice_pitch, ptr, num_events_in_wait_list, + event_wait_list, event); +} + +// clEnqueueMapBuffer wrapper, use OpenCLWrapper function. +void *clEnqueueMapBuffer(cl_command_queue command_queue, cl_mem buffer, cl_bool blocking_map, cl_map_flags map_flags, + size_t offset, size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, + cl_event *event, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueMapBuffer; + MS_ASSERT(func != nullptr); + return func(command_queue, buffer, blocking_map, map_flags, offset, size, num_events_in_wait_list, event_wait_list, + event, errcode_ret); +} + +// clEnqueueMapImage wrapper, use OpenCLWrapper function. +void *clEnqueueMapImage(cl_command_queue command_queue, cl_mem image, cl_bool blocking_map, cl_map_flags map_flags, + const size_t *origin, const size_t *region, size_t *image_row_pitch, size_t *image_slice_pitch, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event, + cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueMapImage; + MS_ASSERT(func != nullptr); + return func(command_queue, image, blocking_map, map_flags, origin, region, image_row_pitch, image_slice_pitch, + num_events_in_wait_list, event_wait_list, event, errcode_ret); +} + +// clEnqueueUnmapMemObject wrapper, use OpenCLWrapper function. +cl_int clEnqueueUnmapMemObject(cl_command_queue command_queue, cl_mem memobj, void *mapped_ptr, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueUnmapMemObject; + MS_ASSERT(func != nullptr); + return func(command_queue, memobj, mapped_ptr, num_events_in_wait_list, event_wait_list, event); +} + +// clGetKernelWorkGroupInfo wrapper, use OpenCLWrapper function. +cl_int clGetKernelWorkGroupInfo(cl_kernel kernel, cl_device_id device, cl_kernel_work_group_info param_name, + size_t param_value_size, void *param_value, size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetKernelWorkGroupInfo; + MS_ASSERT(func != nullptr); + return func(kernel, device, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clGetEventProfilingInfo wrapper, use OpenCLWrapper function. +cl_int clGetEventProfilingInfo(cl_event event, cl_profiling_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetEventProfilingInfo; + MS_ASSERT(func != nullptr); + return func(event, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clEnqueueNDRangeKernel wrapper, use OpenCLWrapper function. +cl_int clEnqueueNDRangeKernel(cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim, + const size_t *global_work_offset, const size_t *global_work_size, + const size_t *local_work_size, cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueNDRangeKernel; + MS_ASSERT(func != nullptr); + return func(command_queue, kernel, work_dim, global_work_offset, global_work_size, local_work_size, + num_events_in_wait_list, event_wait_list, event); +} + +// clWaitForEvents wrapper, use OpenCLWrapper function. +cl_int clWaitForEvents(cl_uint num_events, const cl_event *event_list) { + OHOS::InitOpenCL(); + auto func = OHOS::clWaitForEvents; + MS_ASSERT(func != nullptr); + return func(num_events, event_list); +} + +// clRetainEvent wrapper, use OpenCLWrapper function. +cl_int clRetainEvent(cl_event event) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainEvent; + MS_ASSERT(func != nullptr); + return func(event); +} + +// clReleaseEvent wrapper, use OpenCLWrapper function. +cl_int clReleaseEvent(cl_event event) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseEvent; + MS_ASSERT(func != nullptr); + return func(event); +} + +// clGetEventInfo wrapper, use OpenCLWrapper function. +cl_int clGetEventInfo(cl_event event, cl_event_info param_name, size_t param_value_size, void *param_value, + size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetEventInfo; + MS_ASSERT(func != nullptr); + return func(event, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clFlush wrapper, use OpenCLWrapper function. +cl_int clFlush(cl_command_queue command_queue) { + OHOS::InitOpenCL(); + auto func = OHOS::clFlush; + MS_ASSERT(func != nullptr); + return func(command_queue); +} + +// clFinish wrapper, use OpenCLWrapper function. +cl_int clFinish(cl_command_queue command_queue) { + OHOS::InitOpenCL(); + auto func = OHOS::clFinish; + MS_ASSERT(func != nullptr); + return func(command_queue); +} + +// clCreateImage2D wrapper, use OpenCLWrapper function. +cl_mem clCreateImage2D(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, size_t imageWidth, + size_t imageHeight, size_t image_row_pitch, void *host_ptr, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateImage2D; + MS_ASSERT(func != nullptr); + return func(context, flags, image_format, imageWidth, imageHeight, image_row_pitch, host_ptr, errcode_ret); +} + +// clCreateImage3D wrapper, use OpenCLWrapper function. +cl_mem clCreateImage3D(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, size_t imageWidth, + size_t imageHeight, size_t imageDepth, size_t image_row_pitch, size_t image_slice_pitch, + void *host_ptr, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateImage3D; + MS_ASSERT(func != nullptr); + return func(context, flags, image_format, imageWidth, imageHeight, imageDepth, image_row_pitch, image_slice_pitch, + host_ptr, errcode_ret); +} + +// clCreateCommandQueue wrapper, use OpenCLWrapper function. +cl_command_queue clCreateCommandQueue(cl_context context, cl_device_id device, cl_command_queue_properties properties, + cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateCommandQueue; + MS_ASSERT(func != nullptr); + return func(context, device, properties, errcode_ret); +} + +// clGetCommandQueueInfo wrapper, use OpenCLWrapper function. +cl_int clGetCommandQueueInfo(cl_command_queue command_queue, cl_command_queue_info param_name, size_t param_value_size, + void *param_value, size_t *param_value_size_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetCommandQueueInfo; + MS_ASSERT(func != nullptr); + return func(command_queue, param_name, param_value_size, param_value, param_value_size_ret); +} + +// clEnqueueCopyImage wrapper, use OpenCLWrapper function. +cl_int clEnqueueCopyImage(cl_command_queue queue, cl_mem src_image, cl_mem dst_image, const size_t *src_origin, + const size_t *dst_origin, const size_t *region, cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueCopyImage; + MS_ASSERT(func != nullptr); + return func(queue, src_image, dst_image, src_origin, dst_origin, region, num_events_in_wait_list, event_wait_list, + event); +} + +// clEnqueueCopyBufferToImage wrapper, use OpenCLWrapper function. +cl_int clEnqueueCopyBufferToImage(cl_command_queue command_queue, cl_mem src_buffer, cl_mem dst_image, + size_t src_offset, const size_t *dst_origin, const size_t *region, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueCopyBufferToImage; + MS_ASSERT(func != nullptr); + return func(command_queue, src_buffer, dst_image, src_offset, dst_origin, region, num_events_in_wait_list, + event_wait_list, event); +} + +// clEnqueueCopyImageToBuffer wrapper, use OpenCLWrapper function. +cl_int clEnqueueCopyImageToBuffer(cl_command_queue command_queue, cl_mem src_image, cl_mem dst_buffer, + const size_t *src_origin, const size_t *region, size_t dst_offset, + cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueCopyImageToBuffer; + MS_ASSERT(func != nullptr); + return func(command_queue, src_image, dst_buffer, src_origin, region, dst_offset, num_events_in_wait_list, + event_wait_list, event); +} + +#if CL_TARGET_OPENCL_VERSION >= 120 + +// clRetainDevice wrapper, use OpenCLWrapper function. +cl_int clRetainDevice(cl_device_id device) { + OHOS::InitOpenCL(); + auto func = OHOS::clRetainDevice; + MS_ASSERT(func != nullptr); + return func(device); +} + +// clReleaseDevice wrapper, use OpenCLWrapper function. +cl_int clReleaseDevice(cl_device_id device) { + OHOS::InitOpenCL(); + auto func = OHOS::clReleaseDevice; + MS_ASSERT(func != nullptr); + return func(device); +} + +// clCreateImage wrapper, use OpenCLWrapper function. +cl_mem clCreateImage(cl_context context, cl_mem_flags flags, const cl_image_format *image_format, + const cl_image_desc *image_desc, void *host_ptr, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateImage; + MS_ASSERT(func != nullptr); + return func(context, flags, image_format, image_desc, host_ptr, errcode_ret); +} + +cl_int clEnqueueFillImage(cl_command_queue command_queue, cl_mem image, const void *fill_color, const size_t *origin, + const size_t *region, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, + cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueFillImage; + MS_ASSERT(func != nullptr); + return func(command_queue, image, fill_color, origin, region, num_events_in_wait_list, event_wait_list, event); +} + +#endif + +#if CL_TARGET_OPENCL_VERSION >= 200 + +// clCreateCommandQueueWithProperties wrapper, use OpenCLWrapper function. +cl_command_queue clCreateCommandQueueWithProperties(cl_context context, cl_device_id device, + const cl_queue_properties *properties, cl_int *errcode_ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateCommandQueueWithProperties; + MS_ASSERT(func != nullptr); + return func(context, device, properties, errcode_ret); +} + +// clGetExtensionFunctionAddress wrapper, use OpenCLWrapper function. +void *clGetExtensionFunctionAddress(const char *func_name) { + OHOS::InitOpenCL(); + auto func = OHOS::clGetExtensionFunctionAddress; + MS_ASSERT(func != nullptr); + return func(func_name); +} +// clCreateProgramWithIL wrapper, use OpenCLWrapper function. +cl_program clCreateProgramWithIL(cl_context context, const void *il, size_t length, cl_int *ret) { + OHOS::InitOpenCL(); + auto func = OHOS::clCreateProgramWithIL; + MS_ASSERT(func != nullptr); + return func(context, il, length, ret); +} + +// clSVMAlloc wrapper, use OpenCLWrapper function. +void *clSVMAlloc(cl_context context, cl_mem_flags flags, size_t size, cl_uint align) { + OHOS::InitOpenCL(); + auto func = OHOS::clSVMAlloc; + MS_ASSERT(func != nullptr); + return func(context, flags, size, align); +} + +// clSVMFree wrapper, use OpenCLWrapper function. +void clSVMFree(cl_context context, void *buffer) { + OHOS::InitOpenCL(); + auto func = OHOS::clSVMFree; + MS_ASSERT(func != nullptr); + func(context, buffer); +} + +// clEnqueueSVMMap wrapper, use OpenCLWrapper function. +cl_int clEnqueueSVMMap(cl_command_queue command_queue, cl_bool blocking, cl_map_flags flags, void *host_ptr, + size_t size, cl_uint num_events_in_wait_list, const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueSVMMap; + MS_ASSERT(func != nullptr); + return func(command_queue, blocking, flags, host_ptr, size, num_events_in_wait_list, event_wait_list, event); +} + +// clEnqueueSVMUnmap wrapper, use OpenCLWrapper function. +cl_int clEnqueueSVMUnmap(cl_command_queue command_queue, void *host_ptr, cl_uint num_events_in_wait_list, + const cl_event *event_wait_list, cl_event *event) { + OHOS::InitOpenCL(); + auto func = OHOS::clEnqueueSVMUnmap; + MS_ASSERT(func != nullptr); + return func(command_queue, host_ptr, num_events_in_wait_list, event_wait_list, event); +} + +// clSetKernelArgSVMPointer wrapper, use OpenCLWrapper function. +cl_int clSetKernelArgSVMPointer(cl_kernel kernel, cl_uint index, const void *host_ptr) { + OHOS::InitOpenCL(); + auto func = OHOS::clSetKernelArgSVMPointer; + MS_ASSERT(func != nullptr); + return func(kernel, index, host_ptr); +} +#endif + +#endif // USE_OPENCL_WRAPPER