| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 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 | #ifndef QT3DCORE_QCHANGEARBITER_P_H |
| 41 | #define QT3DCORE_QCHANGEARBITER_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists for the convenience |
| 48 | // of other Qt classes. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <Qt3DCore/qnodeid.h> |
| 55 | #include <Qt3DCore/qscenechange.h> |
| 56 | #include <QtCore/QFlags> |
| 57 | #include <QtCore/QMutex> |
| 58 | #include <QtCore/QObject> |
| 59 | #include <QtCore/QPair> |
| 60 | #include <QtCore/QReadWriteLock> |
| 61 | #include <QtCore/QThreadStorage> |
| 62 | #include <QtCore/QVariant> |
| 63 | #include <QtCore/QVector> |
| 64 | |
| 65 | #include <Qt3DCore/private/qlockableobserverinterface_p.h> |
| 66 | #include <Qt3DCore/private/qt3dcore_global_p.h> |
| 67 | #include <Qt3DCore/private/qscenechange_p.h> |
| 68 | |
| 69 | QT_BEGIN_NAMESPACE |
| 70 | |
| 71 | namespace Qt3DCore { |
| 72 | |
| 73 | class QNode; |
| 74 | class QObservableInterface; |
| 75 | class QAbstractAspectJobManager; |
| 76 | class QSceneObserverInterface; |
| 77 | class QAbstractPostman; |
| 78 | class QScene; |
| 79 | |
| 80 | |
| 81 | class Q_3DCORE_PRIVATE_EXPORT QAbstractArbiter : public QLockableObserverInterface |
| 82 | { |
| 83 | public: |
| 84 | virtual QAbstractPostman *postman() const = 0; |
| 85 | virtual void addDirtyFrontEndNode(QNode *node) = 0; |
| 86 | virtual void removeDirtyFrontEndNode(QNode *node) = 0; |
| 87 | QT_WARNING_PUSH |
| 88 | QT_WARNING_DISABLE_DEPRECATED |
| 89 | virtual void addDirtyFrontEndNode(QNode *node, QNode *subNode, const char *property, ChangeFlag change) = 0; |
| 90 | QT_WARNING_POP |
| 91 | }; |
| 92 | |
| 93 | class Q_3DCORE_PRIVATE_EXPORT QChangeArbiter final |
| 94 | : public QObject |
| 95 | , public QAbstractArbiter |
| 96 | { |
| 97 | Q_OBJECT |
| 98 | public: |
| 99 | explicit QChangeArbiter(QObject *parent = nullptr); |
| 100 | ~QChangeArbiter(); |
| 101 | |
| 102 | void initialize(Qt3DCore::QAbstractAspectJobManager *jobManager); |
| 103 | |
| 104 | void syncChanges(); |
| 105 | |
| 106 | QT_WARNING_PUSH |
| 107 | QT_WARNING_DISABLE_DEPRECATED |
| 108 | void registerObserver(QObserverInterface *observer, |
| 109 | QNodeId nodeId, |
| 110 | ChangeFlags changeFlags = AllChanges); |
| 111 | QT_WARNING_POP |
| 112 | void unregisterObserver(QObserverInterface *observer, |
| 113 | QNodeId nodeId); |
| 114 | |
| 115 | void sceneChangeEvent(const QSceneChangePtr &e) override; // QLockableObserverInterface impl |
| 116 | void sceneChangeEventWithLock(const QSceneChangePtr &e) override; // QLockableObserverInterface impl |
| 117 | void sceneChangeEventWithLock(const QSceneChangeList &e) override; // QLockableObserverInterface impl |
| 118 | |
| 119 | void addDirtyFrontEndNode(QNode *node) override; |
| 120 | QT_WARNING_PUSH |
| 121 | QT_WARNING_DISABLE_DEPRECATED |
| 122 | void addDirtyFrontEndNode(QNode *node, QNode *subNode, const char *property, ChangeFlag change) override; |
| 123 | QT_WARNING_POP |
| 124 | void removeDirtyFrontEndNode(QNode *node) override; |
| 125 | QVector<QNode *> takeDirtyFrontEndNodes(); |
| 126 | QVector<NodeRelationshipChange> takeDirtyFrontEndSubNodes(); |
| 127 | |
| 128 | void setPostman(Qt3DCore::QAbstractPostman *postman); |
| 129 | void setScene(Qt3DCore::QScene *scene); |
| 130 | |
| 131 | QAbstractPostman *postman() const final; |
| 132 | QScene *scene() const; |
| 133 | |
| 134 | static void createUnmanagedThreadLocalChangeQueue(void *changeArbiter); |
| 135 | static void destroyUnmanagedThreadLocalChangeQueue(void *changeArbiter); |
| 136 | static void createThreadLocalChangeQueue(void *changeArbiter); |
| 137 | static void destroyThreadLocalChangeQueue(void *changeArbiter); |
| 138 | |
| 139 | Q_SIGNALS: |
| 140 | void receivedChange(); |
| 141 | void syncedChanges(); |
| 142 | |
| 143 | protected: |
| 144 | typedef std::vector<QSceneChangePtr> QChangeQueue; |
| 145 | typedef QPair<ChangeFlags, QObserverInterface *> QObserverPair; |
| 146 | typedef QVector<QObserverPair> QObserverList; |
| 147 | |
| 148 | void distributeQueueChanges(QChangeQueue *queue); |
| 149 | |
| 150 | QThreadStorage<QChangeQueue *> *tlsChangeQueue(); |
| 151 | void appendChangeQueue(QChangeQueue *queue); |
| 152 | void removeChangeQueue(QChangeQueue *queue); |
| 153 | void appendLockingChangeQueue(QChangeQueue *queue); |
| 154 | void removeLockingChangeQueue(QChangeQueue *queue); |
| 155 | |
| 156 | private: |
| 157 | mutable QRecursiveMutex m_mutex; |
| 158 | QAbstractAspectJobManager *m_jobManager; |
| 159 | |
| 160 | // The lists of observers indexed by observable (QNodeId). |
| 161 | // m_nodeObservations is for observables in the main thread's object tree |
| 162 | QHash<QNodeId, QObserverList> m_nodeObservations; |
| 163 | |
| 164 | // Each thread has a TLS ChangeQueue so we never need to lock whilst |
| 165 | // receiving a QSceneChange. |
| 166 | QThreadStorage<QChangeQueue *> m_tlsChangeQueue; |
| 167 | |
| 168 | // We store a list of the ChangeQueue's from each thread. This will only |
| 169 | // be accessed from the aspect thread during the syncChanges() phase. |
| 170 | QList<QChangeQueue *> m_changeQueues; |
| 171 | QList<QChangeQueue *> m_lockingChangeQueues; |
| 172 | QAbstractPostman *m_postman; |
| 173 | QScene *m_scene; |
| 174 | |
| 175 | QVector<QNode *> m_dirtyFrontEndNodes; |
| 176 | QVector<NodeRelationshipChange> m_dirtySubNodeChanges; |
| 177 | }; |
| 178 | |
| 179 | } // namespace Qt3DCore |
| 180 | |
| 181 | QT_END_NAMESPACE |
| 182 | |
| 183 | #endif // QT3DCORE_QCHANGEARBITER_P_H |
| 184 | |