system.cpp 63.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
I
Ilya Lavrenov 已提交
15
// Copyright (C) 2015, Itseez Inc., all rights reserved.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"
45
#include <iostream>
46

47
#include <opencv2/core/utils/configuration.private.hpp>
48 49
#include <opencv2/core/utils/trace.private.hpp>

50 51 52 53 54 55 56 57 58 59 60 61 62 63
namespace cv {

static Mutex* __initialization_mutex = NULL;
Mutex& getInitializationMutex()
{
    if (__initialization_mutex == NULL)
        __initialization_mutex = new Mutex();
    return *__initialization_mutex;
}
// force initialization (single-threaded environment)
Mutex* __initialization_mutex_initializer = &getInitializationMutex();

} // namespace cv

64 65 66 67 68 69
#ifdef _MSC_VER
# if _MSC_VER >= 1700
#  pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
# endif
#endif

Z
Zoltán Mizsei 已提交
70
#if defined __ANDROID__ || defined __linux__ || defined __FreeBSD__ || defined __HAIKU__
I
Ilya Lavrenov 已提交
71 72 73
#  include <unistd.h>
#  include <fcntl.h>
#  include <elf.h>
74
#if defined __ANDROID__ || defined __linux__
I
Ilya Lavrenov 已提交
75 76
#  include <linux/auxvec.h>
#endif
B
Bert 已提交
77
#endif
I
Ilya Lavrenov 已提交
78

79
#if defined __ANDROID__ && defined HAVE_CPUFEATURES
80 81 82
#  include <cpu-features.h>
#endif

S
Sayed Adel 已提交
83 84 85 86 87 88 89 90 91 92 93 94
#ifndef __VSX__
# if defined __PPC64__ && defined __linux__
#   include "sys/auxv.h"
#   ifndef AT_HWCAP2
#     define AT_HWCAP2 26
#   endif
#   ifndef PPC_FEATURE2_ARCH_2_07
#     define PPC_FEATURE2_ARCH_2_07 0x80000000
#   endif
# endif
#endif

95
#if defined _WIN32 || defined WINCE
96 97 98 99
#ifndef _WIN32_WINNT           // This is needed for the declaration of TryEnterCriticalSection in winbase.h with Visual Studio 2005 (and older?)
  #define _WIN32_WINNT 0x0400  // http://msdn.microsoft.com/en-us/library/ms686857(VS.85).aspx
#endif
#include <windows.h>
100
#if (_WIN32_WINNT >= 0x0602)
101
  #include <synchapi.h>
102
#endif
103 104 105 106
#undef small
#undef min
#undef max
#undef abs
107
#include <tchar.h>
M
Maksim Shabunin 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#if defined _MSC_VER
  #if _MSC_VER >= 1400
    #include <intrin.h>
  #elif defined _M_IX86
    static void __cpuid(int* cpuid_data, int)
    {
        __asm
        {
            push ebx
            push edi
            mov edi, cpuid_data
            mov eax, 1
            cpuid
            mov [edi], eax
            mov [edi + 4], ebx
            mov [edi + 8], ecx
            mov [edi + 12], edx
            pop edi
            pop ebx
        }
    }
    static void __cpuidex(int* cpuid_data, int, int)
    {
        __asm
        {
            push edi
            mov edi, cpuid_data
            mov eax, 7
            mov ecx, 0
            cpuid
            mov [edi], eax
            mov [edi + 4], ebx
            mov [edi + 8], ecx
            mov [edi + 12], edx
            pop edi
        }
    }
  #endif
#endif
147

148
#ifdef WINRT
149
#include <wrl/client.h>
G
GregoryMorse 已提交
150 151 152 153
#ifndef __cplusplus_winrt
#include <windows.storage.h>
#pragma comment(lib, "runtimeobject.lib")
#endif
154 155 156

std::wstring GetTempPathWinRT()
{
G
GregoryMorse 已提交
157
#ifdef __cplusplus_winrt
158
    return std::wstring(Windows::Storage::ApplicationData::Current->TemporaryFolder->Path->Data());
G
GregoryMorse 已提交
159 160 161 162 163 164 165 166 167 168 169
#else
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationDataStatics> appdataFactory;
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::IApplicationData> appdataRef;
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageFolder> storagefolderRef;
    Microsoft::WRL::ComPtr<ABI::Windows::Storage::IStorageItem> storageitemRef;
    HSTRING str;
    HSTRING_HEADER hstrHead;
    std::wstring wstr;
    if (FAILED(WindowsCreateStringReference(RuntimeClass_Windows_Storage_ApplicationData,
                                            (UINT32)wcslen(RuntimeClass_Windows_Storage_ApplicationData), &hstrHead, &str)))
        return wstr;
G
GregoryMorse 已提交
170
    if (FAILED(RoGetActivationFactory(str, IID_PPV_ARGS(appdataFactory.ReleaseAndGetAddressOf()))))
G
GregoryMorse 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184
        return wstr;
    if (FAILED(appdataFactory->get_Current(appdataRef.ReleaseAndGetAddressOf())))
        return wstr;
    if (FAILED(appdataRef->get_TemporaryFolder(storagefolderRef.ReleaseAndGetAddressOf())))
        return wstr;
    if (FAILED(storagefolderRef.As(&storageitemRef)))
        return wstr;
    str = NULL;
    if (FAILED(storageitemRef->get_Path(&str)))
        return wstr;
    wstr = WindowsGetStringRawBuffer(str, NULL);
    WindowsDeleteString(str);
    return wstr;
#endif
185 186 187 188
}

std::wstring GetTempFileNameWinRT(std::wstring prefix)
{
189 190 191
    wchar_t guidStr[40];
    GUID g;
    CoCreateGuid(&g);
192
    wchar_t* mask = L"%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x";
193 194 195 196
    swprintf(&guidStr[0], sizeof(guidStr)/sizeof(wchar_t), mask,
             g.Data1, g.Data2, g.Data3, UINT(g.Data4[0]), UINT(g.Data4[1]),
             UINT(g.Data4[2]), UINT(g.Data4[3]), UINT(g.Data4[4]),
             UINT(g.Data4[5]), UINT(g.Data4[6]), UINT(g.Data4[7]));
197

198
    return prefix.append(std::wstring(guidStr));
199 200 201
}

#endif
202 203 204 205 206
#else
#include <pthread.h>
#include <sys/time.h>
#include <time.h>

207
#if defined __MACH__ && defined __APPLE__
208 209 210 211 212 213 214 215 216 217
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif

#endif

#ifdef _OPENMP
#include "omp.h"
#endif

218
#if defined __linux__ || defined __APPLE__ || defined __EMSCRIPTEN__ || defined __FreeBSD__ || defined __GLIBC__ || defined __HAIKU__
219 220
#include <unistd.h>
#include <stdio.h>
221
#include <sys/types.h>
222
#if defined __ANDROID__
A
Andrey Kamaev 已提交
223
#include <sys/sysconf.h>
224
#endif
A
Andrey Kamaev 已提交
225
#endif
226

227
#ifdef __ANDROID__
228 229 230
# include <android/log.h>
#endif

231 232 233
namespace cv
{

234 235
Exception::Exception() { code = 0; line = 0; }

236
Exception::Exception(int _code, const String& _err, const String& _func, const String& _file, int _line)
237 238 239 240 241 242 243 244 245
: code(_code), err(_err), func(_func), file(_file), line(_line)
{
    formatMessage();
}

Exception::~Exception() throw() {}

/*!
 \return the error description and the context as a text string.
246
 */
247 248 249 250 251
const char* Exception::what() const throw() { return msg.c_str(); }

void Exception::formatMessage()
{
    if( func.size() > 0 )
252
        msg = format("OpenCV(%s) %s:%d: error: (%d) %s in function %s\n", CV_VERSION, file.c_str(), line, code, err.c_str(), func.c_str());
253
    else
254
        msg = format("OpenCV(%s) %s:%d: error: (%d) %s\n", CV_VERSION, file.c_str(), line, code, err.c_str());
255
}
256

257 258 259 260 261 262 263 264 265 266 267 268
static const char* g_hwFeatureNames[CV_HARDWARE_MAX_FEATURE] = { NULL };

static const char* getHWFeatureName(int id)
{
    return (id < CV_HARDWARE_MAX_FEATURE) ? g_hwFeatureNames[id] : NULL;
}
static const char* getHWFeatureNameSafe(int id)
{
    const char* name = getHWFeatureName(id);
    return name ? name : "Unknown feature";
}

M
Maksim Shabunin 已提交
269 270 271 272
struct HWFeatures
{
    enum { MAX_FEATURE = CV_HARDWARE_MAX_FEATURE };

273
    HWFeatures(bool run_initialize = false)
M
Maksim Shabunin 已提交
274
    {
275 276 277
        memset( have, 0, sizeof(have[0]) * MAX_FEATURE );
        if (run_initialize)
            initialize();
M
Maksim Shabunin 已提交
278 279
    }

280
    static void initializeNames()
M
Maksim Shabunin 已提交
281
    {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
        {
            g_hwFeatureNames[i] = 0;
        }
        g_hwFeatureNames[CPU_MMX] = "MMX";
        g_hwFeatureNames[CPU_SSE] = "SSE";
        g_hwFeatureNames[CPU_SSE2] = "SSE2";
        g_hwFeatureNames[CPU_SSE3] = "SSE3";
        g_hwFeatureNames[CPU_SSSE3] = "SSSE3";
        g_hwFeatureNames[CPU_SSE4_1] = "SSE4.1";
        g_hwFeatureNames[CPU_SSE4_2] = "SSE4.2";
        g_hwFeatureNames[CPU_POPCNT] = "POPCNT";
        g_hwFeatureNames[CPU_FP16] = "FP16";
        g_hwFeatureNames[CPU_AVX] = "AVX";
        g_hwFeatureNames[CPU_AVX2] = "AVX2";
        g_hwFeatureNames[CPU_FMA3] = "FMA3";

        g_hwFeatureNames[CPU_AVX_512F] = "AVX512F";
        g_hwFeatureNames[CPU_AVX_512BW] = "AVX512BW";
        g_hwFeatureNames[CPU_AVX_512CD] = "AVX512CD";
        g_hwFeatureNames[CPU_AVX_512DQ] = "AVX512DQ";
        g_hwFeatureNames[CPU_AVX_512ER] = "AVX512ER";
A
Alexander Alekhin 已提交
304
        g_hwFeatureNames[CPU_AVX_512IFMA] = "AVX512IFMA";
305 306 307 308 309
        g_hwFeatureNames[CPU_AVX_512PF] = "AVX512PF";
        g_hwFeatureNames[CPU_AVX_512VBMI] = "AVX512VBMI";
        g_hwFeatureNames[CPU_AVX_512VL] = "AVX512VL";

        g_hwFeatureNames[CPU_NEON] = "NEON";
S
Sayed Adel 已提交
310 311

        g_hwFeatureNames[CPU_VSX] = "VSX";
A
Alexander Alekhin 已提交
312 313

        g_hwFeatureNames[CPU_AVX512_SKX] = "AVX512-SKX";
314 315 316 317 318 319 320 321 322 323 324 325 326 327
    }

