| 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/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 "Starting", | 21 "Starting", |
| 22 "Running", | 22 "Running", |
| 23 "Paused", | 23 "Paused", |
| 24 "Finished", | 24 "Finished", |
| 25 "Aborted" | 25 "Aborted" |
| 26 }; | 26 }; |
| 27 | 27 |
| 28 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == arraysize(s_
runStateNames), RunState_names_match_enum); | 28 COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == |
| 29 arraysize(s_runStateNames), |
| 30 RunState_names_match_enum); |
| 29 | 31 |
| 30 // This should match the TargetProperty enum. | 32 // This should match the TargetProperty enum. |
| 31 static const char* const s_targetPropertyNames[] = { | 33 static const char* const s_targetPropertyNames[] = { |
| 32 "Transform", | 34 "Transform", |
| 33 "Opacity" | 35 "Opacity" |
| 34 }; | 36 }; |
| 35 | 37 |
| 36 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == arrays
ize(s_targetPropertyNames), TargetProperty_names_match_enum); | 38 COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == |
| 37 | 39 arraysize(s_targetPropertyNames), |
| 38 } // namespace | 40 TargetProperty_names_match_enum); |
| 41 |
| 42 } // namespace |
| 39 | 43 |
| 40 namespace cc { | 44 namespace cc { |
| 41 | 45 |
| 42 scoped_ptr<Animation> Animation::create(scoped_ptr<AnimationCurve> curve, int an
imationId, int groupId, TargetProperty targetProperty) | 46 scoped_ptr<Animation> Animation::Create( |
| 43 { | 47 scoped_ptr<AnimationCurve> curve, |
| 44 return make_scoped_ptr(new Animation(curve.Pass(), animationId, groupId, tar
getProperty)); | 48 int animation_id, |
| 45 } | 49 int group_id, |
| 46 | 50 TargetProperty target_property) { |
| 47 Animation::Animation(scoped_ptr<AnimationCurve> curve, int animationId, int grou
pId, TargetProperty targetProperty) | 51 return make_scoped_ptr(new Animation(curve.Pass(), |
| 48 : m_curve(curve.Pass()) | 52 animation_id, |
| 49 , m_id(animationId) | 53 group_id, |
| 50 , m_group(groupId) | 54 target_property)); } |
| 51 , m_targetProperty(targetProperty) | 55 |
| 52 , m_runState(WaitingForTargetAvailability) | 56 Animation::Animation(scoped_ptr<AnimationCurve> curve, |
| 53 , m_iterations(1) | 57 int animation_id, |
| 54 , m_startTime(0) | 58 int group_id, |
| 55 , m_alternatesDirection(false) | 59 TargetProperty target_property) |
| 56 , m_timeOffset(0) | 60 : curve_(curve.Pass()), |
| 57 , m_needsSynchronizedStartTime(false) | 61 id_(animation_id), |
| 58 , m_suspended(false) | 62 group_(group_id), |
| 59 , m_pauseTime(0) | 63 target_property_(target_property), |
| 60 , m_totalPausedTime(0) | 64 run_state_(WaitingForTargetAvailability), |
| 61 , m_isControllingInstance(false) | 65 iterations_(1), |
| 62 , m_isImplOnly(false) | 66 start_time_(0), |
| 63 { | 67 alternates_direction_(false), |
| 64 } | 68 time_offset_(0), |
| 65 | 69 needs_synchronized_start_time_(false), |
| 66 Animation::~Animation() | 70 suspended_(false), |
| 67 { | 71 pause_time_(0), |
| 68 if (m_runState == Running || m_runState == Paused) | 72 total_paused_time_(0), |
| 69 setRunState(Aborted, 0); | 73 is_controlling_instance_(false), |
| 70 } | 74 is_impl_only_(false) {} |
| 71 | 75 |
| 72 void Animation::setRunState(RunState runState, double monotonicTime) | 76 Animation::~Animation() { |
| 73 { | 77 if (run_state_ == Running || run_state_ == Paused) |
| 74 if (m_suspended) | 78 SetRunState(Aborted, 0); |
| 75 return; | 79 } |
| 76 | 80 |
| 77 char nameBuffer[256]; | 81 void Animation::SetRunState(RunState run_state, double monotonic_time) { |
| 78 base::snprintf(nameBuffer, sizeof(nameBuffer), "%s-%d%s", s_targetPropertyNa
mes[m_targetProperty], m_group, m_isControllingInstance ? "(impl)" : ""); | 82 if (suspended_) |
| 79 | 83 return; |
| 80 bool isWaitingToStart = m_runState == WaitingForNextTick | 84 |
| 81 || m_runState == WaitingForTargetAvailability | 85 char nameBuffer[256]; |
| 82 || m_runState == WaitingForStartTime | 86 base::snprintf(nameBuffer, |
| 83 || m_runState == Starting; | 87 sizeof(nameBuffer), |
| 84 | 88 "%s-%d%s", |
| 85 if (isWaitingToStart && runState == Running) | 89 s_targetPropertyNames[target_property_], |
| 86 TRACE_EVENT_ASYNC_BEGIN1("cc", "Animation", this, "Name", TRACE_STR_COPY
(nameBuffer)); | 90 group_, |
| 87 | 91 is_controlling_instance_ ? "(impl)" : ""); |
| 88 bool wasFinished = isFinished(); | 92 |
| 89 | 93 bool is_waiting_to_start = run_state_ == WaitingForNextTick || |
| 90 const char* oldRunStateName = s_runStateNames[m_runState]; | 94 run_state_ == WaitingForTargetAvailability || |
| 91 | 95 run_state_ == WaitingForStartTime || |
| 92 if (runState == Running && m_runState == Paused) | 96 run_state_ == Starting; |
| 93 m_totalPausedTime += monotonicTime - m_pauseTime; | 97 |
| 94 else if (runState == Paused) | 98 if (is_waiting_to_start && run_state == Running) { |
| 95 m_pauseTime = monotonicTime; | 99 TRACE_EVENT_ASYNC_BEGIN1( |
| 96 m_runState = runState; | 100 "cc", "Animation", this, "Name", TRACE_STR_COPY(nameBuffer)); |
| 97 | 101 } |
| 98 const char* newRunStateName = s_runStateNames[runState]; | 102 |
| 99 | 103 bool was_finished = is_finished(); |
| 100 if (!wasFinished && isFinished()) | 104 |
| 101 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); | 105 const char* old_run_state_name = s_runStateNames[run_state_]; |
| 102 | 106 |
| 103 char stateBuffer[256]; | 107 if (run_state == Running && run_state_ == Paused) |
| 104 base::snprintf(stateBuffer, sizeof(stateBuffer), "%s->%s", oldRunStateName,
newRunStateName); | 108 total_paused_time_ += monotonic_time - pause_time_; |
| 105 | 109 else if (run_state == Paused) |
| 106 TRACE_EVENT_INSTANT2("cc", "LayerAnimationController::setRunState", "Name",
TRACE_STR_COPY(nameBuffer), "State", TRACE_STR_COPY(stateBuffer)); | 110 pause_time_ = monotonic_time; |
| 107 } | 111 run_state_ = run_state; |
| 108 | 112 |
| 109 void Animation::suspend(double monotonicTime) | 113 const char* new_run_state_name = s_runStateNames[run_state]; |
| 110 { | 114 |
| 111 setRunState(Paused, monotonicTime); | 115 if (!was_finished && is_finished()) |
| 112 m_suspended = true; | 116 TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
| 113 } | 117 |
| 114 | 118 char stateBuffer[256]; |
| 115 void Animation::resume(double monotonicTime) | 119 base::snprintf(stateBuffer, |
| 116 { | 120 sizeof(stateBuffer), |
| 117 m_suspended = false; | 121 "%s->%s", |
| 118 setRunState(Running, monotonicTime); | 122 old_run_state_name, |
| 119 } | 123 new_run_state_name); |
| 120 | 124 |
| 121 bool Animation::isFinishedAt(double monotonicTime) const | 125 TRACE_EVENT_INSTANT2("cc", |
| 122 { | 126 "LayerAnimationController::setRunState", |
| 123 if (isFinished()) | 127 "Name", |
| 124 return true; | 128 TRACE_STR_COPY(nameBuffer), |
| 125 | 129 "State", |
| 126 if (m_needsSynchronizedStartTime) | 130 TRACE_STR_COPY(stateBuffer)); |
| 127 return false; | 131 } |
| 128 | 132 |
| 129 return m_runState == Running | 133 void Animation::Suspend(double monotonic_time) { |
| 130 && m_iterations >= 0 | 134 SetRunState(Paused, monotonic_time); |
| 131 && m_iterations * m_curve->duration() <= monotonicTime - startTime() - m
_totalPausedTime; | 135 suspended_ = true; |
| 132 } | 136 } |
| 133 | 137 |
| 134 double Animation::trimTimeToCurrentIteration(double monotonicTime) const | 138 void Animation::Resume(double monotonic_time) { |
| 135 { | 139 suspended_ = false; |
| 136 double trimmed = monotonicTime + m_timeOffset; | 140 SetRunState(Running, monotonic_time); |
| 137 | 141 } |
| 138 // If we're paused, time is 'stuck' at the pause time. | 142 |
| 139 if (m_runState == Paused) | 143 bool Animation::IsFinishedAt(double monotonic_time) const { |
| 140 trimmed = m_pauseTime; | 144 if (is_finished()) |
| 141 | 145 return true; |
| 142 // Returned time should always be relative to the start time and should subt
ract | 146 |
| 143 // all time spent paused. | 147 if (needs_synchronized_start_time_) |
| 144 trimmed -= m_startTime + m_totalPausedTime; | 148 return false; |
| 145 | 149 |
| 146 // Zero is always the start of the animation. | 150 return run_state_ == Running && |
| 147 if (trimmed <= 0) | 151 iterations_ >= 0 && |
| 148 return 0; | 152 iterations_ * curve_->Duration() <= (monotonic_time - |
| 149 | 153 start_time() - |
| 150 // Always return zero if we have no iterations. | 154 total_paused_time_); |
| 151 if (!m_iterations) | 155 } |
| 152 return 0; | 156 |
| 153 | 157 double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { |
| 154 // Don't attempt to trim if we have no duration. | 158 double trimmed = monotonic_time + time_offset_; |
| 155 if (m_curve->duration() <= 0) | 159 |
| 156 return 0; | 160 // If we're paused, time is 'stuck' at the pause time. |
| 157 | 161 if (run_state_ == Paused) |
| 158 // If less than an iteration duration, just return trimmed. | 162 trimmed = pause_time_; |
| 159 if (trimmed < m_curve->duration()) | 163 |
| 160 return trimmed; | 164 // Returned time should always be relative to the start time and should |
| 161 | 165 // subtract all time spent paused. |
| 162 // If greater than or equal to the total duration, return iteration duration
. | 166 trimmed -= start_time_ + total_paused_time_; |
| 163 if (m_iterations >= 0 && trimmed >= m_curve->duration() * m_iterations) { | 167 |
| 164 if (m_alternatesDirection && !(m_iterations % 2)) | 168 // Zero is always the start of the animation. |
| 165 return 0; | 169 if (trimmed <= 0) |
| 166 return m_curve->duration(); | 170 return 0; |
| 167 } | 171 |
| 168 | 172 // Always return zero if we have no iterations. |
| 169 // We need to know the current iteration if we're alternating. | 173 if (!iterations_) |
| 170 int iteration = static_cast<int>(trimmed / m_curve->duration()); | 174 return 0; |
| 171 | 175 |
| 172 // Calculate x where trimmed = x + n * m_curve->duration() for some positive
integer n. | 176 // Don't attempt to trim if we have no duration. |
| 173 trimmed = fmod(trimmed, m_curve->duration()); | 177 if (curve_->Duration() <= 0) |
| 174 | 178 return 0; |
| 175 // If we're alternating and on an odd iteration, reverse the direction. | 179 |
| 176 if (m_alternatesDirection && iteration % 2 == 1) | 180 // If less than an iteration duration, just return trimmed. |
| 177 return m_curve->duration() - trimmed; | 181 if (trimmed < curve_->Duration()) |
| 178 | |
| 179 return trimmed; | 182 return trimmed; |
| 180 } | 183 |
| 181 | 184 // If greater than or equal to the total duration, return iteration duration. |
| 182 scoped_ptr<Animation> Animation::clone(InstanceType instanceType) const | 185 if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) { |
| 183 { | 186 if (alternates_direction_ && !(iterations_ % 2)) |
| 184 return cloneAndInitialize(instanceType, m_runState, m_startTime); | 187 return 0; |
| 185 } | 188 return curve_->Duration(); |
| 186 | 189 } |
| 187 scoped_ptr<Animation> Animation::cloneAndInitialize(InstanceType instanceType, R
unState initialRunState, double startTime) const | 190 |
| 188 { | 191 // We need to know the current iteration if we're alternating. |
| 189 scoped_ptr<Animation> toReturn(new Animation(m_curve->clone(), m_id, m_group
, m_targetProperty)); | 192 int iteration = static_cast<int>(trimmed / curve_->Duration()); |
| 190 toReturn->m_runState = initialRunState; | 193 |
| 191 toReturn->m_iterations = m_iterations; | 194 // Calculate x where trimmed = x + n * curve_->Duration() for some positive |
| 192 toReturn->m_startTime = startTime; | 195 // integer n. |
| 193 toReturn->m_pauseTime = m_pauseTime; | 196 trimmed = fmod(trimmed, curve_->Duration()); |
| 194 toReturn->m_totalPausedTime = m_totalPausedTime; | 197 |
| 195 toReturn->m_timeOffset = m_timeOffset; | 198 // If we're alternating and on an odd iteration, reverse the direction. |
| 196 toReturn->m_alternatesDirection = m_alternatesDirection; | 199 if (alternates_direction_ && iteration % 2 == 1) |
| 197 toReturn->m_isControllingInstance = instanceType == ControllingInstance; | 200 return curve_->Duration() - trimmed; |
| 198 return toReturn.Pass(); | 201 |
| 199 } | 202 return trimmed; |
| 200 | 203 } |
| 201 void Animation::pushPropertiesTo(Animation* other) const | 204 |
| 202 { | 205 scoped_ptr<Animation> Animation::Clone(InstanceType instance_type) const { |
| 203 // Currently, we only push changes due to pausing and resuming animations on
the main thread. | 206 return CloneAndInitialize(instance_type, run_state_, start_time_); |
| 204 if (m_runState == Animation::Paused || other->m_runState == Animation::Pause
d) { | 207 } |
| 205 other->m_runState = m_runState; | 208 |
| 206 other->m_pauseTime = m_pauseTime; | 209 scoped_ptr<Animation> Animation::CloneAndInitialize(InstanceType instance_type, |
| 207 other->m_totalPausedTime = m_totalPausedTime; | 210 RunState initial_run_state, |
| 208 } | 211 double start_time) const { |
| 212 scoped_ptr<Animation> to_return( |
| 213 new Animation(curve_->Clone(), id_, group_, target_property_)); |
| 214 to_return->run_state_ = initial_run_state; |
| 215 to_return->iterations_ = iterations_; |
| 216 to_return->start_time_ = start_time; |
| 217 to_return->pause_time_ = pause_time_; |
| 218 to_return->total_paused_time_ = total_paused_time_; |
| 219 to_return->time_offset_ = time_offset_; |
| 220 to_return->alternates_direction_ = alternates_direction_; |
| 221 to_return->is_controlling_instance_ = instance_type == ControllingInstance; |
| 222 return to_return.Pass(); |
| 223 } |
| 224 |
| 225 void Animation::PushPropertiesTo(Animation* other) const { |
| 226 // Currently, we only push changes due to pausing and resuming animations on |
| 227 // the main thread. |
| 228 if (run_state_ == Animation::Paused || |
| 229 other->run_state_ == Animation::Paused) { |
| 230 other->run_state_ = run_state_; |
| 231 other->pause_time_ = pause_time_; |
| 232 other->total_paused_time_ = total_paused_time_; |
| 233 } |
| 209 } | 234 } |
| 210 | 235 |
| 211 } // namespace cc | 236 } // namespace cc |
| OLD | NEW |