From b73e8ab324716f2e622df205b440201e57cd2a1b Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Wed, 21 Oct 2020 15:15:21 +0200 Subject: [PATCH] improve Qt ui smoothess (#2380) * improve ui smoothess * fix backlight filter * More default brightness * write backlight in real background thread * immediate repaint --- selfdrive/ui/qt/window.cc | 55 +++++++++++++++++++++++--------------- selfdrive/ui/qt/window.hpp | 3 +++ 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/selfdrive/ui/qt/window.cc b/selfdrive/ui/qt/window.cc index e9dbd2189..ceea1219b 100644 --- a/selfdrive/ui/qt/window.cc +++ b/selfdrive/ui/qt/window.cc @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include @@ -16,9 +16,21 @@ #include "paint.hpp" #include "common/util.h" +#include "common/timing.h" + +#define BACKLIGHT_DT 0.25 +#define BACKLIGHT_TS 2.00 volatile sig_atomic_t do_exit = 0; +static void set_backlight(int brightness){ + std::ofstream brightness_control("/sys/class/backlight/panel0-backlight/brightness"); + if (brightness_control.is_open()){ + brightness_control << brightness << "\n"; + brightness_control.close(); + } +} + MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { main_layout = new QStackedLayout; @@ -59,11 +71,14 @@ GLWindow::GLWindow(QWidget *parent) : QOpenGLWidget(parent) { timer = new QTimer(this); QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate())); + backlight_timer = new QTimer(this); + QObject::connect(backlight_timer, SIGNAL(timeout()), this, SLOT(backlightUpdate())); + int result = read_param(&brightness_b, "BRIGHTNESS_B", true); result += read_param(&brightness_m, "BRIGHTNESS_M", true); if(result != 0) { - brightness_b = 0.0; - brightness_m = 5.0; + brightness_b = 200.0; + brightness_m = 10.0; } smooth_brightness = 512; } @@ -86,39 +101,37 @@ void GLWindow::initializeGL() { ui_state->fb_h = vwp_h; ui_init(ui_state); - timer->start(50); + timer->start(0); + backlight_timer->start(BACKLIGHT_DT * 100); } -void GLWindow::timerUpdate(){ +void GLWindow::backlightUpdate(){ // Update brightness + float k = (BACKLIGHT_DT / BACKLIGHT_TS) / (1.0f + BACKLIGHT_DT / BACKLIGHT_TS); + float clipped_brightness = std::min(1023.0f, (ui_state->light_sensor*brightness_m) + brightness_b); - smooth_brightness = clipped_brightness * 0.01f + smooth_brightness * 0.99f; + smooth_brightness = clipped_brightness * k + smooth_brightness * (1.0f - k); int brightness = smooth_brightness; - #ifdef QCOM2 - if (ui_state->started != onroad){ - onroad = ui_state->started; - timer->setInterval(onroad ? 50 : 1000); - } - if (!ui_state->started){ brightness = 0; } #endif - auto f = std::async(std::launch::async, - [brightness]{ - std::ofstream brightness_control("/sys/class/backlight/panel0-backlight/brightness"); - if (brightness_control.is_open()){ - brightness_control << brightness << "\n"; - brightness_control.close(); - } - }); + std::thread{set_backlight, brightness}.detach(); +} +void GLWindow::timerUpdate(){ +#ifdef QCOM2 + if (ui_state->started != onroad){ + onroad = ui_state->started; + timer->setInterval(onroad ? 0 : 1000); + } +#endif ui_update(ui_state); - update(); + repaint(); } void GLWindow::resizeGL(int w, int h) { diff --git a/selfdrive/ui/qt/window.hpp b/selfdrive/ui/qt/window.hpp index cc525a6e0..8c82e6023 100644 --- a/selfdrive/ui/qt/window.hpp +++ b/selfdrive/ui/qt/window.hpp @@ -51,6 +51,8 @@ protected: private: QTimer * timer; + QTimer * backlight_timer; + UIState * ui_state; QtSound sound; @@ -62,6 +64,7 @@ private: public slots: void timerUpdate(); + void backlightUpdate(); signals: void openSettings();