    void initialize(void)
    {
#ifndef WINRT
        if (getenv("OPENCV_DUMP_CONFIG"))
        {
            fprintf(stderr, "\nOpenCV build configuration is:\n%s\n",
                cv::getBuildInformation().c_str());
        }
#endif

        initializeNames();

M
Maksim Shabunin 已提交
328
        int cpuid_data[4] = { 0, 0, 0, 0 };
329
        int cpuid_data_ex[4] = { 0, 0, 0, 0 };
M
Maksim Shabunin 已提交
330 331

    #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
332
    #define OPENCV_HAVE_X86_CPUID 1
M
Maksim Shabunin 已提交
333 334
        __cpuid(cpuid_data, 1);
    #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
335
    #define OPENCV_HAVE_X86_CPUID 1
M
Maksim Shabunin 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        #ifdef __x86_64__
        asm __volatile__
        (
         "movl $1, %%eax\n\t"
         "cpuid\n\t"
         :[eax]"=a"(cpuid_data[0]),[ebx]"=b"(cpuid_data[1]),[ecx]"=c"(cpuid_data[2]),[edx]"=d"(cpuid_data[3])
         :
         : "cc"
        );
        #else
        asm volatile
        (
         "pushl %%ebx\n\t"
         "movl $1,%%eax\n\t"
         "cpuid\n\t"
         "popl %%ebx\n\t"
         : "=a"(cpuid_data[0]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3])
         :
         : "cc"
        );
        #endif
    #endif

359 360 361
    #ifdef OPENCV_HAVE_X86_CPUID
        int x86_family = (cpuid_data[0] >> 8) & 15;
        if( x86_family >= 6 )
M
Maksim Shabunin 已提交
362
        {
363 364 365 366 367 368 369 370 371 372 373
            have[CV_CPU_MMX]    = (cpuid_data[3] & (1<<23)) != 0;
            have[CV_CPU_SSE]    = (cpuid_data[3] & (1<<25)) != 0;
            have[CV_CPU_SSE2]   = (cpuid_data[3] & (1<<26)) != 0;
            have[CV_CPU_SSE3]   = (cpuid_data[2] & (1<<0)) != 0;
            have[CV_CPU_SSSE3]  = (cpuid_data[2] & (1<<9)) != 0;
            have[CV_CPU_FMA3]   = (cpuid_data[2] & (1<<12)) != 0;
            have[CV_CPU_SSE4_1] = (cpuid_data[2] & (1<<19)) != 0;
            have[CV_CPU_SSE4_2] = (cpuid_data[2] & (1<<20)) != 0;
            have[CV_CPU_POPCNT] = (cpuid_data[2] & (1<<23)) != 0;
            have[CV_CPU_AVX]    = (cpuid_data[2] & (1<<28)) != 0;
            have[CV_CPU_FP16]   = (cpuid_data[2] & (1<<29)) != 0;
M
Maksim Shabunin 已提交
374 375 376 377

            // make the second call to the cpuid command in order to get
            // information about extended features like AVX2
        #if defined _MSC_VER && (defined _M_IX86 || defined _M_X64)
378 379
        #define OPENCV_HAVE_X86_CPUID_EX 1
            __cpuidex(cpuid_data_ex, 7, 0);
M
Maksim Shabunin 已提交
380
        #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
381
        #define OPENCV_HAVE_X86_CPUID_EX 1
M
Maksim Shabunin 已提交
382 383 384 385 386 387
            #ifdef __x86_64__
            asm __volatile__
            (
             "movl $7, %%eax\n\t"
             "movl $0, %%ecx\n\t"
             "cpuid\n\t"
388
             :[eax]"=a"(cpuid_data_ex[0]),[ebx]"=b"(cpuid_data_ex[1]),[ecx]"=c"(cpuid_data_ex[2]),[edx]"=d"(cpuid_data_ex[3])
M
Maksim Shabunin 已提交
389 390 391 392 393 394 395 396 397 398 399 400
             :
             : "cc"
            );
            #else
            asm volatile
            (
             "pushl %%ebx\n\t"
             "movl $7,%%eax\n\t"
             "movl $0,%%ecx\n\t"
             "cpuid\n\t"
             "movl %%ebx, %0\n\t"
             "popl %%ebx\n\t"
401
             : "=r"(cpuid_data_ex[1]), "=c"(cpuid_data_ex[2])
M
Maksim Shabunin 已提交
402 403 404 405 406
             :
             : "cc"
            );
            #endif
        #endif
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

        #ifdef OPENCV_HAVE_X86_CPUID_EX
            have[CV_CPU_AVX2]   = (cpuid_data_ex[1] & (1<<5)) != 0;

            have[CV_CPU_AVX_512F]       = (cpuid_data_ex[1] & (1<<16)) != 0;
            have[CV_CPU_AVX_512DQ]      = (cpuid_data_ex[1] & (1<<17)) != 0;
            have[CV_CPU_AVX_512IFMA512] = (cpuid_data_ex[1] & (1<<21)) != 0;
            have[CV_CPU_AVX_512PF]      = (cpuid_data_ex[1] & (1<<26)) != 0;
            have[CV_CPU_AVX_512ER]      = (cpuid_data_ex[1] & (1<<27)) != 0;
            have[CV_CPU_AVX_512CD]      = (cpuid_data_ex[1] & (1<<28)) != 0;
            have[CV_CPU_AVX_512BW]      = (cpuid_data_ex[1] & (1<<30)) != 0;
            have[CV_CPU_AVX_512VL]      = (cpuid_data_ex[1] & (1<<31)) != 0;
            have[CV_CPU_AVX_512VBMI]    = (cpuid_data_ex[2] & (1<<1)) != 0;
        #else
            CV_UNUSED(cpuid_data_ex);
        #endif

            bool have_AVX_OS_support = true;
            bool have_AVX512_OS_support = true;
            if (!(cpuid_data[2] & (1<<27)))
                have_AVX_OS_support = false; // OS uses XSAVE_XRSTORE and CPU support AVX
            else
            {
                int xcr0 = 0;
            #ifdef _XCR_XFEATURE_ENABLED_MASK // requires immintrin.h
                xcr0 = (int)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
            #elif defined __GNUC__ && (defined __i386__ || defined __x86_64__)
                __asm__ ("xgetbv" : "=a" (xcr0) : "c" (0) : "%edx" );
            #endif
                if ((xcr0 & 0x6) != 0x6)
                    have_AVX_OS_support = false; // YMM registers
                if ((xcr0 & 0xe6) != 0xe6)
                    have_AVX512_OS_support = false; // ZMM registers
            }

            if (!have_AVX_OS_support)
            {
                have[CV_CPU_AVX] = false;
                have[CV_CPU_FP16] = false;
                have[CV_CPU_AVX2] = false;
                have[CV_CPU_FMA3] = false;
            }
            if (!have_AVX_OS_support || !have_AVX512_OS_support)
            {
                have[CV_CPU_AVX_512F] = false;
                have[CV_CPU_AVX_512BW] = false;
                have[CV_CPU_AVX_512CD] = false;
                have[CV_CPU_AVX_512DQ] = false;
                have[CV_CPU_AVX_512ER] = false;
                have[CV_CPU_AVX_512IFMA512] = false;
                have[CV_CPU_AVX_512PF] = false;
                have[CV_CPU_AVX_512VBMI] = false;
                have[CV_CPU_AVX_512VL] = false;
            }
A
Alexander Alekhin 已提交
461 462 463 464 465

            if (have[CV_CPU_AVX_512F])
            {
                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];
            }
M
Maksim Shabunin 已提交
466
        }
467 468 469 470
    #else
        CV_UNUSED(cpuid_data);
        CV_UNUSED(cpuid_data_ex);
    #endif // OPENCV_HAVE_X86_CPUID
M
Maksim Shabunin 已提交
471

472
    #if defined __ANDROID__ || defined __linux__
M
Maksim Shabunin 已提交
473
    #ifdef __aarch64__
474 475
        have[CV_CPU_NEON] = true;
        have[CV_CPU_FP16] = true;
476
    #elif defined __arm__ && defined __ANDROID__
477
      #if defined HAVE_CPUFEATURES
478 479 480 481 482
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "calling android_getCpuFeatures() ...");
        uint64_t features = android_getCpuFeatures();
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "calling android_getCpuFeatures() ... Done (%llx)", features);
        have[CV_CPU_NEON] = (features & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
        have[CV_CPU_FP16] = (features & ANDROID_CPU_ARM_FEATURE_VFP_FP16) != 0;
483
      #else
L
luz.paz 已提交
484
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "cpufeatures library is not available for CPU detection");
485 486 487 488 489 490 491 492 493 494 495 496 497
        #if CV_NEON
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "- NEON instructions is enabled via build flags");
        have[CV_CPU_NEON] = true;
        #else
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "- NEON instructions is NOT enabled via build flags");
        #endif
        #if CV_FP16
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "- FP16 instructions is enabled via build flags");
        have[CV_CPU_FP16] = true;
        #else
        __android_log_print(ANDROID_LOG_INFO, "OpenCV", "- FP16 instructions is NOT enabled via build flags");
        #endif
      #endif
498
    #elif defined __arm__
M
Maksim Shabunin 已提交
499 500 501 502 503 504 505 506 507 508 509
        int cpufile = open("/proc/self/auxv", O_RDONLY);

        if (cpufile >= 0)
        {
            Elf32_auxv_t auxv;
            const size_t size_auxv_t = sizeof(auxv);

            while ((size_t)read(cpufile, &auxv, size_auxv_t) == size_auxv_t)
            {
                if (auxv.a_type == AT_HWCAP)
                {
510 511
                    have[CV_CPU_NEON] = (auxv.a_un.a_val & 4096) != 0;
                    have[CV_CPU_FP16] = (auxv.a_un.a_val & 2) != 0;
M
Maksim Shabunin 已提交
512 513 514 515 516 517 518
                    break;
                }
            }

            close(cpufile);
        }
    #endif
