Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(727)

Unified Diff: ui/gfx/compositor/layer_animator.cc

Issue 8247009: Explicit animation support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated views desktop demo. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/compositor/layer_animator.cc
diff --git a/ui/gfx/compositor/layer_animator.cc b/ui/gfx/compositor/layer_animator.cc
index 5f2c63cebe3e75700e6aa514066550aa6456bc99..77a9d51eb8bbea2620b812464234f4cd959494f7 100644
--- a/ui/gfx/compositor/layer_animator.cc
+++ b/ui/gfx/compositor/layer_animator.cc
@@ -5,182 +5,265 @@
#include "ui/gfx/compositor/layer_animator.h"
#include "base/logging.h"
-#include "base/stl_util.h"
-#include "ui/base/animation/animation_container.h"
+#include "base/memory/scoped_ptr.h"
#include "ui/base/animation/animation.h"
-#include "ui/base/animation/tween.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/compositor/layer.h"
-#include "ui/gfx/compositor/layer_animator_delegate.h"
-#include "ui/gfx/transform.h"
-#include "ui/gfx/rect.h"
+#include "ui/gfx/compositor/layer_animation_preemption_strategy.h"
+#include "ui/gfx/compositor/layer_animation_sequence.h"
+
+namespace ui {
+
+class LayerAnimator;
namespace {
-void SetMatrixElement(SkMatrix44& matrix, int index, SkMScalar value) {
- int row = index / 4;
- int col = index % 4;
- matrix.set(row, col, value);
-}
+// TODO(vollick) match kennedy theme.
sky 2011/10/14 16:39:52 ?
+static const int kDefaultTransitionDurationMs = 250;
+static const int kDefaultFramerateHz = 50;
-SkMScalar GetMatrixElement(const SkMatrix44& matrix, int index) {
- int row = index / 4;
- int col = index % 4;
- return matrix.get(row, col);
-}
+class LayerAnimation : public Animation {
sky 2011/10/14 16:39:52 I think you'll be better off implementating Animat
+ public:
+ // The layer animation does not own the animator.
+ explicit LayerAnimation(LayerAnimator* animator)
+ : Animation(base::TimeDelta::FromMilliseconds(kDefaultFramerateHz)),
+ animator_(animator) {
+ }
+ virtual ~LayerAnimation() {}
-} // anonymous namespace
+ private:
+ // Implementation of Animation
+ double GetCurrentValue() const { return 0.0; }
+ virtual void Step(base::TimeTicks time_now) { animator_->Step(time_now); }
-namespace ui {
+ LayerAnimator* animator_;
+};
+
+} // namespace;
+
+/* static */
+LayerAnimator* LayerAnimator::CreateDefaultAnimator() {
+ return new LayerAnimator(base::TimeDelta::FromMilliseconds(0));
+}
+
+/* static */
+LayerAnimator* LayerAnimator::CreateImplicitAnimator() {
+ return new LayerAnimator(
+ base::TimeDelta::FromMilliseconds(kDefaultTransitionDurationMs));
+}
-LayerAnimator::LayerAnimator(Layer* layer)
- : layer_(layer),
- got_initial_tick_(false) {
+LayerAnimator::LayerAnimator(base::TimeDelta transition_duration)
+ : layer_(NULL),
+ preemption_strategy_(
+ LayerAnimationPreemptionStrategy::GetImmediatelySetNewTarget()),
+ transition_duration_(transition_duration),
+ ALLOW_THIS_IN_INITIALIZER_LIST(animation_(new LayerAnimation(this))) {
}
LayerAnimator::~LayerAnimator() {
+ ClearAnimations();
}
-void LayerAnimator::SetAnimation(Animation* animation) {
- animation_.reset(animation);
- if (animation_.get()) {
- static ui::AnimationContainer* container = NULL;
- if (!container) {
- container = new AnimationContainer;
- container->AddRef();
- }
- animation_->set_delegate(this);
- animation_->SetContainer(container);
- got_initial_tick_ = false;
- }
+void LayerAnimator::SetTransform(const Transform& transform) {
+ SetAnimation(new LayerAnimationSequence(
+ LayerAnimationElement::CreateTransformElement(transform,
+ transition_duration_)));
}
-void LayerAnimator::AnimateToPoint(const gfx::Point& target) {
- StopAnimating(LOCATION);
- const gfx::Rect& layer_bounds = layer_->bounds();
- if (target == layer_bounds.origin())
- return; // Already there.
+void LayerAnimator::SetBounds(const gfx::Rect& bounds) {
+ SetAnimation(new LayerAnimationSequence(
+ LayerAnimationElement::CreateBoundsElement(bounds,
+ transition_duration_)));
+}
- Params& element = elements_[LOCATION];
- element.location.target_x = target.x();
- element.location.target_y = target.y();
- element.location.start_x = layer_bounds.origin().x();
- element.location.start_y = layer_bounds.origin().y();
+void LayerAnimator::SetOpacity(float opacity) {
+ SetAnimation(new LayerAnimationSequence(
+ LayerAnimationElement::CreateOpacityElement(opacity,
+ transition_duration_)));
}
-void LayerAnimator::AnimateTransform(const Transform& transform) {
- StopAnimating(TRANSFORM);
- const Transform& layer_transform = layer_->transform();
- if (transform == layer_transform)
- return; // Already there.
+void LayerAnimator::SetLayer(Layer* layer) {
+ layer_ = layer;
+ if (!layer_) {
+ ClearAnimations();
+ }
+}
- Params& element = elements_[TRANSFORM];
- for (int i = 0; i < 16; ++i) {
- element.transform.start[i] =
- GetMatrixElement(layer_transform.matrix(), i);
- element.transform.target[i] =
- GetMatrixElement(transform.matrix(), i);
+void LayerAnimator::SetAnimation(LayerAnimationSequence* animation) {
+ if (!StartSequence(animation)) {
+ preemption_strategy_->Preempt(layer(),
+ animation,
+ &running_animations_,
+ &animation_queue_);
}
+ FinishAnyAnimationWithZeroDuration();
}
-void LayerAnimator::AnimateOpacity(float target_opacity) {
- StopAnimating(OPACITY);
- if (layer_->opacity() == target_opacity)
- return;
+void LayerAnimator::EnqueueAnimation(LayerAnimationSequence* animation) {
+ animation_queue_.push_back(make_scoped_refptr(animation));
+ if (animation_queue_.size() == 1) {
+ SetAnimation(animation);
+ }
+}
- Params& element = elements_[OPACITY];
- element.opacity.start = layer_->opacity();
- element.opacity.target = target_opacity;
+bool LayerAnimator::IsAnimating() const {
+ return running_animations_.size() > 0;
}
-gfx::Point LayerAnimator::GetTargetPoint() {
- return IsAnimating(LOCATION) ?
- gfx::Point(elements_[LOCATION].location.target_x,
- elements_[LOCATION].location.target_y) :
- layer_->bounds().origin();
+void LayerAnimator::StopAnimatingProperty(
+ LayerAnimationElement::AnimatableProperty property) {
+ while (true) {
+ RunningAnimation* running = GetRunningAnimation(property);
+ if (!running)
+ break;
+ FinishAnimation(running->sequence);
+ }
}
-float LayerAnimator::GetTargetOpacity() {
- return IsAnimating(OPACITY) ?
- elements_[OPACITY].opacity.target : layer_->opacity();
+void LayerAnimator::StopAnimating() {
+ while (IsAnimating())
+ FinishAnimation(running_animations_[0].sequence);
}
-ui::Transform LayerAnimator::GetTargetTransform() {
- if (IsAnimating(TRANSFORM)) {
- Transform transform;
- for (int i = 0; i < 16; ++i) {
- SetMatrixElement(transform.matrix(), i,
- elements_[TRANSFORM].transform.target[i]);
+void LayerAnimator::Step(base::TimeTicks now) {
+ RunningAnimations running_animations_copy = running_animations_;
+ RunningAnimations::iterator iter = running_animations_copy.begin();
+ while (iter != running_animations_copy.end()) {
+ base::TimeDelta delta = now - (*iter).start_time;
+ if (delta > (*iter).sequence->duration()) {
+ FinishAnimation((*iter).sequence);
+ } else {
+ (*iter).sequence->Progress(delta, layer());
}
- return transform;
+ ++iter;
}
- return layer_->transform();
}
-bool LayerAnimator::IsAnimating(AnimationProperty property) const {
- return elements_.count(property) > 0;
+void LayerAnimator::UpdateAnimationState() {
+ if (IsAnimating())
+ animation_->Start();
+ else
+ animation_->Stop();
+}
+
+void LayerAnimator::RemoveAnimation(LayerAnimationSequence* sequence) {
+ // First remove from running animations
+ RunningAnimations::iterator iter = running_animations_.begin();
+ while (iter != running_animations_.end()) {
+ if ((*iter).sequence == sequence) {
+ running_animations_.erase(iter);
+ break;
+ }
+ ++iter;
+ }
+
+ // Then remove from the queue
+ AnimationQueue::iterator queue_iter = animation_queue_.begin();
+ while (queue_iter != animation_queue_.end()) {
+ if ((*queue_iter).get() == sequence) {
+ animation_queue_.erase(queue_iter);
+ break;
+ }
+ ++queue_iter;
+ }
}
-bool LayerAnimator::IsRunning() const {
- return animation_.get() && animation_->is_animating();
+void LayerAnimator::FinishAnimation(LayerAnimationSequence* sequence) {
+ sequence->Progress(sequence->duration(), layer());
+ RemoveAnimation(sequence);
+ ProcessQueue();
+ UpdateAnimationState();
}
-void LayerAnimator::AnimationProgressed(const ui::Animation* animation) {
- got_initial_tick_ = true;
- for (Elements::const_iterator i = elements_.begin(); i != elements_.end();
- ++i) {
- switch (i->first) {
- case LOCATION: {
- const gfx::Rect& current_bounds(layer_->bounds());
- gfx::Rect new_bounds = animation_->CurrentValueBetween(
- gfx::Rect(gfx::Point(i->second.location.start_x,
- i->second.location.start_y),
- current_bounds.size()),
- gfx::Rect(gfx::Point(i->second.location.target_x,
- i->second.location.target_y),
- current_bounds.size()));
- delegate()->SetBoundsFromAnimator(new_bounds);
- break;
- }
+void LayerAnimator::FinishAnyAnimationWithZeroDuration() {
+ // Special case: if we've started a 0 duration animation, just finish it now
+ // and get rid of it.
+ RunningAnimations copy = running_animations_;
+ RunningAnimations::iterator iter = copy.begin();
+ while (iter != copy.end()) {
+ if ((*iter).sequence->duration() == base::TimeDelta()) {
+ (*iter).sequence->Progress((*iter).sequence->duration(), layer());
+ RemoveAnimation((*iter).sequence);
+ }
+ ++iter;
+ }
+ ProcessQueue();
+ UpdateAnimationState();
+}
- case TRANSFORM: {
- Transform transform;
- for (int j = 0; j < 16; ++j) {
- SkMScalar value = animation_->CurrentValueBetween(
- i->second.transform.start[j],
- i->second.transform.target[j]);
- SetMatrixElement(transform.matrix(), j, value);
- }
- delegate()->SetTransformFromAnimator(transform);
- break;
- }
+void LayerAnimator::ClearAnimations() {
+ RunningAnimations::iterator iter = running_animations_.begin();
+ while (iter != running_animations_.end()) {
+ (*iter).sequence->Abort();
+ ++iter;
+ }
+ running_animations_.clear();
+ animation_queue_.clear();
+ UpdateAnimationState();
+}
- case OPACITY: {
- delegate()->SetOpacityFromAnimator(animation_->CurrentValueBetween(
- i->second.opacity.start, i->second.opacity.target));
- break;
- }
+LayerAnimator::RunningAnimation* LayerAnimator::GetRunningAnimation(
+ LayerAnimationElement::AnimatableProperty property) {
+ RunningAnimations::iterator iter = running_animations_.begin();
+ while (iter != running_animations_.end()) {
+ if ((*iter).sequence->properties().find(property) !=
+ (*iter).sequence->properties().end())
+ return &(*iter);
+ ++iter;
+ }
+ return NULL;
+}
- default:
- NOTREACHED();
+void LayerAnimator::AddToQueueIfNotPresent(LayerAnimationSequence* animation) {
+ // If we don't have the animation in the queue yet, add it.
+ bool found_sequence = false;
+ AnimationQueue::iterator queue_iter = animation_queue_.begin();
+ while (queue_iter != animation_queue_.end()) {
+ if ((*queue_iter).get() == animation) {
+ found_sequence = true;
+ break;
}
+ ++queue_iter;
}
- layer_->ScheduleDraw();
+
+ if (!found_sequence)
+ animation_queue_.push_front(make_scoped_refptr(animation));
}
-void LayerAnimator::AnimationEnded(const ui::Animation* animation) {
- AnimationProgressed(animation);
+void LayerAnimator::ProcessQueue() {
+ bool started_sequence = false;
+ do {
+ started_sequence = false;
+ LayerAnimator::AnimationQueue::iterator iter = animation_queue_.begin();
+ while (iter != animation_queue_.end()) {
+ if (StartSequence((*iter).get())) {
+ started_sequence = true;
+ break;
+ }
+ ++iter;
+ }
+ } while (started_sequence);
}
-void LayerAnimator::StopAnimating(AnimationProperty property) {
- if (!IsAnimating(property))
- return;
+bool LayerAnimator::StartSequence(LayerAnimationSequence* sequence) {
+ // Ensure that no one is animating one of the sequence's properties already.
+ RunningAnimations::const_iterator iter = running_animations_.begin();
+ while (iter != running_animations_.end()) {
+ if ((*iter).sequence->HasCommonProperty(*sequence))
+ return false;
+ ++iter;
+ }
- elements_.erase(property);
-}
+ // All clear, actually start the sequence. Note: base::TimeTicks::Now has
+ // a resolution that can be as bad as 15ms. If this causes glitches in the
+ // animations, this can be switched to HighResNow() (animation uses Now()
+ // internally).
+ running_animations_.push_back(
+ RunningAnimation(sequence, base::TimeTicks::Now()));
sky 2011/10/14 16:39:52 The expectations of multiple calls to start an ani
-LayerAnimatorDelegate* LayerAnimator::delegate() {
- return static_cast<LayerAnimatorDelegate*>(layer_);
+ // Need to keep a reference to the animation.
+ AddToQueueIfNotPresent(sequence);
+ return true;
}
} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698