| 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 |
| 11 #include "ash/ash_switches.h" | 11 #include "ash/ash_switches.h" |
| 12 #include "ash/display/window_tree_host_manager.h" | 12 #include "ash/display/window_tree_host_manager.h" |
| 13 #include "ash/public/cpp/shell_window_ids.h" |
| 13 #include "ash/rotator/screen_rotation_animation.h" | 14 #include "ash/rotator/screen_rotation_animation.h" |
| 14 #include "ash/rotator/screen_rotation_animator_observer.h" | 15 #include "ash/rotator/screen_rotation_animator_observer.h" |
| 15 #include "ash/shell.h" | 16 #include "ash/shell.h" |
| 16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 20 #include "cc/output/copy_output_request.h" | 21 #include "cc/output/copy_output_request.h" |
| 21 #include "cc/output/copy_output_result.h" | 22 #include "cc/output/copy_output_result.h" |
| 22 #include "ui/aura/window.h" | 23 #include "ui/aura/window.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 45 // The number of degrees that the rotation animations animate through. | 46 // The number of degrees that the rotation animations animate through. |
| 46 const int kRotationDegrees = 20; | 47 const int kRotationDegrees = 20; |
| 47 | 48 |
| 48 // The time it takes for the rotation animations to run. | 49 // The time it takes for the rotation animations to run. |
| 49 const int kRotationDurationInMs = 250; | 50 const int kRotationDurationInMs = 250; |
| 50 | 51 |
| 51 // The rotation factors. | 52 // The rotation factors. |
| 52 const int kCounterClockWiseRotationFactor = 1; | 53 const int kCounterClockWiseRotationFactor = 1; |
| 53 const int kClockWiseRotationFactor = -1; | 54 const int kClockWiseRotationFactor = -1; |
| 54 | 55 |
| 55 // Aborts the active animations of the layer, and recurses upon its child | |
| 56 // layers. | |
| 57 void AbortAnimations(ui::Layer* layer) { | |
| 58 for (ui::Layer* child_layer : layer->children()) | |
| 59 AbortAnimations(child_layer); | |
| 60 layer->GetAnimator()->AbortAllAnimations(); | |
| 61 } | |
| 62 | |
| 63 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { | 56 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { |
| 64 return Shell::Get() | 57 return Shell::Get() |
| 65 ->display_manager() | 58 ->display_manager() |
| 66 ->GetDisplayInfo(display_id) | 59 ->GetDisplayInfo(display_id) |
| 67 .GetActiveRotation(); | 60 .GetActiveRotation(); |
| 68 } | 61 } |
| 69 | 62 |
| 70 bool IsDisplayIdValid(int64_t display_id) { | 63 bool IsDisplayIdValid(int64_t display_id) { |
| 71 return Shell::Get()->display_manager()->IsDisplayIdValid(display_id); | 64 return Shell::Get()->display_manager()->IsDisplayIdValid(display_id); |
| 72 } | 65 } |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 303 |
| 311 const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; | 304 const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; |
| 312 | 305 |
| 313 Shell::Get()->display_manager()->SetDisplayRotation( | 306 Shell::Get()->display_manager()->SetDisplayRotation( |
| 314 display_id_, rotation_request->new_rotation, rotation_request->source); | 307 display_id_, rotation_request->new_rotation, rotation_request->source); |
| 315 | 308 |
| 316 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); | 309 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); |
| 317 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, | 310 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
| 318 rotated_screen_bounds.height() / 2); | 311 rotated_screen_bounds.height() / 2); |
| 319 | 312 |
| 320 // We must animate each non-cloned child layer individually because the cloned | 313 ui::Layer* screen_rotation_container_layer = |
| 321 // layer was added as a child to |root_window|'s layer so that it will be | 314 root_window->GetChildById(kShellWindowId_ScreenRotationContainer) |
| 322 // rendered. | 315 ->layer(); |
| 323 // TODO(bruthig): Add a NOT_DRAWN layer in between the root_window's layer and | 316 std::unique_ptr<ScreenRotationAnimation> current_layer_screen_rotation = |
| 324 // its current children so that we only need to initiate two | 317 base::MakeUnique<ScreenRotationAnimation>( |
| 325 // LayerAnimationSequences. One for the new layers and one for the old layer. | 318 screen_rotation_container_layer, kRotationDegrees * rotation_factor, |
| 326 for (ui::Layer* child_layer : root_window->layer()->children()) { | 319 0 /* end_degrees */, screen_rotation_container_layer->opacity(), |
| 327 // Skip the cloned layer because it has a different animation. | 320 screen_rotation_container_layer->opacity() /* target_opacity */, |
| 328 if (child_layer == old_root_layer) | 321 pivot, duration, tween_type); |
| 329 continue; | |
| 330 | 322 |
| 331 std::unique_ptr<ScreenRotationAnimation> screen_rotation = | 323 ui::LayerAnimator* current_layer_animator = |
| 332 base::MakeUnique<ScreenRotationAnimation>( | 324 screen_rotation_container_layer->GetAnimator(); |
| 333 child_layer, kRotationDegrees * rotation_factor, | 325 current_layer_animator->set_preemption_strategy( |
| 334 0 /* end_degrees */, child_layer->opacity(), | 326 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 335 1.0f /* target_opacity */, pivot, duration, tween_type); | 327 std::unique_ptr<ui::LayerAnimationSequence> current_layer_animation_sequence = |
| 336 | 328 base::MakeUnique<ui::LayerAnimationSequence>( |
| 337 ui::LayerAnimator* animator = child_layer->GetAnimator(); | 329 std::move(current_layer_screen_rotation)); |
| 338 animator->set_preemption_strategy( | 330 current_layer_animator->StartAnimation( |
| 339 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 331 current_layer_animation_sequence.release()); |
| 340 std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = | |
| 341 base::MakeUnique<ui::LayerAnimationSequence>( | |
| 342 std::move(screen_rotation)); | |
| 343 animator->StartAnimation(animation_sequence.release()); | |
| 344 } | |
| 345 | 332 |
| 346 // The old layer will also be transformed into the new orientation. We will | 333 // The old layer will also be transformed into the new orientation. We will |
| 347 // translate it so that the old layer's center point aligns with the new | 334 // translate it so that the old layer's center point aligns with the new |
| 348 // orientation's center point and use that center point as the pivot for the | 335 // orientation's center point and use that center point as the pivot for the |
| 349 // rotation animation. | 336 // rotation animation. |
| 350 gfx::Transform translate_transform; | 337 gfx::Transform translate_transform; |
| 351 translate_transform.Translate( | 338 translate_transform.Translate( |
| 352 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, | 339 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
| 353 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); | 340 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
| 354 old_root_layer->SetTransform(translate_transform); | 341 old_root_layer->SetTransform(translate_transform); |
| 355 | 342 |
| 356 std::unique_ptr<ScreenRotationAnimation> screen_rotation = | 343 std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation = |
| 357 base::MakeUnique<ScreenRotationAnimation>( | 344 base::MakeUnique<ScreenRotationAnimation>( |
| 358 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, | 345 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, |
| 359 (old_layer_initial_rotation_degrees - kRotationDegrees) * | 346 (old_layer_initial_rotation_degrees - kRotationDegrees) * |
| 360 rotation_factor, | 347 rotation_factor, |
| 361 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, | 348 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, |
| 362 tween_type); | 349 tween_type); |
| 363 | 350 |
| 364 ui::LayerAnimator* animator = old_root_layer->GetAnimator(); | 351 ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator(); |
| 365 animator->set_preemption_strategy( | 352 old_layer_animator->set_preemption_strategy( |
| 366 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 353 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 367 std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = | 354 std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = |
| 368 base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); | 355 base::MakeUnique<ui::LayerAnimationSequence>( |
| 356 std::move(old_layer_screen_rotation)); |
| 369 // Add an observer so that the cloned layers can be cleaned up with the | 357 // Add an observer so that the cloned layers can be cleaned up with the |
| 370 // animation completes/aborts. | 358 // animation completes/aborts. |
| 371 animation_sequence->AddObserver(old_layer_cleanup_observer.release()); | 359 old_layer_animation_sequence->AddObserver( |
| 360 old_layer_cleanup_observer.release()); |
| 372 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to | 361 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
| 373 // control the animation. | 362 // control the animation. |
| 374 if (disable_animation_timers_for_test_) | 363 if (disable_animation_timers_for_test_) |
| 375 animator->set_disable_timer_for_test(true); | 364 old_layer_animator->set_disable_timer_for_test(true); |
| 376 animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); | 365 old_layer_animation_sequence->SetAnimationMetricsReporter( |
| 377 animator->StartAnimation(animation_sequence.release()); | 366 metrics_reporter_.get()); |
| 367 old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); |
| 368 |
| 369 rotation_request.reset(); |
| 378 } | 370 } |
| 379 | 371 |
| 380 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, | 372 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
| 381 display::Display::RotationSource source) { | 373 display::Display::RotationSource source) { |
| 382 if (GetCurrentScreenRotation(display_id_) == new_rotation) | 374 if (GetCurrentScreenRotation(display_id_) == new_rotation) |
| 383 return; | 375 return; |
| 384 | 376 |
| 385 rotation_request_id_++; | 377 rotation_request_id_++; |
| 386 std::unique_ptr<ScreenRotationRequest> rotation_request = | 378 std::unique_ptr<ScreenRotationRequest> rotation_request = |
| 387 base::MakeUnique<ScreenRotationRequest>(rotation_request_id_, | 379 base::MakeUnique<ScreenRotationRequest>(rotation_request_id_, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 for (auto& observer : screen_rotation_animator_observers_) | 419 for (auto& observer : screen_rotation_animator_observers_) |
| 428 observer.OnScreenRotationAnimationFinished(this); | 420 observer.OnScreenRotationAnimationFinished(this); |
| 429 } | 421 } |
| 430 | 422 |
| 431 void ScreenRotationAnimator::set_disable_animation_timers_for_test( | 423 void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
| 432 bool disable_timers) { | 424 bool disable_timers) { |
| 433 disable_animation_timers_for_test_ = disable_timers; | 425 disable_animation_timers_for_test_ = disable_timers; |
| 434 } | 426 } |
| 435 | 427 |
| 436 void ScreenRotationAnimator::StopAnimating() { | 428 void ScreenRotationAnimator::StopAnimating() { |
| 437 aura::Window* root_window = GetRootWindow(display_id_); | 429 GetRootWindow(display_id_) |
| 438 for (ui::Layer* child_layer : root_window->layer()->children()) { | 430 ->GetChildById(kShellWindowId_ScreenRotationContainer) |
| 439 if (child_layer == old_layer_tree_owner_->root()) | 431 ->layer() |
| 440 continue; | 432 ->GetAnimator() |
| 441 | 433 ->StopAnimating(); |
| 442 child_layer->GetAnimator()->StopAnimating(); | 434 if (old_layer_tree_owner_) |
| 443 } | 435 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
| 444 | |
| 445 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); | |
| 446 } | 436 } |
| 447 | 437 |
| 448 } // namespace ash | 438 } // namespace ash |
| OLD | NEW |