519 520
    #elif (defined __clang__ || defined __APPLE__)
    #if (defined __ARM_NEON__ || (defined __ARM_NEON && defined __aarch64__))
521
        have[CV_CPU_NEON] = true;
522 523
    #endif
    #if (defined __ARM_FP  && (((__ARM_FP & 0x2) != 0) && defined __ARM_NEON__))
524
        have[CV_CPU_FP16] = true;
525
    #endif
M
Maksim Shabunin 已提交
526 527
    #endif

S
Sayed Adel 已提交
528 529 530 531 532 533 534 535 536 537
    #ifdef __VSX__
        have[CV_CPU_VSX] = true;
    #elif (defined __PPC64__ && defined __linux__)
        uint64 hwcaps = getauxval(AT_HWCAP);
        uint64 hwcap2 = getauxval(AT_HWCAP2);
        have[CV_CPU_VSX] = (hwcaps & PPC_FEATURE_PPC_LE && hwcaps & PPC_FEATURE_HAS_VSX && hwcap2 & PPC_FEATURE2_ARCH_2_07);
    #else
        have[CV_CPU_VSX] = false;
    #endif

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
        int baseline_features[] = { CV_CPU_BASELINE_FEATURES };
        if (!checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0])))
        {
            fprintf(stderr, "\n"
                    "******************************************************************\n"
                    "* FATAL ERROR:                                                   *\n"
                    "* This OpenCV build doesn't support current CPU/HW configuration *\n"
                    "*                                                                *\n"
                    "* Use OPENCV_DUMP_CONFIG=1 environment variable for details      *\n"
                    "******************************************************************\n");
            fprintf(stderr, "\nRequired baseline features:\n");
            checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]), true);
            CV_ErrorNoReturn(cv::Error::StsAssert, "Missing support for required CPU baseline features. Check OpenCV build configuration and required CPU/HW setup.");
        }

        readSettings(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0]));
    }

    bool checkFeatures(const int* features, int count, bool dump = false)
    {
        bool result = true;
        for (int i = 0; i < count; i++)
        {
            int feature = features[i];
            if (feature)
            {
                if (have[feature])
                {
                    if (dump) fprintf(stderr, "%s - OK\n", getHWFeatureNameSafe(feature));
                }
                else
                {
                    result = false;
                    if (dump) fprintf(stderr, "%s - NOT AVAILABLE\n", getHWFeatureNameSafe(feature));
                }
            }
        }
        return result;
    }

    static inline bool isSymbolSeparator(char c)
    {
        return c == ',' || c == ';' || c == '-';
    }

    void readSettings(const int* baseline_features, int baseline_count)
    {
        bool dump = true;
        const char* disabled_features =
#ifndef WINRT
                getenv("OPENCV_CPU_DISABLE");
#else
                NULL;
#endif
        if (disabled_features && disabled_features[0] != 0)
        {
            const char* start = disabled_features;
            for (;;)
            {
                while (start[0] != 0 && isSymbolSeparator(start[0]))
                {
                    start++;
                }
                if (start[0] == 0)
                    break;
                const char* end = start;
                while (end[0] != 0 && !isSymbolSeparator(end[0]))
                {
                    end++;
                }
                if (end == start)
                    continue;
                cv::String feature(start, end);
                start = end;

                CV_Assert(feature.size() > 0);

                bool found = false;
                for (int i = 0; i < CV_HARDWARE_MAX_FEATURE; i++)
                {
                    if (!g_hwFeatureNames[i]) continue;
                    size_t len = strlen(g_hwFeatureNames[i]);
                    if (len != feature.size()) continue;
                    if (feature.compare(g_hwFeatureNames[i]) == 0)
                    {
                        bool isBaseline = false;
                        for (int k = 0; k < baseline_count; k++)
                        {
                            if (baseline_features[k] == i)
                            {
                                isBaseline = true;
                                break;
                            }
                        }
                        if (isBaseline)
                        {
                            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));
                        }
                        if (!have[i])
                        {
                            if (dump) fprintf(stderr, "OPENCV: Trying to disable unavailable CPU feature on the current platform: '%s'.\n", getHWFeatureNameSafe(i));
                        }
                        have[i] = false;

                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    if (dump) fprintf(stderr, "OPENCV: Trying to disable unknown CPU feature: '%s'.\n", feature.c_str());
                }
            }
        }
M
Maksim Shabunin 已提交
652 653 654 655 656
    }

    bool have[MAX_FEATURE+1];
};

657
static HWFeatures  featuresEnabled(true), featuresDisabled = HWFeatures(false);
M
Maksim Shabunin 已提交
658 659
static HWFeatures* currentFeatures = &featuresEnabled;

660 661 662
bool checkHardwareSupport(int feature)
{
    CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
M
Maksim Shabunin 已提交
663
    return currentFeatures->have[feature];
664 665
}

666 667 668 669 670
String getHardwareFeatureName(int feature)
{
    const char* name = getHWFeatureName(feature);
    return name ? String(name) : String();
}
M
Maksim Shabunin 已提交
671 672 673

volatile bool useOptimizedFlag = true;

674 675
void setUseOptimized( bool flag )
{
M
Maksim Shabunin 已提交
676 677
    useOptimizedFlag = flag;
    currentFeatures = flag ? &featuresEnabled : &featuresDisabled;
678 679

    ipp::setUseIPP(flag);
680
#ifdef HAVE_OPENCL
681
    ocl::setUseOpenCL(flag);
682
#endif
683 684 685
#ifdef HAVE_TEGRA_OPTIMIZATION
    ::tegra::setUseTegra(flag);
#endif
686 687
}

688
bool useOptimized(void)
689
{
M
Maksim Shabunin 已提交
690
    return useOptimizedFlag;
691
}
692 693

int64 getTickCount(void)
694
{
695
#if defined _WIN32 || defined WINCE
696 697 698 699 700 701 702
    LARGE_INTEGER counter;
    QueryPerformanceCounter( &counter );
    return (int64)counter.QuadPart;
#elif defined __linux || defined __linux__
    struct timespec tp;
    clock_gettime(CLOCK_MONOTONIC, &tp);
    return (int64)tp.tv_sec*1000000000 + tp.tv_nsec;
703
#elif defined __MACH__ && defined __APPLE__
704
    return (int64)mach_absolute_time();
705
#else
706 707 708 709 710 711 712
    struct timeval tv;
    struct timezone tz;
    gettimeofday( &tv, &tz );
    return (int64)tv.tv_sec*1000000 + tv.tv_usec;
#endif
}

713
double getTickFrequency(void)
714
{
715
#if defined _WIN32 || defined WINCE
716 717 718 719 720
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    return (double)freq.QuadPart;
#elif defined __linux || defined __linux__
    return 1e9;
721
#elif defined __MACH__ && defined __APPLE__
722 723 724 725 726 727 728
    static double freq = 0;
    if( freq == 0 )
    {
        mach_timebase_info_data_t sTimebaseInfo;
        mach_timebase_info(&sTimebaseInfo);
        freq = sTimebaseInfo.denom*1e9/sTimebaseInfo.numer;
    }
729
    return freq;
730 731 732 733 734
#else
    return 1e6;
#endif
}

735
#if defined __GNUC__ && (defined __i386__ || defined __x86_64__ || defined __ppc__)
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
#if defined(__i386__)

int64 getCPUTickCount(void)
{
    int64 x;
    __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
    return x;
}
#elif defined(__x86_64__)

int64 getCPUTickCount(void)
{
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return (int64)lo | ((int64)hi << 32);
}

#elif defined(__ppc__)

int64 getCPUTickCount(void)
{
757
    int64 result = 0;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
    unsigned upper, lower, tmp;
    __asm__ volatile(
                     "0:                  \n"
                     "\tmftbu   %0           \n"
                     "\tmftb    %1           \n"
                     "\tmftbu   %2           \n"
                     "\tcmpw    %2,%0        \n"
                     "\tbne     0b         \n"
                     : "=r"(upper),"=r"(lower),"=r"(tmp)
                     );
    return lower | ((int64)upper << 32);
}

#else

#error "RDTSC not defined"

#endif

777
#elif defined _MSC_VER && defined _WIN32 && defined _M_IX86
778 779 780 781 782 783 784 785 786

int64 getCPUTickCount(void)
{
    __asm _emit 0x0f;
    __asm _emit 0x31;
}

#else

V
vbystricky 已提交
787 788 789 790 791 792
//#ifdef HAVE_IPP
//int64 getCPUTickCount(void)
//{
//    return ippGetCpuClocks();
//}
//#else
793
int64 getCPUTickCount(void)
794 795 796
{
    return getTickCount();
}
V
vbystricky 已提交
797
//#endif
798 799 800

#endif

801
const String& getBuildInformation()
802
{
803
    static String build_info =
804 805 806 807 808
#include "version_string.inc"
    ;
    return build_info;
}

809
String format( const char* fmt, ... )
810
{
I
Ilya Lavrenov 已提交
811
    AutoBuffer<char, 1024> buf;
812

I
Ilya Lavrenov 已提交
813
    for ( ; ; )
814
    {
I
Ilya Lavrenov 已提交
815
        va_list va;
816
        va_start(va, fmt);
A
Alexander Alekhin 已提交
817
        int bsize = static_cast<int>(buf.size());
818
        int len = cv_vsnprintf((char *)buf, bsize, fmt, va);
819 820
        va_end(va);

821 822
        CV_Assert(len >= 0 && "Check format string for errors");
        if (len >= bsize)
I
Ilya Lavrenov 已提交
823
        {
824
            buf.resize(len + 1);
I
Ilya Lavrenov 已提交
825 826
            continue;
        }
A
Alexander Alekhin 已提交
827
        buf[bsize - 1] = 0;
I
Ilya Lavrenov 已提交
828 829
        return String((char *)buf, len);
    }
830 831
}

