Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cabana: use QStaticText to boost rending performance #29900

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions tools/cabana/binaryview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void BinaryViewModel::refresh() {
updateState();
}

void BinaryViewModel::updateItem(int row, int col, const QString &val, const QColor &color) {
void BinaryViewModel::updateItem(int row, int col, uint8_t val, const QColor &color) {
auto &item = items[row * column_count + col];
if (item.val != val || item.bg_color != color) {
item.val = val;
Expand All @@ -307,7 +307,7 @@ void BinaryViewModel::updateState() {
for (int i = 0; i < binary.size(); ++i) {
for (int j = 0; j < 8; ++j) {
auto &item = items[i * column_count + j];
QString val = ((binary[i] >> (7 - j)) & 1) != 0 ? "1" : "0";
int val = ((binary[i] >> (7 - j)) & 1) != 0 ? 1 : 0;
// Bit update frequency based highlighting
double offset = !item.sigs.empty() ? 50 : 0;
auto n = last_msg.bit_change_counts[i][7 - j];
Expand All @@ -317,7 +317,7 @@ void BinaryViewModel::updateState() {
color.setAlpha(alpha);
updateItem(i, j, val, color);
}
updateItem(i, 8, toHex(binary[i]), last_msg.colors[i]);
updateItem(i, 8, binary[i], last_msg.colors[i]);
}
}

Expand Down Expand Up @@ -348,6 +348,13 @@ BinaryItemDelegate::BinaryItemDelegate(QObject *parent) : QStyledItemDelegate(pa
small_font.setPixelSize(8);
hex_font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
hex_font.setBold(true);

bin_text_table[0].setText("0");
bin_text_table[1].setText("1");
for (int i = 0; i < 256; ++i) {
hex_text_table[i].setText(QStringLiteral("%1").arg(i, 2, 16, QLatin1Char('0')).toUpper());
hex_text_table[i].prepare({}, hex_font);
}
}

bool BinaryItemDelegate::hasSignal(const QModelIndex &index, int dx, int dy, const cabana::Signal *sig) const {
Expand Down Expand Up @@ -392,7 +399,9 @@ void BinaryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
} else if (!item->valid) {
painter->fillRect(option.rect, QBrush(Qt::darkGray, Qt::BDiagPattern));
}
painter->drawText(option.rect, Qt::AlignCenter, item->val);
if (item->valid) {
utils::drawStaticText(painter, option.rect, index.column() == 8 ? hex_text_table[item->val] : bin_text_table[item->val]);
}
if (item->is_msb || item->is_lsb) {
painter->setFont(small_font);
painter->drawText(option.rect.adjusted(8, 0, -8, -3), Qt::AlignRight | Qt::AlignBottom, item->is_msb ? "M" : "L");
Expand Down
6 changes: 4 additions & 2 deletions tools/cabana/binaryview.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class BinaryItemDelegate : public QStyledItemDelegate {
void drawSignalCell(QPainter* painter, const QStyleOptionViewItem &option, const QModelIndex &index, const cabana::Signal *sig) const;

QFont small_font, hex_font;
std::array<QStaticText, 256> hex_text_table;
std::array<QStaticText, 2> bin_text_table;
};

class BinaryViewModel : public QAbstractTableModel {
public:
BinaryViewModel(QObject *parent) : QAbstractTableModel(parent) {}
void refresh();
void updateState();
void updateItem(int row, int col, const QString &val, const QColor &color);
void updateItem(int row, int col, uint8_t val, const QColor &color);
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override { return row_count; }
Expand All @@ -42,7 +44,7 @@ class BinaryViewModel : public QAbstractTableModel {
QColor bg_color = QColor(102, 86, 169, 255);
bool is_msb = false;
bool is_lsb = false;
QString val;
uint8_t val;
QList<const cabana::Signal *> sigs;
bool valid = false;
};
Expand Down
7 changes: 5 additions & 2 deletions tools/cabana/util.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "tools/cabana/util.h"

#include <algorithm>
#include <array>
#include <csignal>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -56,6 +55,10 @@ std::pair<double, double> SegmentTree::get_minmax(int n, int left, int right, in
MessageBytesDelegate::MessageBytesDelegate(QObject *parent, bool multiple_lines) : multiple_lines(multiple_lines), QStyledItemDelegate(parent) {
fixed_font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
byte_size = QFontMetrics(fixed_font).size(Qt::TextSingleLine, "00 ") + QSize(0, 2);
for (int i = 0; i < 256; ++i) {
hex_text_table[i].setText(QStringLiteral("%1").arg(i, 2, 16, QLatin1Char('0')).toUpper());
hex_text_table[i].prepare({}, fixed_font);
}
}

int MessageBytesDelegate::widthForBytes(int n) const {
Expand Down Expand Up @@ -107,7 +110,7 @@ void MessageBytesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &
} else if (option.state & QStyle::State_Selected) {
painter->setPen(option.palette.color(QPalette::HighlightedText));
}
painter->drawText(r, Qt::AlignCenter, toHex(byte_list[i]));
utils::drawStaticText(painter, r, hex_text_table[(uint8_t)(byte_list[i])]);
}
painter->setFont(old_font);
painter->setPen(old_pen);
Expand Down
8 changes: 8 additions & 0 deletions tools/cabana/util.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <array>
#include <cmath>
#include <deque>
#include <vector>
Expand All @@ -10,8 +11,10 @@
#include <QDateTime>
#include <QDoubleValidator>
#include <QFont>
#include <QPainter>
#include <QRegExpValidator>
#include <QSocketNotifier>
#include <QStaticText>
#include <QStringBuilder>
#include <QStyledItemDelegate>
#include <QToolButton>
Expand Down Expand Up @@ -75,6 +78,7 @@ class MessageBytesDelegate : public QStyledItemDelegate {
int widthForBytes(int n) const;

private:
std::array<QStaticText, 256> hex_text_table;
QFont fixed_font;
QSize byte_size = {};
bool multiple_lines = false;
Expand Down Expand Up @@ -102,6 +106,10 @@ void setTheme(int theme);
inline QString formatSeconds(int seconds) {
return QDateTime::fromSecsSinceEpoch(seconds, Qt::UTC).toString(seconds > 60 * 60 ? "hh:mm:ss" : "mm:ss");
}
inline void drawStaticText(QPainter *p, const QRect &r, const QStaticText &text) {
auto size = (r.size() - text.size()) / 2;
p->drawStaticText(r.left() + size.width(), r.top() + size.height(), text);
}
}

class ToolButton : public QToolButton {
Expand Down