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

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

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