832
String tempfile( const char* suffix )
833
{
834
    String fname;
835
#ifndef WINRT
G
GregoryMorse 已提交
836
    const char *temp_dir = getenv("OPENCV_TEMP_PATH");
837
#endif
838

839
#if defined _WIN32
840
#ifdef WINRT
841
    RoInitialize(RO_INIT_MULTITHREADED);
842
    std::wstring temp_dir = GetTempPathWinRT();
843

844
    std::wstring temp_file = GetTempFileNameWinRT(L"ocv");
845
    if (temp_file.empty())
G
GregoryMorse 已提交
846
        return String();
847

848
    temp_file = temp_dir.append(std::wstring(L"\\")).append(temp_file);
849 850
    DeleteFileW(temp_file.c_str());

851 852 853
    char aname[MAX_PATH];
    size_t copied = wcstombs(aname, temp_file.c_str(), MAX_PATH);
    CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
G
GregoryMorse 已提交
854
    fname = String(aname);
855 856
    RoUninitialize();
#else
857 858
    char temp_dir2[MAX_PATH] = { 0 };
    char temp_file[MAX_PATH] = { 0 };
859

860 861 862 863 864
    if (temp_dir == 0 || temp_dir[0] == 0)
    {
        ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
        temp_dir = temp_dir2;
    }
865
    if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file))
866
        return String();
867

R
Roy Reapor 已提交
868 869
    DeleteFileA(temp_file);

870
    fname = temp_file;
871
#endif
872
# else
873
#  ifdef __ANDROID__
874 875 876 877 878 879
    //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
    char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
#  else
    char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
#  endif

880
    if (temp_dir == 0 || temp_dir[0] == 0)
881 882 883 884 885 886
        fname = defaultTemplate;
    else
    {
        fname = temp_dir;
        char ech = fname[fname.size() - 1];
        if(ech != '/' && ech != '\\')
887 888
            fname = fname + "/";
        fname = fname + "__opencv_temp.XXXXXX";
889 890 891
    }

    const int fd = mkstemp((char*)fname.c_str());
892
    if (fd == -1) return String();
893

894 895
    close(fd);
    remove(fname.c_str());
896
# endif
897

898
    if (suffix)
899 900
    {
        if (suffix[0] != '.')
901
            return fname + "." + suffix;
902
        else
903
            return fname + suffix;
904 905
    }
    return fname;
906 907
}

908
static ErrorCallback customErrorCallback = 0;
909 910 911 912 913 914 915 916
static void* customErrorCallbackData = 0;
static bool breakOnError = false;

bool setBreakOnError(bool value)
{
    bool prevVal = breakOnError;
    breakOnError = value;
    return prevVal;
917
}
918

919
int cv_snprintf(char* buf, int len, const char* fmt, ...)
A
Alexander Alekhin 已提交
920 921 922
{
    va_list va;
    va_start(va, fmt);
923
    int res = cv_vsnprintf(buf, len, fmt, va);
A
Alexander Alekhin 已提交
924
    va_end(va);
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
    return res;
}

int cv_vsnprintf(char* buf, int len, const char* fmt, va_list args)
{
#if defined _MSC_VER
    if (len <= 0) return len == 0 ? 1024 : -1;
    int res = _vsnprintf_s(buf, len, _TRUNCATE, fmt, args);
    // ensure null terminating on VS
    if (res >= 0 && res < len)
    {
        buf[res] = 0;
        return res;
    }
    else
    {
        buf[len - 1] = 0; // truncate happened
        return res >= len ? res : (len * 2);
    }
A
Alexander Alekhin 已提交
944
#else
945
    return vsnprintf(buf, len, fmt, args);
A
Alexander Alekhin 已提交
946 947 948
#endif
}

949 950
void error( const Exception& exc )
{
951
    if (customErrorCallback != 0)
952 953 954 955 956
        customErrorCallback(exc.code, exc.func.c_str(), exc.err.c_str(),
                            exc.file.c_str(), exc.line, customErrorCallbackData);
    else
    {
        const char* errorStr = cvErrorStr(exc.code);
957
        char buf[1 << 12];
958

A
Alexander Alekhin 已提交
959
        cv_snprintf(buf, sizeof(buf),
960 961
            "OpenCV(%s) Error: %s (%s) in %s, file %s, line %d",
            CV_VERSION,
962
            errorStr, exc.err.c_str(), exc.func.size() > 0 ?
A
Alexander Alekhin 已提交
963
            exc.func.c_str() : "unknown function", exc.file.c_str(), exc.line);
964 965
        fprintf( stderr, "%s\n", buf );
        fflush( stderr );
966
#  ifdef __ANDROID__
967 968
        __android_log_print(ANDROID_LOG_ERROR, "cv::error()", "%s", buf);
#  endif
969
    }
970

971 972 973 974 975
    if(breakOnError)
    {
        static volatile int* p = 0;
        *p = 0;
    }
976

M
Maksim Shabunin 已提交
977
    CV_THROW(exc);
978
}
979

980 981 982 983 984
void error(int _code, const String& _err, const char* _func, const char* _file, int _line)
{
    error(cv::Exception(_code, _err, _func, _file, _line));
}

A
Alexander Alekhin 已提交
985

986 987
ErrorCallback
redirectError( ErrorCallback errCallback, void* userdata, void** prevUserdata)
988 989 990
{
    if( prevUserdata )
        *prevUserdata = customErrorCallbackData;
991

992
    ErrorCallback prevCallback = customErrorCallback;
993 994

    customErrorCallback     = errCallback;
995
    customErrorCallbackData = userdata;
996

997 998
    return prevCallback;
}
999

1000 1001 1002 1003 1004
}

CV_IMPL int cvCheckHardwareSupport(int feature)
{
    CV_DbgAssert( 0 <= feature && feature <= CV_HARDWARE_MAX_FEATURE );
M
Maksim Shabunin 已提交
1005
    return cv::currentFeatures->have[feature];
1006 1007 1008 1009
}

CV_IMPL int cvUseOptimized( int flag )
{
M
Maksim Shabunin 已提交
1010
    int prevMode = cv::useOptimizedFlag;
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    cv::setUseOptimized( flag != 0 );
    return prevMode;
}

CV_IMPL int64  cvGetTickCount(void)
{
    return cv::getTickCount();
}

CV_IMPL double cvGetTickFrequency(void)
{
    return cv::getTickFrequency()*1e-6;
}

CV_IMPL CvErrorCallback
cvRedirectError( CvErrorCallback errCallback, void* userdata, void** prevUserdata)
{
    return cv::redirectError(errCallback, userdata, prevUserdata);
}

CV_IMPL int cvNulDevReport( int, const char*, const char*,
                            const char*, int, void* )
{
    return 0;
}

CV_IMPL int cvStdErrReport( int, const char*, const char*,
                            const char*, int, void* )
{
    return 0;
}

CV_IMPL int cvGuiBoxReport( int, const char*, const char*,
                            const char*, int, void* )
{
    return 0;
}

CV_IMPL int cvGetErrInfo( const char**, const char**, const char**, int* )
{
    return 0;
}


CV_IMPL const char* cvErrorStr( int status )
{
    static char buf[256];

    switch (status)
    {
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    case CV_StsOk :                  return "No Error";
    case CV_StsBackTrace :           return "Backtrace";
    case CV_StsError :               return "Unspecified error";
    case CV_StsInternal :            return "Internal error";
    case CV_StsNoMem :               return "Insufficient memory";
    case CV_StsBadArg :              return "Bad argument";
    case CV_StsNoConv :              return "Iterations do not converge";
    case CV_StsAutoTrace :           return "Autotrace call";
    case CV_StsBadSize :             return "Incorrect size of input array";
    case CV_StsNullPtr :             return "Null pointer";
1071
    case CV_StsDivByZero :           return "Division by zero occurred";
1072
    case CV_BadStep :                return "Image step is wrong";
1073 1074
    case CV_StsInplaceNotSupported : return "Inplace operation is not supported";
    case CV_StsObjectNotFound :      return "Requested object was not found";
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    case CV_BadDepth :               return "Input image depth is not supported by function";
    case CV_StsUnmatchedFormats :    return "Formats of input arguments do not match";
    case CV_StsUnmatchedSizes :      return "Sizes of input arguments do not match";
    case CV_StsOutOfRange :          return "One of arguments\' values is out of range";
    case CV_StsUnsupportedFormat :   return "Unsupported format or combination of formats";
    case CV_BadCOI :                 return "Input COI is not supported";
    case CV_BadNumChannels :         return "Bad number of channels";
    case CV_StsBadFlag :             return "Bad flag (parameter or structure field)";
    case CV_StsBadPoint :            return "Bad parameter of type CvPoint";
    case CV_StsBadMask :             return "Bad type of mask argument";
    case CV_StsParseError :          return "Parsing error";
    case CV_StsNotImplemented :      return "The function/feature is not implemented";
    case CV_StsBadMemBlock :         return "Memory block has been corrupted";
    case CV_StsAssert :              return "Assertion failed";
V
Vladislav Vinogradov 已提交
1089
    case CV_GpuNotSupported :        return "No CUDA support";
1090 1091 1092
    case CV_GpuApiCallError :        return "Gpu API call";
    case CV_OpenGlNotSupported :     return "No OpenGL support";
    case CV_OpenGlApiCallError :     return "OpenGL API call";
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
    };

    sprintf(buf, "Unknown %s code %d", status >= 0 ? "status":"error", status);
    return buf;
}

CV_IMPL int cvGetErrMode(void)
{
    return 0;
}

CV_IMPL int cvSetErrMode(int)
{
    return 0;
}

1109
CV_IMPL int cvGetErrStatus(void)
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
{
    return 0;
}

CV_IMPL void cvSetErrStatus(int)
{
}


CV_IMPL void cvError( int code, const char* func_name,
                      const char* err_msg,
                      const char* file_name, int line )
{
    cv::error(cv::Exception(code, err_msg, func_name, file_name, line));
}

