1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40
41#include "qlogicaspect.h"
42#include "qlogicaspect_p.h"
43
44#include <Qt3DLogic/qframeaction.h>
45#include <Qt3DCore/qnode.h>
46#include <QtCore/QThread>
47#include <QtGui/QWindow>
48
49#include <Qt3DLogic/private/executor_p.h>
50#include <Qt3DLogic/private/handler_p.h>
51#include <Qt3DLogic/private/manager_p.h>
52#include <Qt3DCore/private/qchangearbiter_p.h>
53#include <Qt3DCore/private/qscene_p.h>
54#include <Qt3DCore/private/qservicelocator_p.h>
55
56QT_BEGIN_NAMESPACE
57
58using namespace Qt3DCore;
59
60namespace Qt3DLogic {
61
62/*!
63 \class Qt3DLogic::QLogicAspect
64 \inherits Qt3DCore::QAbstractAspect
65 \inmodule Qt3DLogic
66 \brief Responsible for handling frame synchronization jobs.
67 \since 5.7
68*/
69
70QLogicAspectPrivate::QLogicAspectPrivate()
71 : QAbstractAspectPrivate()
72 , m_time(0)
73 , m_initialized(false)
74 , m_manager(new Logic::Manager)
75 , m_executor(new Logic::Executor)
76 , m_callbackJob(new Logic::CallbackJob)
77{
78 m_callbackJob->setManager(m_manager.data());
79 m_manager->setExecutor(m_executor.data());
80}
81
82void QLogicAspectPrivate::onEngineAboutToShutdown()
83{
84 // Throw away any pending work that may deadlock during the shutdown procedure
85 // when the main thread waits for any queued jobs to finish.
86 m_executor->clearQueueAndProceed();
87}
88
89void QLogicAspectPrivate::registerBackendTypes()
90{
91 Q_Q(QLogicAspect);
92 q->registerBackendType<QFrameAction>(QBackendNodeMapperPtr(new Logic::HandlerFunctor(m_manager.data())));
93}
94
95/*!
96 Constructs a new Qt3DLogic::QLogicAspect instance with \a parent.
97*/
98QLogicAspect::QLogicAspect(QObject *parent)
99 : QLogicAspect(*new QLogicAspectPrivate(), parent) {}
100
101/*! \internal */
102QLogicAspect::QLogicAspect(QLogicAspectPrivate &dd, QObject *parent)
103 : QAbstractAspect(dd, parent)
104{
105 Q_D(QLogicAspect);
106 setObjectName(QStringLiteral("Logic Aspect"));
107 d->registerBackendTypes();
108 d_func()->m_manager->setLogicAspect(this);
109}
110
111/*! \internal */
112QLogicAspect::~QLogicAspect()
113{
114}
115
116/*! \internal */
117QVector<QAspectJobPtr> QLogicAspect::jobsToExecute(qint64 time)
118{
119 Q_D(QLogicAspect);
120 const qint64 deltaTime = time - d->m_time;
121 const float dt = static_cast<float>(deltaTime) / 1.0e9;
122 d->m_manager->setDeltaTime(dt);
123 d->m_time = time;
124
125 // Create jobs that will get executed by the threadpool
126 QVector<QAspectJobPtr> jobs;
127
128 if (d->m_manager->hasFrameActions())
129 jobs.append(d->m_callbackJob);
130
131 return jobs;
132}
133
134/*! \internal */
135void QLogicAspect::onRegistered()
136{
137}
138
139/*! \internal */
140void QLogicAspect::onEngineStartup()
141{
142 Q_D(QLogicAspect);
143 d->m_executor->setScene(d->m_arbiter->scene());
144}
145
146} // namespace Qt3DLogic
147
148QT_END_NAMESPACE
149
150QT3D_REGISTER_NAMESPACED_ASPECT("logic", QT_PREPEND_NAMESPACE(Qt3DLogic), QLogicAspect)
151
152