core/ocl: update dynamic runtime
[platform/upstream/opencv.git] / modules / core / src / opencl / runtime / opencl_core.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the OpenCV Foundation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #if defined(HAVE_OPENCL) && !defined(HAVE_OPENCL_STATIC)
45
46 #include "opencv2/core.hpp" // CV_Error
47
48 #include "opencv2/core/opencl/runtime/opencl_core.hpp"
49
50 static const char* funcToCheckOpenCL1_1 = "clEnqueueReadBufferRect";
51 #define ERROR_MSG_CANT_LOAD "Failed to load OpenCL runtime\n"
52 #define ERROR_MSG_INVALID_VERSION "Failed to load OpenCL runtime (expected version 1.1+)\n"
53
54 #if defined(__APPLE__)
55 #include <dlfcn.h>
56
57 static void* AppleCLGetProcAddress(const char* name)
58 {
59     static bool initialized = false;
60     static void* handle = NULL;
61     if (!handle)
62     {
63         if(!initialized)
64         {
65             initialized = true;
66             const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
67             const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
68             if (envPath)
69                 path = envPath;
70             handle = dlopen(oclpath, RTLD_LAZY | RTLD_GLOBAL);
71             if (handle == NULL)
72             {
73                 fprintf(stderr, ERROR_MSG_CANT_LOAD);
74             }
75             else if (dlsym(handle, funcToCheckOpenCL1_1) == NULL)
76             {
77                 fprintf(stderr, ERROR_MSG_INVALID_VERSION);
78                 handle = NULL;
79             }
80         }
81         if (!handle)
82             return NULL;
83     }
84     return dlsym(handle, name);
85 }
86 #define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name)
87 #endif // __APPLE__
88
89 #if defined(_WIN32)
90 #include <windows.h>
91
92 static void* WinGetProcAddress(const char* name)
93 {
94     static bool initialized = false;
95     static HMODULE handle = NULL;
96     if (!handle)
97     {
98         if(!initialized)
99         {
100             initialized = true;
101             handle = GetModuleHandleA("OpenCL.dll");
102             if (!handle)
103             {
104                 const char* path = "OpenCL.dll";
105                 const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
106                 if (envPath)
107                     path = envPath;
108                 handle = LoadLibraryA(path);
109                 if (!handle)
110                 {
111                     fprintf(stderr, ERROR_MSG_CANT_LOAD);
112                 }
113                 else if (GetProcAddress(handle, funcToCheckOpenCL1_1) == NULL)
114                 {
115                     fprintf(stderr, ERROR_MSG_INVALID_VERSION);
116                     handle = NULL;
117                 }
118             }
119         }
120         if (!handle)
121             return NULL;
122     }
123     return (void*)GetProcAddress(handle, name);
124 }
125 #define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name)
126 #endif // _WIN32
127
128 #if defined(linux)
129 #include <dlfcn.h>
130 #include <stdio.h>
131
132 static void* GetProcAddress(const char* name)
133 {
134     static bool initialized = false;
135     static void* handle = NULL;
136     if (!handle)
137     {
138         if(!initialized)
139         {
140             initialized = true;
141             const char* path = "libOpenCL.so";
142             const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
143             if (envPath)
144                 path = envPath;
145             handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
146             if (handle == NULL)
147             {
148                 fprintf(stderr, ERROR_MSG_CANT_LOAD);
149             }
150             else if (dlsym(handle, funcToCheckOpenCL1_1) == NULL)
151             {
152                 fprintf(stderr, ERROR_MSG_INVALID_VERSION);
153                 handle = NULL;
154             }
155         }
156         if (!handle)
157             return NULL;
158     }
159     return dlsym(handle, name);
160 }
161 #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name)
162 #endif
163
164 #ifndef CV_CL_GET_PROC_ADDRESS
165 #define CV_CL_GET_PROC_ADDRESS(name) NULL
166 #endif
167
168 static void* opencl_check_fn(int ID);
169
170 #include "runtime_common.hpp"
171
172 //
173 // BEGIN OF CUSTOM FUNCTIONS
174 //
175
176 #define CUSTOM_FUNCTION_ID 1000
177
178 #undef ADDITIONAL_FN_DEFINITIONS
179
180 //
181 // END OF CUSTOM FUNCTIONS HERE
182 //
183
184 #include "autogenerated/opencl_core_impl.hpp"
185
186 static void* opencl_check_fn(int ID)
187 {
188     ID = (ID <= CUSTOM_FUNCTION_ID) ? ID : ID - CUSTOM_FUNCTION_ID;
189     assert(ID >= 0 && ID < (int)(sizeof(opencl_fn_list)/sizeof(opencl_fn_list[0])));
190     const struct DynamicFnEntry* e = opencl_fn_list[ID];
191     void* func = CV_CL_GET_PROC_ADDRESS(e->fnName);
192     if (!func)
193     {
194         CV_Error(cv::Error::OpenCLApiCallError, cv::format("OpenCL function is not available: [%s]", e->fnName));
195     }
196     *(e->ppFn) = func;
197     return func;
198 }
199
200 #endif