/* function, which converts int to int */
CV_IMPL int
cvErrorFromIppStatus( int status )
{
    switch (status)
    {
1132 1133 1134 1135 1136 1137 1138 1139
    case CV_BADSIZE_ERR:               return CV_StsBadSize;
    case CV_BADMEMBLOCK_ERR:           return CV_StsBadMemBlock;
    case CV_NULLPTR_ERR:               return CV_StsNullPtr;
    case CV_DIV_BY_ZERO_ERR:           return CV_StsDivByZero;
    case CV_BADSTEP_ERR:               return CV_BadStep;
    case CV_OUTOFMEM_ERR:              return CV_StsNoMem;
    case CV_BADARG_ERR:                return CV_StsBadArg;
    case CV_NOTDEFINED_ERR:            return CV_StsError;
1140
    case CV_INPLACE_NOT_SUPPORTED_ERR: return CV_StsInplaceNotSupported;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    case CV_NOTFOUND_ERR:              return CV_StsObjectNotFound;
    case CV_BADCONVERGENCE_ERR:        return CV_StsNoConv;
    case CV_BADDEPTH_ERR:              return CV_BadDepth;
    case CV_UNMATCHED_FORMATS_ERR:     return CV_StsUnmatchedFormats;
    case CV_UNSUPPORTED_COI_ERR:       return CV_BadCOI;
    case CV_UNSUPPORTED_CHANNELS_ERR:  return CV_BadNumChannels;
    case CV_BADFLAG_ERR:               return CV_StsBadFlag;
    case CV_BADRANGE_ERR:              return CV_StsBadArg;
    case CV_BADCOEF_ERR:               return CV_StsBadArg;
    case CV_BADFACTOR_ERR:             return CV_StsBadArg;
    case CV_BADPOINT_ERR:              return CV_StsBadPoint;

    default:
      return CV_StsError;
1155 1156 1157
    }
}

1158 1159 1160
namespace cv {
bool __termination = false;
}
1161

1162 1163 1164
namespace cv
{

1165
#if defined _WIN32 || defined WINCE
1166 1167 1168

struct Mutex::Impl
{
1169 1170 1171 1172 1173 1174 1175 1176 1177
    Impl()
    {
#if (_WIN32_WINNT >= 0x0600)
        ::InitializeCriticalSectionEx(&cs, 1000, 0);
#else
        ::InitializeCriticalSection(&cs);
#endif
        refcount = 1;
    }
1178
    ~Impl() { DeleteCriticalSection(&cs); }
1179

1180 1181 1182
    void lock() { EnterCriticalSection(&cs); }
    bool trylock() { return TryEnterCriticalSection(&cs) != 0; }
    void unlock() { LeaveCriticalSection(&cs); }
1183

1184 1185 1186
    CRITICAL_SECTION cs;
    int refcount;
};
1187

1188 1189 1190 1191
#else

struct Mutex::Impl
{
I
Ilya Lavrenov 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    Impl()
    {
        pthread_mutexattr_t attr;
        pthread_mutexattr_init(&attr);
        pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init(&mt, &attr);
        pthread_mutexattr_destroy(&attr);

        refcount = 1;
    }
    ~Impl() { pthread_mutex_destroy(&mt); }
1203

I
Ilya Lavrenov 已提交
1204 1205 1206
    void lock() { pthread_mutex_lock(&mt); }
    bool trylock() { return pthread_mutex_trylock(&mt) == 0; }
    void unlock() { pthread_mutex_unlock(&mt); }
1207

I
Ilya Lavrenov 已提交
1208
    pthread_mutex_t mt;
1209 1210 1211 1212 1213 1214 1215 1216 1217
    int refcount;
};

#endif

Mutex::Mutex()
{
    impl = new Mutex::Impl;
}
1218

1219 1220 1221 1222 1223 1224
Mutex::~Mutex()
{
    if( CV_XADD(&impl->refcount, -1) == 1 )
        delete impl;
    impl = 0;
}
1225

1226 1227 1228 1229 1230 1231 1232 1233
Mutex::Mutex(const Mutex& m)
{
    impl = m.impl;
    CV_XADD(&impl->refcount, 1);
}

Mutex& Mutex::operator = (const Mutex& m)
{
1234 1235 1236 1237 1238 1239 1240
    if (this != &m)
    {
        CV_XADD(&m.impl->refcount, 1);
        if( CV_XADD(&impl->refcount, -1) == 1 )
            delete impl;
        impl = m.impl;
    }
1241 1242
    return *this;
}
1243

1244 1245
void Mutex::lock() { impl->lock(); }
void Mutex::unlock() { impl->unlock(); }
1246
bool Mutex::trylock() { return impl->trylock(); }
1247

A
Alexander Alekhin 已提交
1248 1249 1250

//////////////////////////////// thread-local storage ////////////////////////////////

1251
#ifdef _WIN32
P
Pavel Vlasov 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
#ifdef _MSC_VER
#pragma warning(disable:4505) // unreferenced local function has been removed
#endif
#ifndef TLS_OUT_OF_INDEXES
#define TLS_OUT_OF_INDEXES ((DWORD)0xFFFFFFFF)
#endif
#endif

// TLS platform abstraction layer
class TlsAbstraction
A
Alexander Alekhin 已提交
1262 1263
{
public:
P
Pavel Vlasov 已提交
1264 1265 1266 1267
    TlsAbstraction();
    ~TlsAbstraction();
    void* GetData() const;
    void  SetData(void *pData);
A
Alexander Alekhin 已提交
1268

P
Pavel Vlasov 已提交
1269
private:
1270
#ifdef _WIN32
P
Pavel Vlasov 已提交
1271 1272
#ifndef WINRT
    DWORD tlsKey;
A
Alexander Alekhin 已提交
1273
#endif
1274
#else // _WIN32
P
Pavel Vlasov 已提交
1275 1276 1277
    pthread_key_t  tlsKey;
#endif
};
A
Alexander Alekhin 已提交
1278

1279
#ifdef _WIN32
1280
#ifdef WINRT
P
Pavel Vlasov 已提交
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
static __declspec( thread ) void* tlsData = NULL; // using C++11 thread attribute for local thread data
TlsAbstraction::TlsAbstraction() {}
TlsAbstraction::~TlsAbstraction() {}
void* TlsAbstraction::GetData() const
{
    return tlsData;
}
void  TlsAbstraction::SetData(void *pData)
{
    tlsData = pData;
}
#else //WINRT
TlsAbstraction::TlsAbstraction()
{
    tlsKey = TlsAlloc();
    CV_Assert(tlsKey != TLS_OUT_OF_INDEXES);
}
TlsAbstraction::~TlsAbstraction()
{
    TlsFree(tlsKey);
}
void* TlsAbstraction::GetData() const
{
    return TlsGetValue(tlsKey);
}
void  TlsAbstraction::SetData(void *pData)
{
    CV_Assert(TlsSetValue(tlsKey, pData) == TRUE);
}
#endif
1311
#else // _WIN32
P
Pavel Vlasov 已提交
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
TlsAbstraction::TlsAbstraction()
{
    CV_Assert(pthread_key_create(&tlsKey, NULL) == 0);
}
TlsAbstraction::~TlsAbstraction()
{
    CV_Assert(pthread_key_delete(tlsKey) == 0);
}
void* TlsAbstraction::GetData() const
{
    return pthread_getspecific(tlsKey);
}
void  TlsAbstraction::SetData(void *pData)
{
    CV_Assert(pthread_setspecific(tlsKey, pData) == 0);
}
#endif
A
Alexander Alekhin 已提交
1329

P
Pavel Vlasov 已提交
1330 1331 1332 1333
// Per-thread data structure
struct ThreadData
{
    ThreadData()
A
Alexander Alekhin 已提交
1334
    {
P
Pavel Vlasov 已提交
1335 1336
        idx = 0;
        slots.reserve(32);
A
Alexander Alekhin 已提交
1337 1338
    }

P
Pavel Vlasov 已提交
1339 1340 1341
    std::vector<void*> slots; // Data array for a thread
    size_t idx;               // Thread index in TLS storage. This is not OS thread ID!
};
A
Alexander Alekhin 已提交
1342

P
Pavel Vlasov 已提交
1343 1344 1345 1346
// Main TLS storage class
class TlsStorage
{
public:
1347 1348
    TlsStorage() :
        tlsSlotsSize(0)
A
Alexander Alekhin 已提交
1349
    {
P
Pavel Vlasov 已提交
1350
        tlsSlots.reserve(32);
P
Pavel Vlasov 已提交
1351
        threads.reserve(32);
A
Alexander Alekhin 已提交
1352
    }
P
Pavel Vlasov 已提交
1353
    ~TlsStorage()
A
Alexander Alekhin 已提交
1354
    {
P
Pavel Vlasov 已提交
1355
        for(size_t i = 0; i < threads.size(); i++)
A
Alexander Alekhin 已提交
1356
        {
P
Pavel Vlasov 已提交
1357 1358
            if(threads[i])
            {
1359
                /* Current architecture doesn't allow proper global objects release, so this check can cause crashes
P
Pavel Vlasov 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368

                // Check if all slots were properly cleared
                for(size_t j = 0; j < threads[i]->slots.size(); j++)
                {
                    CV_Assert(threads[i]->slots[j] == 0);
                }
                */
                delete threads[i];
            }
A
Alexander Alekhin 已提交
1369
        }
P
Pavel Vlasov 已提交
1370
        threads.clear();
A
Alexander Alekhin 已提交
1371 1372
    }

P
Pavel Vlasov 已提交
1373
    void releaseThread()
A
Alexander Alekhin 已提交
1374
    {
P
Pavel Vlasov 已提交
1375 1376 1377
        AutoLock guard(mtxGlobalAccess);
        ThreadData *pTD = (ThreadData*)tls.GetData();
        for(size_t i = 0; i < threads.size(); i++)
1378
        {
P
Pavel Vlasov 已提交
1379 1380 1381 1382 1383
            if(pTD == threads[i])
            {
                threads[i] = 0;
                break;
            }
1384
        }
P
Pavel Vlasov 已提交
1385 1386
        tls.SetData(0);
        delete pTD;
A
Alexander Alekhin 已提交
1387 1388
    }

P
Pavel Vlasov 已提交
1389 1390
    // Reserve TLS storage index
    size_t reserveSlot()
A
Alexander Alekhin 已提交
1391
    {
P
Pavel Vlasov 已提交
1392
        AutoLock guard(mtxGlobalAccess);
1393
        CV_Assert(tlsSlotsSize == tlsSlots.size());
P
Pavel Vlasov 已提交
1394 1395

        // Find unused slots
1396
        for(size_t slot = 0; slot < tlsSlotsSize; slot++)
P
Pavel Vlasov 已提交
1397 1398 1399 1400 1401 1402 1403 1404 1405
        {
            if(!tlsSlots[slot])
            {
                tlsSlots[slot] = 1;
                return slot;
            }
        }

        // Create new slot
1406 1407
        tlsSlots.push_back(1); tlsSlotsSize++;
        return tlsSlotsSize - 1;
A
Alexander Alekhin 已提交
1408 1409
    }

1410
    // Release TLS storage index and pass associated data to caller
1411
    void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
A
Alexander Alekhin 已提交
1412
    {
P
Pavel Vlasov 已提交
1413
        AutoLock guard(mtxGlobalAccess);
1414 1415
        CV_Assert(tlsSlotsSize == tlsSlots.size());
        CV_Assert(tlsSlotsSize > slotIdx);
A
Alexander Alekhin 已提交
1416

P
Pavel Vlasov 已提交
1417
        for(size_t i = 0; i < threads.size(); i++)
A
Alexander Alekhin 已提交
1418
        {
1419
            if(threads[i])
P
Pavel Vlasov 已提交
1420
            {
1421 1422 1423 1424
                std::vector<void*>& thread_slots = threads[i]->slots;
                if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
                {
                    dataVec.push_back(thread_slots[slotIdx]);
1425
                    thread_slots[slotIdx] = NULL;
1426
                }
P
Pavel Vlasov 已提交
1427
            }
A
Alexander Alekhin 已提交
1428
        }
P
Pavel Vlasov 已提交
1429

1430 1431
        if (!keepSlot)
            tlsSlots[slotIdx] = 0;
A
Alexander Alekhin 已提交
1432 1433
    }

P
Pavel Vlasov 已提交
1434 1435
    // Get data by TLS storage index
    void* getData(size_t slotIdx) const
A
Alexander Alekhin 已提交
1436
    {
1437
#ifndef CV_THREAD_SANITIZER
1438
        CV_Assert(tlsSlotsSize > slotIdx);
1439
#endif
A
Alexander Alekhin 已提交
1440

P
Pavel Vlasov 已提交
1441 1442 1443 1444 1445
        ThreadData* threadData = (ThreadData*)tls.GetData();
        if(threadData && threadData->slots.size() > slotIdx)
            return threadData->slots[slotIdx];

        return NULL;
A
Alexander Alekhin 已提交
1446 1447
    }

P
Pavel Vlasov 已提交
1448 1449 1450 1451
    // Gather data from threads by TLS storage index
    void gather(size_t slotIdx, std::vector<void*> &dataVec)
    {
        AutoLock guard(mtxGlobalAccess);
1452 1453
        CV_Assert(tlsSlotsSize == tlsSlots.size());
        CV_Assert(tlsSlotsSize > slotIdx);
P
Pavel Vlasov 已提交
1454 1455 1456

        for(size_t i = 0; i < threads.size(); i++)
        {
1457 1458 1459 1460 1461 1462
            if(threads[i])
            {
                std::vector<void*>& thread_slots = threads[i]->slots;
                if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
                    dataVec.push_back(thread_slots[slotIdx]);
            }
P
Pavel Vlasov 已提交
1463 1464 1465
        }
    }

P
Pavel Vlasov 已提交
1466 1467
    // Set data to storage index
    void setData(size_t slotIdx, void* pData)
A
Alexander Alekhin 已提交
1468
    {
1469
#ifndef CV_THREAD_SANITIZER
1470
        CV_Assert(tlsSlotsSize > slotIdx);
1471
#endif
P
Pavel Vlasov 已提交
1472 1473 1474

        ThreadData* threadData = (ThreadData*)tls.GetData();
        if(!threadData)
A
Alexander Alekhin 已提交
1475
        {
P
Pavel Vlasov 已提交
1476 1477 1478 1479 1480 1481 1482
            threadData = new ThreadData;
            tls.SetData((void*)threadData);
            {
                AutoLock guard(mtxGlobalAccess);
                threadData->idx = threads.size();
                threads.push_back(threadData);
            }
A
Alexander Alekhin 已提交
1483
        }
P
Pavel Vlasov 已提交
1484 1485

        if(slotIdx >= threadData->slots.size())
P
Pavel Vlasov 已提交
1486
        {
1487 1488
            AutoLock guard(mtxGlobalAccess); // keep synchronization with gather() calls
            threadData->slots.resize(slotIdx + 1, NULL);
P
Pavel Vlasov 已提交
1489
        }
P
Pavel Vlasov 已提交
1490
        threadData->slots[slotIdx] = pData;
A
Alexander Alekhin 已提交
1491
    }
P
Pavel Vlasov 已提交
1492 1493 1494 1495 1496

private:
    TlsAbstraction tls; // TLS abstraction layer instance

