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 | 4 |
5 #include "cc/active_animation.h" | 5 #include "cc/animation.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "cc/animation_curve.h" | 11 #include "cc/animation_curve.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // This should match the RunState enum. | 15 // This should match the RunState enum. |
16 static const char* const s_runStateNames[] = { | 16 static const char* const s_runStateNames[] = { |
17 "WaitingForNextTick", | 17 "WaitingForNextTick", |
18 "WaitingForTargetAvailability", | 18 "WaitingForTargetAvailability", |
19 "WaitingForStartTime", | 19 "WaitingForStartTime", |
20 "WaitingForDeletion", | 20 "WaitingForDeletion", |
21 "Running", | 21 "Running", |
22 "Paused", | 22 "Paused", |
23 "Finished", | 23 "Finished", |
24 "Aborted" | 24 "Aborted" |
25 }; | 25 }; |
26 | 26 |
27 COMPILE_ASSERT(static_cast<int>(cc::ActiveAnimation::RunStateEnumSize) == arrays
ize(s_runStateNames), RunState_names_match_enum); | 27 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == arraysize(s_
runStateNames), RunState_names_match_enum); |
28 | 28 |
29 // This should match the TargetProperty enum. | 29 // This should match the TargetProperty enum. |
30 static const char* const s_targetPropertyNames[] = { | 30 static const char* const s_targetPropertyNames[] = { |
31 "Transform", | 31 "Transform", |
32 "Opacity" | 32 "Opacity" |
33 }; | 33 }; |
34 | 34 |
35 COMPILE_ASSERT(static_cast<int>(cc::ActiveAnimation::TargetPropertyEnumSize) ==
arraysize(s_targetPropertyNames), TargetProperty_names_match_enum); | 35 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == arrays
ize(s_targetPropertyNames), TargetProperty_names_match_enum); |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 namespace cc { | 39 namespace cc { |
40 | 40 |
41 scoped_ptr<ActiveAnimation> ActiveAnimation::create(scoped_ptr<AnimationCurve> c
urve, int animationId, int groupId, TargetProperty targetProperty) | 41 scoped_ptr<Animation> Animation::create(scoped_ptr<AnimationCurve> curve, int an
imationId, int groupId, TargetProperty targetProperty) |
42 { | 42 { |
43 return make_scoped_ptr(new ActiveAnimation(curve.Pass(), animationId, groupI
d, targetProperty)); | 43 return make_scoped_ptr(new Animation(curve.Pass(), animationId, groupId, tar
getProperty)); |
44 } | 44 } |
45 | 45 |
46 ActiveAnimation::ActiveAnimation(scoped_ptr<AnimationCurve> curve, int animation
Id, int groupId, TargetProperty targetProperty) | 46 Animation::Animation(scoped_ptr<AnimationCurve> curve, int animationId, int grou
pId, TargetProperty targetProperty) |
47 : m_curve(curve.Pass()) | 47 : m_curve(curve.Pass()) |
48 , m_id(animationId) | 48 , m_id(animationId) |
49 , m_group(groupId) | 49 , m_group(groupId) |
50 , m_targetProperty(targetProperty) | 50 , m_targetProperty(targetProperty) |
51 , m_runState(WaitingForTargetAvailability) | 51 , m_runState(WaitingForTargetAvailability) |
52 , m_iterations(1) | 52 , m_iterations(1) |
53 , m_startTime(0) | 53 , m_startTime(0) |
54 , m_alternatesDirection(false) | 54 , m_alternatesDirection(false) |
55 , m_timeOffset(0) | 55 , m_timeOffset(0) |
56 , m_needsSynchronizedStartTime(false) | 56 , m_needsSynchronizedStartTime(false) |
57 , m_suspended(false) | 57 , m_suspended(false) |
58 , m_pauseTime(0) | 58 , m_pauseTime(0) |
59 , m_totalPausedTime(0) | 59 , m_totalPausedTime(0) |
60 , m_isControllingInstance(false) | 60 , m_isControllingInstance(false) |
61 { | 61 { |
62 } | 62 } |
63 | 63 |
64 ActiveAnimation::~ActiveAnimation() | 64 Animation::~Animation() |
65 { | 65 { |
66 if (m_runState == Running || m_runState == Paused) | 66 if (m_runState == Running || m_runState == Paused) |
67 setRunState(Aborted, 0); | 67 setRunState(Aborted, 0); |
68 } | 68 } |
69 | 69 |
70 void ActiveAnimation::setRunState(RunState runState, double monotonicTime) | 70 void Animation::setRunState(RunState runState, double monotonicTime) |
71 { | 71 { |
72 if (m_suspended) | 72 if (m_suspended) |
73 return; | 73 return; |
74 | 74 |
75 char nameBuffer[256]; | 75 char nameBuffer[256]; |
76 base::snprintf(nameBuffer, sizeof(nameBuffer), "%s-%d%s", s_targetPropertyNa
mes[m_targetProperty], m_group, m_isControllingInstance ? "(impl)" : ""); | 76 base::snprintf(nameBuffer, sizeof(nameBuffer), "%s-%d%s", s_targetPropertyNa
mes[m_targetProperty], m_group, m_isControllingInstance ? "(impl)" : ""); |
77 | 77 |
78 bool isWaitingToStart = m_runState == WaitingForNextTick | 78 bool isWaitingToStart = m_runState == WaitingForNextTick |
79 || m_runState == WaitingForTargetAvailability | 79 || m_runState == WaitingForTargetAvailability |
80 || m_runState == WaitingForStartTime; | 80 || m_runState == WaitingForStartTime; |
81 | 81 |
82 if (isWaitingToStart && runState == Running) | 82 if (isWaitingToStart && runState == Running) |
83 TRACE_EVENT_ASYNC_BEGIN1("cc", "ActiveAnimation", this, "Name", TRACE_ST
R_COPY(nameBuffer)); | 83 TRACE_EVENT_ASYNC_BEGIN1("cc", "Animation", this, "Name", TRACE_STR_COPY
(nameBuffer)); |
84 | 84 |
85 bool wasFinished = isFinished(); | 85 bool wasFinished = isFinished(); |
86 | 86 |
87 const char* oldRunStateName = s_runStateNames[m_runState]; | 87 const char* oldRunStateName = s_runStateNames[m_runState]; |
88 | 88 |
89 if (runState == Running && m_runState == Paused) | 89 if (runState == Running && m_runState == Paused) |
90 m_totalPausedTime += monotonicTime - m_pauseTime; | 90 m_totalPausedTime += monotonicTime - m_pauseTime; |
91 else if (runState == Paused) | 91 else if (runState == Paused) |
92 m_pauseTime = monotonicTime; | 92 m_pauseTime = monotonicTime; |
93 m_runState = runState; | 93 m_runState = runState; |
94 | 94 |
95 const char* newRunStateName = s_runStateNames[runState]; | 95 const char* newRunStateName = s_runStateNames[runState]; |
96 | 96 |
97 if (!wasFinished && isFinished()) | 97 if (!wasFinished && isFinished()) |
98 TRACE_EVENT_ASYNC_END0("cc", "ActiveAnimation", this); | 98 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
99 | 99 |
100 char stateBuffer[256]; | 100 char stateBuffer[256]; |
101 base::snprintf(stateBuffer, sizeof(stateBuffer), "%s->%s", oldRunStateName,
newRunStateName); | 101 base::snprintf(stateBuffer, sizeof(stateBuffer), "%s->%s", oldRunStateName,
newRunStateName); |
102 | 102 |
103 TRACE_EVENT_INSTANT2("cc", "LayerAnimationController::setRunState", "Name",
TRACE_STR_COPY(nameBuffer), "State", TRACE_STR_COPY(stateBuffer)); | 103 TRACE_EVENT_INSTANT2("cc", "LayerAnimationController::setRunState", "Name",
TRACE_STR_COPY(nameBuffer), "State", TRACE_STR_COPY(stateBuffer)); |
104 } | 104 } |
105 | 105 |
106 void ActiveAnimation::suspend(double monotonicTime) | 106 void Animation::suspend(double monotonicTime) |
107 { | 107 { |
108 setRunState(Paused, monotonicTime); | 108 setRunState(Paused, monotonicTime); |
109 m_suspended = true; | 109 m_suspended = true; |
110 } | 110 } |
111 | 111 |
112 void ActiveAnimation::resume(double monotonicTime) | 112 void Animation::resume(double monotonicTime) |
113 { | 113 { |
114 m_suspended = false; | 114 m_suspended = false; |
115 setRunState(Running, monotonicTime); | 115 setRunState(Running, monotonicTime); |
116 } | 116 } |
117 | 117 |
118 bool ActiveAnimation::isFinishedAt(double monotonicTime) const | 118 bool Animation::isFinishedAt(double monotonicTime) const |
119 { | 119 { |
120 if (isFinished()) | 120 if (isFinished()) |
121 return true; | 121 return true; |
122 | 122 |
123 if (m_needsSynchronizedStartTime) | 123 if (m_needsSynchronizedStartTime) |
124 return false; | 124 return false; |
125 | 125 |
126 return m_runState == Running | 126 return m_runState == Running |
127 && m_iterations >= 0 | 127 && m_iterations >= 0 |
128 && m_iterations * m_curve->duration() <= monotonicTime - startTime() - m
_totalPausedTime; | 128 && m_iterations * m_curve->duration() <= monotonicTime - startTime() - m
_totalPausedTime; |
129 } | 129 } |
130 | 130 |
131 double ActiveAnimation::trimTimeToCurrentIteration(double monotonicTime) const | 131 double Animation::trimTimeToCurrentIteration(double monotonicTime) const |
132 { | 132 { |
133 double trimmed = monotonicTime + m_timeOffset; | 133 double trimmed = monotonicTime + m_timeOffset; |
134 | 134 |
135 // If we're paused, time is 'stuck' at the pause time. | 135 // If we're paused, time is 'stuck' at the pause time. |
136 if (m_runState == Paused) | 136 if (m_runState == Paused) |
137 trimmed = m_pauseTime; | 137 trimmed = m_pauseTime; |
138 | 138 |
139 // Returned time should always be relative to the start time and should subt
ract | 139 // Returned time should always be relative to the start time and should subt
ract |
140 // all time spent paused. | 140 // all time spent paused. |
141 trimmed -= m_startTime + m_totalPausedTime; | 141 trimmed -= m_startTime + m_totalPausedTime; |
(...skipping 27 matching lines...) Expand all Loading... |
169 // Calculate x where trimmed = x + n * m_curve->duration() for some positive
integer n. | 169 // Calculate x where trimmed = x + n * m_curve->duration() for some positive
integer n. |
170 trimmed = fmod(trimmed, m_curve->duration()); | 170 trimmed = fmod(trimmed, m_curve->duration()); |
171 | 171 |
172 // If we're alternating and on an odd iteration, reverse the direction. | 172 // If we're alternating and on an odd iteration, reverse the direction. |
173 if (m_alternatesDirection && iteration % 2 == 1) | 173 if (m_alternatesDirection && iteration % 2 == 1) |
174 return m_curve->duration() - trimmed; | 174 return m_curve->duration() - trimmed; |
175 | 175 |
176 return trimmed; | 176 return trimmed; |
177 } | 177 } |
178 | 178 |
179 scoped_ptr<ActiveAnimation> ActiveAnimation::clone(InstanceType instanceType) co
nst | 179 scoped_ptr<Animation> Animation::clone(InstanceType instanceType) const |
180 { | 180 { |
181 return cloneAndInitialize(instanceType, m_runState, m_startTime); | 181 return cloneAndInitialize(instanceType, m_runState, m_startTime); |
182 } | 182 } |
183 | 183 |
184 scoped_ptr<ActiveAnimation> ActiveAnimation::cloneAndInitialize(InstanceType ins
tanceType, RunState initialRunState, double startTime) const | 184 scoped_ptr<Animation> Animation::cloneAndInitialize(InstanceType instanceType, R
unState initialRunState, double startTime) const |
185 { | 185 { |
186 scoped_ptr<ActiveAnimation> toReturn(new ActiveAnimation(m_curve->clone(), m
_id, m_group, m_targetProperty)); | 186 scoped_ptr<Animation> toReturn(new Animation(m_curve->clone(), m_id, m_group
, m_targetProperty)); |
187 toReturn->m_runState = initialRunState; | 187 toReturn->m_runState = initialRunState; |
188 toReturn->m_iterations = m_iterations; | 188 toReturn->m_iterations = m_iterations; |
189 toReturn->m_startTime = startTime; | 189 toReturn->m_startTime = startTime; |
190 toReturn->m_pauseTime = m_pauseTime; | 190 toReturn->m_pauseTime = m_pauseTime; |
191 toReturn->m_totalPausedTime = m_totalPausedTime; | 191 toReturn->m_totalPausedTime = m_totalPausedTime; |
192 toReturn->m_timeOffset = m_timeOffset; | 192 toReturn->m_timeOffset = m_timeOffset; |
193 toReturn->m_alternatesDirection = m_alternatesDirection; | 193 toReturn->m_alternatesDirection = m_alternatesDirection; |
194 toReturn->m_isControllingInstance = instanceType == ControllingInstance; | 194 toReturn->m_isControllingInstance = instanceType == ControllingInstance; |
195 return toReturn.Pass(); | 195 return toReturn.Pass(); |
196 } | 196 } |
197 | 197 |
198 void ActiveAnimation::pushPropertiesTo(ActiveAnimation* other) const | 198 void Animation::pushPropertiesTo(Animation* other) const |
199 { | 199 { |
200 // Currently, we only push changes due to pausing and resuming animations on
the main thread. | 200 // Currently, we only push changes due to pausing and resuming animations on
the main thread. |
201 if (m_runState == ActiveAnimation::Paused || other->m_runState == ActiveAnim
ation::Paused) { | 201 if (m_runState == Animation::Paused || other->m_runState == Animation::Pause
d) { |
202 other->m_runState = m_runState; | 202 other->m_runState = m_runState; |
203 other->m_pauseTime = m_pauseTime; | 203 other->m_pauseTime = m_pauseTime; |
204 other->m_totalPausedTime = m_totalPausedTime; | 204 other->m_totalPausedTime = m_totalPausedTime; |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 } // namespace cc | 208 } // namespace cc |
OLD | NEW |