1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qaspectjob.h"
5#include "qaspectjob_p.h"
6
7#include <Qt3DCore/qaspectengine.h>
8#include <Qt3DCore/private/qaspectengine_p.h>
9
10#include <QtCore/QByteArray>
11
12QT_BEGIN_NAMESPACE
13
14namespace Qt3DCore {
15
16namespace {
17
18bool isDependencyNull(const QWeakPointer<QAspectJob> &dep)
19{
20 return dep.isNull();
21}
22
23} // anonymous
24
25QAspectJobPrivate::QAspectJobPrivate()
26 : m_jobName(QLatin1String("UnknowJob"))
27{
28}
29
30QAspectJobPrivate::~QAspectJobPrivate() = default;
31
32QAspectJobPrivate *QAspectJobPrivate::get(QAspectJob *job)
33{
34 return job->d_func();
35}
36
37bool QAspectJobPrivate::isRequired() const
38{
39 return true;
40}
41
42void QAspectJobPrivate::postFrame(QAspectManager *aspectManager)
43{
44 Q_UNUSED(aspectManager);
45}
46
47QAspectJob::QAspectJob()
48 : d_ptr(new QAspectJobPrivate)
49{
50}
51
52/*!
53 * \class Qt3DCore::QAspectJob
54 * \inheaderfile Qt3DCore/QAspectJob
55 * \inmodule Qt3DCore
56 * \brief The base class for jobs executed in an aspect.
57 */
58
59/*!
60 * \fn void Qt3DCore::QAspectJob::run()
61 * Executes the job. This is called on a separate thread by the scheduler.
62 */
63
64/*!
65 * \internal
66 */
67QAspectJob::QAspectJob(QAspectJobPrivate &dd)
68 : d_ptr(&dd)
69{
70}
71
72QAspectJob::~QAspectJob()
73{
74 delete d_ptr;
75}
76
77/*!
78 * Adds \a dependency to the aspect job.
79 */
80void QAspectJob::addDependency(QWeakPointer<QAspectJob> dependency)
81{
82 Q_D(QAspectJob);
83 d->m_dependencies.push_back(x: dependency);
84#ifdef QT3DCORE_ASPECT_JOB_DEBUG
85 static int threshold = qMax(1, qgetenv("QT3DCORE_ASPECT_JOB_DEPENDENCY_THRESHOLD").toInt());
86 if (d->m_dependencies.count() > threshold)
87 qWarning() << "Suspicious number of job dependencies found";
88#endif
89}
90
91/*!
92 * Removes the given \a dependency from aspect job.
93 */
94void QAspectJob::removeDependency(QWeakPointer<QAspectJob> dependency)
95{
96 Q_D(QAspectJob);
97 if (!dependency.isNull()) {
98 d->m_dependencies.erase(first: std::remove(first: d->m_dependencies.begin(),
99 last: d->m_dependencies.end(),
100 value: dependency),
101 last: d->m_dependencies.end());
102 } else {
103 d->m_dependencies.erase(first: std::remove_if(first: d->m_dependencies.begin(),
104 last: d->m_dependencies.end(),
105 pred: isDependencyNull),
106 last: d->m_dependencies.end());
107 }
108}
109
110/*!
111 * \return the dependencies of the aspect job.
112 */
113const std::vector<QWeakPointer<QAspectJob> > &QAspectJob::dependencies() const
114{
115 Q_D(const QAspectJob);
116 return d->m_dependencies;
117}
118
119/*!
120 * Called in the main thread when all the jobs have completed.
121 * This is a good point to push changes back to the frontend.
122 * \a aspectEngine is the engine responsible for the run loop.
123 */
124void QAspectJob::postFrame(QAspectEngine *aspectEngine)
125{
126 Q_D(QAspectJob);
127 if (aspectEngine) {
128 auto manager = QAspectEnginePrivate::get(engine: aspectEngine)->m_aspectManager;
129 d->postFrame(aspectManager: manager);
130 }
131}
132
133/*!
134 * Should return true (default) if the job has actually something to do.
135 * If returning false, the job will not be scheduled (but it's dependencies
136 * will be).
137 */
138bool QAspectJob::isRequired()
139{
140 Q_D(QAspectJob);
141 return d->isRequired();
142}
143
144} // namespace Qt3DCore
145
146QT_END_NAMESPACE
147

source code of qt3d/src/core/jobs/qaspectjob.cpp