support for sync and thread-safe Sources/Sinks

pull/3471/head
kallaballa 2 years ago
parent fff3a952c9
commit a59af88fff
  1. 53
      modules/v4d/src/v4d.cpp

@ -39,6 +39,7 @@ V4D::V4D(const cv::Size& size, const cv::Size& fbsize, const string& title, bool
#endif #endif
nvgContext_ = new detail::NanoVGContext(*mainFbContext_); nvgContext_ = new detail::NanoVGContext(*mainFbContext_);
clvaContext_ = new detail::CLVAContext(*mainFbContext_); clvaContext_ = new detail::CLVAContext(*mainFbContext_);
if(!offscreen)
imguiContext_ = new detail::ImGuiContextImpl(*mainFbContext_); imguiContext_ = new detail::ImGuiContextImpl(*mainFbContext_);
} }
@ -222,6 +223,7 @@ static void do_frame(void* void_fn_ptr) {
void V4D::run(std::function<bool(cv::Ptr<V4D>)> fn) { void V4D::run(std::function<bool(cv::Ptr<V4D>)> fn) {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
this->makeCurrent();
bool success = true; bool success = true;
while (keepRunning() && success) { while (keepRunning() && success) {
CLExecScope_t scope(fbCtx().getCLExecContext()); CLExecScope_t scope(fbCtx().getCLExecContext());
@ -236,14 +238,14 @@ void V4D::run(std::function<bool(cv::Ptr<V4D>)> fn) {
#endif #endif
} }
void V4D::setSource(const Source& src) { void V4D::setSource(Source& src) {
if (!clvaCtx().hasContext()) { if (!clvaCtx().hasContext()) {
if(isIntelVaSupported()) { if(isIntelVaSupported()) {
clvaCtx().copyContext(); clvaCtx().copyContext();
} }
} }
source_ = src; source_ = &src;
} }
void V4D::feed(cv::InputArray in) { void V4D::feed(cv::InputArray in) {
@ -298,30 +300,45 @@ cv::_InputArray V4D::fetch() {
} }
bool V4D::capture() { bool V4D::capture() {
if(source_.isAsync()) { if(source_) {
if(source_->isAsync()) {
return this->capture([this](cv::UMat& videoFrame) { return this->capture([this](cv::UMat& videoFrame) {
if (source_.isReady()) if (source_->isReady()) {
source_().second.copyTo(videoFrame); auto p = source_->operator()();
currentSeqNr_ = p.first;
p.second.copyTo(videoFrame);
}
}); });
} else { } else {
if(source_.fps() > 0) { if(source_->fps() > 0) {
fb([this](cv::UMat& frameBuffer){ fb([this](cv::UMat& frameBuffer){
if (source_.isReady()) if (source_->isReady()) {
source_().second.copyTo(frameBuffer); auto p = source_->operator()();
currentSeqNr_ = p.first;
p.second.copyTo(frameBuffer);
}
}); });
} else { } else {
source_(); auto p = source_->operator()();
currentSeqNr_ = p.first;
} }
return true; return true;
} }
} else {
#ifndef __EMSCRIPTEN__
return false;
#else
return true;
#endif
}
} }
bool V4D::capture(std::function<void(cv::UMat&)> fn) { bool V4D::capture(std::function<void(cv::UMat&)> fn) {
bool res = true; bool res = true;
TimeTracker::getInstance()->execute("capture", [this, fn, &res](){ TimeTracker::getInstance()->execute("capture", [this, fn, &res](){
CV_UNUSED(res); CV_UNUSED(res);
if (!source_.isReady() || !source_.isOpen()) { if (!source_ || !source_->isReady() || !source_->isOpen()) {
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
res = false; res = false;
#endif #endif
@ -363,28 +380,28 @@ bool V4D::capture(std::function<void(cv::UMat&)> fn) {
} }
bool V4D::isSourceReady() { bool V4D::isSourceReady() {
return source_.isReady(); return source_ && source_->isReady();
} }
void V4D::setSink(const Sink& sink) { void V4D::setSink(Sink& sink) {
if (!clvaCtx().hasContext()) { if (!clvaCtx().hasContext()) {
if(isIntelVaSupported()) { if(isIntelVaSupported()) {
clvaCtx().copyContext(); clvaCtx().copyContext();
} }
} }
sink_ = sink; sink_ = &sink;
} }
void V4D::write() { void V4D::write() {
this->write([&](const cv::UMat& videoFrame) { this->write([&](const cv::UMat& videoFrame) {
if (sink_.isReady()) if (sink_ && sink_->isReady())
sink_(videoFrame); sink_->operator()(currentSeqNr_, videoFrame);
}); });
} }
void V4D::write(std::function<void(const cv::UMat&)> fn) { void V4D::write(std::function<void(const cv::UMat&)> fn) {
TimeTracker::getInstance()->execute("write", [this, fn](){ TimeTracker::getInstance()->execute("write", [this, fn](){
if (!sink_.isReady() || !sink_.isOpen()) if (!sink_ || !sink_->isReady() || !sink_->isOpen())
return; return;
if (futureWriter_.valid()) if (futureWriter_.valid())
@ -401,7 +418,7 @@ void V4D::write(std::function<void(const cv::UMat&)> fn) {
} }
bool V4D::isSinkReady() { bool V4D::isSinkReady() {
return sink_.isReady(); return sink_ && sink_->isReady();
} }
cv::Vec2f V4D::position() { cv::Vec2f V4D::position() {
@ -537,6 +554,7 @@ bool V4D::display() {
FrameBufferContext::GLScope glScope(fbCtx(), GL_READ_FRAMEBUFFER); FrameBufferContext::GLScope glScope(fbCtx(), GL_READ_FRAMEBUFFER);
fbCtx().blitFrameBufferToFrameBuffer(viewport(), fbCtx().getWindowSize(), 0, isStretching()); fbCtx().blitFrameBufferToFrameBuffer(viewport(), fbCtx().getWindowSize(), 0, isStretching());
} }
if(hasImguiCtx())
imguiCtx().render(getShowFPS()); imguiCtx().render(getShowFPS());
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
if(debug_) if(debug_)
@ -601,7 +619,6 @@ void V4D::printSystemInfo() {
void V4D::makeCurrent() { void V4D::makeCurrent() {
fbCtx().makeCurrent(); fbCtx().makeCurrent();
imguiCtx().makeCurrent();
} }
cv::Ptr<V4D> V4D::self() { cv::Ptr<V4D> V4D::self() {

Loading…
Cancel
Save