mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-20 08:42:11 +08:00
qt/spinner: reduce CPU usage from 17% to 12% (#21495)
* faster spinner * initApp * no brightness * Update selfdrive/ui/qt/util.h Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> old-commit-hash: b7f8c6ad03e70b9b73ef67238c7a5946b315483e
This commit is contained in:
@@ -8,10 +8,9 @@
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
qInstallMessageHandler(swagLogMessageHandler);
|
||||
setQtSurfaceFormat();
|
||||
initApp();
|
||||
|
||||
if (Hardware::EON()) {
|
||||
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
|
||||
QSslConfiguration ssl = QSslConfiguration::defaultConfiguration();
|
||||
ssl.setCaCertificates(QSslCertificate::fromPath("/usr/etc/tls/cert.pem"));
|
||||
QSslConfiguration::setDefaultConfiguration(ssl);
|
||||
|
||||
+13
-26
@@ -16,15 +16,19 @@
|
||||
|
||||
TrackWidget::TrackWidget(QWidget *parent) : QWidget(parent) {
|
||||
setFixedSize(spinner_size);
|
||||
setAutoFillBackground(false);
|
||||
|
||||
comma_img = QPixmap("../assets/img_spinner_comma.png").scaled(spinner_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
setAutoFillBackground(true);
|
||||
setPalette(Qt::black);
|
||||
|
||||
// pre-compute all the track imgs. make this a gif instead?
|
||||
QTransform transform;
|
||||
QPixmap comma_img = QPixmap("../assets/img_spinner_comma.png").scaled(spinner_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
QTransform transform(1, 0, 0, 1, width() / 2, height() / 2);
|
||||
QPixmap track_img = QPixmap("../assets/img_spinner_track.png").scaled(spinner_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||
for (auto &img : track_imgs) {
|
||||
img = track_img.transformed(transform.rotate(360/spinner_fps), Qt::SmoothTransformation);
|
||||
for (QPixmap &img : track_imgs) {
|
||||
img = comma_img;
|
||||
QPainter p(&img);
|
||||
p.setRenderHint(QPainter::SmoothPixmapTransform);
|
||||
p.setTransform(transform.rotate(360 / spinner_fps));
|
||||
p.drawPixmap(-width() / 2, -height() / 2, track_img);
|
||||
}
|
||||
|
||||
m_anim.setDuration(1000);
|
||||
@@ -37,18 +41,7 @@ TrackWidget::TrackWidget(QWidget *parent) : QWidget(parent) {
|
||||
|
||||
void TrackWidget::paintEvent(QPaintEvent *event) {
|
||||
QPainter painter(this);
|
||||
QRect bg(0, 0, painter.device()->width(), painter.device()->height());
|
||||
QBrush bgBrush("#000000");
|
||||
painter.fillRect(bg, bgBrush);
|
||||
|
||||
int track_idx = m_anim.currentValue().toInt();
|
||||
QRect rect(track_imgs[track_idx].rect());
|
||||
rect.moveCenter(bg.center());
|
||||
painter.drawPixmap(rect.topLeft(), track_imgs[track_idx]);
|
||||
|
||||
rect = comma_img.rect();
|
||||
rect.moveCenter(bg.center());
|
||||
painter.drawPixmap(rect.topLeft(), comma_img);
|
||||
painter.drawPixmap(0, 0, track_imgs[m_anim.currentValue().toInt()]);
|
||||
}
|
||||
|
||||
// Spinner
|
||||
@@ -75,12 +68,10 @@ Spinner::Spinner(QWidget *parent) : QWidget(parent) {
|
||||
Spinner {
|
||||
background-color: black;
|
||||
}
|
||||
* {
|
||||
background-color: transparent;
|
||||
}
|
||||
QLabel {
|
||||
color: white;
|
||||
font-size: 80px;
|
||||
background-color: transparent;
|
||||
}
|
||||
QProgressBar {
|
||||
background-color: #373737;
|
||||
@@ -114,11 +105,7 @@ void Spinner::update(int n) {
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
setQtSurfaceFormat();
|
||||
|
||||
Hardware::set_display_power(true);
|
||||
Hardware::set_brightness(65);
|
||||
|
||||
initApp();
|
||||
QApplication a(argc, argv);
|
||||
Spinner spinner;
|
||||
setMainWindow(&spinner);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <array>
|
||||
|
||||
#include <QLabel>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QPixmap>
|
||||
#include <QProgressBar>
|
||||
#include <QSocketNotifier>
|
||||
@@ -19,7 +18,6 @@ public:
|
||||
private:
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
std::array<QPixmap, spinner_fps> track_imgs;
|
||||
QPixmap comma_img;
|
||||
QVariantAnimation m_anim;
|
||||
};
|
||||
|
||||
|
||||
@@ -6,17 +6,16 @@
|
||||
#include <QWidget>
|
||||
|
||||
#include "selfdrive/hardware/hw.h"
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
#include "selfdrive/ui/qt/qt_window.h"
|
||||
#include "selfdrive/ui/qt/widgets/scrollview.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
initApp();
|
||||
QApplication a(argc, argv);
|
||||
QWidget window;
|
||||
setMainWindow(&window);
|
||||
|
||||
Hardware::set_display_power(true);
|
||||
Hardware::set_brightness(65);
|
||||
|
||||
QGridLayout *main_layout = new QGridLayout(&window);
|
||||
main_layout->setMargin(50);
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#include "selfdrive/ui/qt/util.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QLayoutItem>
|
||||
#include <QStyleOption>
|
||||
|
||||
#include "selfdrive/common/params.h"
|
||||
#include "selfdrive/common/swaglog.h"
|
||||
#include "selfdrive/hardware/hw.h"
|
||||
|
||||
QString getBrand() {
|
||||
return Params().getBool("Passive") ? "dashcam" : "openpilot";
|
||||
@@ -67,6 +69,15 @@ void setQtSurfaceFormat() {
|
||||
QSurfaceFormat::setDefaultFormat(fmt);
|
||||
}
|
||||
|
||||
void initApp() {
|
||||
Hardware::set_display_power(true);
|
||||
Hardware::set_brightness(65);
|
||||
setQtSurfaceFormat();
|
||||
if (Hardware::EON()) {
|
||||
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
|
||||
}
|
||||
}
|
||||
|
||||
ClickableWidget::ClickableWidget(QWidget *parent) : QWidget(parent) { }
|
||||
|
||||
void ClickableWidget::mouseReleaseEvent(QMouseEvent *event) {
|
||||
|
||||
@@ -14,6 +14,7 @@ void clearLayout(QLayout* layout);
|
||||
void setQtSurfaceFormat();
|
||||
QString timeAgo(const QDateTime &date);
|
||||
void swagLogMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
||||
void initApp();
|
||||
|
||||
class ClickableWidget : public QWidget
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user