desync.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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) 2020-2021 Intel Corporation
  6. #ifndef OPENCV_GAPI_GSTREAMING_DESYNC_HPP
  7. #define OPENCV_GAPI_GSTREAMING_DESYNC_HPP
  8. #include <tuple>
  9. #include <opencv2/gapi/util/util.hpp>
  10. #include <opencv2/gapi/gtype_traits.hpp>
  11. #include <opencv2/gapi/garg.hpp>
  12. #include <opencv2/gapi/gcall.hpp>
  13. #include <opencv2/gapi/gkernel.hpp>
  14. namespace cv {
  15. namespace gapi {
  16. namespace streaming {
  17. namespace detail {
  18. struct GDesync {
  19. static const char *id() {
  20. return "org.opencv.streaming.desync";
  21. }
  22. // An universal yield for desync.
  23. // Yields output objects according to the input Types...
  24. // Reuses gkernel machinery.
  25. // FIXME: This function can be generic and declared in gkernel.hpp
  26. // (it is there already, but a part of GKernelType[M]
  27. template<typename... R, int... IIs>
  28. static std::tuple<R...> yield(cv::GCall &call, cv::detail::Seq<IIs...>) {
  29. return std::make_tuple(cv::detail::Yield<R>::yield(call, IIs)...);
  30. }
  31. };
  32. template<typename G>
  33. G desync(const G &g) {
  34. cv::GKernel k{
  35. GDesync::id() // kernel id
  36. , "" // kernel tag
  37. , [](const GMetaArgs &a, const GArgs &) {return a;} // outMeta callback
  38. , {cv::detail::GTypeTraits<G>::shape} // output Shape
  39. , {cv::detail::GTypeTraits<G>::op_kind} // input data kinds
  40. , {cv::detail::GObtainCtor<G>::get()} // output template ctors
  41. };
  42. cv::GCall call(std::move(k));
  43. call.pass(g);
  44. return std::get<0>(GDesync::yield<G>(call, cv::detail::MkSeq<1>::type()));
  45. }
  46. } // namespace detail
  47. /**
  48. * @brief Starts a desynchronized branch in the graph.
  49. *
  50. * This operation takes a single G-API data object and returns a
  51. * graph-level "duplicate" of this object.
  52. *
  53. * Operations which use this data object can be desynchronized
  54. * from the rest of the graph.
  55. *
  56. * This operation has no effect when a GComputation is compiled with
  57. * regular cv::GComputation::compile(), since cv::GCompiled objects
  58. * always produce their full output vectors.
  59. *
  60. * This operation only makes sense when a GComputation is compiled in
  61. * streaming mode with cv::GComputation::compileStreaming(). If this
  62. * operation is used and there are desynchronized outputs, the user
  63. * should use a special version of cv::GStreamingCompiled::pull()
  64. * which produces an array of cv::util::optional<> objects.
  65. *
  66. * @note This feature is highly experimental now and is currently
  67. * limited to a single GMat/GFrame argument only.
  68. */
  69. GAPI_EXPORTS GMat desync(const GMat &g);
  70. GAPI_EXPORTS GFrame desync(const GFrame &f);
  71. } // namespace streaming
  72. } // namespace gapi
  73. } // namespace cv
  74. #endif // OPENCV_GAPI_GSTREAMING_DESYNC_HPP