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