| 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 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | namespace Qt3DCore { |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | bool isDependencyNull(const QWeakPointer<QAspectJob> &dep) |
| 19 | { |
| 20 | return dep.isNull(); |
| 21 | } |
| 22 | |
| 23 | } // anonymous |
| 24 | |
| 25 | QAspectJobPrivate::QAspectJobPrivate() |
| 26 | : m_jobName(QLatin1String("UnknowJob")) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | QAspectJobPrivate::~QAspectJobPrivate() = default; |
| 31 | |
| 32 | QAspectJobPrivate *QAspectJobPrivate::get(QAspectJob *job) |
| 33 | { |
| 34 | return job->d_func(); |
| 35 | } |
| 36 | |
| 37 | bool QAspectJobPrivate::isRequired() const |
| 38 | { |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void QAspectJobPrivate::postFrame(QAspectManager *aspectManager) |
| 43 | { |
| 44 | Q_UNUSED(aspectManager); |
| 45 | } |
| 46 | |
| 47 | QAspectJob::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 | */ |
| 67 | QAspectJob::QAspectJob(QAspectJobPrivate &dd) |
| 68 | : d_ptr(&dd) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | QAspectJob::~QAspectJob() |
| 73 | { |
| 74 | delete d_ptr; |
| 75 | } |
| 76 | |
| 77 | /*! |
| 78 | * Adds \a dependency to the aspect job. |
| 79 | */ |
| 80 | void 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 | */ |
| 94 | void 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 | */ |
| 113 | const 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 | */ |
| 124 | void 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 | */ |
| 138 | bool QAspectJob::isRequired() |
| 139 | { |
| 140 | Q_D(QAspectJob); |
| 141 | return d->isRequired(); |
| 142 | } |
| 143 | |
| 144 | } // namespace Qt3DCore |
| 145 | |
| 146 | QT_END_NAMESPACE |
| 147 |
