Android OpenCL support
authorGiles Payne <gilespayne@gmail.com>
Tue, 29 Sep 2020 12:55:31 +0000 (21:55 +0900)
committerGiles Payne <gilespayne@gmail.com>
Tue, 29 Sep 2020 12:55:31 +0000 (21:55 +0900)
modules/core/src/opencl/runtime/opencl_core.cpp
platforms/android/build_sdk.py

index db2385a..b6d85d8 100644 (file)
@@ -177,6 +177,55 @@ static void *GetHandle(const char *file)
     return handle;
 }
 
+#ifdef __ANDROID__
+
+static const char *defaultAndroidPaths[] = {
+    "libOpenCL.so",
+    "/system/lib64/libOpenCL.so",
+    "/system/vendor/lib64/libOpenCL.so",
+    "/system/vendor/lib64/egl/libGLES_mali.so",
+    "/system/vendor/lib64/libPVROCL.so",
+    "/data/data/org.pocl.libs/files/lib64/libpocl.so",
+    "/system/lib/libOpenCL.so",
+    "/system/vendor/lib/libOpenCL.so",
+    "/system/vendor/lib/egl/libGLES_mali.so",
+    "/system/vendor/lib/libPVROCL.so",
+    "/data/data/org.pocl.libs/files/lib/libpocl.so"
+};
+
+static void* GetProcAddress(const char* name)
+{
+    static bool initialized = false;
+    static void* handle = NULL;
+    if (!handle && !initialized)
+    {
+        cv::AutoLock lock(cv::getInitializationMutex());
+        if (!initialized)
+        {
+            bool foundOpenCL = false;
+            for (unsigned int i = 0; i < (sizeof(defaultAndroidPaths)/sizeof(char*)); i++)
+            {
+                const char* path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i];
+                if (path) {
+                    handle = GetHandle(path);
+                    if (handle) {
+                        foundOpenCL = true;
+                        break;
+                    }
+                }
+            }
+            initialized = true;
+            if (!foundOpenCL)
+                fprintf(stderr, ERROR_MSG_CANT_LOAD);
+        }
+    }
+    if (!handle)
+        return NULL;
+    return dlsym(handle, name);
+}
+
+#else // NOT __ANDROID__
+
 static void* GetProcAddress(const char* name)
 {
     static bool initialized = false;
@@ -206,6 +255,8 @@ static void* GetProcAddress(const char* name)
         return NULL;
     return dlsym(handle, name);
 }
+#endif // __ANDROID__
+
 #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name)
 #endif
 
index 51383f8..7d898e4 100755 (executable)
@@ -158,6 +158,7 @@ class Builder:
         self.debug = True if config.debug else False
         self.debug_info = True if config.debug_info else False
         self.no_samples_build = True if config.no_samples_build else False
+        self.opencl = True if config.opencl else False
 
     def get_cmake(self):
         if not self.config.use_android_buildtools and check_executable(['cmake', '--version']):
@@ -236,6 +237,9 @@ class Builder:
         if self.debug_info:  # Release with debug info
             cmake_vars['BUILD_WITH_DEBUG_INFO'] = "ON"
 
+        if self.opencl:
+            cmake_vars['WITH_OPENCL'] = "ON"
+
         if self.config.modules_list is not None:
             cmd.append("-DBUILD_LIST='%s'" % self.config.modules_list)
 
@@ -354,6 +358,7 @@ if __name__ == "__main__":
     parser.add_argument('--debug', action="store_true", help="Build 'Debug' binaries (CMAKE_BUILD_TYPE=Debug)")
     parser.add_argument('--debug_info', action="store_true", help="Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON)")
     parser.add_argument('--no_samples_build', action="store_true", help="Do not build samples (speeds up build)")
+    parser.add_argument('--opencl', action="store_true", help="Enable OpenCL support")
     args = parser.parse_args()
 
     log.basicConfig(format='%(message)s', level=log.DEBUG)