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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2015, Itseez Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
44 #include "precomp.hpp"
47 #include <opencv2/core/utils/configuration.private.hpp>
48 #include <opencv2/core/utils/trace.private.hpp>
50 #include <opencv2/core/utils/logger.hpp>
54 static Mutex* __initialization_mutex = NULL;
55 Mutex& getInitializationMutex()
57 if (__initialization_mutex == NULL)
58 __initialization_mutex = new Mutex();
59 return *__initialization_mutex;
61 // force initialization (single-threaded environment)
62 Mutex* __initialization_mutex_initializer = &getInitializationMutex();
68 # pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
72 #if defined __ANDROID__ || defined __linux__ || defined __FreeBSD__ || defined __HAIKU__
76 #if defined __ANDROID__ || defined __linux__
77 # include <linux/auxvec.h>
81 #if defined __ANDROID__ && defined HAVE_CPUFEATURES
82 # include <cpu-features.h>
86 # if defined __PPC64__ && defined __linux__
87 # include "sys/auxv.h"
91 # ifndef PPC_FEATURE2_ARCH_2_07
92 # define PPC_FEATURE2_ARCH_2_07 0x80000000
97 #if defined _WIN32 || defined WINCE
98 #ifndef _WIN32_WINNT // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?)
99 #define _WIN32_WINNT 0x0400 // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx
102 #if (_WIN32_WINNT >= 0x0602)
103 #include <synchapi.h>
112 #include <wrl/client.h>
113 #ifndef __cplusplus_winrt
114 #include <windows.storage.h>
115 #pragma comment(lib, "runtimeobject.lib")
118 std::wstring GetTempPathWinRT()
120 #ifdef __cplusplus_winrt
121 return std::wstring(Windows::Storage::ApplicationData::Current->TemporaryFolder->Path->Data());
123 Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationDataStatics> appdataFactory;
124 Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationData> appdataRef;
125 Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFolder> storagefolderRef;
126 Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageItem> storageitemRef;
128 HSTRING_HEADER hstrHead;
130 if (FAILED(WindowsCreateStringReference(RuntimeClass_Windows_Storage_ApplicationData,
131 (UINT32)wcslen(RuntimeClass_Windows_Storage_ApplicationData), &hstrHead, &str)))
133 if (FAILED(RoGetActivationFactory(str, IID_PPV_ARGS(appdataFactory.ReleaseAndGetAddressOf()))))
135 if (FAILED(appdataFactory->get_Current(appdataRef.ReleaseAndGetAddressOf())))
137 if (FAILED(appdataRef->get_TemporaryFolder(storagefolderRef.ReleaseAndGetAddressOf())))
139 if (FAILED(storagefolderRef.As(&storageitemRef)))
142 if (FAILED(storageitemRef->get_Path(&str)))
144 wstr = WindowsGetStringRawBuffer(str, NULL);
145 WindowsDeleteString(str);
150 std::wstring GetTempFileNameWinRT(std::wstring prefix)
155 wchar_t* mask = L"%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x";
156 swprintf(&guidStr[0], sizeof(guidStr)/sizeof(wchar_t), mask,
157 g.Data1, g.Data2, g.Data3, UINT(g.Data4[0]), UINT(g.Data4[1]),
158 UINT(g.Data4[2]), UINT(g.Data4[3]), UINT(g.Data4[4]),
159 UINT(g.Data4[5]), UINT(g.Data4[6]), UINT(g.Data4[7]));
161 return prefix.append(std::wstring(guidStr));
167 #include <sys/time.h>
170 #if defined __MACH__ && defined __APPLE__
171 #include <mach/mach.h>
172 #include <mach/mach_time.h>
181 #if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__ || defined __GLIBC__ || defined __HAIKU__
184 #include <sys/types.h>
185 #if defined __ANDROID__
186 #include <sys/sysconf.h>
191 # include <android/log.h>
194 #ifdef DECLARE_CV_CPUID_X86
198 #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
199 #if _MSC_VER >= 1400 // MSVS 2005
200 #include <intrin.h> // __cpuidex()
201 #define CV_CPUID_X86 __cpuidex
203 #error "Required MSVS 2005+"
205 #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
206 static void cv_cpuid(int* cpuid_data, int reg_eax, int reg_ecx)
208 int __eax = reg_eax, __ebx = 0, __ecx = reg_ecx, __edx = 0;
209 // tested with available compilers (-fPIC -O2 -m32/-m64): https://godbolt.org/
210 #if !defined(__PIC__) \
211 || defined(__x86_64__) || __GNUC__ >= 5 \
212 || defined(__clang__) || defined(__INTEL_COMPILER)
214 : "+a" (__eax), "=b" (__ebx), "+c" (__ecx), "=d" (__edx)
216 #elif defined(__i386__) // ebx may be reserved as the PIC register
217 __asm__("xchg{l}\t{%%}ebx, %1\n\t"
219 "xchg{l}\t{%%}ebx, %1\n\t"
220 : "+a" (__eax), "=&r" (__ebx), "+c" (__ecx), "=d" (__edx)
223 #error "Configuration error"
225 cpuid_data[0] = __eax; cpuid_data[1] = __ebx; cpuid_data[2] = __ecx; cpuid_data[3] = __edx;
227 #define CV_CPUID_X86 cv_cpuid
235 Exception::Exception() { code = 0; line = 0; }
237 Exception::Exception(int _code, const String& _err, const String& _func, const String& _file, int _line)
238 : code(_code), err(_err), func(_func), file(_file), line(_line)
243 Exception::~Exception() throw() {}
246 \return the error description and the context as a text string.
248 const char* Exception::what() const throw() { return msg.c_str(); }
250 void Exception::formatMessage()
252 size_t pos = err.find('\n');
253 bool multiline = pos != cv::String::npos;
256 std::stringstream ss;
258 while (pos != cv::String::npos)
260 ss << "> " << err.substr(prev_pos, pos - prev_pos) << std::endl;
262 pos = err.find('\n', prev_pos);
264 ss << "> " << err.substr(prev_pos);
265 if (err[err.size() - 1] != '\n')
272 msg = format("OpenCV(%s) %s:%d: error: (%d:%s) in function '%s'\n%s", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), func.c_str(), err.c_str());
274 msg = format("OpenCV(%s) %s:%d: error: (%d:%s) %s in function '%s'\n", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str(), func.c_str());
278 msg = format("OpenCV(%s) %s:%d: error: (%d:%s) %s%s", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str(), multiline ? "" : "\n");
282 static const char* g_hwFeatureNames[CV_HARDWARE_MAX_FEATURE] = { NULL };
284 static const char* getHWFeatureName(int id)
286 return (id < CV_HARDWARE_MAX_FEATURE) ? g_hwFeatureNames[id] : NULL;
288 static const char* getHWFeatureNameSafe(int id)
290 const char* name = getHWFeatureName(id);
291 return name ? name : "Unknown feature";
296 enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE };
298 HWFeatures(bool run_initialize = false)
300 memset( have, 0, sizeof(have[0]) * MAX_FEATURE );
305 static void initializeNames()
307 for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
309 g_hwFeatureNames[i] = 0;
311 g_hwFeatureNames[CPU_MMX] = "MMX";
312 g_hwFeatureNames[CPU_SSE] = "SSE";
313 g_hwFeatureNames[CPU_SSE2] = "SSE2";
314 g_hwFeatureNames[CPU_SSE3] = "SSE3";
315 g_hwFeatureNames[CPU_SSSE3] = "SSSE3";
316 g_hwFeatureNames[CPU_SSE4_1] = "SSE4.1";
317 g_hwFeatureNames[CPU_SSE4_2] = "SSE4.2";
318 g_hwFeatureNames[CPU_POPCNT] = "POPCNT";
319 g_hwFeatureNames[CPU_FP16] = "FP16";
320 g_hwFeatureNames[CPU_AVX] = "AVX";
321 g_hwFeatureNames[CPU_AVX2] = "AVX2";
322 g_hwFeatureNames[CPU_FMA3] = "FMA3";
324 g_hwFeatureNames[CPU_AVX_512F] = "AVX512F";
325 g_hwFeatureNames[CPU_AVX_512BW] = "AVX512BW";
326 g_hwFeatureNames[CPU_AVX_512CD] = "AVX512CD";
327 g_hwFeatureNames[CPU_AVX_512DQ] = "AVX512DQ";
328 g_hwFeatureNames[CPU_AVX_512ER] = "AVX512ER";
329 g_hwFeatureNames[CPU_AVX_512IFMA] = "AVX512IFMA";
330 g_hwFeatureNames[CPU_AVX_512PF] = "AVX512PF";
331 g_hwFeatureNames[CPU_AVX_512VBMI] = "AVX512VBMI";
332 g_hwFeatureNames[CPU_AVX_512VL] = "AVX512VL";
334 g_hwFeatureNames[CPU_NEON] = "NEON";
336 g_hwFeatureNames[CPU_VSX] = "VSX";
338 g_hwFeatureNames[CPU_AVX512_SKX] = "AVX512-SKX";
341 void initialize(void)
344 if (getenv("OPENCV_DUMP_CONFIG"))
346 fprintf(stderr, "\nOpenCV build configuration is:\n%s\n",
347 cv::getBuildInformation().c_str());
354 int cpuid_data[4] = { 0, 0, 0, 0 };
355 int cpuid_data_ex[4] = { 0, 0, 0, 0 };
357 CV_CPUID_X86(cpuid_data, 1, 0/*unused*/);
359 int x86_family = (cpuid_data[0] >> 8) & 15;
360 if( x86_family >= 6 )
362 have[CV_CPU_MMX] = (cpuid_data[3] & (1<<23)) != 0;
363 have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0;
364 have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0;
365 have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0;
366 have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0;
367 have[CV_CPU_FMA3] = (cpuid_data[2] & (1<<12)) != 0;
368 have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0;
369 have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0;
370 have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0;
371 have[CV_CPU_AVX] = (cpuid_data[2] & (1<<28)) != 0;
372 have[CV_CPU_FP16] = (cpuid_data[2] & (1<<29)) != 0;
374 // make the second call to the cpuid command in order to get
375 // information about extended features like AVX2
376 CV_CPUID_X86(cpuid_data_ex, 7, 0);
378 have[CV_CPU_AVX2] = (cpuid_data_ex[1] & (1<<5)) != 0;
380 have[CV_CPU_AVX_512F] = (cpuid_data_ex[1] & (1<<16)) != 0;
381 have[CV_CPU_AVX_512DQ] = (cpuid_data_ex[1] & (1<<17)) != 0;
382 have[CV_CPU_AVX_512IFMA512] = (cpuid_data_ex[1] & (1<<21)) != 0;
383 have[CV_CPU_AVX_512PF] = (cpuid_data_ex[1] & (1<<26)) != 0;
384 have[CV_CPU_AVX_512ER] = (cpuid_data_ex[1] & (1<<27)) != 0;
385 have[CV_CPU_AVX_512CD] = (cpuid_data_ex[1] & (1<<28)) != 0;
386 have[CV_CPU_AVX_512BW] = (cpuid_data_ex[1] & (1<<30)) != 0;
387 have[CV_CPU_AVX_512VL] = (cpuid_data_ex[1] & (1<<31)) != 0;
388 have[CV_CPU_AVX_512VBMI] = (cpuid_data_ex[2] & (1<<1)) != 0;
390 bool have_AVX_OS_support = true;
391 bool have_AVX512_OS_support = true;
392 if (!(cpuid_data[2] & (1<<27)))
393 have_AVX_OS_support = false; // OS uses XSAVE_XRSTORE and CPU support AVX
397 #ifdef _XCR_XFEATURE_ENABLED_MASK // requires immintrin.h
398 xcr0 = (int)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
399 #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
400 __asm__ ("xgetbv\n\t" : "=a" (xcr0) : "c" (0) : "%edx" );
402 if ((xcr0 & 0x6) != 0x6)
403 have_AVX_OS_support = false; // YMM registers
404 if ((xcr0 & 0xe6) != 0xe6)
405 have_AVX512_OS_support = false; // ZMM registers
408 if (!have_AVX_OS_support)
410 have[CV_CPU_AVX] = false;
411 have[CV_CPU_FP16] = false;
412 have[CV_CPU_AVX2] = false;
413 have[CV_CPU_FMA3] = false;
415 if (!have_AVX_OS_support || !have_AVX512_OS_support)
417 have[CV_CPU_AVX_512F] = false;
418 have[CV_CPU_AVX_512BW] = false;
419 have[CV_CPU_AVX_512CD] = false;
420 have[CV_CPU_AVX_512DQ] = false;
421 have[CV_CPU_AVX_512ER] = false;
422 have[CV_CPU_AVX_512IFMA512] = false;
423 have[CV_CPU_AVX_512PF] = false;
424 have[CV_CPU_AVX_512VBMI] = false;
425 have[CV_CPU_AVX_512VL] = false;
428 if (have[CV_CPU_AVX_512F])
430 have[CV_CPU_AVX512_SKX] = have[CV_CPU_AVX_512F] & have[CV_CPU_AVX_512CD] & have[CV_CPU_AVX_512BW] & have[CV_CPU_AVX_512DQ] & have[CV_CPU_AVX_512VL];
433 #endif // CV_CPUID_X86
435 #if defined __ANDROID__ || defined __linux__
437 have[CV_CPU_NEON] = true;
438 have[CV_CPU_FP16] = true;
439 #elif defined __arm__ && defined __ANDROID__
440 #if defined HAVE_CPUFEATURES
441 CV_LOG_INFO(NULL, "calling android_getCpuFeatures() ...");
442 uint64_t features = android_getCpuFeatures();
443 CV_LOG_INFO(NULL, cv::format("calling android_getCpuFeatures() ... Done (%llx)", (long long)features));
444 have[CV_CPU_NEON] = (features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
445 have[CV_CPU_FP16] = (features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) != 0;
447 CV_LOG_INFO(NULL, "cpufeatures library is not available for CPU detection");
449 CV_LOG_INFO(NULL, "- NEON instructions is enabled via build flags");
450 have[CV_CPU_NEON] = true;
452 CV_LOG_INFO(NULL, "- NEON instructions is NOT enabled via build flags");
455 CV_LOG_INFO(NULL, "- FP16 instructions is enabled via build flags");
456 have[CV_CPU_FP16] = true;
458 CV_LOG_INFO(NULL, "- FP16 instructions is NOT enabled via build flags");
461 #elif defined __arm__
462 int cpufile = open("/proc/self/auxv", O_RDONLY);
467 const size_t size_auxv_t = sizeof(auxv);
469 while ((size_t)read(cpufile, &auxv, size_auxv_t) == size_auxv_t)
471 if (auxv.a_type == AT_HWCAP)
473 have[CV_CPU_NEON] = (auxv.a_un.a_val & 4096) != 0;
474 have[CV_CPU_FP16] = (auxv.a_un.a_val & 2) != 0;
482 #elif (defined __clang__ || defined __APPLE__)
483 #if (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__))
484 have[CV_CPU_NEON] = true;
486 #if (defined __ARM_FP && (((__ARM_FP & 0x2) != 0) && defined __ARM_NEON__))
487 have[CV_CPU_FP16] = true;
492 have[CV_CPU_VSX] = true;
493 #elif (defined __PPC64__ && defined __linux__)
494 uint64 hwcaps = getauxval(AT_HWCAP);
495 uint64 hwcap2 = getauxval(AT_HWCAP2);
496 have[CV_CPU_VSX] = (hwcaps & PPC_FEATURE_PPC_LE && hwcaps & PPC_FEATURE_HAS_VSX && hwcap2 & PPC_FEATURE2_ARCH_2_07);
498 have[CV_CPU_VSX] = false;
501 int baseline_features[] = { CV_CPU_BASELINE_FEATURES };
502 if (!checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0])))
505 "******************************************************************\n"
507 "* This OpenCV build doesn't support current CPU/HW configuration *\n"
509 "* Use OPENCV_DUMP_CONFIG=1 environment variable for details *\n"
510 "******************************************************************\n");
511 fprintf(stderr, "\nRequired baseline features:\n");
512 checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]), true);
513 CV_ErrorNoReturn(cv::Error::StsAssert, "Missing support for required CPU baseline features. Check OpenCV build configuration and required CPU/HW setup.");
516 readSettings(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]));
519 bool checkFeatures(const int* features, int count, bool dump = false)
522 for (int i = 0; i < count; i++)
524 int feature = features[i];
529 if (dump) fprintf(stderr, "%s - OK\n", getHWFeatureNameSafe(feature));
534 if (dump) fprintf(stderr, "%s - NOT AVAILABLE\n", getHWFeatureNameSafe(feature));
541 static inline bool isSymbolSeparator(char c)
543 return c == ',' || c == ';';
546 void readSettings(const int* baseline_features, int baseline_count)
549 const char* disabled_features =
551 getenv("OPENCV_CPU_DISABLE");
555 if (disabled_features && disabled_features[0] != 0)
557 const char* start = disabled_features;
560 while (start[0] != 0 && isSymbolSeparator(start[0]))
566 const char* end = start;
567 while (end[0] != 0 && !isSymbolSeparator(end[0]))
573 cv::String feature(start, end);
576 CV_Assert(feature.size() > 0);
579 for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
581 if (!g_hwFeatureNames[i]) continue;
582 size_t len = strlen(g_hwFeatureNames[i]);
583 if (len != feature.size()) continue;
584 if (feature.compare(g_hwFeatureNames[i]) == 0)
586 bool isBaseline = false;
587 for (int k = 0; k < baseline_count; k++)
589 if (baseline_features[k] == i)
597 if (dump) fprintf(stderr, "OPENCV: Trying to disable baseline CPU feature: '%s'. This has very limited effect, because code optimizations for this feature are executed unconditionally in the most cases.\n", getHWFeatureNameSafe(i));
601 if (dump) fprintf(stderr, "OPENCV: Trying to disable unavailable CPU feature on the current platform: '%s'.\n", getHWFeatureNameSafe(i));
611 if (dump) fprintf(stderr, "OPENCV: Trying to disable unknown CPU feature: '%s'.\n", feature.c_str());
617 bool have[MAX_FEATURE+1];
620 static HWFeatures featuresEnabled(true), featuresDisabled = HWFeatures(false);
621 static HWFeatures* currentFeatures = &featuresEnabled;
623 bool checkHardwareSupport(int feature)
625 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
626 return currentFeatures->have[feature];
629 String getHardwareFeatureName(int feature)
631 const char* name = getHWFeatureName(feature);
632 return name ? String(name) : String();
635 volatile bool useOptimizedFlag = true;
637 void setUseOptimized( bool flag )
639 useOptimizedFlag = flag;
640 currentFeatures = flag ? &featuresEnabled : &featuresDisabled;
642 ipp::setUseIPP(flag);
644 ocl::setUseOpenCL(flag);
646 #ifdef HAVE_TEGRA_OPTIMIZATION
647 ::tegra::setUseTegra(flag);
651 bool useOptimized(void)
653 return useOptimizedFlag;
656 int64 getTickCount(void)
658 #if defined _WIN32 || defined WINCE
659 LARGE_INTEGER counter;
660 QueryPerformanceCounter( &counter );
661 return (int64)counter.QuadPart;
662 #elif defined __linux || defined __linux__
664 clock_gettime(CLOCK_MONOTONIC, &tp);
665 return (int64)tp.tv_sec*1000000000 + tp.tv_nsec;
666 #elif defined __MACH__ && defined __APPLE__
667 return (int64)mach_absolute_time();
671 gettimeofday( &tv, &tz );
672 return (int64)tv.tv_sec*1000000 + tv.tv_usec;
676 double getTickFrequency(void)
678 #if defined _WIN32 || defined WINCE
680 QueryPerformanceFrequency(&freq);
681 return (double)freq.QuadPart;
682 #elif defined __linux || defined __linux__
684 #elif defined __MACH__ && defined __APPLE__
685 static double freq = 0;
688 mach_timebase_info_data_t sTimebaseInfo;
689 mach_timebase_info(&sTimebaseInfo);
690 freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer;
698 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__ || defined __ppc__)
699 #if defined(__i386__)
701 int64 getCPUTickCount(void)
704 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
707 #elif defined(__x86_64__)
709 int64 getCPUTickCount(void)
712 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
713 return (int64)lo | ((int64)hi << 32);
716 #elif defined(__ppc__)
718 int64 getCPUTickCount(void)
721 unsigned upper, lower, tmp;
729 : "=r"(upper),"=r"(lower),"=r"(tmp)
731 return lower | ((int64)upper << 32);
736 #error "RDTSC not defined"
740 #elif defined _MSC_VER && defined _WIN32 && defined _M_IX86
742 int64 getCPUTickCount(void)
751 //int64 getCPUTickCount(void)
753 // return ippGetCpuClocks();
756 int64 getCPUTickCount(void)
758 return getTickCount();
764 const String& getBuildInformation()
766 static String build_info =
767 #include "version_string.inc"
772 String getVersionString() { return String(CV_VERSION); }
774 int getVersionMajor() { return CV_VERSION_MAJOR; }
776 int getVersionMinor() { return CV_VERSION_MINOR; }
778 int getVersionRevision() { return CV_VERSION_REVISION; }
780 String format( const char* fmt, ... )
782 AutoBuffer<char, 1024> buf;
788 int bsize = static_cast<int>(buf.size());
789 int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
792 CV_Assert(len >= 0 && "Check format string for errors");
799 return String((char *)buf, len);
803 String tempfile( const char* suffix )
807 const char *temp_dir = getenv("OPENCV_TEMP_PATH");
812 RoInitialize(RO_INIT_MULTITHREADED);
813 std::wstring temp_dir = GetTempPathWinRT();
815 std::wstring temp_file = GetTempFileNameWinRT(L"ocv");
816 if (temp_file.empty())
819 temp_file = temp_dir.append(std::wstring(L"\\")).append(temp_file);
820 DeleteFileW(temp_file.c_str());
822 char aname[MAX_PATH];
823 size_t copied = wcstombs(aname, temp_file.c_str(), MAX_PATH);
824 CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
825 fname = String(aname);
828 char temp_dir2[MAX_PATH] = { 0 };
829 char temp_file[MAX_PATH] = { 0 };
831 if (temp_dir == 0 || temp_dir[0] == 0)
833 ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
834 temp_dir = temp_dir2;
836 if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file))
839 DeleteFileA(temp_file);
845 //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
846 char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
848 char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
851 if (temp_dir == 0 || temp_dir[0] == 0)
852 fname = defaultTemplate;
856 char ech = fname[fname.size() - 1];
857 if(ech != '/' && ech != '\\')
859 fname = fname + "__opencv_temp.XXXXXX";
862 const int fd = mkstemp((char*)fname.c_str());
863 if (fd == -1) return String();
866 remove(fname.c_str());
871 if (suffix[0] != '.')
872 return fname + "." + suffix;
874 return fname + suffix;
879 static ErrorCallback customErrorCallback = 0;
880 static void* customErrorCallbackData = 0;
881 static bool breakOnError = false;
883 bool setBreakOnError(bool value)
885 bool prevVal = breakOnError;
886 breakOnError = value;
890 int cv_snprintf(char* buf, int len, const char* fmt, ...)
894 int res = cv_vsnprintf(buf, len, fmt, va);
899 int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
902 if (len <= 0) return len == 0 ? 1024 : -1;
903 int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);
904 // ensure null terminating on VS
905 if (res >= 0 && res < len)
912 buf[len - 1] = 0; // truncate happened
913 return res >= len ? res : (len * 2);
916 return vsnprintf(buf, len, fmt, args);
920 void error( const Exception& exc )
922 if (customErrorCallback != 0)
923 customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
924 exc.file.c_str(), exc.line, customErrorCallbackData);
927 const char* errorStr = cvErrorStr(exc.code);
930 cv_snprintf(buf, sizeof(buf),
931 "OpenCV(%s) Error: %s (%s) in %s, file %s, line %d",
933 errorStr, exc.err.c_str(), exc.func.size() > 0 ?
934 exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line);
935 fprintf( stderr, "%s\n", buf );
938 __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
944 static volatile int* p = 0;
951 void error(int _code, const String& _err, const char* _func, const char* _file, int _line)
953 error(cv::Exception(_code, _err, _func, _file, _line));
958 redirectError( ErrorCallback errCallback, void* userdata, void** prevUserdata)
961 *prevUserdata = customErrorCallbackData;
963 ErrorCallback prevCallback = customErrorCallback;
965 customErrorCallback = errCallback;
966 customErrorCallbackData = userdata;
973 CV_IMPL int cvCheckHardwareSupport(int feature)
975 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
976 return cv::currentFeatures->have[feature];
979 CV_IMPL int cvUseOptimized( int flag )
981 int prevMode = cv::useOptimizedFlag;
982 cv::setUseOptimized( flag != 0 );
986 CV_IMPL int64 cvGetTickCount(void)
988 return cv::getTickCount();
991 CV_IMPL double cvGetTickFrequency(void)
993 return cv::getTickFrequency()*1e-6;
996 CV_IMPL CvErrorCallback
997 cvRedirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
999 return cv::redirectError(errCallback, userdata, prevUserdata);
1002 CV_IMPL int cvNulDevReport( int, const char*, const char*,
1003 const char*, int, void* )
1008 CV_IMPL int cvStdErrReport( int, const char*, const char*,
1009 const char*, int, void* )
1014 CV_IMPL int cvGuiBoxReport( int, const char*, const char*,
1015 const char*, int, void* )
1020 CV_IMPL int cvGetErrInfo( const char**, const char**, const char**, int* )
1026 CV_IMPL const char* cvErrorStr( int status )
1028 static char buf[256];
1032 case CV_StsOk : return "No Error";
1033 case CV_StsBackTrace : return "Backtrace";
1034 case CV_StsError : return "Unspecified error";
1035 case CV_StsInternal : return "Internal error";
1036 case CV_StsNoMem : return "Insufficient memory";
1037 case CV_StsBadArg : return "Bad argument";
1038 case CV_StsNoConv : return "Iterations do not converge";
1039 case CV_StsAutoTrace : return "Autotrace call";
1040 case CV_StsBadSize : return "Incorrect size of input array";
1041 case CV_StsNullPtr : return "Null pointer";
1042 case CV_StsDivByZero : return "Division by zero occurred";
1043 case CV_BadStep : return "Image step is wrong";
1044 case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
1045 case CV_StsObjectNotFound : return "Requested object was not found";
1046 case CV_BadDepth : return "Input image depth is not supported by function";
1047 case CV_StsUnmatchedFormats : return "Formats of input arguments do not match";
1048 case CV_StsUnmatchedSizes : return "Sizes of input arguments do not match";
1049 case CV_StsOutOfRange : return "One of arguments\' values is out of range";
1050 case CV_StsUnsupportedFormat : return "Unsupported format or combination of formats";
1051 case CV_BadCOI : return "Input COI is not supported";
1052 case CV_BadNumChannels : return "Bad number of channels";
1053 case CV_StsBadFlag : return "Bad flag (parameter or structure field)";
1054 case CV_StsBadPoint : return "Bad parameter of type CvPoint";
1055 case CV_StsBadMask : return "Bad type of mask argument";
1056 case CV_StsParseError : return "Parsing error";
1057 case CV_StsNotImplemented : return "The function/feature is not implemented";
1058 case CV_StsBadMemBlock : return "Memory block has been corrupted";
1059 case CV_StsAssert : return "Assertion failed";
1060 case CV_GpuNotSupported : return "No CUDA support";
1061 case CV_GpuApiCallError : return "Gpu API call";
1062 case CV_OpenGlNotSupported : return "No OpenGL support";
1063 case CV_OpenGlApiCallError : return "OpenGL API call";
1066 sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
1070 CV_IMPL int cvGetErrMode(void)
1075 CV_IMPL int cvSetErrMode(int)
1080 CV_IMPL int cvGetErrStatus(void)
1085 CV_IMPL void cvSetErrStatus(int)
1090 CV_IMPL void cvError( int code, const char* func_name,
1091 const char* err_msg,
1092 const char* file_name, int line )
1094 cv::error(cv::Exception(code, err_msg, func_name, file_name, line));
1097 /* function, which converts int to int */
1099 cvErrorFromIppStatus( int status )
1103 case CV_BADSIZE_ERR: return CV_StsBadSize;
1104 case CV_BADMEMBLOCK_ERR: return CV_StsBadMemBlock;
1105 case CV_NULLPTR_ERR: return CV_StsNullPtr;
1106 case CV_DIV_BY_ZERO_ERR: return CV_StsDivByZero;
1107 case CV_BADSTEP_ERR: return CV_BadStep;
1108 case CV_OUTOFMEM_ERR: return CV_StsNoMem;
1109 case CV_BADARG_ERR: return CV_StsBadArg;
1110 case CV_NOTDEFINED_ERR: return CV_StsError;
1111 case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
1112 case CV_NOTFOUND_ERR: return CV_StsObjectNotFound;
1113 case CV_BADCONVERGENCE_ERR: return CV_StsNoConv;
1114 case CV_BADDEPTH_ERR: return CV_BadDepth;
1115 case CV_UNMATCHED_FORMATS_ERR: return CV_StsUnmatchedFormats;
1116 case CV_UNSUPPORTED_COI_ERR: return CV_BadCOI;
1117 case CV_UNSUPPORTED_CHANNELS_ERR: return CV_BadNumChannels;
1118 case CV_BADFLAG_ERR: return CV_StsBadFlag;
1119 case CV_BADRANGE_ERR: return CV_StsBadArg;
1120 case CV_BADCOEF_ERR: return CV_StsBadArg;
1121 case CV_BADFACTOR_ERR: return CV_StsBadArg;
1122 case CV_BADPOINT_ERR: return CV_StsBadPoint;
1130 bool __termination = false;
1136 #if defined _WIN32 || defined WINCE
1142 #if (_WIN32_WINNT >= 0x0600)
1143 ::InitializeCriticalSectionEx(&cs, 1000, 0);
1145 ::InitializeCriticalSection(&cs);
1149 ~Impl() { DeleteCriticalSection(&cs); }
1151 void lock() { EnterCriticalSection(&cs); }
1152 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
1153 void unlock() { LeaveCriticalSection(&cs); }
1155 CRITICAL_SECTION cs;
1165 pthread_mutexattr_t attr;
1166 pthread_mutexattr_init(&attr);
1167 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
1168 pthread_mutex_init(&mt, &attr);
1169 pthread_mutexattr_destroy(&attr);
1173 ~Impl() { pthread_mutex_destroy(&mt); }
1175 void lock() { pthread_mutex_lock(&mt); }
1176 bool trylock() { return pthread_mutex_trylock(&mt) == 0; }
1177 void unlock() { pthread_mutex_unlock(&mt); }
1187 impl = new Mutex::Impl;
1192 if( CV_XADD(&impl->refcount, -1) == 1 )
1197 Mutex::Mutex(const Mutex& m)
1200 CV_XADD(&impl->refcount, 1);
1203 Mutex& Mutex::operator = (const Mutex& m)
1207 CV_XADD(&m.impl->refcount, 1);
1208 if( CV_XADD(&impl->refcount, -1) == 1 )
1215 void Mutex::lock() { impl->lock(); }
1216 void Mutex::unlock() { impl->unlock(); }
1217 bool Mutex::trylock() { return impl->trylock(); }
1220 //////////////////////////////// thread-local storage ////////////////////////////////
1224 #pragma warning(disable:4505) // unreferenced local function has been removed
1226 #ifndef TLS_OUT_OF_INDEXES
1227 #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
1231 // TLS platform abstraction layer
1232 class TlsAbstraction
1237 void* GetData() const;
1238 void SetData(void *pData);
1246 pthread_key_t tlsKey;
1252 static __declspec( thread ) void* tlsData = NULL; // using C++11 thread attribute for local thread data
1253 TlsAbstraction::TlsAbstraction() {}
1254 TlsAbstraction::~TlsAbstraction() {}
1255 void* TlsAbstraction::GetData() const
1259 void TlsAbstraction::SetData(void *pData)
1264 TlsAbstraction::TlsAbstraction()
1266 tlsKey = TlsAlloc();
1267 CV_Assert(tlsKey != TLS_OUT_OF_INDEXES);
1269 TlsAbstraction::~TlsAbstraction()
1273 void* TlsAbstraction::GetData() const
1275 return TlsGetValue(tlsKey);
1277 void TlsAbstraction::SetData(void *pData)
1279 CV_Assert(TlsSetValue(tlsKey, pData) == TRUE);
1283 TlsAbstraction::TlsAbstraction()
1285 CV_Assert(pthread_key_create(&tlsKey, NULL) == 0);
1287 TlsAbstraction::~TlsAbstraction()
1289 CV_Assert(pthread_key_delete(tlsKey) == 0);
1291 void* TlsAbstraction::GetData() const
1293 return pthread_getspecific(tlsKey);
1295 void TlsAbstraction::SetData(void *pData)
1297 CV_Assert(pthread_setspecific(tlsKey, pData) == 0);
1301 // Per-thread data structure
1310 std::vector<void*> slots; // Data array for a thread
1311 size_t idx; // Thread index in TLS storage. This is not OS thread ID!
1314 // Main TLS storage class
1321 tlsSlots.reserve(32);
1322 threads.reserve(32);
1326 for(size_t i = 0; i < threads.size(); i++)
1330 /* Current architecture doesn't allow proper global objects release, so this check can cause crashes
1332 // Check if all slots were properly cleared
1333 for(size_t j = 0; j < threads[i]->slots.size(); j++)
1335 CV_Assert(threads[i]->slots[j] == 0);
1344 void releaseThread()
1346 AutoLock guard(mtxGlobalAccess);
1347 ThreadData *pTD = (ThreadData*)tls.GetData();
1348 for(size_t i = 0; i < threads.size(); i++)
1350 if(pTD == threads[i])
1360 // Reserve TLS storage index
1361 size_t reserveSlot()
1363 AutoLock guard(mtxGlobalAccess);
1364 CV_Assert(tlsSlotsSize == tlsSlots.size());
1366 // Find unused slots
1367 for(size_t slot = 0; slot < tlsSlotsSize; slot++)
1377 tlsSlots.push_back(1); tlsSlotsSize++;
1378 return tlsSlotsSize - 1;
1381 // Release TLS storage index and pass associated data to caller
1382 void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
1384 AutoLock guard(mtxGlobalAccess);
1385 CV_Assert(tlsSlotsSize == tlsSlots.size());
1386 CV_Assert(tlsSlotsSize > slotIdx);
1388 for(size_t i = 0; i < threads.size(); i++)
1392 std::vector<void*>& thread_slots = threads[i]->slots;
1393 if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
1395 dataVec.push_back(thread_slots[slotIdx]);
1396 thread_slots[slotIdx] = NULL;
1402 tlsSlots[slotIdx] = 0;
1405 // Get data by TLS storage index
1406 void* getData(size_t slotIdx) const
1408 #ifndef CV_THREAD_SANITIZER
1409 CV_Assert(tlsSlotsSize > slotIdx);
1412 ThreadData* threadData = (ThreadData*)tls.GetData();
1413 if(threadData && threadData->slots.size() > slotIdx)
1414 return threadData->slots[slotIdx];
1419 // Gather data from threads by TLS storage index
1420 void gather(size_t slotIdx, std::vector<void*> &dataVec)
1422 AutoLock guard(mtxGlobalAccess);
1423 CV_Assert(tlsSlotsSize == tlsSlots.size());
1424 CV_Assert(tlsSlotsSize > slotIdx);
1426 for(size_t i = 0; i < threads.size(); i++)
1430 std::vector<void*>& thread_slots = threads[i]->slots;
1431 if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
1432 dataVec.push_back(thread_slots[slotIdx]);
1437 // Set data to storage index
1438 void setData(size_t slotIdx, void* pData)
1440 #ifndef CV_THREAD_SANITIZER
1441 CV_Assert(tlsSlotsSize > slotIdx);
1444 ThreadData* threadData = (ThreadData*)tls.GetData();
1447 threadData = new ThreadData;
1448 tls.SetData((void*)threadData);
1450 AutoLock guard(mtxGlobalAccess);
1451 threadData->idx = threads.size();
1452 threads.push_back(threadData);
1456 if(slotIdx >= threadData->slots.size())
1458 AutoLock guard(mtxGlobalAccess); // keep synchronization with gather() calls
1459 threadData->slots.resize(slotIdx + 1, NULL);
1461 threadData->slots[slotIdx] = pData;
1465 TlsAbstraction tls; // TLS abstraction layer instance
1467 Mutex mtxGlobalAccess; // Shared objects operation guard
1468 size_t tlsSlotsSize; // equal to tlsSlots.size() in synchronized sections
1469 // without synchronization this counter doesn't desrease - it is used for slotIdx sanity checks
1470 std::vector<int> tlsSlots; // TLS keys state
1471 std::vector<ThreadData*> threads; // Array for all allocated data. Thread data pointers are placed here to allow data cleanup
1474 // Create global TLS storage object
1475 static TlsStorage &getTlsStorage()
1477 CV_SINGLETON_LAZY_INIT_REF(TlsStorage, new TlsStorage())
1480 TLSDataContainer::TLSDataContainer()
1482 key_ = (int)getTlsStorage().reserveSlot(); // Reserve key from TLS storage
1485 TLSDataContainer::~TLSDataContainer()
1487 CV_Assert(key_ == -1); // Key must be released in child object
1490 void TLSDataContainer::gatherData(std::vector<void*> &data) const
1492 getTlsStorage().gather(key_, data);
1495 void TLSDataContainer::release()
1497 std::vector<void*> data;
1499 getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
1501 for(size_t i = 0; i < data.size(); i++) // Delete all associated data
1502 deleteDataInstance(data[i]);
1505 void TLSDataContainer::cleanup()
1507 std::vector<void*> data;
1509 getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
1510 for(size_t i = 0; i < data.size(); i++) // Delete all associated data
1511 deleteDataInstance(data[i]);
1514 void* TLSDataContainer::getData() const
1516 CV_Assert(key_ != -1 && "Can't fetch data from terminated TLS container.");
1517 void* pData = getTlsStorage().getData(key_); // Check if data was already allocated
1520 // Create new data instance and save it to TLS storage
1521 pData = createDataInstance();
1522 getTlsStorage().setData(key_, pData);
1527 TLSData<CoreTLSData>& getCoreTlsData()
1529 CV_SINGLETON_LAZY_INIT_REF(TLSData<CoreTLSData>, new TLSData<CoreTLSData>())
1532 #if defined CVAPI_EXPORTS && defined _WIN32 && !defined WINCE
1534 #pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
1538 BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved);
1541 BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved)
1543 if (fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH)
1545 if (lpReserved != NULL) // called after ExitProcess() call
1547 cv::__termination = true;
1551 // Not allowed to free resources if lpReserved is non-null
1552 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx
1553 cv::getTlsStorage().releaseThread();
1562 static int g_threadNum = 0;
1567 id(CV_XADD(&g_threadNum, 1))
1569 #ifdef OPENCV_WITH_ITT
1570 __itt_thread_set_name(cv::format("OpenCVThread-%03d", id).c_str());
1575 static TLSData<ThreadID>& getThreadIDTLS()
1577 CV_SINGLETON_LAZY_INIT_REF(TLSData<ThreadID>, new TLSData<ThreadID>());
1581 int utils::getThreadID() { return getThreadIDTLS().get()->id; }
1583 bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
1586 const char* envValue = NULL;
1588 const char* envValue = getenv(name);
1590 if (envValue == NULL)
1592 return defaultValue;
1594 cv::String value = envValue;
1595 if (value == "1" || value == "True" || value == "true" || value == "TRUE")
1599 if (value == "0" || value == "False" || value == "false" || value == "FALSE")
1603 CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
1607 size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultValue)
1610 const char* envValue = NULL;
1612 const char* envValue = getenv(name);
1614 if (envValue == NULL)
1616 return defaultValue;
1618 cv::String value = envValue;
1620 for (; pos < value.size(); pos++)
1622 if (!isdigit(value[pos]))
1625 cv::String valueStr = value.substr(0, pos);
1626 cv::String suffixStr = value.substr(pos, value.length() - pos);
1627 int v = atoi(valueStr.c_str());
1628 if (suffixStr.length() == 0)
1630 else if (suffixStr == "MB" || suffixStr == "Mb" || suffixStr == "mb")
1631 return v * 1024 * 1024;
1632 else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb")
1634 CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
1637 cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)
1640 const char* envValue = NULL;
1642 const char* envValue = getenv(name);
1644 if (envValue == NULL)
1646 return defaultValue;
1648 cv::String value = envValue;
1653 #ifdef CV_COLLECT_IMPL_DATA
1654 ImplCollector& getImplData()
1656 CV_SINGLETON_LAZY_INIT_REF(ImplCollector, new ImplCollector())
1659 void setImpl(int flags)
1661 cv::AutoLock lock(getImplData().mutex);
1663 getImplData().implFlags = flags;
1664 getImplData().implCode.clear();
1665 getImplData().implFun.clear();
1668 void addImpl(int flag, const char* func)
1670 cv::AutoLock lock(getImplData().mutex);
1672 getImplData().implFlags |= flag;
1673 if(func) // use lazy collection if name was not specified
1675 size_t index = getImplData().implCode.size();
1676 if(!index || (getImplData().implCode[index-1] != flag || getImplData().implFun[index-1].compare(func))) // avoid duplicates
1678 getImplData().implCode.push_back(flag);
1679 getImplData().implFun.push_back(func);
1684 int getImpl(std::vector<int> &impl, std::vector<String> &funName)
1686 cv::AutoLock lock(getImplData().mutex);
1688 impl = getImplData().implCode;
1689 funName = getImplData().implFun;
1690 return getImplData().implFlags; // return actual flags for lazy collection
1693 bool useCollection()
1695 return getImplData().useCollection;
1698 void setUseCollection(bool flag)
1700 cv::AutoLock lock(getImplData().mutex);
1702 getImplData().useCollection = flag;
1708 bool useInstrumentation()
1710 #ifdef ENABLE_INSTRUMENTATION
1711 return getInstrumentStruct().useInstr;
1717 void setUseInstrumentation(bool flag)
1719 #ifdef ENABLE_INSTRUMENTATION
1720 getInstrumentStruct().useInstr = flag;
1726 InstrNode* getTrace()
1728 #ifdef ENABLE_INSTRUMENTATION
1729 return &getInstrumentStruct().rootNode;
1737 #ifdef ENABLE_INSTRUMENTATION
1738 getInstrumentStruct().rootNode.removeChilds();
1739 getInstrumentTLSStruct().pCurrentNode = &getInstrumentStruct().rootNode;
1743 void setFlags(FLAGS modeFlags)
1745 #ifdef ENABLE_INSTRUMENTATION
1746 getInstrumentStruct().flags = modeFlags;
1748 CV_UNUSED(modeFlags);
1753 #ifdef ENABLE_INSTRUMENTATION
1754 return (FLAGS)getInstrumentStruct().flags;
1760 NodeData::NodeData(const char* funName, const char* fileName, int lineNum, void* retAddress, bool alwaysExpand, cv::instr::TYPE instrType, cv::instr::IMPL implType)
1762 m_funName = funName;
1763 m_instrType = instrType;
1764 m_implType = implType;
1765 m_fileName = fileName;
1766 m_lineNum = lineNum;
1767 m_retAddress = retAddress;
1768 m_alwaysExpand = alwaysExpand;
1776 NodeData::NodeData(NodeData &ref)
1780 NodeData& NodeData::operator=(const NodeData &right)
1782 this->m_funName = right.m_funName;
1783 this->m_instrType = right.m_instrType;
1784 this->m_implType = right.m_implType;
1785 this->m_fileName = right.m_fileName;
1786 this->m_lineNum = right.m_lineNum;
1787 this->m_retAddress = right.m_retAddress;
1788 this->m_alwaysExpand = right.m_alwaysExpand;
1790 this->m_threads = right.m_threads;
1791 this->m_counter = right.m_counter;
1792 this->m_ticksTotal = right.m_ticksTotal;
1794 this->m_funError = right.m_funError;
1798 NodeData::~NodeData()
1801 bool operator==(const NodeData& left, const NodeData& right)
1803 if(left.m_lineNum == right.m_lineNum && left.m_funName == right.m_funName && left.m_fileName == right.m_fileName)
1805 if(left.m_retAddress == right.m_retAddress || !(cv::instr::getFlags()&cv::instr::FLAGS_EXPAND_SAME_NAMES || left.m_alwaysExpand))
1811 #ifdef ENABLE_INSTRUMENTATION
1812 InstrStruct& getInstrumentStruct()
1814 static InstrStruct instr;
1818 InstrTLSStruct& getInstrumentTLSStruct()
1820 return *getInstrumentStruct().tlsStruct.get();
1823 InstrNode* getCurrentNode()
1825 return getInstrumentTLSStruct().pCurrentNode;
1828 IntrumentationRegion::IntrumentationRegion(const char* funName, const char* fileName, int lineNum, void *retAddress, bool alwaysExpand, TYPE instrType, IMPL implType)
1833 InstrStruct *pStruct = &getInstrumentStruct();
1834 if(pStruct->useInstr)
1836 InstrTLSStruct *pTLS = &getInstrumentTLSStruct();
1838 // Disable in case of failure
1839 if(!pTLS->pCurrentNode)
1845 int depth = pTLS->pCurrentNode->getDepth();
1846 if(pStruct->maxDepth && pStruct->maxDepth <= depth)
1852 NodeData payload(funName, fileName, lineNum, retAddress, alwaysExpand, instrType, implType);
1853 Node<NodeData>* pChild = NULL;
1855 if(pStruct->flags&FLAGS_MAPPING)
1858 cv::AutoLock guard(pStruct->mutexCreate); // Guard from concurrent child creation
1859 pChild = pTLS->pCurrentNode->findChild(payload);
1862 pChild = new Node<NodeData>(payload);
1863 pTLS->pCurrentNode->addChild(pChild);
1868 pChild = pTLS->pCurrentNode->findChild(payload);
1875 pTLS->pCurrentNode = pChild;
1877 m_regionTicks = getTickCount();
1881 IntrumentationRegion::~IntrumentationRegion()
1883 InstrStruct *pStruct = &getInstrumentStruct();
1884 if(pStruct->useInstr)
1888 InstrTLSStruct *pTLS = &getInstrumentTLSStruct();
1890 if (pTLS->pCurrentNode->m_payload.m_implType == cv::instr::IMPL_OPENCL &&
1891 (pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_FUN ||
1892 pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_WRAPPER))
1894 cv::ocl::finish(); // TODO Support "async" OpenCL instrumentation
1897 uint64 ticks = (getTickCount() - m_regionTicks);
1899 cv::AutoLock guard(pStruct->mutexCount); // Concurrent ticks accumulation
1900 pTLS->pCurrentNode->m_payload.m_counter++;
1901 pTLS->pCurrentNode->m_payload.m_ticksTotal += ticks;
1902 pTLS->pCurrentNode->m_payload.m_tls.get()->m_ticksTotal += ticks;
1905 pTLS->pCurrentNode = pTLS->pCurrentNode->m_pParent;
1916 struct IPPInitSingleton
1932 ippStatus = ippGetCpuFeatures(&cpuFeatures, NULL);
1935 std::cerr << "ERROR: IPP cannot detect CPU features, IPP was disabled " << std::endl;
1939 ippFeatures = cpuFeatures;
1941 const char* pIppEnv = getenv("OPENCV_IPP");
1942 cv::String env = pIppEnv;
1945 #if IPP_VERSION_X100 >= 201703
1946 const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
1947 ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_MPX|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
1948 ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
1949 #elif IPP_VERSION_X100 >= 201700
1950 const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
1951 ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
1952 ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
1954 const Ipp64u minorFeatures = 0;
1957 env = env.toLowerCase();
1958 if(env.substr(0, 2) == "ne")
1961 env = env.substr(3, env.size());
1964 if(env == "disabled")
1966 std::cerr << "WARNING: IPP was disabled by OPENCV_IPP environment variable" << std::endl;
1969 else if(env == "sse42")
1970 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42;
1971 else if(env == "avx2")
1972 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2;
1973 #if IPP_VERSION_X100 >= 201700
1974 #if defined (_M_AMD64) || defined (__x86_64__)
1975 else if(env == "avx512")
1976 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2|ippCPUID_AVX512F;
1980 std::cerr << "ERROR: Improper value of OPENCV_IPP: " << env.c_str() << ". Correct values are: disabled, sse42, avx2, avx512 (Intel64 only)" << std::endl;
1982 // Trim unsupported features
1983 ippFeatures &= cpuFeatures;
1986 // Disable AVX1 since we don't track regressions for it. SSE42 will be used instead
1987 if(cpuFeatures&ippCPUID_AVX && !(cpuFeatures&ippCPUID_AVX2))
1988 ippFeatures &= ~((Ipp64u)ippCPUID_AVX);
1990 // IPP integrations in OpenCV support only SSE4.2, AVX2 and AVX-512 optimizations.
1992 #if IPP_VERSION_X100 >= 201700
1993 cpuFeatures&ippCPUID_AVX512F ||
1995 cpuFeatures&ippCPUID_AVX2 ||
1996 cpuFeatures&ippCPUID_SSE42
2003 if(ippFeatures == cpuFeatures)
2006 IPP_INITIALIZER(ippFeatures)
2007 ippFeatures = ippGetEnabledCpuFeatures();
2009 // Detect top level optimizations to make comparison easier for optimizations dependent conditions
2010 #if IPP_VERSION_X100 >= 201700
2011 if(ippFeatures&ippCPUID_AVX512F)
2013 if((ippFeatures&ippCPUID_AVX512_SKX) == ippCPUID_AVX512_SKX)
2014 ippTopFeatures = ippCPUID_AVX512_SKX;
2015 else if((ippFeatures&ippCPUID_AVX512_KNL) == ippCPUID_AVX512_KNL)
2016 ippTopFeatures = ippCPUID_AVX512_KNL;
2018 ippTopFeatures = ippCPUID_AVX512F; // Unknown AVX512 configuration
2022 if(ippFeatures&ippCPUID_AVX2)
2023 ippTopFeatures = ippCPUID_AVX2;
2024 else if(ippFeatures&ippCPUID_SSE42)
2025 ippTopFeatures = ippCPUID_SSE42;
2027 pIppLibInfo = ippiGetLibVersion();
2034 int ippStatus; // 0 - all is ok, -1 - IPP functions failed
2035 const char *funcname;
2036 const char *filename;
2040 Ipp64u ippTopFeatures;
2041 const IppLibraryVersion *pIppLibInfo;
2044 static IPPInitSingleton& getIPPSingleton()
2046 CV_SINGLETON_LAZY_INIT_REF(IPPInitSingleton, new IPPInitSingleton())
2050 #if OPENCV_ABI_COMPATIBILITY > 300
2051 unsigned long long getIppFeatures()
2053 int getIppFeatures()
2057 #if OPENCV_ABI_COMPATIBILITY > 300
2058 return getIPPSingleton().ippFeatures;
2060 return (int)getIPPSingleton().ippFeatures;
2067 unsigned long long getIppTopFeatures();
2069 unsigned long long getIppTopFeatures()
2072 return getIPPSingleton().ippTopFeatures;
2078 void setIppStatus(int status, const char * const _funcname, const char * const _filename, int _line)
2081 getIPPSingleton().ippStatus = status;
2082 getIPPSingleton().funcname = _funcname;
2083 getIPPSingleton().filename = _filename;
2084 getIPPSingleton().linen = _line;
2086 CV_UNUSED(status); CV_UNUSED(_funcname); CV_UNUSED(_filename); CV_UNUSED(_line);
2093 return getIPPSingleton().ippStatus;
2099 String getIppErrorLocation()
2102 return format("%s:%d %s", getIPPSingleton().filename ? getIPPSingleton().filename : "", getIPPSingleton().linen, getIPPSingleton().funcname ? getIPPSingleton().funcname : "");
2108 String getIppVersion()
2111 const IppLibraryVersion *pInfo = getIPPSingleton().pIppLibInfo;
2113 return format("%s %s %s", pInfo->Name, pInfo->Version, pInfo->BuildDate);
2115 return String("error");
2117 return String("disabled");
2124 CoreTLSData* data = getCoreTlsData().get();
2125 if(data->useIPP < 0)
2127 data->useIPP = getIPPSingleton().useIPP;
2129 return (data->useIPP > 0);
2135 void setUseIPP(bool flag)
2137 CoreTLSData* data = getCoreTlsData().get();
2139 data->useIPP = (getIPPSingleton().useIPP)?flag:false;
2142 data->useIPP = false;
2149 CoreTLSData* data = getCoreTlsData().get();
2150 if(data->useIPP_NE < 0)
2152 data->useIPP_NE = getIPPSingleton().useIPP_NE;
2154 return (data->useIPP_NE > 0);
2160 void setUseIPP_NE(bool flag)
2162 CoreTLSData* data = getCoreTlsData().get();
2164 data->useIPP_NE = (getIPPSingleton().useIPP_NE)?flag:false;
2167 data->useIPP_NE = false;
2175 #ifdef HAVE_TEGRA_OPTIMIZATION
2181 cv::CoreTLSData* data = cv::getCoreTlsData().get();
2183 if (data->useTegra < 0)
2185 const char* pTegraEnv = getenv("OPENCV_TEGRA");
2186 if (pTegraEnv && (cv::String(pTegraEnv) == "disabled"))
2187 data->useTegra = false;
2189 data->useTegra = true;
2192 return (data->useTegra > 0);
2195 void setUseTegra(bool flag)
2197 cv::CoreTLSData* data = cv::getCoreTlsData().get();
2198 data->useTegra = flag;
2201 } // namespace tegra