11#ifndef TESSERACT_OPENCL_OPENCLWRAPPER_H_
12#define TESSERACT_OPENCL_OPENCLWRAPPER_H_
14#include <allheaders.h>
28# include <OpenCL/cl.h>
36struct TessDeviceScore;
39enum ds_device_type { DS_DEVICE_NATIVE_CPU = 0, 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;
78typedef 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][256],
97 mArrykernelNames[MAX_CLKERNEL_NUM][MAX_KERNEL_STRING_LEN + 1];
98 cl_kernel_function mpArryKnelFuncs[MAX_CLKERNEL_NUM];
99 int mnKernelCount, mnFileCount,
108 static GPUEnv gpuEnv;
112 static int InitEnv();
113 static int InitOpenclRunEnv(
int argc);
114 static int InitOpenclRunEnv_DeviceSelection(
116 static int RegistOpenclKernel();
117 static int ReleaseOpenclRunEnv();
118 static int ReleaseOpenclEnv(GPUEnv *gpuInfo);
119 static int CompileKernelFile(GPUEnv *gpuInfo,
const char *buildOption);
120 static int CachedOfKernerPrg(
const GPUEnv *gpuEnvCached,
const char *clFileName);
121 static int GeneratBinFromKernelSource(cl_program program,
const char *clFileName);
122 static int WriteBinaryToFile(
const char *fileName,
const char *birary,
size_t numBytes);
123 static int BinaryGenerated(
const char *clFileName, FILE **fhandle);
126 static l_uint32 *pixReadFromTiffKernel(l_uint32 *tiffdata, l_int32 w, l_int32 h, l_int32 wpl,
128 static int composeRGBPixelCl(
int *tiffdata,
int *line,
int h,
int w);
133 static int initMorphCLAllocations(l_int32 wpl, l_int32 h, Image pixs);
134 static void releaseMorphCLBuffers();
136 static void pixGetLinesCL(Image pixd, Image pixs, Image *pix_vline, Image *pix_hline, Image *pixClosed,
137 bool getpixClosed, l_int32 close_hsize, l_int32 close_vsize,
138 l_int32 open_hsize, l_int32 open_vsize, l_int32 line_hsize,
143 static int SetKernelEnv(KernelEnv *envInfo);
153 static int LoadOpencl();
156 static void FreeOpenclDll();
159 inline static int AddKernelConfig(
int kCount,
const char *kName);
162 static int HistogramRectOCL(
void *imagedata,
int bytes_per_pixel,
int bytes_per_line,
int left,
164 int *histogramAllChannels);
166 static int ThresholdRectToPixOCL(
unsigned char *imagedata,
int bytes_per_pixel,
167 int bytes_per_line,
int *thresholds,
int *hi_values, Image *pix,
168 int rect_height,
int rect_width,
int rect_top,
int rect_left);
170 static ds_device getDeviceSelection();
171 static ds_device selectedDevice;
172 static bool deviceIsSelected;
173 static bool selectedDeviceIsOpenCL();