From 4e005ead0ae4c95e450e98358a2f37a25c82638f Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Fri, 22 Oct 2021 14:16:14 -0700 Subject: [PATCH] UI: don't block UI on vipc recv (#22644) * UI: don't block UI on vipc recv * use a timer * move that old-commit-hash: c4de1fef4f925e9ba5292f501c2acd70dd0f1505 --- selfdrive/ui/SConscript | 2 +- selfdrive/ui/qt/onroad.cc | 14 +------------- selfdrive/ui/qt/onroad.h | 1 - selfdrive/ui/qt/widgets/cameraview.cc | 25 ++++++++++++++----------- selfdrive/ui/qt/widgets/cameraview.h | 2 ++ 5 files changed, 18 insertions(+), 26 deletions(-) diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index bfbf2c06d5..be7d95372c 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -114,7 +114,7 @@ if arch in ['x86_64', 'Darwin'] and os.path.exists(Dir("#tools/").get_abspath()) replay_libs = [replay_lib, 'avutil', 'avcodec', 'avformat', 'bz2', 'curl', 'yuv'] + qt_libs qt_env.Program("replay/replay", ["replay/main.cc"], LIBS=replay_libs) - qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs) + qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11']) if GetOption('test'): qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs]) diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 66eec42b15..30bc7fe4f3 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -18,7 +18,6 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { main_layout->addLayout(stacked_layout); nvg = new NvgWindow(VISION_STREAM_RGB_BACK, this); - QObject::connect(this, &OnroadWindow::updateStateSignal, nvg, &NvgWindow::updateState); QWidget * split_wrapper = new QWidget; split = new QHBoxLayout(split_wrapper); @@ -179,18 +178,7 @@ void NvgWindow::initializeGL() { ui_nvg_init(&QUIState::ui_state); prev_draw_t = millis_since_boot(); -} - -void NvgWindow::updateState(const UIState &s) { - // TODO: make camerad startup faster then remove this - if (s.scene.started) { - if (isVisible() != vipc_client->connected) { - setVisible(vipc_client->connected); - } - if (!isVisible()) { - updateFrame(); - } - } + setBackgroundColor(bg_colors[STATUS_DISENGAGED]); } void NvgWindow::paintGL() { diff --git a/selfdrive/ui/qt/onroad.h b/selfdrive/ui/qt/onroad.h index 4066b3d8eb..9be04e04ae 100644 --- a/selfdrive/ui/qt/onroad.h +++ b/selfdrive/ui/qt/onroad.h @@ -30,7 +30,6 @@ class NvgWindow : public CameraViewWidget { public: explicit NvgWindow(VisionStreamType type, QWidget* parent = 0) : CameraViewWidget(type, true, parent) {} - void updateState(const UIState &s); protected: void paintGL() override; diff --git a/selfdrive/ui/qt/widgets/cameraview.cc b/selfdrive/ui/qt/widgets/cameraview.cc index cc46f5f3dc..39d1330959 100644 --- a/selfdrive/ui/qt/widgets/cameraview.cc +++ b/selfdrive/ui/qt/widgets/cameraview.cc @@ -1,7 +1,5 @@ #include "selfdrive/ui/qt/widgets/cameraview.h" -#include "selfdrive/common/swaglog.h" - namespace { const char frame_vertex_shader[] = @@ -96,7 +94,10 @@ mat4 get_fit_view_transform(float widget_aspect_ratio, float frame_aspect_ratio) CameraViewWidget::CameraViewWidget(VisionStreamType stream_type, bool zoom, QWidget* parent) : stream_type(stream_type), zoomed_view(zoom), QOpenGLWidget(parent) { setAttribute(Qt::WA_OpaquePaintEvent); - connect(this, &QOpenGLWidget::frameSwapped, this, &CameraViewWidget::updateFrame); + + QTimer *t = new QTimer(this); + connect(t, &QTimer::timeout, this, &CameraViewWidget::updateFrame); + t->start(1000 / UI_FREQ); } CameraViewWidget::~CameraViewWidget() { @@ -173,6 +174,10 @@ void CameraViewWidget::setStreamType(VisionStreamType type) { } } +void CameraViewWidget::setBackgroundColor(QColor color) { + bg = color; +} + void CameraViewWidget::updateFrameMat(int w, int h) { if (zoomed_view) { if (stream_type == VISION_STREAM_RGB_FRONT) { @@ -204,7 +209,7 @@ void CameraViewWidget::updateFrameMat(int w, int h) { void CameraViewWidget::paintGL() { if (!latest_frame) { - glClearColor(0, 0, 0, 1.0); + glClearColor(bg.redF(), bg.greenF(), bg.blueF(), bg.alphaF()); glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT); return; } @@ -233,6 +238,10 @@ void CameraViewWidget::paintGL() { } void CameraViewWidget::updateFrame() { + if (!isVisible()) { + return; + } + if (!vipc_client->connected) { makeCurrent(); if (vipc_client->connect(false)) { @@ -257,17 +266,11 @@ void CameraViewWidget::updateFrame() { VisionBuf *buf = nullptr; if (vipc_client->connected) { - buf = vipc_client->recv(); + buf = vipc_client->recv(nullptr, 0); if (buf != nullptr) { latest_frame = buf; update(); emit frameUpdated(); - } else { - LOGE("visionIPC receive timeout"); } } - if (buf == nullptr && isVisible()) { - // try to connect or recv again - QTimer::singleShot(1000. / UI_FREQ, this, &CameraViewWidget::updateFrame); - } } diff --git a/selfdrive/ui/qt/widgets/cameraview.h b/selfdrive/ui/qt/widgets/cameraview.h index e4e1346458..3bf34ac58e 100644 --- a/selfdrive/ui/qt/widgets/cameraview.h +++ b/selfdrive/ui/qt/widgets/cameraview.h @@ -19,6 +19,7 @@ public: explicit CameraViewWidget(VisionStreamType stream_type, bool zoom, QWidget* parent = nullptr); ~CameraViewWidget(); void setStreamType(VisionStreamType type); + void setBackgroundColor(QColor color); signals: void clicked(); @@ -45,4 +46,5 @@ private: QOpenGLShaderProgram *program; VisionStreamType stream_type; + QColor bg = QColor("#000000"); };