From e9e3208dd7c2e2f755948b1838faa94c2802cc30 Mon Sep 17 00:00:00 2001 From: Kevin Ottens Date: Wed, 6 Sep 2017 15:58:12 +0200 Subject: GraphicsHelperGL2: Remember to set raw byte size If one ended up with this helper then the applyUniform would lead to a divide by zero for any type. As a side-effect, the support for more matrix types needed to be done for unit tests to keep passing. Change-Id: I66c8a2eb7e5617f2fed96c689cb4ebc024ef9853 Reviewed-by: Sean Harmer --- tests/auto/render/graphicshelpergl2/tst_graphicshelpergl2.cpp | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/auto/render/graphicshelpergl2/tst_graphicshelpergl2.cpp b/tests/auto/render/graphicshelpergl2/tst_graphicshelpergl2.cpp index 5a6f87e23..afd56e9bd 100644 --- a/tests/auto/render/graphicshelpergl2/tst_graphicshelpergl2.cpp +++ b/tests/auto/render/graphicshelpergl2/tst_graphicshelpergl2.cpp @@ -922,7 +922,13 @@ private Q_SLOTS: ADD_UNIFORM_ENTRY(fragCodeUniformsInt, "multiplierVec4", GL_INT_VEC4, 1, 4 * 4); ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m2", GL_FLOAT_MAT2, 1, 4 * 2 * 2); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m23", GL_FLOAT_MAT2x3, 1, 4 * 2 * 3); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m32", GL_FLOAT_MAT3x2, 1, 4 * 3 * 2); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m24", GL_FLOAT_MAT2x4, 1, 4 * 2 * 4); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m42", GL_FLOAT_MAT4x2, 1, 4 * 4 * 2); ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m3", GL_FLOAT_MAT3, 1, 4 * 3 * 3); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m34", GL_FLOAT_MAT3x4, 1, 4 * 3 * 4); + ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m43", GL_FLOAT_MAT4x3, 1, 4 * 4 * 3); ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m4", GL_FLOAT_MAT4, 1, 4 * 4 * 4); ADD_UNIFORM_ENTRY(fragCodeSamplers, "s1", GL_SAMPLER_1D, 1, 4); -- cgit v1.2.3 From 64d018ea11e00174cedabda9230a93054b40d79b Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 6 Sep 2017 13:39:48 +0200 Subject: Remove ArrayPreallocationPolicy It's not used anywhere Change-Id: Id65ec9fc8e38697c0dbd23fe4f816e1637a22483 Reviewed-by: Sean Harmer --- .../arrayresourcesmanager.pro | 3 +- .../preallocatedarraypolicy.pro | 7 - .../tst_preallocatedarraypolicy.cpp | 426 --------------------- .../arraypolicy/tst_bench_arraypolicy.cpp | 48 +-- 4 files changed, 12 insertions(+), 472 deletions(-) delete mode 100644 tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro delete mode 100644 tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp (limited to 'tests') diff --git a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro b/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro index cc929575f..a23a04f3c 100644 --- a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro +++ b/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro @@ -1,5 +1,4 @@ TEMPLATE = subdirs SUBDIRS = \ - dynamicarraypolicy \ - preallocatedarraypolicy + dynamicarraypolicy diff --git a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro deleted file mode 100644 index 636c155b2..000000000 --- a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/preallocatedarraypolicy.pro +++ /dev/null @@ -1,7 +0,0 @@ -TARGET = tst_preallocatedarraypolicy -CONFIG += testcase -TEMPLATE = app - -SOURCES += tst_preallocatedarraypolicy.cpp - -QT += testlib 3dcore 3dcore-private diff --git a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp b/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp deleted file mode 100644 index dbed2abfb..000000000 --- a/tests/auto/core/arrayresourcesmanager/preallocatedarraypolicy/tst_preallocatedarraypolicy.cpp +++ /dev/null @@ -1,426 +0,0 @@ -/**************************************************************************** -** -** 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:GPL-EXCEPT$ -** 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 General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** 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-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include -#include - -class tst_PreallocatedArrayPolicy : public QObject -{ - Q_OBJECT -public: - tst_PreallocatedArrayPolicy() {} - ~tst_PreallocatedArrayPolicy() {} - -private slots: - void createResourcesManager(); - void acquireResources(); - void getResources(); - void registerResourcesResize(); - void removeResource(); - void resetResource(); - void lookupResource(); - void releaseResource(); - void heavyDutyMultiThreadedAccess(); - void heavyDutyMultiThreadedAccessRelease(); - void maximumNumberOfResources(); -}; - -class tst_ArrayResource -{ -public: - tst_ArrayResource() : m_value(0) - {} - - QAtomicInt m_value; -}; - -typedef Qt3DCore::QHandle tHandle; -typedef Qt3DCore::QHandle tHandle4; -typedef Qt3DCore::QHandle tHandle8; -typedef Qt3DCore::QHandle tHandle16; - -void tst_PreallocatedArrayPolicy::createResourcesManager() -{ - Qt3DCore::QResourceManager manager16; - Qt3DCore::QResourceManager manager4; - Qt3DCore::QResourceManager manager8; - QVERIFY(manager16.maximumSize() == 65535); - QVERIFY(manager8.maximumSize() == 255); - QVERIFY(manager4.maximumSize() == 15); -} - -/*! - * Check that the handles returned when a registering resources - * have a correct index and counter. - */ -void tst_PreallocatedArrayPolicy::acquireResources() -{ - Qt3DCore::QResourceManager manager; - - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - } -} - -/*! - * Test that values can be properly retrieved. - */ -void tst_PreallocatedArrayPolicy::getResources() -{ - - Qt3DCore::QResourceManager manager; - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - resources << manager.data(handles.at(i)); - QVERIFY(resources.at(i) != nullptr); - resources.at(i)->m_value = i; - } - - for (int i = 0; i < 5; i++) - QVERIFY(manager.data(handles.at(i))->m_value == i); - - // Check that an invalid resource returns NULL - tHandle8 iHandle; - QVERIFY(manager.data(iHandle) == nullptr); - -} - -/*! - * Test that when a resize of the data vectors in the manager occurs, - * everything behaves correctly. - */ -void tst_PreallocatedArrayPolicy::registerResourcesResize() -{ - Qt3DCore::QResourceManager manager; - QList handles; - - for (uint i = 0; i < 2; i++) { - handles << manager.acquire(); - manager.data(handles.at(i))->m_value = i + 2; - } - - for (uint i = 0; i < 5; i++) { - handles << manager.acquire(); - manager.data(handles.at(i + 2))->m_value = i + 2 + 5; - } - - for (int i = 0; i < 7; i++) { - QVERIFY(handles.at(i).index() == static_cast(i)); - QVERIFY(handles.at(i).counter() == 1); - if (i < 2) - QVERIFY(manager.data(handles.at(i))->m_value == i + 2); - else - QVERIFY(manager.data(handles.at(i))->m_value == i + 5); - } -} - -/*! - * Checks for the removal of resources. - */ -void tst_PreallocatedArrayPolicy::removeResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 32; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - } - - manager.release(handles.at(2)); - QVERIFY(manager.data(handles.at(2)) == nullptr); - // Triggers QASSERT so commented - // manager.release(handles.at(2)); - - tHandle nHandle = manager.acquire(); - QVERIFY(manager.data(nHandle) != nullptr); -} - -/*! - * Checks that reset behaves correctly. - */ -void tst_PreallocatedArrayPolicy::resetResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - manager.reset(); - for (uint i = 0; i < 5; i++) { - QVERIFY(manager.data(handles.at(i)) == nullptr); - } - handles.clear(); - for (uint i = 0; i < 5; i++) - handles << manager.acquire(); - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - QVERIFY(manager.data(handles.at(i))->m_value != 4); - } -} - -void tst_PreallocatedArrayPolicy::lookupResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - - tHandle16 t = manager.lookupHandle(2); - QVERIFY(t.handle() == 0); - QVERIFY(manager.data(t) == nullptr); - tst_ArrayResource *resource = manager.getOrCreateResource(2); - QVERIFY(resource != nullptr); - t = manager.lookupHandle(2); - QVERIFY(manager.data(t) == manager.lookupResource(2)); - QVERIFY(t == manager.getOrAcquireHandle(2)); - QVERIFY(resource == manager.getOrCreateResource(2)); - QVERIFY(manager.data(t) == resource); -} - -void tst_PreallocatedArrayPolicy::releaseResource() -{ - Qt3DCore::QResourceManager manager; - QList resources; - - for (int i = 0; i < 5; i++) { - resources << manager.getOrCreateResource(i); - } - - for (int i = 0; i < 5; i++) { - QVERIFY(resources.at(i) == manager.lookupResource(i)); - } - - for (int i = 0; i < 5; i++) { - manager.releaseResource(i); - QVERIFY(manager.lookupResource(i) == nullptr); - } -} - -class tst_Thread : public QThread -{ - Q_OBJECT -public: - - typedef Qt3DCore::QResourceManager Manager; - - tst_Thread() - : QThread() - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - i++; - QVERIFY(r != nullptr); - Manager::Locker lock(m_manager); - r->m_value++; - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; -}; - -void tst_PreallocatedArrayPolicy::heavyDutyMultiThreadedAccess() -{ - tst_Thread::Manager *manager = new tst_Thread::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int i = 0; i < iterations; i++) { - tst_Thread *thread = new tst_Thread(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) != nullptr); - QVERIFY(manager->lookupResource(i)->m_value = iterations); - } - - qDeleteAll(threads); - delete manager; -} - -class tst_Thread2 : public QThread -{ - Q_OBJECT -public: - - typedef Qt3DCore::QResourceManager Manager; - - tst_Thread2(int releaseAbove = 7) - : QThread() - , m_releaseAbove(releaseAbove) - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - QVERIFY(r != nullptr); - int oldValue = r->m_value.fetchAndAddOrdered(+1); - if (oldValue == m_releaseAbove) - m_manager->releaseResource(i); - i++; - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; - int m_releaseAbove; -}; - -void tst_PreallocatedArrayPolicy::heavyDutyMultiThreadedAccessRelease() -{ - tst_Thread2::Manager *manager = new tst_Thread2::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int u = 0; u < 2; u++) { - - for (int i = 0; i < iterations; i++) { - tst_Thread2 *thread = new tst_Thread2(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) == nullptr); - } - - qDeleteAll(threads); - threads.clear(); - } - - delete manager; -} - -void tst_PreallocatedArrayPolicy::maximumNumberOfResources() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - QCOMPARE(tHandle16::maxIndex(), (uint)manager.maximumSize()); - - for (int i = 0; i < manager.maximumSize(); i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } -} - -QTEST_APPLESS_MAIN(tst_PreallocatedArrayPolicy) - -#include "tst_preallocatedarraypolicy.moc" diff --git a/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp b/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp index 0d89bd8c4..0b4587ae8 100644 --- a/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp +++ b/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp @@ -38,11 +38,6 @@ private Q_SLOTS: void benchmarkDynamicReleaseSmallResources(); void benchmarkDynamicAllocateBigResources(); void benchmarkDynamicReleaseBigResources(); - - void benchmarkPreallocatedAllocateSmallResources(); - void benchmarkPreallocatedReleaseSmallResources(); - void benchmarkPreallocatedAllocateBigResources(); - void benchmarkPreallocatedReleaseBigResources(); }; struct SmallType @@ -55,33 +50,32 @@ struct BigType QMatrix4x4 a; }; -template +template void benchmarkAllocateResources() { - C allocator; + Qt3DCore::ArrayAllocatingPolicy allocator; const int max = (1 << 16) - 1; QBENCHMARK_ONCE { for (int i = 0; i < max; i++) { - T* ptr = allocator.allocateResource(); - Q_UNUSED(ptr); + (void) allocator.allocateResource(); } } } -template +template void benchmarkReleaseResources() { - C allocator; + Qt3DCore::ArrayAllocatingPolicy allocator; const int max = (1 << 16) - 1; - QVector resources(max); + QVector resources(max); for (int i = 0; i < max; i++) { resources[i] = allocator.allocateResource(); } QBENCHMARK_ONCE { - foreach (T* ptr, resources) { + foreach (auto ptr, resources) { allocator.releaseResource(ptr); } } @@ -89,42 +83,22 @@ void benchmarkReleaseResources() void tst_ArrayPolicy::benchmarkDynamicAllocateSmallResources() { - benchmarkAllocateResources, SmallType>(); + benchmarkAllocateResources(); } void tst_ArrayPolicy::benchmarkDynamicReleaseSmallResources() { - benchmarkReleaseResources, SmallType>(); + benchmarkReleaseResources(); } void tst_ArrayPolicy::benchmarkDynamicAllocateBigResources() { - benchmarkAllocateResources, BigType>(); + benchmarkAllocateResources(); } void tst_ArrayPolicy::benchmarkDynamicReleaseBigResources() { - benchmarkReleaseResources, BigType>(); -} - -void tst_ArrayPolicy::benchmarkPreallocatedAllocateSmallResources() -{ - benchmarkAllocateResources, SmallType>(); -} - -void tst_ArrayPolicy::benchmarkPreallocatedReleaseSmallResources() -{ - benchmarkReleaseResources, SmallType>(); -} - -void tst_ArrayPolicy::benchmarkPreallocatedAllocateBigResources() -{ - benchmarkAllocateResources, BigType>(); -} - -void tst_ArrayPolicy::benchmarkPreallocatedReleaseBigResources() -{ - benchmarkReleaseResources, BigType>(); + benchmarkReleaseResources(); } QTEST_APPLESS_MAIN(tst_ArrayPolicy) -- cgit v1.2.3 From 837a56fa10b26f129cb0a70caaff9f84e00a3acb Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Tue, 5 Sep 2017 14:21:09 +0200 Subject: Get rid of the HandleManager class Instead, direclty use the Allocator to acquire and release handles without an additional indirection. This removes around 30% of the overhead in run(). Change-Id: Ic4a9343dd52a900eb1c7eb6b4135bc7670076df1 Reviewed-by: Sean Harmer --- .../dynamicarraypolicy/tst_dynamicarraypolicy.cpp | 31 --- tests/auto/core/core.pro | 1 - tests/auto/core/handlemanager/handlemanager.pro | 7 - .../auto/core/handlemanager/tst_handlemanager.cpp | 306 --------------------- .../arraypolicy/tst_bench_arraypolicy.cpp | 2 +- .../tst_bench_qresourcesmanager.cpp | 2 +- 6 files changed, 2 insertions(+), 347 deletions(-) delete mode 100644 tests/auto/core/handlemanager/handlemanager.pro delete mode 100644 tests/auto/core/handlemanager/tst_handlemanager.cpp (limited to 'tests') diff --git a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp index 874ac42ca..5c193ec90 100644 --- a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp +++ b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp @@ -44,7 +44,6 @@ private slots: void getResources(); void registerResourcesResize(); void removeResource(); - void resetResource(); void lookupResource(); void releaseResource(); void heavyDutyMultiThreadedAccess(); @@ -187,36 +186,6 @@ void tst_DynamicArrayPolicy::removeResource() QVERIFY(manager.data(nHandle) != nullptr); } -/*! - * Checks that reset behaves correctly. - */ -void tst_DynamicArrayPolicy::resetResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - manager.reset(); - for (uint i = 0; i < 5; i++) { - QVERIFY(manager.data(handles.at(i)) == nullptr); - } - handles.clear(); - for (uint i = 0; i < 5; i++) - handles << manager.acquire(); - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - QVERIFY(manager.data(handles.at(i))->m_value != 4); - } -} - void tst_DynamicArrayPolicy::lookupResource() { Qt3DCore::QResourceManager manager; diff --git a/tests/auto/core/core.pro b/tests/auto/core/core.pro index 637ee086e..088940b7d 100644 --- a/tests/auto/core/core.pro +++ b/tests/auto/core/core.pro @@ -2,7 +2,6 @@ TEMPLATE = subdirs SUBDIRS = \ handle \ - handlemanager \ arrayresourcesmanager \ qcircularbuffer \ qboundedcircularbuffer \ diff --git a/tests/auto/core/handlemanager/handlemanager.pro b/tests/auto/core/handlemanager/handlemanager.pro deleted file mode 100644 index ad18d5b9b..000000000 --- a/tests/auto/core/handlemanager/handlemanager.pro +++ /dev/null @@ -1,7 +0,0 @@ -TARGET = tst_handlemanager -CONFIG += testcase -TEMPLATE = app - -SOURCES += tst_handlemanager.cpp - -QT += testlib 3dcore 3dcore-private diff --git a/tests/auto/core/handlemanager/tst_handlemanager.cpp b/tests/auto/core/handlemanager/tst_handlemanager.cpp deleted file mode 100644 index f4879a3df..000000000 --- a/tests/auto/core/handlemanager/tst_handlemanager.cpp +++ /dev/null @@ -1,306 +0,0 @@ -/**************************************************************************** -** -** 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:GPL-EXCEPT$ -** 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 General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** 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-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include - -class tst_HandleManager : public QObject -{ - Q_OBJECT -public: - tst_HandleManager() {} - ~tst_HandleManager() {} - -private slots: - void construction(); - void correctPointer(); - void correctPointers(); - void correctConstPointer(); - void nullForRemovedEntry(); - void validHandleForReplacementEntry(); - void updateChangesValue(); - void resetRemovesAllEntries(); - void maximumEntries(); - void checkNoCounterOverflow(); -}; - -class SimpleResource -{ -public: - SimpleResource() - : m_value(0) - {} - - int m_value; -}; - -typedef Qt3DCore::QHandle Handle; - -void tst_HandleManager::construction() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - - // THEN - QVERIFY(manager.activeEntries() == 0); -} - -void tst_HandleManager::correctPointer() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - SimpleResource *p1 = (SimpleResource *)(quintptr)0xdeadbeef; - - // WHEN - const Handle h = manager.acquire(p1); - - bool ok = false; - SimpleResource *p2 = manager.data(h, &ok); - - // THEN - QVERIFY(ok == true); - QVERIFY(p1 == p2); -} - -void tst_HandleManager::correctPointers() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - SimpleResource *p[3]; - p[0] = (SimpleResource *)(quintptr)0xdeadbeef; - p[1] = (SimpleResource *)(quintptr)0x11111111; - p[2] = (SimpleResource *)(quintptr)0x22222222; - - // WHEN - for (int i = 0; i < 3; ++i) { - // WHEN - const Handle h = manager.acquire(p[i]); - - bool ok = false; - SimpleResource *q = manager.data(h, &ok); - - // THEN - QVERIFY(ok == true); - QVERIFY(p[i] == q); - } - - // THEN - QVERIFY(manager.activeEntries() == 3); -} - -void tst_HandleManager::correctConstPointer() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - QSharedPointer p1(new SimpleResource); - const Handle h = manager.acquire(p1.data()); - - // WHEN - bool ok = false; - const SimpleResource *p2 = manager.constData(h, &ok); - - // THEN - QVERIFY(ok == true); - QVERIFY(p1.data() == p2); -} - -void tst_HandleManager::nullForRemovedEntry() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - QSharedPointer p1(new SimpleResource); - const Handle h = manager.acquire(p1.data()); - - // WHEN - manager.release(h); - - // THEN - bool ok = false; - SimpleResource *p2 = manager.data(h, &ok); - QVERIFY(ok == false); - QVERIFY(p2 == nullptr); -} - -void tst_HandleManager::validHandleForReplacementEntry() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - QSharedPointer p1(new SimpleResource); - const Handle h = manager.acquire(p1.data()); - - // THEN - QVERIFY(manager.activeEntries() == 1); - - // WHEN - manager.release(h); - - // THEN - QVERIFY(manager.activeEntries() == 0); - - // WHEN - QSharedPointer p2(new SimpleResource); - const Handle h2 = manager.acquire(p2.data()); - - // THEN - QVERIFY(h2.isNull() == false); - QVERIFY(h2.counter() == 2); - QVERIFY(manager.activeEntries() == 1); - - // WHEN - bool ok = false; - SimpleResource *p3 = manager.data(h2, &ok); - - // THEN - QVERIFY(ok == true); - QVERIFY(p3 == p2); -} - -void tst_HandleManager::updateChangesValue() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - QSharedPointer p1(new SimpleResource); - const Handle h = manager.acquire(p1.data()); - - // WHEN - QSharedPointer p2(new SimpleResource); - manager.update(h, p2.data()); - - // THEN - QVERIFY(manager.activeEntries() == 1); - - // WHEN - bool ok = false; - SimpleResource *p3 = manager.data(h, &ok); - - // THEN - QVERIFY(ok == true); - QVERIFY(p3 == p2); -} - -void tst_HandleManager::resetRemovesAllEntries() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - - // WHEN - for (int i = 0; i < 100; ++i) { - SimpleResource *p = (SimpleResource *)(quintptr)(0xdead0000 + i); - const Handle h = manager.acquire(p); - - bool ok = false; - SimpleResource *q = manager.data(h, &ok); - QVERIFY(ok == true); - QVERIFY(p == q); - } - - // THEN - QVERIFY(manager.activeEntries() == 100); - - // WHEN - manager.reset(); - - // THEN - QVERIFY(manager.activeEntries() == 0); -} - -void tst_HandleManager::maximumEntries() -{ - // GIVEN - Qt3DCore::QHandleManager manager; - - // THEN - QCOMPARE(Handle::maxIndex(), (uint)((1 << 16) - 1)); - - // WHEN - for (int i = 0; i < (int)Handle::maxIndex(); ++i) { - SimpleResource *p = (SimpleResource *)(quintptr)(0xdead0000 + i); - const Handle h = manager.acquire(p); - - bool ok = false; - SimpleResource *q = manager.data(h, &ok); - QVERIFY(ok == true); - QVERIFY(p == q); - } - - // THEN - QVERIFY(manager.activeEntries() == Handle::maxIndex());\ - - // WHEN - manager.reset(); - - // THEN - QVERIFY(manager.activeEntries() == 0); -} - -void tst_HandleManager::checkNoCounterOverflow() -{ - // GIVEN - const int indexBits = 16; - Qt3DCore::QHandleManager manager; - SimpleResource *p = (SimpleResource *)(quintptr)0xdead0000; - Qt3DCore::QHandle h = manager.acquire(p); - - // THEN - QCOMPARE(h.maxCounter(), (quint32)((1 << (32 - indexBits - 2)) - 1)); - QCOMPARE(h.counter(), (quint32)1); - - // WHEN - const quint32 maxIterations = h.maxCounter() - 2; - const quint32 handleIndex = h.index(); - - qDebug() << maxIterations << handleIndex; - - // Acquire and release maxIteration time to increase counter - for (quint32 i = 0; i < maxIterations; ++i) { - // WHEN - manager.release(h); - h = manager.acquire(p); - - // THEN - QCOMPARE(h.index(), handleIndex); - QCOMPARE(h.counter(), i + 2); - } - - // THEN - QCOMPARE(h.counter(), h.maxCounter() - 1); - - // WHEN - manager.release(h); - h = manager.acquire(p); - - // THEN - QCOMPARE(h.counter(), (quint32)1); -} - - - -QTEST_APPLESS_MAIN(tst_HandleManager) - -#include "tst_handlemanager.moc" diff --git a/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp b/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp index 0b4587ae8..0813fff91 100644 --- a/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp +++ b/tests/benchmarks/core/qresourcesmanager/arraypolicy/tst_bench_arraypolicy.cpp @@ -69,7 +69,7 @@ void benchmarkReleaseResources() Qt3DCore::ArrayAllocatingPolicy allocator; const int max = (1 << 16) - 1; - QVector resources(max); + QVector> resources(max); for (int i = 0; i < max; i++) { resources[i] = allocator.allocateResource(); } diff --git a/tests/benchmarks/core/qresourcesmanager/qresourcesmanager/tst_bench_qresourcesmanager.cpp b/tests/benchmarks/core/qresourcesmanager/qresourcesmanager/tst_bench_qresourcesmanager.cpp index 48b12b5f2..6ddb058a1 100644 --- a/tests/benchmarks/core/qresourcesmanager/qresourcesmanager/tst_bench_qresourcesmanager.cpp +++ b/tests/benchmarks/core/qresourcesmanager/qresourcesmanager/tst_bench_qresourcesmanager.cpp @@ -161,7 +161,7 @@ void benchmarkReleaseResources() handles[i] = manager.acquire(); QBENCHMARK_ONCE { - manager.reset(); + /*manager.reset()*/; } } -- cgit v1.2.3 From cd467d5ffc6eaaa7da089ab4acc5284cd11d1109 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 6 Sep 2017 13:17:54 +0200 Subject: Get rid of the AllocationPolicy template argument in the ResourceManager We always use the same policy anyway. Change-Id: I3349b8c19ce0e6239b140dfac7ba66f8dda1b9be Reviewed-by: Sean Harmer --- .../arrayresourcesmanager.pro | 4 - .../dynamicarraypolicy/dynamicarraypolicy.pro | 7 - .../dynamicarraypolicy/tst_dynamicarraypolicy.cpp | 438 --------------------- tests/auto/core/core.pro | 2 +- .../core/qresourcemanager/qresourcemanager.pro | 7 + .../core/qresourcemanager/tst_qresourcemanager.cpp | 436 ++++++++++++++++++++ 6 files changed, 444 insertions(+), 450 deletions(-) delete mode 100644 tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro delete mode 100644 tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro delete mode 100644 tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp create mode 100644 tests/auto/core/qresourcemanager/qresourcemanager.pro create mode 100644 tests/auto/core/qresourcemanager/tst_qresourcemanager.cpp (limited to 'tests') diff --git a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro b/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro deleted file mode 100644 index a23a04f3c..000000000 --- a/tests/auto/core/arrayresourcesmanager/arrayresourcesmanager.pro +++ /dev/null @@ -1,4 +0,0 @@ -TEMPLATE = subdirs - -SUBDIRS = \ - dynamicarraypolicy diff --git a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro deleted file mode 100644 index ee2a73e03..000000000 --- a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/dynamicarraypolicy.pro +++ /dev/null @@ -1,7 +0,0 @@ -TARGET = tst_dynamicarraypolicy -CONFIG += testcase -TEMPLATE = app - -SOURCES += tst_dynamicarraypolicy.cpp - -QT += testlib 3dcore 3dcore-private diff --git a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp b/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp deleted file mode 100644 index 5c193ec90..000000000 --- a/tests/auto/core/arrayresourcesmanager/dynamicarraypolicy/tst_dynamicarraypolicy.cpp +++ /dev/null @@ -1,438 +0,0 @@ -/**************************************************************************** -** -** 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:GPL-EXCEPT$ -** 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 General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** 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-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include -#include - -class tst_DynamicArrayPolicy : public QObject -{ - Q_OBJECT -public: - tst_DynamicArrayPolicy() {} - ~tst_DynamicArrayPolicy() {} - -private slots: - void createResourcesManager(); - void acquireResources(); - void getResources(); - void registerResourcesResize(); - void removeResource(); - void lookupResource(); - void releaseResource(); - void heavyDutyMultiThreadedAccess(); - void heavyDutyMultiThreadedAccessRelease(); - void maximumNumberOfResources(); - void activeHandles(); -}; - -class tst_ArrayResource -{ -public: - tst_ArrayResource() : m_value(0) - {} - - void cleanup() { m_value = 0; } - - QAtomicInt m_value; -}; - -QT_BEGIN_NAMESPACE -Q_DECLARE_RESOURCE_INFO(tst_ArrayResource, Q_REQUIRES_CLEANUP) -QT_END_NAMESPACE - -typedef Qt3DCore::QHandle tHandle; -typedef Qt3DCore::QHandle tHandle4; -typedef Qt3DCore::QHandle tHandle8; -typedef Qt3DCore::QHandle tHandle16; - -void tst_DynamicArrayPolicy::createResourcesManager() -{ - Qt3DCore::QResourceManager manager16; - Qt3DCore::QResourceManager manager4; - Qt3DCore::QResourceManager manager8; - QVERIFY(manager16.maximumSize() == 65535); - QVERIFY(manager8.maximumSize() == 255); - QVERIFY(manager4.maximumSize() == 15); -} - -/*! - * Check that the handles returned when a registering resources - * have a correct index and counter. - */ -void tst_DynamicArrayPolicy::acquireResources() -{ - Qt3DCore::QResourceManager manager; - - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - } -} - -/*! - * Test that values can be properly retrieved. - */ -void tst_DynamicArrayPolicy::getResources() -{ - - Qt3DCore::QResourceManager manager; - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - } - - for (uint i = 0; i < 5; i++) { - QVERIFY(handles.at(i).index() == i); - QVERIFY(handles.at(i).counter() == 1); - resources << manager.data(handles.at(i)); - QVERIFY(resources.at(i) != nullptr); - resources.at(i)->m_value = i; - } - - for (int i = 0; i < 5; i++) - QVERIFY(manager.data(handles.at(i))->m_value == i); - - // Check that an invalid resource returns NULL - tHandle8 iHandle; - QVERIFY(manager.data(iHandle) == nullptr); - -} - -/*! - * Test that when a resize of the data vectors in the manager occurs, - * everything behaves correctly. - */ -void tst_DynamicArrayPolicy::registerResourcesResize() -{ - Qt3DCore::QResourceManager manager; - QList handles; - - for (uint i = 0; i < 2; i++) { - handles << manager.acquire(); - manager.data(handles.at(i))->m_value = i + 2; - } - - for (uint i = 0; i < 5; i++) { - handles << manager.acquire(); - manager.data(handles.at(i + 2))->m_value = i + 2 + 5; - } - - for (int i = 0; i < 7; i++) { - QVERIFY(handles.at(i).index() == static_cast(i)); - QVERIFY(handles.at(i).counter() == 1); - if (i < 2) - QVERIFY(manager.data(handles.at(i))->m_value == i + 2); - else - QVERIFY(manager.data(handles.at(i))->m_value == i + 5); - } -} - -/*! - * Checks for the removal of resources. - */ -void tst_DynamicArrayPolicy::removeResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 32; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - } - - manager.release(handles.at(2)); - QVERIFY(manager.data(handles.at(2)) == nullptr); - // Triggers QASSERT so commented - // manager.release(handles.at(2)); - - tHandle nHandle = manager.acquire(); - QVERIFY(manager.data(nHandle) != nullptr); -} - -void tst_DynamicArrayPolicy::lookupResource() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - for (int i = 0; i < 5; i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } - - tHandle16 t = manager.lookupHandle(2); - QVERIFY(t.handle() == 0); - QVERIFY(manager.data(t) == nullptr); - tst_ArrayResource *resource = manager.getOrCreateResource(2); - QVERIFY(resource != nullptr); - t = manager.lookupHandle(2); - QVERIFY(manager.data(t) == manager.lookupResource(2)); - QVERIFY(t == manager.getOrAcquireHandle(2)); - QVERIFY(resource == manager.getOrCreateResource(2)); - QVERIFY(manager.data(t) == resource); -} - -void tst_DynamicArrayPolicy::releaseResource() -{ - Qt3DCore::QResourceManager manager; - QList resources; - - for (int i = 0; i < 5; i++) { - resources << manager.getOrCreateResource(i); - } - - for (int i = 0; i < 5; i++) { - QVERIFY(resources.at(i) == manager.lookupResource(i)); - } - - for (int i = 0; i < 5; i++) { - manager.releaseResource(i); - QVERIFY(manager.lookupResource(i) == nullptr); - } -} - -class tst_Thread : public QThread -{ - Q_OBJECT -public: - - typedef Qt3DCore::QResourceManager Manager; - - tst_Thread() - : QThread() - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - i++; - QVERIFY(r != nullptr); - r->m_value.fetchAndAddOrdered(+1); - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; -}; - -void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccess() -{ - tst_Thread::Manager *manager = new tst_Thread::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int i = 0; i < iterations; i++) { - tst_Thread *thread = new tst_Thread(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) != nullptr); - QVERIFY(manager->lookupResource(i)->m_value = iterations); - } - - qDeleteAll(threads); - delete manager; -} - -class tst_Thread2 : public QThread -{ - Q_OBJECT -public: - - typedef Qt3DCore::QResourceManager Manager; - - tst_Thread2(int releaseAbove = 7) - : QThread() - , m_releaseAbove(releaseAbove) - { - } - - void setManager(Manager *manager) - { - m_manager = manager; - } - - // QThread interface -protected: - void run() - { - int i = 0; - int max = tHandle::maxIndex(); - while (i < max) { - tst_ArrayResource *r = m_manager->getOrCreateResource(i); - QVERIFY(r != nullptr); - int oldValue = r->m_value.fetchAndAddOrdered(+1); - if (oldValue == m_releaseAbove) - m_manager->releaseResource(i); - i++; - } - qDebug() << QThread::currentThread() << "Done"; - } - - Manager *m_manager; - int m_releaseAbove; -}; - -void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccessRelease() -{ - tst_Thread2::Manager *manager = new tst_Thread2::Manager(); - - QList threads; - - int iterations = 8; - int max = tHandle16::maxIndex(); - - for (int u = 0; u < 2; u++) { - - for (int i = 0; i < iterations; i++) { - tst_Thread2 *thread = new tst_Thread2(); - thread->setManager(manager); - threads << thread; - } - - for (int i = 0; i < iterations; i++) { - threads[i]->start(); - } - - for (int i = 0; i < iterations; i++) { - threads[i]->wait(); - } - - for (int i = 0; i < max; i++) { - QVERIFY(manager->lookupResource(i) == nullptr); - } - - qDeleteAll(threads); - threads.clear(); - } - - delete manager; -} - -void tst_DynamicArrayPolicy::maximumNumberOfResources() -{ - Qt3DCore::QResourceManager manager; - - QList resources; - QList handles; - - QCOMPARE(tHandle16::maxIndex(), (uint)manager.maximumSize()); - - for (int i = 0; i < manager.maximumSize(); i++) { - handles << manager.acquire(); - resources << manager.data(handles.at(i)); - resources.at(i)->m_value = 4; - } -} - -void tst_DynamicArrayPolicy::activeHandles() -{ - // GIVEN - Qt3DCore::QResourceManager manager; - - { - // WHEN - const tHandle newHandle = manager.getOrAcquireHandle(883U); - // THEN - QCOMPARE(manager.activeHandles().size(), 1); - QCOMPARE(manager.activeHandles().first(), newHandle); - } - - { - // WHEN - manager.releaseResource(883U); - // THEN - QVERIFY(manager.activeHandles().empty()); - } - - { - // WHEN - const tHandle newHandle = manager.acquire(); - // THEN - QCOMPARE(manager.activeHandles().size(), 1); - QCOMPARE(manager.activeHandles().first(), newHandle); - - // WHEN - manager.release(newHandle); - // THEN - QVERIFY(manager.activeHandles().empty()); - } -} - - - - -QTEST_APPLESS_MAIN(tst_DynamicArrayPolicy) - -#include "tst_dynamicarraypolicy.moc" diff --git a/tests/auto/core/core.pro b/tests/auto/core/core.pro index 088940b7d..3595755d6 100644 --- a/tests/auto/core/core.pro +++ b/tests/auto/core/core.pro @@ -2,7 +2,7 @@ TEMPLATE = subdirs SUBDIRS = \ handle \ - arrayresourcesmanager \ + qresourcemanager \ qcircularbuffer \ qboundedcircularbuffer \ nodes \ diff --git a/tests/auto/core/qresourcemanager/qresourcemanager.pro b/tests/auto/core/qresourcemanager/qresourcemanager.pro new file mode 100644 index 000000000..8fdf9b569 --- /dev/null +++ b/tests/auto/core/qresourcemanager/qresourcemanager.pro @@ -0,0 +1,7 @@ +TARGET = tst_qresourcemanager +CONFIG += testcase +TEMPLATE = app + +SOURCES += tst_qresourcemanager.cpp + +QT += testlib 3dcore 3dcore-private diff --git a/tests/auto/core/qresourcemanager/tst_qresourcemanager.cpp b/tests/auto/core/qresourcemanager/tst_qresourcemanager.cpp new file mode 100644 index 000000000..c3238fc8f --- /dev/null +++ b/tests/auto/core/qresourcemanager/tst_qresourcemanager.cpp @@ -0,0 +1,436 @@ +/**************************************************************************** +** +** 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:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +class tst_DynamicArrayPolicy : public QObject +{ + Q_OBJECT +public: + tst_DynamicArrayPolicy() {} + ~tst_DynamicArrayPolicy() {} + +private slots: + void createResourcesManager(); + void acquireResources(); + void getResources(); + void registerResourcesResize(); + void removeResource(); + void lookupResource(); + void releaseResource(); + void heavyDutyMultiThreadedAccess(); + void heavyDutyMultiThreadedAccessRelease(); + void maximumNumberOfResources(); + void activeHandles(); +}; + +class tst_ArrayResource +{ +public: + tst_ArrayResource() : m_value(0) + {} + + void cleanup() { m_value = 0; } + + QAtomicInt m_value; +}; + +QT_BEGIN_NAMESPACE +Q_DECLARE_RESOURCE_INFO(tst_ArrayResource, Q_REQUIRES_CLEANUP) +QT_END_NAMESPACE + +typedef Qt3DCore::QHandle tHandle; +typedef Qt3DCore::QHandle tHandle4; +typedef Qt3DCore::QHandle tHandle8; +typedef Qt3DCore::QHandle tHandle16; + +void tst_DynamicArrayPolicy::createResourcesManager() +{ + Qt3DCore::QResourceManager manager16; + Qt3DCore::QResourceManager manager4; + Qt3DCore::QResourceManager manager8; + QVERIFY(manager16.maximumSize() == 65535); + QVERIFY(manager8.maximumSize() == 255); + QVERIFY(manager4.maximumSize() == 15); +} + +/*! + * Check that the handles returned when a registering resources + * have a correct index and counter. + */ +void tst_DynamicArrayPolicy::acquireResources() +{ + Qt3DCore::QResourceManager manager; + + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + } +} + +/*! + * Test that values can be properly retrieved. + */ +void tst_DynamicArrayPolicy::getResources() +{ + + Qt3DCore::QResourceManager manager; + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + } + + for (uint i = 0; i < 5; i++) { + QVERIFY(handles.at(i).index() == i); + QVERIFY(handles.at(i).counter() == 1); + resources << manager.data(handles.at(i)); + QVERIFY(resources.at(i) != nullptr); + resources.at(i)->m_value = i; + } + + for (int i = 0; i < 5; i++) + QVERIFY(manager.data(handles.at(i))->m_value == i); + + // Check that an invalid resource returns NULL + tHandle8 iHandle; + QVERIFY(manager.data(iHandle) == nullptr); + +} + +/*! + * Test that when a resize of the data vectors in the manager occurs, + * everything behaves correctly. + */ +void tst_DynamicArrayPolicy::registerResourcesResize() +{ + Qt3DCore::QResourceManager manager; + QList handles; + + for (uint i = 0; i < 2; i++) { + handles << manager.acquire(); + manager.data(handles.at(i))->m_value = i + 2; + } + + for (uint i = 0; i < 5; i++) { + handles << manager.acquire(); + manager.data(handles.at(i + 2))->m_value = i + 2 + 5; + } + + for (int i = 0; i < 7; i++) { + QVERIFY(handles.at(i).index() == static_cast(i)); + QVERIFY(handles.at(i).counter() == 1); + if (i < 2) + QVERIFY(manager.data(handles.at(i))->m_value == i + 2); + else + QVERIFY(manager.data(handles.at(i))->m_value == i + 5); + } +} + +/*! + * Checks for the removal of resources. + */ +void tst_DynamicArrayPolicy::removeResource() +{ + Qt3DCore::QResourceManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 32; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + } + + manager.release(handles.at(2)); + QVERIFY(manager.data(handles.at(2)) == nullptr); + // Triggers QASSERT so commented + // manager.release(handles.at(2)); + + tHandle nHandle = manager.acquire(); + QVERIFY(manager.data(nHandle) != nullptr); +} + +void tst_DynamicArrayPolicy::lookupResource() +{ + Qt3DCore::QResourceManager manager; + + QList resources; + QList handles; + + for (int i = 0; i < 5; i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } + + tHandle16 t = manager.lookupHandle(2); + QVERIFY(t.handle() == 0); + QVERIFY(manager.data(t) == nullptr); + tst_ArrayResource *resource = manager.getOrCreateResource(2); + QVERIFY(resource != nullptr); + t = manager.lookupHandle(2); + QVERIFY(manager.data(t) == manager.lookupResource(2)); + QVERIFY(t == manager.getOrAcquireHandle(2)); + QVERIFY(resource == manager.getOrCreateResource(2)); + QVERIFY(manager.data(t) == resource); +} + +void tst_DynamicArrayPolicy::releaseResource() +{ + Qt3DCore::QResourceManager manager; + QList resources; + + for (int i = 0; i < 5; i++) { + resources << manager.getOrCreateResource(i); + } + + for (int i = 0; i < 5; i++) { + QVERIFY(resources.at(i) == manager.lookupResource(i)); + } + + for (int i = 0; i < 5; i++) { + manager.releaseResource(i); + QVERIFY(manager.lookupResource(i) == nullptr); + } +} + +class tst_Thread : public QThread +{ + Q_OBJECT +public: + + typedef Qt3DCore::QResourceManager Manager; + + tst_Thread() + : QThread() + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + i++; + QVERIFY(r != nullptr); + r->m_value.fetchAndAddOrdered(+1); + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; +}; + +void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccess() +{ + tst_Thread::Manager *manager = new tst_Thread::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int i = 0; i < iterations; i++) { + tst_Thread *thread = new tst_Thread(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) != nullptr); + QVERIFY(manager->lookupResource(i)->m_value = iterations); + } + + qDeleteAll(threads); + delete manager; +} + +class tst_Thread2 : public QThread +{ + Q_OBJECT +public: + + typedef Qt3DCore::QResourceManager Manager; + + tst_Thread2(int releaseAbove = 7) + : QThread() + , m_releaseAbove(releaseAbove) + { + } + + void setManager(Manager *manager) + { + m_manager = manager; + } + + // QThread interface +protected: + void run() + { + int i = 0; + int max = tHandle::maxIndex(); + while (i < max) { + tst_ArrayResource *r = m_manager->getOrCreateResource(i); + QVERIFY(r != nullptr); + int oldValue = r->m_value.fetchAndAddOrdered(+1); + if (oldValue == m_releaseAbove) + m_manager->releaseResource(i); + i++; + } + qDebug() << QThread::currentThread() << "Done"; + } + + Manager *m_manager; + int m_releaseAbove; +}; + +void tst_DynamicArrayPolicy::heavyDutyMultiThreadedAccessRelease() +{ + tst_Thread2::Manager *manager = new tst_Thread2::Manager(); + + QList threads; + + int iterations = 8; + int max = tHandle16::maxIndex(); + + for (int u = 0; u < 2; u++) { + + for (int i = 0; i < iterations; i++) { + tst_Thread2 *thread = new tst_Thread2(); + thread->setManager(manager); + threads << thread; + } + + for (int i = 0; i < iterations; i++) { + threads[i]->start(); + } + + for (int i = 0; i < iterations; i++) { + threads[i]->wait(); + } + + for (int i = 0; i < max; i++) { + QVERIFY(manager->lookupResource(i) == nullptr); + } + + qDeleteAll(threads); + threads.clear(); + } + + delete manager; +} + +void tst_DynamicArrayPolicy::maximumNumberOfResources() +{ + Qt3DCore::QResourceManager manager; + + QList resources; + QList handles; + + QCOMPARE(tHandle16::maxIndex(), (uint)manager.maximumSize()); + + for (int i = 0; i < manager.maximumSize(); i++) { + handles << manager.acquire(); + resources << manager.data(handles.at(i)); + resources.at(i)->m_value = 4; + } +} + +void tst_DynamicArrayPolicy::activeHandles() +{ + // GIVEN + Qt3DCore::QResourceManager manager; + + { + // WHEN + const tHandle newHandle = manager.getOrAcquireHandle(883U); + // THEN + QCOMPARE(manager.activeHandles().size(), 1); + QCOMPARE(manager.activeHandles().first(), newHandle); + } + + { + // WHEN + manager.releaseResource(883U); + // THEN + QVERIFY(manager.activeHandles().empty()); + } + + { + // WHEN + const tHandle newHandle = manager.acquire(); + // THEN + QCOMPARE(manager.activeHandles().size(), 1); + QCOMPARE(manager.activeHandles().first(), newHandle); + + // WHEN + manager.release(newHandle); + // THEN + QVERIFY(manager.activeHandles().empty()); + } +} + + + + +QTEST_APPLESS_MAIN(tst_DynamicArrayPolicy) + +#include "tst_qresourcemanager.moc" -- cgit v1.2.3 From d88f9796150b8bc3b1506d2bca450c0840bf3807 Mon Sep 17 00:00:00 2001 From: Mike Krus Date: Mon, 11 Sep 2017 17:48:12 +0100 Subject: Fix bounding sphere picking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some cases, the list of pickable objects may contain entities that don’t have an object picker (or not parent has). Now filter the pick results to only keep entities which either have an object picker or are children of an entity that does. Added unit test which now passes Change-Id: I930c3d60cf2d19e845fe6c0de904c53b93ebe8be Reviewed-by: Kevin Ottens --- .../pickboundingvolumejob.qrc | 1 + .../testscene_parententity.qml | 132 +++++++++++++++++++++ .../tst_pickboundingvolumejob.cpp | 50 ++++++++ 3 files changed, 183 insertions(+) create mode 100644 tests/auto/render/pickboundingvolumejob/testscene_parententity.qml (limited to 'tests') diff --git a/tests/auto/render/pickboundingvolumejob/pickboundingvolumejob.qrc b/tests/auto/render/pickboundingvolumejob/pickboundingvolumejob.qrc index fbd6a0a05..c2b0c7fff 100644 --- a/tests/auto/render/pickboundingvolumejob/pickboundingvolumejob.qrc +++ b/tests/auto/render/pickboundingvolumejob/pickboundingvolumejob.qrc @@ -7,5 +7,6 @@ testscene_dragenabledhoverenabled.qml testscene_pickersdisabled.qml testscene_dragenabledoverlapping.qml + testscene_parententity.qml diff --git a/tests/auto/render/pickboundingvolumejob/testscene_parententity.qml b/tests/auto/render/pickboundingvolumejob/testscene_parententity.qml new file mode 100644 index 000000000..759544bb9 --- /dev/null +++ b/tests/auto/render/pickboundingvolumejob/testscene_parententity.qml @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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:BSD$ +** 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. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Qt3D.Core 2.0 +import Qt3D.Render 2.0 +import Qt3D.Extras 2.0 +import QtQuick.Window 2.0 + +Entity { + id: sceneRoot + + Window { + id: win + width: 600 + height: 600 + visible: true + } + + Camera { + id: camera + projectionType: CameraLens.PerspectiveProjection + fieldOfView: 45 + nearPlane : 0.1 + farPlane : 1000.0 + position: Qt.vector3d( 0.0, 0.0, -40.0 ) + upVector: Qt.vector3d( 0.0, 1.0, 0.0 ) + viewCenter: Qt.vector3d( 0.0, 0.0, 0.0 ) + } + + components: [ + RenderSettings { + Viewport { + normalizedRect: Qt.rect(0.0, 0.0, 1.0, 1.0) + + RenderSurfaceSelector { + + surface: win + + ClearBuffers { + buffers : ClearBuffers.ColorDepthBuffer + NoDraw {} + } + + CameraSelector { + camera: camera + } + } + } + } + ] + + CuboidMesh { id: cubeMesh } + PhongMaterial { id: material } + + // Parent Entity + Entity { + + Entity { + components: [ + PlaneMesh { + width: 100 + height: 100 + }, + PhongMaterial { + } + ] + } + + // Child Entity + Entity { + property Transform transform: Transform { + translation: Qt.vector3d(-5, 0, 0) + scale: 2.0 + } + + property ObjectPicker picker: ObjectPicker { + objectName: "Picker" + dragEnabled: true + } + + components: [cubeMesh, material, transform, picker] + } + } + +} diff --git a/tests/auto/render/pickboundingvolumejob/tst_pickboundingvolumejob.cpp b/tests/auto/render/pickboundingvolumejob/tst_pickboundingvolumejob.cpp index 1d391101e..c60ddda27 100644 --- a/tests/auto/render/pickboundingvolumejob/tst_pickboundingvolumejob.cpp +++ b/tests/auto/render/pickboundingvolumejob/tst_pickboundingvolumejob.cpp @@ -1155,6 +1155,56 @@ private Q_SLOTS: arbiter1.events.clear(); arbiter2.events.clear(); } + + void checkParentNoPickerChildPicker() + { + // GIVEN + QmlSceneReader sceneReader(QUrl("qrc:/testscene_parententity.qml")); + QScopedPointer root(qobject_cast(sceneReader.root())); + QVERIFY(root); + + QList renderSettings = root->findChildren(); + QCOMPARE(renderSettings.size(), 1); + Qt3DRender::QPickingSettings *settings = renderSettings.first()->pickingSettings(); + + settings->setPickMethod(Qt3DRender::QPickingSettings::BoundingVolumePicking); + settings->setPickResultMode(Qt3DRender::QPickingSettings::NearestPick); + settings->setFaceOrientationPickingMode(Qt3DRender::QPickingSettings::FrontFace); + + QScopedPointer test(new Qt3DRender::TestAspect(root.data())); + TestArbiter arbiter; + + // Runs Required jobs + runRequiredJobs(test.data()); + + // THEN + // large no pickable object encapsing child and camera should not interfere with smaller picking object + QList pickers = root->findChildren(); + QCOMPARE(pickers.size(), 1); + + Qt3DRender::QObjectPicker *picker = pickers.first(); + QCOMPARE(pickers.first()->objectName(), QLatin1String("Picker")); + + Qt3DRender::Render::ObjectPicker *backendPicker = test->nodeManagers()->objectPickerManager()->lookupResource(picker->id()); + QVERIFY(backendPicker); + Qt3DCore::QBackendNodePrivate::get(backendPicker)->setArbiter(&arbiter); + + // WHEN -> Pressed on object + Qt3DRender::Render::PickBoundingVolumeJob pickBVJob; + initializePickBoundingVolumeJob(&pickBVJob, test.data()); + + QList events; + events.push_back(QMouseEvent(QMouseEvent::MouseButtonPress, QPointF(400.0f, 300.0f), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)); + pickBVJob.setMouseEvents(events); + bool earlyReturn = !pickBVJob.runHelper(); + + // THEN -> Pressed + QVERIFY(!earlyReturn); + QVERIFY(backendPicker->isPressed()); + QCOMPARE(arbiter.events.count(), 1); + Qt3DCore::QPropertyUpdatedChangePtr change = arbiter.events.first().staticCast(); + QCOMPARE(change->propertyName(), "pressed"); + } }; QTEST_MAIN(tst_PickBoundingVolumeJob) -- cgit v1.2.3 From bf5fd7a78c91e29332ce70ad844b756150f32f18 Mon Sep 17 00:00:00 2001 From: Mike Krus Date: Mon, 11 Sep 2017 22:43:56 +0100 Subject: Fix crash on surface selector unit test Change order of local variables affecting the destructions order and making sure window outlives the surface selector Change-Id: Ia04f110656f4cc563643fb056ae491ffadf3a8b4 Reviewed-by: Sean Harmer --- .../auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp b/tests/auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp index 1e003e6b8..982a58b2b 100644 --- a/tests/auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp +++ b/tests/auto/render/qrendersurfaceselector/tst_qrendersurfaceselector.cpp @@ -188,8 +188,8 @@ private Q_SLOTS: void checkCreationData() { // GIVEN - Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector; QWindow newValue; + Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector; renderSurfaceSelector.setSurface(&newValue); renderSurfaceSelector.setExternalRenderTargetSize(QSize(128, 128)); @@ -250,10 +250,10 @@ private Q_SLOTS: void checkSurfaceUpdate() { // GIVEN + QWindow newWindow; TestArbiter arbiter; Qt3DRender::QRenderSurfaceSelector renderSurfaceSelector; arbiter.setArbiterOnNode(&renderSurfaceSelector); - QWindow newWindow; { // WHEN -- cgit v1.2.3