github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/opencv4/include/opencv2/gapi/gcompoundkernel.hpp (about)

     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) 2018-2019 Intel Corporation
     6  
     7  
     8  #ifndef OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
     9  #define OPENCV_GAPI_GCOMPOUNDKERNEL_HPP
    10  
    11  #include <opencv2/gapi/opencv_includes.hpp>
    12  #include <opencv2/gapi/gcommon.hpp>
    13  #include <opencv2/gapi/gkernel.hpp>
    14  #include <opencv2/gapi/garg.hpp>
    15  
    16  namespace cv {
    17  namespace gapi
    18  {
    19  namespace compound
    20  {
    21      // FIXME User does not need to know about this function
    22      // Needs that user may define compound kernels(as cpu kernels)
    23      GAPI_EXPORTS cv::gapi::GBackend backend();
    24  } // namespace compound
    25  } // namespace gapi
    26  
    27  namespace detail
    28  {
    29  
    30  struct GCompoundContext
    31  {
    32      explicit GCompoundContext(const GArgs& in_args);
    33      template<typename T>
    34      const T& inArg(int input) { return m_args.at(input).get<T>(); }
    35  
    36      GArgs m_args;
    37      GArgs m_results;
    38  };
    39  
    40  class GAPI_EXPORTS GCompoundKernel
    41  {
    42  // Compound kernel must use all of it's inputs
    43  public:
    44      using F = std::function<void(GCompoundContext& ctx)>;
    45  
    46      explicit GCompoundKernel(const F& f);
    47      void apply(GCompoundContext& ctx);
    48  
    49  protected:
    50      F m_f;
    51  };
    52  
    53  template<typename T> struct get_compound_in
    54  {
    55      static T get(GCompoundContext &ctx, int idx) { return ctx.inArg<T>(idx); }
    56  };
    57  
    58  template<typename U> struct get_compound_in<cv::GArray<U>>
    59  {
    60      static cv::GArray<U> get(GCompoundContext &ctx, int idx)
    61      {
    62          auto array = cv::GArray<U>();
    63          ctx.m_args[idx] = GArg(array);
    64          return array;
    65      }
    66  };
    67  
    68  template<typename U> struct get_compound_in<cv::GOpaque<U>>
    69  {
    70      static cv::GOpaque<U> get(GCompoundContext &ctx, int idx)
    71      {
    72          auto opaq = cv::GOpaque<U>();
    73          ctx.m_args[idx] = GArg(opaq);
    74          return opaq;
    75      }
    76  };
    77  
    78  template<> struct get_compound_in<cv::GMatP>
    79  {
    80      static cv::GMatP get(GCompoundContext &ctx, int idx)
    81      {
    82          auto mat = cv::GMatP();
    83          ctx.m_args[idx] = GArg(mat);
    84          return mat;
    85      }
    86  };
    87  
    88  template<typename, typename, typename>
    89  struct GCompoundCallHelper;
    90  
    91  template<typename Impl, typename... Ins, typename... Outs>
    92  struct GCompoundCallHelper<Impl, std::tuple<Ins...>, std::tuple<Outs...> >
    93  {
    94      template<int... IIs, int... OIs>
    95      static void expand_impl(GCompoundContext &ctx, detail::Seq<IIs...>, detail::Seq<OIs...>)
    96      {
    97          auto result = Impl::expand(get_compound_in<Ins>::get(ctx, IIs)...);
    98          auto tuple_return = tuple_wrap_helper<decltype(result)>::get(std::move(result));
    99          ctx.m_results = { cv::GArg(std::get<OIs>(tuple_return))... };
   100      }
   101  
   102      static void expand(GCompoundContext &ctx)
   103      {
   104          expand_impl(ctx,
   105                      typename detail::MkSeq<sizeof...(Ins)>::type(),
   106                      typename detail::MkSeq<sizeof...(Outs)>::type());
   107      }
   108  };
   109  
   110  template<class Impl, class K>
   111  class GCompoundKernelImpl: public cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>,
   112                             public cv::detail::KernelTag
   113  {
   114      using P = cv::detail::GCompoundCallHelper<Impl, typename K::InArgs, typename K::OutArgs>;
   115  
   116  public:
   117      using API = K;
   118  
   119      static cv::gapi::GBackend backend() { return cv::gapi::compound::backend(); }
   120      static GCompoundKernel    kernel()  { return GCompoundKernel(&P::expand);   }
   121  };
   122  
   123  } // namespace detail
   124  
   125  
   126  /**
   127   * Declares a new compound kernel. See this
   128   * [documentation chapter](@ref gapi_kernel_compound)
   129   * on compound kernels for more details.
   130   *
   131   * @param Name type name for new kernel
   132   * @param API the interface this kernel implements
   133   */
   134  #define GAPI_COMPOUND_KERNEL(Name, API) \
   135      struct Name: public cv::detail::GCompoundKernelImpl<Name, API>
   136  
   137  } // namespace cv
   138  
   139  #endif // OPENCV_GAPI_GCOMPOUNDKERNEL_HPP