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

Side by Side Diff: ui/gfx/compositor/layer_animator.cc

Issue 8362006: Reland r107720 - Enable the new layer animation framework. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge Created 9 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/compositor/layer_animator.h ('k') | ui/gfx/compositor/layer_animator_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ui/gfx/compositor/layer_animator.h" 5 #include "ui/gfx/compositor/layer_animator.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "ui/base/animation/animation_container.h" 10 #include "ui/base/animation/animation_container.h"
11 #include "ui/gfx/compositor/compositor.h" 11 #include "ui/gfx/compositor/compositor.h"
12 #include "ui/gfx/compositor/layer.h" 12 #include "ui/gfx/compositor/layer.h"
13 #include "ui/gfx/compositor/layer_animation_delegate.h" 13 #include "ui/gfx/compositor/layer_animator_delegate.h"
14 #include "ui/gfx/compositor/layer_animation_sequence.h" 14 #include "ui/gfx/compositor/layer_animation_sequence.h"
15 15
16 namespace ui { 16 namespace ui {
17 17
18 class LayerAnimator; 18 class LayerAnimator;
19 19
20 namespace { 20 namespace {
21 21
22 static const base::TimeDelta kDefaultTransitionDuration = 22 static const base::TimeDelta kDefaultTransitionDuration =
23 base::TimeDelta::FromMilliseconds(250); 23 base::TimeDelta::FromMilliseconds(200);
24 24
25 static const base::TimeDelta kTimerInterval = 25 static const base::TimeDelta kTimerInterval =
26 base::TimeDelta::FromMilliseconds(10); 26 base::TimeDelta::FromMilliseconds(10);
27 27
28 } // namespace; 28 } // namespace;
29 29
30 // LayerAnimator public -------------------------------------------------------- 30 // LayerAnimator public --------------------------------------------------------
31 31
32 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration) 32 LayerAnimator::LayerAnimator(base::TimeDelta transition_duration)
33 : delegate_(NULL), 33 : delegate_(NULL),
(...skipping 14 matching lines...) Expand all
48 48
49 // static 49 // static
50 LayerAnimator* LayerAnimator::CreateImplicitAnimator() { 50 LayerAnimator* LayerAnimator::CreateImplicitAnimator() {
51 return new LayerAnimator(kDefaultTransitionDuration); 51 return new LayerAnimator(kDefaultTransitionDuration);
52 } 52 }
53 53
54 void LayerAnimator::SetTransform(const Transform& transform) { 54 void LayerAnimator::SetTransform(const Transform& transform) {
55 if (transition_duration_ == base::TimeDelta()) 55 if (transition_duration_ == base::TimeDelta())
56 delegate_->SetTransformFromAnimation(transform); 56 delegate_->SetTransformFromAnimation(transform);
57 else 57 else
58 StartAnimation(new LayerAnimationSequence( 58 StartAnimationElement(LayerAnimationElement::CreateTransformElement(
59 LayerAnimationElement::CreateTransformElement(transform, 59 transform, transition_duration_));
60 transition_duration_))); 60 }
61
62 Transform LayerAnimator::GetTargetTransform() const {
63 LayerAnimationElement::TargetValue target;
64 GetTargetValue(&target);
65 return target.transform;
61 } 66 }
62 67
63 void LayerAnimator::SetBounds(const gfx::Rect& bounds) { 68 void LayerAnimator::SetBounds(const gfx::Rect& bounds) {
64 if (transition_duration_ == base::TimeDelta()) 69 if (transition_duration_ == base::TimeDelta())
65 delegate_->SetBoundsFromAnimation(bounds); 70 delegate_->SetBoundsFromAnimation(bounds);
66 else 71 else
67 StartAnimation(new LayerAnimationSequence( 72 StartAnimationElement(LayerAnimationElement::CreateBoundsElement(
68 LayerAnimationElement::CreateBoundsElement(bounds, 73 bounds, transition_duration_));
69 transition_duration_))); 74 }
75
76 gfx::Rect LayerAnimator::GetTargetBounds() const {
77 LayerAnimationElement::TargetValue target;
78 GetTargetValue(&target);
79 return target.bounds;
70 } 80 }
71 81
72 void LayerAnimator::SetOpacity(float opacity) { 82 void LayerAnimator::SetOpacity(float opacity) {
73 if (transition_duration_ == base::TimeDelta()) 83 if (transition_duration_ == base::TimeDelta())
74 delegate_->SetOpacityFromAnimation(opacity); 84 delegate_->SetOpacityFromAnimation(opacity);
75 else 85 else
76 StartAnimation(new LayerAnimationSequence( 86 StartAnimationElement(LayerAnimationElement::CreateOpacityElement(
77 LayerAnimationElement::CreateOpacityElement(opacity, 87 opacity, transition_duration_));
78 transition_duration_)));
79 } 88 }
80 89
81 void LayerAnimator::SetDelegate(LayerAnimationDelegate* delegate) { 90 float LayerAnimator::GetTargetOpacity() const {
91 LayerAnimationElement::TargetValue target;
92 GetTargetValue(&target);
93 return target.opacity;
94 }
95
96 void LayerAnimator::SetDelegate(LayerAnimatorDelegate* delegate) {
82 DCHECK(delegate); 97 DCHECK(delegate);
83 delegate_ = delegate; 98 delegate_ = delegate;
84 } 99 }
85 100
86 void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) { 101 void LayerAnimator::StartAnimation(LayerAnimationSequence* animation) {
87 if (!StartSequenceImmediately(animation)) { 102 if (!StartSequenceImmediately(animation)) {
88 // Attempt to preempt a running animation. 103 // Attempt to preempt a running animation.
89 switch (preemption_strategy_) { 104 switch (preemption_strategy_) {
90 case IMMEDIATELY_SET_NEW_TARGET: 105 case IMMEDIATELY_SET_NEW_TARGET:
91 ImmediatelySetNewTarget(animation); 106 ImmediatelySetNewTarget(animation);
92 break; 107 break;
93 case IMMEDIATELY_ANIMATE_TO_NEW_TARGET: 108 case IMMEDIATELY_ANIMATE_TO_NEW_TARGET:
94 ImmediatelyAnimateToNewTarget(animation); 109 ImmediatelyAnimateToNewTarget(animation);
95 break; 110 break;
96 case ENQUEUE_NEW_ANIMATION: 111 case ENQUEUE_NEW_ANIMATION:
97 EnqueueNewAnimation(animation); 112 EnqueueNewAnimation(animation);
98 break; 113 break;
99 case REPLACE_QUEUED_ANIMATIONS: 114 case REPLACE_QUEUED_ANIMATIONS:
100 ReplaceQueuedAnimations(animation); 115 ReplaceQueuedAnimations(animation);
101 break; 116 break;
102 case BLEND_WITH_CURRENT_ANIMATION: { 117 case BLEND_WITH_CURRENT_ANIMATION: {
103 // TODO(vollick) Add support for blended sequences and use them here. 118 // TODO(vollick) Add support for blended sequences and use them here.
104 NOTIMPLEMENTED(); 119 NOTIMPLEMENTED();
105 break; 120 break;
106 } 121 }
107 } 122 }
108 } 123 }
109 FinishAnyAnimationWithZeroDuration(); 124 FinishAnyAnimationWithZeroDuration();
125 UpdateAnimationState();
110 } 126 }
111 127
112 void LayerAnimator::ScheduleAnimation(LayerAnimationSequence* animation) { 128 void LayerAnimator::ScheduleAnimation(LayerAnimationSequence* animation) {
113 if (is_animating()) { 129 if (is_animating()) {
114 animation_queue_.push_back(make_linked_ptr(animation)); 130 animation_queue_.push_back(make_linked_ptr(animation));
115 ProcessQueue(); 131 ProcessQueue();
116 } else { 132 } else {
117 StartSequenceImmediately(animation); 133 StartSequenceImmediately(animation);
118 } 134 }
135 UpdateAnimationState();
119 } 136 }
120 137
121 void LayerAnimator::ScheduleTogether( 138 void LayerAnimator::ScheduleTogether(
122 const std::vector<LayerAnimationSequence*>& animations) { 139 const std::vector<LayerAnimationSequence*>& animations) {
123 // Collect all the affected properties. 140 // Collect all the affected properties.
124 LayerAnimationElement::AnimatableProperties animated_properties; 141 LayerAnimationElement::AnimatableProperties animated_properties;
125 std::vector<LayerAnimationSequence*>::const_iterator iter; 142 std::vector<LayerAnimationSequence*>::const_iterator iter;
126 for (iter = animations.begin(); iter != animations.end(); ++iter) { 143 for (iter = animations.begin(); iter != animations.end(); ++iter) {
127 animated_properties.insert((*iter)->properties().begin(), 144 animated_properties.insert((*iter)->properties().begin(),
128 (*iter)->properties().end()); 145 (*iter)->properties().end());
129 } 146 }
130 147
131 // Scheduling a zero duration pause that affects all the animated properties 148 // Scheduling a zero duration pause that affects all the animated properties
132 // will prevent any of the sequences from animating until there are no 149 // will prevent any of the sequences from animating until there are no
133 // running animations that affect any of these properties. 150 // running animations that affect any of these properties.
134 ScheduleAnimation( 151 ScheduleAnimation(
135 new LayerAnimationSequence( 152 new LayerAnimationSequence(
136 LayerAnimationElement::CreatePauseElement(animated_properties, 153 LayerAnimationElement::CreatePauseElement(animated_properties,
137 base::TimeDelta()))); 154 base::TimeDelta())));
138 155
139 // These animations (provided they don't animate any common properties) will 156 // These animations (provided they don't animate any common properties) will
140 // now animate together if trivially scheduled. 157 // now animate together if trivially scheduled.
141 for (iter = animations.begin(); iter != animations.end(); ++iter) { 158 for (iter = animations.begin(); iter != animations.end(); ++iter) {
142 ScheduleAnimation(*iter); 159 ScheduleAnimation(*iter);
143 } 160 }
161
162 UpdateAnimationState();
163 }
164
165 void LayerAnimator::StartAnimationElement(LayerAnimationElement* animation) {
166 StartAnimation(new LayerAnimationSequence(animation));
167 }
168
169 void LayerAnimator::ScheduleAnimationElement(LayerAnimationElement* animation) {
170 ScheduleAnimation(new LayerAnimationSequence(animation));
171 }
172
173 void LayerAnimator::ScheduleElementsTogether(
174 const std::vector<LayerAnimationElement*>& animations) {
175 std::vector<LayerAnimationSequence*> sequences;
176 for (size_t i = 0; i < animations.size(); ++i)
177 sequences.push_back(new LayerAnimationSequence(animations[i]));
178 ScheduleTogether(sequences);
144 } 179 }
145 180
146 void LayerAnimator::StopAnimatingProperty( 181 void LayerAnimator::StopAnimatingProperty(
147 LayerAnimationElement::AnimatableProperty property) { 182 LayerAnimationElement::AnimatableProperty property) {
148 while (true) { 183 while (true) {
149 RunningAnimation* running = GetRunningAnimation(property); 184 RunningAnimation* running = GetRunningAnimation(property);
150 if (!running) 185 if (!running)
151 break; 186 break;
152 FinishAnimation(running->sequence); 187 FinishAnimation(running->sequence);
153 } 188 }
154 } 189 }
155 190
156 void LayerAnimator::StopAnimating() { 191 void LayerAnimator::StopAnimating() {
157 while (is_animating()) 192 while (is_animating())
158 FinishAnimation(running_animations_[0].sequence); 193 FinishAnimation(running_animations_[0].sequence);
159 } 194 }
160 195
196 LayerAnimator::ScopedSettings::ScopedSettings(LayerAnimator* animator)
197 : animator_(animator),
198 old_transition_duration_(animator->transition_duration_) {
199 SetTransitionDuration(kDefaultTransitionDuration);
200 }
201
202 LayerAnimator::ScopedSettings::~ScopedSettings() {
203 animator_->transition_duration_ = old_transition_duration_;
204 }
205
206 void LayerAnimator::ScopedSettings::SetTransitionDuration(
207 base::TimeDelta duration) {
208 animator_->transition_duration_ = duration;
209 }
210
161 // LayerAnimator private ------------------------------------------------------- 211 // LayerAnimator private -------------------------------------------------------
162 212
163 void LayerAnimator::Step(base::TimeTicks now) { 213 void LayerAnimator::Step(base::TimeTicks now) {
164 TRACE_EVENT0("LayerAnimator", "Step");
165 last_step_time_ = now; 214 last_step_time_ = now;
166 std::vector<LayerAnimationSequence*> to_finish; 215 std::vector<LayerAnimationSequence*> to_finish;
167 for (RunningAnimations::iterator iter = running_animations_.begin(); 216 for (RunningAnimations::iterator iter = running_animations_.begin();
168 iter != running_animations_.end(); ++iter) { 217 iter != running_animations_.end(); ++iter) {
169 base::TimeDelta delta = now - (*iter).start_time; 218 base::TimeDelta delta = now - (*iter).start_time;
170 if (delta >= (*iter).sequence->duration() && 219 if (delta >= (*iter).sequence->duration() &&
171 !(*iter).sequence->is_cyclic()) { 220 !(*iter).sequence->is_cyclic()) {
172 to_finish.push_back((*iter).sequence); 221 to_finish.push_back((*iter).sequence);
173 } else { 222 } else {
174 (*iter).sequence->Progress(delta, delegate()); 223 (*iter).sequence->Progress(delta, delegate());
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 queue_iter != animation_queue_.end(); ++queue_iter) { 270 queue_iter != animation_queue_.end(); ++queue_iter) {
222 if ((*queue_iter) == sequence) { 271 if ((*queue_iter) == sequence) {
223 animation_queue_.erase(queue_iter); 272 animation_queue_.erase(queue_iter);
224 break; 273 break;
225 } 274 }
226 } 275 }
227 } 276 }
228 277
229 void LayerAnimator::FinishAnimation(LayerAnimationSequence* sequence) { 278 void LayerAnimator::FinishAnimation(LayerAnimationSequence* sequence) {
230 sequence->Progress(sequence->duration(), delegate()); 279 sequence->Progress(sequence->duration(), delegate());
280 delegate()->OnLayerAnimationEnded(sequence);
231 RemoveAnimation(sequence); 281 RemoveAnimation(sequence);
232 ProcessQueue(); 282 ProcessQueue();
233 UpdateAnimationState(); 283 UpdateAnimationState();
234 } 284 }
235 285
236 void LayerAnimator::FinishAnyAnimationWithZeroDuration() { 286 void LayerAnimator::FinishAnyAnimationWithZeroDuration() {
237 // Special case: if we've started a 0 duration animation, just finish it now 287 // Special case: if we've started a 0 duration animation, just finish it now
238 // and get rid of it. Note at each iteration of the loop, we either increment 288 // and get rid of it. Note at each iteration of the loop, we either increment
239 // i or remove an element from running_animations_, so 289 // i or remove an element from running_animations_, so
240 // running_animations_.size() - i is always decreasing and we are always 290 // running_animations_.size() - i is always decreasing and we are always
241 // progressing towards the termination of the loop. 291 // progressing towards the termination of the loop.
242 for (size_t i = 0; i < running_animations_.size();) { 292 for (size_t i = 0; i < running_animations_.size();) {
243 if (running_animations_[i].sequence->duration() == base::TimeDelta()) { 293 if (running_animations_[i].sequence->duration() == base::TimeDelta()) {
244 running_animations_[i].sequence->Progress( 294 running_animations_[i].sequence->Progress(
245 running_animations_[i].sequence->duration(), delegate()); 295 running_animations_[i].sequence->duration(), delegate());
296 delegate()->OnLayerAnimationEnded(running_animations_[i].sequence);
246 RemoveAnimation(running_animations_[i].sequence); 297 RemoveAnimation(running_animations_[i].sequence);
247 } else { 298 } else {
248 ++i; 299 ++i;
249 } 300 }
250 } 301 }
251 ProcessQueue(); 302 ProcessQueue();
252 UpdateAnimationState(); 303 UpdateAnimationState();
253 } 304 }
254 305
255 void LayerAnimator::ClearAnimations() { 306 void LayerAnimator::ClearAnimations() {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 bool abort) { 344 bool abort) {
294 // For all the running animations, if they animate the same property, 345 // For all the running animations, if they animate the same property,
295 // progress them to the end and remove them. Note: at each iteration i is 346 // progress them to the end and remove them. Note: at each iteration i is
296 // incremented or an element is removed from the queue, so 347 // incremented or an element is removed from the queue, so
297 // animation_queue_.size() - i is always decreasing and we are always making 348 // animation_queue_.size() - i is always decreasing and we are always making
298 // progress towards the loop terminating. 349 // progress towards the loop terminating.
299 for (size_t i = 0; i < running_animations_.size();) { 350 for (size_t i = 0; i < running_animations_.size();) {
300 if (running_animations_[i].sequence->HasCommonProperty( 351 if (running_animations_[i].sequence->HasCommonProperty(
301 sequence->properties())) { 352 sequence->properties())) {
302 // Finish the animation. 353 // Finish the animation.
303 if (abort) 354 if (abort){
304 running_animations_[i].sequence->Abort(); 355 running_animations_[i].sequence->Abort();
305 else 356 } else {
306 running_animations_[i].sequence->Progress( 357 running_animations_[i].sequence->Progress(
307 running_animations_[i].sequence->duration(), delegate()); 358 running_animations_[i].sequence->duration(), delegate());
359 delegate()->OnLayerAnimationEnded(running_animations_[i].sequence);
360 }
308 RemoveAnimation(running_animations_[i].sequence); 361 RemoveAnimation(running_animations_[i].sequence);
309 } else { 362 } else {
310 ++i; 363 ++i;
311 } 364 }
312 } 365 }
313 366
314 // Same for the queued animations. See comment above about loop termination. 367 // Same for the queued animations. See comment above about loop termination.
315 for (size_t i = 0; i < animation_queue_.size();) { 368 for (size_t i = 0; i < animation_queue_.size();) {
316 if (animation_queue_[i]->HasCommonProperty(sequence->properties())) { 369 if (animation_queue_[i]->HasCommonProperty(sequence->properties())) {
317 // Finish the animation. 370 // Finish the animation.
318 if (abort) 371 if (abort)
319 animation_queue_[i]->Abort(); 372 animation_queue_[i]->Abort();
320 else 373 else
321 animation_queue_[i]->Progress(animation_queue_[i]->duration(), 374 animation_queue_[i]->Progress(animation_queue_[i]->duration(),
322 delegate()); 375 delegate());
323 RemoveAnimation(animation_queue_[i].get()); 376 RemoveAnimation(animation_queue_[i].get());
324 } else { 377 } else {
325 ++i; 378 ++i;
326 } 379 }
327 } 380 }
328 } 381 }
329 382
330 void LayerAnimator::ImmediatelySetNewTarget(LayerAnimationSequence* sequence) { 383 void LayerAnimator::ImmediatelySetNewTarget(LayerAnimationSequence* sequence) {
331 const bool abort = false; 384 const bool abort = false;
332 RemoveAllAnimationsWithACommonProperty(sequence, abort); 385 RemoveAllAnimationsWithACommonProperty(sequence, abort);
333 sequence->Progress(sequence->duration(), delegate()); 386 sequence->Progress(sequence->duration(), delegate());
387 delegate()->OnLayerAnimationEnded(sequence);
334 RemoveAnimation(sequence); 388 RemoveAnimation(sequence);
335 } 389 }
336 390
337 void LayerAnimator::ImmediatelyAnimateToNewTarget( 391 void LayerAnimator::ImmediatelyAnimateToNewTarget(
338 LayerAnimationSequence* sequence) { 392 LayerAnimationSequence* sequence) {
339 const bool abort = true; 393 const bool abort = true;
340 RemoveAllAnimationsWithACommonProperty(sequence, abort); 394 RemoveAllAnimationsWithACommonProperty(sequence, abort);
341 AddToQueueIfNotPresent(sequence); 395 AddToQueueIfNotPresent(sequence);
342 StartSequenceImmediately(sequence); 396 StartSequenceImmediately(sequence);
343 } 397 }
(...skipping 26 matching lines...) Expand all
370 ++i; 424 ++i;
371 } 425 }
372 animation_queue_.push_back(make_linked_ptr(sequence)); 426 animation_queue_.push_back(make_linked_ptr(sequence));
373 ProcessQueue(); 427 ProcessQueue();
374 } 428 }
375 429
376 void LayerAnimator::ProcessQueue() { 430 void LayerAnimator::ProcessQueue() {
377 bool started_sequence = false; 431 bool started_sequence = false;
378 do { 432 do {
379 started_sequence = false; 433 started_sequence = false;
380
381 // Build a list of all currently animated properties. 434 // Build a list of all currently animated properties.
382 LayerAnimationElement::AnimatableProperties animated; 435 LayerAnimationElement::AnimatableProperties animated;
383
384 for (RunningAnimations::const_iterator iter = running_animations_.begin(); 436 for (RunningAnimations::const_iterator iter = running_animations_.begin();
385 iter != running_animations_.end(); ++iter) { 437 iter != running_animations_.end(); ++iter) {
386 animated.insert((*iter).sequence->properties().begin(), 438 animated.insert((*iter).sequence->properties().begin(),
387 (*iter).sequence->properties().end()); 439 (*iter).sequence->properties().end());
388 } 440 }
389 441
390 // Try to find an animation that doesn't conflict with an animated 442 // Try to find an animation that doesn't conflict with an animated
391 // property or a property that will be animated before it. 443 // property or a property that will be animated before it.
392 for (AnimationQueue::iterator queue_iter = animation_queue_.begin(); 444 for (AnimationQueue::iterator queue_iter = animation_queue_.begin();
393 queue_iter != animation_queue_.end(); ++queue_iter) { 445 queue_iter != animation_queue_.end(); ++queue_iter) {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 485
434 // Need to keep a reference to the animation. 486 // Need to keep a reference to the animation.
435 AddToQueueIfNotPresent(sequence); 487 AddToQueueIfNotPresent(sequence);
436 488
437 // Ensure that animations get stepped at their start time. 489 // Ensure that animations get stepped at their start time.
438 Step(start_time); 490 Step(start_time);
439 491
440 return true; 492 return true;
441 } 493 }
442 494
495 void LayerAnimator::GetTargetValue(
496 LayerAnimationElement::TargetValue* target) const {
497 for (RunningAnimations::const_iterator iter = running_animations_.begin();
498 iter != running_animations_.end(); ++iter) {
499 (*iter).sequence->GetTargetValue(target);
500 }
501 }
502
443 } // namespace ui 503 } // namespace ui
OLDNEW
« no previous file with comments | « ui/gfx/compositor/layer_animator.h ('k') | ui/gfx/compositor/layer_animator_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698