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 if( func.size() > 0 )
253 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());
255 msg = format("OpenCV(%s) %s:%d: error: (%d) %s: %s\n", CV_VERSION, file.c_str(), line, code, cvErrorStr(code), err.c_str());
258 static const char* g_hwFeatureNames[CV_HARDWARE_MAX_FEATURE] = { NULL };
260 static const char* getHWFeatureName(int id)
262 return (id < CV_HARDWARE_MAX_FEATURE) ? g_hwFeatureNames[id] : NULL;
264 static const char* getHWFeatureNameSafe(int id)
266 const char* name = getHWFeatureName(id);
267 return name ? name : "Unknown feature";
272 enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE };
274 HWFeatures(bool run_initialize = false)
276 memset( have, 0, sizeof(have[0]) * MAX_FEATURE );
281 static void initializeNames()
283 for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
285 g_hwFeatureNames[i] = 0;
287 g_hwFeatureNames[CPU_MMX] = "MMX";
288 g_hwFeatureNames[CPU_SSE] = "SSE";
289 g_hwFeatureNames[CPU_SSE2] = "SSE2";
290 g_hwFeatureNames[CPU_SSE3] = "SSE3";
291 g_hwFeatureNames[CPU_SSSE3] = "SSSE3";
292 g_hwFeatureNames[CPU_SSE4_1] = "SSE4.1";
293 g_hwFeatureNames[CPU_SSE4_2] = "SSE4.2";
294 g_hwFeatureNames[CPU_POPCNT] = "POPCNT";
295 g_hwFeatureNames[CPU_FP16] = "FP16";
296 g_hwFeatureNames[CPU_AVX] = "AVX";
297 g_hwFeatureNames[CPU_AVX2] = "AVX2";
298 g_hwFeatureNames[CPU_FMA3] = "FMA3";
300 g_hwFeatureNames[CPU_AVX_512F] = "AVX512F";
301 g_hwFeatureNames[CPU_AVX_512BW] = "AVX512BW";
302 g_hwFeatureNames[CPU_AVX_512CD] = "AVX512CD";
303 g_hwFeatureNames[CPU_AVX_512DQ] = "AVX512DQ";
304 g_hwFeatureNames[CPU_AVX_512ER] = "AVX512ER";
305 g_hwFeatureNames[CPU_AVX_512IFMA] = "AVX512IFMA";
306 g_hwFeatureNames[CPU_AVX_512PF] = "AVX512PF";
307 g_hwFeatureNames[CPU_AVX_512VBMI] = "AVX512VBMI";
308 g_hwFeatureNames[CPU_AVX_512VL] = "AVX512VL";
310 g_hwFeatureNames[CPU_NEON] = "NEON";
312 g_hwFeatureNames[CPU_VSX] = "VSX";
314 g_hwFeatureNames[CPU_AVX512_SKX] = "AVX512-SKX";
317 void initialize(void)
320 if (getenv("OPENCV_DUMP_CONFIG"))
322 fprintf(stderr, "\nOpenCV build configuration is:\n%s\n",
323 cv::getBuildInformation().c_str());
330 int cpuid_data[4] = { 0, 0, 0, 0 };
331 int cpuid_data_ex[4] = { 0, 0, 0, 0 };
333 CV_CPUID_X86(cpuid_data, 1, 0/*unused*/);
335 int x86_family = (cpuid_data[0] >> 8) & 15;
336 if( x86_family >= 6 )
338 have[CV_CPU_MMX] = (cpuid_data[3] & (1<<23)) != 0;
339 have[CV_CPU_SSE] = (cpuid_data[3] & (1<<25)) != 0;
340 have[CV_CPU_SSE2] = (cpuid_data[3] & (1<<26)) != 0;
341 have[CV_CPU_SSE3] = (cpuid_data[2] & (1<<0)) != 0;
342 have[CV_CPU_SSSE3] = (cpuid_data[2] & (1<<9)) != 0;
343 have[CV_CPU_FMA3] = (cpuid_data[2] & (1<<12)) != 0;
344 have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0;
345 have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0;
346 have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0;
347 have[CV_CPU_AVX] = (cpuid_data[2] & (1<<28)) != 0;
348 have[CV_CPU_FP16] = (cpuid_data[2] & (1<<29)) != 0;
350 // make the second call to the cpuid command in order to get
351 // information about extended features like AVX2
352 CV_CPUID_X86(cpuid_data_ex, 7, 0);
354 have[CV_CPU_AVX2] = (cpuid_data_ex[1] & (1<<5)) != 0;
356 have[CV_CPU_AVX_512F] = (cpuid_data_ex[1] & (1<<16)) != 0;
357 have[CV_CPU_AVX_512DQ] = (cpuid_data_ex[1] & (1<<17)) != 0;
358 have[CV_CPU_AVX_512IFMA512] = (cpuid_data_ex[1] & (1<<21)) != 0;
359 have[CV_CPU_AVX_512PF] = (cpuid_data_ex[1] & (1<<26)) != 0;
360 have[CV_CPU_AVX_512ER] = (cpuid_data_ex[1] & (1<<27)) != 0;
361 have[CV_CPU_AVX_512CD] = (cpuid_data_ex[1] & (1<<28)) != 0;
362 have[CV_CPU_AVX_512BW] = (cpuid_data_ex[1] & (1<<30)) != 0;
363 have[CV_CPU_AVX_512VL] = (cpuid_data_ex[1] & (1<<31)) != 0;
364 have[CV_CPU_AVX_512VBMI] = (cpuid_data_ex[2] & (1<<1)) != 0;
366 bool have_AVX_OS_support = true;
367 bool have_AVX512_OS_support = true;
368 if (!(cpuid_data[2] & (1<<27)))
369 have_AVX_OS_support = false; // OS uses XSAVE_XRSTORE and CPU support AVX
373 #ifdef _XCR_XFEATURE_ENABLED_MASK // requires immintrin.h
374 xcr0 = (int)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
375 #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
376 __asm__ ("xgetbv\n\t" : "=a" (xcr0) : "c" (0) : "%edx" );
378 if ((xcr0 & 0x6) != 0x6)
379 have_AVX_OS_support = false; // YMM registers
380 if ((xcr0 & 0xe6) != 0xe6)
381 have_AVX512_OS_support = false; // ZMM registers
384 if (!have_AVX_OS_support)
386 have[CV_CPU_AVX] = false;
387 have[CV_CPU_FP16] = false;
388 have[CV_CPU_AVX2] = false;
389 have[CV_CPU_FMA3] = false;
391 if (!have_AVX_OS_support || !have_AVX512_OS_support)
393 have[CV_CPU_AVX_512F] = false;
394 have[CV_CPU_AVX_512BW] = false;
395 have[CV_CPU_AVX_512CD] = false;
396 have[CV_CPU_AVX_512DQ] = false;
397 have[CV_CPU_AVX_512ER] = false;
398 have[CV_CPU_AVX_512IFMA512] = false;
399 have[CV_CPU_AVX_512PF] = false;
400 have[CV_CPU_AVX_512VBMI] = false;
401 have[CV_CPU_AVX_512VL] = false;
404 if (have[CV_CPU_AVX_512F])
406 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];
409 #endif // CV_CPUID_X86
411 #if defined __ANDROID__ || defined __linux__
413 have[CV_CPU_NEON] = true;
414 have[CV_CPU_FP16] = true;
415 #elif defined __arm__ && defined __ANDROID__
416 #if defined HAVE_CPUFEATURES
417 CV_LOG_INFO(NULL, "calling android_getCpuFeatures() ...");
418 uint64_t features = android_getCpuFeatures();
419 CV_LOG_INFO(NULL, cv::format("calling android_getCpuFeatures() ... Done (%llx)", (long long)features));
420 have[CV_CPU_NEON] = (features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
421 have[CV_CPU_FP16] = (features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) != 0;
423 CV_LOG_INFO(NULL, "cpufeatures library is not available for CPU detection");
425 CV_LOG_INFO(NULL, "- NEON instructions is enabled via build flags");
426 have[CV_CPU_NEON] = true;
428 CV_LOG_INFO(NULL, "- NEON instructions is NOT enabled via build flags");
431 CV_LOG_INFO(NULL, "- FP16 instructions is enabled via build flags");
432 have[CV_CPU_FP16] = true;
434 CV_LOG_INFO(NULL, "- FP16 instructions is NOT enabled via build flags");
437 #elif defined __arm__
438 int cpufile = open("/proc/self/auxv", O_RDONLY);
443 const size_t size_auxv_t = sizeof(auxv);
445 while ((size_t)read(cpufile, &auxv, size_auxv_t) == size_auxv_t)
447 if (auxv.a_type == AT_HWCAP)
449 have[CV_CPU_NEON] = (auxv.a_un.a_val & 4096) != 0;
450 have[CV_CPU_FP16] = (auxv.a_un.a_val & 2) != 0;
458 #elif (defined __clang__ || defined __APPLE__)
459 #if (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__))
460 have[CV_CPU_NEON] = true;
462 #if (defined __ARM_FP && (((__ARM_FP & 0x2) != 0) && defined __ARM_NEON__))
463 have[CV_CPU_FP16] = true;
468 have[CV_CPU_VSX] = true;
469 #elif (defined __PPC64__ && defined __linux__)
470 uint64 hwcaps = getauxval(AT_HWCAP);
471 uint64 hwcap2 = getauxval(AT_HWCAP2);
472 have[CV_CPU_VSX] = (hwcaps & PPC_FEATURE_PPC_LE && hwcaps & PPC_FEATURE_HAS_VSX && hwcap2 & PPC_FEATURE2_ARCH_2_07);
474 have[CV_CPU_VSX] = false;
477 int baseline_features[] = { CV_CPU_BASELINE_FEATURES };
478 if (!checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0])))
481 "******************************************************************\n"
483 "* This OpenCV build doesn't support current CPU/HW configuration *\n"
485 "* Use OPENCV_DUMP_CONFIG=1 environment variable for details *\n"
486 "******************************************************************\n");
487 fprintf(stderr, "\nRequired baseline features:\n");
488 checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]), true);
489 CV_ErrorNoReturn(cv::Error::StsAssert, "Missing support for required CPU baseline features. Check OpenCV build configuration and required CPU/HW setup.");
492 readSettings(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]));
495 bool checkFeatures(const int* features, int count, bool dump = false)
498 for (int i = 0; i < count; i++)
500 int feature = features[i];
505 if (dump) fprintf(stderr, "%s - OK\n", getHWFeatureNameSafe(feature));
510 if (dump) fprintf(stderr, "%s - NOT AVAILABLE\n", getHWFeatureNameSafe(feature));
517 static inline bool isSymbolSeparator(char c)
519 return c == ',' || c == ';';
522 void readSettings(const int* baseline_features, int baseline_count)
525 const char* disabled_features =
527 getenv("OPENCV_CPU_DISABLE");
531 if (disabled_features && disabled_features[0] != 0)
533 const char* start = disabled_features;
536 while (start[0] != 0 && isSymbolSeparator(start[0]))
542 const char* end = start;
543 while (end[0] != 0 && !isSymbolSeparator(end[0]))
549 cv::String feature(start, end);
552 CV_Assert(feature.size() > 0);
555 for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
557 if (!g_hwFeatureNames[i]) continue;
558 size_t len = strlen(g_hwFeatureNames[i]);
559 if (len != feature.size()) continue;
560 if (feature.compare(g_hwFeatureNames[i]) == 0)
562 bool isBaseline = false;
563 for (int k = 0; k < baseline_count; k++)
565 if (baseline_features[k] == i)
573 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));
577 if (dump) fprintf(stderr, "OPENCV: Trying to disable unavailable CPU feature on the current platform: '%s'.\n", getHWFeatureNameSafe(i));
587 if (dump) fprintf(stderr, "OPENCV: Trying to disable unknown CPU feature: '%s'.\n", feature.c_str());
593 bool have[MAX_FEATURE+1];
596 static HWFeatures featuresEnabled(true), featuresDisabled = HWFeatures(false);
597 static HWFeatures* currentFeatures = &featuresEnabled;
599 bool checkHardwareSupport(int feature)
601 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
602 return currentFeatures->have[feature];
605 String getHardwareFeatureName(int feature)
607 const char* name = getHWFeatureName(feature);
608 return name ? String(name) : String();
611 volatile bool useOptimizedFlag = true;
613 void setUseOptimized( bool flag )
615 useOptimizedFlag = flag;
616 currentFeatures = flag ? &featuresEnabled : &featuresDisabled;
618 ipp::setUseIPP(flag);
620 ocl::setUseOpenCL(flag);
624 bool useOptimized(void)
626 return useOptimizedFlag;
629 int64 getTickCount(void)
631 #if defined _WIN32 || defined WINCE
632 LARGE_INTEGER counter;
633 QueryPerformanceCounter( &counter );
634 return (int64)counter.QuadPart;
635 #elif defined __linux || defined __linux__
637 clock_gettime(CLOCK_MONOTONIC, &tp);
638 return (int64)tp.tv_sec*1000000000 + tp.tv_nsec;
639 #elif defined __MACH__ && defined __APPLE__
640 return (int64)mach_absolute_time();
644 gettimeofday( &tv, &tz );
645 return (int64)tv.tv_sec*1000000 + tv.tv_usec;
649 double getTickFrequency(void)
651 #if defined _WIN32 || defined WINCE
653 QueryPerformanceFrequency(&freq);
654 return (double)freq.QuadPart;
655 #elif defined __linux || defined __linux__
657 #elif defined __MACH__ && defined __APPLE__
658 static double freq = 0;
661 mach_timebase_info_data_t sTimebaseInfo;
662 mach_timebase_info(&sTimebaseInfo);
663 freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer;
671 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__ || defined __ppc__)
672 #if defined(__i386__)
674 int64 getCPUTickCount(void)
677 __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
680 #elif defined(__x86_64__)
682 int64 getCPUTickCount(void)
685 __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
686 return (int64)lo | ((int64)hi << 32);
689 #elif defined(__ppc__)
691 int64 getCPUTickCount(void)
694 unsigned upper, lower, tmp;
702 : "=r"(upper),"=r"(lower),"=r"(tmp)
704 return lower | ((int64)upper << 32);
709 #error "RDTSC not defined"
713 #elif defined _MSC_VER && defined _WIN32 && defined _M_IX86
715 int64 getCPUTickCount(void)
724 //int64 getCPUTickCount(void)
726 // return ippGetCpuClocks();
729 int64 getCPUTickCount(void)
731 return getTickCount();
737 const String& getBuildInformation()
739 static String build_info =
740 #include "version_string.inc"
745 String getVersionString() { return String(CV_VERSION); }
747 int getVersionMajor() { return CV_VERSION_MAJOR; }
749 int getVersionMinor() { return CV_VERSION_MINOR; }
751 int getVersionRevision() { return CV_VERSION_REVISION; }
753 String format( const char* fmt, ... )
755 AutoBuffer<char, 1024> buf;
761 int bsize = static_cast<int>(buf.size());
762 int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
765 CV_Assert(len >= 0 && "Check format string for errors");
772 return String((char *)buf, len);
776 String tempfile( const char* suffix )
780 const char *temp_dir = getenv("OPENCV_TEMP_PATH");
785 RoInitialize(RO_INIT_MULTITHREADED);
786 std::wstring temp_dir = GetTempPathWinRT();
788 std::wstring temp_file = GetTempFileNameWinRT(L"ocv");
789 if (temp_file.empty())
792 temp_file = temp_dir.append(std::wstring(L"\\")).append(temp_file);
793 DeleteFileW(temp_file.c_str());
795 char aname[MAX_PATH];
796 size_t copied = wcstombs(aname, temp_file.c_str(), MAX_PATH);
797 CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
798 fname = String(aname);
801 char temp_dir2[MAX_PATH] = { 0 };
802 char temp_file[MAX_PATH] = { 0 };
804 if (temp_dir == 0 || temp_dir[0] == 0)
806 ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
807 temp_dir = temp_dir2;
809 if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file))
812 DeleteFileA(temp_file);
818 //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
819 char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
821 char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
824 if (temp_dir == 0 || temp_dir[0] == 0)
825 fname = defaultTemplate;
829 char ech = fname[fname.size() - 1];
830 if(ech != '/' && ech != '\\')
832 fname = fname + "__opencv_temp.XXXXXX";
835 const int fd = mkstemp((char*)fname.c_str());
836 if (fd == -1) return String();
839 remove(fname.c_str());
844 if (suffix[0] != '.')
845 return fname + "." + suffix;
847 return fname + suffix;
852 static ErrorCallback customErrorCallback = 0;
853 static void* customErrorCallbackData = 0;
854 static bool breakOnError = false;
856 bool setBreakOnError(bool value)
858 bool prevVal = breakOnError;
859 breakOnError = value;
863 int cv_snprintf(char* buf, int len, const char* fmt, ...)
867 int res = cv_vsnprintf(buf, len, fmt, va);
872 int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
875 if (len <= 0) return len == 0 ? 1024 : -1;
876 int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);
877 // ensure null terminating on VS
878 if (res >= 0 && res < len)
885 buf[len - 1] = 0; // truncate happened
886 return res >= len ? res : (len * 2);
889 return vsnprintf(buf, len, fmt, args);
893 void error( const Exception& exc )
895 if (customErrorCallback != 0)
896 customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
897 exc.file.c_str(), exc.line, customErrorCallbackData);
900 const char* errorStr = cvErrorStr(exc.code);
903 cv_snprintf(buf, sizeof(buf),
904 "OpenCV(%s) Error: %s (%s) in %s, file %s, line %d",
906 errorStr, exc.err.c_str(), exc.func.size() > 0 ?
907 exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line);
908 fprintf( stderr, "%s\n", buf );
911 __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
917 static volatile int* p = 0;
924 void error(int _code, const String& _err, const char* _func, const char* _file, int _line)
926 error(cv::Exception(_code, _err, _func, _file, _line));
931 redirectError( ErrorCallback errCallback, void* userdata, void** prevUserdata)
934 *prevUserdata = customErrorCallbackData;
936 ErrorCallback prevCallback = customErrorCallback;
938 customErrorCallback = errCallback;
939 customErrorCallbackData = userdata;
946 CV_IMPL int cvCheckHardwareSupport(int feature)
948 CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
949 return cv::currentFeatures->have[feature];
952 CV_IMPL int cvUseOptimized( int flag )
954 int prevMode = cv::useOptimizedFlag;
955 cv::setUseOptimized( flag != 0 );
959 CV_IMPL int64 cvGetTickCount(void)
961 return cv::getTickCount();
964 CV_IMPL double cvGetTickFrequency(void)
966 return cv::getTickFrequency()*1e-6;
969 CV_IMPL CvErrorCallback
970 cvRedirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
972 return cv::redirectError(errCallback, userdata, prevUserdata);
975 CV_IMPL int cvNulDevReport( int, const char*, const char*,
976 const char*, int, void* )
981 CV_IMPL int cvStdErrReport( int, const char*, const char*,
982 const char*, int, void* )
987 CV_IMPL int cvGuiBoxReport( int, const char*, const char*,
988 const char*, int, void* )
993 CV_IMPL int cvGetErrInfo( const char**, const char**, const char**, int* )
999 CV_IMPL const char* cvErrorStr( int status )
1001 static char buf[256];
1005 case CV_StsOk : return "No Error";
1006 case CV_StsBackTrace : return "Backtrace";
1007 case CV_StsError : return "Unspecified error";
1008 case CV_StsInternal : return "Internal error";
1009 case CV_StsNoMem : return "Insufficient memory";
1010 case CV_StsBadArg : return "Bad argument";
1011 case CV_StsNoConv : return "Iterations do not converge";
1012 case CV_StsAutoTrace : return "Autotrace call";
1013 case CV_StsBadSize : return "Incorrect size of input array";
1014 case CV_StsNullPtr : return "Null pointer";
1015 case CV_StsDivByZero : return "Division by zero occurred";
1016 case CV_BadStep : return "Image step is wrong";
1017 case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
1018 case CV_StsObjectNotFound : return "Requested object was not found";
1019 case CV_BadDepth : return "Input image depth is not supported by function";
1020 case CV_StsUnmatchedFormats : return "Formats of input arguments do not match";
1021 case CV_StsUnmatchedSizes : return "Sizes of input arguments do not match";
1022 case CV_StsOutOfRange : return "One of arguments\' values is out of range";
1023 case CV_StsUnsupportedFormat : return "Unsupported format or combination of formats";
1024 case CV_BadCOI : return "Input COI is not supported";
1025 case CV_BadNumChannels : return "Bad number of channels";
1026 case CV_StsBadFlag : return "Bad flag (parameter or structure field)";
1027 case CV_StsBadPoint : return "Bad parameter of type CvPoint";
1028 case CV_StsBadMask : return "Bad type of mask argument";
1029 case CV_StsParseError : return "Parsing error";
1030 case CV_StsNotImplemented : return "The function/feature is not implemented";
1031 case CV_StsBadMemBlock : return "Memory block has been corrupted";
1032 case CV_StsAssert : return "Assertion failed";
1033 case CV_GpuNotSupported : return "No CUDA support";
1034 case CV_GpuApiCallError : return "Gpu API call";
1035 case CV_OpenGlNotSupported : return "No OpenGL support";
1036 case CV_OpenGlApiCallError : return "OpenGL API call";
1039 sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
1043 CV_IMPL int cvGetErrMode(void)
1048 CV_IMPL int cvSetErrMode(int)
1053 CV_IMPL int cvGetErrStatus(void)
1058 CV_IMPL void cvSetErrStatus(int)
1063 CV_IMPL void cvError( int code, const char* func_name,
1064 const char* err_msg,
1065 const char* file_name, int line )
1067 cv::error(cv::Exception(code, err_msg, func_name, file_name, line));
1070 /* function, which converts int to int */
1072 cvErrorFromIppStatus( int status )
1076 case CV_BADSIZE_ERR: return CV_StsBadSize;
1077 case CV_BADMEMBLOCK_ERR: return CV_StsBadMemBlock;
1078 case CV_NULLPTR_ERR: return CV_StsNullPtr;
1079 case CV_DIV_BY_ZERO_ERR: return CV_StsDivByZero;
1080 case CV_BADSTEP_ERR: return CV_BadStep;
1081 case CV_OUTOFMEM_ERR: return CV_StsNoMem;
1082 case CV_BADARG_ERR: return CV_StsBadArg;
1083 case CV_NOTDEFINED_ERR: return CV_StsError;
1084 case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
1085 case CV_NOTFOUND_ERR: return CV_StsObjectNotFound;
1086 case CV_BADCONVERGENCE_ERR: return CV_StsNoConv;
1087 case CV_BADDEPTH_ERR: return CV_BadDepth;
1088 case CV_UNMATCHED_FORMATS_ERR: return CV_StsUnmatchedFormats;
1089 case CV_UNSUPPORTED_COI_ERR: return CV_BadCOI;
1090 case CV_UNSUPPORTED_CHANNELS_ERR: return CV_BadNumChannels;
1091 case CV_BADFLAG_ERR: return CV_StsBadFlag;
1092 case CV_BADRANGE_ERR: return CV_StsBadArg;
1093 case CV_BADCOEF_ERR: return CV_StsBadArg;
1094 case CV_BADFACTOR_ERR: return CV_StsBadArg;
1095 case CV_BADPOINT_ERR: return CV_StsBadPoint;
1103 bool __termination = false;
1109 #if defined _WIN32 || defined WINCE
1115 #if (_WIN32_WINNT >= 0x0600)
1116 ::InitializeCriticalSectionEx(&cs, 1000, 0);
1118 ::InitializeCriticalSection(&cs);
1122 ~Impl() { DeleteCriticalSection(&cs); }
1124 void lock() { EnterCriticalSection(&cs); }
1125 bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
1126 void unlock() { LeaveCriticalSection(&cs); }
1128 CRITICAL_SECTION cs;
1138 pthread_mutexattr_t attr;
1139 pthread_mutexattr_init(&attr);
1140 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
1141 pthread_mutex_init(&mt, &attr);
1142 pthread_mutexattr_destroy(&attr);
1146 ~Impl() { pthread_mutex_destroy(&mt); }
1148 void lock() { pthread_mutex_lock(&mt); }
1149 bool trylock() { return pthread_mutex_trylock(&mt) == 0; }
1150 void unlock() { pthread_mutex_unlock(&mt); }
1160 impl = new Mutex::Impl;
1165 if( CV_XADD(&impl->refcount, -1) == 1 )
1170 Mutex::Mutex(const Mutex& m)
1173 CV_XADD(&impl->refcount, 1);
1176 Mutex& Mutex::operator = (const Mutex& m)
1180 CV_XADD(&m.impl->refcount, 1);
1181 if( CV_XADD(&impl->refcount, -1) == 1 )
1188 void Mutex::lock() { impl->lock(); }
1189 void Mutex::unlock() { impl->unlock(); }
1190 bool Mutex::trylock() { return impl->trylock(); }
1193 //////////////////////////////// thread-local storage ////////////////////////////////
1197 #pragma warning(disable:4505) // unreferenced local function has been removed
1199 #ifndef TLS_OUT_OF_INDEXES
1200 #define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
1204 // TLS platform abstraction layer
1205 class TlsAbstraction
1210 void* GetData() const;
1211 void SetData(void *pData);
1219 pthread_key_t tlsKey;
1225 static __declspec( thread ) void* tlsData = NULL; // using C++11 thread attribute for local thread data
1226 TlsAbstraction::TlsAbstraction() {}
1227 TlsAbstraction::~TlsAbstraction() {}
1228 void* TlsAbstraction::GetData() const
1232 void TlsAbstraction::SetData(void *pData)
1237 TlsAbstraction::TlsAbstraction()
1239 tlsKey = TlsAlloc();
1240 CV_Assert(tlsKey != TLS_OUT_OF_INDEXES);
1242 TlsAbstraction::~TlsAbstraction()
1246 void* TlsAbstraction::GetData() const
1248 return TlsGetValue(tlsKey);
1250 void TlsAbstraction::SetData(void *pData)
1252 CV_Assert(TlsSetValue(tlsKey, pData) == TRUE);
1256 TlsAbstraction::TlsAbstraction()
1258 CV_Assert(pthread_key_create(&tlsKey, NULL) == 0);
1260 TlsAbstraction::~TlsAbstraction()
1262 CV_Assert(pthread_key_delete(tlsKey) == 0);
1264 void* TlsAbstraction::GetData() const
1266 return pthread_getspecific(tlsKey);
1268 void TlsAbstraction::SetData(void *pData)
1270 CV_Assert(pthread_setspecific(tlsKey, pData) == 0);
1274 // Per-thread data structure
1283 std::vector<void*> slots; // Data array for a thread
1284 size_t idx; // Thread index in TLS storage. This is not OS thread ID!
1287 // Main TLS storage class
1294 tlsSlots.reserve(32);
1295 threads.reserve(32);
1299 for(size_t i = 0; i < threads.size(); i++)
1303 /* Current architecture doesn't allow proper global objects release, so this check can cause crashes
1305 // Check if all slots were properly cleared
1306 for(size_t j = 0; j < threads[i]->slots.size(); j++)
1308 CV_Assert(threads[i]->slots[j] == 0);
1317 void releaseThread()
1319 AutoLock guard(mtxGlobalAccess);
1320 ThreadData *pTD = (ThreadData*)tls.GetData();
1321 for(size_t i = 0; i < threads.size(); i++)
1323 if(pTD == threads[i])
1333 // Reserve TLS storage index
1334 size_t reserveSlot()
1336 AutoLock guard(mtxGlobalAccess);
1337 CV_Assert(tlsSlotsSize == tlsSlots.size());
1339 // Find unused slots
1340 for(size_t slot = 0; slot < tlsSlotsSize; slot++)
1350 tlsSlots.push_back(1); tlsSlotsSize++;
1351 return tlsSlotsSize - 1;
1354 // Release TLS storage index and pass associated data to caller
1355 void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
1357 AutoLock guard(mtxGlobalAccess);
1358 CV_Assert(tlsSlotsSize == tlsSlots.size());
1359 CV_Assert(tlsSlotsSize > slotIdx);
1361 for(size_t i = 0; i < threads.size(); i++)
1365 std::vector<void*>& thread_slots = threads[i]->slots;
1366 if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
1368 dataVec.push_back(thread_slots[slotIdx]);
1369 thread_slots[slotIdx] = NULL;
1375 tlsSlots[slotIdx] = 0;
1378 // Get data by TLS storage index
1379 void* getData(size_t slotIdx) const
1381 #ifndef CV_THREAD_SANITIZER
1382 CV_Assert(tlsSlotsSize > slotIdx);
1385 ThreadData* threadData = (ThreadData*)tls.GetData();
1386 if(threadData && threadData->slots.size() > slotIdx)
1387 return threadData->slots[slotIdx];
1392 // Gather data from threads by TLS storage index
1393 void gather(size_t slotIdx, std::vector<void*> &dataVec)
1395 AutoLock guard(mtxGlobalAccess);
1396 CV_Assert(tlsSlotsSize == tlsSlots.size());
1397 CV_Assert(tlsSlotsSize > slotIdx);
1399 for(size_t i = 0; i < threads.size(); i++)
1403 std::vector<void*>& thread_slots = threads[i]->slots;
1404 if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
1405 dataVec.push_back(thread_slots[slotIdx]);
1410 // Set data to storage index
1411 void setData(size_t slotIdx, void* pData)
1413 #ifndef CV_THREAD_SANITIZER
1414 CV_Assert(tlsSlotsSize > slotIdx);
1417 ThreadData* threadData = (ThreadData*)tls.GetData();
1420 threadData = new ThreadData;
1421 tls.SetData((void*)threadData);
1423 AutoLock guard(mtxGlobalAccess);
1424 threadData->idx = threads.size();
1425 threads.push_back(threadData);
1429 if(slotIdx >= threadData->slots.size())
1431 AutoLock guard(mtxGlobalAccess); // keep synchronization with gather() calls
1432 threadData->slots.resize(slotIdx + 1, NULL);
1434 threadData->slots[slotIdx] = pData;
1438 TlsAbstraction tls; // TLS abstraction layer instance
1440 Mutex mtxGlobalAccess; // Shared objects operation guard
1441 size_t tlsSlotsSize; // equal to tlsSlots.size() in synchronized sections
1442 // without synchronization this counter doesn't desrease - it is used for slotIdx sanity checks
1443 std::vector<int> tlsSlots; // TLS keys state
1444 std::vector<ThreadData*> threads; // Array for all allocated data. Thread data pointers are placed here to allow data cleanup
1447 // Create global TLS storage object
1448 static TlsStorage &getTlsStorage()
1450 CV_SINGLETON_LAZY_INIT_REF(TlsStorage, new TlsStorage())
1453 TLSDataContainer::TLSDataContainer()
1455 key_ = (int)getTlsStorage().reserveSlot(); // Reserve key from TLS storage
1458 TLSDataContainer::~TLSDataContainer()
1460 CV_Assert(key_ == -1); // Key must be released in child object
1463 void TLSDataContainer::gatherData(std::vector<void*> &data) const
1465 getTlsStorage().gather(key_, data);
1468 void TLSDataContainer::release()
1470 std::vector<void*> data;
1472 getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
1474 for(size_t i = 0; i < data.size(); i++) // Delete all associated data
1475 deleteDataInstance(data[i]);
1478 void TLSDataContainer::cleanup()
1480 std::vector<void*> data;
1482 getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
1483 for(size_t i = 0; i < data.size(); i++) // Delete all associated data
1484 deleteDataInstance(data[i]);
1487 void* TLSDataContainer::getData() const
1489 CV_Assert(key_ != -1 && "Can't fetch data from terminated TLS container.");
1490 void* pData = getTlsStorage().getData(key_); // Check if data was already allocated
1493 // Create new data instance and save it to TLS storage
1494 pData = createDataInstance();
1495 getTlsStorage().setData(key_, pData);
1500 TLSData<CoreTLSData>& getCoreTlsData()
1502 CV_SINGLETON_LAZY_INIT_REF(TLSData<CoreTLSData>, new TLSData<CoreTLSData>())
1505 #if defined CVAPI_EXPORTS && defined _WIN32 && !defined WINCE
1507 #pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
1511 BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved);
1514 BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved)
1516 if (fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH)
1518 if (lpReserved != NULL) // called after ExitProcess() call
1520 cv::__termination = true;
1524 // Not allowed to free resources if lpReserved is non-null
1525 // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx
1526 cv::getTlsStorage().releaseThread();
1535 static int g_threadNum = 0;
1540 id(CV_XADD(&g_threadNum, 1))
1542 #ifdef OPENCV_WITH_ITT
1543 __itt_thread_set_name(cv::format("OpenCVThread-%03d", id).c_str());
1548 static TLSData<ThreadID>& getThreadIDTLS()
1550 CV_SINGLETON_LAZY_INIT_REF(TLSData<ThreadID>, new TLSData<ThreadID>());
1554 int utils::getThreadID() { return getThreadIDTLS().get()->id; }
1556 bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
1559 const char* envValue = NULL;
1561 const char* envValue = getenv(name);
1563 if (envValue == NULL)
1565 return defaultValue;
1567 cv::String value = envValue;
1568 if (value == "1" || value == "True" || value == "true" || value == "TRUE")
1572 if (value == "0" || value == "False" || value == "false" || value == "FALSE")
1576 CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
1580 size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultValue)
1583 const char* envValue = NULL;
1585 const char* envValue = getenv(name);
1587 if (envValue == NULL)
1589 return defaultValue;
1591 cv::String value = envValue;
1593 for (; pos < value.size(); pos++)
1595 if (!isdigit(value[pos]))
1598 cv::String valueStr = value.substr(0, pos);
1599 cv::String suffixStr = value.substr(pos, value.length() - pos);
1600 int v = atoi(valueStr.c_str());
1601 if (suffixStr.length() == 0)
1603 else if (suffixStr == "MB" || suffixStr == "Mb" || suffixStr == "mb")
1604 return v * 1024 * 1024;
1605 else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb")
1607 CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
1610 cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)
1613 const char* envValue = NULL;
1615 const char* envValue = getenv(name);
1617 if (envValue == NULL)
1619 return defaultValue;
1621 cv::String value = envValue;
1626 #ifdef CV_COLLECT_IMPL_DATA
1627 ImplCollector& getImplData()
1629 CV_SINGLETON_LAZY_INIT_REF(ImplCollector, new ImplCollector())
1632 void setImpl(int flags)
1634 cv::AutoLock lock(getImplData().mutex);
1636 getImplData().implFlags = flags;
1637 getImplData().implCode.clear();
1638 getImplData().implFun.clear();
1641 void addImpl(int flag, const char* func)
1643 cv::AutoLock lock(getImplData().mutex);
1645 getImplData().implFlags |= flag;
1646 if(func) // use lazy collection if name was not specified
1648 size_t index = getImplData().implCode.size();
1649 if(!index || (getImplData().implCode[index-1] != flag || getImplData().implFun[index-1].compare(func))) // avoid duplicates
1651 getImplData().implCode.push_back(flag);
1652 getImplData().implFun.push_back(func);
1657 int getImpl(std::vector<int> &impl, std::vector<String> &funName)
1659 cv::AutoLock lock(getImplData().mutex);
1661 impl = getImplData().implCode;
1662 funName = getImplData().implFun;
1663 return getImplData().implFlags; // return actual flags for lazy collection
1666 bool useCollection()
1668 return getImplData().useCollection;
1671 void setUseCollection(bool flag)
1673 cv::AutoLock lock(getImplData().mutex);
1675 getImplData().useCollection = flag;
1681 bool useInstrumentation()
1683 #ifdef ENABLE_INSTRUMENTATION
1684 return getInstrumentStruct().useInstr;
1690 void setUseInstrumentation(bool flag)
1692 #ifdef ENABLE_INSTRUMENTATION
1693 getInstrumentStruct().useInstr = flag;
1699 InstrNode* getTrace()
1701 #ifdef ENABLE_INSTRUMENTATION
1702 return &getInstrumentStruct().rootNode;
1710 #ifdef ENABLE_INSTRUMENTATION
1711 getInstrumentStruct().rootNode.removeChilds();
1712 getInstrumentTLSStruct().pCurrentNode = &getInstrumentStruct().rootNode;
1716 void setFlags(FLAGS modeFlags)
1718 #ifdef ENABLE_INSTRUMENTATION
1719 getInstrumentStruct().flags = modeFlags;
1721 CV_UNUSED(modeFlags);
1726 #ifdef ENABLE_INSTRUMENTATION
1727 return (FLAGS)getInstrumentStruct().flags;
1733 NodeData::NodeData(const char* funName, const char* fileName, int lineNum, void* retAddress, bool alwaysExpand, cv::instr::TYPE instrType, cv::instr::IMPL implType)
1735 m_funName = funName;
1736 m_instrType = instrType;
1737 m_implType = implType;
1738 m_fileName = fileName;
1739 m_lineNum = lineNum;
1740 m_retAddress = retAddress;
1741 m_alwaysExpand = alwaysExpand;
1749 NodeData::NodeData(NodeData &ref)
1753 NodeData& NodeData::operator=(const NodeData &right)
1755 this->m_funName = right.m_funName;
1756 this->m_instrType = right.m_instrType;
1757 this->m_implType = right.m_implType;
1758 this->m_fileName = right.m_fileName;
1759 this->m_lineNum = right.m_lineNum;
1760 this->m_retAddress = right.m_retAddress;
1761 this->m_alwaysExpand = right.m_alwaysExpand;
1763 this->m_threads = right.m_threads;
1764 this->m_counter = right.m_counter;
1765 this->m_ticksTotal = right.m_ticksTotal;
1767 this->m_funError = right.m_funError;
1771 NodeData::~NodeData()
1774 bool operator==(const NodeData& left, const NodeData& right)
1776 if(left.m_lineNum == right.m_lineNum && left.m_funName == right.m_funName && left.m_fileName == right.m_fileName)
1778 if(left.m_retAddress == right.m_retAddress || !(cv::instr::getFlags()&cv::instr::FLAGS_EXPAND_SAME_NAMES || left.m_alwaysExpand))
1784 #ifdef ENABLE_INSTRUMENTATION
1785 InstrStruct& getInstrumentStruct()
1787 static InstrStruct instr;
1791 InstrTLSStruct& getInstrumentTLSStruct()
1793 return *getInstrumentStruct().tlsStruct.get();
1796 InstrNode* getCurrentNode()
1798 return getInstrumentTLSStruct().pCurrentNode;
1801 IntrumentationRegion::IntrumentationRegion(const char* funName, const char* fileName, int lineNum, void *retAddress, bool alwaysExpand, TYPE instrType, IMPL implType)
1806 InstrStruct *pStruct = &getInstrumentStruct();
1807 if(pStruct->useInstr)
1809 InstrTLSStruct *pTLS = &getInstrumentTLSStruct();
1811 // Disable in case of failure
1812 if(!pTLS->pCurrentNode)
1818 int depth = pTLS->pCurrentNode->getDepth();
1819 if(pStruct->maxDepth && pStruct->maxDepth <= depth)
1825 NodeData payload(funName, fileName, lineNum, retAddress, alwaysExpand, instrType, implType);
1826 Node<NodeData>* pChild = NULL;
1828 if(pStruct->flags&FLAGS_MAPPING)
1831 cv::AutoLock guard(pStruct->mutexCreate); // Guard from concurrent child creation
1832 pChild = pTLS->pCurrentNode->findChild(payload);
1835 pChild = new Node<NodeData>(payload);
1836 pTLS->pCurrentNode->addChild(pChild);
1841 pChild = pTLS->pCurrentNode->findChild(payload);
1848 pTLS->pCurrentNode = pChild;
1850 m_regionTicks = getTickCount();
1854 IntrumentationRegion::~IntrumentationRegion()
1856 InstrStruct *pStruct = &getInstrumentStruct();
1857 if(pStruct->useInstr)
1861 InstrTLSStruct *pTLS = &getInstrumentTLSStruct();
1863 if (pTLS->pCurrentNode->m_payload.m_implType == cv::instr::IMPL_OPENCL &&
1864 (pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_FUN ||
1865 pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_WRAPPER))
1867 cv::ocl::finish(); // TODO Support "async" OpenCL instrumentation
1870 uint64 ticks = (getTickCount() - m_regionTicks);
1872 cv::AutoLock guard(pStruct->mutexCount); // Concurrent ticks accumulation
1873 pTLS->pCurrentNode->m_payload.m_counter++;
1874 pTLS->pCurrentNode->m_payload.m_ticksTotal += ticks;
1875 pTLS->pCurrentNode->m_payload.m_tls.get()->m_ticksTotal += ticks;
1878 pTLS->pCurrentNode = pTLS->pCurrentNode->m_pParent;
1889 struct IPPInitSingleton
1905 ippStatus = ippGetCpuFeatures(&cpuFeatures, NULL);
1908 std::cerr << "ERROR: IPP cannot detect CPU features, IPP was disabled " << std::endl;
1912 ippFeatures = cpuFeatures;
1914 const char* pIppEnv = getenv("OPENCV_IPP");
1915 cv::String env = pIppEnv;
1918 #if IPP_VERSION_X100 >= 201703
1919 const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
1920 ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_MPX|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
1921 ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
1922 #elif IPP_VERSION_X100 >= 201700
1923 const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
1924 ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
1925 ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
1927 const Ipp64u minorFeatures = 0;
1930 env = env.toLowerCase();
1931 if(env.substr(0, 2) == "ne")
1934 env = env.substr(3, env.size());
1937 if(env == "disabled")
1939 std::cerr << "WARNING: IPP was disabled by OPENCV_IPP environment variable" << std::endl;
1942 else if(env == "sse42")
1943 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42;
1944 else if(env == "avx2")
1945 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2;
1946 #if IPP_VERSION_X100 >= 201700
1947 #if defined (_M_AMD64) || defined (__x86_64__)
1948 else if(env == "avx512")
1949 ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2|ippCPUID_AVX512F;
1953 std::cerr << "ERROR: Improper value of OPENCV_IPP: " << env.c_str() << ". Correct values are: disabled, sse42, avx2, avx512 (Intel64 only)" << std::endl;
1955 // Trim unsupported features
1956 ippFeatures &= cpuFeatures;
1959 // Disable AVX1 since we don't track regressions for it. SSE42 will be used instead
1960 if(cpuFeatures&ippCPUID_AVX && !(cpuFeatures&ippCPUID_AVX2))
1961 ippFeatures &= ~((Ipp64u)ippCPUID_AVX);
1963 // IPP integrations in OpenCV support only SSE4.2, AVX2 and AVX-512 optimizations.
1965 #if IPP_VERSION_X100 >= 201700
1966 cpuFeatures&ippCPUID_AVX512F ||
1968 cpuFeatures&ippCPUID_AVX2 ||
1969 cpuFeatures&ippCPUID_SSE42
1976 if(ippFeatures == cpuFeatures)
1979 IPP_INITIALIZER(ippFeatures)
1980 ippFeatures = ippGetEnabledCpuFeatures();
1982 // Detect top level optimizations to make comparison easier for optimizations dependent conditions
1983 #if IPP_VERSION_X100 >= 201700
1984 if(ippFeatures&ippCPUID_AVX512F)
1986 if((ippFeatures&ippCPUID_AVX512_SKX) == ippCPUID_AVX512_SKX)
1987 ippTopFeatures = ippCPUID_AVX512_SKX;
1988 else if((ippFeatures&ippCPUID_AVX512_KNL) == ippCPUID_AVX512_KNL)
1989 ippTopFeatures = ippCPUID_AVX512_KNL;
1991 ippTopFeatures = ippCPUID_AVX512F; // Unknown AVX512 configuration
1995 if(ippFeatures&ippCPUID_AVX2)
1996 ippTopFeatures = ippCPUID_AVX2;
1997 else if(ippFeatures&ippCPUID_SSE42)
1998 ippTopFeatures = ippCPUID_SSE42;
2000 pIppLibInfo = ippiGetLibVersion();
2007 int ippStatus; // 0 - all is ok, -1 - IPP functions failed
2008 const char *funcname;
2009 const char *filename;
2013 Ipp64u ippTopFeatures;
2014 const IppLibraryVersion *pIppLibInfo;
2017 static IPPInitSingleton& getIPPSingleton()
2019 CV_SINGLETON_LAZY_INIT_REF(IPPInitSingleton, new IPPInitSingleton())
2023 unsigned long long getIppFeatures()
2026 return getIPPSingleton().ippFeatures;
2032 unsigned long long getIppTopFeatures();
2034 unsigned long long getIppTopFeatures()
2037 return getIPPSingleton().ippTopFeatures;
2043 void setIppStatus(int status, const char * const _funcname, const char * const _filename, int _line)
2046 getIPPSingleton().ippStatus = status;
2047 getIPPSingleton().funcname = _funcname;
2048 getIPPSingleton().filename = _filename;
2049 getIPPSingleton().linen = _line;
2051 CV_UNUSED(status); CV_UNUSED(_funcname); CV_UNUSED(_filename); CV_UNUSED(_line);
2058 return getIPPSingleton().ippStatus;
2064 String getIppErrorLocation()
2067 return format("%s:%d %s", getIPPSingleton().filename ? getIPPSingleton().filename : "", getIPPSingleton().linen, getIPPSingleton().funcname ? getIPPSingleton().funcname : "");
2073 String getIppVersion()
2076 const IppLibraryVersion *pInfo = getIPPSingleton().pIppLibInfo;
2078 return format("%s %s %s", pInfo->Name, pInfo->Version, pInfo->BuildDate);
2080 return String("error");
2082 return String("disabled");
2089 CoreTLSData* data = getCoreTlsData().get();
2090 if(data->useIPP < 0)
2092 data->useIPP = getIPPSingleton().useIPP;
2094 return (data->useIPP > 0);
2100 void setUseIPP(bool flag)
2102 CoreTLSData* data = getCoreTlsData().get();
2104 data->useIPP = (getIPPSingleton().useIPP)?flag:false;
2107 data->useIPP = false;
2114 CoreTLSData* data = getCoreTlsData().get();
2115 if(data->useIPP_NE < 0)
2117 data->useIPP_NE = getIPPSingleton().useIPP_NE;
2119 return (data->useIPP_NE > 0);
2125 void setUseIPP_NE(bool flag)
2127 CoreTLSData* data = getCoreTlsData().get();
2129 data->useIPP_NE = (getIPPSingleton().useIPP_NE)?flag:false;
2132 data->useIPP_NE = false;