1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4 //
5 // Copyright (C) 2021 Intel Corporation
6 
7 #ifndef OPENCV_GAPI_ITT_HPP
8 #define OPENCV_GAPI_ITT_HPP
9 
10 // for GAPI_ITT_NAMED_TRACE_GUARD
11 #include <type_traits>
12 #include <memory>
13 
14 #include <opencv2/gapi/util/compiler_hints.hpp>
15 
16 // NOTE: OPENCV_WITH_ITT is only defined if ITT dependecy is built by OpenCV infrastructure.
17 //       There will not be such define in G-API standalone mode.
18 // TODO: Consider using OpenCV's trace.hpp
19 #if defined(OPENCV_WITH_ITT)
20 #include <ittnotify.h>
21 
22 namespace cv {
23 namespace util {
24     template< class T >
25     using remove_reference_t = typename std::remove_reference<T>::type;
26 
27     // Home brew ScopeGuard
28     // D will be called automatically with p as argument when ScopeGuard goes out of scope.
29     // call release() on the ScopeGuard object to revoke guard action
30     template<typename T, typename D>
make_ptr_guard(T * p,D && d)31     auto make_ptr_guard(T* p, D&& d) -> std::unique_ptr<T, util::remove_reference_t<D>> {
32         return {p, std::forward<D>(d)};
33     }
34 }  // namespace util
35 
36 namespace gimpl {
37     extern __itt_domain* gapi_itt_domain;
38     namespace {
__anon2a46baf90202(__itt_string_handle* h) 39         auto make_itt_guard = [](__itt_string_handle* h) {
40            __itt_task_begin(gapi_itt_domain, __itt_null, __itt_null, (h));
41            return util::make_ptr_guard(reinterpret_cast<int*>(1),
42                                        [](int* ){ __itt_task_end(gapi_itt_domain); });
43         };
44     }  // namespace
45 } // namespace gimpl
46 } // namespace cv
47 
48 #define GAPI_ITT_NAMED_TRACE_GUARD(name, h)      auto name = cv::gimpl::make_itt_guard(h); \
49                                                  cv::util::suppress_unused_warning(name)
50 #define GAPI_ITT_STATIC_LOCAL_HANDLE_IMPL(n, h)  static __itt_string_handle* n = \
51                                                  __itt_string_handle_create(h)
52 #define GAPI_ITT_DYNAMIC_LOCAL_HANDLE_IMPL(n, h) __itt_string_handle* n = \
53                                                  __itt_string_handle_create(h)
54 #else // OPENCV_WITH_ITT
55 
56 namespace cv {
57 namespace gimpl {
resetcv::gimpl::dumb_guard58 struct dumb_guard { void reset() { } };
59 } // namespace gimpl
60 } // namespace cv
61 
62 #define GAPI_ITT_NAMED_TRACE_GUARD(name, h)      cv::gimpl::dumb_guard name; \
63                                                  cv::util::suppress_unused_warning(name); \
64                                                  cv::util::suppress_unused_warning(h)
65 #define GAPI_ITT_STATIC_LOCAL_HANDLE_IMPL(n, h)  static auto n = h
66 #define GAPI_ITT_DYNAMIC_LOCAL_HANDLE_IMPL(n, h) auto n = h
67 
68 #endif // OPENCV_WITH_ITT
69 
70 #define GAPI_ITT_AUTO_TRACE_GUARD_IMPL_(LINE, h) GAPI_ITT_NAMED_TRACE_GUARD( \
71                                                     itt_trace_guard_##LINE, h)
72 #define GAPI_ITT_AUTO_TRACE_GUARD_IMPL(LINE, h)  GAPI_ITT_AUTO_TRACE_GUARD_IMPL_(LINE, h)
73 #define GAPI_ITT_AUTO_TRACE_GUARD(h)             GAPI_ITT_AUTO_TRACE_GUARD_IMPL(__LINE__, h)
74 
75 #define GAPI_ITT_STATIC_LOCAL_HANDLE(n, h)       GAPI_ITT_STATIC_LOCAL_HANDLE_IMPL(n, h)
76 #define GAPI_ITT_DYNAMIC_LOCAL_HANDLE(n, h)      GAPI_ITT_DYNAMIC_LOCAL_HANDLE_IMPL(n, h)
77 
78 #endif // OPENCV_GAPI_ITT_HPP
79