diff --git a/modules/v4d/include/opencv2/v4d/dialog.hpp b/modules/v4d/include/opencv2/v4d/dialog.hpp index 22b9dd9e4..91211be03 100644 --- a/modules/v4d/include/opencv2/v4d/dialog.hpp +++ b/modules/v4d/include/opencv2/v4d/dialog.hpp @@ -6,6 +6,7 @@ #ifndef SRC_OPENCV_V4D_DIALOG_HPP_ #define SRC_OPENCV_V4D_DIALOG_HPP_ +#include #include #include #include @@ -18,6 +19,16 @@ namespace v4d { using std::string; +class CustomTheme : public nanogui::Theme { +public: + CustomTheme(NVGcontext *ctx) : nanogui::Theme(ctx) { + m_window_drop_shadow_size = 0; + } + + virtual ~CustomTheme() { + } +}; + /*! * A class for light-weight dialog (a dialog renderered inside a window) derived from nanogui::Window. * It keeps track of which dialogs are presented and which are lowered and is responsible for layout of lowered dialog-bars. @@ -34,6 +45,7 @@ private: nanogui::ref oldLayout_; nanogui::ref newLayout_; bool minimized_ = false; + nanogui::ref customTheme_; bool mouse_drag_event(const nanogui::Vector2i& p, const nanogui::Vector2i& rel, int button, int mods) override; public: diff --git a/modules/v4d/include/opencv2/v4d/util.hpp b/modules/v4d/include/opencv2/v4d/util.hpp index 3c74eac36..6a340f4f8 100644 --- a/modules/v4d/include/opencv2/v4d/util.hpp +++ b/modules/v4d/include/opencv2/v4d/util.hpp @@ -27,12 +27,9 @@ #include #include -const static std::thread::id MAIN_THREAD_ID = std::this_thread::get_id(); - namespace cv { namespace v4d { namespace detail { - template struct fun_ptr_helper { @@ -82,9 +79,6 @@ make_function(T *t) template void run_sync_on_main(std::function fn) { #ifdef __EMSCRIPTEN__ -// if(MAIN_THREAD_ID == std::this_thread::get_id()) { -// throw std::runtime_error("proxying from main thread"); -// } emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, cv::v4d::detail::get_fn_ptr(fn)); #else fn(); diff --git a/modules/v4d/src/detail/framebuffercontext.cpp b/modules/v4d/src/detail/framebuffercontext.cpp index 406253a44..33e0f8840 100644 --- a/modules/v4d/src/detail/framebuffercontext.cpp +++ b/modules/v4d/src/detail/framebuffercontext.cpp @@ -107,8 +107,6 @@ void FrameBufferContext::loadBuffers() { void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) { #ifdef __EMSCRIPTEN__ - cerr << "init: " << dst.getFramebufferID() << endl; - this->makeCurrent(); GL_CHECK(glGenFramebuffers(1, ©Framebuffer_)); GL_CHECK(glGenTextures(1, ©Texture_)); @@ -126,7 +124,6 @@ void FrameBufferContext::initWebGLCopy(FrameBufferContext& dst) { void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) { #ifdef __EMSCRIPTEN__ - cerr << "copy: " << dst.getFramebufferID() << endl; int width = dst.getWindowSize().width; int height = dst.getWindowSize().height; { @@ -170,8 +167,6 @@ void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) { }, dst.getIndex() - 1); GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - GL_CHECK(glFlush()); - GL_CHECK(glFinish()); GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT1)); GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, this->getFramebufferID())); glViewport(0, 0, width, height); @@ -186,9 +181,7 @@ void FrameBufferContext::doWebGLCopy(FrameBufferContext& dst) { GL_CHECK(glBindVertexArray(copyVao)); GL_CHECK(glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)); -// dst.makeCurrent(); GL_CHECK(glFlush()); - GL_CHECK(glFinish()); #else throw std::runtime_error("WebGL not supported in none WASM builds"); #endif @@ -464,7 +457,6 @@ cv::Size FrameBufferContext::size() { } void FrameBufferContext::copyTo(cv::UMat& dst) { - cerr << "copyTo:" << getFramebufferID() << endl; run_sync_on_main<7>([&,this](){ #ifndef __EMSCRIPTEN__ CLExecScope_t clExecScope(getCLExecContext()); @@ -476,8 +468,6 @@ void FrameBufferContext::copyTo(cv::UMat& dst) { } void FrameBufferContext::copyFrom(const cv::UMat& src) { - cerr << "copyFrom:" << getFramebufferID() << endl; - run_sync_on_main<18>([&,this](){ #ifndef __EMSCRIPTEN__ CLExecScope_t clExecScope(getCLExecContext()); @@ -496,6 +486,9 @@ void FrameBufferContext::execute(std::function fn) { FrameBufferContext::GLScope glScope(*this, GL_FRAMEBUFFER); FrameBufferContext::FrameBufferScope fbScope(*this, framebuffer_); fn(framebuffer_); +#ifndef __EMSCRIPTEN__ + GL_CHECK(glFinish()); +#endif }); } @@ -562,6 +555,7 @@ void FrameBufferContext::blitFrameBufferToScreen(const cv::Rect& viewport, void FrameBufferContext::begin(GLenum framebufferTarget) { this->makeCurrent(); + GL_CHECK(glFinish()); GL_CHECK(glBindFramebuffer(framebufferTarget, frameBufferID_)); GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID_)); GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, renderBufferID_)); @@ -576,15 +570,12 @@ void FrameBufferContext::begin(GLenum framebufferTarget) { void FrameBufferContext::end() { GL_CHECK(glFlush()); - GL_CHECK(glFinish()); } void FrameBufferContext::download(cv::UMat& m) { cv::Mat tmp = m.getMat(cv::ACCESS_WRITE); assert(tmp.data != nullptr); GL_CHECK(glReadPixels(0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data)); - GL_CHECK(glFlush()); - GL_CHECK(glFinish()); tmp.release(); } @@ -593,8 +584,6 @@ void FrameBufferContext::upload(const cv::UMat& m) { assert(tmp.data != nullptr); GL_CHECK( glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, tmp.cols, tmp.rows, GL_RGBA, GL_UNSIGNED_BYTE, tmp.data)); - GL_CHECK(glFlush()); - GL_CHECK(glFinish()); tmp.release(); } diff --git a/modules/v4d/src/detail/nanoguicontext.cpp b/modules/v4d/src/detail/nanoguicontext.cpp index d5181b8b9..54f871702 100644 --- a/modules/v4d/src/detail/nanoguicontext.cpp +++ b/modules/v4d/src/detail/nanoguicontext.cpp @@ -76,10 +76,11 @@ void NanoguiContext::render(bool print, bool graphical) { GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); // GLfloat cColor[4]; // glGetFloatv(GL_COLOR_CLEAR_VALUE, cColor); - glClearColor(0,0,0,0); - glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); +// glClearColor(0,0,1,1); +// glClearColor(0,0,0,0); +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // glClearColor(cColor[0], cColor[1], cColor[2], cColor[3]); -// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); } { FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); @@ -112,8 +113,6 @@ void NanoguiContext::render(bool print, bool graphical) { nvgEndFrame(context_); nvgRestore(context_); screen().draw_widgets(); - GL_CHECK(glFlush()); - GL_CHECK(glFinish()); } if(!fbCtx().isShared()) { @@ -131,108 +130,17 @@ void NanoguiContext::render(bool print, bool graphical) { tick_.start(); } -//void NanoguiContext::updateFps(bool print, bool graphical) { -// if (!first_) { -// tick_.stop(); -// -// if (tick_.getTimeMilli() > 50) { -// if(print) { -// cerr << "FPS : " << (fps_ = tick_.getFPS()); -//#ifndef __EMSCRIPTEN__ -// cerr << '\r'; -//#else -// cerr << endl; -//#endif -// } -// tick_.reset(); -// } -// -// if (graphical) { -// run_sync_on_main<24>([&, this](){ -// string txt = "FPS: " + std::to_string(fps_); -//#ifndef __EMSCRIPTEN__ -// if(!fbCtx().isShared()) { -// mainFbContext_.copyTo(copyBuffer_); -// fbCtx().copyFrom(copyBuffer_); -// } -//#endif -// { -// #ifndef __EMSCRIPTEN__ -// mainFbContext_.makeCurrent(); -// GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); -// GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); -// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); -// #else -// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); -// GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); -//// GLfloat cColor[4]; -//// glGetFloatv(GL_COLOR_CLEAR_VALUE, cColor); -//// glClearColor(0,0,0,0); -//// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); -//// glClearColor(cColor[0], cColor[1], cColor[2], cColor[3]); -// glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); -// } -// { -// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); -// #endif -// float w = mainFbContext_.size().width; -// float h = mainFbContext_.size().height; -// float r = mainFbContext_.pixelRatioX(); -// -// nvgSave(context_); -// nvgBeginFrame(context_, w, h, r); -// cv::v4d::nvg::detail::NVG::initializeContext(context_); -// -// using namespace cv::v4d::nvg; -// beginPath(); -// roundedRect(5, 5, 15 * txt.size() + 5, 30, 5); -// fillColor(cv::Scalar(255, 255, 255, 180)); -// fill(); -// } -// { -//#ifdef __EMSCRIPTEN__ -// FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); -//#endif -// using namespace cv::v4d::nvg; -// fontSize(30.0f); -// fontFace("mono"); -// fillColor(cv::Scalar(90, 90, 90, 255)); -// textAlign(NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); -// text(10, 20, txt.c_str(), nullptr); -// -// nvgEndFrame(context_); -// nvgRestore(context_); -// GL_CHECK(glFlush()); -// GL_CHECK(glFinish()); -// } -// if(!fbCtx().isShared()) { -//#ifdef __EMSCRIPTEN__ -// mainFbContext_.doWebGLCopy(fbCtx()); -//#else -// fbCtx().copyTo(copyBuffer_); -// mainFbContext_.copyFrom(copyBuffer_); -//#endif -// } -// fbCtx().makeCurrent(); -// GL_CHECK(glFlush()); -// GL_CHECK(glFinish()); -// mainFbContext_.makeCurrent(); -// GL_CHECK(glFlush()); -// GL_CHECK(glFinish()); -// }); -// -// -// } -// } -// first_ = false; -// tick_.start(); -//} - void NanoguiContext::build(std::function fn) { run_sync_on_main<5>([fn,this](){ - FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); - GL_CHECK(glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); - GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); +#ifndef __EMSCRIPTEN__ + mainFbContext_.makeCurrent(); + GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); + GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); +#else + FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); + GL_CHECK(glViewport(0, 0, mainFbContext_.getWindowSize().width, mainFbContext_.getWindowSize().height)); + +#endif fn(form()); screen().perform_layout(); }); diff --git a/modules/v4d/src/dialog.cpp b/modules/v4d/src/dialog.cpp index cb2ec3e12..149217af1 100644 --- a/modules/v4d/src/dialog.cpp +++ b/modules/v4d/src/dialog.cpp @@ -17,7 +17,8 @@ std::set Dialog::all_windows_xsor v4dWin_Xcomparator); Dialog::Dialog(nanogui::Screen* screen, int x, int y, const string& title) : - Window(screen, title), screen_(screen), lastDragPos_(x, y) { + Window(screen, title), screen_(screen), lastDragPos_(x, y), customTheme_(new CustomTheme(screen->nvg_context())) { + set_theme(customTheme_); all_windows_xsorted_.insert(this); oldLayout_ = new nanogui::AdvancedGridLayout( { 10, 0, 10, 0 }, { }); oldLayout_->set_margin(10); diff --git a/modules/v4d/src/v4d.cpp b/modules/v4d/src/v4d.cpp index c9b873583..e928543e7 100644 --- a/modules/v4d/src/v4d.cpp +++ b/modules/v4d/src/v4d.cpp @@ -675,13 +675,19 @@ bool V4D::display() { fbCtx().makeCurrent(); #ifndef __EMSCRIPTEN__ glfwSwapBuffers(fbCtx().getGLFWWindow()); - GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); - GL_CHECK(glClearColor(0, 0, 0, 1)); - GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); #else emscripten_webgl_commit_frame(); #endif - + { +#ifndef __EMSCRIPTEN__ + fbCtx().makeCurrent(); + GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0)); +#else + FrameBufferContext::GLScope glScope(fbCtx(), GL_FRAMEBUFFER); +#endif + GL_CHECK(glClearColor(0, 0, 0, 0)); + GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); + } glfwPollEvents(); result = !glfwWindowShouldClose(getGLFWWindow());