diff --git a/modules/gapi/include/opencv2/gapi/gkernel.hpp b/modules/gapi/include/opencv2/gapi/gkernel.hpp index 060fda7b5e..f70e50253d 100644 --- a/modules/gapi/include/opencv2/gapi/gkernel.hpp +++ b/modules/gapi/include/opencv2/gapi/gkernel.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2020 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #ifndef OPENCV_GAPI_GKERNEL_HPP @@ -517,6 +517,13 @@ namespace gapi { */ const std::vector& get_transformations() const; + /** + * @brief Returns vector of kernel ids included in the package + * + * @return vector of kernel ids included in the package + */ + std::vector get_kernel_ids() const; + /** * @brief Test if a particular kernel _implementation_ KImpl is * included in this kernel package. @@ -606,6 +613,18 @@ namespace gapi { includeHelper(); } + /** + * @brief Adds a new kernel based on it's backend and id into the kernel package + * + * @param backend backend associated with the kernel + * @param kernel_id a name/id of the kernel + */ + void include(const cv::gapi::GBackend& backend, const std::string& kernel_id) + { + removeAPI(kernel_id); + m_id_kernels[kernel_id] = std::make_pair(backend, GKernelImpl{{}, {}}); + } + /** * @brief Lists all backends which are included into package * diff --git a/modules/gapi/include/opencv2/gapi/s11n.hpp b/modules/gapi/include/opencv2/gapi/s11n.hpp index 0e2c4c239b..5a64410e5a 100644 --- a/modules/gapi/include/opencv2/gapi/s11n.hpp +++ b/modules/gapi/include/opencv2/gapi/s11n.hpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2020 Intel Corporation +// Copyright (C) 2020-2021 Intel Corporation #ifndef OPENCV_GAPI_S11N_HPP #define OPENCV_GAPI_S11N_HPP @@ -24,6 +24,8 @@ namespace detail { GAPI_EXPORTS cv::GRunArgs getRunArgs(const std::vector &p); + GAPI_EXPORTS std::vector getVectorOfStrings(const std::vector &p); + template cv::GCompileArgs getCompileArgs(const std::vector &p); @@ -42,6 +44,7 @@ T deserialize(const std::vector &p); GAPI_EXPORTS std::vector serialize(const cv::GCompileArgs&); GAPI_EXPORTS std::vector serialize(const cv::GMetaArgs&); GAPI_EXPORTS std::vector serialize(const cv::GRunArgs&); +GAPI_EXPORTS std::vector serialize(const std::vector&); template<> inline cv::GComputation deserialize(const std::vector &p) { @@ -58,6 +61,11 @@ cv::GRunArgs deserialize(const std::vector &p) { return detail::getRunArgs(p); } +template<> inline +std::vector deserialize(const std::vector &p) { + return detail::getVectorOfStrings(p); +} + template inline typename std::enable_if::value, GCompileArgs>:: type deserialize(const std::vector &p) { diff --git a/modules/gapi/src/api/gkernel.cpp b/modules/gapi/src/api/gkernel.cpp index 6993e95807..2a68272a4c 100644 --- a/modules/gapi/src/api/gkernel.cpp +++ b/modules/gapi/src/api/gkernel.cpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018-2019 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation #include "precomp.hpp" @@ -55,6 +55,16 @@ const std::vector &cv::gapi::GKernelPackage::get_transformations return m_transformations; } +std::vector cv::gapi::GKernelPackage::get_kernel_ids() const +{ + std::vector ids; + for (auto &&id : m_id_kernels) + { + ids.emplace_back(id.first); + } + return ids; +} + cv::gapi::GKernelPackage cv::gapi::combine(const GKernelPackage &lhs, const GKernelPackage &rhs) { diff --git a/modules/gapi/src/api/s11n.cpp b/modules/gapi/src/api/s11n.cpp index b6acf28ea4..d08f47fd26 100644 --- a/modules/gapi/src/api/s11n.cpp +++ b/modules/gapi/src/api/s11n.cpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2020 Intel Corporation +// Copyright (C) 2020-2021 Intel Corporation #include #include @@ -30,6 +30,11 @@ cv::GRunArgs cv::gapi::detail::getRunArgs(const std::vector &p) { return run_args_deserialize(is); } +std::vector cv::gapi::detail::getVectorOfStrings(const std::vector &p) { + cv::gapi::s11n::ByteMemoryInStream is(p); + return vector_of_strings_deserialize(is); +} + std::vector cv::gapi::serialize(const cv::GMetaArgs& ma) { cv::gapi::s11n::ByteMemoryOutStream os; @@ -51,6 +56,13 @@ std::vector cv::gapi::serialize(const cv::GCompileArgs& ca) return os.data(); } +std::vector cv::gapi::serialize(const std::vector& vs) +{ + cv::gapi::s11n::ByteMemoryOutStream os; + serialize(os, vs); + return os.data(); +} + // FIXME: This function should move from S11N to GRunArg-related entities. // it has nothing to do with the S11N as it is cv::GRunArgsP cv::gapi::bind(cv::GRunArgs &results) diff --git a/modules/gapi/src/backends/common/serialization.cpp b/modules/gapi/src/backends/common/serialization.cpp index 3b60f5420a..7389bacb02 100644 --- a/modules/gapi/src/backends/common/serialization.cpp +++ b/modules/gapi/src/backends/common/serialization.cpp @@ -2,7 +2,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2020 Intel Corporation +// Copyright (C) 2020-2021 Intel Corporation #include // set #include // map @@ -906,6 +906,9 @@ GAPI_EXPORTS void serialize(IOStream& os, const cv::GMetaArgs &ma) { GAPI_EXPORTS void serialize(IOStream& os, const cv::GRunArgs &ra) { os << ra; } +GAPI_EXPORTS void serialize(IOStream& os, const std::vector &vs) { + os << vs; +} GAPI_EXPORTS GMetaArgs meta_args_deserialize(IIStream& is) { GMetaArgs s; is >> s; @@ -916,6 +919,11 @@ GAPI_EXPORTS GRunArgs run_args_deserialize(IIStream& is) { is >> s; return s; } +GAPI_EXPORTS std::vector vector_of_strings_deserialize(IIStream& is) { + std::vector s; + is >> s; + return s; +} } // namespace s11n } // namespace gapi diff --git a/modules/gapi/src/backends/common/serialization.hpp b/modules/gapi/src/backends/common/serialization.hpp index a3134d84d2..b4204ca64e 100644 --- a/modules/gapi/src/backends/common/serialization.hpp +++ b/modules/gapi/src/backends/common/serialization.hpp @@ -5,7 +5,7 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2020 Intel Corporation +// Copyright (C) 2020-2021 Intel Corporation #include #include @@ -217,8 +217,10 @@ GAPI_EXPORTS std::unique_ptr getInStream(const std::vector &p); GAPI_EXPORTS void serialize(IOStream& os, const cv::GCompileArgs &ca); GAPI_EXPORTS void serialize(IOStream& os, const cv::GMetaArgs &ma); GAPI_EXPORTS void serialize(IOStream& os, const cv::GRunArgs &ra); +GAPI_EXPORTS void serialize(IOStream& os, const std::vector &vs); GAPI_EXPORTS GMetaArgs meta_args_deserialize(IIStream& is); GAPI_EXPORTS GRunArgs run_args_deserialize(IIStream& is); +GAPI_EXPORTS std::vector vector_of_strings_deserialize(IIStream& is); } // namespace s11n } // namespace gapi diff --git a/modules/gapi/test/gapi_kernel_tests.cpp b/modules/gapi/test/gapi_kernel_tests.cpp index d57e25110c..dbb0a7f269 100644 --- a/modules/gapi/test/gapi_kernel_tests.cpp +++ b/modules/gapi/test/gapi_kernel_tests.cpp @@ -2,8 +2,9 @@ // It is subject to the license terms in the LICENSE file found in the top-level directory // of this distribution and at http://opencv.org/license.html. // -// Copyright (C) 2018 Intel Corporation +// Copyright (C) 2018-2021 Intel Corporation +#include #include "test_precomp.hpp" #include "gapi_mock_kernels.hpp" @@ -146,6 +147,29 @@ TEST(KernelPackage, Includes) EXPECT_FALSE(pkg.includes()); } +TEST(KernelPackage, Include) +{ + namespace J = Jupiter; + auto pkg = cv::gapi::kernels(); + pkg.include(J::backend(), "test.kernels.foo"); + pkg.include(J::backend(), "test.kernels.bar"); + EXPECT_TRUE (pkg.includes()); + EXPECT_TRUE (pkg.includes()); +} + +TEST(KernelPackage, GetIds) +{ + namespace J = Jupiter; + auto pkg = cv::gapi::kernels(); + pkg.include(J::backend(), "test.kernels.foo"); + pkg.include(J::backend(), "test.kernels.bar"); + pkg.include(); + auto ids = pkg.get_kernel_ids(); + EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.foo")); + EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.bar")); + EXPECT_NE(ids.end(), std::find(ids.begin(), ids.end(), "test.kernels.baz")); +} + TEST(KernelPackage, IncludesAPI) { namespace J = Jupiter; diff --git a/modules/gapi/test/s11n/gapi_s11n_tests.cpp b/modules/gapi/test/s11n/gapi_s11n_tests.cpp index 16f19846ed..f4a30b3946 100644 --- a/modules/gapi/test/s11n/gapi_s11n_tests.cpp +++ b/modules/gapi/test/s11n/gapi_s11n_tests.cpp @@ -571,6 +571,16 @@ TEST_F(S11N_Basic, Test_Bind_RunArgs_MatScalar) { } } +TEST_F(S11N_Basic, Test_Vector_Of_Strings) { + std::vector vs{"hello", "world", "42"}; + + const std::vector ser = cv::gapi::serialize(vs); + auto des = cv::gapi::deserialize>(ser); + EXPECT_EQ("hello", des[0]); + EXPECT_EQ("world", des[1]); + EXPECT_EQ("42", des[2]); +} + TEST_F(S11N_Basic, Test_RunArg_RMat) { cv::Mat mat = cv::Mat::eye(cv::Size(128, 64), CV_8UC3); cv::RMat rmat = cv::make_rmat(mat, 42, "It actually works");