OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CCActiveAnimation_h |
| 6 #define CCActiveAnimation_h |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 |
| 11 namespace cc { |
| 12 |
| 13 class CCAnimationCurve; |
| 14 |
| 15 // A CCActiveAnimation, contains all the state required to play a CCAnimationCur
ve. |
| 16 // Specifically, the affected property, the run state (paused, finished, etc.), |
| 17 // loop count, last pause time, and the total time spent paused. |
| 18 class CCActiveAnimation { |
| 19 public: |
| 20 // Animations begin in one of the 'waiting' states. Animations waiting for t
he next tick |
| 21 // will start the next time the controller animates. Animations waiting for
target |
| 22 // availibility will run as soon as their target property is free (and all t
he animations |
| 23 // animating with it are also able to run). Animations waiting for their sta
rt time to |
| 24 // come have be scheduled to run at a particular point in time. When this ti
me arrives, |
| 25 // the controller will move the animations into the Running state. Running a
nimations |
| 26 // may toggle between Running and Paused, and may be stopped by moving into
either the |
| 27 // Aborted or Finished states. A Finished animation was allowed to run to co
mpletion, but |
| 28 // an Aborted animation was not. |
| 29 enum RunState { |
| 30 WaitingForNextTick = 0, |
| 31 WaitingForTargetAvailability, |
| 32 WaitingForStartTime, |
| 33 WaitingForDeletion, |
| 34 Running, |
| 35 Paused, |
| 36 Finished, |
| 37 Aborted, |
| 38 // This sentinel must be last. |
| 39 RunStateEnumSize |
| 40 }; |
| 41 |
| 42 enum TargetProperty { |
| 43 Transform = 0, |
| 44 Opacity, |
| 45 // This sentinel must be last. |
| 46 TargetPropertyEnumSize |
| 47 }; |
| 48 |
| 49 static scoped_ptr<CCActiveAnimation> create(scoped_ptr<CCAnimationCurve>, in
t animationId, int groupId, TargetProperty); |
| 50 |
| 51 virtual ~CCActiveAnimation(); |
| 52 |
| 53 int id() const { return m_id; } |
| 54 int group() const { return m_group; } |
| 55 TargetProperty targetProperty() const { return m_targetProperty; } |
| 56 |
| 57 RunState runState() const { return m_runState; } |
| 58 void setRunState(RunState, double monotonicTime); |
| 59 |
| 60 // This is the number of times that the animation will play. If this |
| 61 // value is zero the animation will not play. If it is negative, then |
| 62 // the animation will loop indefinitely. |
| 63 int iterations() const { return m_iterations; } |
| 64 void setIterations(int n) { m_iterations = n; } |
| 65 |
| 66 double startTime() const { return m_startTime; } |
| 67 void setStartTime(double monotonicTime) { m_startTime = monotonicTime; } |
| 68 bool hasSetStartTime() const { return m_startTime; } |
| 69 |
| 70 double timeOffset() const { return m_timeOffset; } |
| 71 void setTimeOffset(double monotonicTime) { m_timeOffset = monotonicTime; } |
| 72 |
| 73 void suspend(double monotonicTime); |
| 74 void resume(double monotonicTime); |
| 75 |
| 76 // If alternatesDirection is true, on odd numbered iterations we reverse the
curve. |
| 77 bool alternatesDirection() const { return m_alternatesDirection; } |
| 78 void setAlternatesDirection(bool alternates) { m_alternatesDirection = alter
nates; } |
| 79 |
| 80 bool isFinishedAt(double monotonicTime) const; |
| 81 bool isFinished() const { return m_runState == Finished |
| 82 || m_runState == Aborted |
| 83 || m_runState == WaitingForDeletion; } |
| 84 |
| 85 CCAnimationCurve* curve() { return m_curve.get(); } |
| 86 const CCAnimationCurve* curve() const { return m_curve.get(); } |
| 87 |
| 88 // If this is true, even if the animation is running, it will not be tickabl
e until |
| 89 // it is given a start time. This is true for animations running on the main
thread. |
| 90 bool needsSynchronizedStartTime() const { return m_needsSynchronizedStartTim
e; } |
| 91 void setNeedsSynchronizedStartTime(bool needsSynchronizedStartTime) { m_need
sSynchronizedStartTime = needsSynchronizedStartTime; } |
| 92 |
| 93 // Takes the given absolute time, and using the start time and the number |
| 94 // of iterations, returns the relative time in the current iteration. |
| 95 double trimTimeToCurrentIteration(double monotonicTime) const; |
| 96 |
| 97 enum InstanceType { |
| 98 ControllingInstance = 0, |
| 99 NonControllingInstance |
| 100 }; |
| 101 |
| 102 scoped_ptr<CCActiveAnimation> clone(InstanceType) const; |
| 103 scoped_ptr<CCActiveAnimation> cloneAndInitialize(InstanceType, RunState init
ialRunState, double startTime) const; |
| 104 bool isControllingInstance() const { return m_isControllingInstance; } |
| 105 |
| 106 void pushPropertiesTo(CCActiveAnimation*) const; |
| 107 |
| 108 private: |
| 109 CCActiveAnimation(scoped_ptr<CCAnimationCurve>, int animationId, int groupId
, TargetProperty); |
| 110 |
| 111 scoped_ptr<CCAnimationCurve> m_curve; |
| 112 |
| 113 // IDs are not necessarily unique. |
| 114 int m_id; |
| 115 |
| 116 // Animations that must be run together are called 'grouped' and have the sa
me group id |
| 117 // Grouped animations are guaranteed to start at the same time and no other
animations |
| 118 // may animate any of the group's target properties until all animations in
the |
| 119 // group have finished animating. Note: an active animation's group id and t
arget |
| 120 // property uniquely identify that animation. |
| 121 int m_group; |
| 122 |
| 123 TargetProperty m_targetProperty; |
| 124 RunState m_runState; |
| 125 int m_iterations; |
| 126 double m_startTime; |
| 127 bool m_alternatesDirection; |
| 128 |
| 129 // The time offset effectively pushes the start of the animation back in tim
e. This is |
| 130 // used for resuming paused animations -- an animation is added with a non-z
ero time |
| 131 // offset, causing the animation to skip ahead to the desired point in time. |
| 132 double m_timeOffset; |
| 133 |
| 134 bool m_needsSynchronizedStartTime; |
| 135 |
| 136 // When an animation is suspended, it behaves as if it is paused and it also
ignores |
| 137 // all run state changes until it is resumed. This is used for testing purpo
ses. |
| 138 bool m_suspended; |
| 139 |
| 140 // These are used in trimTimeToCurrentIteration to account for time |
| 141 // spent while paused. This is not included in AnimationState since it |
| 142 // there is absolutely no need for clients of this controller to know |
| 143 // about these values. |
| 144 double m_pauseTime; |
| 145 double m_totalPausedTime; |
| 146 |
| 147 // Animations lead dual lives. An active animation will be conceptually owne
d by |
| 148 // two controllers, one on the impl thread and one on the main. In reality,
there |
| 149 // will be two separate CCActiveAnimation instances for the same animation.
They |
| 150 // will have the same group id and the same target property (these two value
s |
| 151 // uniquely identify an animation). The instance on the impl thread is the i
nstance |
| 152 // that ultimately controls the values of the animating layer and so we will
refer |
| 153 // to it as the 'controlling instance'. |
| 154 bool m_isControllingInstance; |
| 155 |
| 156 DISALLOW_COPY_AND_ASSIGN(CCActiveAnimation); |
| 157 }; |
| 158 |
| 159 } // namespace cc |
| 160 |
| 161 #endif // CCActiveAnimation_h |
OLD | NEW |