Index: ash/rotator/screen_rotation_animator.cc |
diff --git a/ash/rotator/screen_rotation_animator.cc b/ash/rotator/screen_rotation_animator.cc |
index 5b11d929d0838acdffbc58fa049df290dcd444d1..e23169049b6e8a91d6d88e70f1c08d77313be28f 100644 |
--- a/ash/rotator/screen_rotation_animator.cc |
+++ b/ash/rotator/screen_rotation_animator.cc |
@@ -21,9 +21,9 @@ |
#include "cc/output/copy_output_request.h" |
#include "cc/output/copy_output_result.h" |
#include "ui/aura/window.h" |
+#include "ui/compositor/callback_layer_animation_observer.h" |
#include "ui/compositor/layer.h" |
#include "ui/compositor/layer_animation_element.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" |
@@ -77,6 +77,14 @@ aura::Window* GetRootWindow(int64_t display_id) { |
display_id); |
} |
+aura::Window* GetScreenRotationContainer(aura::Window* root_window) { |
+ return root_window->GetChildById(kShellWindowId_ScreenRotationContainer); |
+} |
+ |
+aura::Window* GetScreenRotationContainer(int64_t display_id) { |
+ return GetScreenRotationContainer(GetRootWindow(display_id)); |
+} |
+ |
// Returns true if the rotation between |initial_rotation| and |new_rotation| is |
// 180 degrees. |
bool Is180DegreeFlip(display::Display::Rotation initial_rotation, |
@@ -90,78 +98,66 @@ int GetInitialDegrees(display::Display::Rotation initial_rotation, |
return (Is180DegreeFlip(initial_rotation, new_rotation) ? 180 : 90); |
} |
-// 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: |
- // Takes WeakPtr of ScreenRotationAnimator. |this| may outlive the |animator_| |
- // instance and the |animator_| isn't detaching itself as an observer when |
- // being destroyed. However, ideally, when |animator_| is destroying, |
- // deleting |old_layer_tree_owner_| will trigger OnLayerAnimationAborted and |
- // delete |this| before |animator_| deleted. |
- explicit LayerCleanupObserver(base::WeakPtr<ScreenRotationAnimator> animator); |
- ~LayerCleanupObserver() override; |
- |
- // 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: |
- base::WeakPtr<ScreenRotationAnimator> animator_; |
- |
- // The LayerAnimationSequence that |this| has been attached to. Defaults to |
- // nullptr. |
- ui::LayerAnimationSequence* sequence_; |
- |
- DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver); |
-}; |
- |
-LayerCleanupObserver::LayerCleanupObserver( |
- base::WeakPtr<ScreenRotationAnimator> animator) |
- : animator_(animator), sequence_(nullptr) {} |
- |
-LayerCleanupObserver::~LayerCleanupObserver() { |
- // We must eplicitly detach from |sequence_| because we return true from |
- // RequiresNotificationWhenAnimatorDestroyed. |
- if (sequence_) |
- sequence_->RemoveObserver(this); |
+void AddLayerAtTopOfWindowLayers(aura::Window* root_window, ui::Layer* layer) { |
+ // Add the cloned/copied layer tree into the root, so it will be rendered. |
+ root_window->layer()->Add(layer); |
+ root_window->layer()->StackAtTop(layer); |
} |
-void LayerCleanupObserver::OnLayerAnimationEnded( |
- ui::LayerAnimationSequence* sequence) { |
- if (animator_) |
- animator_->ProcessAnimationQueue(); |
- |
- delete this; |
+// The Callback will be invoked when all animation sequences have |
+// finished. |observer| will be destroyed after invoking the Callback if it |
+// returns true. |
+bool AnimationEndedCallback( |
+ base::WeakPtr<ScreenRotationAnimator> animator, |
+ const ui::CallbackLayerAnimationObserver& observer) { |
+ if (animator) |
+ animator->ProcessAnimationQueue(); |
+ return true; |
} |
-void LayerCleanupObserver::OnLayerAnimationAborted( |
- ui::LayerAnimationSequence* sequence) { |
- if (animator_) |
- animator_->ProcessAnimationQueue(); |
- |
- delete this; |
+// Round near zero value to zero. |
+void RoundNearZero(gfx::Transform* transform) { |
+ const float kEpsilon = 0.001f; |
+ SkMatrix44& matrix = transform->matrix(); |
+ for (int x = 0; x < 4; ++x) { |
+ for (int y = 0; y < 4; ++y) { |
+ if (std::abs(SkMScalarToFloat(matrix.get(x, y))) < kEpsilon) |
+ matrix.set(x, y, SkFloatToMScalar(0.0f)); |
+ } |
+ } |
} |
-void LayerCleanupObserver::OnAttachedToSequence( |
- ui::LayerAnimationSequence* sequence) { |
- sequence_ = sequence; |
-} |
+void TransformLayerToOldRotation( |
+ ui::Layer* layer, |
+ const int64_t display_id, |
+ const display::Display::Rotation& new_rotation, |
+ const display::Display::Rotation& old_rotation) { |
+ const display::Display display = |
+ Shell::Get()->display_manager()->GetDisplayForId(display_id); |
+ |
+ const int rotation_changed = ((new_rotation - old_rotation) + 4) % 4; |
+ float one_pixel = 1.0f / display.device_scale_factor(); |
+ gfx::Transform transform; |
+ switch (rotation_changed) { |
+ case 0: |
+ break; |
+ case 1: |
+ transform.Rotate(270); |
+ transform.Translate(-display.bounds().height() + one_pixel, 0); |
+ break; |
+ case 2: |
+ transform.Rotate(180); |
+ transform.Translate(-display.bounds().width() + one_pixel, |
+ -display.bounds().height() + one_pixel); |
+ break; |
+ case 3: |
+ transform.Rotate(90); |
+ transform.Translate(0, -display.bounds().width() + one_pixel); |
+ break; |
+ } |
-void LayerCleanupObserver::OnDetachedFromSequence( |
- ui::LayerAnimationSequence* sequence) { |
- DCHECK_EQ(sequence, sequence_); |
- sequence_ = nullptr; |
+ RoundNearZero(&transform); |
+ layer->SetTransform(transform); |
oshima
2017/04/07 20:48:56
Can you refactor and reuse CreateRotationTransform
wutao
2017/04/10 08:01:07
It is not exactly the same. It is an inverse trans
|
} |
class ScreenRotationAnimationMetricsReporter |
@@ -204,32 +200,59 @@ void ScreenRotationAnimator::StartRotationAnimation( |
std::unique_ptr<ScreenRotationRequest> rotation_request) { |
if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
switches::kAshEnableSmoothScreenRotation)) { |
oshima
2017/04/07 20:48:56
you may cache this value.
wutao
2017/04/10 08:01:07
Done.
|
- RequestCopyRootLayerAndAnimateRotation(std::move(rotation_request)); |
+ std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
+ cc::CopyOutputRequest::CreateRequest( |
+ CreateAfterCopyCallbackBeforeRotation(std::move(rotation_request))); |
+ RequestCopyScreenRotationContainerLayer(std::move(copy_output_request)); |
} else { |
- CreateOldLayerTree(); |
- AnimateRotation(std::move(rotation_request)); |
+ StartSlowAnimation(std::move(rotation_request)); |
} |
} |
-void ScreenRotationAnimator::RequestCopyRootLayerAndAnimateRotation( |
+void ScreenRotationAnimator::StartSlowAnimation( |
std::unique_ptr<ScreenRotationRequest> rotation_request) { |
- std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
- cc::CopyOutputRequest::CreateRequest( |
- CreateAfterCopyCallback(std::move(rotation_request))); |
- ui::Layer* layer = GetRootWindow(display_id_)->layer(); |
+ CreateOldLayerTree(); |
+ SetRotation(rotation_request->new_rotation, rotation_request->old_rotation, |
+ rotation_request->source); |
+ AnimateRotation(std::move(rotation_request)); |
+} |
+ |
+void ScreenRotationAnimator::SetRotation( |
+ const display::Display::Rotation& new_rotation, |
+ const display::Display::Rotation& old_rotation, |
+ const display::Display::RotationSource& source) { |
+ Shell::Get()->display_manager()->SetDisplayRotation(display_id_, new_rotation, |
+ source); |
+ TransformLayerToOldRotation(old_layer_tree_owner_->root(), display_id_, |
+ new_rotation, old_rotation); |
+} |
+ |
+void ScreenRotationAnimator::RequestCopyScreenRotationContainerLayer( |
+ std::unique_ptr<cc::CopyOutputRequest> copy_output_request) { |
+ ui::Layer* layer = GetScreenRotationContainer(display_id_)->layer(); |
oshima
2017/04/07 20:48:56
since the container will never change, you should
wutao
2017/04/10 08:01:07
Done.
|
copy_output_request->set_area(gfx::Rect(layer->size())); |
layer->RequestCopyOfOutput(std::move(copy_output_request)); |
} |
ScreenRotationAnimator::CopyCallback |
-ScreenRotationAnimator::CreateAfterCopyCallback( |
+ScreenRotationAnimator::CreateAfterCopyCallbackBeforeRotation( |
+ std::unique_ptr<ScreenRotationRequest> rotation_request) { |
+ return base::Bind(&ScreenRotationAnimator:: |
+ OnScreenRotationContainerLayerCopiedBeforeRotation, |
+ weak_factory_.GetWeakPtr(), |
+ base::Passed(&rotation_request)); |
+} |
+ |
+ScreenRotationAnimator::CopyCallback |
+ScreenRotationAnimator::CreateAfterCopyCallbackAfterRotation( |
std::unique_ptr<ScreenRotationRequest> rotation_request) { |
- return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation, |
+ return base::Bind(&ScreenRotationAnimator:: |
+ OnScreenRotationContainerLayerCopiedAfterRotation, |
weak_factory_.GetWeakPtr(), |
base::Passed(&rotation_request)); |
} |
-void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( |
+void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedBeforeRotation( |
std::unique_ptr<ScreenRotationRequest> rotation_request, |
std::unique_ptr<cc::CopyOutputResult> result) { |
// If display was removed, should abort rotation. |
@@ -242,18 +265,42 @@ void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( |
// canceled or failed. It would fail if, for examples: a) The layer is removed |
// from the compositor and destroyed before committing the request to the |
// compositor. b) The compositor is shutdown. |
oshima
2017/04/07 20:48:56
do we have to continue under these conditions? Can
wutao
2017/04/10 08:01:07
Yes, we can
|
- if (result->IsEmpty()) |
- CreateOldLayerTree(); |
- else |
- CopyOldLayerTree(std::move(result)); |
+ if (result->IsEmpty()) { |
+ StartSlowAnimation(std::move(rotation_request)); |
+ } else { |
+ old_layer_tree_owner_ = CopyLayerTree(std::move(result)); |
+ SetRotation(rotation_request->new_rotation, rotation_request->old_rotation, |
+ rotation_request->source); |
+ std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
+ cc::CopyOutputRequest::CreateRequest( |
+ CreateAfterCopyCallbackAfterRotation(std::move(rotation_request))); |
+ RequestCopyScreenRotationContainerLayer(std::move(copy_output_request)); |
+ } |
+} |
+ |
+void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedAfterRotation( |
+ std::unique_ptr<ScreenRotationRequest> rotation_request, |
+ std::unique_ptr<cc::CopyOutputResult> result) { |
+ // If display was removed, should abort rotation. |
+ if (!IsDisplayIdValid(display_id_)) { |
+ ProcessAnimationQueue(); |
+ return; |
+ } |
+ |
+ // If copy request has been canceled or failed, use the real layers for |
+ // animation. |
oshima
2017/04/07 20:48:56
ditto
wutao
2017/04/10 08:01:07
Abort here.
|
+ if (!result->IsEmpty()) |
+ new_layer_tree_owner_ = CopyLayerTree(std::move(result)); |
AnimateRotation(std::move(rotation_request)); |
} |
void ScreenRotationAnimator::CreateOldLayerTree() { |
- old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_)); |
+ aura::Window* root_window = GetRootWindow(display_id_); |
+ old_layer_tree_owner_ = ::wm::RecreateLayers(root_window); |
+ AddLayerAtTopOfWindowLayers(root_window, old_layer_tree_owner_->root()); |
} |
-void ScreenRotationAnimator::CopyOldLayerTree( |
+std::unique_ptr<ui::LayerTreeOwner> ScreenRotationAnimator::CopyLayerTree( |
std::unique_ptr<cc::CopyOutputResult> result) { |
cc::TextureMailbox texture_mailbox; |
std::unique_ptr<cc::SingleReleaseCallback> release_callback; |
@@ -266,61 +313,53 @@ void ScreenRotationAnimator::CopyOldLayerTree( |
copy_layer->SetBounds(rect); |
copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback), |
rect.size()); |
- old_layer_tree_owner_ = |
- base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer)); |
+ |
+ AddLayerAtTopOfWindowLayers(root_window, copy_layer.get()); |
+ return base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer)); |
} |
void ScreenRotationAnimator::AnimateRotation( |
std::unique_ptr<ScreenRotationRequest> rotation_request) { |
- aura::Window* root_window = GetRootWindow(display_id_); |
- std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( |
- new LayerCleanupObserver(weak_factory_.GetWeakPtr())); |
- ui::Layer* old_root_layer = old_layer_tree_owner_->root(); |
- old_root_layer->set_name("ScreenRotationAnimator:old_layer_tree"); |
- // Add the cloned layer tree in to the root, so it will be rendered. |
- root_window->layer()->Add(old_root_layer); |
- root_window->layer()->StackAtTop(old_root_layer); |
- |
- const gfx::Rect original_screen_bounds = root_window->GetTargetBounds(); |
- |
- const int rotation_factor = GetRotationFactor( |
- GetCurrentScreenRotation(display_id_), rotation_request->new_rotation); |
- |
+ const int rotation_factor = GetRotationFactor(rotation_request->old_rotation, |
+ rotation_request->new_rotation); |
const int old_layer_initial_rotation_degrees = GetInitialDegrees( |
- GetCurrentScreenRotation(display_id_), rotation_request->new_rotation); |
- |
+ rotation_request->old_rotation, rotation_request->new_rotation); |
const base::TimeDelta duration = |
base::TimeDelta::FromMilliseconds(kRotationDurationInMs); |
- |
const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; |
- |
- Shell::Get()->display_manager()->SetDisplayRotation( |
- display_id_, rotation_request->new_rotation, rotation_request->source); |
- |
+ aura::Window* root_window = GetRootWindow(display_id_); |
const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); |
const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
rotated_screen_bounds.height() / 2); |
- ui::Layer* screen_rotation_container_layer = |
- root_window->GetChildById(kShellWindowId_ScreenRotationContainer) |
- ->layer(); |
- std::unique_ptr<ScreenRotationAnimation> current_layer_screen_rotation = |
+ ui::Layer* new_root_layer; |
+ if (new_layer_tree_owner_ && |
+ base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kAshEnableSmoothScreenRotation)) { |
+ // Make the current layers invisible if the copy request after rotation is |
+ // succeesful. |
+ GetScreenRotationContainer(root_window)->layer()->SetOpacity(0.0f); |
+ new_root_layer = new_layer_tree_owner_->root(); |
+ } else { |
+ new_root_layer = GetScreenRotationContainer(root_window)->layer(); |
+ } |
+ |
+ std::unique_ptr<ScreenRotationAnimation> new_layer_screen_rotation = |
base::MakeUnique<ScreenRotationAnimation>( |
- screen_rotation_container_layer, kRotationDegrees * rotation_factor, |
+ new_root_layer, kRotationDegrees * rotation_factor, |
0 /* end_degrees */, 0.0f, |
- screen_rotation_container_layer->opacity() /* target_opacity */, |
- pivot, duration, tween_type); |
+ new_root_layer->opacity() /* target_opacity */, pivot, duration, |
+ tween_type); |
- ui::LayerAnimator* current_layer_animator = |
- screen_rotation_container_layer->GetAnimator(); |
- current_layer_animator->set_preemption_strategy( |
+ ui::LayerAnimator* new_layer_animator = new_root_layer->GetAnimator(); |
+ new_layer_animator->set_preemption_strategy( |
ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
- std::unique_ptr<ui::LayerAnimationSequence> current_layer_animation_sequence = |
+ std::unique_ptr<ui::LayerAnimationSequence> new_layer_animation_sequence = |
base::MakeUnique<ui::LayerAnimationSequence>( |
- std::move(current_layer_screen_rotation)); |
- current_layer_animator->StartAnimation( |
- current_layer_animation_sequence.release()); |
+ std::move(new_layer_screen_rotation)); |
+ ui::Layer* old_root_layer = old_layer_tree_owner_->root(); |
+ const gfx::Rect original_screen_bounds = old_root_layer->GetTargetBounds(); |
// 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 |
@@ -345,28 +384,43 @@ void ScreenRotationAnimator::AnimateRotation( |
std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = |
base::MakeUnique<ui::LayerAnimationSequence>( |
std::move(old_layer_screen_rotation)); |
- // Add an observer so that the cloned layers can be cleaned up with the |
- // animation completes/aborts. |
- old_layer_animation_sequence->AddObserver( |
- old_layer_cleanup_observer.release()); |
+ |
// In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
// control the animation. |
- if (disable_animation_timers_for_test_) |
+ if (disable_animation_timers_for_test_) { |
+ if (new_layer_tree_owner_) |
+ new_layer_animator->set_disable_timer_for_test(true); |
old_layer_animator->set_disable_timer_for_test(true); |
+ } |
old_layer_animation_sequence->SetAnimationMetricsReporter( |
metrics_reporter_.get()); |
+ |
+ // Add an observer so that the cloned/copied layers can be cleaned up with the |
+ // animation completes/aborts. |
+ ui::CallbackLayerAnimationObserver* observer = |
+ new ui::CallbackLayerAnimationObserver( |
+ base::Bind(&AnimationEndedCallback, weak_factory_.GetWeakPtr())); |
+ if (new_layer_tree_owner_) { |
+ new_layer_animator->AddObserver(observer); |
+ new_layer_animator->StartAnimation(new_layer_animation_sequence.release()); |
+ } |
+ old_layer_animator->AddObserver(observer); |
old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); |
+ observer->SetActive(); |
rotation_request.reset(); |
} |
void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
display::Display::RotationSource source) { |
- if (GetCurrentScreenRotation(display_id_) == new_rotation) |
+ const display::Display::Rotation current_rotation = |
+ GetCurrentScreenRotation(display_id_); |
+ if (current_rotation == new_rotation) |
return; |
std::unique_ptr<ScreenRotationRequest> rotation_request = |
- base::MakeUnique<ScreenRotationRequest>(new_rotation, source); |
+ base::MakeUnique<ScreenRotationRequest>(current_rotation, new_rotation, |
+ source); |
if (is_rotating_) { |
last_pending_request_ = std::move(rotation_request); |
@@ -392,7 +446,9 @@ void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( |
void ScreenRotationAnimator::ProcessAnimationQueue() { |
is_rotating_ = false; |
+ StopAnimating(); |
old_layer_tree_owner_.reset(); |
+ new_layer_tree_owner_.reset(); |
if (last_pending_request_ && IsDisplayIdValid(display_id_)) { |
std::unique_ptr<ScreenRotationRequest> rotation_request = |
std::move(last_pending_request_); |
@@ -411,12 +467,14 @@ void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
} |
void ScreenRotationAnimator::StopAnimating() { |
- GetRootWindow(display_id_) |
- ->GetChildById(kShellWindowId_ScreenRotationContainer) |
- ->layer() |
- ->GetAnimator() |
- ->StopAnimating(); |
- old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
+ // |old_layer_tree_owner_| new_layer_tree_owner_| could be nullptr if the |
+ // copy request after screen rotation was canceled or failed. |
+ if (old_layer_tree_owner_) |
+ old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
+ if (new_layer_tree_owner_) { |
+ GetScreenRotationContainer(display_id_)->layer()->SetOpacity(1.0f); |
+ new_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
+ } |
} |
} // namespace ash |