1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #18339 from rgarnov:rg/rmat_integration

[GAPI] RMat integration into the framework

* RMat integration

* Added initialization of input mat in GArray initialization tests

* Fixed klocwork warnings in RMat tests, changed argument order in EXPECT_EQ
This commit is contained in:
Ruslan Garnov
2020-10-04 21:57:41 +03:00
committed by GitHub
parent 199687a1c5
commit 5224d016e9
31 changed files with 659 additions and 229 deletions
+57 -80
View File
@@ -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 Intel Corporation
// Copyright (C) 2018-2020 Intel Corporation
#include "precomp.hpp"
@@ -103,38 +103,34 @@ namespace cv {
namespace gimpl {
namespace magazine {
// FIXME implement the below functions with visit()?
namespace {
// Utility function, used in both bindInArg and bindOutArg,
// implements default RMat bind behaviour (if backend doesn't handle RMats in specific way):
// view + wrapped cv::Mat are placed into the magazine
void bindRMat(Mag& mag, const RcDesc& rc, const cv::RMat& rmat, RMat::Access a)
{
auto& matv = mag.template slot<RMat::View>()[rc.id];
matv = rmat.access(a);
mag.template slot<cv::Mat>()[rc.id] = asMat(matv);
}
} // anonymous namespace
void bindInArg(Mag& mag, const RcDesc &rc, const GRunArg &arg, bool is_umat)
// FIXME implement the below functions with visit()?
void bindInArg(Mag& mag, const RcDesc &rc, const GRunArg &arg, HandleRMat handleRMat)
{
switch (rc.shape)
{
case GShape::GMAT:
{
switch (arg.index())
{
case GRunArg::index_of<cv::Mat>() :
if (is_umat)
{
#if !defined(GAPI_STANDALONE)
auto& mag_umat = mag.template slot<cv::UMat>()[rc.id];
mag_umat = util::get<cv::Mat>(arg).getUMat(ACCESS_READ);
#else
util::throw_error(std::logic_error("UMat is not supported in standalone build"));
#endif // !defined(GAPI_STANDALONE)
}
else
{
auto& mag_mat = mag.template slot<cv::Mat>()[rc.id];
mag_mat = util::get<cv::Mat>(arg);
}
break;
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
}
// In case of handleRMat == SKIP
// We assume that backend can work with some device-specific RMats
// and will handle them in some specific way, so just return
if (handleRMat == HandleRMat::SKIP) return;
GAPI_Assert(arg.index() == GRunArg::index_of<cv::RMat>());
bindRMat(mag, rc, util::get<cv::RMat>(arg), RMat::Access::R);
break;
}
case GShape::GSCALAR:
{
auto& mag_scalar = mag.template slot<cv::Scalar>()[rc.id];
@@ -159,32 +155,18 @@ void bindInArg(Mag& mag, const RcDesc &rc, const GRunArg &arg, bool is_umat)
}
}
void bindOutArg(Mag& mag, const RcDesc &rc, const GRunArgP &arg, bool is_umat)
void bindOutArg(Mag& mag, const RcDesc &rc, const GRunArgP &arg, HandleRMat handleRMat)
{
switch (rc.shape)
{
case GShape::GMAT:
{
switch (arg.index())
{
case GRunArgP::index_of<cv::Mat*>() :
if (is_umat)
{
#if !defined(GAPI_STANDALONE)
auto& mag_umat = mag.template slot<cv::UMat>()[rc.id];
mag_umat = util::get<cv::Mat*>(arg)->getUMat(ACCESS_RW);
#else
util::throw_error(std::logic_error("UMat is not supported in standalone build"));
#endif // !defined(GAPI_STANDALONE)
}
else
{
auto& mag_mat = mag.template slot<cv::Mat>()[rc.id];
mag_mat = *util::get<cv::Mat*>(arg);
}
break;
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
}
// In case of handleRMat == SKIP
// We assume that backend can work with some device-specific RMats
// and will handle them in some specific way, so just return
if (handleRMat == HandleRMat::SKIP) return;
GAPI_Assert(arg.index() == GRunArgP::index_of<cv::RMat*>());
bindRMat(mag, rc, *util::get<cv::RMat*>(arg), RMat::Access::W);
break;
}
@@ -248,7 +230,7 @@ cv::GRunArg getArg(const Mag& mag, const RcDesc &ref)
// Wrap associated CPU object (either host or an internal one)
switch (ref.shape)
{
case GShape::GMAT: return GRunArg(mag.template slot<cv::Mat>().at(ref.id));
case GShape::GMAT: return GRunArg(mag.template slot<cv::RMat>().at(ref.id));
case GShape::GSCALAR: return GRunArg(mag.template slot<cv::Scalar>().at(ref.id));
// Note: .at() is intentional for GArray and GOpaque as objects MUST be already there
// (and constructed by either bindIn/Out or resetInternal)
@@ -300,46 +282,15 @@ cv::GRunArgP getObjPtr(Mag& mag, const RcDesc &rc, bool is_umat)
}
}
void writeBack(const Mag& mag, const RcDesc &rc, GRunArgP &g_arg, bool is_umat)
void writeBack(const Mag& mag, const RcDesc &rc, GRunArgP &g_arg)
{
switch (rc.shape)
{
case GShape::GARRAY:
// Do nothing - should we really do anything here?
break;
case GShape::GOPAQUE:
// Do nothing - should we really do anything here?
break;
case GShape::GMAT:
{
//simply check that memory was not reallocated, i.e.
//both instances of Mat pointing to the same memory
uchar* out_arg_data = nullptr;
switch (g_arg.index())
{
case GRunArgP::index_of<cv::Mat*>() : out_arg_data = util::get<cv::Mat*>(g_arg)->data; break;
#if !defined(GAPI_STANDALONE)
case GRunArgP::index_of<cv::UMat*>() : out_arg_data = (util::get<cv::UMat*>(g_arg))->getMat(ACCESS_RW).data; break;
#endif // !defined(GAPI_STANDALONE)
default: util::throw_error(std::logic_error("content type of the runtime argument does not match to resource description ?"));
}
if (is_umat)
{
#if !defined(GAPI_STANDALONE)
auto& in_mag = mag.template slot<cv::UMat>().at(rc.id);
GAPI_Assert((out_arg_data == (in_mag.getMat(ACCESS_RW).data)) && " data for output parameters was reallocated ?");
#else
util::throw_error(std::logic_error("UMat is not supported in standalone build"));
#endif // !defined(GAPI_STANDALONE)
}
else
{
auto& in_mag = mag.template slot<cv::Mat>().at(rc.id);
GAPI_Assert((out_arg_data == in_mag.data) && " data for output parameters was reallocated ?");
}
case GShape::GOPAQUE:
// Do nothing - should we really do anything here?
break;
}
case GShape::GSCALAR:
{
@@ -357,6 +308,32 @@ void writeBack(const Mag& mag, const RcDesc &rc, GRunArgP &g_arg, bool is_umat)
}
}
void unbind(Mag& mag, const RcDesc &rc)
{
switch (rc.shape)
{
case GShape::GARRAY:
case GShape::GOPAQUE:
case GShape::GSCALAR:
// TODO: Do nothing - should we really do anything here?
break;
case GShape::GMAT:
// Clean-up everything - a cv::Mat, cv::RMat::View, a cv::UMat, and cv::RMat
// if applicable
mag.slot<cv::Mat>().erase(rc.id);
#if !defined(GAPI_STANDALONE)
mag.slot<cv::UMat>().erase(rc.id);
#endif
mag.slot<cv::RMat::View>().erase(rc.id);
mag.slot<cv::RMat>().erase(rc.id);
break;
default:
GAPI_Assert(false);
}
}
} // namespace magazine
void createMat(const cv::GMatDesc &desc, cv::Mat& mat)
+10
View File
@@ -95,6 +95,11 @@ cv::GMetaArgs cv::gapi::own::descrs_of(const std::vector<Mat> &vec)
return vec_descr_of(vec);
}
cv::GMatDesc cv::descr_of(const cv::RMat &mat)
{
return mat.desc();
}
namespace cv {
std::ostream& operator<<(std::ostream& os, const cv::GMatDesc &desc)
{
@@ -137,4 +142,9 @@ bool GMatDesc::canDescribe(const cv::Mat& mat) const
return canDescribeHelper(*this, mat);
}
bool GMatDesc::canDescribe(const cv::RMat& mat) const
{
return *this == mat.desc();
}
}// namespace cv
+7
View File
@@ -119,6 +119,9 @@ cv::GMetaArg cv::descr_of(const cv::GRunArg &arg)
case GRunArg::index_of<cv::gapi::wip::IStreamSource::Ptr>():
return cv::util::get<cv::gapi::wip::IStreamSource::Ptr>(arg)->descr_of();
case GRunArg::index_of<cv::RMat>():
return cv::GMetaArg(cv::util::get<cv::RMat>(arg).desc());
default: util::throw_error(std::logic_error("Unsupported GRunArg type"));
}
}
@@ -174,6 +177,8 @@ bool cv::can_describe(const GMetaArg& meta, const GRunArg& arg)
case GRunArg::index_of<cv::detail::VectorRef>(): return meta == cv::GMetaArg(util::get<cv::detail::VectorRef>(arg).descr_of());
case GRunArg::index_of<cv::detail::OpaqueRef>(): return meta == cv::GMetaArg(util::get<cv::detail::OpaqueRef>(arg).descr_of());
case GRunArg::index_of<cv::gapi::wip::IStreamSource::Ptr>(): return util::holds_alternative<GMatDesc>(meta); // FIXME(?) may be not the best option
case GRunArg::index_of<cv::RMat>(): return util::holds_alternative<GMatDesc>(meta) &&
util::get<GMatDesc>(meta).canDescribe(cv::util::get<cv::RMat>(arg));
default: util::throw_error(std::logic_error("Unsupported GRunArg type"));
}
}
@@ -263,6 +268,8 @@ const void* cv::gimpl::proto::ptr(const GRunArgP &arg)
return static_cast<const void*>(cv::util::get<cv::Mat*>(arg));
case GRunArgP::index_of<cv::Scalar*>():
return static_cast<const void*>(cv::util::get<cv::Scalar*>(arg));
case GRunArgP::index_of<cv::RMat*>():
return static_cast<const void*>(cv::util::get<cv::RMat*>(arg));
case GRunArgP::index_of<cv::detail::VectorRef>():
return cv::util::get<cv::detail::VectorRef>(arg).ptr();
case GRunArgP::index_of<cv::detail::OpaqueRef>():
+23
View File
@@ -0,0 +1,23 @@
// This file is part of OpenCV project.
// 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
#include <opencv2/gapi/rmat.hpp>
using View = cv::RMat::View;
// There is an issue with default generated operator=(View&&) on Mac:
// it doesn't nullify m_cb of a moved object
View& View::operator=(View&& v) {
m_desc = v.m_desc;
m_data = v.m_data;
m_step = v.m_step;
m_cb = v.m_cb;
v.m_desc = {};
v.m_data = nullptr;
v.m_step = 0u;
v.m_cb = nullptr;
return *this;
}