ocl: fix CL_RUNTIME_EXPORT for master branch
[profile/ivi/opencv.git] / modules / ocl / src / cl_runtime / cl_runtime.cpp
1 #include "precomp.hpp"
2
3 #if defined(HAVE_OPENCL) && (!defined(__APPLE__) || defined(IOS))
4
5 #include "opencv2/ocl/cl_runtime/cl_runtime.hpp"
6
7 #if defined(__APPLE__)
8     #include <dlfcn.h>
9
10     static void* AppleCLGetProcAddress(const char* name)
11     {
12         static void * image = NULL;
13         if (!image)
14         {
15             image = dlopen("/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL", RTLD_LAZY | RTLD_GLOBAL);
16             if (!image)
17                 return NULL;
18         }
19
20         return dlsym(image, name);
21     }
22     #define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name)
23 #endif // __APPLE__
24
25 #if defined(_WIN32)
26     static void* WinGetProcAddress(const char* name)
27     {
28         static HMODULE opencl_module = NULL;
29         if (!opencl_module)
30         {
31             opencl_module = GetModuleHandleA("OpenCL.dll");
32             if (!opencl_module)
33             {
34                 const char* name = "OpenCL.dll";
35                 const char* envOpenCLBinary = getenv("OPENCV_OPENCL_BINARY");
36                 if (envOpenCLBinary)
37                     name = envOpenCLBinary;
38                 opencl_module = LoadLibraryA(name);
39                 if (!opencl_module)
40                     return NULL;
41             }
42         }
43         return (void*)GetProcAddress(opencl_module, name);
44     }
45     #define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name)
46 #endif // _WIN32
47
48 #if defined(linux)
49     #include <dlfcn.h>
50     #include <stdio.h>
51
52     static void* GetProcAddress (const char* name)
53     {
54         static void* h = NULL;
55         if (!h)
56         {
57             const char* name = "libOpenCL.so";
58             const char* envOpenCLBinary = getenv("OPENCV_OPENCL_BINARY");
59             if (envOpenCLBinary)
60                 name = envOpenCLBinary;
61             h = dlopen(name, RTLD_LAZY | RTLD_GLOBAL);
62             if (!h)
63                 return NULL;
64         }
65
66         return dlsym(h, name);
67     }
68     #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name)
69 #endif
70
71 #ifndef CV_CL_GET_PROC_ADDRESS
72 #define CV_CL_GET_PROC_ADDRESS(name) NULL
73 #endif
74
75 static void* opencl_check_fn(int ID)
76 {
77     extern const char* opencl_fn_names[];
78     void* func = CV_CL_GET_PROC_ADDRESS(opencl_fn_names[ID]);
79     if (!func)
80     {
81         std::ostringstream msg;
82         msg << "OpenCL function is not available: [" << opencl_fn_names[ID] << "]";
83         CV_Error(CV_StsBadFunc, msg.str());
84     }
85     extern void* opencl_fn_ptrs[];
86     *(void**)(opencl_fn_ptrs[ID]) = func;
87     return func;
88 }
89
90 #if defined(HAVE_OPENCL12)
91 #include "cl_runtime_opencl12_impl.hpp"
92 #elif defined(HAVE_OPENCL11)
93 #include "cl_runtime_opencl11_impl.hpp"
94 #else
95 #error Invalid OpenCL configuration
96 #endif
97
98 #endif