From 55592b3dcb69cbe1a444ee0040bb7cd612184384 Mon Sep 17 00:00:00 2001 From: kallaballa Date: Sat, 29 Apr 2023 02:21:20 +0200 Subject: [PATCH] big time refactoring mainly for portability reasons --- .../include/opencv2/v4d/detail/threadpool.hpp | 1 - modules/v4d/include/opencv2/v4d/v4d.hpp | 12 ++--- .../v4d/samples/custom_source_and_sink.cpp | 26 ++++----- modules/v4d/samples/display_image.cpp | 23 ++++---- modules/v4d/samples/display_image_fb.cpp | 21 ++++---- .../example_v4d_custom_source_and_sink.html | 3 +- .../samples/example_v4d_display_image.html | 3 +- .../samples/example_v4d_display_image_fb.html | 3 +- .../samples/example_v4d_font_rendering.html | 3 +- .../samples/example_v4d_font_with_gui.html | 3 +- .../samples/example_v4d_render_opengl.html | 2 +- .../samples/example_v4d_vector_graphics.html | 3 +- .../example_v4d_vector_graphics_and_fb.html | 2 +- .../samples/example_v4d_video_editing.html | 3 +- modules/v4d/samples/font_rendering.cpp | 36 ++++++------- modules/v4d/samples/font_with_gui.cpp | 21 ++++---- modules/v4d/samples/render_opengl.cpp | 25 ++++----- modules/v4d/samples/vector_graphics.cpp | 21 ++++---- .../v4d/samples/vector_graphics_and_fb.cpp | 20 +++---- modules/v4d/samples/video-demo.cpp | 7 +-- modules/v4d/samples/video_editing.cpp | 31 +++++------ modules/v4d/src/detail/framebuffercontext.cpp | 48 +++++++---------- modules/v4d/src/detail/framebuffercontext.hpp | 2 - modules/v4d/src/detail/nanoguicontext.cpp | 1 - modules/v4d/src/util.cpp | 2 +- modules/v4d/src/v4d.cpp | 53 +++++++++++-------- .../04-vector_graphics_and_fb.markdown | 2 +- .../v4d/tutorials/05-render_opengl.markdown | 2 +- 28 files changed, 188 insertions(+), 191 deletions(-) diff --git a/modules/v4d/include/opencv2/v4d/detail/threadpool.hpp b/modules/v4d/include/opencv2/v4d/detail/threadpool.hpp index 7c8597be2..403d799fa 100644 --- a/modules/v4d/include/opencv2/v4d/detail/threadpool.hpp +++ b/modules/v4d/include/opencv2/v4d/detail/threadpool.hpp @@ -29,7 +29,6 @@ public: } for (auto i = 0llu; i < threads; i++) { - std::cerr << "work" << std::endl; workers.emplace_back([this] { worker_main(); }); } } diff --git a/modules/v4d/include/opencv2/v4d/v4d.hpp b/modules/v4d/include/opencv2/v4d/v4d.hpp index 00a3e974d..cd841e6b7 100644 --- a/modules/v4d/include/opencv2/v4d/v4d.hpp +++ b/modules/v4d/include/opencv2/v4d/v4d.hpp @@ -115,7 +115,7 @@ class NVG; class CV_EXPORTS V4D { friend class detail::NanoVGContext; friend class detail::FrameBufferContext; - const cv::Size initialSize_; + cv::Size initialSize_; bool offscreen_; const string& title_; int major_; @@ -305,17 +305,13 @@ public: * @return The current viewport. */ CV_EXPORTS cv::Rect& viewport(); + CV_EXPORTS float getXPixelRatio(); + CV_EXPORTS float getYPixelRatio(); /*! * Set the window size. * @param sz The new window size. */ CV_EXPORTS void resizeWindow(const cv::Size& sz); - CV_EXPORTS void setWindowSize(const cv::Size& sz); - /*! - * Get the window size - * @return The current window size. - */ - CV_EXPORTS cv::Size getWindowSize(); /*! * Get the initial size. * @return The initial size. @@ -404,7 +400,7 @@ public: bool display_impl(); private: V4D(const cv::Size& initialSize, bool offscreen, - const string& title, int major = 4, int minor = 6, bool compat = true, int samples = 0, bool debug = true); + const string& title, int major = 4, int minor = 6, bool compat = true, int samples = 0, bool debug = false); void setDefaultKeyboardEventCallback(); void setKeyboardEventCallback( std::function fn); diff --git a/modules/v4d/samples/custom_source_and_sink.cpp b/modules/v4d/samples/custom_source_and_sink.cpp index d334069ad..255fe8701 100644 --- a/modules/v4d/samples/custom_source_and_sink.cpp +++ b/modules/v4d/samples/custom_source_and_sink.cpp @@ -3,13 +3,13 @@ # include #endif -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; + +static Ptr window = V4D::make(Size(1280, 720), "Custom Source/Sink"); +int main() { string hr = "Hello Rainbow!"; - Ptr v4d = V4D::make(Size(1280, 720), "Custom Source/Sink"); - v4d->setVisible(true); //Make a Source that generates rainbow frames. Source src([](cv::UMat& frame){ static long cnt = 0; @@ -36,16 +36,16 @@ int main() { }); //Attach source and sink - v4d->setSource(src); - v4d->setSink(sink); + window->setSource(src); + window->setSink(sink); - v4d->run([=]() { + window->run([=]() { //Capture video from the Source - if(!v4d->capture()) + if(!window->capture()) return false; //end of input video //Render "Hello Rainbow!" over the frame - v4d->nvg([=](const Size& sz) { + window->nvg([=](const Size& sz) { using namespace cv::v4d::nvg; fontSize(40.0f); @@ -54,9 +54,9 @@ int main() { textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); text(sz.width / 2.0, sz.height / 2.0, hr.c_str(), hr.c_str() + hr.size()); }); - updateFps(v4d,true); - v4d->write(); //Write video to the Sink - return v4d->display(); //Display the framebuffer in the native window + updateFps(window,true); + window->write(); //Write video to the Sink + return window->display(); //Display the framebuffer in the native window }); } diff --git a/modules/v4d/samples/display_image.cpp b/modules/v4d/samples/display_image.cpp index 7932b8895..a36e7e95c 100644 --- a/modules/v4d/samples/display_image.cpp +++ b/modules/v4d/samples/display_image.cpp @@ -1,30 +1,27 @@ #include #include -#ifdef __EMSCRIPTEN__ -# include -#endif +using namespace cv; +using namespace cv::v4d; -int main() { - using namespace cv; - using namespace cv::v4d; +//Creates a V4D window for on screen rendering +static Ptr window = V4D::make(Size(1280, 720), "Show image"); - //Creates a V4D object for on screen rendering - Ptr v4d = V4D::make(Size(1280, 720), "Show image"); - v4d->setVisible(true); +int main() { //An image #ifdef __EMSCRIPTEN__ Mat image = read_image("doc/lena.png"); #else Mat image = imread(samples::findFile("lena.jpg")); #endif + //Feeds the image to the video pipeline + window->feed(image); //Display the framebuffer in the native window in an endless loop. //V4D::run() though it takes a functor is not a context. It is simply an abstraction //of a run loop for portability reasons and executes the functor until the application //terminates or the functor returns false. - v4d->run([=](){ - //Feeds the image to the video pipeline - v4d->feed(image); - return v4d->display(); + window->run([=](){ + updateFps(window, true); + return window->display(); }); } diff --git a/modules/v4d/samples/display_image_fb.cpp b/modules/v4d/samples/display_image_fb.cpp index 2f10a9b12..2888b32af 100644 --- a/modules/v4d/samples/display_image_fb.cpp +++ b/modules/v4d/samples/display_image_fb.cpp @@ -1,13 +1,13 @@ #include #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; + +//Creates a V4D object for on screen rendering +static Ptr window = V4D::make(Size(1280, 720), "Show image"); - //Creates a V4D object for on screen rendering - Ptr v4d = V4D::make(Size(1280, 720), "Show image"); - v4d->setVisible(true); +int main() { //Read an image as UMat #ifdef __EMSCRIPTEN__ UMat image = read_image("doc/lena.png").getUMat(ACCESS_READ); @@ -16,13 +16,14 @@ int main() { #endif UMat resized; //Resize and color convert the image to framebuffer size - v4d->fb([&](const UMat& framebuffer) { - resize(image, resized, v4d->getFrameBufferSize()); + window->fb([&](const UMat& framebuffer) { + resize(image, resized, window->getFrameBufferSize()); cvtColor(resized, framebuffer, COLOR_RGB2BGRA); }); //Display the framebuffer in the native window in an endless loop - v4d->run([=](){ - return v4d->display(); + window->run([=](){ + updateFps(window, true); + return window->display(); }); } diff --git a/modules/v4d/samples/example_v4d_custom_source_and_sink.html b/modules/v4d/samples/example_v4d_custom_source_and_sink.html index f876ab897..93ddce8b6 100644 --- a/modules/v4d/samples/example_v4d_custom_source_and_sink.html +++ b/modules/v4d/samples/example_v4d_custom_source_and_sink.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_display_image.html b/modules/v4d/samples/example_v4d_display_image.html index 3602b404d..97ace37af 100644 --- a/modules/v4d/samples/example_v4d_display_image.html +++ b/modules/v4d/samples/example_v4d_display_image.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_display_image_fb.html b/modules/v4d/samples/example_v4d_display_image_fb.html index 6d971d67f..8c9e78882 100644 --- a/modules/v4d/samples/example_v4d_display_image_fb.html +++ b/modules/v4d/samples/example_v4d_display_image_fb.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_font_rendering.html b/modules/v4d/samples/example_v4d_font_rendering.html index c14ccfa45..bc620f407 100644 --- a/modules/v4d/samples/example_v4d_font_rendering.html +++ b/modules/v4d/samples/example_v4d_font_rendering.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_font_with_gui.html b/modules/v4d/samples/example_v4d_font_with_gui.html index 2504e81a8..b2b352dae 100644 --- a/modules/v4d/samples/example_v4d_font_with_gui.html +++ b/modules/v4d/samples/example_v4d_font_with_gui.html @@ -135,10 +135,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_render_opengl.html b/modules/v4d/samples/example_v4d_render_opengl.html index a98d52f85..ea2f53a5a 100644 --- a/modules/v4d/samples/example_v4d_render_opengl.html +++ b/modules/v4d/samples/example_v4d_render_opengl.html @@ -134,10 +134,10 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_vector_graphics.html b/modules/v4d/samples/example_v4d_vector_graphics.html index 8be25c587..83f60da7a 100644 --- a/modules/v4d/samples/example_v4d_vector_graphics.html +++ b/modules/v4d/samples/example_v4d_vector_graphics.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_vector_graphics_and_fb.html b/modules/v4d/samples/example_v4d_vector_graphics_and_fb.html index 13b3c4340..fa36ccd8d 100644 --- a/modules/v4d/samples/example_v4d_vector_graphics_and_fb.html +++ b/modules/v4d/samples/example_v4d_vector_graphics_and_fb.html @@ -134,10 +134,10 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/example_v4d_video_editing.html b/modules/v4d/samples/example_v4d_video_editing.html index f3148038c..d068f9ae4 100644 --- a/modules/v4d/samples/example_v4d_video_editing.html +++ b/modules/v4d/samples/example_v4d_video_editing.html @@ -134,10 +134,11 @@ var Module = { onRuntimeInitialized: function() { + fixCanvasSize(); }, preRun: [], postRun: function() { - fixCanvasSize(); + }, print: (function() { var element = document.getElementById('output'); diff --git a/modules/v4d/samples/font_rendering.cpp b/modules/v4d/samples/font_rendering.cpp index b1ab502d4..2aa01ae8a 100644 --- a/modules/v4d/samples/font_rendering.cpp +++ b/modules/v4d/samples/font_rendering.cpp @@ -1,30 +1,28 @@ #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; - Ptr v4d = V4D::make(Size(1280, 720), "Font Rendering"); - v4d->setVisible(true); +static Ptr window = V4D::make(Size(1280, 720), "Font Rendering"); +int main() { //The text to render string hw = "Hello World"; - //Clear with black - v4d->clear(); - //Render the text at the center of the screen - v4d->nvg([&](const Size& sz) { - using namespace cv::v4d::nvg; - fontSize(40.0f); - fontFace("sans-bold"); - fillColor(Scalar(255, 0, 0, 255)); - textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); - text(sz.width / 2.0, sz.height / 2.0, hw.c_str(), hw.c_str() + hw.size()); - }); //Display the framebuffer in the native window in an endless loop - v4d->run([=](){ - updateFps(v4d,true); - return v4d->display(); + window->run([=](){ + window->clear(); + //Render the text at the center of the screen + window->nvg([&](const Size& sz) { + using namespace cv::v4d::nvg; + fontSize(40.0f); + fontFace("sans-bold"); + fillColor(Scalar(255, 0, 0, 255)); + textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); + text(sz.width / 2.0, sz.height / 2.0, hw.c_str(), hw.c_str() + hw.size()); + }); + updateFps(window,true); + return window->display(); }); } diff --git a/modules/v4d/samples/font_with_gui.cpp b/modules/v4d/samples/font_with_gui.cpp index 40ba48fe5..153007d93 100644 --- a/modules/v4d/samples/font_with_gui.cpp +++ b/modules/v4d/samples/font_with_gui.cpp @@ -1,12 +1,11 @@ #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; - Ptr v4d = V4D::make(Size(1280, 720), "Font Rendering with GUI"); - v4d->setVisible(true); +static Ptr window = V4D::make(Size(1280, 720), "Font Rendering with GUI"); +int main() { //The text color. NanoGUI uses rgba with floating point nanogui::Color textColor = {0.0f, 0.0f, 1.0f, 1.0f}; //The font size @@ -14,7 +13,7 @@ int main() { //The text string hw = "hello world"; //Setup the GUI - v4d->nanogui([&](FormHelper& form) { + window->nanogui([&](FormHelper& form) { //Create a light-weight dialog form.makeDialog(5, 30, "Settings"); //Create a group @@ -25,10 +24,10 @@ int main() { form.makeColorPicker("Text Color", textColor, "The text color"); }); - v4d->run([&]() { - v4d->clear(); + window->run([&]() { + window->clear(); //Render the text at the center of the screen - v4d->nvg([&](const Size& sz) { + window->nvg([&](const Size& sz) { using namespace cv::v4d::nvg; fontSize(size); fontFace("sans-bold"); @@ -36,9 +35,9 @@ int main() { textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); text(sz.width / 2.0, sz.height / 2.0, hw.c_str(), hw.c_str() + hw.size()); }); - updateFps(v4d,true); + updateFps(window,true); //Display the framebuffer in the native window - return v4d->display(); + return window->display(); }); } diff --git a/modules/v4d/samples/render_opengl.cpp b/modules/v4d/samples/render_opengl.cpp index 909f1a0a9..dc832ce05 100644 --- a/modules/v4d/samples/render_opengl.cpp +++ b/modules/v4d/samples/render_opengl.cpp @@ -1,23 +1,24 @@ #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; - Ptr v4d = V4D::make(Size(1280, 720), "GL Blue Screen", true); - v4d->printSystemInfo(); - v4d->setVisible(true); +static Ptr window = V4D::make(Size(1280, 720), "GL Blue Screen"); - v4d->run([=]() { - v4d->gl([]() { - //Clears the screen blue - glClearColor(0.0f, 0.0f, 1.0f, 1.0f); +int main() { + window->gl([](){ + //Sets blue as clear color + glClearColor(0.0f, 0.0f, 1.0f, 1.0f); + }); + window->run([=]() { + window->gl([]() { + //Clears the screen glClear(GL_COLOR_BUFFER_BIT); }); - updateFps(v4d,true); + updateFps(window,true); //If onscreen rendering is enabled it displays the framebuffer in the native window. //Returns false if the window was closed. - return v4d->display(); + return window->display(); }); } diff --git a/modules/v4d/samples/vector_graphics.cpp b/modules/v4d/samples/vector_graphics.cpp index 955538e0b..5edf969ba 100644 --- a/modules/v4d/samples/vector_graphics.cpp +++ b/modules/v4d/samples/vector_graphics.cpp @@ -1,16 +1,16 @@ #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; - Ptr v4d = V4D::make(Size(1280, 720), "Vector Graphics"); - v4d->setVisible(true); - //Creates a NanoVG context and draws a cross-hair on the framebuffer +static Ptr window = V4D::make(Size(1280, 720), "Vector Graphics"); +int main() { //Display the framebuffer in the native window in an endless loop - v4d->run([=](){ - v4d->nvg([](const Size& sz) { + window->run([=](){ + window->clear(); + //Creates a NanoVG context and draws a cross-hair on the framebuffer + window->nvg([](const Size& sz) { //Calls from this namespace may only be used inside a nvg context using namespace cv::v4d::nvg; @@ -24,8 +24,9 @@ int main() { lineTo(sz.width, sz.height/2.0); stroke(); }); - updateFps(v4d,true); - return v4d->display(); + + updateFps(window,true); + return window->display(); }); } diff --git a/modules/v4d/samples/vector_graphics_and_fb.cpp b/modules/v4d/samples/vector_graphics_and_fb.cpp index 99793dfe9..82cbb6281 100644 --- a/modules/v4d/samples/vector_graphics_and_fb.cpp +++ b/modules/v4d/samples/vector_graphics_and_fb.cpp @@ -1,16 +1,16 @@ #include -int main() { - using namespace cv; - using namespace cv::v4d; +using namespace cv; +using namespace cv::v4d; - Ptr v4d = V4D::make(Size(1280, 720), "Vector Graphics and Framebuffer"); - v4d->setVisible(true); +static Ptr window = V4D::make(Size(1280, 720), "Vector Graphics and Framebuffer"); +int main() { //Display the framebuffer in the native window in an endless loop - v4d->run([=](){ + window->run([=](){ + window->clear(); //Creates a NanoVG context and draws a cross-hair on the framebuffer - v4d->nvg([](const Size& sz) { + window->nvg([](const Size& sz) { //Calls from this namespace may only be used inside a nvg context using namespace cv::v4d::nvg; @@ -25,12 +25,12 @@ int main() { stroke(); }); - v4d->fb([](UMat& framebuffer) { + window->fb([](UMat& framebuffer) { //Heavily blurs the crosshair using a cheap boxFilter boxFilter(framebuffer, framebuffer, -1, Size(15, 15), Point(-1,-1), true, BORDER_REPLICATE); }); - updateFps(v4d,true); - return v4d->display(); + updateFps(window,true); + return window->display(); }); } diff --git a/modules/v4d/samples/video-demo.cpp b/modules/v4d/samples/video-demo.cpp index 54c85afe6..ca06833ae 100644 --- a/modules/v4d/samples/video-demo.cpp +++ b/modules/v4d/samples/video-demo.cpp @@ -206,13 +206,10 @@ static bool iteration() { updateFps(v4d, true); - //If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed. - if (!v4d->display()) - return false; - v4d->write(); - return true; + //If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed. + return v4d->display(); } #ifndef __EMSCRIPTEN__ diff --git a/modules/v4d/samples/video_editing.cpp b/modules/v4d/samples/video_editing.cpp index b29785ccb..d854b65fa 100644 --- a/modules/v4d/samples/video_editing.cpp +++ b/modules/v4d/samples/video_editing.cpp @@ -1,40 +1,41 @@ #include +using namespace cv; +using namespace cv::v4d; + +static Ptr window = V4D::make(cv::Size(1280, 720), "Video Editing"); + int main(int argc, char** argv) { try { //In case of emscripten CV_UNUSED(argc); CV_UNUSED(argv); - using namespace cv; - using namespace cv::v4d; - string hv = "Hello Video!"; - Ptr v4d = V4D::make(Size(1280, 720), "Video Editing"); - v4d->setVisible(true); + #ifndef __EMSCRIPTEN__ //Make the video source Source src = makeCaptureSource(argv[1]); //Make the video sink - Sink sink = makeWriterSink(argv[2], VideoWriter::fourcc('V', 'P', '9', '0'), src.fps(), v4d->getFrameBufferSize()); + Sink sink = makeWriterSink(argv[2], VideoWriter::fourcc('V', 'P', '9', '0'), src.fps(), window->getFrameBufferSize()); //Attach source and sink - v4d->setSource(src); - v4d->setSink(sink); + window->setSource(src); + window->setSink(sink); #else //Make a webcam Source Source src = makeCaptureSource(1280,720); //Attach web source - v4d->setSource(src); + window->setSource(src); #endif - v4d->run([=]() { + window->run([=]() { //Capture video from the Source - if(!v4d->capture()) + if(!window->capture()) return false; //end of input video - v4d->nvg([=](const Size& sz) { + window->nvg([=](const Size& sz) { using namespace cv::v4d::nvg; fontSize(40.0f); @@ -43,11 +44,11 @@ int main(int argc, char** argv) { textAlign(NVG_ALIGN_CENTER | NVG_ALIGN_TOP); text(sz.width / 2.0, sz.height / 2.0, hv.c_str(), hv.c_str() + hv.size()); }); - updateFps(v4d,true); + updateFps(window,true); - v4d->write(); //Write video to the Sink + window->write(); //Write video to the Sink - return v4d->display(); //Display the framebuffer in the native window + return window->display(); //Display the framebuffer in the native window }); } catch(std::exception& ex) { cerr << ex.what() << endl; diff --git a/modules/v4d/src/detail/framebuffercontext.cpp b/modules/v4d/src/detail/framebuffercontext.cpp index ef30cf5dd..22570b71c 100644 --- a/modules/v4d/src/detail/framebuffercontext.cpp +++ b/modules/v4d/src/detail/framebuffercontext.cpp @@ -49,11 +49,8 @@ void FrameBufferContext::init() { #endif if (glfwInit() != GLFW_TRUE) assert(false); -#ifndef __EMSCRIPTEN__ glfwSetErrorCallback(cv::v4d::glfw_error_callback); - cerr << "after" << endl; - glfwDefaultWindowHints(); -#endif + if (debug_) glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); @@ -87,17 +84,16 @@ void FrameBufferContext::init() { glfwWindowHint(GLFW_STENCIL_BITS, 8); glfwWindowHint(GLFW_DEPTH_BITS, 24); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); -// glfwWindowHint(GLFW_DOUBLEBUFFER, GL_FALSE); glfwWindow_ = glfwCreateWindow(frameBufferSize_.width, frameBufferSize_.height, std::to_string(++window_cnt).c_str(), nullptr, sharedWindow_); if (glfwWindow_ == NULL) { assert(false); } - cerr << "win" << endl; this->makeCurrent(); - cerr << "current" << endl; + glfwSwapInterval(0); #ifndef OPENCV_V4D_USE_ES3 if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) throw std::runtime_error("Could not initialize GLAD!"); @@ -114,16 +110,19 @@ void FrameBufferContext::init() { cerr << "CL-GL sharing failed with unknown error." << endl; clglSharing_ = false; } - context_ = CLExecContext_t::getCurrent(); #else clglSharing_ = false; #endif +#ifndef __EMSCRIPTEN__ + context_ = CLExecContext_t::getCurrent(); +#endif + setup(frameBufferSize_); glfwSetWindowUserPointer(getGLFWWindow(), v4d_); glfwSetCursorPosCallback(getGLFWWindow(), [](GLFWwindow* glfwWin, double x, double y) { V4D* v4d = reinterpret_cast(glfwGetWindowUserPointer(glfwWin)); - v4d->nguiCtx().screen().cursor_pos_callback_event(x, y); + v4d->nguiCtx().screen().cursor_pos_callback_event(x * v4d->getXPixelRatio(), y * v4d->getXPixelRatio()); auto cursor = v4d->getMousePosition(); auto diff = cursor - cv::Vec2f(x, y); if (v4d->isMouseDrag()) { @@ -163,7 +162,7 @@ void FrameBufferContext::init() { V4D* v4d = reinterpret_cast(glfwGetWindowUserPointer(glfwWin)); std::vector widgets; for (auto* w : v4d->nguiCtx().screen().children()) { - auto mousePos = nanogui::Vector2i(v4d->getMousePosition()[0] / v4d->fbCtx().getXPixelRatio(), v4d->getMousePosition()[1] / v4d->fbCtx().getYPixelRatio()); + auto mousePos = nanogui::Vector2i(v4d->getMousePosition()[0] / v4d->getXPixelRatio(), v4d->getMousePosition()[1] / v4d->getYPixelRatio()); if(contains_absolute(w, mousePos)) { v4d->nguiCtx().screen().scroll_callback_event(x, y); return; @@ -174,15 +173,19 @@ void FrameBufferContext::init() { } ); +// glfwSetWindowSizeCallback(getGLFWWindow(), +// [](GLFWwindow* glfwWin, int width, int height) { +// V4D* v4d = reinterpret_cast(glfwGetWindowUserPointer(glfwWin)); +// v4d->nguiCtx().screen().resize_callback_event(width, height); +// }); glfwSetFramebufferSizeCallback(getGLFWWindow(), [](GLFWwindow* glfwWin, int width, int height) { V4D* v4d = reinterpret_cast(glfwGetWindowUserPointer(glfwWin)); - v4d->setWindowSize(cv::Size(width, height)); - cv::Rect& vp = v4d->viewport(); - vp.x = 0; - vp.y = 0; - vp.width = width; - vp.height = height; +// cv::Rect& vp = v4d->viewport(); +// vp.x = 0; +// vp.y = 0; +// vp.width = width; +// vp.height = height; #ifndef __EMSCRIPTEN__ if(v4d->isResizable()) { v4d->nvgCtx().fbCtx().teardown(); @@ -200,7 +203,6 @@ void FrameBufferContext::setup(const cv::Size& sz) { this->makeCurrent(); if(!isShared_) { GL_CHECK(glGenFramebuffers(1, &frameBufferID_)); - cerr << "GENFB1: " << frameBufferID_ << "/" << getGLFWWindow() << endl; GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID_)); GL_CHECK(glGenRenderbuffers(1, &renderBufferID_)); @@ -227,7 +229,6 @@ void FrameBufferContext::setup(const cv::Size& sz) { } else { assert(parent_ != nullptr); GL_CHECK(glGenFramebuffers(1, &frameBufferID_)); - cerr << "GENFB2: " << frameBufferID_ << "/" << getGLFWWindow() << endl; GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frameBufferID_)); GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_)); @@ -533,21 +534,12 @@ void FrameBufferContext::makeCurrent() { bool FrameBufferContext::isResizable() { makeCurrent(); - return glfwGetWindowAttrib(getGLFWWindow(), GLFW_RESIZABLE) == GLFW_TRUE; } void FrameBufferContext::setResizable(bool r) { makeCurrent(); - glfwWindowHint(GLFW_RESIZABLE, r ? GLFW_TRUE : GLFW_FALSE); -} - -cv::Size FrameBufferContext::getWindowSize() { - return windowSize_; -} - -void FrameBufferContext::setWindowSize(const cv::Size& sz) { - windowSize_ = sz; + glfwSetWindowAttrib(getGLFWWindow(), GLFW_RESIZABLE, r ? GLFW_TRUE : GLFW_FALSE); } void FrameBufferContext::resizeWindow(const cv::Size& sz) { diff --git a/modules/v4d/src/detail/framebuffercontext.hpp b/modules/v4d/src/detail/framebuffercontext.hpp index 1d6347671..9b36575a6 100644 --- a/modules/v4d/src/detail/framebuffercontext.hpp +++ b/modules/v4d/src/detail/framebuffercontext.hpp @@ -178,8 +178,6 @@ public: * To make it possible for other V4D objects to become current all other * V4D instances have to become non-current. */ - CV_EXPORTS void setWindowSize(const cv::Size& sz); - CV_EXPORTS cv::Size getWindowSize(); CV_EXPORTS void resizeWindow(const cv::Size& sz); CV_EXPORTS bool isFullscreen(); CV_EXPORTS void setFullscreen(bool f); diff --git a/modules/v4d/src/detail/nanoguicontext.cpp b/modules/v4d/src/detail/nanoguicontext.cpp index 01c6c7306..a1aa0e065 100644 --- a/modules/v4d/src/detail/nanoguicontext.cpp +++ b/modules/v4d/src/detail/nanoguicontext.cpp @@ -19,7 +19,6 @@ void NanoguiContext::init() { screen_ = new nanogui::Screen(); screen_->initialize(nguiFbContext_.getGLFWWindow(), false); form_ = new cv::v4d::FormHelper(screen_); - fbCtx().resizeWindow(fbCtx().getSize()); } void NanoguiContext::render() { diff --git a/modules/v4d/src/util.cpp b/modules/v4d/src/util.cpp index ae80e8e6e..06ab3f22c 100644 --- a/modules/v4d/src/util.cpp +++ b/modules/v4d/src/util.cpp @@ -32,7 +32,7 @@ namespace v4d { namespace detail { void run_sync_on_main(std::function fn) { #ifdef __EMSCRIPTEN__ - emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, cv::v4d::detail::get_fn_ptr<0>(fn)); + emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, cv::v4d::detail::get_fn_ptr<__COUNTER__>(fn)); #else fn(); #endif diff --git a/modules/v4d/src/v4d.cpp b/modules/v4d/src/v4d.cpp index bace62fc8..f9ebd0690 100644 --- a/modules/v4d/src/v4d.cpp +++ b/modules/v4d/src/v4d.cpp @@ -71,11 +71,6 @@ V4D::V4D(const cv::Size& size, bool offscreen, const string& title, int major, i nguiContext_ = new detail::NanoguiContext(*this, *mainFbContext_); clvaContext_ = new detail::CLVAContext(*this, *mainFbContext_); glContext_ = new detail::GLContext(*this, *mainFbContext_); -// fbCtx().resizeWindow(initialSize_); -// clvaCtx().fbCtx().resizeWindow(initialSize_); -// glCtx().fbCtx().resizeWindow(initialSize_); -// nvgCtx().fbCtx().resizeWindow(initialSize_); -// nguiCtx().fbCtx().resizeWindow(initialSize_); } V4D::~V4D() { @@ -199,7 +194,7 @@ void V4D::feed(cv::InputArray& in) { }, frame); fb([frame](cv::UMat& frameBuffer){ - cvtColor(frame,frameBuffer, cv::COLOR_BGR2BGRA); + cvtColor(frame,frameBuffer, cv::COLOR_RGB2BGRA); }); } @@ -405,6 +400,34 @@ cv::Rect& V4D::viewport() { return viewport_; } +float V4D::getXPixelRatio() { + fbCtx().makeCurrent(); +#ifdef __EMSCRIPTEN__ + float r = emscripten_get_device_pixel_ratio(); + + return r; +#else + float xscale, yscale; + glfwGetWindowContentScale(getGLFWWindow(), &xscale, &yscale); + + return xscale; +#endif +} + +float V4D::getYPixelRatio() { + fbCtx().makeCurrent(); +#ifdef __EMSCRIPTEN__ + float r = emscripten_get_device_pixel_ratio(); + + return r; +#else + float xscale, yscale; + glfwGetWindowContentScale(getGLFWWindow(), &xscale, &yscale); + + return yscale; +#endif +} + cv::Size V4D::getNativeFrameBufferSize() { fbCtx().makeCurrent(); int w, h; @@ -416,24 +439,12 @@ cv::Size V4D::getFrameBufferSize() { return fbCtx().getSize(); } -cv::Size V4D::getWindowSize() { - return fbCtx().getWindowSize(); -} - cv::Size V4D::getInitialSize() { return initialSize_; } -void V4D::setWindowSize(const cv::Size& sz) { - if(mainFbContext_ != nullptr) - fbCtx().setWindowSize(sz); - if(nguiContext_ != nullptr) - nguiCtx().screen().resize_callback_event(sz.width, sz.height); -} - void V4D::resizeWindow(const cv::Size& sz) { fbCtx().resizeWindow(sz); - fbCtx().setWindowSize(sz); } bool V4D::isFullscreen() { @@ -538,12 +549,12 @@ bool V4D::display() { nguiCtx().render(); - run_sync_on_main([this](){ + run_sync_on_main([&, this](){ FrameBufferContext::GLScope glScope(fbCtx()); - fbCtx().blitFrameBufferToScreen(viewport(), getWindowSize(), isStretching()); + fbCtx().blitFrameBufferToScreen(viewport(), getFrameBufferSize(), isStretching()); glfwSwapBuffers(fbCtx().getGLFWWindow()); glfwPollEvents(); - return !glfwWindowShouldClose(getGLFWWindow()); + result = !glfwWindowShouldClose(getGLFWWindow()); }); } diff --git a/modules/v4d/tutorials/04-vector_graphics_and_fb.markdown b/modules/v4d/tutorials/04-vector_graphics_and_fb.markdown index 5bdd5635b..3fcf6f735 100644 --- a/modules/v4d/tutorials/04-vector_graphics_and_fb.markdown +++ b/modules/v4d/tutorials/04-vector_graphics_and_fb.markdown @@ -1,4 +1,4 @@ -# Render vector graphics {#v4d_vector_graphics_and_fb} +# Render vector graphics and manipulate the framebuffer {#v4d_vector_graphics_and_fb} @prev_tutorial{v4d_vector_graphics} @next_tutorial{v4d_render_opengl} diff --git a/modules/v4d/tutorials/05-render_opengl.markdown b/modules/v4d/tutorials/05-render_opengl.markdown index 110020d67..1381fbc06 100644 --- a/modules/v4d/tutorials/05-render_opengl.markdown +++ b/modules/v4d/tutorials/05-render_opengl.markdown @@ -1,6 +1,6 @@ # OpenGL Rendering {#v4d_render_opengl} -@prev_tutorial{v4d_vector_graphics} +@prev_tutorial{v4d_vector_graphics_and_fb} @next_tutorial{v4d_font_rendering} | | |