1// Copyright (C) 2016 The Qt Company Ltd.
2// Copyright (C) 2016 Gunnar Sletta <gunnar@sletta.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QQUICKANIMATORJOB_P_H
6#define QQUICKANIMATORJOB_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qabstractanimationjob_p.h>
20#include <private/qquickanimator_p.h>
21#include <private/qtquickglobal_p.h>
22
23#include <QtQuick/qquickitem.h>
24
25#include <QtCore/qeasingcurve.h>
26
27QT_BEGIN_NAMESPACE
28
29class QQuickAnimator;
30class QQuickWindow;
31class QQuickItem;
32class QQuickAbstractAnimation;
33
34class QQuickAnimatorController;
35
36class QSGOpacityNode;
37
38class Q_QUICK_PRIVATE_EXPORT QQuickAnimatorProxyJob : public QObject, public QAbstractAnimationJob
39{
40 Q_OBJECT
41
42public:
43 QQuickAnimatorProxyJob(QAbstractAnimationJob *job, QQuickAbstractAnimation *animation);
44 ~QQuickAnimatorProxyJob();
45
46 int duration() const override { return m_duration; }
47
48 const QSharedPointer<QAbstractAnimationJob> &job() const { return m_job; }
49
50protected:
51 void updateCurrentTime(int) override;
52 void updateLoopCount(int) override;
53 void updateState(QAbstractAnimationJob::State newState, QAbstractAnimationJob::State oldState) override;
54 void debugAnimation(QDebug d) const override;
55
56public Q_SLOTS:
57 void windowChanged(QQuickWindow *window);
58 void sceneGraphInitialized();
59
60private:
61 void syncBackCurrentValues();
62 void readyToAnimate();
63 void setWindow(QQuickWindow *window);
64 static QObject *findAnimationContext(QQuickAbstractAnimation *);
65
66 QPointer<QQuickAnimatorController> m_controller;
67 QSharedPointer<QAbstractAnimationJob> m_job;
68 int m_duration;
69
70 enum InternalState {
71 State_Starting, // Used when it should be running, but no we're still missing the controller.
72 State_Running,
73 State_Paused,
74 State_Stopped
75 };
76
77 InternalState m_internalState;
78};
79
80class Q_QUICK_PRIVATE_EXPORT QQuickAnimatorJob : public QAbstractAnimationJob
81{
82public:
83 virtual void setTarget(QQuickItem *target);
84 QQuickItem *target() const { return m_target; }
85
86 void setFrom(qreal from) {
87 m_from = from;
88 boundValue();
89 }
90 qreal from() const { return m_from; }
91
92 void setTo(qreal to) {
93 m_to = to;
94 boundValue();
95 }
96 qreal to() const { return m_to; }
97
98 void setDuration(int duration) { m_duration = duration; }
99 int duration() const override { return m_duration; }
100
101 QEasingCurve easingCurve() const { return m_easing; }
102 void setEasingCurve(const QEasingCurve &curve) { m_easing = curve; }
103
104 // Initialize is called on the GUI thread just before it is started
105 // and taken over on the render thread.
106 virtual void initialize(QQuickAnimatorController *controller);
107
108 // Called on the render thread during SG shutdown.
109 virtual void invalidate() = 0;
110
111 // Called on the GUI thread after a complete render thread animation job
112 // has been completed to write back a given animator's result to the
113 // source item.
114 virtual void writeBack() = 0;
115
116 // Called before the SG sync on the render thread. The GUI thread is
117 // locked during this call.
118 virtual void preSync() { }
119
120 // Called after the SG sync on the render thread. The GUI thread is
121 // locked during this call.
122 virtual void postSync() { }
123
124 // Called after animations have ticked on the render thread. No locks are
125 // held at this time, so synchronization needs to be taken into account
126 // if applicable.
127 virtual void commit() { }
128
129 bool isTransform() const { return m_isTransform; }
130 bool isUniform() const { return m_isUniform; }
131
132 qreal value() const;
133
134 QQuickAnimatorController *controller() const { return m_controller; }
135
136protected:
137 QQuickAnimatorJob();
138 void debugAnimation(QDebug d) const override;
139
140 qreal progress(int time) const;
141 void boundValue();
142
143 QPointer<QQuickItem> m_target;
144 QQuickAnimatorController *m_controller;
145
146 qreal m_from;
147 qreal m_to;
148 qreal m_value;
149
150 QEasingCurve m_easing;
151
152 int m_duration;
153
154 uint m_isTransform : 1;
155 uint m_isUniform : 1;
156};
157
158class QQuickTransformAnimatorJob : public QQuickAnimatorJob
159{
160public:
161
162 struct Helper
163 {
164 Helper()
165 : ref(1)
166 , node(nullptr)
167 , ox(0)
168 , oy(0)
169 , dx(0)
170 , dy(0)
171 , scale(1)
172 , rotation(0)
173 , wasSynced(false)
174 , wasChanged(false)
175 {
176 }
177
178 void sync();
179 void commit();
180
181 int ref;
182 QQuickItem *item;
183 QSGTransformNode *node;
184
185 // Origin
186 float ox;
187 float oy;
188
189 float dx;
190 float dy;
191 float scale;
192 float rotation;
193
194 uint wasSynced : 1;
195 uint wasChanged : 1;
196 };
197
198 ~QQuickTransformAnimatorJob();
199
200 void commit() override;
201 void preSync() override;
202
203 void setTarget(QQuickItem *item) override;
204
205protected:
206 QQuickTransformAnimatorJob();
207 void invalidate() override;
208
209 Helper *m_helper;
210};
211
212class Q_QUICK_PRIVATE_EXPORT QQuickScaleAnimatorJob : public QQuickTransformAnimatorJob
213{
214public:
215 void updateCurrentTime(int time) override;
216 void writeBack() override;
217};
218
219class Q_QUICK_PRIVATE_EXPORT QQuickXAnimatorJob : public QQuickTransformAnimatorJob
220{
221public:
222 void updateCurrentTime(int time) override;
223 void writeBack() override;
224};
225
226class Q_QUICK_PRIVATE_EXPORT QQuickYAnimatorJob : public QQuickTransformAnimatorJob
227{
228public:
229 void updateCurrentTime(int time) override;
230 void writeBack() override;
231};
232
233class Q_QUICK_PRIVATE_EXPORT QQuickRotationAnimatorJob : public QQuickTransformAnimatorJob
234{
235public:
236 QQuickRotationAnimatorJob();
237
238 void updateCurrentTime(int time) override;
239 void writeBack() override;
240
241 void setDirection(QQuickRotationAnimator::RotationDirection direction) { m_direction = direction; }
242 QQuickRotationAnimator::RotationDirection direction() const { return m_direction; }
243
244private:
245 QQuickRotationAnimator::RotationDirection m_direction;
246};
247
248class Q_QUICK_PRIVATE_EXPORT QQuickOpacityAnimatorJob : public QQuickAnimatorJob
249{
250public:
251 QQuickOpacityAnimatorJob();
252
253 void invalidate() override;
254 void updateCurrentTime(int time) override;
255 void writeBack() override;
256 void postSync() override;
257
258private:
259 QSGOpacityNode *m_opacityNode;
260};
261
262#if QT_CONFIG(quick_shadereffect)
263class QQuickShaderEffect;
264
265class Q_QUICK_PRIVATE_EXPORT QQuickUniformAnimatorJob : public QQuickAnimatorJob
266{
267public:
268 QQuickUniformAnimatorJob();
269
270 void setTarget(QQuickItem *target) override;
271
272 void setUniform(const QByteArray &uniform) { m_uniform = uniform; }
273 QByteArray uniform() const { return m_uniform; }
274
275 void updateCurrentTime(int time) override;
276 void writeBack() override;
277 void postSync() override;
278
279 void invalidate() override;
280
281private:
282 QByteArray m_uniform;
283 QQuickShaderEffect *m_effect = nullptr;
284};
285#endif
286
287QT_END_NAMESPACE
288
289#endif // QQUICKANIMATORJOB_P_H
290

source code of qtdeclarative/src/quick/util/qquickanimatorjob_p.h