1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OPENCL_WRAPPER_H_
17#define OPENCL_WRAPPER_H_
18
19#include <memory>
20#include <string>
21#define CL_TARGET_OPENCL_VERSION 300
22#include <CL/cl.h>
23
24#ifdef USE_OPENCL_WRAPPER
25#define MS_ASSERT(f) ((void)0)
26
27namespace OHOS {
28// This is a opencl function wrapper.
29bool LoadOpenCLLibrary(void **handle_ptr);
30bool UnLoadOpenCLLibrary(void *handle);
31bool InitOpenCL();
32
33bool InitOpenCLExtern(void **clSoHandle);
34bool UnLoadCLExtern(void *clSoHandle);
35
36// get platform id
37using clGetPlatformIDsFunc = cl_int (*)(cl_uint, cl_platform_id *, cl_uint *);
38// get platform info
39using clGetPlatformInfoFunc = cl_int (*)(cl_platform_id, cl_platform_info, size_t, void *, size_t *);
40// build program
41using clBuildProgramFunc = cl_int (*)(cl_program, cl_uint, const cl_device_id *, const char *,
42                                      void (*pfn_notify)(cl_program, void *), void *);
43// enqueue run kernel
44using clEnqueueNDRangeKernelFunc = cl_int (*)(cl_command_queue, cl_kernel, cl_uint, const size_t *, const size_t *,
45                                              const size_t *, cl_uint, const cl_event *, cl_event *);
46// set kernel parameter
47using clSetKernelArgFunc = cl_int (*)(cl_kernel, cl_uint, size_t, const void *);
48using clRetainMemObjectFunc = cl_int (*)(cl_mem);
49using clReleaseMemObjectFunc = cl_int (*)(cl_mem);
50using clEnqueueUnmapMemObjectFunc = cl_int (*)(cl_command_queue, cl_mem, void *, cl_uint, const cl_event *, cl_event *);
51using clRetainCommandQueueFunc = cl_int (*)(cl_command_queue command_queue);
52// create context
53using clCreateContextFunc = cl_context (*)(const cl_context_properties *, cl_uint, const cl_device_id *,
54                                           void(CL_CALLBACK *)(  // NOLINT(readability/casting)
55                                             const char *, const void *, size_t, void *),
56                                           void *, cl_int *);
57using clEnqueueCopyImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
58                                          const size_t *, cl_uint, const cl_event *, cl_event *);
59
60using clCreateContextFromTypeFunc = cl_context (*)(const cl_context_properties *, cl_device_type,
61                                                   void(CL_CALLBACK *)(  // NOLINT(readability/casting)
62                                                     const char *, const void *, size_t, void *),
63                                                   void *, cl_int *);
64using clReleaseContextFunc = cl_int (*)(cl_context);
65using clWaitForEventsFunc = cl_int (*)(cl_uint, const cl_event *);
66using clReleaseEventFunc = cl_int (*)(cl_event);
67using clEnqueueWriteBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, const void *, cl_uint,
68                                            const cl_event *, cl_event *);
69using clEnqueueWriteImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
70                                           size_t, const void *, cl_uint, const cl_event *, cl_event *);
71using clEnqueueReadImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *, size_t,
72                                          size_t, void *, cl_uint, const cl_event *, cl_event *);
73using clEnqueueReadBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, size_t, size_t, void *, cl_uint,
74                                           const cl_event *, cl_event *);
75using clEnqueueReadBufferRectFunc = cl_int (*)(cl_command_queue, cl_mem, cl_bool, const size_t *, const size_t *,
76                                               const size_t *, size_t, size_t, size_t, size_t, void *, cl_uint,
77                                               const cl_event *, cl_event *);
78using clGetProgramBuildInfoFunc = cl_int (*)(cl_program, cl_device_id, cl_program_build_info, size_t, void *, size_t *);
79using clRetainProgramFunc = cl_int (*)(cl_program program);
80using clEnqueueMapBufferFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, size_t, size_t, cl_uint,
81                                         const cl_event *, cl_event *, cl_int *);
82using clEnqueueMapImageFunc = void *(*)(cl_command_queue, cl_mem, cl_bool, cl_map_flags, const size_t *, const size_t *,
83                                        size_t *, size_t *, cl_uint, const cl_event *, cl_event *, cl_int *);
84using clCreateCommandQueueFunc = cl_command_queue (*)(cl_context, cl_device_id, cl_command_queue_properties, cl_int *);
85using clGetCommandQueueInfoFunc = cl_int (*)(cl_command_queue, cl_command_queue_info, size_t, void *, size_t *);
86using clReleaseCommandQueueFunc = cl_int (*)(cl_command_queue);
87using clCreateProgramWithBinaryFunc = cl_program (*)(cl_context, cl_uint, const cl_device_id *, const size_t *,
88                                                     const unsigned char **, cl_int *, cl_int *);
89using clRetainContextFunc = cl_int (*)(cl_context context);
90using clGetContextInfoFunc = cl_int (*)(cl_context, cl_context_info, size_t, void *, size_t *);
91using clReleaseProgramFunc = cl_int (*)(cl_program program);
92using clFlushFunc = cl_int (*)(cl_command_queue command_queue);
93using clFinishFunc = cl_int (*)(cl_command_queue command_queue);
94using clGetProgramInfoFunc = cl_int (*)(cl_program, cl_program_info, size_t, void *, size_t *);
95using clCreateKernelFunc = cl_kernel (*)(cl_program, const char *, cl_int *);
96using clRetainKernelFunc = cl_int (*)(cl_kernel kernel);
97using clCreateBufferFunc = cl_mem (*)(cl_context, cl_mem_flags, size_t, void *, cl_int *);
98using clCreateImage2DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
99                                       void *, cl_int *);
100using clImportMemoryARMFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, void *, ssize_t, cl_int *);
101using clCreateImage3DFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, size_t, size_t, size_t,
102                                       size_t, size_t, void *, cl_int *);
103using clCreateProgramWithSourceFunc = cl_program (*)(cl_context, cl_uint, const char **, const size_t *, cl_int *);
104using clReleaseKernelFunc = cl_int (*)(cl_kernel kernel);
105using clGetDeviceInfoFunc = cl_int (*)(cl_device_id, cl_device_info, size_t, void *, size_t *);
106using clGetDeviceIDsFunc = cl_int (*)(cl_platform_id, cl_device_type, cl_uint, cl_device_id *, cl_uint *);
107using clRetainEventFunc = cl_int (*)(cl_event);
108using clGetKernelWorkGroupInfoFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_work_group_info, size_t, void *,
109                                                size_t *);
110using clGetEventInfoFunc = cl_int (*)(cl_event event, cl_event_info param_name, size_t param_value_size,
111                                      void *param_value, size_t *param_value_size_ret);
112using clGetEventProfilingInfoFunc = cl_int (*)(cl_event event, cl_profiling_info param_name, size_t param_value_size,
113                                               void *param_value, size_t *param_value_size_ret);
114using clGetImageInfoFunc = cl_int (*)(cl_mem, cl_image_info, size_t, void *, size_t *);
115using clEnqueueCopyBufferToImageFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, size_t, const size_t *,
116                                                  const size_t *, cl_uint, const cl_event *, cl_event *);
117using clEnqueueCopyImageToBufferFunc = cl_int (*)(cl_command_queue, cl_mem, cl_mem, const size_t *, const size_t *,
118                                                  size_t, cl_uint, const cl_event *, cl_event *);
119#if CL_TARGET_OPENCL_VERSION >= 120
120using clRetainDeviceFunc = cl_int (*)(cl_device_id);
121using clReleaseDeviceFunc = cl_int (*)(cl_device_id);
122using clCreateImageFunc = cl_mem (*)(cl_context, cl_mem_flags, const cl_image_format *, const cl_image_desc *, void *,
123                                     cl_int *);
124using clEnqueueFillImageFunc = cl_int (*)(cl_command_queue, cl_mem, const void *, const size_t *, const size_t *,
125                                          cl_uint, const cl_event *, cl_event *);
126#endif
127#if CL_TARGET_OPENCL_VERSION >= 200
128using clCreateProgramWithILFunc = cl_program (*)(cl_context, const void *, size_t, cl_int *);
129using clSVMAllocFunc = void *(*)(cl_context, cl_mem_flags, size_t size, cl_uint);
130using clSVMFreeFunc = void (*)(cl_context, void *);
131using clEnqueueSVMMapFunc = cl_int (*)(cl_command_queue, cl_bool, cl_map_flags, void *, size_t, cl_uint,
132                                       const cl_event *, cl_event *);
133using clEnqueueSVMUnmapFunc = cl_int (*)(cl_command_queue, void *, cl_uint, const cl_event *, cl_event *);
134using clSetKernelArgSVMPointerFunc = cl_int (*)(cl_kernel, cl_uint, const void *);
135// opencl 2.0 can get sub group info and wave size.
136using clGetKernelSubGroupInfoKHRFunc = cl_int (*)(cl_kernel, cl_device_id, cl_kernel_sub_group_info, size_t,
137                                                  const void *, size_t, void *, size_t *);
138using clCreateCommandQueueWithPropertiesFunc = cl_command_queue (*)(cl_context, cl_device_id,
139                                                                    const cl_queue_properties *, cl_int *);
140using clGetExtensionFunctionAddressFunc = void *(*)(const char *);
141#endif
142
143#define CL_DECLARE_FUNC_PTR(func) extern func##Func func
144
145CL_DECLARE_FUNC_PTR(clGetPlatformIDs);
146CL_DECLARE_FUNC_PTR(clGetPlatformInfo);
147CL_DECLARE_FUNC_PTR(clBuildProgram);
148CL_DECLARE_FUNC_PTR(clEnqueueNDRangeKernel);
149CL_DECLARE_FUNC_PTR(clSetKernelArg);
150CL_DECLARE_FUNC_PTR(clReleaseKernel);
151CL_DECLARE_FUNC_PTR(clCreateProgramWithSource);
152CL_DECLARE_FUNC_PTR(clCreateBuffer);
153CL_DECLARE_FUNC_PTR(clCreateImage2D);
154CL_DECLARE_FUNC_PTR(clImportMemoryARM);
155CL_DECLARE_FUNC_PTR(clCreateImage3D);
156CL_DECLARE_FUNC_PTR(clRetainKernel);
157CL_DECLARE_FUNC_PTR(clCreateKernel);
158CL_DECLARE_FUNC_PTR(clGetProgramInfo);
159CL_DECLARE_FUNC_PTR(clFlush);
160CL_DECLARE_FUNC_PTR(clFinish);
161CL_DECLARE_FUNC_PTR(clReleaseProgram);
162CL_DECLARE_FUNC_PTR(clRetainContext);
163CL_DECLARE_FUNC_PTR(clGetContextInfo);
164CL_DECLARE_FUNC_PTR(clCreateProgramWithBinary);
165CL_DECLARE_FUNC_PTR(clCreateCommandQueue);
166CL_DECLARE_FUNC_PTR(clGetCommandQueueInfo);
167CL_DECLARE_FUNC_PTR(clReleaseCommandQueue);
168CL_DECLARE_FUNC_PTR(clEnqueueMapBuffer);
169CL_DECLARE_FUNC_PTR(clEnqueueMapImage);
170CL_DECLARE_FUNC_PTR(clEnqueueCopyImage);
171CL_DECLARE_FUNC_PTR(clRetainProgram);
172CL_DECLARE_FUNC_PTR(clGetProgramBuildInfo);
173CL_DECLARE_FUNC_PTR(clEnqueueReadBuffer);
174CL_DECLARE_FUNC_PTR(clEnqueueReadBufferRect);
175CL_DECLARE_FUNC_PTR(clEnqueueWriteBuffer);
176CL_DECLARE_FUNC_PTR(clEnqueueWriteImage);
177CL_DECLARE_FUNC_PTR(clEnqueueReadImage);
178CL_DECLARE_FUNC_PTR(clWaitForEvents);
179CL_DECLARE_FUNC_PTR(clReleaseEvent);
180CL_DECLARE_FUNC_PTR(clCreateContext);
181CL_DECLARE_FUNC_PTR(clCreateContextFromType);
182CL_DECLARE_FUNC_PTR(clReleaseContext);
183CL_DECLARE_FUNC_PTR(clRetainCommandQueue);
184CL_DECLARE_FUNC_PTR(clEnqueueUnmapMemObject);
185CL_DECLARE_FUNC_PTR(clRetainMemObject);
186CL_DECLARE_FUNC_PTR(clReleaseMemObject);
187CL_DECLARE_FUNC_PTR(clGetDeviceInfo);
188CL_DECLARE_FUNC_PTR(clGetDeviceIDs);
189CL_DECLARE_FUNC_PTR(clRetainEvent);
190CL_DECLARE_FUNC_PTR(clGetKernelWorkGroupInfo);
191CL_DECLARE_FUNC_PTR(clGetEventInfo);
192CL_DECLARE_FUNC_PTR(clGetEventProfilingInfo);
193CL_DECLARE_FUNC_PTR(clGetImageInfo);
194CL_DECLARE_FUNC_PTR(clEnqueueCopyBufferToImage);
195CL_DECLARE_FUNC_PTR(clEnqueueCopyImageToBuffer);
196#if CL_TARGET_OPENCL_VERSION >= 120
197CL_DECLARE_FUNC_PTR(clRetainDevice);
198CL_DECLARE_FUNC_PTR(clReleaseDevice);
199CL_DECLARE_FUNC_PTR(clCreateImage);
200CL_DECLARE_FUNC_PTR(clEnqueueFillImage);
201#endif
202#if CL_TARGET_OPENCL_VERSION >= 200
203CL_DECLARE_FUNC_PTR(clGetKernelSubGroupInfoKHR);
204CL_DECLARE_FUNC_PTR(clCreateCommandQueueWithProperties);
205CL_DECLARE_FUNC_PTR(clGetExtensionFunctionAddress);
206CL_DECLARE_FUNC_PTR(clCreateProgramWithIL);
207CL_DECLARE_FUNC_PTR(clSVMAlloc);
208CL_DECLARE_FUNC_PTR(clSVMFree);
209CL_DECLARE_FUNC_PTR(clEnqueueSVMMap);
210CL_DECLARE_FUNC_PTR(clEnqueueSVMUnmap);
211CL_DECLARE_FUNC_PTR(clSetKernelArgSVMPointer);
212#endif
213
214#undef CL_DECLARE_FUNC_PTR
215}  // namespace OHOS
216#endif  // USE_OPENCL_WRAPPER
217#endif  // OPENCL_WRAPPER_H_
218