    Mutex  mtxGlobalAccess;           // Shared objects operation guard
1497 1498
    size_t tlsSlotsSize;              // equal to tlsSlots.size() in synchronized sections
                                      // without synchronization this counter doesn't desrease - it is used for slotIdx sanity checks
P
Pavel Vlasov 已提交
1499
    std::vector<int> tlsSlots;        // TLS keys state
P
Pavel Vlasov 已提交
1500
    std::vector<ThreadData*> threads; // Array for all allocated data. Thread data pointers are placed here to allow data cleanup
A
Alexander Alekhin 已提交
1501
};
1502

P
Pavel Vlasov 已提交
1503 1504
// Create global TLS storage object
static TlsStorage &getTlsStorage()
1505
{
P
Pavel Vlasov 已提交
1506
    CV_SINGLETON_LAZY_INIT_REF(TlsStorage, new TlsStorage())
1507
}
A
Alexander Alekhin 已提交
1508 1509 1510

TLSDataContainer::TLSDataContainer()
{
P
Pavel Vlasov 已提交
1511
    key_ = (int)getTlsStorage().reserveSlot(); // Reserve key from TLS storage
1512 1513
}

A
Alexander Alekhin 已提交
1514 1515
TLSDataContainer::~TLSDataContainer()
{
P
Pavel Vlasov 已提交
1516
    CV_Assert(key_ == -1); // Key must be released in child object
A
Alexander Alekhin 已提交
1517 1518
}

P
Pavel Vlasov 已提交
1519 1520 1521 1522 1523
void TLSDataContainer::gatherData(std::vector<void*> &data) const
{
    getTlsStorage().gather(key_, data);
}

P
Pavel Vlasov 已提交
1524
void TLSDataContainer::release()
A
Alexander Alekhin 已提交
1525
{
P
Pavel Vlasov 已提交
1526 1527 1528
    std::vector<void*> data;
    data.reserve(32);
    getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    key_ = -1;
    for(size_t i = 0; i < data.size(); i++)  // Delete all associated data
        deleteDataInstance(data[i]);
}

void TLSDataContainer::cleanup()
{
    std::vector<void*> data;
    data.reserve(32);
    getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
1539
    for(size_t i = 0; i < data.size(); i++)  // Delete all associated data
P
Pavel Vlasov 已提交
1540
        deleteDataInstance(data[i]);
A
Alexander Alekhin 已提交
1541 1542
}

P
Pavel Vlasov 已提交
1543
void* TLSDataContainer::getData() const
A
Alexander Alekhin 已提交
1544
{
1545
    CV_Assert(key_ != -1 && "Can't fetch data from terminated TLS container.");
P
Pavel Vlasov 已提交
1546 1547
    void* pData = getTlsStorage().getData(key_); // Check if data was already allocated
    if(!pData)
A
Alexander Alekhin 已提交
1548
    {
P
Pavel Vlasov 已提交
1549 1550 1551
        // Create new data instance and save it to TLS storage
        pData = createDataInstance();
        getTlsStorage().setData(key_, pData);
A
Alexander Alekhin 已提交
1552
    }
P
Pavel Vlasov 已提交
1553
    return pData;
A
Alexander Alekhin 已提交
1554 1555
}

1556 1557
TLSData<CoreTLSData>& getCoreTlsData()
{
1558
    CV_SINGLETON_LAZY_INIT_REF(TLSData<CoreTLSData>, new TLSData<CoreTLSData>())
1559 1560
}

1561
#if defined CVAPI_EXPORTS && defined _WIN32 && !defined WINCE
P
Pavel Vlasov 已提交
1562 1563 1564 1565
#ifdef WINRT
    #pragma warning(disable:4447) // Disable warning 'main' signature found without threading model
#endif

1566 1567 1568
extern "C"
BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved);

P
Pavel Vlasov 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
extern "C"
BOOL WINAPI DllMain(HINSTANCE, DWORD fdwReason, LPVOID lpReserved)
{
    if (fdwReason == DLL_THREAD_DETACH || fdwReason == DLL_PROCESS_DETACH)
    {
        if (lpReserved != NULL) // called after ExitProcess() call
        {
            cv::__termination = true;
        }
        else
        {
            // Not allowed to free resources if lpReserved is non-null
            // http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583.aspx
            cv::getTlsStorage().releaseThread();
        }
    }
    return TRUE;
}
#endif
1588

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681

namespace {
static int g_threadNum = 0;
class ThreadID {
public:
    const int id;
    ThreadID() :
        id(CV_XADD(&g_threadNum, 1))
    {
#ifdef OPENCV_WITH_ITT
        __itt_thread_set_name(cv::format("OpenCVThread-%03d", id).c_str());
#endif
    }
};

static TLSData<ThreadID>& getThreadIDTLS()
{
    CV_SINGLETON_LAZY_INIT_REF(TLSData<ThreadID>, new TLSData<ThreadID>());
}

} // namespace
int utils::getThreadID() { return getThreadIDTLS().get()->id; }

bool utils::getConfigurationParameterBool(const char* name, bool defaultValue)
{
#ifdef NO_GETENV
    const char* envValue = NULL;
#else
    const char* envValue = getenv(name);
#endif
    if (envValue == NULL)
    {
        return defaultValue;
    }
    cv::String value = envValue;
    if (value == "1" || value == "True" || value == "true" || value == "TRUE")
    {
        return true;
    }
    if (value == "0" || value == "False" || value == "false" || value == "FALSE")
    {
        return false;
    }
    CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
}


size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultValue)
{
#ifdef NO_GETENV
    const char* envValue = NULL;
#else
    const char* envValue = getenv(name);
#endif
    if (envValue == NULL)
    {
        return defaultValue;
    }
    cv::String value = envValue;
    size_t pos = 0;
    for (; pos < value.size(); pos++)
    {
        if (!isdigit(value[pos]))
            break;
    }
    cv::String valueStr = value.substr(0, pos);
    cv::String suffixStr = value.substr(pos, value.length() - pos);
    int v = atoi(valueStr.c_str());
    if (suffixStr.length() == 0)
        return v;
    else if (suffixStr == "MB" || suffixStr == "Mb" || suffixStr == "mb")
        return v * 1024 * 1024;
    else if (suffixStr == "KB" || suffixStr == "Kb" || suffixStr == "kb")
        return v * 1024;
    CV_ErrorNoReturn(cv::Error::StsBadArg, cv::format("Invalid value for %s parameter: %s", name, value.c_str()));
}

cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue)
{
#ifdef NO_GETENV
    const char* envValue = NULL;
#else
    const char* envValue = getenv(name);
#endif
    if (envValue == NULL)
    {
        return defaultValue;
    }
    cv::String value = envValue;
    return value;
}


1682
#ifdef CV_COLLECT_IMPL_DATA
P
Pavel Vlasov 已提交
1683 1684
ImplCollector& getImplData()
{
1685
    CV_SINGLETON_LAZY_INIT_REF(ImplCollector, new ImplCollector())
P
Pavel Vlasov 已提交
1686 1687
}

1688 1689
void setImpl(int flags)
{
P
Pavel Vlasov 已提交
1690 1691 1692 1693 1694
    cv::AutoLock lock(getImplData().mutex);

    getImplData().implFlags = flags;
    getImplData().implCode.clear();
    getImplData().implFun.clear();
1695 1696 1697 1698
}

void addImpl(int flag, const char* func)
{
P
Pavel Vlasov 已提交
1699 1700 1701
    cv::AutoLock lock(getImplData().mutex);

    getImplData().implFlags |= flag;
1702 1703
    if(func) // use lazy collection if name was not specified
    {
P
Pavel Vlasov 已提交
1704 1705
        size_t index = getImplData().implCode.size();
        if(!index || (getImplData().implCode[index-1] != flag || getImplData().implFun[index-1].compare(func))) // avoid duplicates
1706
        {
P
Pavel Vlasov 已提交
1707 1708
            getImplData().implCode.push_back(flag);
            getImplData().implFun.push_back(func);
1709 1710 1711 1712 1713 1714
        }
    }
}

int getImpl(std::vector<int> &impl, std::vector<String> &funName)
{
P
Pavel Vlasov 已提交
1715 1716 1717 1718 1719
    cv::AutoLock lock(getImplData().mutex);

    impl    = getImplData().implCode;
    funName = getImplData().implFun;
    return getImplData().implFlags; // return actual flags for lazy collection
1720 1721 1722 1723
}

bool useCollection()
{
P
Pavel Vlasov 已提交
1724
    return getImplData().useCollection;
1725 1726 1727 1728
}

void setUseCollection(bool flag)
{
P
Pavel Vlasov 已提交
1729 1730 1731
    cv::AutoLock lock(getImplData().mutex);

    getImplData().useCollection = flag;
1732 1733 1734
}
#endif

1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
namespace instr
{
bool useInstrumentation()
{
#ifdef ENABLE_INSTRUMENTATION
    return getInstrumentStruct().useInstr;
#else
    return false;
#endif
}

void setUseInstrumentation(bool flag)
{
#ifdef ENABLE_INSTRUMENTATION
    getInstrumentStruct().useInstr = flag;
#else
    CV_UNUSED(flag);
#endif
}

InstrNode* getTrace()
{
#ifdef ENABLE_INSTRUMENTATION
    return &getInstrumentStruct().rootNode;
#else
    return NULL;
#endif
}

void resetTrace()
{
#ifdef ENABLE_INSTRUMENTATION
    getInstrumentStruct().rootNode.removeChilds();
    getInstrumentTLSStruct().pCurrentNode = &getInstrumentStruct().rootNode;
#endif
}

1772
void setFlags(FLAGS modeFlags)
1773 1774
{
#ifdef ENABLE_INSTRUMENTATION
1775
    getInstrumentStruct().flags = modeFlags;
1776 1777 1778 1779
#else
    CV_UNUSED(modeFlags);
#endif
}
1780
FLAGS getFlags()
1781 1782
{
#ifdef ENABLE_INSTRUMENTATION
1783
    return (FLAGS)getInstrumentStruct().flags;
1784
#else
1785
    return (FLAGS)0;
1786 1787 1788
#endif
}

1789
NodeData::NodeData(const char* funName, const char* fileName, int lineNum, void* retAddress, bool alwaysExpand, cv::instr::TYPE instrType, cv::instr::IMPL implType)
1790
{
1791 1792 1793 1794 1795 1796 1797
    m_funName       = funName;
    m_instrType     = instrType;
    m_implType      = implType;
    m_fileName      = fileName;
    m_lineNum       = lineNum;
    m_retAddress    = retAddress;
    m_alwaysExpand  = alwaysExpand;
1798

1799 1800
    m_threads    = 1;
    m_counter    = 0;
1801 1802
    m_ticksTotal = 0;

1803
    m_funError  = false;
1804 1805 1806 1807 1808 1809 1810
}
NodeData::NodeData(NodeData &ref)
{
    *this = ref;
}
NodeData& NodeData::operator=(const NodeData &right)
{
1811 1812 1813 1814 1815 1816 1817 1818 1819
    this->m_funName      = right.m_funName;
    this->m_instrType    = right.m_instrType;
    this->m_implType     = right.m_implType;
    this->m_fileName     = right.m_fileName;
    this->m_lineNum      = right.m_lineNum;
    this->m_retAddress   = right.m_retAddress;
    this->m_alwaysExpand = right.m_alwaysExpand;

    this->m_threads     = right.m_threads;
1820 1821
    this->m_counter     = right.m_counter;
    this->m_ticksTotal  = right.m_ticksTotal;
1822

1823
    this->m_funError    = right.m_funError;
1824

1825 1826 1827 1828 1829 1830 1831 1832
    return *this;
}
NodeData::~NodeData()
{
}
bool operator==(const NodeData& left, const NodeData& right)
{
    if(left.m_lineNum == right.m_lineNum && left.m_funName == right.m_funName && left.m_fileName == right.m_fileName)
1833 1834 1835 1836
    {
        if(left.m_retAddress == right.m_retAddress || !(cv::instr::getFlags()&cv::instr::FLAGS_EXPAND_SAME_NAMES || left.m_alwaysExpand))
            return true;
    }
1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
    return false;
}

#ifdef ENABLE_INSTRUMENTATION
InstrStruct& getInstrumentStruct()
{
    static InstrStruct instr;
    return instr;
}

InstrTLSStruct& getInstrumentTLSStruct()
{
    return *getInstrumentStruct().tlsStruct.get();
}

InstrNode* getCurrentNode()
{
    return getInstrumentTLSStruct().pCurrentNode;
}

1857
IntrumentationRegion::IntrumentationRegion(const char* funName, const char* fileName, int lineNum, void *retAddress, bool alwaysExpand, TYPE instrType, IMPL implType)
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
{
    m_disabled    = false;
    m_regionTicks = 0;

    InstrStruct *pStruct = &getInstrumentStruct();
    if(pStruct->useInstr)
    {
        InstrTLSStruct *pTLS = &getInstrumentTLSStruct();

        // Disable in case of failure
        if(!pTLS->pCurrentNode)
        {
            m_disabled = true;
            return;
        }

1874 1875 1876 1877
        int depth = pTLS->pCurrentNode->getDepth();
        if(pStruct->maxDepth && pStruct->maxDepth <= depth)
        {
            m_disabled = true;
1878
            return;
1879
        }
1880

1881
        NodeData payload(funName, fileName, lineNum, retAddress, alwaysExpand, instrType, implType);
1882 1883
        Node<NodeData>* pChild = NULL;

1884
        if(pStruct->flags&FLAGS_MAPPING)
1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
        {
            // Critical section
            cv::AutoLock guard(pStruct->mutexCreate); // Guard from concurrent child creation
            pChild = pTLS->pCurrentNode->findChild(payload);
            if(!pChild)
            {
                pChild = new Node<NodeData>(payload);
                pTLS->pCurrentNode->addChild(pChild);
            }
        }
        else
        {
            pChild = pTLS->pCurrentNode->findChild(payload);
            if(!pChild)
            {
1900
                m_disabled = true;
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
                return;
            }
        }
        pTLS->pCurrentNode = pChild;

        m_regionTicks = getTickCount();
    }
}

IntrumentationRegion::~IntrumentationRegion()
{
    InstrStruct *pStruct = &getInstrumentStruct();
    if(pStruct->useInstr)
    {
        if(!m_disabled)
        {
            InstrTLSStruct *pTLS = &getInstrumentTLSStruct();
1918 1919 1920 1921

            if (pTLS->pCurrentNode->m_payload.m_implType == cv::instr::IMPL_OPENCL &&
                (pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_FUN ||
                    pTLS->pCurrentNode->m_payload.m_instrType == cv::instr::TYPE_WRAPPER))
1922
            {
1923
                cv::ocl::finish(); // TODO Support "async" OpenCL instrumentation
1924 1925
            }

1926 1927 1928 1929 1930 1931
            uint64 ticks = (getTickCount() - m_regionTicks);
            {
                cv::AutoLock guard(pStruct->mutexCount); // Concurrent ticks accumulation
                pTLS->pCurrentNode->m_payload.m_counter++;
                pTLS->pCurrentNode->m_payload.m_ticksTotal += ticks;
                pTLS->pCurrentNode->m_payload.m_tls.get()->m_ticksTotal += ticks;
1932
            }
1933 1934

            pTLS->pCurrentNode = pTLS->pCurrentNode->m_pParent;
1935 1936 1937 1938 1939 1940
        }
    }
}
#endif
}

