Skip to content

Commit 3dee87b

Browse files
committed
update CPU detection on ANDROID patch
1 parent bb3a416 commit 3dee87b

File tree

7 files changed

+44
-20
lines changed

7 files changed

+44
-20
lines changed

3rdparty/cpufeatures/CMakeLists.txt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
if(NOT ANDROID)
2+
message("cpufeatures is ANDROID project")
3+
endif()
14

2-
ocv_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
3-
file(GLOB cpuf_s *.c)
4-
file(GLOB cpuf_h *.h)
5+
set(CPUFEATURES_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH "Android cpufeatures project sources (for example, <android-ndk>/sources/android/cpufeatures)")
56

6-
set(lib_srcs ${cpuf_s})
7-
set(lib_hdrs ${cpuf_h})
7+
set(CPUFEATURES_INCLUDE_DIRS ${CPUFEATURES_ROOT} CACHE INTERNAL "")
8+
set(CPUFEATURES_LIBRARIES cpufeatures CACHE INTERNAL "")
89

9-
set(CPUFEATURES_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
10-
set(CPUFEATURES_LIBRARIES cpufeatures CACHE INTERNAL "")
10+
if(NOT DEFINED CPUFEATURES_SOURCES)
11+
set(CPUFEATURES_SOURCES ${CPUFEATURES_ROOT}/cpu-features.c ${CPUFEATURES_ROOT}/cpu-features.h)
12+
endif()
1113

12-
add_library(cpufeatures STATIC ${lib_srcs} ${lib_hdrs})
14+
include_directories(${CPUFEATURES_INCLUDE_DIRS})
15+
add_library(cpufeatures STATIC ${CPUFEATURES_SOURCES})
1316

1417
set_target_properties(cpufeatures
1518
PROPERTIES OUTPUT_NAME cpufeatures
1619
DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1720
COMPILE_PDB_NAME cpufeatures
1821
COMPILE_PDB_NAME_DEBUG "cpufeatures${OPENCV_DEBUG_POSTFIX}"
1922
ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
20-
)
21-
23+
)
24+
2225
if(ENABLE_SOLUTION_FOLDERS)
2326
set_target_properties(cpufeatures PROPERTIES FOLDER "3rdparty")
2427
endif()
2528

2629
if(NOT BUILD_SHARED_LIBS)
2730
ocv_install_target(cpufeatures EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev)
2831
endif()
29-

3rdparty/cpufeatures/LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (C) 2016 The Android Open Source Project
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

3rdparty/cpufeatures/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The Android NDK provides a small library named cpufeatures that your app can use at runtime to detect the target device's CPU family and the optional features it supports.
2+
It is designed to work as-is on all official Android platform versions.
3+
4+
https://developer.android.com/ndk/guides/cpu-features.html

3rdparty/libwebp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
project(${WEBP_LIBRARY})
66

77
ocv_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
8-
ocv_include_directories("${CPUFEATURES_INCLUDE_DIR}")
8+
ocv_include_directories(${CPUFEATURES_INCLUDE_DIRS})
99

1010
file(GLOB lib_srcs dec/*.c demux/*.c dsp/*.c enc/*.c mux/*.c utils/*.c webp/*.c)
1111
file(GLOB lib_hdrs dec/*.h demux/*.h dsp/*.h enc/*.h mux/*.h utils/*.h webp/*.h)

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ if(POLICY CMP0042)
9898
cmake_policy(SET CMP0042 NEW)
9999
endif()
100100

101+
if(POLICY CMP0046)
102+
cmake_policy(SET CMP0046 OLD)
103+
endif()
104+
101105
if(POLICY CMP0051)
102106
cmake_policy(SET CMP0051 NEW)
103107
endif()

modules/core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ source_group("Src" FILES "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string
3232
ocv_glob_module_sources(SOURCES "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc"
3333
HEADERS ${lib_cuda_hdrs} ${lib_cuda_hdrs_detail})
3434

35-
ocv_module_include_directories(${the_module} ${ZLIB_INCLUDE_DIRS} ${OPENCL_INCLUDE_DIRS} ${CPUFEATURES_INCLUDE_DIR})
35+
ocv_module_include_directories(${the_module} ${ZLIB_INCLUDE_DIRS} ${OPENCL_INCLUDE_DIRS} ${CPUFEATURES_INCLUDE_DIRS})
3636
ocv_create_module(${extra_libs})
3737

38-
ocv_target_link_libraries(${the_module} ${ZLIB_LIBRARIES} "${OPENCL_LIBRARIES}" "${VA_LIBRARIES}" "${LAPACK_LIBRARIES}" "${CPUFEATURES_LIBRARIES}")
38+
ocv_target_link_libraries(${the_module} ${ZLIB_LIBRARIES} "${OPENCL_LIBRARIES}" "${VA_LIBRARIES}" "${LAPACK_LIBRARIES}" "${CPUFEATURES_LIBRARIES}")
3939

4040
ocv_add_accuracy_tests()
4141
ocv_add_perf_tests()

modules/core/src/system.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,16 @@ struct HWFeatures
447447
CV_UNUSED(cpuid_data_ex);
448448
#endif // OPENCV_HAVE_X86_CPUID
449449

450-
#if defined ANDROID || defined __linux__
450+
#if defined __ANDROID__ || defined __linux__
451451
#ifdef __aarch64__
452452
have[CV_CPU_NEON] = true;
453453
have[CV_CPU_FP16] = true;
454+
#elif defined __arm__ && defined __ANDROID__
455+
__android_log_print(ANDROID_LOG_INFO, "OpenCV", "calling android_getCpuFeatures() ...");
456+
uint64_t features = android_getCpuFeatures();
457+
__android_log_print(ANDROID_LOG_INFO, "OpenCV", "calling android_getCpuFeatures() ... Done (%llx)", features);
458+
have[CV_CPU_NEON] = (features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
459+
have[CV_CPU_FP16] = (features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) != 0;
454460
#elif defined __arm__
455461
int cpufile = open("/proc/self/auxv", O_RDONLY);
456462

@@ -471,11 +477,6 @@ struct HWFeatures
471477

472478
close(cpufile);
473479
}
474-
#ifdef ANDROID
475-
uint64_t features = android_getCpuFeatures();
476-
have[CV_CPU_NEON] = (features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
477-
have[CV_CPU_FP16] = (features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) != 0;
478-
#endif
479480
#endif
480481
#elif (defined __clang__ || defined __APPLE__)
481482
#if (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__))

0 commit comments

Comments
 (0)