Chromium Code Reviews| Index: cc/animation.cc |
| diff --git a/cc/animation.cc b/cc/animation.cc |
| index 2dec58d71f82ddda9feda68a557b67d6a8866f82..83876d856e8a62acbdc32c67aacf20e67530f170 100644 |
| --- a/cc/animation.cc |
| +++ b/cc/animation.cc |
| @@ -14,198 +14,221 @@ namespace { |
| // This should match the RunState enum. |
| static const char* const s_runStateNames[] = { |
| - "WaitingForNextTick", |
| - "WaitingForTargetAvailability", |
| - "WaitingForStartTime", |
| - "WaitingForDeletion", |
| - "Starting", |
| - "Running", |
| - "Paused", |
| - "Finished", |
| - "Aborted" |
| + "WaitingForNextTick", |
| + "WaitingForTargetAvailability", |
| + "WaitingForStartTime", |
| + "WaitingForDeletion", |
| + "Starting", |
| + "Running", |
| + "Paused", |
| + "Finished", |
| + "Aborted" |
| }; |
| -COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == arraysize(s_runStateNames), RunState_names_match_enum); |
| +COMPILE_ASSERT(static_cast<int>(cc::Animation::RunStateEnumSize) == |
| + arraysize(s_runStateNames), |
| + RunState_names_match_enum); |
| // This should match the TargetProperty enum. |
| static const char* const s_targetPropertyNames[] = { |
| - "Transform", |
| - "Opacity" |
| + "Transform", |
| + "Opacity" |
| }; |
| -COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == arraysize(s_targetPropertyNames), TargetProperty_names_match_enum); |
| - |
| -} // namespace |
| - |
| -namespace cc { |
| - |
| -scoped_ptr<Animation> Animation::create(scoped_ptr<AnimationCurve> curve, int animationId, int groupId, TargetProperty targetProperty) |
| -{ |
| - return make_scoped_ptr(new Animation(curve.Pass(), animationId, groupId, targetProperty)); |
| +COMPILE_ASSERT(static_cast<int>(cc::Animation::TargetPropertyEnumSize) == |
| + arraysize(s_targetPropertyNames), |
| + TargetProperty_names_match_enum); |
| + |
| +} // namespace |
| + |
| +namespace cc { scoped_ptr<Animation> Animation::Create( |
|
jamesr
2013/03/09 02:44:56
namespace cc { should be on its own line. probably
danakj
2013/03/09 03:01:11
oops, bad script. thanks.
|
| + scoped_ptr<AnimationCurve> curve, |
| + int animation_id, |
| + int group_id, |
| + TargetProperty target_property) { |
| + return make_scoped_ptr(new Animation(curve.Pass(), |
| + animation_id, |
| + group_id, |
| + target_property)); } |
| + |
| +Animation::Animation(scoped_ptr<AnimationCurve> curve, |
| + int animation_id, |
| + int group_id, |
| + TargetProperty target_property) |
| + : curve_(curve.Pass()), |
| + id_(animation_id), |
| + group_(group_id), |
| + target_property_(target_property), |
| + run_state_(WaitingForTargetAvailability), |
| + iterations_(1), |
| + start_time_(0), |
| + alternates_direction_(false), |
| + time_offset_(0), |
| + needs_synchronized_start_time_(false), |
| + suspended_(false), |
| + pause_time_(0), |
| + total_paused_time_(0), |
| + is_controlling_instance_(false), |
| + is_impl_only_(false) {} |
| + |
| +Animation::~Animation() { |
| + if (run_state_ == Running || run_state_ == Paused) |
| + SetRunState(Aborted, 0); |
| } |
| -Animation::Animation(scoped_ptr<AnimationCurve> curve, int animationId, int groupId, TargetProperty targetProperty) |
| - : m_curve(curve.Pass()) |
| - , m_id(animationId) |
| - , m_group(groupId) |
| - , m_targetProperty(targetProperty) |
| - , m_runState(WaitingForTargetAvailability) |
| - , m_iterations(1) |
| - , m_startTime(0) |
| - , m_alternatesDirection(false) |
| - , m_timeOffset(0) |
| - , m_needsSynchronizedStartTime(false) |
| - , m_suspended(false) |
| - , m_pauseTime(0) |
| - , m_totalPausedTime(0) |
| - , m_isControllingInstance(false) |
| - , m_isImplOnly(false) |
| -{ |
| +void Animation::SetRunState(RunState run_state, double monotonic_time) { |
| + if (suspended_) |
| + return; |
| + |
| + char nameBuffer[256]; |
| + base::snprintf(nameBuffer, |
| + sizeof(nameBuffer), |
| + "%s-%d%s", |
| + s_targetPropertyNames[target_property_], |
| + group_, |
| + is_controlling_instance_ ? "(impl)" : ""); |
| + |
| + bool is_waiting_to_start = run_state_ == WaitingForNextTick || |
| + run_state_ == WaitingForTargetAvailability || |
| + run_state_ == WaitingForStartTime || |
| + run_state_ == Starting; |
| + |
| + if (is_waiting_to_start && run_state == Running) { |
| + TRACE_EVENT_ASYNC_BEGIN1( |
| + "cc", "Animation", this, "Name", TRACE_STR_COPY(nameBuffer)); |
| + } |
| + |
| + bool was_finished = is_finished(); |
| + |
| + const char* old_run_state_name = s_runStateNames[run_state_]; |
| + |
| + if (run_state == Running && run_state_ == Paused) |
| + total_paused_time_ += monotonic_time - pause_time_; |
| + else if (run_state == Paused) |
| + pause_time_ = monotonic_time; |
| + run_state_ = run_state; |
| + |
| + const char* new_run_state_name = s_runStateNames[run_state]; |
| + |
| + if (!was_finished && is_finished()) |
| + TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
| + |
| + char stateBuffer[256]; |
| + base::snprintf(stateBuffer, |
| + sizeof(stateBuffer), |
| + "%s->%s", |
| + old_run_state_name, |
| + new_run_state_name); |
| + |
| + TRACE_EVENT_INSTANT2("cc", |
| + "LayerAnimationController::setRunState", |
| + "Name", |
| + TRACE_STR_COPY(nameBuffer), |
| + "State", |
| + TRACE_STR_COPY(stateBuffer)); |
| } |
| -Animation::~Animation() |
| -{ |
| - if (m_runState == Running || m_runState == Paused) |
| - setRunState(Aborted, 0); |
| +void Animation::Suspend(double monotonic_time) { |
| + SetRunState(Paused, monotonic_time); |
| + suspended_ = true; |
| } |
| -void Animation::setRunState(RunState runState, double monotonicTime) |
| -{ |
| - if (m_suspended) |
| - return; |
| - |
| - char nameBuffer[256]; |
| - base::snprintf(nameBuffer, sizeof(nameBuffer), "%s-%d%s", s_targetPropertyNames[m_targetProperty], m_group, m_isControllingInstance ? "(impl)" : ""); |
| - |
| - bool isWaitingToStart = m_runState == WaitingForNextTick |
| - || m_runState == WaitingForTargetAvailability |
| - || m_runState == WaitingForStartTime |
| - || m_runState == Starting; |
| - |
| - if (isWaitingToStart && runState == Running) |
| - TRACE_EVENT_ASYNC_BEGIN1("cc", "Animation", this, "Name", TRACE_STR_COPY(nameBuffer)); |
| - |
| - bool wasFinished = isFinished(); |
| - |
| - const char* oldRunStateName = s_runStateNames[m_runState]; |
| - |
| - if (runState == Running && m_runState == Paused) |
| - m_totalPausedTime += monotonicTime - m_pauseTime; |
| - else if (runState == Paused) |
| - m_pauseTime = monotonicTime; |
| - m_runState = runState; |
| - |
| - const char* newRunStateName = s_runStateNames[runState]; |
| - |
| - if (!wasFinished && isFinished()) |
| - TRACE_EVENT_ASYNC_END0("cc", "Animation", this); |
| - |
| - char stateBuffer[256]; |
| - base::snprintf(stateBuffer, sizeof(stateBuffer), "%s->%s", oldRunStateName, newRunStateName); |
| - |
| - TRACE_EVENT_INSTANT2("cc", "LayerAnimationController::setRunState", "Name", TRACE_STR_COPY(nameBuffer), "State", TRACE_STR_COPY(stateBuffer)); |
| +void Animation::Resume(double monotonic_time) { |
| + suspended_ = false; |
| + SetRunState(Running, monotonic_time); |
| } |
| -void Animation::suspend(double monotonicTime) |
| -{ |
| - setRunState(Paused, monotonicTime); |
| - m_suspended = true; |
| -} |
| +bool Animation::IsFinishedAt(double monotonic_time) const { |
| + if (is_finished()) |
| + return true; |
| -void Animation::resume(double monotonicTime) |
| -{ |
| - m_suspended = false; |
| - setRunState(Running, monotonicTime); |
| -} |
| + if (needs_synchronized_start_time_) |
| + return false; |
| -bool Animation::isFinishedAt(double monotonicTime) const |
| -{ |
| - if (isFinished()) |
| - return true; |
| - |
| - if (m_needsSynchronizedStartTime) |
| - return false; |
| - |
| - return m_runState == Running |
| - && m_iterations >= 0 |
| - && m_iterations * m_curve->duration() <= monotonicTime - startTime() - m_totalPausedTime; |
| + return run_state_ == Running && |
| + iterations_ >= 0 && |
| + iterations_ * curve_->Duration() <= (monotonic_time - |
| + start_time() - |
| + total_paused_time_); |
| } |
| -double Animation::trimTimeToCurrentIteration(double monotonicTime) const |
| -{ |
| - double trimmed = monotonicTime + m_timeOffset; |
| +double Animation::TrimTimeToCurrentIteration(double monotonic_time) const { |
| + double trimmed = monotonic_time + time_offset_; |
| - // If we're paused, time is 'stuck' at the pause time. |
| - if (m_runState == Paused) |
| - trimmed = m_pauseTime; |
| + // If we're paused, time is 'stuck' at the pause time. |
| + if (run_state_ == Paused) |
| + trimmed = pause_time_; |
| - // Returned time should always be relative to the start time and should subtract |
| - // all time spent paused. |
| - trimmed -= m_startTime + m_totalPausedTime; |
| + // Returned time should always be relative to the start time and should |
| + // subtract all time spent paused. |
| + trimmed -= start_time_ + total_paused_time_; |
| - // Zero is always the start of the animation. |
| - if (trimmed <= 0) |
| - return 0; |
| + // Zero is always the start of the animation. |
| + if (trimmed <= 0) |
| + return 0; |
| - // Always return zero if we have no iterations. |
| - if (!m_iterations) |
| - return 0; |
| + // Always return zero if we have no iterations. |
| + if (!iterations_) |
| + return 0; |
| - // Don't attempt to trim if we have no duration. |
| - if (m_curve->duration() <= 0) |
| - return 0; |
| + // Don't attempt to trim if we have no duration. |
| + if (curve_->Duration() <= 0) |
| + return 0; |
| - // If less than an iteration duration, just return trimmed. |
| - if (trimmed < m_curve->duration()) |
| - return trimmed; |
| + // If less than an iteration duration, just return trimmed. |
| + if (trimmed < curve_->Duration()) |
| + return trimmed; |
| - // If greater than or equal to the total duration, return iteration duration. |
| - if (m_iterations >= 0 && trimmed >= m_curve->duration() * m_iterations) { |
| - if (m_alternatesDirection && !(m_iterations % 2)) |
| - return 0; |
| - return m_curve->duration(); |
| - } |
| + // If greater than or equal to the total duration, return iteration duration. |
| + if (iterations_ >= 0 && trimmed >= curve_->Duration() * iterations_) { |
| + if (alternates_direction_ && !(iterations_ % 2)) |
| + return 0; |
| + return curve_->Duration(); |
| + } |
| - // We need to know the current iteration if we're alternating. |
| - int iteration = static_cast<int>(trimmed / m_curve->duration()); |
| + // We need to know the current iteration if we're alternating. |
| + int iteration = static_cast<int>(trimmed / curve_->Duration()); |
| - // Calculate x where trimmed = x + n * m_curve->duration() for some positive integer n. |
| - trimmed = fmod(trimmed, m_curve->duration()); |
| + // Calculate x where trimmed = x + n * curve_->Duration() for some positive |
| + // integer n. |
| + trimmed = fmod(trimmed, curve_->Duration()); |
| - // If we're alternating and on an odd iteration, reverse the direction. |
| - if (m_alternatesDirection && iteration % 2 == 1) |
| - return m_curve->duration() - trimmed; |
| + // If we're alternating and on an odd iteration, reverse the direction. |
| + if (alternates_direction_ && iteration % 2 == 1) |
| + return curve_->Duration() - trimmed; |
| - return trimmed; |
| + return trimmed; |
| } |
| -scoped_ptr<Animation> Animation::clone(InstanceType instanceType) const |
| -{ |
| - return cloneAndInitialize(instanceType, m_runState, m_startTime); |
| +scoped_ptr<Animation> Animation::Clone(InstanceType instance_type) const { |
| + return CloneAndInitialize(instance_type, run_state_, start_time_); |
| } |
| -scoped_ptr<Animation> Animation::cloneAndInitialize(InstanceType instanceType, RunState initialRunState, double startTime) const |
| -{ |
| - scoped_ptr<Animation> toReturn(new Animation(m_curve->clone(), m_id, m_group, m_targetProperty)); |
| - toReturn->m_runState = initialRunState; |
| - toReturn->m_iterations = m_iterations; |
| - toReturn->m_startTime = startTime; |
| - toReturn->m_pauseTime = m_pauseTime; |
| - toReturn->m_totalPausedTime = m_totalPausedTime; |
| - toReturn->m_timeOffset = m_timeOffset; |
| - toReturn->m_alternatesDirection = m_alternatesDirection; |
| - toReturn->m_isControllingInstance = instanceType == ControllingInstance; |
| - return toReturn.Pass(); |
| +scoped_ptr<Animation> Animation::CloneAndInitialize(InstanceType instance_type, |
| + RunState initial_run_state, |
| + double start_time) const { |
| + scoped_ptr<Animation> to_return( |
| + new Animation(curve_->Clone(), id_, group_, target_property_)); |
| + to_return->run_state_ = initial_run_state; |
| + to_return->iterations_ = iterations_; |
| + to_return->start_time_ = start_time; |
| + to_return->pause_time_ = pause_time_; |
| + to_return->total_paused_time_ = total_paused_time_; |
| + to_return->time_offset_ = time_offset_; |
| + to_return->alternates_direction_ = alternates_direction_; |
| + to_return->is_controlling_instance_ = instance_type == ControllingInstance; |
| + return to_return.Pass(); |
| } |
| -void Animation::pushPropertiesTo(Animation* other) const |
| -{ |
| - // Currently, we only push changes due to pausing and resuming animations on the main thread. |
| - if (m_runState == Animation::Paused || other->m_runState == Animation::Paused) { |
| - other->m_runState = m_runState; |
| - other->m_pauseTime = m_pauseTime; |
| - other->m_totalPausedTime = m_totalPausedTime; |
| - } |
| +void Animation::PushPropertiesTo(Animation* other) const { |
| + // Currently, we only push changes due to pausing and resuming animations on |
| + // the main thread. |
| + if (run_state_ == Animation::Paused || |
| + other->run_state_ == Animation::Paused) { |
| + other->run_state_ = run_state_; |
| + other->pause_time_ = pause_time_; |
| + other->total_paused_time_ = total_paused_time_; |
| + } |
| } |
| } // namespace cc |