summaryrefslogtreecommitdiffstats
path: root/src/core/aspects/qaspectmanager.cpp
blob: f24248399fffa35280155e428eae64a9d0ce257d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module 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$
**
****************************************************************************/

#include "qaspectmanager_p.h"

#include <Qt3DCore/qabstractaspect.h>
#include <Qt3DCore/qentity.h>
#include <QtCore/QAbstractEventDispatcher>
#include <QtCore/QEventLoop>
#include <QtCore/QThread>
#include <QtCore/QWaitCondition>
#include <QtGui/QSurface>

#include <Qt3DCore/private/corelogging_p.h>
#include <Qt3DCore/private/qabstractaspect_p.h>
#include <Qt3DCore/private/qabstractaspectjobmanager_p.h>
#include <Qt3DCore/private/qabstractframeadvanceservice_p.h>
// TODO Make the kind of job manager configurable (e.g. ThreadWeaver vs Intel TBB)
#include <Qt3DCore/private/qaspectjobmanager_p.h>
#include <Qt3DCore/private/qaspectjob_p.h>
#include <Qt3DCore/private/qchangearbiter_p.h>
#include <Qt3DCore/private/qscheduler_p.h>
#include <Qt3DCore/private/qservicelocator_p.h>
#include <Qt3DCore/private/qthreadpooler_p.h>
#include <Qt3DCore/private/qtickclock_p.h>
#include <Qt3DCore/private/qtickclockservice_p.h>
#include <Qt3DCore/private/qnodevisitor_p.h>
#include <Qt3DCore/private/qnode_p.h>
#include <Qt3DCore/private/qscene_p.h>

#include <QtCore/QCoreApplication>

#if defined(QT3D_CORE_JOB_TIMING)
#include <QElapsedTimer>
#endif

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

namespace  {

class RequestFrameEvent : public QEvent
{
public:
    RequestFrameEvent()
        : QEvent(static_cast<QEvent::Type>(RequestFrameEvent::requestEventType))
    {}

    static int eventType() { return RequestFrameEvent::requestEventType; }

private:
    static int requestEventType;
};

int RequestFrameEvent::requestEventType = QEvent::registerEventType();

} // anonymous


/*!
    \class Qt3DCore::QAspectManager
    \internal
*/
QAspectManager::QAspectManager(QObject *parent)
    : QObject(parent)
    , m_root(nullptr)
    , m_scheduler(new QScheduler(this))
    , m_jobManager(new QAspectJobManager(this))
    , m_changeArbiter(new QChangeArbiter(this))
    , m_serviceLocator(new QServiceLocator())
    , m_simulationLoopRunning(false)
    , m_driveMode(QAspectEngine::Automatic)
    , m_postConstructorInit(nullptr)
{
    qRegisterMetaType<QSurface *>("QSurface*");
    qCDebug(Aspects) << Q_FUNC_INFO;
}

QAspectManager::~QAspectManager()
{
    delete m_changeArbiter;
    delete m_jobManager;
    delete m_scheduler;
}

void QAspectManager::setRunMode(QAspectEngine::RunMode mode)
{
    qCDebug(Aspects) << Q_FUNC_INFO << "Running Loop Drive Mode set to" << mode;
    m_driveMode = mode;
}

// Main thread (called by QAspectEngine)
void QAspectManager::enterSimulationLoop()
{
    qCDebug(Aspects) << Q_FUNC_INFO;
    m_simulationLoopRunning = true;

    // Retrieve the frame advance service. Defaults to timer based if there is no renderer.
    QAbstractFrameAdvanceService *frameAdvanceService =
            m_serviceLocator->service<QAbstractFrameAdvanceService>(QServiceLocator::FrameAdvanceService);

    // Start the frameAdvanceService
    frameAdvanceService->start();

    // We are about to enter the simulation loop. Give aspects a chance to do any last
    // pieces of initialization
    qCDebug(Aspects) << "Calling onEngineStartup() for each aspect";
    for (QAbstractAspect *aspect : qAsConst(m_aspects)) {
        qCDebug(Aspects) << "\t" << aspect->objectName();
        aspect->onEngineStartup();
    }
    qCDebug(Aspects) << "Done calling onEngineStartup() for each aspect";

    // Start running loop if Qt3D is in charge of driving it
    if (m_driveMode == QAspectEngine::Automatic)
        requestNextFrame();
}