I
Ilya Lavrenov 已提交
1941 1942 1943
namespace ipp
{

1944
#ifdef HAVE_IPP
P
Pavel Vlasov 已提交
1945
struct IPPInitSingleton
1946 1947
{
public:
P
Pavel Vlasov 已提交
1948
    IPPInitSingleton()
1949
    {
P
Pavel Vlasov 已提交
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
        useIPP         = true;
        useIPP_NE      = false;
        ippStatus      = 0;
        funcname       = NULL;
        filename       = NULL;
        linen          = 0;
        cpuFeatures    = 0;
        ippFeatures    = 0;
        ippTopFeatures = 0;
        pIppLibInfo    = NULL;

        ippStatus = ippGetCpuFeatures(&cpuFeatures, NULL);
        if(ippStatus < 0)
        {
            std::cerr << "ERROR: IPP cannot detect CPU features, IPP was disabled " << std::endl;
            useIPP = false;
            return;
        }
        ippFeatures = cpuFeatures;
1969 1970 1971 1972 1973

        const char* pIppEnv = getenv("OPENCV_IPP");
        cv::String env = pIppEnv;
        if(env.size())
        {
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
#if IPP_VERSION_X100 >= 201703
            const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
                ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_MPX|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
                ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
#elif IPP_VERSION_X100 >= 201700
            const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C|
                ippCPUID_ADCOX|ippCPUID_RDSEED|ippCPUID_PREFETCHW|ippCPUID_SHA|ippCPUID_AVX512CD|ippCPUID_AVX512ER|
                ippCPUID_AVX512PF|ippCPUID_AVX512BW|ippCPUID_AVX512DQ|ippCPUID_AVX512VL|ippCPUID_AVX512VBMI;
#else
            const Ipp64u minorFeatures = 0;
#endif

P
Pavel Vlasov 已提交
1986 1987 1988 1989 1990 1991 1992
            env = env.toLowerCase();
            if(env.substr(0, 2) == "ne")
            {
                useIPP_NE = true;
                env = env.substr(3, env.size());
            }

1993 1994 1995 1996 1997 1998
            if(env == "disabled")
            {
                std::cerr << "WARNING: IPP was disabled by OPENCV_IPP environment variable" << std::endl;
                useIPP = false;
            }
            else if(env == "sse42")
1999
                ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42;
2000
            else if(env == "avx2")
2001 2002
                ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2;
#if IPP_VERSION_X100 >= 201700
P
Pavel Vlasov 已提交
2003 2004
#if defined (_M_AMD64) || defined (__x86_64__)
            else if(env == "avx512")
2005 2006
                ippFeatures = minorFeatures|ippCPUID_SSE2|ippCPUID_SSE3|ippCPUID_SSSE3|ippCPUID_SSE41|ippCPUID_SSE42|ippCPUID_AVX|ippCPUID_AVX2|ippCPUID_AVX512F;
#endif
2007 2008
#endif
            else
P
Pavel Vlasov 已提交
2009 2010
                std::cerr << "ERROR: Improper value of OPENCV_IPP: " << env.c_str() << ". Correct values are: disabled, sse42, avx2, avx512 (Intel64 only)" << std::endl;

2011 2012
            // Trim unsupported features
            ippFeatures &= cpuFeatures;
P
Pavel Vlasov 已提交
2013 2014 2015 2016
        }

        // Disable AVX1 since we don't track regressions for it. SSE42 will be used instead
        if(cpuFeatures&ippCPUID_AVX && !(cpuFeatures&ippCPUID_AVX2))
2017
            ippFeatures &= ~((Ipp64u)ippCPUID_AVX);
P
Pavel Vlasov 已提交
2018 2019 2020

        // IPP integrations in OpenCV support only SSE4.2, AVX2 and AVX-512 optimizations.
        if(!(
2021
#if IPP_VERSION_X100 >= 201700
P
Pavel Vlasov 已提交
2022
            cpuFeatures&ippCPUID_AVX512F ||
2023
#endif
P
Pavel Vlasov 已提交
2024 2025 2026 2027 2028 2029
            cpuFeatures&ippCPUID_AVX2 ||
            cpuFeatures&ippCPUID_SSE42
            ))
        {
            useIPP = false;
            return;
2030 2031
        }

2032 2033 2034 2035
        if(ippFeatures == cpuFeatures)
            IPP_INITIALIZER(0)
        else
            IPP_INITIALIZER(ippFeatures)
2036
        ippFeatures = ippGetEnabledCpuFeatures();
P
Pavel Vlasov 已提交
2037 2038

        // Detect top level optimizations to make comparison easier for optimizations dependent conditions
2039
#if IPP_VERSION_X100 >= 201700
P
Pavel Vlasov 已提交
2040 2041 2042 2043 2044 2045 2046 2047 2048
        if(ippFeatures&ippCPUID_AVX512F)
        {
            if((ippFeatures&ippCPUID_AVX512_SKX) == ippCPUID_AVX512_SKX)
                ippTopFeatures = ippCPUID_AVX512_SKX;
            else if((ippFeatures&ippCPUID_AVX512_KNL) == ippCPUID_AVX512_KNL)
                ippTopFeatures = ippCPUID_AVX512_KNL;
            else
                ippTopFeatures = ippCPUID_AVX512F; // Unknown AVX512 configuration
        }
2049 2050 2051
        else
#endif
        if(ippFeatures&ippCPUID_AVX2)
P
Pavel Vlasov 已提交
2052 2053 2054 2055 2056
            ippTopFeatures = ippCPUID_AVX2;
        else if(ippFeatures&ippCPUID_SSE42)
            ippTopFeatures = ippCPUID_SSE42;

        pIppLibInfo = ippiGetLibVersion();
2057 2058
    }

P
Pavel Vlasov 已提交
2059 2060 2061
public:
    bool        useIPP;
    bool        useIPP_NE;
2062

P
Pavel Vlasov 已提交
2063
    int         ippStatus;  // 0 - all is ok, -1 - IPP functions failed
2064 2065 2066
    const char *funcname;
    const char *filename;
    int         linen;
2067
    Ipp64u      ippFeatures;
P
Pavel Vlasov 已提交
2068 2069 2070
    Ipp64u      cpuFeatures;
    Ipp64u      ippTopFeatures;
    const IppLibraryVersion *pIppLibInfo;
2071 2072
};

P
Pavel Vlasov 已提交
2073
static IPPInitSingleton& getIPPSingleton()
2074
{
P
Pavel Vlasov 已提交
2075
    CV_SINGLETON_LAZY_INIT_REF(IPPInitSingleton, new IPPInitSingleton())
2076
}
2077
#endif
2078

2079 2080 2081
#if OPENCV_ABI_COMPATIBILITY > 300
unsigned long long getIppFeatures()
#else
2082
int getIppFeatures()
2083
#endif
2084 2085
{
#ifdef HAVE_IPP
2086
#if OPENCV_ABI_COMPATIBILITY > 300
P
Pavel Vlasov 已提交
2087
    return getIPPSingleton().ippFeatures;
2088 2089 2090
#else
    return (int)getIPPSingleton().ippFeatures;
#endif
2091 2092 2093 2094
#else
    return 0;
#endif
}
I
Ilya Lavrenov 已提交
2095

P
Pavel Vlasov 已提交
2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
unsigned long long getIppTopFeatures();

unsigned long long getIppTopFeatures()
{
#ifdef HAVE_IPP
    return getIPPSingleton().ippTopFeatures;
#else
    return 0;
#endif
}

I
Ilya Lavrenov 已提交
2107
void setIppStatus(int status, const char * const _funcname, const char * const _filename, int _line)
I
Ilya Lavrenov 已提交
2108
{
2109
#ifdef HAVE_IPP
P
Pavel Vlasov 已提交
2110 2111 2112 2113
    getIPPSingleton().ippStatus = status;
    getIPPSingleton().funcname = _funcname;
    getIPPSingleton().filename = _filename;
    getIPPSingleton().linen = _line;
2114 2115 2116
#else
    CV_UNUSED(status); CV_UNUSED(_funcname); CV_UNUSED(_filename); CV_UNUSED(_line);
#endif
I
Ilya Lavrenov 已提交
2117 2118 2119 2120
}

int getIppStatus()
{
2121
#ifdef HAVE_IPP
P
Pavel Vlasov 已提交
2122
    return getIPPSingleton().ippStatus;
2123 2124 2125
#else
    return 0;
#endif
I
Ilya Lavrenov 已提交
2126 2127 2128 2129
}

String getIppErrorLocation()
{
2130
#ifdef HAVE_IPP
P
Pavel Vlasov 已提交
2131
    return format("%s:%d %s", getIPPSingleton().filename ? getIPPSingleton().filename : "", getIPPSingleton().linen, getIPPSingleton().funcname ? getIPPSingleton().funcname : "");
2132 2133 2134
#else
    return String();
#endif
I
Ilya Lavrenov 已提交
2135 2136
}

P
Pavel Vlasov 已提交
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
String getIppVersion()
{
#ifdef HAVE_IPP
    const IppLibraryVersion *pInfo = getIPPSingleton().pIppLibInfo;
    if(pInfo)
        return format("%s %s %s", pInfo->Name, pInfo->Version, pInfo->BuildDate);
    else
        return String("error");
#else
    return String("disabled");
#endif
}

2150 2151 2152
bool useIPP()
{
#ifdef HAVE_IPP
2153
    CoreTLSData* data = getCoreTlsData().get();
2154 2155
    if(data->useIPP < 0)
    {
P
Pavel Vlasov 已提交
2156
        data->useIPP = getIPPSingleton().useIPP;
2157 2158 2159 2160 2161 2162 2163 2164 2165
    }
    return (data->useIPP > 0);
#else
    return false;
#endif
}

void setUseIPP(bool flag)
{
2166
    CoreTLSData* data = getCoreTlsData().get();
2167
#ifdef HAVE_IPP
P
Pavel Vlasov 已提交
2168
    data->useIPP = (getIPPSingleton().useIPP)?flag:false;
2169 2170 2171 2172 2173 2174
#else
    (void)flag;
    data->useIPP = false;
#endif
}

P
Pavel Vlasov 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
bool useIPP_NE()
{
#ifdef HAVE_IPP
    CoreTLSData* data = getCoreTlsData().get();
    if(data->useIPP_NE < 0)
    {
        data->useIPP_NE = getIPPSingleton().useIPP_NE;
    }
    return (data->useIPP_NE > 0);
#else
    return false;
#endif
}

void setUseIPP_NE(bool flag)
{
    CoreTLSData* data = getCoreTlsData().get();
#ifdef HAVE_IPP
    data->useIPP_NE = (getIPPSingleton().useIPP_NE)?flag:false;
#else
    (void)flag;
    data->useIPP_NE = false;
#endif
}

I
Ilya Lavrenov 已提交
2200 2201
} // namespace ipp

2202 2203
} // namespace cv

2204 2205 2206 2207 2208 2209
#ifdef HAVE_TEGRA_OPTIMIZATION

namespace tegra {

bool useTegra()
{
2210
    cv::CoreTLSData* data = cv::getCoreTlsData().get();
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225

    if (data->useTegra < 0)
    {
        const char* pTegraEnv = getenv("OPENCV_TEGRA");
        if (pTegraEnv && (cv::String(pTegraEnv) == "disabled"))
            data->useTegra = false;
        else
            data->useTegra = true;
    }

    return (data->useTegra > 0);
}

void setUseTegra(bool flag)
{
2226
    cv::CoreTLSData* data = cv::getCoreTlsData().get();
2227 2228 2229 2230 2231 2232 2233
    data->useTegra = flag;
}

} // namespace tegra

#endif

2234
/* End of file. */