1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
42 #include "precomp.hpp"
44 #if defined(HAVE_OPENCL) && !defined(HAVE_OPENCL_STATIC)
46 #include "opencv2/core.hpp" // CV_Error
48 #include "opencv2/core/opencl/runtime/opencl_core.hpp"
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"
54 #if defined(__APPLE__)
57 static void* AppleCLGetProcAddress(const char* name)
59 static bool initialized = false;
60 static void* handle = NULL;
66 const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
67 const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
70 handle = dlopen(oclpath, RTLD_LAZY | RTLD_GLOBAL);
73 fprintf(stderr, ERROR_MSG_CANT_LOAD);
75 else if (dlsym(handle, funcToCheckOpenCL1_1) == NULL)
77 fprintf(stderr, ERROR_MSG_INVALID_VERSION);
84 return dlsym(handle, name);
86 #define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name)
92 static void* WinGetProcAddress(const char* name)
94 static bool initialized = false;
95 static HMODULE handle = NULL;
101 handle = GetModuleHandleA("OpenCL.dll");
104 const char* path = "OpenCL.dll";
105 const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
108 handle = LoadLibraryA(path);
111 fprintf(stderr, ERROR_MSG_CANT_LOAD);
113 else if (GetProcAddress(handle, funcToCheckOpenCL1_1) == NULL)
115 fprintf(stderr, ERROR_MSG_INVALID_VERSION);
123 return (void*)GetProcAddress(handle, name);
125 #define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name)
132 static void* GetProcAddress(const char* name)
134 static bool initialized = false;
135 static void* handle = NULL;
141 const char* path = "libOpenCL.so";
142 const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
145 handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
148 fprintf(stderr, ERROR_MSG_CANT_LOAD);
150 else if (dlsym(handle, funcToCheckOpenCL1_1) == NULL)
152 fprintf(stderr, ERROR_MSG_INVALID_VERSION);
159 return dlsym(handle, name);
161 #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name)
164 #ifndef CV_CL_GET_PROC_ADDRESS
165 #define CV_CL_GET_PROC_ADDRESS(name) NULL
168 static void* opencl_check_fn(int ID);
170 #include "runtime_common.hpp"
173 // BEGIN OF CUSTOM FUNCTIONS
176 #define CUSTOM_FUNCTION_ID 1000
178 #undef ADDITIONAL_FN_DEFINITIONS
181 // END OF CUSTOM FUNCTIONS HERE
184 #include "autogenerated/opencl_core_impl.hpp"
186 static void* opencl_check_fn(int ID)
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);
194 CV_Error(cv::Error::OpenCLApiCallError, cv::format("OpenCL function is not available: [%s]", e->fnName));