bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ash/rotator/screen_rotation_animator.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include "ash/ash_switches.h" |
| 11 | #include "ash/display/display_controller.h" |
| 12 | #include "ash/display/display_info.h" |
| 13 | #include "ash/display/display_manager.h" |
| 14 | #include "ash/rotator/screen_rotation_animation.h" |
| 15 | #include "ash/shell.h" |
| 16 | #include "base/command_line.h" |
| 17 | #include "base/time/time.h" |
| 18 | #include "ui/aura/window.h" |
| 19 | #include "ui/compositor/layer.h" |
| 20 | #include "ui/compositor/layer_animation_observer.h" |
| 21 | #include "ui/compositor/layer_animation_sequence.h" |
| 22 | #include "ui/compositor/layer_animator.h" |
| 23 | #include "ui/compositor/layer_owner.h" |
| 24 | #include "ui/compositor/layer_tree_owner.h" |
| 25 | #include "ui/gfx/animation/tween.h" |
| 26 | #include "ui/gfx/display.h" |
| 27 | #include "ui/gfx/geometry/point.h" |
| 28 | #include "ui/gfx/geometry/rect.h" |
| 29 | #include "ui/gfx/geometry/rect_f.h" |
| 30 | #include "ui/gfx/transform.h" |
| 31 | #include "ui/gfx/transform_util.h" |
| 32 | #include "ui/wm/core/window_util.h" |
| 33 | |
| 34 | namespace ash { |
| 35 | |
| 36 | namespace { |
| 37 | |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 38 | // Switch value for the default animation. |
| 39 | const char kRotationAnimation_Default[] = ""; |
| 40 | |
| 41 | // Switch value for no animation. |
| 42 | const char kRotationAnimation_None[] = "none"; |
| 43 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 44 | // Switch value for an animation that will rotate the initial orientation's |
| 45 | // layer towards the new orientation and the new orientation's layer in to |
| 46 | // position from the initial orientation through an arc of |
| 47 | // |kPartialRotationDegrees| degrees. The initial orientation's layer will be |
| 48 | // faded out as well. |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 49 | const char kRotationAnimation_Partial[] = "partial-rotation"; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 50 | |
| 51 | // Switch value for an animation that will rotate both the initial and target |
| 52 | // orientation's layers from the initial orientation into the target |
| 53 | // orientation. The initial orientation's layer will be faded out as well and an |
| 54 | // interpolated scaling factor is applied to the animation so that the initial |
| 55 | // and target layers are both the same size throughout the animation. |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 56 | const char kRotationAnimation_Full[] = "full-rotation"; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 57 | |
| 58 | // The number of degrees the partial rotation animations animate through. |
| 59 | const int kPartialRotationDegrees = 20; |
| 60 | |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 61 | // The time it takes for the rotation animations to run. |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 62 | const int kRotationDurationInMs = 250; |
| 63 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 64 | // Gets the current display rotation for the display with the specified |
| 65 | // |display_id|. |
| 66 | gfx::Display::Rotation GetCurrentRotation(int64 display_id) { |
| 67 | return Shell::GetInstance() |
| 68 | ->display_manager() |
| 69 | ->GetDisplayInfo(display_id) |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 70 | .GetActiveRotation(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Returns true if the rotation between |initial_rotation| and |new_rotation| is |
| 74 | // 180 degrees. |
| 75 | bool Is180DegreeFlip(gfx::Display::Rotation initial_rotation, |
| 76 | gfx::Display::Rotation new_rotation) { |
| 77 | return (initial_rotation + 2) % 4 == new_rotation; |
| 78 | } |
| 79 | |
| 80 | // A LayerAnimationObserver that will destroy the contained LayerTreeOwner when |
| 81 | // notified that a layer animation has ended or was aborted. |
| 82 | class LayerCleanupObserver : public ui::LayerAnimationObserver { |
| 83 | public: |
| 84 | explicit LayerCleanupObserver( |
| 85 | scoped_ptr<ui::LayerTreeOwner> layer_tree_owner); |
| 86 | ~LayerCleanupObserver() override; |
| 87 | |
| 88 | // Get the root layer of the owned layer tree. |
| 89 | ui::Layer* GetRootLayer(); |
| 90 | |
| 91 | // ui::LayerAnimationObserver: |
| 92 | void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; |
| 93 | void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; |
| 94 | void OnLayerAnimationScheduled( |
| 95 | ui::LayerAnimationSequence* sequence) override {} |
| 96 | |
| 97 | protected: |
| 98 | // ui::LayerAnimationObserver: |
| 99 | bool RequiresNotificationWhenAnimatorDestroyed() const override { |
| 100 | return true; |
| 101 | } |
| 102 | void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override; |
| 103 | void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override; |
| 104 | |
| 105 | private: |
jonross | 7351feb | 2015-04-23 22:10:26 | [diff] [blame] | 106 | // Aborts the active animations of the layer, and recurses upon its child |
| 107 | // layers. |
| 108 | void AbortAnimations(ui::Layer* layer); |
| 109 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 110 | // The owned layer tree. |
| 111 | scoped_ptr<ui::LayerTreeOwner> layer_tree_owner_; |
| 112 | |
| 113 | // The LayerAnimationSequence that |this| has been attached to. Defaults to |
| 114 | // nullptr. |
| 115 | ui::LayerAnimationSequence* sequence_; |
| 116 | |
| 117 | DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver); |
| 118 | }; |
| 119 | |
| 120 | LayerCleanupObserver::LayerCleanupObserver( |
| 121 | scoped_ptr<ui::LayerTreeOwner> layer_tree_owner) |
| 122 | : layer_tree_owner_(layer_tree_owner.Pass()), sequence_(nullptr) { |
| 123 | } |
| 124 | |
| 125 | LayerCleanupObserver::~LayerCleanupObserver() { |
| 126 | // We must eplicitly detach from |sequence_| because we return true from |
| 127 | // RequiresNotificationWhenAnimatorDestroyed. |
| 128 | if (sequence_) |
| 129 | sequence_->RemoveObserver(this); |
jonross | 7351feb | 2015-04-23 22:10:26 | [diff] [blame] | 130 | AbortAnimations(layer_tree_owner_->root()); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | ui::Layer* LayerCleanupObserver::GetRootLayer() { |
| 134 | return layer_tree_owner_->root(); |
| 135 | } |
| 136 | |
| 137 | void LayerCleanupObserver::OnLayerAnimationEnded( |
| 138 | ui::LayerAnimationSequence* sequence) { |
| 139 | delete this; |
| 140 | } |
| 141 | |
| 142 | void LayerCleanupObserver::OnLayerAnimationAborted( |
| 143 | ui::LayerAnimationSequence* sequence) { |
| 144 | delete this; |
| 145 | } |
| 146 | |
| 147 | void LayerCleanupObserver::OnAttachedToSequence( |
| 148 | ui::LayerAnimationSequence* sequence) { |
| 149 | sequence_ = sequence; |
| 150 | } |
| 151 | |
| 152 | void LayerCleanupObserver::OnDetachedFromSequence( |
| 153 | ui::LayerAnimationSequence* sequence) { |
| 154 | DCHECK_EQ(sequence, sequence_); |
| 155 | sequence_ = nullptr; |
| 156 | } |
| 157 | |
jonross | 7351feb | 2015-04-23 22:10:26 | [diff] [blame] | 158 | void LayerCleanupObserver::AbortAnimations(ui::Layer* layer) { |
| 159 | for (ui::Layer* child_layer : layer->children()) |
| 160 | AbortAnimations(child_layer); |
| 161 | layer->GetAnimator()->AbortAllAnimations(); |
| 162 | } |
| 163 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 164 | // Set the screen orientation for the given |display| to |new_rotation| and |
| 165 | // animate the change. |
| 166 | void RotateScreen(int64 display_id, |
| 167 | gfx::Display::Rotation new_rotation, |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 168 | gfx::Display::RotationSource source, |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 169 | base::TimeDelta duration, |
| 170 | int rotation_degrees, |
| 171 | int rotation_degree_offset, |
| 172 | gfx::Tween::Type tween_type, |
| 173 | bool should_scale) { |
| 174 | aura::Window* root_window = |
| 175 | Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId( |
| 176 | display_id); |
| 177 | |
| 178 | const gfx::Display::Rotation initial_orientation = |
| 179 | GetCurrentRotation(display_id); |
| 180 | |
| 181 | const gfx::RectF original_screen_bounds = root_window->GetTargetBounds(); |
| 182 | // 180 degree rotations should animate clock-wise. |
| 183 | const int rotation_factor = |
| 184 | (initial_orientation + 3) % 4 == new_rotation ? 1 : -1; |
| 185 | |
| 186 | scoped_ptr<ui::LayerTreeOwner> old_layer_tree = |
| 187 | wm::RecreateLayers(root_window); |
| 188 | |
| 189 | // Add the cloned layer tree in to the root, so it will be rendered. |
| 190 | root_window->layer()->Add(old_layer_tree->root()); |
| 191 | root_window->layer()->StackAtTop(old_layer_tree->root()); |
| 192 | |
| 193 | scoped_ptr<LayerCleanupObserver> layer_cleanup_observer( |
| 194 | new LayerCleanupObserver(old_layer_tree.Pass())); |
| 195 | |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 196 | Shell::GetInstance()->display_manager()->SetDisplayRotation( |
| 197 | display_id, new_rotation, source); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 198 | |
| 199 | const gfx::RectF rotated_screen_bounds = root_window->GetTargetBounds(); |
| 200 | const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
| 201 | rotated_screen_bounds.height() / 2); |
| 202 | |
| 203 | gfx::Point3F new_layer_initial_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); |
| 204 | gfx::Point3F old_layer_target_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); |
| 205 | |
| 206 | if (should_scale) { |
| 207 | new_layer_initial_scale = gfx::Point3F( |
| 208 | original_screen_bounds.width() / rotated_screen_bounds.width(), |
| 209 | original_screen_bounds.height() / rotated_screen_bounds.height(), 1.0f); |
| 210 | |
| 211 | old_layer_target_scale = gfx::Point3F( |
| 212 | rotated_screen_bounds.width() / original_screen_bounds.width(), |
| 213 | rotated_screen_bounds.height() / original_screen_bounds.height(), 1.0f); |
| 214 | } |
| 215 | |
| 216 | // We must animate each non-cloned child layer individually because the cloned |
| 217 | // layer was added as a child to |root_window|'s layer so that it will be |
| 218 | // rendered. |
| 219 | // TODO(bruthig): Add a NOT_DRAWN layer in between the root_window's layer and |
| 220 | // its current children so that we only need to initiate two |
| 221 | // LayerAnimationSequences. One for the new layers and one for the old layer. |
| 222 | for (ui::Layer* child_layer : root_window->layer()->children()) { |
| 223 | // Skip the cloned layer because it has a different animation. |
| 224 | if (child_layer == layer_cleanup_observer->GetRootLayer()) |
| 225 | continue; |
| 226 | |
| 227 | scoped_ptr<ScreenRotationAnimation> screen_rotation( |
| 228 | new ScreenRotationAnimation( |
| 229 | child_layer, rotation_degrees * rotation_factor, |
| 230 | 0 /* end_degrees */, child_layer->opacity(), |
| 231 | 1.0f /* target_opacity */, new_layer_initial_scale, |
| 232 | gfx::Point3F(1.0f, 1.0f, 1.0f), pivot, duration, tween_type)); |
| 233 | |
| 234 | ui::LayerAnimator* animator = child_layer->GetAnimator(); |
| 235 | animator->set_preemption_strategy( |
| 236 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 237 | scoped_ptr<ui::LayerAnimationSequence> animation_sequence( |
| 238 | new ui::LayerAnimationSequence(screen_rotation.release())); |
| 239 | animator->StartAnimation(animation_sequence.release()); |
| 240 | } |
| 241 | |
| 242 | // The old layer will also be transformed into the new orientation. We will |
| 243 | // translate it so that the old layer's center point aligns with the new |
| 244 | // orientation's center point and use that center point as the pivot for the |
| 245 | // rotation animation. |
| 246 | gfx::Transform translate_transform; |
| 247 | translate_transform.Translate( |
| 248 | (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
| 249 | (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
| 250 | layer_cleanup_observer->GetRootLayer()->SetTransform(translate_transform); |
| 251 | |
| 252 | scoped_ptr<ScreenRotationAnimation> screen_rotation( |
| 253 | new ScreenRotationAnimation( |
| 254 | layer_cleanup_observer->GetRootLayer(), |
| 255 | (rotation_degrees + rotation_degree_offset) * rotation_factor, |
| 256 | rotation_degree_offset * rotation_factor, |
| 257 | layer_cleanup_observer->GetRootLayer()->opacity(), |
| 258 | 0.0f /* target_opacity */, gfx::Point3F(1.0f, 1.0f, 1.0f), |
| 259 | old_layer_target_scale, pivot, duration, tween_type)); |
| 260 | |
| 261 | ui::LayerAnimator* animator = |
| 262 | layer_cleanup_observer->GetRootLayer()->GetAnimator(); |
| 263 | animator->set_preemption_strategy( |
| 264 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
| 265 | scoped_ptr<ui::LayerAnimationSequence> animation_sequence( |
| 266 | new ui::LayerAnimationSequence(screen_rotation.release())); |
| 267 | // Add an observer so that the cloned layers can be cleaned up with the |
| 268 | // animation completes/aborts. |
| 269 | animation_sequence->AddObserver(layer_cleanup_observer.release()); |
| 270 | animator->StartAnimation(animation_sequence.release()); |
| 271 | } |
| 272 | |
| 273 | } // namespace |
| 274 | |
| 275 | ScreenRotationAnimator::ScreenRotationAnimator(int64 display_id) |
| 276 | : display_id_(display_id) { |
| 277 | } |
| 278 | |
| 279 | ScreenRotationAnimator::~ScreenRotationAnimator() { |
| 280 | } |
| 281 | |
bruthig | f57a7bba | 2015-04-24 21:19:31 | [diff] [blame^] | 282 | bool ScreenRotationAnimator::CanAnimate() const { |
| 283 | return Shell::GetInstance() |
| 284 | ->display_manager() |
| 285 | ->GetDisplayForId(display_id_) |
| 286 | .is_valid(); |
| 287 | } |
| 288 | |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 289 | void ScreenRotationAnimator::Rotate(gfx::Display::Rotation new_rotation, |
| 290 | gfx::Display::RotationSource source) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 291 | const gfx::Display::Rotation current_rotation = |
| 292 | GetCurrentRotation(display_id_); |
| 293 | |
| 294 | if (current_rotation == new_rotation) |
| 295 | return; |
| 296 | |
| 297 | const std::string switch_value = |
| 298 | base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 299 | switches::kAshEnableScreenRotationAnimation); |
| 300 | |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 301 | if (switch_value == kRotationAnimation_None) { |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 302 | Shell::GetInstance()->display_manager()->SetDisplayRotation( |
| 303 | display_id_, new_rotation, source); |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 304 | } else if (kRotationAnimation_Default == switch_value || |
| 305 | kRotationAnimation_Partial == switch_value) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 306 | const int rotation_degree_offset = |
| 307 | Is180DegreeFlip(current_rotation, new_rotation) |
| 308 | ? 180 - kPartialRotationDegrees |
| 309 | : 90 - kPartialRotationDegrees; |
| 310 | |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 311 | RotateScreen(display_id_, new_rotation, source, |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 312 | base::TimeDelta::FromMilliseconds(kRotationDurationInMs), |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 313 | kPartialRotationDegrees, rotation_degree_offset, |
| 314 | gfx::Tween::FAST_OUT_LINEAR_IN, false /* should_scale */); |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 315 | } else if (kRotationAnimation_Full == switch_value) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 316 | const int rotation_degrees = |
| 317 | Is180DegreeFlip(current_rotation, new_rotation) ? 180 : 90; |
| 318 | |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 319 | RotateScreen(display_id_, new_rotation, source, |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 320 | base::TimeDelta::FromMilliseconds(kRotationDurationInMs), |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 321 | rotation_degrees, 0 /* rotation_degree_offset */, |
| 322 | gfx::Tween::FAST_OUT_LINEAR_IN, true /* should_scale */); |
| 323 | } else { |
| 324 | NOTREACHED(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | } // namespace ash |