opencv / opencv

Open Source Computer Vision Library
https://opencv.org
Apache License 2.0
75.95k stars 55.62k forks source link

highgui: wayland: too high cpu usage #25501

Closed Kumataro closed 2 weeks ago

Kumataro commented 2 weeks ago

System Information

OpenCV version: 4.x ( 2cd330486ec4597eab49c1575fc4a6603f205a6a ) Operating System / Platform: Ubuntu 24.04 Compiler & compiler version: GCC 13.2

Detailed description

We expect that cv::waitKey(1000) requests low cpu usage. However, with Wayland backend, user application runs with 100% cpu usage.

https://github.com/opencv/opencv/blob/12e2cc9502bc51bb01ed3fdd2f39ce1533c8236e/modules/highgui/src/window_wayland.cpp#L2450-L2458

When elapsed is larger then 64000(nanosec = 64 millisec), sleep_time will be negative. In this case std::this_thread::sleep_for() will not be called.

So CvWlCore::getInstance().display().run_once(); is called again and again. It makes high CPU usage.

Steps to reproduce

// g++ main.cpp -o a.out -I /usr/local/include/opencv4 -lopencv_core -lopencv_highgui -lopencv_imgcodecs
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
  std::cout << "cv::currentUIFramework() returns " << cv::currentUIFramework() << std::endl;

  cv::Mat src;
  src = cv::imread("opencv-logo.png");

  cv::namedWindow("src");

  int key = 0;
  do
  {
      cv::imshow("src", src);
      key = cv::waitKey(1000);
  } while( key != 'q' );
  return 0;
}

Issue submission checklist