fix: isolate camera snapshot rendering
This commit is contained in:
parent
daf547e1c2
commit
aeca58c087
@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
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_EXTENSIONS OFF)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
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
|
||||
|
||||
DEFINES += QT_DEPRECATED_WARNINGS _WIN32_WINNT=0x0600
|
||||
|
||||
@ -2006,6 +2006,10 @@ void MainWindow::updateVideoFrame(const QString &cameraId, const VideoFrame &fra
|
||||
{
|
||||
const QString resolvedId = cameraId.isEmpty() && !m_cameraPreviewTiles.isEmpty()
|
||||
? 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);
|
||||
if (it == m_cameraPreviewTiles.end())
|
||||
return;
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QPainter>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
@ -66,6 +68,10 @@ VideoRenderWidget::VideoRenderWidget(QWidget *parent)
|
||||
{
|
||||
setAutoFillBackground(false);
|
||||
setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
|
||||
m_stillImageOverlay = new QLabel(this);
|
||||
m_stillImageOverlay->setAlignment(Qt::AlignCenter);
|
||||
m_stillImageOverlay->setStyleSheet(QStringLiteral("background: #0e1115;"));
|
||||
m_stillImageOverlay->hide();
|
||||
}
|
||||
|
||||
VideoRenderWidget::~VideoRenderWidget()
|
||||
@ -109,6 +115,7 @@ void VideoRenderWidget::updateAspectHeight()
|
||||
void VideoRenderWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
QOpenGLWidget::resizeEvent(event);
|
||||
updateStillImageOverlay();
|
||||
updateAspectHeight();
|
||||
}
|
||||
|
||||
@ -116,6 +123,9 @@ void VideoRenderWidget::setFrame(const VideoFrame &frame)
|
||||
{
|
||||
if (!frame.isValid())
|
||||
return;
|
||||
m_stillImage = QImage();
|
||||
m_stillImageOverlay->clear();
|
||||
m_stillImageOverlay->hide();
|
||||
m_frame = frame;
|
||||
if (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())
|
||||
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));
|
||||
m_stillImage = image.copy();
|
||||
if (m_layoutFrameSize != m_stillImage.size()) {
|
||||
m_layoutFrameSize = m_stillImage.size();
|
||||
updateAspectHeight();
|
||||
if (m_frameAspectLayout)
|
||||
updateGeometry();
|
||||
}
|
||||
setFrame(frame);
|
||||
m_message.clear();
|
||||
updateStillImageOverlay();
|
||||
m_stillImageOverlay->show();
|
||||
m_stillImageOverlay->raise();
|
||||
}
|
||||
|
||||
void VideoRenderWidget::clearFrame(const QString &message)
|
||||
{
|
||||
m_frame = VideoFrame();
|
||||
m_stillImage = QImage();
|
||||
m_stillImageOverlay->clear();
|
||||
m_stillImageOverlay->hide();
|
||||
m_message = message;
|
||||
m_frameDirty = false;
|
||||
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()
|
||||
{
|
||||
initializeOpenGLFunctions();
|
||||
@ -202,24 +225,6 @@ void VideoRenderWidget::paintGL()
|
||||
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) {
|
||||
@ -268,9 +273,11 @@ void VideoRenderWidget::paintGL()
|
||||
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("scaleU", m_frame.format == VideoPixelFormat::RGBA
|
||||
? 1.0f
|
||||
: 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");
|
||||
|
||||
@ -1,55 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include "videoframe.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QOpenGLShaderProgram;
|
||||
|
||||
class VideoRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VideoRenderWidget(QWidget *parent = nullptr);
|
||||
~VideoRenderWidget() override;
|
||||
|
||||
void setFrame(const VideoFrame &frame);
|
||||
void setImage(const QImage &image);
|
||||
void clearFrame(const QString &message = QStringLiteral("暂无画面"));
|
||||
bool hasFrame() const { return m_frame.isValid(); }
|
||||
#pragma once
|
||||
|
||||
#include "videoframe.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLWidget>
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class QOpenGLShaderProgram;
|
||||
class QLabel;
|
||||
|
||||
class VideoRenderWidget : public QOpenGLWidget, protected QOpenGLFunctions
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VideoRenderWidget(QWidget *parent = nullptr);
|
||||
~VideoRenderWidget() override;
|
||||
|
||||
void setFrame(const VideoFrame &frame);
|
||||
void setImage(const QImage &image);
|
||||
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.
|
||||
void setFrameAspectLayoutEnabled(bool enabled);
|
||||
int heightForWidth(int width) const override;
|
||||
QSize sizeHint() const override;
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
|
||||
protected:
|
||||
void initializeGL() override;
|
||||
void paintGL() override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
|
||||
private:
|
||||
void releaseTextures();
|
||||
void uploadPlane(unsigned int texture, int unit, int width, int height,
|
||||
unsigned int format, const QByteArray &data,
|
||||
int textureIndex);
|
||||
|
||||
private:
|
||||
void releaseTextures();
|
||||
void uploadPlane(unsigned int texture, int unit, int width, int height,
|
||||
unsigned int format, const QByteArray &data,
|
||||
int textureIndex);
|
||||
void drawMessage(const QString &message);
|
||||
void updateAspectHeight();
|
||||
void updateStillImageOverlay();
|
||||
bool m_frameAspectLayout = false;
|
||||
QSize m_layoutFrameSize{640, 480}; // Placeholder until the first camera frame.
|
||||
|
||||
std::unique_ptr<QOpenGLShaderProgram> m_program;
|
||||
VideoFrame m_frame;
|
||||
QString m_message = QStringLiteral("暂无画面");
|
||||
unsigned int m_textures[3] = {0, 0, 0};
|
||||
int m_textureWidths[3] = {0, 0, 0};
|
||||
int m_textureHeights[3] = {0, 0, 0};
|
||||
unsigned int m_textureFormats[3] = {0, 0, 0};
|
||||
bool m_frameDirty = false;
|
||||
bool m_glReady = false;
|
||||
};
|
||||
|
||||
std::unique_ptr<QOpenGLShaderProgram> m_program;
|
||||
VideoFrame m_frame;
|
||||
QImage m_stillImage;
|
||||
QLabel *m_stillImageOverlay = nullptr;
|
||||
QString m_message = QStringLiteral("暂无画面");
|
||||
unsigned int m_textures[3] = {0, 0, 0};
|
||||
int m_textureWidths[3] = {0, 0, 0};
|
||||
int m_textureHeights[3] = {0, 0, 0};
|
||||
unsigned int m_textureFormats[3] = {0, 0, 0};
|
||||
bool m_frameDirty = false;
|
||||
bool m_glReady = false;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user