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/display/window_tree_host_manager.h" | 11 #include "ash/display/window_tree_host_manager.h" |
| 12 #include "ash/public/cpp/shell_window_ids.h" |
12 #include "ash/rotator/screen_rotation_animation.h" | 13 #include "ash/rotator/screen_rotation_animation.h" |
13 #include "ash/rotator/screen_rotation_animator_observer.h" | 14 #include "ash/rotator/screen_rotation_animator_observer.h" |
14 #include "ash/shell.h" | 15 #include "ash/shell.h" |
15 #include "base/command_line.h" | 16 #include "base/command_line.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
18 #include "base/time/time.h" | 19 #include "base/time/time.h" |
19 #include "ui/aura/window.h" | 20 #include "ui/aura/window.h" |
20 #include "ui/compositor/layer.h" | 21 #include "ui/compositor/layer.h" |
21 #include "ui/compositor/layer_animation_element.h" | 22 #include "ui/compositor/layer_animation_element.h" |
(...skipping 20 matching lines...) Expand all Loading... |
42 // The number of degrees that the rotation animations animate through. | 43 // The number of degrees that the rotation animations animate through. |
43 const int kRotationDegrees = 20; | 44 const int kRotationDegrees = 20; |
44 | 45 |
45 // The time it takes for the rotation animations to run. | 46 // The time it takes for the rotation animations to run. |
46 const int kRotationDurationInMs = 250; | 47 const int kRotationDurationInMs = 250; |
47 | 48 |
48 // The rotation factors. | 49 // The rotation factors. |
49 const int kCounterClockWiseRotationFactor = 1; | 50 const int kCounterClockWiseRotationFactor = 1; |
50 const int kClockWiseRotationFactor = -1; | 51 const int kClockWiseRotationFactor = -1; |
51 | 52 |
52 // Aborts the active animations of the layer, and recurses upon its child | |
53 // layers. | |
54 void AbortAnimations(ui::Layer* layer) { | |
55 for (ui::Layer* child_layer : layer->children()) | |
56 AbortAnimations(child_layer); | |
57 layer->GetAnimator()->AbortAllAnimations(); | |
58 } | |
59 | |
60 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { | 53 display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { |
61 return Shell::GetInstance() | 54 return Shell::GetInstance() |
62 ->display_manager() | 55 ->display_manager() |
63 ->GetDisplayInfo(display_id) | 56 ->GetDisplayInfo(display_id) |
64 .GetActiveRotation(); | 57 .GetActiveRotation(); |
65 } | 58 } |
66 | 59 |
67 bool IsDisplayIdValid(int64_t display_id) { | 60 bool IsDisplayIdValid(int64_t display_id) { |
68 return Shell::GetInstance()->display_manager()->IsDisplayIdValid(display_id); | 61 return Shell::GetInstance()->display_manager()->IsDisplayIdValid(display_id); |
69 } | 62 } |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( | 235 std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( |
243 new LayerCleanupObserver(weak_factory_.GetWeakPtr())); | 236 new LayerCleanupObserver(weak_factory_.GetWeakPtr())); |
244 | 237 |
245 Shell::GetInstance()->display_manager()->SetDisplayRotation( | 238 Shell::GetInstance()->display_manager()->SetDisplayRotation( |
246 display_id_, rotation_request->new_rotation, rotation_request->source); | 239 display_id_, rotation_request->new_rotation, rotation_request->source); |
247 | 240 |
248 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); | 241 const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); |
249 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, | 242 const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
250 rotated_screen_bounds.height() / 2); | 243 rotated_screen_bounds.height() / 2); |
251 | 244 |
| 245 ui::Layer* screen_rotation_container_layer = |
| 246 root_window->GetChildById(kShellWindowId_ScreenRotationContainer) |
| 247 ->layer(); |
| 248 std::unique_ptr<ScreenRotationAnimation> current_layer_screen_rotation = |
| 249 base::MakeUnique<ScreenRotationAnimation>( |
| 250 screen_rotation_container_layer, kRotationDegrees * rotation_factor, |
| 251 0 /* end_degrees */, 0.0f, |
| 252 screen_rotation_container_layer->opacity() /* target_opacity */, |
| 253 pivot, duration, tween_type); |
| 254 |
| 255 ui::LayerAnimator* current_layer_animator = |
| 256 screen_rotation_container_layer->GetAnimator(); |
| 257 current_layer_animator->set_preemption_strategy( |
| 258 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 259 std::unique_ptr<ui::LayerAnimationSequence> current_layer_animation_sequence = |
| 260 base::MakeUnique<ui::LayerAnimationSequence>( |
| 261 std::move(current_layer_screen_rotation)); |
| 262 current_layer_animator->StartAnimation( |
| 263 current_layer_animation_sequence.release()); |
| 264 |
252 ui::Layer* old_root_layer = old_layer_tree_owner_->root(); | 265 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 | 266 // 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 | 267 // 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 | 268 // orientation's center point and use that center point as the pivot for the |
282 // rotation animation. | 269 // rotation animation. |
283 gfx::Transform translate_transform; | 270 gfx::Transform translate_transform; |
284 translate_transform.Translate( | 271 translate_transform.Translate( |
285 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, | 272 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
286 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); | 273 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
287 old_root_layer->SetTransform(translate_transform); | 274 old_root_layer->SetTransform(translate_transform); |
288 | 275 |
289 std::unique_ptr<ScreenRotationAnimation> screen_rotation = | 276 std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation = |
290 base::MakeUnique<ScreenRotationAnimation>( | 277 base::MakeUnique<ScreenRotationAnimation>( |
291 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, | 278 old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, |
292 (old_layer_initial_rotation_degrees - kRotationDegrees) * | 279 (old_layer_initial_rotation_degrees - kRotationDegrees) * |
293 rotation_factor, | 280 rotation_factor, |
294 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, | 281 old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, |
295 tween_type); | 282 tween_type); |
296 | 283 |
297 ui::LayerAnimator* animator = old_root_layer->GetAnimator(); | 284 ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator(); |
298 animator->set_preemption_strategy( | 285 old_layer_animator->set_preemption_strategy( |
299 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | 286 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
300 std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = | 287 std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = |
301 base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); | 288 base::MakeUnique<ui::LayerAnimationSequence>( |
| 289 std::move(old_layer_screen_rotation)); |
302 // Add an observer so that the cloned layers can be cleaned up with the | 290 // Add an observer so that the cloned layers can be cleaned up with the |
303 // animation completes/aborts. | 291 // animation completes/aborts. |
304 animation_sequence->AddObserver(old_layer_cleanup_observer.release()); | 292 old_layer_animation_sequence->AddObserver( |
| 293 old_layer_cleanup_observer.release()); |
305 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to | 294 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
306 // control the animation. | 295 // control the animation. |
307 if (disable_animation_timers_for_test_) | 296 if (disable_animation_timers_for_test_) |
308 animator->set_disable_timer_for_test(true); | 297 old_layer_animator->set_disable_timer_for_test(true); |
309 animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); | 298 old_layer_animation_sequence->SetAnimationMetricsReporter( |
310 animator->StartAnimation(animation_sequence.release()); | 299 metrics_reporter_.get()); |
| 300 old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); |
311 | 301 |
312 rotation_request.reset(); | 302 rotation_request.reset(); |
313 } | 303 } |
314 | 304 |
315 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, | 305 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
316 display::Display::RotationSource source) { | 306 display::Display::RotationSource source) { |
317 if (GetCurrentScreenRotation(display_id_) == new_rotation) | 307 if (GetCurrentScreenRotation(display_id_) == new_rotation) |
318 return; | 308 return; |
319 | 309 |
320 std::unique_ptr<ScreenRotationRequest> rotation_request = | 310 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_) | 346 for (auto& observer : screen_rotation_animator_observers_) |
357 observer.OnScreenRotationAnimationFinished(this); | 347 observer.OnScreenRotationAnimationFinished(this); |
358 } | 348 } |
359 | 349 |
360 void ScreenRotationAnimator::set_disable_animation_timers_for_test( | 350 void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
361 bool disable_timers) { | 351 bool disable_timers) { |
362 disable_animation_timers_for_test_ = disable_timers; | 352 disable_animation_timers_for_test_ = disable_timers; |
363 } | 353 } |
364 | 354 |
365 void ScreenRotationAnimator::StopAnimating() { | 355 void ScreenRotationAnimator::StopAnimating() { |
366 aura::Window* root_window = GetRootWindow(display_id_); | 356 GetRootWindow(display_id_) |
367 for (ui::Layer* child_layer : root_window->layer()->children()) { | 357 ->GetChildById(kShellWindowId_ScreenRotationContainer) |
368 if (child_layer == old_layer_tree_owner_->root()) | 358 ->layer() |
369 continue; | 359 ->GetAnimator() |
370 | 360 ->StopAnimating(); |
371 child_layer->GetAnimator()->StopAnimating(); | |
372 } | |
373 | |
374 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); | 361 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
375 } | 362 } |
376 | 363 |
377 } // namespace ash | 364 } // namespace ash |
OLD | NEW |