From ccfca97dfc08f2d7a583fe268bb816c5ae47fc6b Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Sat, 15 Jul 2023 03:18:49 +0800 Subject: [PATCH] ui/map: cache navigation images at startup (#28783) Co-authored-by: Cameron Clough --- selfdrive/ui/qt/maps/map.cc | 50 +++++++++++++++++++++++-------------- selfdrive/ui/qt/maps/map.h | 3 +++ 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/selfdrive/ui/qt/maps/map.cc b/selfdrive/ui/qt/maps/map.cc index f6d0d6a3b9..ee958dd432 100644 --- a/selfdrive/ui/qt/maps/map.cc +++ b/selfdrive/ui/qt/maps/map.cc @@ -3,6 +3,7 @@ #include #include +#include #include "common/transformations/coordinates.hpp" #include "selfdrive/ui/qt/maps/map_helpers.h" @@ -411,6 +412,31 @@ MapInstructions::MapInstructions(QWidget *parent) : QWidget(parent) { pal.setColor(QPalette::Background, QColor(0, 0, 0, 150)); setAutoFillBackground(true); setPalette(pal); + + buildPixmapCache(); +} + +void MapInstructions::buildPixmapCache() { + QDir dir("../assets/navigation"); + for (QString fn : dir.entryList({"*" + ICON_SUFFIX}, QDir::Files)) { + QPixmap pm(dir.filePath(fn)); + QString key = fn.left(fn.size() - ICON_SUFFIX.length()); + pm = pm.scaledToWidth(200, Qt::SmoothTransformation); + + // Maneuver icons + pixmap_cache[key] = pm; + // lane direction icons + if (key.contains("turn_")) { + pixmap_cache["lane_" + key] = pm.scaled({125, 125}, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + } + + // for rhd, reflect direction and then flip + if (key.contains("_left")) { + pixmap_cache["rhd_" + key.replace("_left", "_right")] = pm.transformed(QTransform().scale(-1, 1)); + } else if (key.contains("_right")) { + pixmap_cache["rhd_" + key.replace("_right", "_left")] = pm.transformed(QTransform().scale(-1, 1)); + } + } } QString MapInstructions::getDistance(float d) { @@ -441,27 +467,13 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct QString type = QString::fromStdString(instruction.getManeuverType()); QString modifier = QString::fromStdString(instruction.getManeuverModifier()); if (!type.isEmpty()) { - QString fn = "../assets/navigation/direction_" + type; + QString fn = "direction_" + type; if (!modifier.isEmpty()) { fn += "_" + modifier; } - fn += ICON_SUFFIX; fn = fn.replace(' ', '_'); - - // for rhd, reflect direction and then flip - if (is_rhd) { - if (fn.contains("left")) { - fn.replace("left", "right"); - } else if (fn.contains("right")) { - fn.replace("right", "left"); - } - } - - QPixmap pix(fn); - if (is_rhd) { - pix = pix.transformed(QTransform().scale(-1, 1)); - } - icon_01->setPixmap(pix.scaledToWidth(200, Qt::SmoothTransformation)); + bool rhd = is_rhd && (fn.contains("_left") || fn.contains("_right")); + icon_01->setPixmap(pixmap_cache[!rhd ? fn : "rhd_" + fn]); icon_01->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); icon_01->setVisible(true); } @@ -480,7 +492,7 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct } // TODO: Make more images based on active direction and combined directions - QString fn = "../assets/navigation/direction_"; + QString fn = "lane_direction_"; if (left) { fn += "turn_left"; } else if (right) { @@ -497,7 +509,7 @@ void MapInstructions::updateInstructions(cereal::NavInstruction::Reader instruct if (!label->parentWidget()) { lane_layout->addWidget(label); } - label->setPixmap(loadPixmap(fn + ICON_SUFFIX, {125, 125}, Qt::IgnoreAspectRatio)); + label->setPixmap(pixmap_cache[fn]); label->setVisible(true); } diff --git a/selfdrive/ui/qt/maps/map.h b/selfdrive/ui/qt/maps/map.h index 34b55e8997..ff4b0b3401 100644 --- a/selfdrive/ui/qt/maps/map.h +++ b/selfdrive/ui/qt/maps/map.h @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -34,9 +35,11 @@ private: QHBoxLayout *lane_layout; bool is_rhd = false; std::vector lane_labels; + QHash pixmap_cache; public: MapInstructions(QWidget * parent=nullptr); + void buildPixmapCache(); QString getDistance(float d); void updateInstructions(cereal::NavInstruction::Reader instruction); };