// Main thread (called by QAspectEngine)
void QAspectManager::exitSimulationLoop()
{
    qCDebug(Aspects) << Q_FUNC_INFO;

    // If this fails, simulation loop is already exited so nothing to do
    if (!m_simulationLoopRunning) {
        qCDebug(Aspects) << "Simulation loop was not running. Nothing to do";
        return;
    }

    QAbstractFrameAdvanceService *frameAdvanceService =
            m_serviceLocator->service<QAbstractFrameAdvanceService>(QServiceLocator::FrameAdvanceService);
    if (frameAdvanceService)
        frameAdvanceService->stop();

    // Give any aspects a chance to unqueue any asynchronous work they
    // may have scheduled that would otherwise potentially deadlock or
    // cause races. For example, the QLogicAspect queues up a vector of
    // QNodeIds to be processed by a callback on the main thread. However,
    // if we don't unqueue this work and release its semaphore, the logic
    // aspect would cause a deadlock when trying to exit the inner loop.
    // This is because we call this function from the main thread and the
    // logic aspect is waiting for the main thread to execute the
    // QLogicComponent::onFrameUpdate() callback.
    for (QAbstractAspect *aspect : qAsConst(m_aspects))
        aspect->d_func()->onEngineAboutToShutdown();

    // Process any pending changes from the frontend before we shut the aspects down
    m_changeArbiter->syncChanges();

    // Give aspects a chance to perform any shutdown actions. This may include unqueuing
    // any blocking work on the main thread that could potentially deadlock during shutdown.
    qCDebug(Aspects) << "Calling onEngineShutdown() for each aspect";
    for (QAbstractAspect *aspect : qAsConst(m_aspects)) {
        qCDebug(Aspects) << "\t" << aspect->objectName();
        aspect->onEngineShutdown();
    }
    qCDebug(Aspects) << "Done calling onEngineShutdown() for each aspect";


    m_simulationLoopRunning = false;
    qCDebug(Aspects) << "exitSimulationLoop completed";
}

bool QAspectManager::isShuttingDown() const
{
    return !m_simulationLoopRunning;
}

/*!
    \internal

    Called by the QAspectThread's run() method immediately after the manager
    has been created
*/
void QAspectManager::initialize()
{
    qCDebug(Aspects) << Q_FUNC_INFO;
    m_jobManager->initialize();
    m_scheduler->setAspectManager(this);
    m_changeArbiter->initialize(m_jobManager);
}

/*!
    \internal

    Called by the QAspectThread's run() method immediately after the manager's
    exec() function has returned.
*/
void QAspectManager::shutdown()
{
    qCDebug(Aspects) << Q_FUNC_INFO;

    // Aspects must be deleted in the Thread they were created in
}

// MainThread called by QAspectEngine::setRootEntity
void QAspectManager::setRootEntity(Qt3DCore::QEntity *root, const QVector<QNode *> &nodes)
{
    qCDebug(Aspects) << Q_FUNC_INFO;

    if (root == m_root)
        return;

    if (m_root) {
        // TODO: Delete all backend nodes. This is to be symmetric with how
        // we create them below in the call to setRootAndCreateNodes
    }

    m_root = root;

    if (m_root) {

        QVector<NodeTreeChange> nodeTreeChanges;
        nodeTreeChanges.reserve(nodes.size());

        for (QNode *n : nodes) {
            nodeTreeChanges.push_back({
                                          n->id(),
                                          QNodePrivate::get(n)->m_typeInfo,
                                          NodeTreeChange::Added,
                                          n
                                      });
        }

        for (QAbstractAspect *aspect : qAsConst(m_aspects))
            aspect->d_func()->setRootAndCreateNodes(m_root, nodeTreeChanges);
    }
}


// Main Thread -> immediately following node insertion
void QAspectManager::addNodes(const QVector<QNode *> &nodes)
{
    // We record the nodes added information, which we will actually use when
    // processFrame is called (later but within the same loop of the even loop
    // as this call) The idea is we want to avoid modifying the backend tree if
    // the Renderer hasn't allowed processFrame to continue yet

    QVector<NodeTreeChange> treeChanges;
    treeChanges.reserve(nodes.size());

    for (QNode *node : nodes) {
        treeChanges.push_back({ node->id(),
                                QNodePrivate::get(node)->m_typeInfo,
                                NodeTreeChange::Added,
                                node });
    }

    m_nodeTreeChanges += treeChanges;
}

