Index: ash/rotator/screen_rotation_animator.cc |
diff --git a/ash/rotator/screen_rotation_animator.cc b/ash/rotator/screen_rotation_animator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9568890b33d630c262a6a947eae673d8be4ece9d |
--- /dev/null |
+++ b/ash/rotator/screen_rotation_animator.cc |
@@ -0,0 +1,329 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/rotator/screen_rotation_animator.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "ash/ash_switches.h" |
+#include "ash/display/display_controller.h" |
+#include "ash/display/display_info.h" |
+#include "ash/display/display_manager.h" |
+#include "ash/rotator/screen_rotation_animation.h" |
+#include "ash/shell.h" |
+#include "base/command_line.h" |
+#include "base/time/time.h" |
+#include "ui/aura/window.h" |
+#include "ui/compositor/layer.h" |
+#include "ui/compositor/layer_animation_observer.h" |
+#include "ui/compositor/layer_animation_sequence.h" |
+#include "ui/compositor/layer_animator.h" |
+#include "ui/compositor/layer_owner.h" |
+#include "ui/compositor/layer_tree_owner.h" |
+#include "ui/gfx/animation/tween.h" |
+#include "ui/gfx/display.h" |
+#include "ui/gfx/geometry/point.h" |
+#include "ui/gfx/geometry/rect.h" |
+#include "ui/gfx/geometry/rect_f.h" |
+#include "ui/gfx/transform.h" |
+#include "ui/gfx/transform_util.h" |
+ |
+namespace ash { |
+ |
+namespace { |
+ |
+// The number of degrees the partial rotation animations animate through. |
+const int kPartialRotationDegrees = 20; |
+ |
+// The time it takes for the normal speed rotation animations to run. |
+const int kRotationDurationInMs = 250; |
+ |
+// The time it takes for the slow speed rotation animations to run. |
+const int kRotationDurationSlowInMs = 2500; |
+ |
+// Clones all child layers of |to_clone| and adds the clones as children to |
+// |parent|. Recurses on each child of |to_clone|. |
+void CloneChildren(ui::Layer* to_clone, ui::Layer* parent) { |
+ typedef std::vector<ui::Layer*> Layers; |
+ // Make a copy of the children since RecreateLayer() mutates it. |
+ Layers children(to_clone->children()); |
+ for (Layers::const_iterator i = children.begin(); i != children.end(); ++i) { |
+ ui::LayerOwner* owner = (*i)->owner(); |
+ ui::Layer* clone = owner ? owner->RecreateLayer().release() : NULL; |
+ if (clone) { |
+ parent->Add(clone); |
+ // RecreateLayer() moves the existing children to the new layer. Create a |
+ // copy of those. |
+ CloneChildren(owner->layer(), clone); |
+ } |
+ } |
+} |
+ |
+// Clones the layer tree owned by |window| and adds the cloned layer tree as a |
+// child of |window|'s layer. |
+scoped_ptr<ui::LayerTreeOwner> CloneWindowLayers(aura::Window* window) { |
oshima
2015/03/04 21:31:35
Can you use wm::RecreateLayers in ui/wm/core/windo
bruthig
2015/03/06 16:00:33
Not with the current implementation of wm::Recreat
|
+ scoped_ptr<ui::LayerTreeOwner> cloned_layer_tree( |
+ new ui::LayerTreeOwner(new ui::Layer())); |
+ CloneChildren(window->layer(), cloned_layer_tree->root()); |
+ |
+ DCHECK(cloned_layer_tree->root()); |
+ DCHECK(window->layer()); |
+ |
+ // Add the cloned layer tree into the root, so it will be rendered. |
+ window->layer()->Add(cloned_layer_tree->root()); |
+ window->layer()->StackAtTop(cloned_layer_tree->root()); |
+ |
+ return cloned_layer_tree.Pass(); |
+} |
+ |
+// Gets the current display rotation for the display with the specified |
+// |display_id|. |
+gfx::Display::Rotation GetCurrentRotation(int64 display_id) { |
+ return Shell::GetInstance() |
+ ->display_manager() |
+ ->GetDisplayInfo(display_id) |
+ .rotation(); |
+} |
+ |
+// Returns true if the rotation between |initial_rotation| and |new_rotation| is |
+// 180 degrees. |
+bool Is180DegreeFlip(gfx::Display::Rotation initial_rotation, |
+ gfx::Display::Rotation new_rotation) { |
+ return (initial_rotation + 2) % 4 == new_rotation; |
+} |
+ |
+// A LayerAnimationObserver that will destroy the contained LayerTreeOwner when |
+// notified that a layer animation has ended or was aborted. |
+class LayerCleanupObserver : public ui::LayerAnimationObserver { |
+ public: |
+ explicit LayerCleanupObserver( |
+ scoped_ptr<ui::LayerTreeOwner> layer_tree_owner); |
+ ~LayerCleanupObserver() override; |
+ |
+ // Get the root layer of the owned layer tree. |
+ ui::Layer* GetRootLayer(); |
+ |
+ // ui::LayerAnimationObserver: |
+ void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; |
+ void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; |
+ void OnLayerAnimationScheduled( |
+ ui::LayerAnimationSequence* sequence) override {} |
+ |
+ protected: |
+ // ui::LayerAnimationObserver: |
+ bool RequiresNotificationWhenAnimatorDestroyed() const override { |
+ return true; |
+ } |
+ void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override; |
+ void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override; |
+ |
+ private: |
+ // The owned layer tree. |
+ scoped_ptr<ui::LayerTreeOwner> layer_tree_owner_; |
+ |
+ // The LayerAnimationSequence that |this| has been attached to. Defaults to |
+ // nullptr. |
+ ui::LayerAnimationSequence* sequence_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver); |
+}; |
+ |
+LayerCleanupObserver::LayerCleanupObserver( |
+ scoped_ptr<ui::LayerTreeOwner> layer_tree_owner) |
+ : layer_tree_owner_(layer_tree_owner.Pass()), sequence_(nullptr) { |
+} |
+ |
+LayerCleanupObserver::~LayerCleanupObserver() { |
+ // We must eplicitly detach from |sequence_| because we return true from |
+ // RequiresNotificationWhenAnimatorDestroyed. |
+ if (sequence_) |
+ sequence_->RemoveObserver(this); |
+} |
+ |
+ui::Layer* LayerCleanupObserver::GetRootLayer() { |
+ return layer_tree_owner_->root(); |
+} |
+ |
+void LayerCleanupObserver::OnLayerAnimationEnded( |
+ ui::LayerAnimationSequence* sequence) { |
+ delete this; |
+} |
+ |
+void LayerCleanupObserver::OnLayerAnimationAborted( |
+ ui::LayerAnimationSequence* sequence) { |
+ delete this; |
+} |
+ |
+void LayerCleanupObserver::OnAttachedToSequence( |
+ ui::LayerAnimationSequence* sequence) { |
+ sequence_ = sequence; |
+} |
+ |
+void LayerCleanupObserver::OnDetachedFromSequence( |
+ ui::LayerAnimationSequence* sequence) { |
+ DCHECK(sequence == sequence_); |
+ sequence_ = nullptr; |
+} |
+ |
+// Set the screen orientation for the given |display| to |new_rotation| and |
+// animate the change. |
+void RotateScreen(gfx::Display display, |
+ gfx::Display::Rotation new_rotation, |
+ base::TimeDelta duration, |
+ int rotation_degrees, |
+ int rotation_degree_offset, |
+ gfx::Tween::Type tween_type, |
+ bool should_scale) { |
+ aura::Window* root_window = |
+ Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId( |
+ display.id()); |
+ |
+ const gfx::RectF original_screen_bounds = root_window->GetTargetBounds(); |
oshima
2015/03/04 21:31:35
What happens if the root window is destroyed while
bruthig
2015/03/06 16:00:33
As best I can tell, the RootWindow will complete a
|
+ // 180 degree rotations should animation clock-wise. |
+ const int rotation_factor = |
+ (GetCurrentRotation(display.id()) + 3) % 4 == new_rotation ? 1 : -1; |
+ scoped_ptr<LayerCleanupObserver> layer_cleanup_observer( |
+ new LayerCleanupObserver(CloneWindowLayers(root_window).Pass())); |
+ |
+ Shell::GetInstance()->display_manager()->SetDisplayRotation(display.id(), |
+ new_rotation); |
+ |
+ const gfx::RectF rotated_screen_bounds = root_window->GetTargetBounds(); |
+ const gfx::Point pivot = |
+ Is180DegreeFlip(GetCurrentRotation(display.id()), new_rotation) |
+ ? gfx::Point(rotated_screen_bounds.height() / 2, |
+ rotated_screen_bounds.width() / 2) |
+ : gfx::Point(rotated_screen_bounds.width() / 2, |
+ rotated_screen_bounds.height() / 2); |
+ |
+ gfx::Point3F new_layer_initial_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); |
+ gfx::Point3F old_layer_target_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); |
+ |
+ if (should_scale) { |
+ new_layer_initial_scale = gfx::Point3F( |
+ original_screen_bounds.width() / rotated_screen_bounds.width(), |
+ original_screen_bounds.height() / rotated_screen_bounds.height(), 1.0f); |
+ |
+ old_layer_target_scale = gfx::Point3F( |
+ rotated_screen_bounds.width() / original_screen_bounds.width(), |
+ rotated_screen_bounds.height() / original_screen_bounds.height(), 1.0f); |
+ } |
+ |
+ // We must animate each non-cloned child layer individually because the cloned |
+ // layer was added as a child to |root_window|'s layer so that it will be |
+ // rendered. |
+ for (ui::Layer* child_layer : root_window->layer()->children()) { |
+ // Skip the cloned layer because it has a different animation. |
+ if (child_layer == layer_cleanup_observer->GetRootLayer()) |
+ continue; |
+ |
+ scoped_ptr<ScreenRotationAnimation> screen_rotation( |
+ new ScreenRotationAnimation( |
+ child_layer, rotation_degrees * rotation_factor, |
+ 0 /* end_degrees */, new_layer_initial_scale, |
+ gfx::Point3F(1.0f, 1.0f, 1.0f), pivot, duration)); |
+ |
+ screen_rotation->set_tween_type(tween_type); |
+ screen_rotation->set_target_opacity(1.0f); |
+ |
+ ui::LayerAnimator* animator = child_layer->GetAnimator(); |
+ animator->set_preemption_strategy( |
+ ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
+ scoped_ptr<ui::LayerAnimationSequence> animation_sequence( |
+ new ui::LayerAnimationSequence(screen_rotation.release())); |
+ animator->StartAnimation(animation_sequence.release()); |
+ } |
+ |
+ // The old layer will also be transformed into the new orientation. We will |
+ // translate it so that the old layer's center point aligns with the new |
+ // orientation's center point and use that center point as the pivot for the |
+ // rotation animation. |
+ gfx::Transform translate_transform = |
+ layer_cleanup_observer->GetRootLayer()->GetTargetTransform(); |
+ translate_transform.Translate( |
+ (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
+ (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
+ layer_cleanup_observer->GetRootLayer()->SetTransform(translate_transform); |
+ |
+ scoped_ptr<ScreenRotationAnimation> screen_rotation( |
+ new ScreenRotationAnimation( |
+ layer_cleanup_observer->GetRootLayer(), |
+ (rotation_degrees + rotation_degree_offset) * rotation_factor, |
+ rotation_degree_offset * rotation_factor, |
+ gfx::Point3F(1.0f, 1.0f, 1.0f), old_layer_target_scale, pivot, |
+ duration)); |
+ |
+ screen_rotation->set_tween_type(tween_type); |
+ screen_rotation->set_target_opacity(0.0f); |
+ |
+ ui::LayerAnimator* animator = |
+ layer_cleanup_observer->GetRootLayer()->GetAnimator(); |
+ animator->set_preemption_strategy( |
+ ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
+ scoped_ptr<ui::LayerAnimationSequence> animation_sequence( |
+ new ui::LayerAnimationSequence(screen_rotation.release())); |
+ // Add an observer so that the cloned layers can be cleaned up with the |
+ // animation completes/aborts. |
+ animation_sequence->AddObserver(layer_cleanup_observer.release()); |
+ animator->StartAnimation(animation_sequence.release()); |
+} |
+ |
+} // namespace |
+ |
+ScreenRotationAnimator::ScreenRotationAnimator(int64 display_id) |
+ : display_(Shell::GetInstance()->display_manager()->GetDisplayForId( |
+ display_id)) { |
+} |
+ |
+ScreenRotationAnimator::ScreenRotationAnimator(gfx::Display display) |
+ : display_(display) { |
+} |
+ |
+ScreenRotationAnimator::~ScreenRotationAnimator() { |
+} |
+ |
+void ScreenRotationAnimator::Rotate(gfx::Display::Rotation new_rotation) { |
+ const gfx::Display::Rotation current_rotation = |
+ GetCurrentRotation(display_.id()); |
+ |
+ if (current_rotation == new_rotation) |
+ return; |
+ |
+ const std::string switch_value = |
+ base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
+ switches::kAshEnableScreenRotationAnimation); |
+ |
+ if ("" == switch_value) { |
oshima
2015/03/04 21:31:35
switch_value.empty()
bruthig
2015/03/06 16:00:33
Done.
|
+ Shell::GetInstance()->display_manager()->SetDisplayRotation(display_.id(), |
+ new_rotation); |
+ return; |
+ } |
+ |
+ if ("partial-rotation" == switch_value || |
+ "partial-rotation-slow" == switch_value) { |
oshima
2015/03/04 21:31:35
can you define const with the comments that explai
bruthig
2015/03/06 16:00:33
Done.
|
+ RotateScreen( |
+ display_, new_rotation, |
+ base::TimeDelta::FromMilliseconds("partial-rotation" == switch_value |
+ ? kRotationDurationInMs |
+ : kRotationDurationSlowInMs), |
+ kPartialRotationDegrees, Is180DegreeFlip(current_rotation, new_rotation) |
+ ? 180 - kPartialRotationDegrees |
+ : 90 - kPartialRotationDegrees, |
oshima
2015/03/04 21:31:35
can you move this out of constructor to make this
bruthig
2015/03/06 16:00:33
Done.
|
+ gfx::Tween::FAST_OUT_LINEAR_IN, false /* should_scale */); |
+ } else if ("full-rotation" == switch_value || |
+ "full-rotation-slow" == switch_value) { |
+ RotateScreen(display_, new_rotation, base::TimeDelta::FromMilliseconds( |
+ "full-rotation" == switch_value |
+ ? kRotationDurationInMs |
+ : kRotationDurationSlowInMs), |
oshima
2015/03/04 21:31:35
ditto
bruthig
2015/03/06 16:00:33
Done.
|
+ Is180DegreeFlip(current_rotation, new_rotation) ? 180 : 90, |
+ 0 /* rotation_degree_offset */, gfx::Tween::FAST_OUT_LINEAR_IN, |
+ true /* should_scale */); |
+ } else { |
+ DCHECK(false); |
oshima
2015/03/04 21:31:35
NOTREACHED()
bruthig
2015/03/06 16:00:33
Done.
|
+ } |
+} |
+ |
+} // namespace ash |