diff options
Diffstat (limited to 'src/multimediaquick/qquickvideooutput.cpp')
-rw-r--r-- | src/multimediaquick/qquickvideooutput.cpp | 135 |
1 files changed, 63 insertions, 72 deletions
diff --git a/src/multimediaquick/qquickvideooutput.cpp b/src/multimediaquick/qquickvideooutput.cpp index fe2119e61..8af974759 100644 --- a/src/multimediaquick/qquickvideooutput.cpp +++ b/src/multimediaquick/qquickvideooutput.cpp @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Copyright (C) 2016 Research In Motion -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2016 Research In Motion +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qquickvideooutput_p.h" #include <private/qvideooutputorientationhandler_p.h> @@ -47,11 +11,13 @@ #include <qvideosink.h> #include <QtQuick/QQuickWindow> #include <private/qquickwindow_p.h> +#include <private/qmultimediautils_p.h> #include <qsgvideonode_p.h> +#include <QtCore/qrunnable.h> QT_BEGIN_NAMESPACE -Q_LOGGING_CATEGORY(qLcVideo, "qt.multimedia.video") +static Q_LOGGING_CATEGORY(qLcVideo, "qt.multimedia.video") namespace { @@ -141,8 +107,12 @@ QQuickVideoOutput::QQuickVideoOutput(QQuickItem *parent) : m_sink = new QVideoSink(this); qRegisterMetaType<QVideoFrameFormat>(); - QObject::connect(m_sink, SIGNAL(videoFrameChanged(const QVideoFrame &)), - this, SLOT(_q_newFrame(const QVideoFrame &)), Qt::QueuedConnection); + connect(m_sink, &QVideoSink::videoFrameChanged, this, + [this](const QVideoFrame &frame) { + setFrame(frame); + QMetaObject::invokeMethod(this, &QQuickVideoOutput::_q_newFrame, frame.size()); + }, + Qt::DirectConnection); initRhiForSink(); } @@ -197,13 +167,11 @@ void QQuickVideoOutput::setFillMode(FillMode mode) emit fillModeChanged(mode); } -void QQuickVideoOutput::_q_newFrame(const QVideoFrame &frame) +void QQuickVideoOutput::_q_newFrame(QSize size) { - present(frame); - QSize size = frame.size(); - if (!qIsDefaultAspect(m_orientation + m_frameOrientation)) { - size.transpose(); - } + update(); + + size = qRotatedFrameSize(size, m_orientation + m_frameOrientation); if (m_nativeSize != size) { m_nativeSize = size; @@ -356,7 +324,7 @@ QRectF QQuickVideoOutput::sourceRect() const if (!size.isValid()) return {}; - if (!qIsDefaultAspect(m_orientation)) + if (!qIsDefaultAspect(m_orientation + m_frameOrientation)) size.transpose(); @@ -425,23 +393,23 @@ void QQuickVideoOutput::itemChange(QQuickItem::ItemChange change, if (m_window) { // We want to receive the signals in the render thread - QObject::connect(m_window, &QQuickWindow::sceneGraphInitialized, this, &QQuickVideoOutput::_q_sceneGraphInitialized, - Qt::DirectConnection); - QObject::connect(m_window, &QQuickWindow::sceneGraphInvalidated, - this, &QQuickVideoOutput::_q_invalidateSceneGraph, Qt::DirectConnection); + connect(m_window, &QQuickWindow::sceneGraphInitialized, this, + &QQuickVideoOutput::_q_sceneGraphInitialized, Qt::DirectConnection); + connect(m_window, &QQuickWindow::sceneGraphInvalidated, this, + &QQuickVideoOutput::_q_invalidateSceneGraph, Qt::DirectConnection); } initRhiForSink(); } QSize QQuickVideoOutput::nativeSize() const { - return m_surfaceFormat.viewport().size(); + return m_videoFormat.viewport().size(); } void QQuickVideoOutput::updateGeometry() { - const QRectF viewport = m_surfaceFormat.viewport(); - const QSizeF frameSize = m_surfaceFormat.frameSize(); + const QRectF viewport = m_videoFormat.viewport(); + const QSizeF frameSize = m_videoFormat.frameSize(); const QRectF normalizedViewport(viewport.x() / frameSize.width(), viewport.y() / frameSize.height(), viewport.width() / frameSize.width(), @@ -482,13 +450,13 @@ void QQuickVideoOutput::updateGeometry() } } - if (m_surfaceFormat.scanLineDirection() == QVideoFrameFormat::BottomToTop) { + if (m_videoFormat.scanLineDirection() == QVideoFrameFormat::BottomToTop) { qreal top = m_sourceTextureRect.top(); m_sourceTextureRect.setTop(m_sourceTextureRect.bottom()); m_sourceTextureRect.setBottom(top); } - if (m_surfaceFormat.isMirrored()) { + if (m_videoFormat.isMirrored()) { qreal left = m_sourceTextureRect.left(); m_sourceTextureRect.setLeft(m_sourceTextureRect.right()); m_sourceTextureRect.setRight(left); @@ -522,7 +490,7 @@ QSGNode *QQuickVideoOutput::updatePaintNode(QSGNode *oldNode, // Get a node that supports our frame. The surface is irrelevant, our // QSGVideoItemSurface supports (logically) anything. updateGeometry(); - videoNode = new QSGVideoNode(this, m_surfaceFormat); + videoNode = new QSGVideoNode(this, m_videoFormat); qCDebug(qLcVideo) << "updatePaintNode: Video node created. Handle type:" << m_frame.handleType(); } } @@ -536,6 +504,8 @@ QSGNode *QQuickVideoOutput::updatePaintNode(QSGNode *oldNode, if (m_frameChanged) { videoNode->setCurrentFrame(m_frame); + updateHdr(videoNode); + //don't keep the frame for more than really necessary m_frameChanged = false; m_frame = QVideoFrame(); @@ -548,26 +518,47 @@ QSGNode *QQuickVideoOutput::updatePaintNode(QSGNode *oldNode, return videoNode; } -QRectF QQuickVideoOutput::adjustedViewport() const +void QQuickVideoOutput::updateHdr(QSGVideoNode *videoNode) { - return m_surfaceFormat.viewport(); + auto *videoOutputWindow = window(); + if (!videoOutputWindow) + return; + + auto *swapChain = videoOutputWindow->swapChain(); + if (!swapChain) + return; + + const auto requiredSwapChainFormat = qGetRequiredSwapChainFormat(m_frame.surfaceFormat()); + if (qShouldUpdateSwapChainFormat(swapChain, requiredSwapChainFormat)) { + auto *recreateSwapChainJob = QRunnable::create([swapChain, requiredSwapChainFormat]() { + swapChain->destroy(); + swapChain->setFormat(requiredSwapChainFormat); + swapChain->createOrResize(); + }); + + // Even though the 'recreate swap chain' job is scheduled for the current frame the + // effect will be visible only starting from the next frame since the recreation would + // happen after the actual swap. + videoOutputWindow->scheduleRenderJob(recreateSwapChainJob, QQuickWindow::AfterSwapStage); + } + + videoNode->setSurfaceFormat(swapChain->format()); + videoNode->setHdrInfo(swapChain->hdrInfo()); } -void QQuickVideoOutput::present(const QVideoFrame &frame) +QRectF QQuickVideoOutput::adjustedViewport() const { - m_frameMutex.lock(); - m_surfaceFormat = frame.surfaceFormat(); - m_frame = frame; - m_frameOrientation = frame.rotationAngle(); - m_frameChanged = true; - m_frameMutex.unlock(); - - update(); + return m_videoFormat.viewport(); } -void QQuickVideoOutput::stop() +void QQuickVideoOutput::setFrame(const QVideoFrame &frame) { - present(QVideoFrame()); + QMutexLocker lock(&m_frameMutex); + + m_videoFormat = frame.surfaceFormat(); + m_frame = frame; + m_frameOrientation = static_cast<int>(frame.rotation()); + m_frameChanged = true; } QT_END_NAMESPACE |