diff options
author | Mike Krus <mike.krus@kdab.com> | 2019-04-03 21:20:53 +0100 |
---|---|---|
committer | Mike Krus <mike.krus@kdab.com> | 2019-05-25 09:28:45 +0100 |
commit | 7dcfd013e60787827fa5e31a408de04d1ed6ec0d (patch) | |
tree | 02ff6d1ef34ca17d8446a21368df617532ba29ac /src/render/backend | |
parent | 861a73b5328334ebf076d3dd6b2ad6c17bdc0700 (diff) |
Introduce EntityVisitor
Used to traverse the scene graph without having to create numerous
(and mostly empty) QVector<Entity *> for children, which leads to
a lot of memory allocation.
Change-Id: I023b2314d75249d0e3009010fd36dcbe2088c7db
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'src/render/backend')
-rw-r--r-- | src/render/backend/entityvisitor.cpp | 113 | ||||
-rw-r--r-- | src/render/backend/entityvisitor_p.h | 95 | ||||
-rw-r--r-- | src/render/backend/render-backend.pri | 2 |
3 files changed, 210 insertions, 0 deletions
diff --git a/src/render/backend/entityvisitor.cpp b/src/render/backend/entityvisitor.cpp new file mode 100644 index 000000000..87dd353bb --- /dev/null +++ b/src/render/backend/entityvisitor.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2019 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 "entityvisitor_p.h" +#include <Qt3DRender/private/managers_p.h> +#include <Qt3DRender/private/nodemanagers_p.h> + +QT_USE_NAMESPACE +using namespace Qt3DRender::Render; + +EntityVisitor::EntityVisitor(NodeManagers *manager) + : m_manager(manager) + , m_pruneDisabled(false) +{ + +} + +EntityVisitor::~EntityVisitor() = default; + +/*! + * \internal + * + * Override in derived class to do work on the current entity + * + * Return value (Continue, Prune, Stop) will affect traversal + */ +EntityVisitor::Operation EntityVisitor::visit(Entity *entity) { + // return false to stop traversal + if (!entity) + return Stop; + return Continue; +} + +/*! + * \internal + * + * If true, disabled entities and all their children will be ignored + * during traversal + * + */ +bool EntityVisitor::pruneDisabled() const +{ + return m_pruneDisabled; +} + +void EntityVisitor::setPruneDisabled(bool pruneDisabled) +{ + m_pruneDisabled = pruneDisabled; +} + +/*! + * \internal + * + * Call on the root of the tree that should be traversed. + * Returns false if any visit resulted in Stop + */ +bool EntityVisitor::apply(Entity *root) { + if (!root) + return false; + if (m_pruneDisabled && !root->isEnabled()) + return true; + + const auto op = visit(root); + if (op == Stop) + return false; + if (op == Prune) + return true; + + const auto childrenHandles = root->childrenHandles(); + for (const HEntity &handle : childrenHandles) { + Entity *child = m_manager->renderNodesManager()->data(handle); + if (child != nullptr && !apply(child)) + return false; + } + + return true; +} diff --git a/src/render/backend/entityvisitor_p.h b/src/render/backend/entityvisitor_p.h new file mode 100644 index 000000000..2f4f1c813 --- /dev/null +++ b/src/render/backend/entityvisitor_p.h @@ -0,0 +1,95 @@ +/**************************************************************************** +** +** Copyright (C) 2019 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$ +** +****************************************************************************/ + +#ifndef QT3DRENDER_RENDER_ENTITYVISITOR_H +#define QT3DRENDER_RENDER_ENTITYVISITOR_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists for the convenience +// of other Qt classes. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <qglobal.h> + +QT_BEGIN_NAMESPACE + +namespace Qt3DRender { +namespace Render { + +class Entity; +class NodeManagers; + +class Q_AUTOTEST_EXPORT EntityVisitor +{ +public: + enum Operation { + Continue, //! continue traversal + Prune, //! don't traverse children + Stop //! abort traversal + }; + + EntityVisitor(NodeManagers *manager); + virtual ~EntityVisitor(); + + virtual Operation visit(Entity *entity = nullptr); + + bool pruneDisabled() const; + void setPruneDisabled(bool pruneDisabled); + + bool apply(Entity *root); + +protected: + NodeManagers *m_manager; + +private: + bool m_pruneDisabled; +}; + +} // namespace Render +} // namespace Qt3DRender + +QT_END_NAMESPACE + +#endif // QT3DRENDER_RENDER_ENTITYVISITOR_H diff --git a/src/render/backend/render-backend.pri b/src/render/backend/render-backend.pri index 352de3be5..10dc1b759 100644 --- a/src/render/backend/render-backend.pri +++ b/src/render/backend/render-backend.pri @@ -11,6 +11,7 @@ HEADERS += \ $$PWD/platformsurfacefilter_p.h \ $$PWD/cameralens_p.h \ $$PWD/entity_p.h \ + $$PWD/entityvisitor_p.h \ $$PWD/layer_p.h \ $$PWD/levelofdetail_p.h \ $$PWD/nodefunctor_p.h \ @@ -44,6 +45,7 @@ SOURCES += \ $$PWD/platformsurfacefilter.cpp \ $$PWD/cameralens.cpp \ $$PWD/entity.cpp \ + $$PWD/entityvisitor.cpp \ $$PWD/layer.cpp \ $$PWD/levelofdetail.cpp \ $$PWD/transform.cpp \ |