aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/util/qquickanimatorcontroller.cpp
blob: d7c25dadc1c573898f5948ee6be1100a076bd5f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2016 Gunnar Sletta <gunnar@sletta.org>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquickanimatorcontroller_p.h"

#include <private/qquickitem_p.h>
#include <private/qsgrenderloop_p.h>

#include <private/qanimationgroupjob_p.h>

#include <QtGui/qscreen.h>

#include <QtCore/qcoreapplication.h>

QT_BEGIN_NAMESPACE

QQuickAnimatorController::~QQuickAnimatorController()
{
}

QQuickAnimatorController::QQuickAnimatorController(QQuickWindow *window)
    : m_window(window)
{
}

static void qquickanimator_invalidate_jobs(QAbstractAnimationJob *job)
{
    if (job->isRenderThreadJob()) {
        static_cast<QQuickAnimatorJob *>(job)->invalidate();
    } else if (job->isGroup()) {
        for (QAbstractAnimationJob *a : *static_cast<QAnimationGroupJob *>(job)->children())
            qquickanimator_invalidate_jobs(a);
    }
}

void QQuickAnimatorController::windowNodesDestroyed()
{
    for (const QSharedPointer<QAbstractAnimationJob> &toStop : std::as_const(m_rootsPendingStop)) {
        qquickanimator_invalidate_jobs(toStop.data());
        toStop->stop();
    }
    m_rootsPendingStop.clear();

    // Clear animation roots and iterate over a temporary to avoid that job->stop()
    // modifies the m_animationRoots and messes with our iteration
    const auto roots = m_animationRoots;
    m_animationRoots.clear();
    for (const QSharedPointer<QAbstractAnimationJob> &job : roots) {
        qquickanimator_invalidate_jobs(job.data());

        // Stop it and add it to the list of pending start so it might get
        // started later on.
        job->stop();
        m_rootsPendingStart.insert(job);
    }
}

void QQuickAnimatorController::advance()
{
    bool running = false;
    for (const QSharedPointer<QAbstractAnimationJob> &job : std::as_const(m_animationRoots)) {
        if (job->isRunning()) {
            running = true;
            break;
        }
    }

    for (QQuickAnimatorJob *job : std::as_const(m_runningAnimators))
        job->commit();

    if (running)
        m_window->update();
}

static void qquickanimator_sync_before_start(QAbstractAnimationJob *job)
{
    if (job->isRenderThreadJob()) {
        static_cast<QQuickAnimatorJob *>(job)->preSync();
    } else if (job->isGroup()) {
        for (QAbstractAnimationJob *a : *static_cast<QAnimationGroupJob *>(job)->children())
            qquickanimator_sync_before_start(a);
    }
}

void QQuickAnimatorController::beforeNodeSync()
{
    for (const QSharedPointer<QAbstractAnimationJob> &toStop : std::as_const(m_rootsPendingStop)) {
        toStop->stop();
        m_animationRoots.remove(toStop.data());
    }
    m_rootsPendingStop.clear();


    for (QQuickAnimatorJob *job : std::as_const(m_runningAnimators))
        job->preSync();

    // Start pending jobs
    for (const QSharedPointer<QAbstractAnimationJob> &job : std::as_const(m_rootsPendingStart)) {
        Q_ASSERT(!job->isRunning());

        // We want to make sure that presync is called before
        // updateAnimationTime is called the very first time, so before
        // starting a tree of jobs, we go through it and call preSync on all
        // its animators.
        qquickanimator_sync_before_start(job.data());

        // The start the job..
        job->start();
        m_animationRoots.insert(job.data(), job);
    }
    m_rootsPendingStart.clear();

    // Issue an update directly on the window to force another render pass.
    if (m_animationRoots.size())
        m_window->update();
}

void QQuickAnimatorController::afterNodeSync()
{
    for (QQuickAnimatorJob *job : std::as_const(m_runningAnimators))
        job->postSync();
}

void QQuickAnimatorController::animationFinished(QAbstractAnimationJob *job)
{
     m_animationRoots.remove(job);
}

void QQuickAnimatorController::animationStateChanged(QAbstractAnimationJob *job,
                                                     QAbstractAnimationJob::State newState,
                                                     QAbstractAnimationJob::State oldState)
{
    Q_ASSERT(job->isRenderThreadJob());
    QQuickAnimatorJob *animator = static_cast<QQuickAnimatorJob *>(job);
    if (newState == QAbstractAnimationJob::Running) {
        m_runningAnimators.insert(animator);
    } else if (oldState == QAbstractAnimationJob::Running) {
        animator->commit();
        m_runningAnimators.remove(animator);
    }
}


void QQuickAnimatorController::requestSync()
{
    // Force a "sync" pass as the newly started animation needs to sync properties from GUI.
    m_window->maybeUpdate();
}

// All this is being executed on the GUI thread while the animator controller
// is locked.
void QQuickAnimatorController::start_helper(QAbstractAnimationJob *job)
{
    if (job->isRenderThreadJob()) {
        QQuickAnimatorJob *j = static_cast<QQuickAnimatorJob *>(job);
        j->addAnimationChangeListener(this, QAbstractAnimationJob::StateChange);
        j->initialize(this);
    } else if (job->isGroup()) {
        for (QAbstractAnimationJob *a : *static_cast<QAnimationGroupJob *>(job)->children())
            start_helper(a);
    }
}

// Called by the proxy when it is time to kick off an animation job
void QQuickAnimatorController::start(const QSharedPointer<QAbstractAnimationJob> &job)
{
    m_rootsPendingStart.insert(job);
    m_rootsPendingStop.remove(job);
    job->addAnimationChangeListener(this, QAbstractAnimationJob::Completion);
    start_helper(job.data());
    requestSync();
}


// Called by the proxy when it is time to stop an animation job.
void QQuickAnimatorController::cancel(const QSharedPointer<QAbstractAnimationJob> &job)
{
    m_rootsPendingStart.remove(job);
    m_rootsPendingStop.insert(job);
}


QT_END_NAMESPACE

#include "moc_qquickanimatorcontroller_p.cpp"