From a3cfa444d37bacdac5278335ac7a49b6cb5b81f0 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Fri, 29 Apr 2022 19:55:44 +0200 Subject: [PATCH] ui: combine OnroadHud into NvgWindow (#24369) * ui: combine OnroadHUD and NVGWindow * draw hud first * cleanup * removed commented line * fix text rendering * increase cpu usage --- selfdrive/test/test_onroad.py | 2 +- selfdrive/ui/qt/onroad.cc | 42 +++++++----------- selfdrive/ui/qt/onroad.h | 64 +++++++++++---------------- selfdrive/ui/qt/widgets/cameraview.cc | 1 - 4 files changed, 44 insertions(+), 65 deletions(-) diff --git a/selfdrive/test/test_onroad.py b/selfdrive/test/test_onroad.py index b845ee0e09..0fea95d0f5 100755 --- a/selfdrive/test/test_onroad.py +++ b/selfdrive/test/test_onroad.py @@ -26,7 +26,7 @@ PROCS = { "./camerad": 26.0, "./locationd": 9.1, "selfdrive.controls.plannerd": 11.7, - "./_ui": 18.4, + "./_ui": 21.0, "selfdrive.locationd.paramsd": 9.0, "./_sensord": 6.17, "selfdrive.controls.radard": 4.5, diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 844095a11d..cee347daa9 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -18,18 +18,13 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { stacked_layout->setStackingMode(QStackedLayout::StackAll); main_layout->addLayout(stacked_layout); - QStackedLayout *road_view_layout = new QStackedLayout; - road_view_layout->setStackingMode(QStackedLayout::StackAll); nvg = new NvgWindow(VISION_STREAM_ROAD, this); - road_view_layout->addWidget(nvg); - hud = new OnroadHud(this); - road_view_layout->addWidget(hud); QWidget * split_wrapper = new QWidget; split = new QHBoxLayout(split_wrapper); split->setContentsMargins(0, 0, 0, 0); split->setSpacing(0); - split->addLayout(road_view_layout); + split->addWidget(nvg); stacked_layout->addWidget(split_wrapper); @@ -57,7 +52,7 @@ void OnroadWindow::updateState(const UIState &s) { alerts->updateAlert(alert, bgColor); } - hud->updateState(s); + nvg->updateState(s); if (bg != bgColor) { // repaint border @@ -167,15 +162,14 @@ void OnroadAlerts::paintEvent(QPaintEvent *event) { } } -// OnroadHud -OnroadHud::OnroadHud(QWidget *parent) : QWidget(parent) { +// NvgWindow + +NvgWindow::NvgWindow(VisionStreamType type, QWidget* parent) : fps_filter(UI_FREQ, 3, 1. / UI_FREQ), CameraViewWidget("camerad", type, true, parent) { engage_img = loadPixmap("../assets/img_chffr_wheel.png", {img_size, img_size}); dm_img = loadPixmap("../assets/img_driver_face.png", {img_size, img_size}); - - connect(this, &OnroadHud::valueChanged, [=] { update(); }); } -void OnroadHud::updateState(const UIState &s) { +void NvgWindow::updateState(const UIState &s) { const int SET_SPEED_NA = 255; const SubMaster &sm = *(s.sm); const auto cs = sm["controlsState"].getControlsState(); @@ -202,9 +196,8 @@ void OnroadHud::updateState(const UIState &s) { } } -void OnroadHud::paintEvent(QPaintEvent *event) { - QPainter p(this); - p.setRenderHint(QPainter::Antialiasing); +void NvgWindow::drawHud(QPainter &p) { + p.save(); // Header gradient QLinearGradient bg(0, header_h - (header_h / 2.5), 0, header_h); @@ -246,9 +239,10 @@ void OnroadHud::paintEvent(QPaintEvent *event) { drawIcon(p, radius / 2 + (bdr_s * 2), rect().bottom() - footer_h / 2, dm_img, QColor(0, 0, 0, 70), dmActive ? 1.0 : 0.2); } + p.restore(); } -void OnroadHud::drawText(QPainter &p, int x, int y, const QString &text, int alpha) { +void NvgWindow::drawText(QPainter &p, int x, int y, const QString &text, int alpha) { QFontMetrics fm(p.font()); QRect init_rect = fm.boundingRect(text); QRect real_rect = fm.boundingRect(init_rect, 0, text); @@ -258,7 +252,7 @@ void OnroadHud::drawText(QPainter &p, int x, int y, const QString &text, int alp p.drawText(real_rect.x(), real_rect.bottom(), text); } -void OnroadHud::drawIcon(QPainter &p, int x, int y, QPixmap &img, QBrush bg, float opacity) { +void NvgWindow::drawIcon(QPainter &p, int x, int y, QPixmap &img, QBrush bg, float opacity) { p.setPen(Qt::NoPen); p.setBrush(bg); p.drawEllipse(x - radius / 2, y - radius / 2, radius, radius); @@ -266,11 +260,6 @@ void OnroadHud::drawIcon(QPainter &p, int x, int y, QPixmap &img, QBrush bg, flo p.drawPixmap(x - img_size / 2, y - img_size / 2, img); } -// NvgWindow - -NvgWindow::NvgWindow(VisionStreamType type, QWidget* parent) : fps_filter(UI_FREQ, 3, 1. / UI_FREQ), CameraViewWidget("camerad", type, true, parent) { - -} void NvgWindow::initializeGL() { CameraViewWidget::initializeGL(); @@ -377,11 +366,14 @@ void NvgWindow::drawLead(QPainter &painter, const cereal::ModelDataV2::LeadDataV void NvgWindow::paintGL() { CameraViewWidget::paintGL(); + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.setPen(Qt::NoPen); + + drawHud(painter); + UIState *s = uiState(); if (s->worldObjectsVisible()) { - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - painter.setPen(Qt::NoPen); drawLaneLines(painter, s); diff --git a/selfdrive/ui/qt/onroad.h b/selfdrive/ui/qt/onroad.h index 1d95fa49f8..f4c62d1c7d 100644 --- a/selfdrive/ui/qt/onroad.h +++ b/selfdrive/ui/qt/onroad.h @@ -9,26 +9,40 @@ // ***** onroad widgets ***** +class OnroadAlerts : public QWidget { + Q_OBJECT + +public: + OnroadAlerts(QWidget *parent = 0) : QWidget(parent) {}; + void updateAlert(const Alert &a, const QColor &color); -class OnroadHud : public QWidget { +protected: + void paintEvent(QPaintEvent*) override; + +private: + QColor bg; + Alert alert = {}; +}; + +// container window for the NVG UI +class NvgWindow : public CameraViewWidget { Q_OBJECT - Q_PROPERTY(QString speed MEMBER speed NOTIFY valueChanged); - Q_PROPERTY(QString speedUnit MEMBER speedUnit NOTIFY valueChanged); - Q_PROPERTY(QString maxSpeed MEMBER maxSpeed NOTIFY valueChanged); - Q_PROPERTY(bool is_cruise_set MEMBER is_cruise_set NOTIFY valueChanged); - Q_PROPERTY(bool engageable MEMBER engageable NOTIFY valueChanged); - Q_PROPERTY(bool dmActive MEMBER dmActive NOTIFY valueChanged); - Q_PROPERTY(bool hideDM MEMBER hideDM NOTIFY valueChanged); - Q_PROPERTY(int status MEMBER status NOTIFY valueChanged); + Q_PROPERTY(QString speed MEMBER speed); + Q_PROPERTY(QString speedUnit MEMBER speedUnit); + Q_PROPERTY(QString maxSpeed MEMBER maxSpeed); + Q_PROPERTY(bool is_cruise_set MEMBER is_cruise_set); + Q_PROPERTY(bool engageable MEMBER engageable); + Q_PROPERTY(bool dmActive MEMBER dmActive); + Q_PROPERTY(bool hideDM MEMBER hideDM); + Q_PROPERTY(int status MEMBER status); public: - explicit OnroadHud(QWidget *parent); + explicit NvgWindow(VisionStreamType type, QWidget* parent = 0); void updateState(const UIState &s); private: void drawIcon(QPainter &p, int x, int y, QPixmap &img, QBrush bg, float opacity); void drawText(QPainter &p, int x, int y, const QString &text, int alpha = 255); - void paintEvent(QPaintEvent *event) override; QPixmap engage_img; QPixmap dm_img; @@ -43,32 +57,6 @@ private: bool hideDM = false; int status = STATUS_DISENGAGED; -signals: - void valueChanged(); -}; - -class OnroadAlerts : public QWidget { - Q_OBJECT - -public: - OnroadAlerts(QWidget *parent = 0) : QWidget(parent) {}; - void updateAlert(const Alert &a, const QColor &color); - -protected: - void paintEvent(QPaintEvent*) override; - -private: - QColor bg; - Alert alert = {}; -}; - -// container window for the NVG UI -class NvgWindow : public CameraViewWidget { - Q_OBJECT - -public: - explicit NvgWindow(VisionStreamType type, QWidget* parent = 0); - protected: void paintGL() override; void initializeGL() override; @@ -76,6 +64,7 @@ protected: void updateFrameMat(int w, int h) override; void drawLaneLines(QPainter &painter, const UIState *s); void drawLead(QPainter &painter, const cereal::ModelDataV2::LeadDataV3::Reader &lead_data, const QPointF &vd); + void drawHud(QPainter &p); inline QColor redColor(int alpha = 255) { return QColor(201, 34, 49, alpha); } inline QColor whiteColor(int alpha = 255) { return QColor(255, 255, 255, alpha); } @@ -94,7 +83,6 @@ public: private: void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent* e) override; - OnroadHud *hud; OnroadAlerts *alerts; NvgWindow *nvg; QColor bg = bg_colors[STATUS_DISENGAGED]; diff --git a/selfdrive/ui/qt/widgets/cameraview.cc b/selfdrive/ui/qt/widgets/cameraview.cc index 4ef9ef1370..d817d55568 100644 --- a/selfdrive/ui/qt/widgets/cameraview.cc +++ b/selfdrive/ui/qt/widgets/cameraview.cc @@ -253,7 +253,6 @@ void CameraViewWidget::vipcConnected(VisionIpcClient *vipc_client) { stream_width = vipc_client->buffers[0].width; stream_height = vipc_client->buffers[0].height; - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); for (int i = 0; i < 3; ++i) { glBindTexture(GL_TEXTURE_2D, textures[i]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);