diff options
-rw-r--r-- | src/core/aspects/qaspectmanager.cpp | 48 | ||||
-rw-r--r-- | src/core/aspects/qaspectmanager_p.h | 9 | ||||
-rw-r--r-- | src/render/services/vsyncframeadvanceservice.cpp | 5 | ||||
-rw-r--r-- | tests/auto/core/nodes/tst_nodes.cpp | 6 |
4 files changed, 59 insertions, 9 deletions
diff --git a/src/core/aspects/qaspectmanager.cpp b/src/core/aspects/qaspectmanager.cpp index f24248399..74803349c 100644 --- a/src/core/aspects/qaspectmanager.cpp +++ b/src/core/aspects/qaspectmanager.cpp @@ -65,6 +65,7 @@ #include <Qt3DCore/private/qscene_p.h> #include <QtCore/QCoreApplication> +#include <QtCore/QAbstractAnimation> #if defined(QT3D_CORE_JOB_TIMING) #include <QElapsedTimer> @@ -74,6 +75,23 @@ QT_BEGIN_NAMESPACE namespace Qt3DCore { +#if QT_CONFIG(animation) +class RequestFrameAnimation final : public QAbstractAnimation +{ +public: + RequestFrameAnimation(QObject *parent) + : QAbstractAnimation(parent) + { + } + + ~RequestFrameAnimation() override; + + int duration() const override { return 1; } + void updateCurrentTime(int currentTime) override { Q_UNUSED(currentTime) } +}; + +RequestFrameAnimation::~RequestFrameAnimation() = default; +#else namespace { class RequestFrameEvent : public QEvent @@ -92,7 +110,7 @@ private: int RequestFrameEvent::requestEventType = QEvent::registerEventType(); } // anonymous - +#endif /*! \class Qt3DCore::QAspectManager @@ -108,6 +126,9 @@ QAspectManager::QAspectManager(QObject *parent) , m_simulationLoopRunning(false) , m_driveMode(QAspectEngine::Automatic) , m_postConstructorInit(nullptr) +#if QT_CONFIG(animation) + , m_simulationAnimation(nullptr) +#endif { qRegisterMetaType<QSurface *>("QSurface*"); qCDebug(Aspects) << Q_FUNC_INFO; @@ -149,8 +170,19 @@ void QAspectManager::enterSimulationLoop() qCDebug(Aspects) << "Done calling onEngineStartup() for each aspect"; // Start running loop if Qt3D is in charge of driving it - if (m_driveMode == QAspectEngine::Automatic) + if (m_driveMode == QAspectEngine::Automatic) { +#if QT_CONFIG(animation) + if (!m_simulationAnimation) { + m_simulationAnimation = new RequestFrameAnimation(this); + connect(m_simulationAnimation, &QAbstractAnimation::finished, this, [this]() { + processFrame(); + if (m_simulationLoopRunning && m_driveMode == QAspectEngine::Automatic) + requestNextFrame(); + }); + } +#endif requestNextFrame(); + } } // Main thread (called by QAspectEngine) @@ -164,6 +196,11 @@ void QAspectManager::exitSimulationLoop() return; } +#if QT_CONFIG(animation) + if (m_simulationAnimation) + m_simulationAnimation->stop(); +#endif + QAbstractFrameAdvanceService *frameAdvanceService = m_serviceLocator->service<QAbstractFrameAdvanceService>(QServiceLocator::FrameAdvanceService); if (frameAdvanceService) @@ -193,7 +230,6 @@ void QAspectManager::exitSimulationLoop() } qCDebug(Aspects) << "Done calling onEngineShutdown() for each aspect"; - m_simulationLoopRunning = false; qCDebug(Aspects) << "exitSimulationLoop completed"; } @@ -399,6 +435,7 @@ QVector<QNode *> QAspectManager::lookupNodes(const QVector<QNodeId> &ids) const return d->m_scene ? d->m_scene->lookupNodes(ids) : QVector<QNode *>{}; } +#if !QT_CONFIG(animation) /*! \internal \brief Drives the Qt3D simulation loop in the main thread @@ -420,13 +457,18 @@ bool QAspectManager::event(QEvent *e) return QObject::event(e); } +#endif void QAspectManager::requestNextFrame() { qCDebug(Aspects) << "Requesting new Frame"; // Post event in the event loop to force // next frame to be processed +#if QT_CONFIG(animation) + m_simulationAnimation->start(); +#else QCoreApplication::postEvent(this, new RequestFrameEvent()); +#endif } void QAspectManager::processFrame() diff --git a/src/core/aspects/qaspectmanager_p.h b/src/core/aspects/qaspectmanager_p.h index ebc148324..12bf5d72a 100644 --- a/src/core/aspects/qaspectmanager_p.h +++ b/src/core/aspects/qaspectmanager_p.h @@ -76,6 +76,9 @@ class QAbstractAspectJobManager; class QServiceLocator; class NodePostConstructorInit; struct NodeTreeChange; +#if QT_CONFIG(animation) +class RequestFrameAnimation; +#endif class Q_3DCORE_PRIVATE_EXPORT QAspectManager : public QObject { @@ -112,7 +115,9 @@ public: QVector<QNode *> lookupNodes(const QVector<QNodeId> &ids) const; private: +#if !QT_CONFIG(animation) bool event(QEvent *event) override; +#endif void requestNextFrame(); QVector<QAbstractAspect *> m_aspects; @@ -126,7 +131,9 @@ private: QAspectEngine::RunMode m_driveMode; QVector<NodeTreeChange> m_nodeTreeChanges; NodePostConstructorInit* m_postConstructorInit; - +#if QT_CONFIG(animation) + RequestFrameAnimation *m_simulationAnimation; +#endif }; } // namespace Qt3DCore diff --git a/src/render/services/vsyncframeadvanceservice.cpp b/src/render/services/vsyncframeadvanceservice.cpp index d7398e2ce..662c12e4e 100644 --- a/src/render/services/vsyncframeadvanceservice.cpp +++ b/src/render/services/vsyncframeadvanceservice.cpp @@ -80,12 +80,7 @@ qint64 VSyncFrameAdvanceService::waitForNextFrame() { Q_D(VSyncFrameAdvanceService); -#ifdef Q_OS_MACOS - if (!d->m_semaphore.tryAcquire(std::max(d->m_semaphore.available(), 1), 4)) - return -1; -#else d->m_semaphore.acquire(std::max(d->m_semaphore.available(), 1)); -#endif const qint64 currentTime = d->m_elapsed.nsecsElapsed(); qCDebug(VSyncAdvanceService) << "Elapsed nsecs since last call " << currentTime - d->m_elapsedTimeSincePreviousFrame; diff --git a/tests/auto/core/nodes/tst_nodes.cpp b/tests/auto/core/nodes/tst_nodes.cpp index b5291cab7..43d9f7778 100644 --- a/tests/auto/core/nodes/tst_nodes.cpp +++ b/tests/auto/core/nodes/tst_nodes.cpp @@ -944,6 +944,7 @@ void tst_Nodes::checkParentChangeFromExistingBackendParentToNewlyCreatedParent() // GIVEN ObserverSpy spy; Qt3DCore::QAspectEngine engine; + engine.setRunMode(Qt3DCore::QAspectEngine::Manual); QScopedPointer<MyQEntity> root(new MyQEntity()); root->setArbiterAndEngine(&spy, &engine); auto aspect = new TestAspect; @@ -953,6 +954,7 @@ void tst_Nodes::checkParentChangeFromExistingBackendParentToNewlyCreatedParent() MyQNode *child2(new MyQNode(root.data())); QCoreApplication::processEvents(); + engine.processFrame(); // Due to the way we create root, it has a backend QVERIFY(Qt3DCore::QNodePrivate::get(root.data())->m_hasBackendNode == true); @@ -983,6 +985,7 @@ void tst_Nodes::checkParentChangeFromExistingBackendParentToNewlyCreatedParent() // WHEN QCoreApplication::processEvents(); + engine.processFrame(); // THEN QCOMPARE(spy.events.size(), 2); @@ -1059,6 +1062,7 @@ void tst_Nodes::checkParentChangeFromExistingBackendParentToNewlyCreatedParent() // WHEN QCoreApplication::processEvents(); + engine.processFrame(); // THEN QCOMPARE(spy.events.size(), 2); @@ -1237,6 +1241,7 @@ void tst_Nodes::checkAllBackendCreationDoneInSingleFrame() // GIVEN ObserverSpy spy; Qt3DCore::QAspectEngine engine; + engine.setRunMode(Qt3DCore::QAspectEngine::Manual); auto aspect = new TestAspect; engine.registerAspect(aspect); @@ -1269,6 +1274,7 @@ void tst_Nodes::checkAllBackendCreationDoneInSingleFrame() // WHEN QCoreApplication::processEvents(); + engine.processFrame(); // THEN - both children have their backend nodes actually created. QCOMPARE(aspect->events.count(), 2); |