feat: improve AGV live map controls
This commit is contained in:
parent
3fb913e859
commit
96e5fe0c76
@ -143,6 +143,62 @@ QPointF AgvMapViewWidget::imageToWidget(const QPointF &point) const
|
||||
return imageTopLeft() + point * viewScale();
|
||||
}
|
||||
|
||||
void AgvMapViewWidget::drawCoordinateAxes(QPainter &painter) const
|
||||
{
|
||||
constexpr double panelSize = 92.0;
|
||||
constexpr double panelMargin = 12.0;
|
||||
constexpr double arrowLength = 27.0;
|
||||
const QRectF panel(width() - panelMargin - panelSize,
|
||||
panelMargin, panelSize, panelSize);
|
||||
|
||||
painter.save();
|
||||
painter.setPen(QPen(QColor(QStringLiteral("#c9d4de")), 1));
|
||||
painter.setBrush(QColor(255, 255, 255, 232));
|
||||
painter.drawRoundedRect(panel, 8, 8);
|
||||
|
||||
const QPointF center = panel.center();
|
||||
const double originTheta = m_metadata.value(
|
||||
QStringLiteral("originTheta")).toDouble();
|
||||
const QPointF xDirection(qCos(originTheta), qSin(originTheta));
|
||||
const QPointF yDirection(qSin(originTheta), -qCos(originTheta));
|
||||
|
||||
const auto drawArrow = [&painter, center, arrowLength](
|
||||
const QPointF &direction,
|
||||
const QColor &color,
|
||||
const QString &label) {
|
||||
const QPointF normal(-direction.y(), direction.x());
|
||||
const QPointF tip = center + direction * arrowLength;
|
||||
painter.setPen(QPen(color, 2.4, Qt::SolidLine, Qt::RoundCap));
|
||||
painter.setBrush(color);
|
||||
painter.drawLine(center, tip);
|
||||
|
||||
QPolygonF arrowHead;
|
||||
arrowHead << tip
|
||||
<< tip - direction * 8.0 + normal * 4.5
|
||||
<< tip - direction * 8.0 - normal * 4.5;
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.drawPolygon(arrowHead);
|
||||
|
||||
const QPointF labelCenter = tip + direction * 9.0;
|
||||
const QRectF labelRect(labelCenter.x() - 9.0,
|
||||
labelCenter.y() - 9.0, 18.0, 18.0);
|
||||
QFont labelFont = painter.font();
|
||||
labelFont.setBold(true);
|
||||
painter.setFont(labelFont);
|
||||
painter.setPen(color);
|
||||
painter.drawText(labelRect, Qt::AlignCenter, label);
|
||||
};
|
||||
|
||||
drawArrow(xDirection, QColor(QStringLiteral("#d94a4a")),
|
||||
QStringLiteral("X"));
|
||||
drawArrow(yDirection, QColor(QStringLiteral("#2f9e69")),
|
||||
QStringLiteral("Y"));
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.setBrush(QColor(QStringLiteral("#374151")));
|
||||
painter.drawEllipse(center, 3.2, 3.2);
|
||||
painter.restore();
|
||||
}
|
||||
|
||||
void AgvMapViewWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
@ -221,6 +277,9 @@ void AgvMapViewWidget::paintEvent(QPaintEvent *event)
|
||||
painter.drawText(labelRect, Qt::AlignCenter, poseText);
|
||||
}
|
||||
|
||||
if (canProjectPose())
|
||||
drawCoordinateAxes(painter);
|
||||
|
||||
if (!m_streamMessage.isEmpty()) {
|
||||
const QRect messageRect(12, height() - 36, width() - 24, 24);
|
||||
painter.setPen(QColor(QStringLiteral("#5c6975")));
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include <QWidget>
|
||||
|
||||
class QMouseEvent;
|
||||
class QPainter;
|
||||
class QPaintEvent;
|
||||
class QWheelEvent;
|
||||
|
||||
@ -38,6 +39,7 @@ private:
|
||||
QPointF imageToWidget(const QPointF &point) const;
|
||||
double viewScale() const;
|
||||
QPointF imageTopLeft() const;
|
||||
void drawCoordinateAxes(QPainter &painter) const;
|
||||
void changeZoom(double factor, const QPointF &anchor);
|
||||
|
||||
QImage m_image;
|
||||
|
||||
@ -522,16 +522,35 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
connect(m_client, &RobotClient::agvMapFrameReceived, this,
|
||||
[this](const QImage &image, const QVariantMap &metadata,
|
||||
const QString &detail) {
|
||||
if (m_agvMapView) {
|
||||
m_agvMapView->setMapFrame(image, metadata);
|
||||
m_agvMapView->setStreamMessage(detail);
|
||||
m_pendingAgvMapImage = image;
|
||||
m_pendingAgvMapMetadata = metadata;
|
||||
m_pendingAgvMapDetail = detail;
|
||||
m_agvMapFramePending = true;
|
||||
|
||||
const int intervalMs = m_agvMapRefreshCombo
|
||||
? m_agvMapRefreshCombo->currentData().toInt() : 0;
|
||||
if (intervalMs <= 0) {
|
||||
presentPendingAgvMapFrame();
|
||||
} else if (m_agvMapRefreshTimer && !m_agvMapRefreshTimer->isActive()) {
|
||||
presentPendingAgvMapFrame();
|
||||
m_agvMapRefreshTimer->setInterval(intervalMs);
|
||||
m_agvMapRefreshTimer->start();
|
||||
}
|
||||
if (m_agvMappingStateLabel)
|
||||
m_agvMappingStateLabel->setText(detail);
|
||||
});
|
||||
connect(m_client, &RobotClient::agvMapStreamStateChanged, this,
|
||||
[this](bool running, const QString &detail) {
|
||||
m_agvMapStreamActive = running;
|
||||
const int intervalMs = m_agvMapRefreshCombo
|
||||
? m_agvMapRefreshCombo->currentData().toInt() : 0;
|
||||
if (m_agvMapRefreshTimer) {
|
||||
if (running && intervalMs > 0) {
|
||||
m_agvMapRefreshTimer->setInterval(intervalMs);
|
||||
m_agvMapRefreshTimer->start();
|
||||
} else if (!running) {
|
||||
presentPendingAgvMapFrame();
|
||||
m_agvMapRefreshTimer->stop();
|
||||
}
|
||||
}
|
||||
if (m_agvMapView)
|
||||
m_agvMapView->setStreamMessage(detail);
|
||||
if (!m_agvMappingStateLabel)
|
||||
@ -926,6 +945,12 @@ void MainWindow::selectPage(int index)
|
||||
stopAgvManualMotion();
|
||||
if (m_agvStatusRefreshTimer)
|
||||
m_agvStatusRefreshTimer->stop();
|
||||
if (m_agvMapRefreshTimer)
|
||||
m_agvMapRefreshTimer->stop();
|
||||
m_agvMapFramePending = false;
|
||||
m_pendingAgvMapImage = QImage();
|
||||
m_pendingAgvMapMetadata.clear();
|
||||
m_pendingAgvMapDetail.clear();
|
||||
if (m_agvMapStreamActive) {
|
||||
m_client->stopAgvMapStream();
|
||||
m_agvMapStreamActive = false;
|
||||
@ -981,6 +1006,25 @@ void MainWindow::restartAgvMapStream(const QString &mapName)
|
||||
1, true, true);
|
||||
}
|
||||
|
||||
void MainWindow::presentPendingAgvMapFrame()
|
||||
{
|
||||
if (!m_agvMapFramePending)
|
||||
return;
|
||||
|
||||
if (m_agvMapView) {
|
||||
m_agvMapView->setMapFrame(m_pendingAgvMapImage,
|
||||
m_pendingAgvMapMetadata);
|
||||
m_agvMapView->setStreamMessage(m_pendingAgvMapDetail);
|
||||
}
|
||||
if (m_agvMappingStateLabel)
|
||||
m_agvMappingStateLabel->setText(m_pendingAgvMapDetail);
|
||||
|
||||
m_agvMapFramePending = false;
|
||||
m_pendingAgvMapImage = QImage();
|
||||
m_pendingAgvMapMetadata.clear();
|
||||
m_pendingAgvMapDetail.clear();
|
||||
}
|
||||
|
||||
void MainWindow::ensureArmModelLoaded()
|
||||
{
|
||||
if (!m_urdfViewer || m_pendingUrdfPath.isEmpty()
|
||||
@ -1834,9 +1878,41 @@ void MainWindow::handleCommand(const QString &command, bool success, const QStri
|
||||
m_client->requestAgvInventory(m_agvIdEdit->text().trimmed());
|
||||
restartAgvMapStream(m_agvMapCombo->currentText());
|
||||
}
|
||||
if (success && command == QStringLiteral("AGV StopMapping")) {
|
||||
m_client->requestAgvInventory(m_agvIdEdit->text().trimmed());
|
||||
restartAgvMapStream();
|
||||
if (command == QStringLiteral("AGV StopMapping")) {
|
||||
const bool asynchronousMappingFrame =
|
||||
!success
|
||||
&& detail.contains(QStringLiteral("expected=16101"))
|
||||
&& detail.contains(QStringLiteral("actual=17002"));
|
||||
if (asynchronousMappingFrame && !m_agvStopMappingRetryPending) {
|
||||
m_agvStopMappingRetryPending = true;
|
||||
if (m_agvMappingStateLabel) {
|
||||
m_agvMappingStateLabel->setText(
|
||||
QStringLiteral("正在重新确认停止建图"));
|
||||
}
|
||||
appendLog(
|
||||
QStringLiteral("WARN"),
|
||||
QStringLiteral("停止建图响应被异步地图帧打断,正在自动重试一次"));
|
||||
QTimer::singleShot(350, this, [this]() {
|
||||
if (!m_agvStopMappingRetryPending)
|
||||
return;
|
||||
m_client->requestAgvAction(
|
||||
m_agvIdEdit->text().trimmed(),
|
||||
QStringLiteral("StopMapping"));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
m_agvStopMappingRetryPending = false;
|
||||
if (m_agvStopMappingButton)
|
||||
m_agvStopMappingButton->setEnabled(true);
|
||||
if (success) {
|
||||
if (m_agvMappingStateLabel)
|
||||
m_agvMappingStateLabel->setText(QStringLiteral("建图已停止"));
|
||||
m_client->requestAgvInventory(m_agvIdEdit->text().trimmed());
|
||||
restartAgvMapStream();
|
||||
} else if (m_agvMappingStateLabel) {
|
||||
m_agvMappingStateLabel->setText(QStringLiteral("停止建图失败"));
|
||||
}
|
||||
}
|
||||
|
||||
appendLog(success ? QStringLiteral("OK") : QStringLiteral("ERROR"),
|
||||
@ -1946,6 +2022,12 @@ void MainWindow::loadSettings()
|
||||
m_cameraStreamModeCombo->setCurrentIndex(modeIndex);
|
||||
}
|
||||
m_agvIdEdit->setText(settings.value(QStringLiteral("agv/device"), QStringLiteral("agv")).toString());
|
||||
if (m_agvMapRefreshCombo) {
|
||||
const int intervalMs = settings.value(
|
||||
QStringLiteral("agv/mapRefreshIntervalMs"), 0).toInt();
|
||||
const int index = m_agvMapRefreshCombo->findData(intervalMs);
|
||||
m_agvMapRefreshCombo->setCurrentIndex(index >= 0 ? index : 0);
|
||||
}
|
||||
m_velocitySpin->setValue(settings.value(
|
||||
QStringLiteral("motion/speedJVelocity"),
|
||||
settings.value(QStringLiteral("motion/velocity"), 1.0)).toDouble());
|
||||
@ -2011,6 +2093,9 @@ void MainWindow::saveSettings()
|
||||
settings.setValue(QStringLiteral("camera/streamMode"),
|
||||
m_cameraStreamModeCombo->currentData().toString());
|
||||
settings.setValue(QStringLiteral("agv/device"), m_agvIdEdit->text().trimmed());
|
||||
if (m_agvMapRefreshCombo)
|
||||
settings.setValue(QStringLiteral("agv/mapRefreshIntervalMs"),
|
||||
m_agvMapRefreshCombo->currentData().toInt());
|
||||
settings.setValue(QStringLiteral("motion/speedJVelocity"), m_velocitySpin->value());
|
||||
settings.setValue(QStringLiteral("motion/speedJAcceleration"),
|
||||
m_accelerationSpin->value());
|
||||
|
||||
@ -110,6 +110,7 @@ private:
|
||||
void startAgvManualMotion(double vx, double vy, double wz);
|
||||
void stopAgvManualMotion(bool sendStop = true);
|
||||
void restartAgvMapStream(const QString &mapName = QString());
|
||||
void presentPendingAgvMapFrame();
|
||||
void updateArmAlert(const QString &deviceId, const QString &alertKey,
|
||||
const QString &level, const QString &summary,
|
||||
const QString &detail = QString());
|
||||
@ -252,6 +253,7 @@ private:
|
||||
QDialog *m_agvStatusDialog = nullptr;
|
||||
QTimer *m_agvStatusRefreshTimer = nullptr;
|
||||
QTimer *m_agvManualVelocityTimer = nullptr;
|
||||
QTimer *m_agvMapRefreshTimer = nullptr;
|
||||
QLabel *m_agvConnectionValue = nullptr;
|
||||
QLabel *m_agvPoseValue = nullptr;
|
||||
QLabel *m_agvMotionValue = nullptr;
|
||||
@ -270,6 +272,7 @@ private:
|
||||
AgvMapViewWidget *m_agvMapView = nullptr;
|
||||
QWidget *m_agvControlWorkspace = nullptr;
|
||||
QPushButton *m_agvDeleteMapButton = nullptr;
|
||||
QPushButton *m_agvStopMappingButton = nullptr;
|
||||
QProgressBar *m_agvProgress = nullptr;
|
||||
QDoubleSpinBox *m_agvXSpin = nullptr;
|
||||
QDoubleSpinBox *m_agvYSpin = nullptr;
|
||||
@ -279,15 +282,21 @@ private:
|
||||
QDoubleSpinBox *m_agvVySpin = nullptr;
|
||||
QDoubleSpinBox *m_agvWzSpin = nullptr;
|
||||
QComboBox *m_agvMapCombo = nullptr;
|
||||
QComboBox *m_agvMapRefreshCombo = nullptr;
|
||||
QComboBox *m_agvStationCombo = nullptr;
|
||||
QTableWidget *m_agvStationTable = nullptr;
|
||||
QString m_agvDownloadPath;
|
||||
QVariantMap m_latestAgvState;
|
||||
QImage m_pendingAgvMapImage;
|
||||
QVariantMap m_pendingAgvMapMetadata;
|
||||
QString m_pendingAgvMapDetail;
|
||||
double m_agvManualVx = 0.0;
|
||||
double m_agvManualVy = 0.0;
|
||||
double m_agvManualWz = 0.0;
|
||||
bool m_agvManualMotionActive = false;
|
||||
bool m_agvMapStreamActive = false;
|
||||
bool m_agvMapFramePending = false;
|
||||
bool m_agvStopMappingRetryPending = false;
|
||||
QList<QPushButton *> m_serviceButtons;
|
||||
QPushButton *m_speakerStreamStartButton = nullptr;
|
||||
QPushButton *m_speakerStreamStopButton = nullptr;
|
||||
|
||||
@ -1087,6 +1087,10 @@ QWidget *MainWindow::createAgvPage()
|
||||
m_agvIdEdit = ui.agvIdEdit;
|
||||
m_agvStatusRefreshTimer = new QTimer(page);
|
||||
m_agvStatusRefreshTimer->setInterval(1000);
|
||||
m_agvMapRefreshTimer = new QTimer(page);
|
||||
m_agvMapRefreshTimer->setTimerType(Qt::PreciseTimer);
|
||||
connect(m_agvMapRefreshTimer, &QTimer::timeout,
|
||||
this, &MainWindow::presentPendingAgvMapFrame);
|
||||
m_agvConnectionValue = statusUi.agvConnectionValue;
|
||||
m_agvPoseValue = statusUi.agvPoseValue;
|
||||
m_agvMotionValue = statusUi.agvMotionValue;
|
||||
@ -1166,6 +1170,19 @@ QWidget *MainWindow::createAgvPage()
|
||||
QStringLiteral("蓝色箭头:AGV 当前位姿 · 虚线:运动轨迹"),
|
||||
ui.mappingPreviewPanel);
|
||||
mapLegend->setObjectName(QStringLiteral("panelSubtitle"));
|
||||
QLabel *mapRefreshLabel = new QLabel(QStringLiteral("刷新率"),
|
||||
ui.mappingPreviewPanel);
|
||||
mapRefreshLabel->setObjectName(QStringLiteral("panelSubtitle"));
|
||||
m_agvMapRefreshCombo = new QComboBox(ui.mappingPreviewPanel);
|
||||
m_agvMapRefreshCombo->setObjectName(QStringLiteral("agvMapRefreshCombo"));
|
||||
m_agvMapRefreshCombo->addItem(QStringLiteral("自动"), 0);
|
||||
m_agvMapRefreshCombo->addItem(QStringLiteral("1 Hz"), 1000);
|
||||
m_agvMapRefreshCombo->addItem(QStringLiteral("2 Hz"), 500);
|
||||
m_agvMapRefreshCombo->addItem(QStringLiteral("5 Hz"), 200);
|
||||
m_agvMapRefreshCombo->addItem(QStringLiteral("10 Hz"), 100);
|
||||
m_agvMapRefreshCombo->setMaximumWidth(96);
|
||||
m_agvMapRefreshCombo->setToolTip(
|
||||
QStringLiteral("控制上位机地图画面的刷新频率;自动模式跟随地图数据"));
|
||||
QPushButton *zoomOutButton = new QPushButton(QStringLiteral("−"),
|
||||
ui.mappingPreviewPanel);
|
||||
QPushButton *zoomInButton = new QPushButton(QStringLiteral("+"),
|
||||
@ -1183,6 +1200,9 @@ QWidget *MainWindow::createAgvPage()
|
||||
zoomInButton->setMaximumWidth(42);
|
||||
mapToolbar->addWidget(mapLegend);
|
||||
mapToolbar->addStretch();
|
||||
mapToolbar->addWidget(mapRefreshLabel);
|
||||
mapToolbar->addWidget(m_agvMapRefreshCombo);
|
||||
mapToolbar->addSpacing(4);
|
||||
mapToolbar->addWidget(zoomOutButton);
|
||||
mapToolbar->addWidget(zoomInButton);
|
||||
mapToolbar->addWidget(resetViewButton);
|
||||
@ -1316,6 +1336,19 @@ QWidget *MainWindow::createAgvPage()
|
||||
m_agvMapView, &AgvMapViewWidget::resetView);
|
||||
connect(clearTrackButton, &QPushButton::clicked,
|
||||
m_agvMapView, &AgvMapViewWidget::clearTrajectory);
|
||||
connect(m_agvMapRefreshCombo,
|
||||
QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, [this](int) {
|
||||
const int intervalMs = m_agvMapRefreshCombo->currentData().toInt();
|
||||
if (intervalMs <= 0) {
|
||||
m_agvMapRefreshTimer->stop();
|
||||
presentPendingAgvMapFrame();
|
||||
return;
|
||||
}
|
||||
m_agvMapRefreshTimer->setInterval(intervalMs);
|
||||
if (m_agvMapStreamActive)
|
||||
m_agvMapRefreshTimer->start();
|
||||
});
|
||||
connect(addMapButton, &QPushButton::clicked, this,
|
||||
[this, nameEdit = ui.mappingNameEdit]() {
|
||||
bool accepted = false;
|
||||
@ -1551,7 +1584,10 @@ QWidget *MainWindow::createAgvPage()
|
||||
m_agvIdEdit->text().trimmed());
|
||||
ui.mappingStateLabel->setText(QStringLiteral("建图启动请求已发送"));
|
||||
});
|
||||
m_agvStopMappingButton = ui.stopMappingButton;
|
||||
connect(ui.stopMappingButton, &QPushButton::clicked, this, [this, ui]() {
|
||||
m_agvStopMappingRetryPending = false;
|
||||
m_agvStopMappingButton->setEnabled(false);
|
||||
m_client->requestAgvAction(m_agvIdEdit->text().trimmed(),
|
||||
QStringLiteral("StopMapping"));
|
||||
ui.mappingStateLabel->setText(QStringLiteral("正在停止建图"));
|
||||
|
||||
@ -1812,10 +1812,13 @@ public:
|
||||
painter.drawEllipse(geometry.first(), 6, 6);
|
||||
else if (geometry.size() > 1)
|
||||
painter.drawPolyline(geometry);
|
||||
if (!geometry.isEmpty()) {
|
||||
const QString objectId = QString::fromStdString(
|
||||
object.id()).trimmed();
|
||||
if (!geometry.isEmpty() && !objectId.isEmpty()
|
||||
&& objectId != QStringLiteral("0")) {
|
||||
painter.setPen(QColor(QStringLiteral("#145da8")));
|
||||
painter.drawText(geometry.first() + QPointF(8, -6),
|
||||
QString::fromStdString(object.id()));
|
||||
objectId);
|
||||
}
|
||||
}
|
||||
painter.end();
|
||||
|
||||
307
src/videorenderwidget.cpp
Normal file
307
src/videorenderwidget.cpp
Normal file
@ -0,0 +1,307 @@
|
||||
#include "videorenderwidget.h"
|
||||
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QPainter>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace {
|
||||
const char *vertexShaderSource = R"(
|
||||
attribute vec2 position;
|
||||
attribute vec2 texCoord;
|
||||
varying vec2 textureCoordinate;
|
||||
uniform vec2 contentScale;
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(position * contentScale, 0.0, 1.0);
|
||||
textureCoordinate = texCoord;
|
||||
}
|
||||
)";
|
||||
|
||||
const char *fragmentShaderSource = R"(
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
varying vec2 textureCoordinate;
|
||||
uniform sampler2D textureY;
|
||||
uniform sampler2D textureU;
|
||||
uniform sampler2D textureV;
|
||||
uniform float pixelFormat;
|
||||
uniform float scaleY;
|
||||
uniform float scaleU;
|
||||
uniform float scaleV;
|
||||
void main()
|
||||
{
|
||||
if (pixelFormat > 1.5) {
|
||||
gl_FragColor = texture2D(textureY, textureCoordinate);
|
||||
return;
|
||||
}
|
||||
|
||||
float y = texture2D(textureY,
|
||||
vec2(textureCoordinate.x * scaleY, textureCoordinate.y)).r;
|
||||
float u;
|
||||
float v;
|
||||
if (pixelFormat > 0.5) {
|
||||
vec4 uv = texture2D(textureU,
|
||||
vec2(textureCoordinate.x * scaleU, textureCoordinate.y));
|
||||
u = uv.r - 0.5;
|
||||
v = uv.a - 0.5;
|
||||
} else {
|
||||
u = texture2D(textureU,
|
||||
vec2(textureCoordinate.x * scaleU, textureCoordinate.y)).r - 0.5;
|
||||
v = texture2D(textureV,
|
||||
vec2(textureCoordinate.x * scaleV, textureCoordinate.y)).r - 0.5;
|
||||
}
|
||||
y = 1.16438356 * (y - 0.0625);
|
||||
gl_FragColor = vec4(y + 1.59602678 * v,
|
||||
y - 0.39176229 * u - 0.81296764 * v,
|
||||
y + 2.01723214 * u, 1.0);
|
||||
}
|
||||
)";
|
||||
}
|
||||
|
||||
VideoRenderWidget::VideoRenderWidget(QWidget *parent)
|
||||
: QOpenGLWidget(parent)
|
||||
{
|
||||
setAutoFillBackground(false);
|
||||
setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
|
||||
}
|
||||
|
||||
VideoRenderWidget::~VideoRenderWidget()
|
||||
{
|
||||
if (context()) {
|
||||
makeCurrent();
|
||||
releaseTextures();
|
||||
doneCurrent();
|
||||
}
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setFrameAspectLayoutEnabled(bool enabled)
|
||||
{
|
||||
m_frameAspectLayout = enabled;
|
||||
QSizePolicy policy(QSizePolicy::Expanding, enabled ? QSizePolicy::Fixed : QSizePolicy::Expanding);
|
||||
policy.setHeightForWidth(enabled);
|
||||
setSizePolicy(policy);
|
||||
if (enabled) updateAspectHeight();
|
||||
else { setMinimumHeight(0); setMaximumHeight(QWIDGETSIZE_MAX); }
|
||||
updateGeometry();
|
||||
}
|
||||
|
||||
int VideoRenderWidget::heightForWidth(int width) const
|
||||
{
|
||||
return m_frameAspectLayout ? qMax(1, qRound(double(width) * m_layoutFrameSize.height()
|
||||
/ m_layoutFrameSize.width())) : -1;
|
||||
}
|
||||
|
||||
QSize VideoRenderWidget::sizeHint() const
|
||||
{
|
||||
return m_frameAspectLayout ? QSize(640,heightForWidth(640)) : QOpenGLWidget::sizeHint();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::updateAspectHeight()
|
||||
{
|
||||
if (!m_frameAspectLayout) return;
|
||||
const int wanted = heightForWidth(width());
|
||||
if (minimumHeight() != wanted || maximumHeight() != wanted) setFixedHeight(wanted);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QOpenGLWidget::resizeEvent(event);
|
||||
updateAspectHeight();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setFrame(const VideoFrame &frame)
|
||||
{
|
||||
if (!frame.isValid())
|
||||
return;
|
||||
m_frame = frame;
|
||||
if (m_layoutFrameSize != QSize(frame.width, frame.height)) {
|
||||
m_layoutFrameSize = QSize(frame.width, frame.height);
|
||||
updateAspectHeight();
|
||||
if (m_frameAspectLayout) updateGeometry();
|
||||
}
|
||||
m_message.clear();
|
||||
m_frameDirty = true;
|
||||
update();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::setImage(const QImage &image)
|
||||
{
|
||||
if (image.isNull())
|
||||
return;
|
||||
const QImage converted = image.convertToFormat(QImage::Format_RGBA8888);
|
||||
VideoFrame frame;
|
||||
frame.width = converted.width();
|
||||
frame.height = converted.height();
|
||||
frame.format = VideoPixelFormat::RGBA;
|
||||
frame.stride0 = frame.width * 4;
|
||||
frame.plane0.resize(frame.width * frame.height * 4);
|
||||
for (int row = 0; row < frame.height; ++row) {
|
||||
std::memcpy(frame.plane0.data() + row * frame.width * 4,
|
||||
converted.constScanLine(row), static_cast<size_t>(frame.width * 4));
|
||||
}
|
||||
setFrame(frame);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::clearFrame(const QString &message)
|
||||
{
|
||||
m_frame = VideoFrame();
|
||||
m_message = message;
|
||||
m_frameDirty = false;
|
||||
update();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::initializeGL()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
glClearColor(0.055f, 0.067f, 0.078f, 1.0f);
|
||||
glGenTextures(3, m_textures);
|
||||
|
||||
m_program.reset(new QOpenGLShaderProgram);
|
||||
if (!m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource) ||
|
||||
!m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource) ||
|
||||
!m_program->link()) {
|
||||
m_message = QStringLiteral("OpenGL 视频着色器初始化失败:%1").arg(m_program->log());
|
||||
m_program.reset();
|
||||
return;
|
||||
}
|
||||
m_glReady = true;
|
||||
}
|
||||
|
||||
void VideoRenderWidget::uploadPlane(unsigned int texture, int unit, int width,
|
||||
int height, unsigned int format,
|
||||
const QByteArray &data, int textureIndex)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + unit);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
if (m_textureWidths[textureIndex] != width ||
|
||||
m_textureHeights[textureIndex] != height ||
|
||||
m_textureFormats[textureIndex] != format) {
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, static_cast<int>(format), width, height, 0,
|
||||
format, GL_UNSIGNED_BYTE, data.constData());
|
||||
m_textureWidths[textureIndex] = width;
|
||||
m_textureHeights[textureIndex] = height;
|
||||
m_textureFormats[textureIndex] = format;
|
||||
} else {
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format,
|
||||
GL_UNSIGNED_BYTE, data.constData());
|
||||
}
|
||||
}
|
||||
|
||||
void VideoRenderWidget::paintGL()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
if (!m_glReady || !m_program || !m_frame.isValid()) {
|
||||
drawMessage(m_message);
|
||||
return;
|
||||
}
|
||||
|
||||
// Qt 5.12's ANGLE backend can upload RGBA QImage data with an incorrect
|
||||
// channel layout. Draw still RGBA frames with the CPU painter instead.
|
||||
if (m_frame.format == VideoPixelFormat::RGBA) {
|
||||
const QImage image(reinterpret_cast<const uchar *>(m_frame.plane0.constData()),
|
||||
m_frame.width, m_frame.height, m_frame.stride0,
|
||||
QImage::Format_RGBA8888);
|
||||
QPainter painter(this);
|
||||
painter.fillRect(rect(), QColor(QStringLiteral("#0e1115")));
|
||||
QSize target = image.size();
|
||||
target.scale(size(), Qt::KeepAspectRatio);
|
||||
const QRect destination((width() - target.width()) / 2,
|
||||
(height() - target.height()) / 2,
|
||||
target.width(), target.height());
|
||||
painter.drawImage(destination, image);
|
||||
m_frameDirty = false;
|
||||
return;
|
||||
}
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
if (m_frameDirty) {
|
||||
if (m_frame.format == VideoPixelFormat::I420) {
|
||||
uploadPlane(m_textures[0], 0, m_frame.stride0, m_frame.height,
|
||||
GL_LUMINANCE, m_frame.plane0, 0);
|
||||
uploadPlane(m_textures[1], 1, m_frame.stride1,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE, m_frame.plane1, 1);
|
||||
uploadPlane(m_textures[2], 2, m_frame.stride2,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE, m_frame.plane2, 2);
|
||||
} else if (m_frame.format == VideoPixelFormat::NV12) {
|
||||
uploadPlane(m_textures[0], 0, m_frame.stride0, m_frame.height,
|
||||
GL_LUMINANCE, m_frame.plane0, 0);
|
||||
uploadPlane(m_textures[1], 1, m_frame.stride1 / 2,
|
||||
(m_frame.height + 1) / 2, GL_LUMINANCE_ALPHA,
|
||||
m_frame.plane1, 1);
|
||||
} else {
|
||||
uploadPlane(m_textures[0], 0, m_frame.width, m_frame.height,
|
||||
GL_RGBA, m_frame.plane0, 0);
|
||||
}
|
||||
m_frameDirty = false;
|
||||
}
|
||||
|
||||
const GLfloat vertices[] = {
|
||||
-1.0f, -1.0f, 0.0f, 1.0f,
|
||||
1.0f, -1.0f, 1.0f, 1.0f,
|
||||
-1.0f, 1.0f, 0.0f, 0.0f,
|
||||
1.0f, 1.0f, 1.0f, 0.0f
|
||||
};
|
||||
float scaleX = 1.0f;
|
||||
float scaleY = 1.0f;
|
||||
const float frameAspect = static_cast<float>(m_frame.width) / m_frame.height;
|
||||
const float widgetAspect = width() > 0 && height() > 0
|
||||
? static_cast<float>(width()) / height() : frameAspect;
|
||||
if (frameAspect > widgetAspect)
|
||||
scaleY = widgetAspect / frameAspect;
|
||||
else
|
||||
scaleX = frameAspect / widgetAspect;
|
||||
|
||||
m_program->bind();
|
||||
m_program->setUniformValue("contentScale", scaleX, scaleY);
|
||||
m_program->setUniformValue("textureY", 0);
|
||||
m_program->setUniformValue("textureU", 1);
|
||||
m_program->setUniformValue("textureV", 2);
|
||||
m_program->setUniformValue("pixelFormat",
|
||||
m_frame.format == VideoPixelFormat::NV12 ? 1.0f :
|
||||
m_frame.format == VideoPixelFormat::RGBA ? 2.0f : 0.0f);
|
||||
m_program->setUniformValue("scaleY", m_frame.format == VideoPixelFormat::RGBA
|
||||
? 1.0f : static_cast<float>(m_frame.width) / m_frame.stride0);
|
||||
m_program->setUniformValue("scaleU", m_frame.format == VideoPixelFormat::NV12
|
||||
? static_cast<float>((m_frame.width + 1) / 2) / (m_frame.stride1 / 2)
|
||||
: static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride1);
|
||||
m_program->setUniformValue("scaleV", m_frame.format == VideoPixelFormat::I420
|
||||
? static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride2 : 1.0f);
|
||||
const int position = m_program->attributeLocation("position");
|
||||
const int texCoord = m_program->attributeLocation("texCoord");
|
||||
m_program->enableAttributeArray(position);
|
||||
m_program->enableAttributeArray(texCoord);
|
||||
m_program->setAttributeArray(position, GL_FLOAT, vertices, 2, 4 * sizeof(GLfloat));
|
||||
m_program->setAttributeArray(texCoord, GL_FLOAT, vertices + 2, 2,
|
||||
4 * sizeof(GLfloat));
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
m_program->disableAttributeArray(position);
|
||||
m_program->disableAttributeArray(texCoord);
|
||||
m_program->release();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::drawMessage(const QString &message)
|
||||
{
|
||||
if (message.isEmpty())
|
||||
return;
|
||||
QPainter painter(this);
|
||||
painter.setPen(QColor(QStringLiteral("#8d98a5")));
|
||||
painter.drawText(rect(), Qt::AlignCenter | Qt::TextWordWrap, message);
|
||||
}
|
||||
|
||||
void VideoRenderWidget::releaseTextures()
|
||||
{
|
||||
if (m_textures[0]) {
|
||||
glDeleteTextures(3, m_textures);
|
||||
m_textures[0] = m_textures[1] = m_textures[2] = 0;
|
||||
std::fill(m_textureWidths, m_textureWidths + 3, 0);
|
||||
std::fill(m_textureHeights, m_textureHeights + 3, 0);
|
||||
std::fill(m_textureFormats, m_textureFormats + 3, 0);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user