use scopes for context lifetimes wherever possible

pull/3471/head
kallaballa 2 years ago
parent 985a5a0e4d
commit 363a59b3e3
  1. 5
      src/common/clglcontext.cpp
  2. 19
      src/common/clglcontext.hpp
  3. 6
      src/common/clvacontext.cpp
  4. 1
      src/common/clvacontext.hpp
  5. 3
      src/common/nanovgcontext.cpp
  6. 2
      src/common/nanovgcontext.hpp
  7. 3
      src/common/viz2d.cpp
  8. 1
      src/common/viz2d.hpp

@ -45,7 +45,8 @@ cv::Size CLGLContext::getSize() {
void CLGLContext::opencl(std::function<void(cv::UMat&)> fn) {
CLExecScope_t clExecScope(getCLExecContext());
CLGLContext::Scope fbScope(*this, frameBuffer_);
CLGLContext::GLScope glScope(*this);
CLGLContext::FrameBufferScope fbScope(*this, frameBuffer_);
fn(frameBuffer_);
}
@ -85,7 +86,6 @@ void CLGLContext::end() {
}
void CLGLContext::acquireFromGL(cv::UMat &m) {
begin();
GL_CHECK(cv::ogl::convertFromGLTexture2D(getTexture2D(), m));
//FIXME
cv::flip(m, m, 0);
@ -95,7 +95,6 @@ void CLGLContext::releaseToGL(cv::UMat &m) {
//FIXME
cv::flip(m, m, 0);
GL_CHECK(cv::ogl::convertToGLTexture2D(m, getTexture2D()));
end();
}
} /* namespace kb */

@ -21,7 +21,6 @@ class Viz2D;
class CLGLContext {
friend class CLVAContext;
friend class NanoVGContext;
friend class Viz2D;
cv::ogl::Texture2D *frameBufferTex_;
GLuint frameBufferID;
GLuint renderBufferID;
@ -29,19 +28,31 @@ class CLGLContext {
CLExecContext_t context_;
cv::Size frameBufferSize_;
public:
class Scope {
class FrameBufferScope {
CLGLContext& ctx_;
cv::UMat& m_;
public:
Scope(CLGLContext& ctx, cv::UMat& m) : ctx_(ctx), m_(m) {
FrameBufferScope(CLGLContext& ctx, cv::UMat& m) : ctx_(ctx), m_(m) {
ctx_.acquireFromGL(m_);
}
~Scope() {
~FrameBufferScope() {
ctx_.releaseToGL(m_);
}
};
class GLScope {
CLGLContext& ctx_;
public:
GLScope(CLGLContext& ctx) : ctx_(ctx) {
ctx_.begin();
}
~GLScope() {
ctx_.end();
}
};
CLGLContext(const cv::Size& frameBufferSize);
virtual ~CLGLContext();
cv::ogl::Texture2D& getFrameBufferTexture();

@ -29,7 +29,8 @@ bool CLVAContext::capture(std::function<void(cv::UMat&)> fn) {
}
{
CLExecScope_t scope(clglContext_.getCLExecContext());
CLGLContext::Scope fbScope(clglContext_, frameBuffer_);
CLGLContext::GLScope glScope(clglContext_);
CLGLContext::FrameBufferScope fbScope(clglContext_, frameBuffer_);
if (videoFrame_.empty())
return false;
@ -46,7 +47,8 @@ void CLVAContext::write(std::function<void(const cv::UMat&)> fn) {
cv::Size fbSize = clglContext_.getSize();
{
CLExecScope_t scope(clglContext_.getCLExecContext());
CLGLContext::Scope fbScope(clglContext_, frameBuffer_);
CLGLContext::GLScope glScope(clglContext_);
CLGLContext::FrameBufferScope fbScope(clglContext_, frameBuffer_);
cv::cvtColor(frameBuffer_, rgbBuffer_, cv::COLOR_BGRA2RGB);
cv::resize(rgbBuffer_, videoFrame_, videoFrameSize_);

@ -8,7 +8,6 @@ namespace kb {
class Viz2D;
class CLVAContext {
friend class Viz2D;
CLExecContext_t context_;
CLGLContext &clglContext_;
cv::UMat frameBuffer_;

@ -16,12 +16,12 @@ NanoVGContext::NanoVGContext(Viz2D &v2d, NVGcontext *context, CLGLContext &fbCon
void NanoVGContext::render(std::function<void(NVGcontext*, const cv::Size&)> fn) {
CLExecScope_t scope(clglContext_.getCLExecContext());
CLGLContext::GLScope glScope(clglContext_);
NanoVGContext::Scope nvgScope(*this);
fn(context_, clglContext_.getSize());
}
void NanoVGContext::begin() {
clglContext_.begin();
float w = v2d_.getVideoFrameSize().width;
float h = v2d_.getVideoFrameSize().height;
float r = v2d_.getXPixelRatio();
@ -34,6 +34,5 @@ void NanoVGContext::begin() {
void NanoVGContext::end() {
nvgEndFrame(context_);
nvgRestore(context_);
clglContext_.end();
}
} /* namespace kb */

@ -8,8 +8,6 @@
#include "util.hpp"
namespace kb {
class Viz2D;
class NanoVGContext {
Viz2D& v2d_;
NVGcontext *context_;

@ -167,9 +167,8 @@ void Viz2D::setVideoFrameSize(const cv::Size& sz) {
void Viz2D::opengl(std::function<void(const cv::Size&)> fn) {
CLExecScope_t scope(clglContext_->getCLExecContext());
clglContext_->begin();
CLGLContext::GLScope glScope(*clglContext_);
fn(getFrameBufferSize());
clglContext_->end();
}
void Viz2D::opencl(std::function<void(cv::UMat&)> fn) {

@ -4,6 +4,7 @@
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include "clglcontext.hpp"
#include "clvacontext.hpp"
#include "nanovgcontext.hpp"

Loading…
Cancel
Save