// Main Thread -> immediately following node destruction (call from QNode dtor)
void QAspectManager::removeNodes(const QVector<QNode *> &nodes)
{
    // We record the nodes removed information, which we will actually use when
    // processFrame is called (later but within the same loop of the even loop
    // as this call) The idea is we want to avoid modifying the backend tree if
    // the Renderer hasn't allowed processFrame to continue yet The drawback is
    // that when processFrame is processed, the QNode* pointer might be invalid by
    // that point. Therefore we record all we need to remove the object.

    for (QNode *node : nodes) {
        // In addition, we check if we contain an Added change for a given node
        // that is now about to be destroyed. If so we remove the Added change
        // entirely

        m_nodeTreeChanges.erase(std::remove_if(m_nodeTreeChanges.begin(),
                                               m_nodeTreeChanges.end(),
                                               [&node] (const NodeTreeChange &change) { return change.id == node->id(); }),
                                m_nodeTreeChanges.end());

        m_nodeTreeChanges.push_back({ node->id(),
                                      QNodePrivate::get(node)->m_typeInfo,
                                      NodeTreeChange::Removed,
                                      nullptr });
    }
}

/*!
 * \internal
 *
 * Registers a new \a aspect.
 */
void QAspectManager::registerAspect(QAbstractAspect *aspect)
{
    qCDebug(Aspects) << "Registering aspect";

    if (aspect != nullptr) {
        m_aspects.append(aspect);
        QAbstractAspectPrivate::get(aspect)->m_aspectManager = this;
        QAbstractAspectPrivate::get(aspect)->m_jobManager = m_jobManager;
        QAbstractAspectPrivate::get(aspect)->m_arbiter = m_changeArbiter;

        // Allow the aspect to do some work now that it is registered
        aspect->onRegistered();
    }
    else {
        qCWarning(Aspects) << "Failed to register aspect";
    }
    qCDebug(Aspects) << "Completed registering aspect";
}

/*!
 * \internal
 *
 * Calls QAbstractAspect::onUnregistered(), unregisters the aspect from the
 * change arbiter and unsets the arbiter, job manager and aspect manager.
 * Operations are performed in the reverse order to registerAspect.
 */
void QAspectManager::unregisterAspect(Qt3DCore::QAbstractAspect *aspect)
{
    qCDebug(Aspects) << "Unregistering aspect";
    Q_ASSERT(aspect);
    aspect->onUnregistered();
    QAbstractAspectPrivate::get(aspect)->m_arbiter = nullptr;
    QAbstractAspectPrivate::get(aspect)->m_jobManager = nullptr;
    QAbstractAspectPrivate::get(aspect)->m_aspectManager = nullptr;
    m_aspects.removeOne(aspect);
    qCDebug(Aspects) << "Completed unregistering aspect";
}

const QVector<QAbstractAspect *> &QAspectManager::aspects() const
{
    return m_aspects;
}

QAbstractAspectJobManager *QAspectManager::jobManager() const
{
    return m_jobManager;
}

QChangeArbiter *QAspectManager::changeArbiter() const
{
    return m_changeArbiter;
}

QServiceLocator *QAspectManager::serviceLocator() const
{
    return m_serviceLocator.data();
}

void QAspectManager::setPostConstructorInit(NodePostConstructorInit *postConstructorInit)
{
    m_postConstructorInit = postConstructorInit;
}

QNode *QAspectManager::lookupNode(QNodeId id) const
{
    if (!m_root)
        return nullptr;

    QNodePrivate *d = QNodePrivate::get(m_root);
    return d->m_scene ? d->m_scene->lookupNode(id) : nullptr;
}

QVector<QNode *> QAspectManager::lookupNodes(const QVector<QNodeId> &ids) const
{
    if (!m_root)
        return {};

    QNodePrivate *d = QNodePrivate::get(m_root);
    return d->m_scene ? d->m_scene->lookupNodes(ids) : QVector<QNode *>{};
}

/*!
    \internal
    \brief Drives the Qt3D simulation loop in the main thread
 */
bool QAspectManager::event(QEvent *e)
{
    if (e->type() == RequestFrameEvent::eventType()) {

        // Process current frame
        processFrame();

        // Request next frame if we are still running and if Qt3D is driving
        // the loop
        if (m_simulationLoopRunning && m_driveMode == QAspectEngine::Automatic)
            requestNextFrame();

        return true;
    }

    return QObject::event(e);
}

