| 1 | // Copyright (C) 2015 Paul Lemire |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <QSemaphore> |
| 6 | #include <QThread> |
| 7 | #include <Qt3DRender/private/vsyncframeadvanceservice_p.h> |
| 8 | |
| 9 | class FakeRenderThread final : public QThread |
| 10 | { |
| 11 | public: |
| 12 | FakeRenderThread(Qt3DRender::Render::VSyncFrameAdvanceService *tickService) |
| 13 | : m_tickService(tickService) |
| 14 | , m_running(1) |
| 15 | , m_submitCount(0) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | int submitCount() const { return m_submitCount; } |
| 20 | |
| 21 | void stopRunning() |
| 22 | { |
| 23 | m_running.fetchAndStoreOrdered(0); |
| 24 | m_submitSemaphore.release(1); |
| 25 | } |
| 26 | |
| 27 | void enqueueRenderView() |
| 28 | { |
| 29 | m_submitSemaphore.release(1); |
| 30 | } |
| 31 | |
| 32 | protected: |
| 33 | // QThread interface |
| 34 | void run() final |
| 35 | { |
| 36 | m_tickService->proceedToNextFrame(); |
| 37 | |
| 38 | while (true) { |
| 39 | if (!isReadyToSubmit()) |
| 40 | break; |
| 41 | ++m_submitCount; |
| 42 | m_tickService->proceedToNextFrame(); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | bool isReadyToSubmit() |
| 48 | { |
| 49 | m_submitSemaphore.acquire(1); |
| 50 | return m_running.loadRelaxed() == 1; |
| 51 | } |
| 52 | |
| 53 | Qt3DRender::Render::VSyncFrameAdvanceService *m_tickService; |
| 54 | QAtomicInt m_running; |
| 55 | QSemaphore m_submitSemaphore; |
| 56 | int m_submitCount; |
| 57 | }; |
| 58 | |
| 59 | class tst_VSyncFrameAdvanceService : public QObject |
| 60 | { |
| 61 | Q_OBJECT |
| 62 | |
| 63 | private Q_SLOTS: |
| 64 | |
| 65 | void checkSynchronisation() |
| 66 | { |
| 67 | // GIVEN |
| 68 | Qt3DRender::Render::VSyncFrameAdvanceService tickService(true); |
| 69 | FakeRenderThread renderThread(&tickService); |
| 70 | |
| 71 | // WHEN |
| 72 | renderThread.start(); |
| 73 | |
| 74 | for (int i = 0; i < 10; ++i) { |
| 75 | tickService.waitForNextFrame(); |
| 76 | renderThread.enqueueRenderView(); |
| 77 | } |
| 78 | |
| 79 | tickService.waitForNextFrame(); |
| 80 | |
| 81 | renderThread.stopRunning(); |
| 82 | renderThread.wait(); |
| 83 | |
| 84 | // THEN |
| 85 | QCOMPARE(renderThread.submitCount(), 10); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | QTEST_MAIN(tst_VSyncFrameAdvanceService) |
| 90 | |
| 91 | #include "tst_vsyncframeadvanceservice.moc" |