|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
#include <ade/util/zip_range.hpp>
|
|
|
|
|
|
#include <opencv2/gapi/opencv_includes.hpp>
|
|
|
|
|
|
#if !defined(GAPI_STANDALONE)
|
|
|
#include <opencv2/gapi/core.hpp>
|
|
|
#endif
|
|
|
|
|
|
#include "utils/itt.hpp"
|
|
|
|
|
|
#include "api/gproto_priv.hpp"
|
|
|
#include "compiler/passes/passes.hpp"
|
|
|
#include "backends/common/gbackend.hpp"
|
|
|
#include "backends/streaming/gstreamingbackend.hpp"
|
|
|
#include "compiler/gcompiler.hpp"
|
|
|
|
|
|
#include <logger.hpp>
|
|
|
|
|
|
#include "executor/gstreamingexecutor.hpp"
|
|
|
|
|
|
#include <opencv2/gapi/streaming/meta.hpp>
|
|
|
#include <opencv2/gapi/streaming/sync.hpp>
|
|
|
|
|
|
#include <opencv2/gapi/util/variant.hpp>
|
|
|
|
|
|
namespace
|
|
|
{
|
|
|
using namespace cv::gimpl::stream;
|
|
|
|
|
|
#if !defined(GAPI_STANDALONE)
|
|
|
class VideoEmitter final: public cv::gimpl::GIslandEmitter {
|
|
|
cv::gapi::wip::IStreamSource::Ptr src;
|
|
|
|
|
|
virtual void halt() override {
|
|
|
src->halt();
|
|
|
}
|
|
|
|
|
|
virtual bool pull(cv::GRunArg &arg) override {
|
|
|
|
|
|
|
|
|
|
|
|
cv::gapi::wip::Data newData;
|
|
|
if (!src->pull(newData)) {
|
|
|
return false;
|
|
|
}
|
|
|
arg = std::move(static_cast<cv::GRunArg&>(newData));
|
|
|
return true;
|
|
|
}
|
|
|
public:
|
|
|
explicit VideoEmitter(const cv::GRunArg &arg) {
|
|
|
src = cv::util::get<cv::gapi::wip::IStreamSource::Ptr>(arg);
|
|
|
}
|
|
|
};
|
|
|
#endif
|
|
|
|
|
|
class ConstEmitter final: public cv::gimpl::GIslandEmitter {
|
|
|
cv::GRunArg m_arg;
|
|
|
|
|
|
virtual void halt() override {
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual bool pull(cv::GRunArg &arg) override {
|
|
|
arg = const_cast<const cv::GRunArg&>(m_arg);
|
|
|
return true;
|
|
|
}
|
|
|
public:
|
|
|
|
|
|
explicit ConstEmitter(const cv::GRunArg &arg) : m_arg(arg) {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
struct DataQueue {
|
|
|
static const char *name() { return "StreamingDataQueue"; }
|
|
|
enum tag { DESYNC };
|
|
|
|
|
|
explicit DataQueue(std::size_t capacity) {
|
|
|
|
|
|
auto ptr = std::make_shared<cv::gimpl::stream::SyncQueue>();
|
|
|
if (capacity != 0) {
|
|
|
ptr->set_capacity(capacity);
|
|
|
}
|
|
|
q = std::move(ptr);
|
|
|
}
|
|
|
explicit DataQueue(tag t)
|
|
|
: q(new cv::gimpl::stream::DesyncQueue()) {
|
|
|
GAPI_Assert(t == DESYNC);
|
|
|
}
|
|
|
|
|
|
|
|
|
std::shared_ptr<cv::gimpl::stream::Q> q;
|
|
|
};
|
|
|
|
|
|
struct DesyncSpecialCase {
|
|
|
static const char *name() { return "DesyncSpecialCase"; }
|
|
|
};
|
|
|
|
|
|
std::vector<cv::gimpl::stream::Q*> reader_queues( ade::Graph &g,
|
|
|
const ade::NodeHandle &obj)
|
|
|
{
|
|
|
ade::TypedGraph<DataQueue> qgr(g);
|
|
|
std::vector<cv::gimpl::stream::Q*> result;
|
|
|
for (auto &&out_eh : obj->outEdges())
|
|
|
{
|
|
|
result.push_back(qgr.metadata(out_eh).get<DataQueue>().q.get());
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
std::vector<cv::gimpl::stream::Q*> input_queues( ade::Graph &g,
|
|
|
const ade::NodeHandle &obj)
|
|
|
{
|
|
|
ade::TypedGraph<DataQueue> qgr(g);
|
|
|
std::vector<cv::gimpl::stream::Q*> result;
|
|
|
for (auto &&in_eh : obj->inEdges())
|
|
|
{
|
|
|
result.push_back(qgr.metadata(in_eh).contains<DataQueue>()
|
|
|
? qgr.metadata(in_eh).get<DataQueue>().q.get()
|
|
|
: nullptr);
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
void sync_data(cv::GRunArgs &results, cv::GRunArgsP &outputs)
|
|
|
{
|
|
|
for (auto && it : ade::util::zip(ade::util::toRange(outputs),
|
|
|
ade::util::toRange(results)))
|
|
|
{
|
|
|
auto &out_obj = std::get<0>(it);
|
|
|
auto &res_obj = std::get<1>(it);
|
|
|
|
|
|
|
|
|
using T = cv::GRunArgP;
|
|
|
switch (out_obj.index())
|
|
|
{
|
|
|
case T::index_of<cv::Mat*>():
|
|
|
{
|
|
|
auto out_mat_p = cv::util::get<cv::Mat*>(out_obj);
|
|
|
auto view = cv::util::get<cv::RMat>(res_obj).access(cv::RMat::Access::R);
|
|
|
*out_mat_p = cv::gimpl::asMat(view).clone();
|
|
|
} break;
|
|
|
case T::index_of<cv::RMat*>():
|
|
|
*cv::util::get<cv::RMat*>(out_obj) = std::move(cv::util::get<cv::RMat>(res_obj));
|
|
|
break;
|
|
|
case T::index_of<cv::Scalar*>():
|
|
|
*cv::util::get<cv::Scalar*>(out_obj) = std::move(cv::util::get<cv::Scalar>(res_obj));
|
|
|
break;
|
|
|
case T::index_of<cv::detail::VectorRef>():
|
|
|
cv::util::get<cv::detail::VectorRef>(out_obj).mov(cv::util::get<cv::detail::VectorRef>(res_obj));
|
|
|
break;
|
|
|
case T::index_of<cv::detail::OpaqueRef>():
|
|
|
cv::util::get<cv::detail::OpaqueRef>(out_obj).mov(cv::util::get<cv::detail::OpaqueRef>(res_obj));
|
|
|
break;
|
|
|
case T::index_of<cv::MediaFrame*>():
|
|
|
*cv::util::get<cv::MediaFrame*>(out_obj) = std::move(cv::util::get<cv::MediaFrame>(res_obj));
|
|
|
break;
|
|
|
default:
|
|
|
GAPI_Error("This value type is not supported!");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
template<class C> using O = cv::util::optional<C>;
|
|
|
void sync_data(cv::gimpl::stream::Result &r, cv::GOptRunArgsP &outputs)
|
|
|
{
|
|
|
namespace own = cv::gapi::own;
|
|
|
|
|
|
for (auto && it : ade::util::zip(ade::util::toRange(outputs),
|
|
|
ade::util::toRange(r.args),
|
|
|
ade::util::toRange(r.flags)))
|
|
|
{
|
|
|
auto &out_obj = std::get<0>(it);
|
|
|
auto &res_obj = std::get<1>(it);
|
|
|
bool available = std::get<2>(it);
|
|
|
|
|
|
using T = cv::GOptRunArgP;
|
|
|
#define HANDLE_CASE(Type) \
|
|
|
case T::index_of<O<Type>*>(): \
|
|
|
if (available) { \
|
|
|
*cv::util::get<O<Type>*>(out_obj) \
|
|
|
= cv::util::make_optional(std::move(cv::util::get<Type>(res_obj))); \
|
|
|
} else { \
|
|
|
cv::util::get<O<Type>*>(out_obj)->reset(); \
|
|
|
}
|
|
|
|
|
|
|
|
|
switch (out_obj.index())
|
|
|
{
|
|
|
HANDLE_CASE(cv::Scalar); break;
|
|
|
HANDLE_CASE(cv::RMat); break;
|
|
|
HANDLE_CASE(cv::MediaFrame); break;
|
|
|
|
|
|
case T::index_of<O<cv::Mat>*>(): {
|
|
|
|
|
|
auto &mat_opt = *cv::util::get<O<cv::Mat>*>(out_obj);
|
|
|
if (available) {
|
|
|
auto q_map = cv::util::get<cv::RMat>(res_obj).access(cv::RMat::Access::R);
|
|
|
|
|
|
|
|
|
|
|
|
mat_opt = cv::util::make_optional(cv::gimpl::asMat(q_map).clone());
|
|
|
} else {
|
|
|
mat_opt.reset();
|
|
|
}
|
|
|
} break;
|
|
|
case T::index_of<cv::detail::OptionalVectorRef>(): {
|
|
|
|
|
|
auto &vec_opt = cv::util::get<cv::detail::OptionalVectorRef>(out_obj);
|
|
|
if (available) {
|
|
|
vec_opt.mov(cv::util::get<cv::detail::VectorRef>(res_obj));
|
|
|
} else {
|
|
|
vec_opt.reset();
|
|
|
}
|
|
|
} break;
|
|
|
case T::index_of<cv::detail::OptionalOpaqueRef>(): {
|
|
|
|
|
|
auto &opq_opt = cv::util::get<cv::detail::OptionalOpaqueRef>(out_obj);
|
|
|
if (available) {
|
|
|
opq_opt.mov(cv::util::get<cv::detail::OpaqueRef>(res_obj));
|
|
|
} else {
|
|
|
opq_opt.reset();
|
|
|
}
|
|
|
} break;
|
|
|
default:
|
|
|
|
|
|
GAPI_Error("This value type is not supported!");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
#undef HANDLE_CASE
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QueueReader
|
|
|
{
|
|
|
bool m_finishing = false;
|
|
|
std::vector<Cmd> m_cmd;
|
|
|
|
|
|
void rewindToStop(std::vector<Q*> &in_queues,
|
|
|
const std::size_t this_id);
|
|
|
|
|
|
public:
|
|
|
cv::gimpl::StreamMsg getInputVector (std::vector<Q*> &in_queues,
|
|
|
cv::GRunArgs &in_constants);
|
|
|
|
|
|
using V = cv::util::variant<cv::GRunArgs, Stop, cv::gimpl::Exception>;
|
|
|
V getResultsVector(std::vector<Q*> &in_queues,
|
|
|
const std::vector<int> &in_mapping,
|
|
|
const std::size_t out_size);
|
|
|
};
|
|
|
|
|
|
void rewindToStop(std::vector<Q*> &in_queues,
|
|
|
const std::size_t this_id)
|
|
|
{
|
|
|
size_t expected_stop_count = std::count_if(in_queues.begin(), in_queues.end(), [] (const Q* ptr) {
|
|
|
return ptr != nullptr;
|
|
|
});
|
|
|
|
|
|
if (expected_stop_count > 0) {
|
|
|
|
|
|
|
|
|
expected_stop_count--;
|
|
|
}
|
|
|
GAPI_LOG_DEBUG(nullptr, "id: " << this_id << ", queues count: " << in_queues.size() <<
|
|
|
", expected stop msg count: " << expected_stop_count);
|
|
|
size_t got_stop_count = 0;
|
|
|
while(got_stop_count < expected_stop_count) {
|
|
|
for (auto &&qit : ade::util::indexed(in_queues)) {
|
|
|
auto id2 = ade::util::index(qit);
|
|
|
auto &q2 = ade::util::value(qit);
|
|
|
if (this_id == id2) continue;
|
|
|
|
|
|
GAPI_LOG_DEBUG(nullptr, "drain next id: " << id2 <<
|
|
|
", stop count (" << got_stop_count << "/" <<
|
|
|
expected_stop_count << ")");
|
|
|
bool got_cmd = true;
|
|
|
while (q2 && got_cmd) {
|
|
|
Cmd cmd;
|
|
|
got_cmd = q2->try_pop(cmd);
|
|
|
if (got_cmd && cv::util::holds_alternative<Stop>(cmd)) {
|
|
|
got_stop_count ++;
|
|
|
GAPI_LOG_DEBUG(nullptr, "got stop from id: " << id2);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
GAPI_LOG_DEBUG(nullptr, "completed");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void QueueReader::rewindToStop(std::vector<Q*> &in_queues,
|
|
|
const std::size_t this_id)
|
|
|
{
|
|
|
::rewindToStop(in_queues, this_id);
|
|
|
}
|
|
|
|
|
|
cv::gimpl::StreamMsg QueueReader::getInputVector(std::vector<Q*> &in_queues,
|
|
|
cv::GRunArgs &in_constants)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
m_cmd.clear();
|
|
|
|
|
|
|
|
|
|
|
|
m_cmd.resize(in_queues.size());
|
|
|
cv::GRunArgs isl_inputs(in_queues.size());
|
|
|
|
|
|
cv::optional<cv::gimpl::Exception> exception;
|
|
|
for (auto &&it : ade::util::indexed(in_queues))
|
|
|
{
|
|
|
auto id = ade::util::index(it);
|
|
|
auto &q = ade::util::value(it);
|
|
|
|
|
|
if (q == nullptr)
|
|
|
{
|
|
|
GAPI_Assert(!in_constants.empty());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isl_inputs[id] = in_constants[id];
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
q->pop(m_cmd[id]);
|
|
|
switch (m_cmd[id].index())
|
|
|
{
|
|
|
case Cmd::index_of<cv::GRunArg>():
|
|
|
isl_inputs[id] = cv::util::get<cv::GRunArg>(m_cmd[id]);
|
|
|
break;
|
|
|
case Cmd::index_of<Stop>():
|
|
|
{
|
|
|
const auto &stop = cv::util::get<Stop>(m_cmd[id]);
|
|
|
if (stop.kind == Stop::Kind::CNST)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_finishing = true;
|
|
|
in_queues[id] = nullptr;
|
|
|
in_constants.resize(in_queues.size());
|
|
|
in_constants[id] = std::move(stop.cdata);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isl_inputs[id] = in_constants[id];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
GAPI_Assert(stop.kind == Stop::Kind::HARD);
|
|
|
rewindToStop(in_queues, id);
|
|
|
|
|
|
|
|
|
return cv::gimpl::StreamMsg{cv::gimpl::EndOfStream{}};
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
case Cmd::index_of<cv::gimpl::Exception>():
|
|
|
{
|
|
|
exception =
|
|
|
cv::util::make_optional(cv::util::get<cv::gimpl::Exception>(m_cmd[id]));
|
|
|
break;
|
|
|
}
|
|
|
default:
|
|
|
GAPI_Error("Unsupported cmd type in getInputVector()");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (exception.has_value()) {
|
|
|
return cv::gimpl::StreamMsg{exception.value()};
|
|
|
}
|
|
|
|
|
|
if (m_finishing)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (ade::util::all_of(in_queues, [](Q *ptr){return ptr == nullptr;})) {
|
|
|
return cv::gimpl::StreamMsg{cv::gimpl::EndOfStream{}};
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (auto& arg : isl_inputs) {
|
|
|
if (arg.index() == cv::GRunArg::index_of<cv::Mat>()) {
|
|
|
arg = cv::GRunArg{ cv::make_rmat<cv::gimpl::RMatOnMat>(cv::util::get<cv::Mat>(arg))
|
|
|
, arg.meta
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
return cv::gimpl::StreamMsg{std::move(isl_inputs)};
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QueueReader::V QueueReader::getResultsVector(std::vector<Q*> &in_queues,
|
|
|
const std::vector<int> &in_mapping,
|
|
|
const std::size_t out_size)
|
|
|
{
|
|
|
cv::GRunArgs out_results(out_size);
|
|
|
m_cmd.resize(out_size);
|
|
|
cv::optional<cv::gimpl::Exception> exception;
|
|
|
for (auto &&it : ade::util::indexed(in_queues))
|
|
|
{
|
|
|
auto ii = ade::util::index(it);
|
|
|
auto oi = in_mapping[ii];
|
|
|
auto &q = ade::util::value(it);
|
|
|
q->pop(m_cmd[oi]);
|
|
|
|
|
|
switch (m_cmd[oi].index()) {
|
|
|
case Cmd::index_of<cv::GRunArg>():
|
|
|
out_results[oi] = std::move(cv::util::get<cv::GRunArg>(m_cmd[oi]));
|
|
|
break;
|
|
|
case Cmd::index_of<Stop>():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rewindToStop(in_queues, ii);
|
|
|
return QueueReader::V(Stop{});
|
|
|
case Cmd::index_of<cv::gimpl::Exception>():
|
|
|
exception =
|
|
|
cv::util::make_optional(cv::util::get<cv::gimpl::Exception>(m_cmd[oi]));
|
|
|
break;
|
|
|
default:
|
|
|
cv::util::throw_error(
|
|
|
std::logic_error("Unexpected cmd kind in getResultsVector"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (exception.has_value()) {
|
|
|
return QueueReader::V(exception.value());
|
|
|
}
|
|
|
|
|
|
return QueueReader::V(out_results);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void emitterActorThread(std::shared_ptr<cv::gimpl::GIslandEmitter> emitter,
|
|
|
Q& in_queue,
|
|
|
std::vector<Q*> out_queues,
|
|
|
std::function<void()> cb_completion)
|
|
|
{
|
|
|
|
|
|
|
|
|
Cmd cmd;
|
|
|
in_queue.pop(cmd);
|
|
|
GAPI_Assert( cv::util::holds_alternative<Start>(cmd)
|
|
|
|| cv::util::holds_alternative<Stop>(cmd));
|
|
|
if (cv::util::holds_alternative<Stop>(cmd))
|
|
|
{
|
|
|
for (auto &&oq : out_queues) {
|
|
|
oq->push(cmd);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(emitter_hndl, "emitter");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(emitter_pull_hndl, "emitter_pull");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(emitter_push_hndl, "emitter_push");
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(emitter_hndl);
|
|
|
|
|
|
Cmd cancel;
|
|
|
if (in_queue.try_pop(cancel))
|
|
|
{
|
|
|
|
|
|
GAPI_Assert(cv::util::holds_alternative<Stop>(cancel));
|
|
|
|
|
|
for (auto &&oq : out_queues) oq->push(cancel);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
cv::GRunArg data;
|
|
|
|
|
|
bool result = false;
|
|
|
try {
|
|
|
result = [&](){
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(emitter_pull_hndl);
|
|
|
return emitter->pull(data);
|
|
|
}();
|
|
|
} catch (...) {
|
|
|
auto eptr = std::current_exception();
|
|
|
for (auto &&oq : out_queues)
|
|
|
{
|
|
|
oq->push(Cmd{cv::gimpl::Exception{eptr}});
|
|
|
}
|
|
|
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (result)
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(emitter_push_hndl);
|
|
|
|
|
|
for (auto &&oq : out_queues)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
const auto tmp = data;
|
|
|
oq->push(Cmd{tmp});
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
|
|
|
for (auto &&oq : out_queues) oq->push(Cmd{Stop{}});
|
|
|
if (cb_completion) cb_completion();
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void syncActorThread(std::vector<Q*> in_queues,
|
|
|
std::vector<std::vector<Q*>> out_queues) {
|
|
|
using timestamp_t = int64_t;
|
|
|
std::vector<bool> pop_nexts(in_queues.size());
|
|
|
std::vector<Cmd> cmds(in_queues.size());
|
|
|
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(sync_hndl, "sync_actor");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(sync_pull_1_queue_hndl, "sync_actor_pull_from_1_queue");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(sync_push_hndl, "sync_actor_push");
|
|
|
while (true) {
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(sync_hndl);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto&& p : pop_nexts) {
|
|
|
p = true;
|
|
|
}
|
|
|
timestamp_t max_ts = 0u;
|
|
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
for (auto&& it : ade::util::indexed(
|
|
|
ade::util::zip(pop_nexts, in_queues, cmds))) {
|
|
|
auto& val = ade::util::value(it);
|
|
|
auto& pop_next = std::get<0>(val);
|
|
|
if (!pop_next) {
|
|
|
continue;
|
|
|
}
|
|
|
auto& q = std::get<1>(val);
|
|
|
auto& cmd = std::get<2>(val);
|
|
|
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(sync_pull_1_queue_hndl);
|
|
|
q->pop(cmd);
|
|
|
}
|
|
|
if (cv::util::holds_alternative<Stop>(cmd)) {
|
|
|
|
|
|
|
|
|
|
|
|
rewindToStop(in_queues, ade::util::index(it));
|
|
|
for (auto &&oqs : out_queues) {
|
|
|
for (auto &&oq : oqs) {
|
|
|
oq->push(Cmd{Stop{}});
|
|
|
}
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
auto& arg = cv::util::get<cv::GRunArg>(cmd);
|
|
|
auto ts = cv::util::any_cast<int64_t>(arg.meta[cv::gapi::streaming::meta_tag::timestamp]);
|
|
|
GAPI_Assert(ts >= 0u);
|
|
|
|
|
|
|
|
|
|
|
|
if (ts < max_ts) {
|
|
|
|
|
|
pop_next = true;
|
|
|
} else if (ts == max_ts) {
|
|
|
|
|
|
pop_next = false;
|
|
|
} else if (ts > max_ts) {
|
|
|
|
|
|
|
|
|
|
|
|
max_ts = ts;
|
|
|
for (auto&& p : pop_nexts) {
|
|
|
p = true;
|
|
|
}
|
|
|
pop_next = false;
|
|
|
}
|
|
|
}
|
|
|
} while (ade::util::any_of(pop_nexts, [](bool v){ return v; }));
|
|
|
|
|
|
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(sync_push_hndl);
|
|
|
for (auto &&it : ade::util::zip(out_queues, cmds)) {
|
|
|
for (auto &&q : std::get<0>(it)) {
|
|
|
q->push(std::get<1>(it));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class StreamingInput final: public cv::gimpl::GIslandExecutable::IInput
|
|
|
{
|
|
|
QueueReader &qr;
|
|
|
std::vector<Q*> &in_queues;
|
|
|
cv::GRunArgs &in_constants;
|
|
|
|
|
|
cv::optional<cv::gimpl::StreamMsg> last_read_msg;
|
|
|
|
|
|
virtual cv::gimpl::StreamMsg try_get() override
|
|
|
{
|
|
|
|
|
|
return get();
|
|
|
}
|
|
|
public:
|
|
|
explicit StreamingInput(QueueReader &rdr,
|
|
|
std::vector<Q*> &inq,
|
|
|
cv::GRunArgs &inc,
|
|
|
const std::vector<cv::gimpl::RcDesc> &in_descs)
|
|
|
: qr(rdr), in_queues(inq), in_constants(inc)
|
|
|
{
|
|
|
set(in_descs);
|
|
|
}
|
|
|
|
|
|
const cv::gimpl::StreamMsg& read()
|
|
|
{
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(inputs_get_hndl, "StreamingInput::read");
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(inputs_get_hndl);
|
|
|
|
|
|
last_read_msg =
|
|
|
cv::optional<cv::gimpl::StreamMsg>(
|
|
|
qr.getInputVector(in_queues, in_constants));
|
|
|
return last_read_msg.value();
|
|
|
}
|
|
|
|
|
|
virtual cv::gimpl::StreamMsg get() override
|
|
|
{
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(inputs_get_hndl, "StreamingInput::get");
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(inputs_get_hndl);
|
|
|
|
|
|
if (!last_read_msg.has_value()) {
|
|
|
(void)read();
|
|
|
}
|
|
|
auto msg = std::move(last_read_msg.value());
|
|
|
last_read_msg = cv::optional<cv::gimpl::StreamMsg>();
|
|
|
return msg;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
class StreamingOutput final: public cv::gimpl::GIslandExecutable::IOutput
|
|
|
{
|
|
|
|
|
|
struct Posting
|
|
|
{
|
|
|
using V = cv::util::variant<cv::GRunArg,
|
|
|
cv::gimpl::EndOfStream,
|
|
|
cv::gimpl::Exception>;
|
|
|
V data;
|
|
|
bool ready = false;
|
|
|
};
|
|
|
using PostingList = std::list<Posting>;
|
|
|
std::vector<PostingList> m_postings;
|
|
|
std::unordered_map< const void*
|
|
|
, std::pair<int, PostingList::iterator>
|
|
|
> m_postIdx;
|
|
|
std::size_t m_stops_sent = 0u;
|
|
|
|
|
|
|
|
|
const cv::GMetaArgs &m_metas;
|
|
|
std::vector< std::vector<Q*> > &m_out_queues;
|
|
|
std::shared_ptr<cv::gimpl::GIslandExecutable> m_island;
|
|
|
|
|
|
|
|
|
|
|
|
mutable std::mutex m_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
virtual cv::GRunArgP get(int idx) override
|
|
|
{
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(outputs_get_hndl, "StreamingOutput::get (alloc)");
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(outputs_get_hndl);
|
|
|
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
|
|
|
using MatType = cv::Mat;
|
|
|
using SclType = cv::Scalar;
|
|
|
|
|
|
|
|
|
auto iter = m_postings[idx].insert(m_postings[idx].end(), Posting{});
|
|
|
const auto r = desc()[idx];
|
|
|
cv::GRunArg& out_arg = cv::util::get<cv::GRunArg>(iter->data);
|
|
|
cv::GRunArgP ret_val;
|
|
|
switch (r.shape) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case cv::GShape::GMAT:
|
|
|
{
|
|
|
auto desc = cv::util::get<cv::GMatDesc>(m_metas[idx]);
|
|
|
if (m_island->allocatesOutputs())
|
|
|
{
|
|
|
out_arg = cv::GRunArg(m_island->allocate(desc));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MatType newMat;
|
|
|
cv::gimpl::createMat(desc, newMat);
|
|
|
auto rmat = cv::make_rmat<cv::gimpl::RMatOnMat>(newMat);
|
|
|
out_arg = cv::GRunArg(std::move(rmat));
|
|
|
}
|
|
|
ret_val = cv::GRunArgP(&cv::util::get<cv::RMat>(out_arg));
|
|
|
}
|
|
|
break;
|
|
|
case cv::GShape::GSCALAR:
|
|
|
{
|
|
|
SclType newScl;
|
|
|
out_arg = cv::GRunArg(std::move(newScl));
|
|
|
ret_val = cv::GRunArgP(&cv::util::get<SclType>(out_arg));
|
|
|
}
|
|
|
break;
|
|
|
case cv::GShape::GARRAY:
|
|
|
{
|
|
|
cv::detail::VectorRef newVec;
|
|
|
cv::util::get<cv::detail::ConstructVec>(r.ctor)(newVec);
|
|
|
out_arg = cv::GRunArg(std::move(newVec));
|
|
|
|
|
|
|
|
|
const auto &rr = cv::util::get<cv::detail::VectorRef>(out_arg);
|
|
|
ret_val = cv::GRunArgP(rr);
|
|
|
}
|
|
|
break;
|
|
|
case cv::GShape::GOPAQUE:
|
|
|
{
|
|
|
cv::detail::OpaqueRef newOpaque;
|
|
|
cv::util::get<cv::detail::ConstructOpaque>(r.ctor)(newOpaque);
|
|
|
out_arg = cv::GRunArg(std::move(newOpaque));
|
|
|
|
|
|
|
|
|
const auto &rr = cv::util::get<cv::detail::OpaqueRef>(out_arg);
|
|
|
ret_val = cv::GRunArgP(rr);
|
|
|
}
|
|
|
break;
|
|
|
case cv::GShape::GFRAME:
|
|
|
{
|
|
|
cv::MediaFrame frame;
|
|
|
out_arg = cv::GRunArg(std::move(frame));
|
|
|
ret_val = cv::GRunArgP(&cv::util::get<cv::MediaFrame>(out_arg));
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
cv::util::throw_error(std::logic_error("Unsupported GShape"));
|
|
|
}
|
|
|
m_postIdx[cv::gimpl::proto::ptr(ret_val)] = std::make_pair(idx, iter);
|
|
|
return ret_val;
|
|
|
}
|
|
|
|
|
|
virtual void post(cv::GRunArgP&& argp, const std::exception_ptr& exptr) override
|
|
|
{
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(outputs_post_hndl, "StreamingOutput::post");
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(outputs_post_hndl);
|
|
|
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
|
|
|
|
|
|
|
|
|
auto it = m_postIdx.find(cv::gimpl::proto::ptr(argp));
|
|
|
GAPI_Assert(it != m_postIdx.end());
|
|
|
const int out_idx = it->second.first;
|
|
|
const auto out_iter = it->second.second;
|
|
|
out_iter->ready = true;
|
|
|
if (exptr) {
|
|
|
out_iter->data = cv::gimpl::Exception{exptr};
|
|
|
}
|
|
|
m_postIdx.erase(it);
|
|
|
if (out_iter != m_postings[out_idx].begin())
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
GAPI_Assert(out_iter == m_postings[out_idx].begin());
|
|
|
auto post_iter = m_postings[out_idx].begin();
|
|
|
while (post_iter != m_postings[out_idx].end() && post_iter->ready == true)
|
|
|
{
|
|
|
Cmd cmd;
|
|
|
switch (post_iter->data.index())
|
|
|
{
|
|
|
case Posting::V::index_of<cv::GRunArg>():
|
|
|
cmd = Cmd{cv::util::get<cv::GRunArg>(post_iter->data)};
|
|
|
break;
|
|
|
case Posting::V::index_of<cv::gimpl::Exception>():
|
|
|
cmd = Cmd{cv::util::get<cv::gimpl::Exception>(post_iter->data)};
|
|
|
break;
|
|
|
case Posting::V::index_of<cv::gimpl::EndOfStream>():
|
|
|
cmd = Cmd{Stop{}};
|
|
|
m_stops_sent++;
|
|
|
break;
|
|
|
default:
|
|
|
GAPI_Error("Unreachable code");
|
|
|
}
|
|
|
|
|
|
for (auto &&q : m_out_queues[out_idx])
|
|
|
{
|
|
|
q->push(cmd);
|
|
|
}
|
|
|
post_iter = m_postings[out_idx].erase(post_iter);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
virtual void post(cv::gimpl::EndOfStream&&) override
|
|
|
{
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
|
|
|
|
|
|
for (auto &&it : ade::util::indexed(m_postings))
|
|
|
{
|
|
|
const auto idx = ade::util::index(it);
|
|
|
auto &lst = ade::util::value(it);
|
|
|
if (lst.empty())
|
|
|
{
|
|
|
for (auto &&q : m_out_queues[idx])
|
|
|
{
|
|
|
q->push(Cmd(Stop{}));
|
|
|
}
|
|
|
m_stops_sent++;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Posting p;
|
|
|
p.data = Posting::V{cv::gimpl::EndOfStream{}};
|
|
|
p.ready = true;
|
|
|
lst.push_back(std::move(p));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void meta(const cv::GRunArgP &out, const cv::GRunArg::Meta &m) override
|
|
|
{
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
const auto it = m_postIdx.find(cv::gimpl::proto::ptr(out));
|
|
|
GAPI_Assert(it != m_postIdx.end());
|
|
|
|
|
|
const auto out_iter = it->second.second;
|
|
|
cv::util::get<cv::GRunArg>(out_iter->data).meta = m;
|
|
|
}
|
|
|
|
|
|
public:
|
|
|
explicit StreamingOutput(const cv::GMetaArgs &metas,
|
|
|
std::vector< std::vector<Q*> > &out_queues,
|
|
|
const std::vector<cv::gimpl::RcDesc> &out_descs,
|
|
|
std::shared_ptr<cv::gimpl::GIslandExecutable> island)
|
|
|
: m_metas(metas)
|
|
|
, m_out_queues(out_queues)
|
|
|
, m_island(island)
|
|
|
{
|
|
|
set(out_descs);
|
|
|
m_postings.resize(out_descs.size());
|
|
|
}
|
|
|
|
|
|
bool done() const
|
|
|
{
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
|
|
|
|
|
|
return m_stops_sent == desc().size();
|
|
|
}
|
|
|
|
|
|
virtual void post(cv::gimpl::Exception&& error) override
|
|
|
{
|
|
|
std::lock_guard<std::mutex> lock{m_mutex};
|
|
|
|
|
|
|
|
|
for (auto &&it : ade::util::indexed(m_postings))
|
|
|
{
|
|
|
const auto idx = ade::util::index(it);
|
|
|
auto &lst = ade::util::value(it);
|
|
|
if (lst.empty())
|
|
|
{
|
|
|
for (auto &&q : m_out_queues[idx])
|
|
|
{
|
|
|
q->push(Cmd(std::move(error)));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
Posting p;
|
|
|
p.data = Posting::V{std::move(error)};
|
|
|
p.ready = true;
|
|
|
lst.push_back(std::move(p));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void islandActorThread(std::vector<cv::gimpl::RcDesc> in_rcs,
|
|
|
std::vector<cv::gimpl::RcDesc> out_rcs,
|
|
|
cv::GMetaArgs out_metas,
|
|
|
std::shared_ptr<cv::gimpl::GIslandExecutable> island_exec,
|
|
|
std::vector<Q*> in_queues,
|
|
|
cv::GRunArgs in_constants,
|
|
|
std::vector< std::vector<Q*> > out_queues,
|
|
|
const std::string& island_meta_info)
|
|
|
{
|
|
|
GAPI_Assert(in_queues.size() == in_rcs.size());
|
|
|
GAPI_Assert(out_queues.size() == out_rcs.size());
|
|
|
GAPI_Assert(out_queues.size() == out_metas.size());
|
|
|
QueueReader qr;
|
|
|
StreamingInput input(qr, in_queues, in_constants, in_rcs);
|
|
|
StreamingOutput output(out_metas, out_queues, out_rcs, island_exec);
|
|
|
|
|
|
GAPI_ITT_DYNAMIC_LOCAL_HANDLE(island_hndl, island_meta_info.c_str());
|
|
|
while (!output.done())
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(island_hndl);
|
|
|
|
|
|
|
|
|
if (cv::util::holds_alternative<cv::gimpl::Exception>(input.read()))
|
|
|
{
|
|
|
auto in_msg = input.get();
|
|
|
output.post(std::move(cv::util::get<cv::gimpl::Exception>(in_msg)));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
island_exec->run(input, output);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void collectorThread(std::vector<Q*> in_queues,
|
|
|
std::vector<int> in_mapping,
|
|
|
const std::size_t out_size,
|
|
|
const bool handle_stop,
|
|
|
Q& out_queue)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<bool> flags(out_size, false);
|
|
|
for (auto idx : in_mapping) {
|
|
|
flags[idx] = true;
|
|
|
}
|
|
|
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(collector_hndl, "collector");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(collector_get_results_hndl, "collector_get_results");
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(collector_push_hndl, "collector_push");
|
|
|
|
|
|
QueueReader qr;
|
|
|
while (true)
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(collector_hndl);
|
|
|
|
|
|
const auto result = [&](){
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(collector_get_results_hndl);
|
|
|
return qr.getResultsVector(in_queues, in_mapping, out_size);
|
|
|
}();
|
|
|
|
|
|
switch (result.index())
|
|
|
{
|
|
|
case QueueReader::V::index_of<cv::GRunArgs>():
|
|
|
{
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(collector_push_hndl);
|
|
|
auto this_result = cv::util::get<cv::GRunArgs>(result);
|
|
|
out_queue.push(Cmd{Result{std::move(this_result), flags}});
|
|
|
break;
|
|
|
}
|
|
|
case QueueReader::V::index_of<Stop>():
|
|
|
if (handle_stop)
|
|
|
{
|
|
|
out_queue.push(Cmd{Stop{}});
|
|
|
}
|
|
|
|
|
|
return;
|
|
|
case QueueReader::V::index_of<cv::gimpl::Exception>():
|
|
|
out_queue.push(Cmd{cv::util::get<cv::gimpl::Exception>(result)});
|
|
|
break;
|
|
|
default:
|
|
|
GAPI_Error("Unreachable code");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void check_DesyncObjectConsumedByMultipleIslands(const cv::gimpl::GIslandModel::Graph &gim) {
|
|
|
using namespace cv::gimpl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &&nh : gim.nodes()) {
|
|
|
if (gim.metadata(nh).get<NodeKind>().k == NodeKind::SLOT) {
|
|
|
|
|
|
|
|
|
std::unordered_map<int, GIsland*> out_desync_islands;
|
|
|
for (auto &&out_eh : nh->outEdges()) {
|
|
|
if (gim.metadata(out_eh).contains<DesyncIslEdge>()) {
|
|
|
|
|
|
|
|
|
const auto out_desync_idx = gim.metadata(out_eh)
|
|
|
.get<DesyncIslEdge>().index;
|
|
|
const auto out_island = gim.metadata(out_eh->dstNode())
|
|
|
.get<FusedIsland>().object;
|
|
|
|
|
|
auto it = out_desync_islands.find(out_desync_idx);
|
|
|
if (it != out_desync_islands.end()) {
|
|
|
|
|
|
|
|
|
GAPI_Assert(it->second == out_island.get()
|
|
|
&& "A single desync object may only be used by a single island!");
|
|
|
} else {
|
|
|
|
|
|
out_desync_islands[out_desync_idx] = out_island.get();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void constructOptGraphOutputs(const cv::GTypesInfo &out_info,
|
|
|
cv::GOptRunArgs &args,
|
|
|
cv::GOptRunArgsP &outs)
|
|
|
{
|
|
|
for (auto&& info : out_info)
|
|
|
{
|
|
|
switch (info.shape)
|
|
|
{
|
|
|
case cv::GShape::GMAT:
|
|
|
{
|
|
|
args.emplace_back(cv::optional<cv::Mat>{});
|
|
|
outs.emplace_back(&cv::util::get<cv::optional<cv::Mat>>(args.back()));
|
|
|
break;
|
|
|
}
|
|
|
case cv::GShape::GSCALAR:
|
|
|
{
|
|
|
args.emplace_back(cv::optional<cv::Scalar>{});
|
|
|
outs.emplace_back(&cv::util::get<cv::optional<cv::Scalar>>(args.back()));
|
|
|
break;
|
|
|
}
|
|
|
case cv::GShape::GARRAY:
|
|
|
{
|
|
|
cv::detail::VectorRef ref;
|
|
|
cv::util::get<cv::detail::ConstructVec>(info.ctor)(ref);
|
|
|
args.emplace_back(cv::util::make_optional(std::move(ref)));
|
|
|
outs.emplace_back(wrap_opt_arg(cv::util::get<cv::optional<cv::detail::VectorRef>>(args.back())));
|
|
|
break;
|
|
|
}
|
|
|
case cv::GShape::GOPAQUE:
|
|
|
{
|
|
|
cv::detail::OpaqueRef ref;
|
|
|
cv::util::get<cv::detail::ConstructOpaque>(info.ctor)(ref);
|
|
|
args.emplace_back(cv::util::make_optional(std::move(ref)));
|
|
|
outs.emplace_back(wrap_opt_arg(cv::util::get<cv::optional<cv::detail::OpaqueRef>>(args.back())));
|
|
|
break;
|
|
|
}
|
|
|
default:
|
|
|
cv::util::throw_error(std::logic_error("Unsupported optional output shape for Python"));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
class cv::gimpl::GStreamingExecutor::Synchronizer final {
|
|
|
gapi::streaming::sync_policy m_sync_policy = gapi::streaming::sync_policy::dont_sync;
|
|
|
ade::Graph& m_island_graph;
|
|
|
cv::gimpl::GIslandModel::Graph m_gim;
|
|
|
std::size_t m_queue_capacity = 0u;
|
|
|
std::thread m_thread;
|
|
|
|
|
|
std::vector<ade::NodeHandle> m_synchronized_emitters;
|
|
|
std::vector<stream::SyncQueue> m_sync_queues;
|
|
|
|
|
|
std::vector<stream::Q*> newSyncQueue() {
|
|
|
m_sync_queues.emplace_back(SyncQueue{});
|
|
|
m_sync_queues.back().set_capacity(m_queue_capacity);
|
|
|
return std::vector<Q*>{&m_sync_queues.back()};
|
|
|
}
|
|
|
public:
|
|
|
Synchronizer(gapi::streaming::sync_policy sync_policy,
|
|
|
ade::Graph& island_graph,
|
|
|
std::size_t queue_capacity)
|
|
|
: m_sync_policy(sync_policy)
|
|
|
, m_island_graph(island_graph)
|
|
|
, m_gim(m_island_graph)
|
|
|
, m_queue_capacity(queue_capacity) {
|
|
|
}
|
|
|
|
|
|
void registerVideoEmitters(std::vector<ade::NodeHandle>&& emitters) {
|
|
|
|
|
|
|
|
|
if ( m_sync_policy == cv::gapi::streaming::sync_policy::drop
|
|
|
&& emitters.size() > 1u) {
|
|
|
m_synchronized_emitters = std::move(emitters);
|
|
|
m_sync_queues.reserve(m_synchronized_emitters.size());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
std::vector<stream::Q*> outQueues(const ade::NodeHandle& emitter) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return m_synchronized_emitters.end() != std::find(m_synchronized_emitters.begin(),
|
|
|
m_synchronized_emitters.end(),
|
|
|
emitter)
|
|
|
? newSyncQueue()
|
|
|
: reader_queues(m_island_graph, emitter->outNodes().front());
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void start() {
|
|
|
if (m_synchronized_emitters.size() != 0) {
|
|
|
GAPI_Assert(m_synchronized_emitters.size() > 1u);
|
|
|
std::vector<Q*> sync_in_queues(m_synchronized_emitters.size());
|
|
|
std::vector<std::vector<Q*>> sync_out_queues(m_synchronized_emitters.size());
|
|
|
for (auto it : ade::util::indexed(m_synchronized_emitters)) {
|
|
|
const auto id = ade::util::index(it);
|
|
|
const auto eh = ade::util::value(it);
|
|
|
sync_in_queues[id] = &m_sync_queues[id];
|
|
|
sync_out_queues[id] = reader_queues(m_island_graph, eh->outNodes().front());
|
|
|
}
|
|
|
m_thread = std::thread(syncActorThread,
|
|
|
std::move(sync_in_queues),
|
|
|
std::move(sync_out_queues));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void join() {
|
|
|
if (m_synchronized_emitters.size() != 0) {
|
|
|
m_thread.join();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void clear() {
|
|
|
for (auto &q : m_sync_queues) q.clear();
|
|
|
m_sync_queues.clear();
|
|
|
m_synchronized_emitters.clear();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
cv::gimpl::GStreamingExecutor::GStreamingExecutor(std::unique_ptr<ade::Graph> &&g_model,
|
|
|
const GCompileArgs &comp_args)
|
|
|
: GAbstractStreamingExecutor(std::move(g_model), comp_args)
|
|
|
{
|
|
|
GModel::Graph gm(*m_orig_graph);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_desync) {
|
|
|
check_DesyncObjectConsumedByMultipleIslands(m_gim);
|
|
|
}
|
|
|
|
|
|
const auto proto = gm.metadata().get<Protocol>();
|
|
|
m_emitters .resize(proto.in_nhs.size());
|
|
|
m_emitter_queues.resize(proto.in_nhs.size());
|
|
|
m_sinks .resize(proto.out_nhs.size());
|
|
|
m_sink_queues .resize(proto.out_nhs.size(), nullptr);
|
|
|
m_sink_sync .resize(proto.out_nhs.size(), -1);
|
|
|
|
|
|
|
|
|
|
|
|
auto has_queue_capacity = cv::gapi::getCompileArg<cv::gapi::streaming::queue_capacity>(m_comp_args);
|
|
|
const auto queue_capacity = has_queue_capacity ? has_queue_capacity->capacity :
|
|
|
3*std::count_if
|
|
|
(m_gim.nodes().begin(),
|
|
|
m_gim.nodes().end(),
|
|
|
[&](ade::NodeHandle nh) {
|
|
|
return m_gim.metadata(nh).get<NodeKind>().k == NodeKind::ISLAND;
|
|
|
});
|
|
|
GAPI_Assert(queue_capacity != 0u);
|
|
|
|
|
|
auto sync_policy = cv::gimpl::getCompileArg<cv::gapi::streaming::sync_policy>(m_comp_args)
|
|
|
.value_or(cv::gapi::streaming::sync_policy::dont_sync);
|
|
|
m_sync.reset(new Synchronizer(sync_policy, *m_island_graph, queue_capacity));
|
|
|
|
|
|
|
|
|
|
|
|
const bool islands_compiled = m_gim.metadata().contains<IslandsCompiled>();
|
|
|
|
|
|
auto sorted = m_gim.metadata().get<ade::passes::TopologicalSortData>();
|
|
|
for (auto nh : sorted.nodes())
|
|
|
{
|
|
|
switch (m_gim.metadata(nh).get<NodeKind>().k)
|
|
|
{
|
|
|
case NodeKind::ISLAND:
|
|
|
{
|
|
|
std::vector<RcDesc> input_rcs;
|
|
|
std::vector<RcDesc> output_rcs;
|
|
|
std::vector<GRunArg> in_constants;
|
|
|
cv::GMetaArgs output_metas;
|
|
|
input_rcs.reserve(nh->inNodes().size());
|
|
|
in_constants.reserve(nh->inNodes().size());
|
|
|
output_rcs.reserve(nh->outNodes().size());
|
|
|
output_metas.reserve(nh->outNodes().size());
|
|
|
|
|
|
std::unordered_set<ade::NodeHandle, ade::HandleHasher<ade::Node> > const_ins;
|
|
|
|
|
|
|
|
|
|
|
|
auto xtract_in = [&](ade::NodeHandle slot_nh, std::vector<RcDesc> &vec)
|
|
|
{
|
|
|
const auto orig_data_nh
|
|
|
= m_gim.metadata(slot_nh).get<DataSlot>().original_data_node;
|
|
|
const auto &orig_data_info
|
|
|
= gm.metadata(orig_data_nh).get<Data>();
|
|
|
if (orig_data_info.storage == Data::Storage::CONST_VAL) {
|
|
|
const_ins.insert(slot_nh);
|
|
|
|
|
|
in_constants.push_back(const_cast<const cv::GRunArg&>(gm.metadata(orig_data_nh).get<ConstValue>().arg));
|
|
|
} else in_constants.push_back(cv::GRunArg{});
|
|
|
if (orig_data_info.shape == GShape::GARRAY) {
|
|
|
|
|
|
GAPI_Assert(!cv::util::holds_alternative<cv::util::monostate>(orig_data_info.ctor));
|
|
|
}
|
|
|
vec.emplace_back(RcDesc{ orig_data_info.rc
|
|
|
, orig_data_info.shape
|
|
|
, orig_data_info.ctor});
|
|
|
};
|
|
|
auto xtract_out = [&](ade::NodeHandle slot_nh, std::vector<RcDesc> &vec, cv::GMetaArgs &metas)
|
|
|
{
|
|
|
const auto orig_data_nh
|
|
|
= m_gim.metadata(slot_nh).get<DataSlot>().original_data_node;
|
|
|
const auto &orig_data_info
|
|
|
= gm.metadata(orig_data_nh).get<Data>();
|
|
|
if (orig_data_info.shape == GShape::GARRAY) {
|
|
|
|
|
|
GAPI_Assert(!cv::util::holds_alternative<cv::util::monostate>(orig_data_info.ctor));
|
|
|
}
|
|
|
vec.emplace_back(RcDesc{ orig_data_info.rc
|
|
|
, orig_data_info.shape
|
|
|
, orig_data_info.ctor});
|
|
|
metas.emplace_back(orig_data_info.meta);
|
|
|
};
|
|
|
|
|
|
for (auto in_slot_nh : nh->inNodes()) xtract_in(in_slot_nh, input_rcs);
|
|
|
for (auto out_slot_nh : nh->outNodes()) xtract_out(out_slot_nh, output_rcs, output_metas);
|
|
|
|
|
|
std::shared_ptr<GIslandExecutable> isl_exec = islands_compiled
|
|
|
? m_gim.metadata(nh).get<IslandExec>().object
|
|
|
: nullptr;
|
|
|
m_ops.emplace_back(OpDesc{ std::move(input_rcs)
|
|
|
, std::move(output_rcs)
|
|
|
, std::move(output_metas)
|
|
|
, nh
|
|
|
, in_constants
|
|
|
, isl_exec
|
|
|
});
|
|
|
|
|
|
ade::TypedGraph<DataQueue, DesyncSpecialCase> qgr(*m_island_graph);
|
|
|
bool is_desync_start = false;
|
|
|
for (auto eh : nh->inEdges())
|
|
|
{
|
|
|
|
|
|
if (const_ins.count(eh->srcNode()) == 0) {
|
|
|
if (m_gim.metadata(eh).contains<DesyncIslEdge>()) {
|
|
|
qgr.metadata(eh).set(DataQueue(DataQueue::DESYNC));
|
|
|
is_desync_start = true;
|
|
|
} else if (qgr.metadata(eh).contains<DesyncSpecialCase>()) {
|
|
|
|
|
|
|
|
|
qgr.metadata(eh).set(DataQueue(1u));
|
|
|
} else {
|
|
|
qgr.metadata(eh).set(DataQueue(queue_capacity));
|
|
|
}
|
|
|
m_internal_queues.insert(qgr.metadata(eh).get<DataQueue>().q.get());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (is_desync_start) {
|
|
|
auto isl = m_gim.metadata(nh).get<FusedIsland>().object;
|
|
|
|
|
|
|
|
|
GAPI_Assert(isl->in_ops().size() == 1u);
|
|
|
GAPI_Assert(GModel::Graph(*m_orig_graph)
|
|
|
.metadata(*isl->in_ops().begin())
|
|
|
.get<cv::gimpl::Op>()
|
|
|
.k.name == cv::gimpl::streaming::GCopy::id());
|
|
|
for (auto out_nh : nh->outNodes()) {
|
|
|
for (auto out_eh : out_nh->outEdges()) {
|
|
|
qgr.metadata(out_eh).set(DesyncSpecialCase{});
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
break;
|
|
|
case NodeKind::SLOT:
|
|
|
{
|
|
|
const auto orig_data_nh
|
|
|
= m_gim.metadata(nh).get<DataSlot>().original_data_node;
|
|
|
m_slots.emplace_back(DataDesc{nh, orig_data_nh});
|
|
|
}
|
|
|
break;
|
|
|
case NodeKind::EMIT:
|
|
|
{
|
|
|
const auto emitter_idx
|
|
|
= m_gim.metadata(nh).get<Emitter>().proto_index;
|
|
|
GAPI_Assert(emitter_idx < m_emitters.size());
|
|
|
m_emitters[emitter_idx] = nh;
|
|
|
}
|
|
|
break;
|
|
|
case NodeKind::SINK:
|
|
|
{
|
|
|
const auto sink_idx
|
|
|
= m_gim.metadata(nh).get<Sink>().proto_index;
|
|
|
GAPI_Assert(sink_idx < m_sinks.size());
|
|
|
m_sinks[sink_idx] = nh;
|
|
|
|
|
|
|
|
|
ade::TypedGraph<DataQueue> qgr(*m_island_graph);
|
|
|
GAPI_Assert(nh->inEdges().size() == 1u);
|
|
|
qgr.metadata(nh->inEdges().front()).set(DataQueue(queue_capacity));
|
|
|
m_sink_queues[sink_idx] = qgr.metadata(nh->inEdges().front()).get<DataQueue>().q.get();
|
|
|
|
|
|
|
|
|
const auto sink_out_nh = gm.metadata().get<Protocol>().out_nhs[sink_idx];
|
|
|
if (gm.metadata(sink_out_nh).contains<DesyncPath>()) {
|
|
|
|
|
|
m_sink_sync[sink_idx] = gm.metadata(sink_out_nh).get<DesyncPath>().index;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
GAPI_Error("InternalError");
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &&idx : ade::util::iota(m_sink_queues.size())) {
|
|
|
auto path_id = m_sink_sync[idx];
|
|
|
auto &info = m_collector_map[path_id];
|
|
|
info.queues.push_back(m_sink_queues[idx]);
|
|
|
info.mapping.push_back(static_cast<int>(idx));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GAPI_Assert(m_collector_map.size() > 0u);
|
|
|
m_out_queue.set_capacity(queue_capacity * m_collector_map.size());
|
|
|
|
|
|
|
|
|
cv::gimpl::GModel::ConstGraph cgr(*m_orig_graph);
|
|
|
auto meta = cgr.metadata().get<cv::gimpl::Protocol>().out_nhs;
|
|
|
out_info.reserve(meta.size());
|
|
|
|
|
|
ade::util::transform(meta, std::back_inserter(out_info), [&cgr](const ade::NodeHandle& nh) {
|
|
|
const auto& data = cgr.metadata(nh).get<cv::gimpl::Data>();
|
|
|
return cv::GTypeInfo{data.shape, data.kind, data.ctor};
|
|
|
});
|
|
|
}
|
|
|
|
|
|
cv::gimpl::GStreamingExecutor::~GStreamingExecutor()
|
|
|
{
|
|
|
|
|
|
|
|
|
try {
|
|
|
if (state == State::READY || state == State::RUNNING)
|
|
|
stop();
|
|
|
} catch (const std::exception& e) {
|
|
|
std::stringstream message;
|
|
|
message << "~GStreamingExecutor() threw exception with message '" << e.what() << "'\n";
|
|
|
GAPI_LOG_WARNING(NULL, message.str());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void cv::gimpl::GStreamingExecutor::setSource(GRunArgs &&ins)
|
|
|
{
|
|
|
GAPI_Assert(state == State::READY || state == State::STOPPED);
|
|
|
|
|
|
GModel::ConstGraph gm(*m_orig_graph);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const auto update_int_metas = [&]()
|
|
|
{
|
|
|
for (auto& op : m_ops)
|
|
|
{
|
|
|
op.out_metas.resize(0);
|
|
|
for (auto out_slot_nh : op.nh->outNodes())
|
|
|
{
|
|
|
const auto &orig_nh = m_gim.metadata(out_slot_nh).get<DataSlot>().original_data_node;
|
|
|
const auto &orig_info = gm.metadata(orig_nh).get<Data>();
|
|
|
op.out_metas.emplace_back(orig_info.meta);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const auto new_meta = cv::descr_of(ins);
|
|
|
if (gm.metadata().contains<OriginalInputMeta>())
|
|
|
{
|
|
|
|
|
|
GAPI_Assert(new_meta == gm.metadata().get<OriginalInputMeta>().inputMeta);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
GCompiler::runMetaPasses(*m_orig_graph.get(), new_meta);
|
|
|
if (!m_gim.metadata().contains<IslandsCompiled>()
|
|
|
|| (m_reshapable.has_value() && m_reshapable.value() == false))
|
|
|
{
|
|
|
bool is_reshapable = true;
|
|
|
GCompiler::compileIslands(*m_orig_graph.get(), m_comp_args);
|
|
|
for (auto& op : m_ops)
|
|
|
{
|
|
|
op.isl_exec = m_gim.metadata(op.nh).get<IslandExec>().object;
|
|
|
is_reshapable = is_reshapable && op.isl_exec->canReshape();
|
|
|
}
|
|
|
update_int_metas();
|
|
|
m_reshapable = util::make_optional(is_reshapable);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
for (auto& op : m_ops)
|
|
|
{
|
|
|
op.isl_exec->reshape(*m_orig_graph, m_comp_args);
|
|
|
}
|
|
|
update_int_metas();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<ade::NodeHandle> video_emitters;
|
|
|
for (auto it : ade::util::zip(ade::util::toRange(m_emitters),
|
|
|
ade::util::toRange(ins),
|
|
|
ade::util::iota(m_emitters.size())))
|
|
|
{
|
|
|
auto emit_nh = std::get<0>(it);
|
|
|
auto& emit_arg = std::get<1>(it);
|
|
|
auto emit_idx = std::get<2>(it);
|
|
|
auto& emitter = m_gim.metadata(emit_nh).get<Emitter>().object;
|
|
|
|
|
|
using T = GRunArg;
|
|
|
switch (emit_arg.index())
|
|
|
{
|
|
|
|
|
|
|
|
|
case T::index_of<cv::gapi::wip::IStreamSource::Ptr>():
|
|
|
#if !defined(GAPI_STANDALONE)
|
|
|
emitter.reset(new VideoEmitter{emit_arg});
|
|
|
|
|
|
|
|
|
video_emitters.emplace_back(emit_nh);
|
|
|
#else
|
|
|
util::throw_error(std::logic_error("Video is not supported in the "
|
|
|
"standalone mode"));
|
|
|
#endif
|
|
|
break;
|
|
|
default:
|
|
|
|
|
|
|
|
|
emitter.reset(new ConstEmitter{emit_arg});
|
|
|
m_const_vals.push_back(const_cast<cv::GRunArg &>(emit_arg));
|
|
|
m_const_emitter_queues.push_back(&m_emitter_queues[emit_idx]);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
m_sync->registerVideoEmitters(std::move(video_emitters));
|
|
|
|
|
|
|
|
|
|
|
|
GAPI_Assert(m_const_emitter_queues.size() == m_const_vals.size());
|
|
|
auto real_video_completion_cb = [this]()
|
|
|
{
|
|
|
for (auto it : ade::util::zip(ade::util::toRange(m_const_emitter_queues),
|
|
|
ade::util::toRange(m_const_vals)))
|
|
|
{
|
|
|
Stop stop;
|
|
|
stop.kind = Stop::Kind::CNST;
|
|
|
stop.cdata = std::get<1>(it);
|
|
|
std::get<0>(it)->push(Cmd{std::move(stop)});
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (state == State::READY)
|
|
|
{
|
|
|
stop();
|
|
|
}
|
|
|
|
|
|
for (auto it : ade::util::indexed(m_emitters))
|
|
|
{
|
|
|
const auto id = ade::util::index(it);
|
|
|
const auto eh = ade::util::value(it);
|
|
|
|
|
|
|
|
|
auto emitter = m_gim.metadata(eh).get<Emitter>().object;
|
|
|
|
|
|
|
|
|
auto out_queues = m_sync->outQueues(eh);
|
|
|
|
|
|
m_threads.emplace_back(emitterActorThread,
|
|
|
emitter,
|
|
|
std::ref(m_emitter_queues[id]),
|
|
|
out_queues,
|
|
|
real_video_completion_cb);
|
|
|
}
|
|
|
|
|
|
m_sync->start();
|
|
|
|
|
|
|
|
|
for (auto &&op : m_ops)
|
|
|
{
|
|
|
|
|
|
auto island_exec = m_gim.metadata(op.nh).get<IslandExec>().object;
|
|
|
|
|
|
|
|
|
auto in_queues = input_queues(*m_island_graph, op.nh);
|
|
|
|
|
|
|
|
|
|
|
|
std::vector< std::vector<stream::Q*> > out_queues;
|
|
|
for (auto &&out_eh : op.nh->outNodes()) {
|
|
|
out_queues.push_back(reader_queues(*m_island_graph, out_eh));
|
|
|
}
|
|
|
|
|
|
|
|
|
std::string island_meta_info { };
|
|
|
#if defined(OPENCV_WITH_ITT)
|
|
|
|
|
|
island_meta_info = GIslandModel::traceIslandName(op.nh, m_gim);
|
|
|
#endif
|
|
|
|
|
|
|
|
|
op.isl_exec->handleNewStream();
|
|
|
|
|
|
m_threads.emplace_back(islandActorThread,
|
|
|
op.in_objects,
|
|
|
op.out_objects,
|
|
|
op.out_metas,
|
|
|
island_exec,
|
|
|
in_queues,
|
|
|
op.in_constants,
|
|
|
out_queues,
|
|
|
island_meta_info);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const bool has_main_path = m_sink_sync.end() !=
|
|
|
std::find(m_sink_sync.begin(), m_sink_sync.end(), -1);
|
|
|
for (auto &&info : m_collector_map) {
|
|
|
m_threads.emplace_back(collectorThread,
|
|
|
info.second.queues,
|
|
|
info.second.mapping,
|
|
|
m_sink_queues.size(),
|
|
|
has_main_path ? info.first == -1 : true,
|
|
|
std::ref(m_out_queue));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
state = State::READY;
|
|
|
}
|
|
|
|
|
|
void cv::gimpl::GStreamingExecutor::start()
|
|
|
{
|
|
|
if (state == State::STOPPED)
|
|
|
{
|
|
|
util::throw_error(std::logic_error("Please call setSource() before start() "
|
|
|
"if the pipeline has been already stopped"));
|
|
|
}
|
|
|
GAPI_Assert(state == State::READY);
|
|
|
|
|
|
|
|
|
state = State::RUNNING;
|
|
|
for (auto &q : m_emitter_queues)
|
|
|
{
|
|
|
q.push(stream::Cmd{stream::Start{}});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void cv::gimpl::GStreamingExecutor::wait_shutdown()
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &t : m_threads) t.join();
|
|
|
m_threads.clear();
|
|
|
m_sync->join();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &q : m_emitter_queues) q.clear();
|
|
|
for (auto &q : m_sink_queues) q->clear();
|
|
|
for (auto &q : m_internal_queues) q->clear();
|
|
|
m_const_emitter_queues.clear();
|
|
|
m_const_vals.clear();
|
|
|
m_out_queue.clear();
|
|
|
m_sync->clear();
|
|
|
|
|
|
for (auto &&op : m_ops) {
|
|
|
op.isl_exec->handleStopStream();
|
|
|
}
|
|
|
|
|
|
state = State::STOPPED;
|
|
|
}
|
|
|
|
|
|
bool cv::gimpl::GStreamingExecutor::pull(cv::GRunArgsP &&outs)
|
|
|
{
|
|
|
GAPI_ITT_STATIC_LOCAL_HANDLE(pull_hndl, "GStreamingExecutor::pull");
|
|
|
GAPI_ITT_AUTO_TRACE_GUARD(pull_hndl);
|
|
|
|
|
|
|
|
|
|
|
|
GAPI_Assert(!m_desync &&
|
|
|
"This graph has desynchronized parts! Please use another pull()");
|
|
|
|
|
|
if (state == State::STOPPED)
|
|
|
return false;
|
|
|
GAPI_Assert(state == State::RUNNING);
|
|
|
GAPI_Assert(m_sink_queues.size() == outs.size() &&
|
|
|
"Number of data objects in cv::gout() must match the number of graph outputs in cv::GOut()");
|
|
|
|
|
|
Cmd cmd;
|
|
|
m_out_queue.pop(cmd);
|
|
|
switch (cmd.index()) {
|
|
|
case Cmd::index_of<Stop>():
|
|
|
wait_shutdown();
|
|
|
return false;
|
|
|
case Cmd::index_of<Result>(): {
|
|
|
GAPI_Assert(cv::util::holds_alternative<Result>(cmd));
|
|
|
cv::GRunArgs &this_result = cv::util::get<Result>(cmd).args;
|
|
|
sync_data(this_result, outs);
|
|
|
return true;
|
|
|
}
|
|
|
case Cmd::index_of<Exception>(): {
|
|
|
std::rethrow_exception(cv::util::get<Exception>(cmd).eptr);
|
|
|
return true;
|
|
|
}
|
|
|
default:
|
|
|
GAPI_Error("Unsupported cmd type in pull");
|
|
|
}
|
|
|
GAPI_Error("Unreachable code");
|
|
|
}
|
|
|
|
|
|
bool cv::gimpl::GStreamingExecutor::pull(cv::GOptRunArgsP &&outs)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (state == State::STOPPED)
|
|
|
return false;
|
|
|
GAPI_Assert(state == State::RUNNING);
|
|
|
GAPI_Assert(m_sink_queues.size() == outs.size() &&
|
|
|
"Number of data objects in cv::gout() must match the number of graph outputs in cv::GOut()");
|
|
|
|
|
|
Cmd cmd;
|
|
|
m_out_queue.pop(cmd);
|
|
|
switch (cmd.index()) {
|
|
|
case Cmd::index_of<Stop>():
|
|
|
wait_shutdown();
|
|
|
return false;
|
|
|
case Cmd::index_of<Result>(): {
|
|
|
sync_data(cv::util::get<Result>(cmd), outs);
|
|
|
return true;
|
|
|
}
|
|
|
case Cmd::index_of<Exception>(): {
|
|
|
std::rethrow_exception(cv::util::get<Exception>(cmd).eptr);
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
GAPI_Error("Unreachable code");
|
|
|
}
|
|
|
|
|
|
cv::gimpl::GAbstractStreamingExecutor::PyPullResult cv::gimpl::GStreamingExecutor::pull()
|
|
|
{
|
|
|
using RunArgs = cv::util::variant<cv::GRunArgs, cv::GOptRunArgs>;
|
|
|
bool is_over = false;
|
|
|
|
|
|
if (m_desync) {
|
|
|
GOptRunArgs opt_run_args;
|
|
|
GOptRunArgsP opt_outs;
|
|
|
opt_outs.reserve(out_info.size());
|
|
|
opt_run_args.reserve(out_info.size());
|
|
|
|
|
|
constructOptGraphOutputs(out_info, opt_run_args, opt_outs);
|
|
|
is_over = pull(std::move(opt_outs));
|
|
|
return std::make_tuple(is_over, RunArgs(opt_run_args));
|
|
|
}
|
|
|
|
|
|
GRunArgs run_args;
|
|
|
GRunArgsP outs;
|
|
|
run_args.reserve(out_info.size());
|
|
|
outs.reserve(out_info.size());
|
|
|
|
|
|
constructGraphOutputs(out_info, run_args, outs);
|
|
|
is_over = pull(std::move(outs));
|
|
|
return std::make_tuple(is_over, RunArgs(run_args));
|
|
|
}
|
|
|
|
|
|
bool cv::gimpl::GStreamingExecutor::try_pull(cv::GRunArgsP &&outs)
|
|
|
{
|
|
|
if (state == State::STOPPED)
|
|
|
return false;
|
|
|
|
|
|
GAPI_Assert(m_sink_queues.size() == outs.size());
|
|
|
|
|
|
Cmd cmd;
|
|
|
if (!m_out_queue.try_pop(cmd)) {
|
|
|
return false;
|
|
|
}
|
|
|
if (cv::util::holds_alternative<Stop>(cmd))
|
|
|
{
|
|
|
wait_shutdown();
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
GAPI_Assert(cv::util::holds_alternative<Result>(cmd));
|
|
|
cv::GRunArgs &this_result = cv::util::get<Result>(cmd).args;
|
|
|
sync_data(this_result, outs);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
void cv::gimpl::GStreamingExecutor::stop()
|
|
|
{
|
|
|
if (state == State::STOPPED)
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &q : m_emitter_queues) {
|
|
|
q.push(stream::Cmd{stream::Stop{}});
|
|
|
}
|
|
|
|
|
|
|
|
|
for (auto &nh : m_emitters) {
|
|
|
m_gim.metadata(nh).get<Emitter>().object->halt();
|
|
|
}
|
|
|
|
|
|
|
|
|
Cmd cmd;
|
|
|
while (!cv::util::holds_alternative<Stop>(cmd))
|
|
|
{
|
|
|
m_out_queue.pop(cmd);
|
|
|
}
|
|
|
GAPI_Assert(cv::util::holds_alternative<Stop>(cmd));
|
|
|
wait_shutdown();
|
|
|
}
|
|
|
|
|
|
bool cv::gimpl::GStreamingExecutor::running() const
|
|
|
{
|
|
|
return (state == State::RUNNING);
|
|
|
}
|
|
|
|