11 #ifndef TESSERACT_OPENCL_OPENCLWRAPPER_H_
12 #define TESSERACT_OPENCL_OPENCLWRAPPER_H_
15 #include "allheaders.h"
28 #include <OpenCL/cl.h>
33 struct TessDeviceScore;
37 DS_DEVICE_NATIVE_CPU = 0,
38 DS_DEVICE_OPENCL_DEVICE
43 cl_device_id oclDeviceID;
45 char* oclDriverVersion;
47 TessDeviceScore* score;
51 #define strcasecmp strcmp
54 #define MAX_KERNEL_STRING_LEN 64
55 #define MAX_CLFILE_NUM 50
56 #define MAX_CLKERNEL_NUM 200
57 #define MAX_KERNEL_NAME_LEN 64
58 #define CL_QUEUE_THREAD_HANDLE_AMD 0x403E
59 #define GROUPSIZE_X 16
60 #define GROUPSIZE_Y 16
61 #define GROUPSIZE_HMORX 256
62 #define GROUPSIZE_HMORY 1
65 cl_context mpkContext;
66 cl_command_queue mpkCmdQueue;
67 cl_program mpkProgram;
69 char mckKernelName[150];
73 cl_platform_id mpOclPlatformID;
74 cl_context mpOclContext;
75 cl_device_id mpOclDevsID;
76 cl_command_queue mpOclCmdQueue;
78 typedef int (*cl_kernel_function)(
void** userdata, KernelEnv* kenv);
80 #define CHECK_OPENCL(status, name) \
81 if (status != CL_SUCCESS) { \
82 tprintf("OpenCL error code is %d at when %s .\n", status, name); \
87 cl_platform_id mpPlatformID;
88 cl_device_type mDevType;
90 cl_device_id* mpArryDevsID;
92 cl_command_queue mpCmdQueue;
93 cl_kernel mpArryKernels[MAX_CLFILE_NUM];
94 cl_program mpArryPrograms[MAX_CLFILE_NUM];
96 char mArryKnelSrcFile[MAX_CLFILE_NUM]
98 mArrykernelNames[MAX_CLKERNEL_NUM][MAX_KERNEL_STRING_LEN + 1];
99 cl_kernel_function mpArryKnelFuncs[MAX_CLKERNEL_NUM];
100 int mnKernelCount, mnFileCount,
109 static GPUEnv gpuEnv;
113 static int InitEnv();
114 static int InitOpenclRunEnv(
116 static int InitOpenclRunEnv_DeviceSelection(
118 static int RegistOpenclKernel();
119 static int ReleaseOpenclRunEnv();
120 static int ReleaseOpenclEnv(GPUEnv* gpuInfo);
121 static int CompileKernelFile(GPUEnv* gpuInfo,
const char* buildOption);
122 static int CachedOfKernerPrg(
const GPUEnv* gpuEnvCached,
123 const char* clFileName);
124 static int GeneratBinFromKernelSource(cl_program program,
125 const char* clFileName);
126 static int WriteBinaryToFile(
const char* fileName,
const char* birary,
128 static int BinaryGenerated(
const char* clFileName, FILE** fhandle);
131 static l_uint32* pixReadFromTiffKernel(l_uint32* tiffdata, l_int32 w,
132 l_int32 h, l_int32 wpl,
134 static int composeRGBPixelCl(
int* tiffdata,
int* line,
int h,
int w);
139 static int initMorphCLAllocations(l_int32 wpl, l_int32 h, Pix* pixs);
140 static void releaseMorphCLBuffers();
142 static void pixGetLinesCL(Pix* pixd, Pix* pixs, Pix** pix_vline,
143 Pix** pix_hline, Pix** pixClosed,
bool getpixClosed,
144 l_int32 close_hsize, l_int32 close_vsize,
145 l_int32 open_hsize, l_int32 open_vsize,
146 l_int32 line_hsize, l_int32 line_vsize);
150 static int SetKernelEnv(KernelEnv* envInfo);
160 static int LoadOpencl();
163 static void FreeOpenclDll();
166 inline static int AddKernelConfig(
int kCount,
const char* kName);
169 static int HistogramRectOCL(
void* imagedata,
int bytes_per_pixel,
170 int bytes_per_line,
int left,
int top,
int width,
172 int* histogramAllChannels);
174 static int ThresholdRectToPixOCL(
unsigned char* imagedata,
175 int bytes_per_pixel,
int bytes_per_line,
176 int* thresholds,
int* hi_values, Pix** pix,
177 int rect_height,
int rect_width,
178 int rect_top,
int rect_left);
180 static ds_device getDeviceSelection();
181 static ds_device selectedDevice;
182 static bool deviceIsSelected;
183 static bool selectedDeviceIsOpenCL();
187 #endif // TESSERACT_OPENCL_OPENCLWRAPPER_H_