Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ash/rotator/screen_rotation_animator.h" | 5 #include "ash/rotator/screen_rotation_animator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 // The number of degrees that the rotation animations animate through. | 42 // The number of degrees that the rotation animations animate through. |
| 43 const int kRotationDegrees = 20; | 43 const int kRotationDegrees = 20; |
| 44 | 44 |
| 45 // The time it takes for the rotation animations to run. | 45 // The time it takes for the rotation animations to run. |
| 46 const int kRotationDurationInMs = 250; | 46 const int kRotationDurationInMs = 250; |
| 47 | 47 |
| 48 // The rotation factors. | 48 // The rotation factors. |
| 49 const int kCounterClockWiseRotationFactor = 1; | 49 const int kCounterClockWiseRotationFactor = 1; |
| 50 const int kClockWiseRotationFactor = -1; | 50 const int kClockWiseRotationFactor = -1; |
| 51 | 51 |
| 52 // Aborts the active animations of the layer, and recurses upon its child | 52 // The delegate root layer name. |
| 53 // layers. | 53 const std::string kDelegateRootLayerName = "DelegateRootLayer"; |
| 54 void AbortAnimations(ui::Layer* layer) { | |
| 55 for (ui::Layer* child_layer : layer->children()) | |
| 56 AbortAnimations(child_layer); | |
| 57 layer->GetAnimator()->AbortAllAnimations(); | |
| 58 } | |
| 59 | 54 |
| 60 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { | 55 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { |
| 61 return Shell::GetInstance() | 56 return Shell::GetInstance() |
| 62 ->display_manager() | 57 ->display_manager() |
| 63 ->GetDisplayInfo(display_id) | 58 ->GetDisplayInfo(display_id) |
| 64 .GetActiveRotation(); | 59 .GetActiveRotation(); |
| 65 } | 60 } |
| 66 | 61 |
| 67 bool IsDisplayIdValid(int64_t display_id) { | 62 bool IsDisplayIdValid(int64_t display_id) { |
| 68 return Shell::GetInstance()->display_manager()->IsDisplayIdValid(display_id); | 63 return Shell::GetInstance()->display_manager()->IsDisplayIdValid(display_id); |
| 69 } | 64 } |
| 70 | 65 |
| 71 // 180 degree rotations should animate clock-wise. | 66 // 180 degree rotations should animate clock-wise. |
| 72 int GetRotationFactor(display::Display::Rotation initial_rotation, | 67 int GetRotationFactor(display::Display::Rotation initial_rotation, |
| 73 display::Display::Rotation new_rotation) { | 68 display::Display::Rotation new_rotation) { |
| 74 return (initial_rotation + 3) % 4 == new_rotation | 69 return (initial_rotation + 3) % 4 == new_rotation |
| 75 ? kCounterClockWiseRotationFactor | 70 ? kCounterClockWiseRotationFactor |
| 76 : kClockWiseRotationFactor; | 71 : kClockWiseRotationFactor; |
| 77 } | 72 } |
| 78 | 73 |
| 79 aura::Window* GetRootWindow(int64_t display_id) { | 74 aura::Window* GetRootWindow(int64_t display_id) { |
| 80 return Shell::GetInstance() | 75 return Shell::GetInstance() |
| 81 ->window_tree_host_manager() | 76 ->window_tree_host_manager() |
| 82 ->GetRootWindowForDisplayId(display_id); | 77 ->GetRootWindowForDisplayId(display_id); |
| 83 } | 78 } |
| 84 | 79 |
| 80 ui::Layer* GetChildLayerByName(ui::Layer* parent, const std::string& name) { | |
| 81 for (ui::Layer* child_layer : parent->children()) { | |
| 82 if (child_layer->name() == name) | |
| 83 return child_layer; | |
| 84 } | |
| 85 NOTREACHED(); | |
| 86 return nullptr; | |
| 87 } | |
| 88 | |
| 85 // Returns true if the rotation between |initial_rotation| and |new_rotation| is | 89 // Returns true if the rotation between |initial_rotation| and |new_rotation| is |
| 86 // 180 degrees. | 90 // 180 degrees. |
| 87 bool Is180DegreeFlip(display::Display::Rotation initial_rotation, | 91 bool Is180DegreeFlip(display::Display::Rotation initial_rotation, |
| 88 display::Display::Rotation new_rotation) { | 92 display::Display::Rotation new_rotation) { |
| 89 return (initial_rotation + 2) % 4 == new_rotation; | 93 return (initial_rotation + 2) % 4 == new_rotation; |
| 90 } | 94 } |
| 91 | 95 |
| 92 // Returns the initial degrees the old layer animation to begin with. | 96 // Returns the initial degrees the old layer animation to begin with. |
| 93 int GetInitialDegrees(display::Display::Rotation initial_rotation, | 97 int GetInitialDegrees(display::Display::Rotation initial_rotation, |
| 94 display::Display::Rotation new_rotation) { | 98 display::Display::Rotation new_rotation) { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( | 246 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( |
| 243 new LayerCleanupObserver(weak_factory_.GetWeakPtr())); | 247 new LayerCleanupObserver(weak_factory_.GetWeakPtr())); |
| 244 | 248 |
| 245 Shell::GetInstance()->display_manager()->SetDisplayRotation( | 249 Shell::GetInstance()->display_manager()->SetDisplayRotation( |
| 246 display_id_, rotation_request->new_rotation, rotation_request->source); | 250 display_id_, rotation_request->new_rotation, rotation_request->source); |
| 247 | 251 |
| 248 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); | 252 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); |
| 249 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, | 253 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
| 250 rotated_screen_bounds.height() / 2); | 254 rotated_screen_bounds.height() / 2); |
| 251 | 255 |
| 256 ui::Layer* delegate_root_layer = | |
| 257 GetChildLayerByName(root_window->layer(), kDelegateRootLayerName); | |
|
oshima
2017/04/05 20:26:51
can't you just find the window by id and get layer
wutao
2017/04/05 23:37:34
Done.
| |
| 258 std::unique_ptr<ScreenRotationAnimation> current_layer_screen_rotation = | |
| 259 base::MakeUnique<ScreenRotationAnimation>( | |
| 260 delegate_root_layer, kRotationDegrees * rotation_factor, | |
| 261 0 /* end_degrees */, 0.0f, | |
| 262 delegate_root_layer->opacity() /* target_opacity */, pivot, duration, | |
| 263 tween_type); | |
| 264 | |
| 265 ui::LayerAnimator* current_layer_animator = | |
| 266 delegate_root_layer->GetAnimator(); | |
| 267 current_layer_animator->set_preemption_strategy( | |
| 268 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
| 269 std::unique_ptr<ui::LayerAnimationSequence> current_layer_animation_sequence = | |
| 270 base::MakeUnique<ui::LayerAnimationSequence>( | |
| 271 std::move(current_layer_screen_rotation)); | |
| 272 current_layer_animator->StartAnimation( | |
| 273 current_layer_animation_sequence.release()); | |
| 274 | |
| 252 ui::Layer* old_root_layer = old_layer_tree_owner_->root(); | 275 ui::Layer* old_root_layer = old_layer_tree_owner_->root(); |
| 253 // We must animate each non-cloned child layer individually because the cloned | |
| 254 // layer was added as a child to |root_window|'s layer so that it will be | |
| 255 // rendered. | |
| 256 // TODO(bruthig): Add a NOT_DRAWN layer in between the root_window's layer and | |
| 257 // its current children so that we only need to initiate two | |
| 258 // LayerAnimationSequences. One for the new layers and one for the old layer. | |
| 259 for (ui::Layer* child_layer : root_window->layer()->children()) { | |
| 260 // Skip the cloned layer because it has a different animation. | |
| 261 if (child_layer == old_root_layer) | |
| 262 continue; | |
| 263 | |
| 264 std::unique_ptr<ScreenRotationAnimation> screen_rotation = | |
| 265 base::MakeUnique<ScreenRotationAnimation>( | |
| 266 child_layer, kRotationDegrees * rotation_factor, | |
| 267 0 /* end_degrees */, child_layer->opacity(), | |
| 268 1.0f /* target_opacity */, pivot, duration, tween_type); | |
| 269 | |
| 270 ui::LayerAnimator* animator = child_layer->GetAnimator(); | |
| 271 animator->set_preemption_strategy( | |
| 272 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
| 273 std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = | |
| 274 base::MakeUnique<ui::LayerAnimationSequence>( | |
| 275 std::move(screen_rotation)); | |
| 276 animator->StartAnimation(animation_sequence.release()); | |
| 277 } | |
| 278 | |
| 279 // The old layer will also be transformed into the new orientation. We will | 276 // The old layer will also be transformed into the new orientation. We will |
| 280 // translate it so that the old layer's center point aligns with the new | 277 // translate it so that the old layer's center point aligns with the new |
| 281 // orientation's center point and use that center point as the pivot for the | 278 // orientation's center point and use that center point as the pivot for the |
| 282 // rotation animation. | 279 // rotation animation. |
| 283 gfx::Transform translate_transform; | 280 gfx::Transform translate_transform; |
| 284 translate_transform.Translate( | 281 translate_transform.Translate( |
| 285 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, | 282 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
| 286 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); | 283 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
| 287 old_root_layer->SetTransform(translate_transform); | 284 old_root_layer->SetTransform(translate_transform); |
| 288 | 285 |
| 289 std::unique_ptr<ScreenRotationAnimation> screen_rotation = | 286 std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation = |
| 290 base::MakeUnique<ScreenRotationAnimation>( | 287 base::MakeUnique<ScreenRotationAnimation>( |
| 291 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, | 288 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, |
| 292 (old_layer_initial_rotation_degrees - kRotationDegrees) * | 289 (old_layer_initial_rotation_degrees - kRotationDegrees) * |
| 293 rotation_factor, | 290 rotation_factor, |
| 294 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, | 291 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, |
| 295 tween_type); | 292 tween_type); |
| 296 | 293 |
| 297 ui::LayerAnimator* animator = old_root_layer->GetAnimator(); | 294 ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator(); |
| 298 animator->set_preemption_strategy( | 295 old_layer_animator->set_preemption_strategy( |
| 299 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 296 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 300 std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = | 297 std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = |
| 301 base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); | 298 base::MakeUnique<ui::LayerAnimationSequence>( |
| 299 std::move(old_layer_screen_rotation)); | |
| 302 // Add an observer so that the cloned layers can be cleaned up with the | 300 // Add an observer so that the cloned layers can be cleaned up with the |
| 303 // animation completes/aborts. | 301 // animation completes/aborts. |
| 304 animation_sequence->AddObserver(old_layer_cleanup_observer.release()); | 302 old_layer_animation_sequence->AddObserver( |
| 303 old_layer_cleanup_observer.release()); | |
| 305 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to | 304 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
| 306 // control the animation. | 305 // control the animation. |
| 307 if (disable_animation_timers_for_test_) | 306 if (disable_animation_timers_for_test_) |
| 308 animator->set_disable_timer_for_test(true); | 307 old_layer_animator->set_disable_timer_for_test(true); |
| 309 animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); | 308 old_layer_animation_sequence->SetAnimationMetricsReporter( |
| 310 animator->StartAnimation(animation_sequence.release()); | 309 metrics_reporter_.get()); |
| 310 old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); | |
| 311 | 311 |
| 312 rotation_request.reset(); | 312 rotation_request.reset(); |
| 313 } | 313 } |
| 314 | 314 |
| 315 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, | 315 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
| 316 display::Display::RotationSource source) { | 316 display::Display::RotationSource source) { |
| 317 if (GetCurrentScreenRotation(display_id_) == new_rotation) | 317 if (GetCurrentScreenRotation(display_id_) == new_rotation) |
| 318 return; | 318 return; |
| 319 | 319 |
| 320 std::unique_ptr<ScreenRotationRequest> rotation_request = | 320 std::unique_ptr<ScreenRotationRequest> rotation_request = |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 for (auto& observer : screen_rotation_animator_observers_) | 356 for (auto& observer : screen_rotation_animator_observers_) |
| 357 observer.OnScreenRotationAnimationFinished(this); | 357 observer.OnScreenRotationAnimationFinished(this); |
| 358 } | 358 } |
| 359 | 359 |
| 360 void ScreenRotationAnimator::set_disable_animation_timers_for_test( | 360 void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
| 361 bool disable_timers) { | 361 bool disable_timers) { |
| 362 disable_animation_timers_for_test_ = disable_timers; | 362 disable_animation_timers_for_test_ = disable_timers; |
| 363 } | 363 } |
| 364 | 364 |
| 365 void ScreenRotationAnimator::StopAnimating() { | 365 void ScreenRotationAnimator::StopAnimating() { |
| 366 aura::Window* root_window = GetRootWindow(display_id_); | 366 GetChildLayerByName(GetRootWindow(display_id_)->layer(), |
| 367 for (ui::Layer* child_layer : root_window->layer()->children()) { | 367 kDelegateRootLayerName) |
| 368 if (child_layer == old_layer_tree_owner_->root()) | 368 ->GetAnimator() |
| 369 continue; | 369 ->StopAnimating(); |
| 370 | |
| 371 child_layer->GetAnimator()->StopAnimating(); | |
| 372 } | |
| 373 | |
| 374 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); | 370 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
| 375 } | 371 } |
| 376 | 372 |
| 377 } // namespace ash | 373 } // namespace ash |
| OLD | NEW |