fix: isolate camera snapshot rendering

This commit is contained in:
linbo 2026-10-09 16:55:09 +08:00
parent daf547e1c2
commit aeca58c087
5 changed files with 94 additions and 79 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(CmvrControlStation VERSION 1.0.0 LANGUAGES CXX) project(CmvrControlStation VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_AUTOMOC ON) set(CMAKE_AUTOMOC ON)

View File

@ -1,6 +1,6 @@
QT += core gui widgets multimedia 3dcore 3drender 3dextras 3dinput 3danimation QT += core gui widgets multimedia 3dcore 3drender 3dextras 3dinput 3danimation
CONFIG += c++14 warn_on debug_and_release CONFIG += c++17 warn_on debug_and_release
TEMPLATE = app TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS _WIN32_WINNT=0x0600 DEFINES += QT_DEPRECATED_WARNINGS _WIN32_WINNT=0x0600

View File

@ -2006,6 +2006,10 @@ void MainWindow::updateVideoFrame(const QString &cameraId, const VideoFrame &fra
{ {
const QString resolvedId = cameraId.isEmpty() && !m_cameraPreviewTiles.isEmpty() const QString resolvedId = cameraId.isEmpty() && !m_cameraPreviewTiles.isEmpty()
? m_cameraPreviewTiles.constBegin().key() : cameraId; ? m_cameraPreviewTiles.constBegin().key() : cameraId;
// The decoder thread can already have queued frames when a stream is
// stopped. Do not let those stale frames overwrite a later snapshot.
if (!m_runningCameraIds.contains(resolvedId))
return;
const auto it = m_cameraPreviewTiles.find(resolvedId); const auto it = m_cameraPreviewTiles.find(resolvedId);
if (it == m_cameraPreviewTiles.end()) if (it == m_cameraPreviewTiles.end())
return; return;

View File

@ -2,6 +2,8 @@
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QPainter> #include <QPainter>
#include <QLabel>
#include <QPixmap>
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
@ -66,6 +68,10 @@ VideoRenderWidget::VideoRenderWidget(QWidget *parent)
{ {
setAutoFillBackground(false); setAutoFillBackground(false);
setUpdateBehavior(QOpenGLWidget::NoPartialUpdate); setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
m_stillImageOverlay = new QLabel(this);
m_stillImageOverlay->setAlignment(Qt::AlignCenter);
m_stillImageOverlay->setStyleSheet(QStringLiteral("background: #0e1115;"));
m_stillImageOverlay->hide();
} }
VideoRenderWidget::~VideoRenderWidget() VideoRenderWidget::~VideoRenderWidget()
@ -109,6 +115,7 @@ void VideoRenderWidget::updateAspectHeight()
void VideoRenderWidget::resizeEvent(QResizeEvent *event) void VideoRenderWidget::resizeEvent(QResizeEvent *event)
{ {
QOpenGLWidget::resizeEvent(event); QOpenGLWidget::resizeEvent(event);
updateStillImageOverlay();
updateAspectHeight(); updateAspectHeight();
} }
@ -116,6 +123,9 @@ void VideoRenderWidget::setFrame(const VideoFrame &frame)
{ {
if (!frame.isValid()) if (!frame.isValid())
return; return;
m_stillImage = QImage();
m_stillImageOverlay->clear();
m_stillImageOverlay->hide();
m_frame = frame; m_frame = frame;
if (m_layoutFrameSize != QSize(frame.width, frame.height)) { if (m_layoutFrameSize != QSize(frame.width, frame.height)) {
m_layoutFrameSize = QSize(frame.width, frame.height); m_layoutFrameSize = QSize(frame.width, frame.height);
@ -131,28 +141,41 @@ void VideoRenderWidget::setImage(const QImage &image)
{ {
if (image.isNull()) if (image.isNull())
return; return;
const QImage converted = image.convertToFormat(QImage::Format_RGBA8888); m_stillImage = image.copy();
VideoFrame frame; if (m_layoutFrameSize != m_stillImage.size()) {
frame.width = converted.width(); m_layoutFrameSize = m_stillImage.size();
frame.height = converted.height(); updateAspectHeight();
frame.format = VideoPixelFormat::RGBA; if (m_frameAspectLayout)
frame.stride0 = frame.width * 4; updateGeometry();
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); m_message.clear();
updateStillImageOverlay();
m_stillImageOverlay->show();
m_stillImageOverlay->raise();
} }
void VideoRenderWidget::clearFrame(const QString &message) void VideoRenderWidget::clearFrame(const QString &message)
{ {
m_frame = VideoFrame(); m_frame = VideoFrame();
m_stillImage = QImage();
m_stillImageOverlay->clear();
m_stillImageOverlay->hide();
m_message = message; m_message = message;
m_frameDirty = false; m_frameDirty = false;
update(); update();
} }
void VideoRenderWidget::updateStillImageOverlay()
{
if (!m_stillImageOverlay)
return;
m_stillImageOverlay->setGeometry(rect());
if (m_stillImage.isNull())
return;
m_stillImageOverlay->setPixmap(QPixmap::fromImage(m_stillImage).scaled(
size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
void VideoRenderWidget::initializeGL() void VideoRenderWidget::initializeGL()
{ {
initializeOpenGLFunctions(); initializeOpenGLFunctions();
@ -202,24 +225,6 @@ void VideoRenderWidget::paintGL()
return; 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); glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
if (m_frameDirty) { if (m_frameDirty) {
if (m_frame.format == VideoPixelFormat::I420) { if (m_frame.format == VideoPixelFormat::I420) {
@ -268,9 +273,11 @@ void VideoRenderWidget::paintGL()
m_frame.format == VideoPixelFormat::RGBA ? 2.0f : 0.0f); m_frame.format == VideoPixelFormat::RGBA ? 2.0f : 0.0f);
m_program->setUniformValue("scaleY", m_frame.format == VideoPixelFormat::RGBA m_program->setUniformValue("scaleY", m_frame.format == VideoPixelFormat::RGBA
? 1.0f : static_cast<float>(m_frame.width) / m_frame.stride0); ? 1.0f : static_cast<float>(m_frame.width) / m_frame.stride0);
m_program->setUniformValue("scaleU", m_frame.format == VideoPixelFormat::NV12 m_program->setUniformValue("scaleU", m_frame.format == VideoPixelFormat::RGBA
? static_cast<float>((m_frame.width + 1) / 2) / (m_frame.stride1 / 2) ? 1.0f
: static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride1); : 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 m_program->setUniformValue("scaleV", m_frame.format == VideoPixelFormat::I420
? static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride2 : 1.0f); ? static_cast<float>((m_frame.width + 1) / 2) / m_frame.stride2 : 1.0f);
const int position = m_program->attributeLocation("position"); const int position = m_program->attributeLocation("position");

View File

@ -1,55 +1,59 @@
#pragma once #pragma once
#include "videoframe.h" #include "videoframe.h"
#include <QImage> #include <QImage>
#include <QOpenGLFunctions> #include <QOpenGLFunctions>
#include <QOpenGLWidget> #include <QOpenGLWidget>
#include <QString> #include <QString>
#include <memory> #include <memory>
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class QLabel;
class VideoRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions
{ class VideoRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions
Q_OBJECT {
Q_OBJECT
public:
explicit VideoRenderWidget(QWidget *parent = nullptr); public:
~VideoRenderWidget() override; explicit VideoRenderWidget(QWidget *parent = nullptr);
~VideoRenderWidget() override;
void setFrame(const VideoFrame &frame);
void setImage(const QImage &image); void setFrame(const VideoFrame &frame);
void clearFrame(const QString &message = QStringLiteral("暂无画面")); void setImage(const QImage &image);
bool hasFrame() const { return m_frame.isValid(); } void clearFrame(const QString &message = QStringLiteral("暂无画面"));
bool hasFrame() const { return m_frame.isValid() || !m_stillImage.isNull(); }
// Opt-in for calibration panels; other video pages keep their layout. // Opt-in for calibration panels; other video pages keep their layout.
void setFrameAspectLayoutEnabled(bool enabled); void setFrameAspectLayoutEnabled(bool enabled);
int heightForWidth(int width) const override; int heightForWidth(int width) const override;
QSize sizeHint() const override; QSize sizeHint() const override;
protected: protected:
void initializeGL() override; void initializeGL() override;
void paintGL() override; void paintGL() override;
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
private: private:
void releaseTextures(); void releaseTextures();
void uploadPlane(unsigned int texture, int unit, int width, int height, void uploadPlane(unsigned int texture, int unit, int width, int height,
unsigned int format, const QByteArray &data, unsigned int format, const QByteArray &data,
int textureIndex); int textureIndex);
void drawMessage(const QString &message); void drawMessage(const QString &message);
void updateAspectHeight(); void updateAspectHeight();
void updateStillImageOverlay();
bool m_frameAspectLayout = false; bool m_frameAspectLayout = false;
QSize m_layoutFrameSize{640, 480}; // Placeholder until the first camera frame. QSize m_layoutFrameSize{640, 480}; // Placeholder until the first camera frame.
std::unique_ptr<QOpenGLShaderProgram> m_program; std::unique_ptr<QOpenGLShaderProgram> m_program;
VideoFrame m_frame; VideoFrame m_frame;
QString m_message = QStringLiteral("暂无画面"); QImage m_stillImage;
unsigned int m_textures[3] = {0, 0, 0}; QLabel *m_stillImageOverlay = nullptr;
int m_textureWidths[3] = {0, 0, 0}; QString m_message = QStringLiteral("暂无画面");
int m_textureHeights[3] = {0, 0, 0}; unsigned int m_textures[3] = {0, 0, 0};
unsigned int m_textureFormats[3] = {0, 0, 0}; int m_textureWidths[3] = {0, 0, 0};
bool m_frameDirty = false; int m_textureHeights[3] = {0, 0, 0};
bool m_glReady = false; unsigned int m_textureFormats[3] = {0, 0, 0};
}; bool m_frameDirty = false;
bool m_glReady = false;
};