void QAspectManager::requestNextFrame()
{
    qCDebug(Aspects) << "Requesting new Frame";
    // Post event in the event loop to force
    // next frame to be processed
    QCoreApplication::postEvent(this, new RequestFrameEvent());
}

void QAspectManager::processFrame()
{
    qCDebug(Aspects) << "Processing Frame";

    // Retrieve the frame advance service. Defaults to timer based if there is no renderer.
    QAbstractFrameAdvanceService *frameAdvanceService =
            m_serviceLocator->service<QAbstractFrameAdvanceService>(QServiceLocator::FrameAdvanceService);

    const qint64 t = frameAdvanceService->waitForNextFrame();
    if (t < 0)
        return;

    // Distribute accumulated changes. This includes changes sent from the frontend
    // to the backend nodes. We call this before the call to m_scheduler->update() to ensure
    // that any property changes do not set dirty flags in a data race with the renderer's
    // submission thread which may be looking for dirty flags, acting upon them and then
    // clearing the dirty flags.
    //
    // Doing this as the first call in the new frame ensures the lock free approach works
    // without any such data race.
#if QT_CONFIG(qt3d_profile_jobs)
    const quint32 arbiterId = 4096;
    JobRunStats changeArbiterStats;
    changeArbiterStats.jobId.typeAndInstance[0] = arbiterId;
    changeArbiterStats.jobId.typeAndInstance[1] = 0;
    changeArbiterStats.threadId = reinterpret_cast<quint64>(QThread::currentThreadId());
    changeArbiterStats.startTime = QThreadPooler::m_jobsStatTimer.nsecsElapsed();
#endif

    // Tell the NodePostConstructorInit to process any pending nodes which will add them to our list of
    // tree changes
    m_postConstructorInit->processNodes();

    // Add and Remove Nodes
    const QVector<NodeTreeChange> nodeTreeChanges = std::move(m_nodeTreeChanges);
    for (const NodeTreeChange &change : nodeTreeChanges) {
        // Buckets ensure that even if we have intermingled node added / removed
        // buckets, we preserve the order of the sequences

        for (QAbstractAspect *aspect : qAsConst(m_aspects)) {
            switch (change.type) {
            case NodeTreeChange::Added:
                aspect->d_func()->createBackendNode(change);
                break;
            case NodeTreeChange::Removed:
                aspect->d_func()->clearBackendNode(change);
                break;
            }
        }
    }

    // Sync node / subnode relationship changes
    const auto dirtySubNodes = m_changeArbiter->takeDirtyFrontEndSubNodes();
    if (dirtySubNodes.size())
        for (QAbstractAspect *aspect : qAsConst(m_aspects))
            QAbstractAspectPrivate::get(aspect)->syncDirtyFrontEndSubNodes(dirtySubNodes);

    // Sync property updates
    const auto dirtyFrontEndNodes = m_changeArbiter->takeDirtyFrontEndNodes();
    if (dirtyFrontEndNodes.size())
        for (QAbstractAspect *aspect : qAsConst(m_aspects))
           QAbstractAspectPrivate::get(aspect)->syncDirtyFrontEndNodes(dirtyFrontEndNodes);

    // TO DO: Having this done in the main thread actually means aspects could just
    // as simply read info out of the Frontend classes without risk of introducing
    // races. This could therefore be removed for Qt 6.
    m_changeArbiter->syncChanges();
#if QT_CONFIG(qt3d_profile_jobs)
    changeArbiterStats.endTime = QThreadPooler::m_jobsStatTimer.nsecsElapsed();
    QThreadPooler::addJobLogStatsEntry(changeArbiterStats);
#endif

    // For each Aspect
    // Ask them to launch set of jobs for the current frame
    // Updates matrices, bounding volumes, render bins ...
#if defined(QT3D_CORE_JOB_TIMING)
    QElapsedTimer timer;
    timer.start();
#endif
    m_scheduler->scheduleAndWaitForFrameAspectJobs(t);
#if defined(QT3D_CORE_JOB_TIMING)
    qDebug() << "Jobs took" << timer.nsecsElapsed() / 1.0e6;
#endif

    // TODO sync backend changes to frontend
}

} // namespace Qt3DCore

QT_END_NAMESPACE