diff --git a/src/common/subsystems.hpp b/src/common/subsystems.hpp index 37835ab7f..283530b3c 100644 --- a/src/common/subsystems.hpp +++ b/src/common/subsystems.hpp @@ -262,11 +262,11 @@ static void error_callback(int error, const char *description) { class Window { cv::Size size_; bool offscreen_; + string title_; int major_; int minor_; int samples_; bool debug_; - string title_; GLFWwindow *glfwWindow_ = nullptr; CLGLContext* clglContext_ = nullptr; CLVAContext* clvaContext_ = nullptr; @@ -276,8 +276,6 @@ class Window { nanogui::Screen* screen_ = nullptr; nanogui::FormHelper* form_ = nullptr; cv::TickMeter tickMeter_; - std::mutex pollMutex_; - bool startPolling_ = false; bool closed_ = false; public: @@ -579,9 +577,9 @@ public: } bool display() { - std::scoped_lock lock(pollMutex_); bool result = true; if (!offscreen_) { + glfwPollEvents(); screen_->draw_contents(); clglContext_->blitFrameBufferToScreen(); screen_->draw_widgets(); @@ -589,16 +587,9 @@ public: result = !glfwWindowShouldClose(glfwWindow_); } - startPolling_ = true; return result; } - void pollEvents() { - std::scoped_lock lock(pollMutex_); - if(startPolling_) - glfwPollEvents(); - } - bool isClosed() { return closed_; diff --git a/src/optflow/optflow-demo.cpp b/src/optflow/optflow-demo.cpp index 8ac1f4360..21d7a1d6e 100644 --- a/src/optflow/optflow-demo.cpp +++ b/src/optflow/optflow-demo.cpp @@ -184,10 +184,9 @@ void composite_layers(const cv::UMat background, const cv::UMat foreground, cons } void setup_gui(cv::Ptr window) { - window->makeWindow(6, 45, "Settings"); - - auto useOpenCL = window->makeFormVariable("Use OpenCL", use_opencl, "Enable or disable OpenCL acceleration"); + window->makeWindow(5, 45, "Settings"); + window->makeFormVariable("Use OpenCL", use_opencl, "Enable or disable OpenCL acceleration"); window->makeGroup("Foreground"); window->makeFormVariable("Scale", fg_scale, 0.1f, 4.0f, true, "", "Generate the foreground at this scale"); window->makeFormVariable("Loss", fg_loss, 0.1f, 99.9f, true, "%", "On every frame the foreground loses on brightness"); @@ -233,84 +232,74 @@ int main(int argc, char **argv) { exit(1); } cv::Ptr window = new kb::Window(cv::Size(WIDTH, HEIGHT), OFFSCREEN, "Sparse Optical Flow Demo"); + window->initialize(); - std::thread worker([&]() { - window->initialize(); - kb::print_system_info(); - setup_gui(window); + kb::print_system_info(); + setup_gui(window); - auto capture = window->makeVACapture(argv[1], VA_HW_DEVICE_INDEX); + auto capture = window->makeVACapture(argv[1], VA_HW_DEVICE_INDEX); - if (!capture.isOpened()) { - cerr << "ERROR! Unable to open video input" << endl; - exit(-1); - } + if (!capture.isOpened()) { + cerr << "ERROR! Unable to open video input" << endl; + exit(-1); + } - float fps = capture.get(cv::CAP_PROP_FPS); - - window->makeVAWriter(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'), fps, window->getSize(), VA_HW_DEVICE_INDEX); - - //BGRA - cv::UMat background, foreground(window->getSize(), CV_8UC4, cv::Scalar::all(0)); - //RGB - cv::UMat rgb, down; - //GREY - cv::UMat backgroundGrey, downPrevGrey, downNextGrey, downMotionMaskGrey; - vector detectedPoints; - - while (true) { - if(cv::ocl::useOpenCL() != use_opencl) - window->setUseOpenCL(use_opencl); - - if(!window->captureVA()) - break; - - window->compute([&](cv::UMat& frameBuffer){ - cv::resize(frameBuffer, down, cv::Size(window->getSize().width * fg_scale, window->getSize().height * fg_scale)); - cv::cvtColor(frameBuffer, background, cv::COLOR_RGB2BGRA); - }); - - cv::cvtColor(down, downNextGrey, cv::COLOR_RGB2GRAY); - //Subtract the background to create a motion mask - prepare_motion_mask(downNextGrey, downMotionMaskGrey); - //Detect trackable points in the motion mask - detect_points(downMotionMaskGrey, detectedPoints); - - window->renderNVG([&](NVGcontext* vg, const cv::Size& sz) { - window->clear(); - if (!downPrevGrey.empty()) { - //We don't want the algorithm to get out of hand when there is a scene change, so we suppress it when we detect one. - if (!detect_scene_change(downMotionMaskGrey, scene_change_thresh, scene_change_thresh_diff)) { - //Visualize the sparse optical flow using nanovg - cv::Scalar color = cv::Scalar(effect_color.r() * 255.0f, effect_color.g() * 255.0f, effect_color.b() * 255.0f, alpha * 255.0f); - visualize_sparse_optical_flow(vg, downPrevGrey, downNextGrey, detectedPoints, fg_scale, max_stroke, color, max_points, point_loss); - } + float fps = capture.get(cv::CAP_PROP_FPS); + + window->makeVAWriter(OUTPUT_FILENAME, cv::VideoWriter::fourcc('V', 'P', '9', '0'), fps, window->getSize(), VA_HW_DEVICE_INDEX); + + //BGRA + cv::UMat background, foreground(window->getSize(), CV_8UC4, cv::Scalar::all(0)); + //RGB + cv::UMat rgb, down; + //GREY + cv::UMat backgroundGrey, downPrevGrey, downNextGrey, downMotionMaskGrey; + vector detectedPoints; + + while (true) { + if(cv::ocl::useOpenCL() != use_opencl) + window->setUseOpenCL(use_opencl); + + if(!window->captureVA()) + break; + + window->compute([&](cv::UMat& frameBuffer){ + cv::resize(frameBuffer, down, cv::Size(window->getSize().width * fg_scale, window->getSize().height * fg_scale)); + cv::cvtColor(frameBuffer, background, cv::COLOR_RGB2BGRA); + }); + + cv::cvtColor(down, downNextGrey, cv::COLOR_RGB2GRAY); + //Subtract the background to create a motion mask + prepare_motion_mask(downNextGrey, downMotionMaskGrey); + //Detect trackable points in the motion mask + detect_points(downMotionMaskGrey, detectedPoints); + + window->renderNVG([&](NVGcontext* vg, const cv::Size& sz) { + window->clear(); + if (!downPrevGrey.empty()) { + //We don't want the algorithm to get out of hand when there is a scene change, so we suppress it when we detect one. + if (!detect_scene_change(downMotionMaskGrey, scene_change_thresh, scene_change_thresh_diff)) { + //Visualize the sparse optical flow using nanovg + cv::Scalar color = cv::Scalar(effect_color.r() * 255.0f, effect_color.g() * 255.0f, effect_color.b() * 255.0f, alpha * 255.0f); + visualize_sparse_optical_flow(vg, downPrevGrey, downNextGrey, detectedPoints, fg_scale, max_stroke, color, max_points, point_loss); } - }); - - downPrevGrey = downNextGrey.clone(); + } + }); - window->compute([&](cv::UMat& frameBuffer){ - //Put it all together (OpenCL) - composite_layers(background, foreground, frameBuffer, frameBuffer, glow_kernel_size, fg_loss); - }); + downPrevGrey = downNextGrey.clone(); - window->writeVA(); + window->compute([&](cv::UMat& frameBuffer){ + //Put it all together (OpenCL) + composite_layers(background, foreground, frameBuffer, frameBuffer, glow_kernel_size, fg_loss); + }); - update_fps(window, show_fps); - //If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed. - if(!window->display()) - break; - } - window->close(); - }); + window->writeVA(); - while(!window->isClosed()) { - window->pollEvents(); - std::this_thread::sleep_for(10ms); + update_fps(window, show_fps); + //If onscreen rendering is enabled it displays the framebuffer in the native window. Returns false if the window was closed. + if(!window->display()) + break; } - worker.join(); - return 0; }