Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 33 namespace ash { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 // The number of degrees the partial rotation animations animate through. | |
| 38 const int kPartialRotationDegrees = 20; | |
| 39 | |
| 40 // The time it takes for the normal speed rotation animations to run. | |
| 41 const int kRotationDurationInMs = 250; | |
| 42 | |
| 43 // The time it takes for the slow speed rotation animations to run. | |
| 44 const int kRotationDurationSlowInMs = 2500; | |
| 45 | |
| 46 // Clones all child layers of |to_clone| and adds the clones as children to | |
| 47 // |parent|. Recurses on each child of |to_clone|. | |
| 48 void CloneChildren(ui::Layer* to_clone, ui::Layer* parent) { | |
| 49 typedef std::vector<ui::Layer*> Layers; | |
| 50 // Make a copy of the children since RecreateLayer() mutates it. | |
| 51 Layers children(to_clone->children()); | |
| 52 for (Layers::const_iterator i = children.begin(); i != children.end(); ++i) { | |
| 53 ui::LayerOwner* owner = (*i)->owner(); | |
| 54 ui::Layer* clone = owner ? owner->RecreateLayer().release() : NULL; | |
| 55 if (clone) { | |
| 56 parent->Add(clone); | |
| 57 // RecreateLayer() moves the existing children to the new layer. Create a | |
| 58 // copy of those. | |
| 59 CloneChildren(owner->layer(), clone); | |
| 60 } | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 // Clones the layer tree owned by |window| and adds the cloned layer tree as a | |
| 65 // child of |window|'s layer. | |
| 66 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
| |
| 67 scoped_ptr<ui::LayerTreeOwner> cloned_layer_tree( | |
| 68 new ui::LayerTreeOwner(new ui::Layer())); | |
| 69 CloneChildren(window->layer(), cloned_layer_tree->root()); | |
| 70 | |
| 71 DCHECK(cloned_layer_tree->root()); | |
| 72 DCHECK(window->layer()); | |
| 73 | |
| 74 // Add the cloned layer tree into the root, so it will be rendered. | |
| 75 window->layer()->Add(cloned_layer_tree->root()); | |
| 76 window->layer()->StackAtTop(cloned_layer_tree->root()); | |
| 77 | |
| 78 return cloned_layer_tree.Pass(); | |
| 79 } | |
| 80 | |
| 81 // Gets the current display rotation for the display with the specified | |
| 82 // |display_id|. | |
| 83 gfx::Display::Rotation GetCurrentRotation(int64 display_id) { | |
| 84 return Shell::GetInstance() | |
| 85 ->display_manager() | |
| 86 ->GetDisplayInfo(display_id) | |
| 87 .rotation(); | |
| 88 } | |
| 89 | |
| 90 // Returns true if the rotation between |initial_rotation| and |new_rotation| is | |
| 91 // 180 degrees. | |
| 92 bool Is180DegreeFlip(gfx::Display::Rotation initial_rotation, | |
| 93 gfx::Display::Rotation new_rotation) { | |
| 94 return (initial_rotation + 2) % 4 == new_rotation; | |
| 95 } | |
| 96 | |
| 97 // A LayerAnimationObserver that will destroy the contained LayerTreeOwner when | |
| 98 // notified that a layer animation has ended or was aborted. | |
| 99 class LayerCleanupObserver : public ui::LayerAnimationObserver { | |
| 100 public: | |
| 101 explicit LayerCleanupObserver( | |
| 102 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner); | |
| 103 ~LayerCleanupObserver() override; | |
| 104 | |
| 105 // Get the root layer of the owned layer tree. | |
| 106 ui::Layer* GetRootLayer(); | |
| 107 | |
| 108 // ui::LayerAnimationObserver: | |
| 109 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; | |
| 110 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; | |
| 111 void OnLayerAnimationScheduled( | |
| 112 ui::LayerAnimationSequence* sequence) override {} | |
| 113 | |
| 114 protected: | |
| 115 // ui::LayerAnimationObserver: | |
| 116 bool RequiresNotificationWhenAnimatorDestroyed() const override { | |
| 117 return true; | |
| 118 } | |
| 119 void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override; | |
| 120 void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override; | |
| 121 | |
| 122 private: | |
| 123 // The owned layer tree. | |
| 124 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner_; | |
| 125 | |
| 126 // The LayerAnimationSequence that |this| has been attached to. Defaults to | |
| 127 // nullptr. | |
| 128 ui::LayerAnimationSequence* sequence_; | |
| 129 | |
| 130 DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver); | |
| 131 }; | |
| 132 | |
| 133 LayerCleanupObserver::LayerCleanupObserver( | |
| 134 scoped_ptr<ui::LayerTreeOwner> layer_tree_owner) | |
| 135 : layer_tree_owner_(layer_tree_owner.Pass()), sequence_(nullptr) { | |
| 136 } | |
| 137 | |
| 138 LayerCleanupObserver::~LayerCleanupObserver() { | |
| 139 // We must eplicitly detach from |sequence_| because we return true from | |
| 140 // RequiresNotificationWhenAnimatorDestroyed. | |
| 141 if (sequence_) | |
| 142 sequence_->RemoveObserver(this); | |
| 143 } | |
| 144 | |
| 145 ui::Layer* LayerCleanupObserver::GetRootLayer() { | |
| 146 return layer_tree_owner_->root(); | |
| 147 } | |
| 148 | |
| 149 void LayerCleanupObserver::OnLayerAnimationEnded( | |
| 150 ui::LayerAnimationSequence* sequence) { | |
| 151 delete this; | |
| 152 } | |
| 153 | |
| 154 void LayerCleanupObserver::OnLayerAnimationAborted( | |
| 155 ui::LayerAnimationSequence* sequence) { | |
| 156 delete this; | |
| 157 } | |
| 158 | |
| 159 void LayerCleanupObserver::OnAttachedToSequence( | |
| 160 ui::LayerAnimationSequence* sequence) { | |
| 161 sequence_ = sequence; | |
| 162 } | |
| 163 | |
| 164 void LayerCleanupObserver::OnDetachedFromSequence( | |
| 165 ui::LayerAnimationSequence* sequence) { | |
| 166 DCHECK(sequence == sequence_); | |
| 167 sequence_ = nullptr; | |
| 168 } | |
| 169 | |
| 170 // Set the screen orientation for the given |display| to |new_rotation| and | |
| 171 // animate the change. | |
| 172 void RotateScreen(gfx::Display display, | |
| 173 gfx::Display::Rotation new_rotation, | |
| 174 base::TimeDelta duration, | |
| 175 int rotation_degrees, | |
| 176 int rotation_degree_offset, | |
| 177 gfx::Tween::Type tween_type, | |
| 178 bool should_scale) { | |
| 179 aura::Window* root_window = | |
| 180 Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId( | |
| 181 display.id()); | |
| 182 | |
| 183 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
| |
| 184 // 180 degree rotations should animation clock-wise. | |
| 185 const int rotation_factor = | |
| 186 (GetCurrentRotation(display.id()) + 3) % 4 == new_rotation ? 1 : -1; | |
| 187 scoped_ptr<LayerCleanupObserver> layer_cleanup_observer( | |
| 188 new LayerCleanupObserver(CloneWindowLayers(root_window).Pass())); | |
| 189 | |
| 190 Shell::GetInstance()->display_manager()->SetDisplayRotation(display.id(), | |
| 191 new_rotation); | |
| 192 | |
| 193 const gfx::RectF rotated_screen_bounds = root_window->GetTargetBounds(); | |
| 194 const gfx::Point pivot = | |
| 195 Is180DegreeFlip(GetCurrentRotation(display.id()), new_rotation) | |
| 196 ? gfx::Point(rotated_screen_bounds.height() / 2, | |
| 197 rotated_screen_bounds.width() / 2) | |
| 198 : gfx::Point(rotated_screen_bounds.width() / 2, | |
| 199 rotated_screen_bounds.height() / 2); | |
| 200 | |
| 201 gfx::Point3F new_layer_initial_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); | |
| 202 gfx::Point3F old_layer_target_scale = gfx::Point3F(1.0f, 1.0f, 1.0f); | |
| 203 | |
| 204 if (should_scale) { | |
| 205 new_layer_initial_scale = gfx::Point3F( | |
| 206 original_screen_bounds.width() / rotated_screen_bounds.width(), | |
| 207 original_screen_bounds.height() / rotated_screen_bounds.height(), 1.0f); | |
| 208 | |
| 209 old_layer_target_scale = gfx::Point3F( | |
| 210 rotated_screen_bounds.width() / original_screen_bounds.width(), | |
| 211 rotated_screen_bounds.height() / original_screen_bounds.height(), 1.0f); | |
| 212 } | |
| 213 | |
| 214 // We must animate each non-cloned child layer individually because the cloned | |
| 215 // layer was added as a child to |root_window|'s layer so that it will be | |
| 216 // rendered. | |
| 217 for (ui::Layer* child_layer : root_window->layer()->children()) { | |
| 218 // Skip the cloned layer because it has a different animation. | |
| 219 if (child_layer == layer_cleanup_observer->GetRootLayer()) | |
| 220 continue; | |
| 221 | |
| 222 scoped_ptr<ScreenRotationAnimation> screen_rotation( | |
| 223 new ScreenRotationAnimation( | |
| 224 child_layer, rotation_degrees * rotation_factor, | |
| 225 0 /* end_degrees */, new_layer_initial_scale, | |
| 226 gfx::Point3F(1.0f, 1.0f, 1.0f), pivot, duration)); | |
| 227 | |
| 228 screen_rotation->set_tween_type(tween_type); | |
| 229 screen_rotation->set_target_opacity(1.0f); | |
| 230 | |
| 231 ui::LayerAnimator* animator = child_layer->GetAnimator(); | |
| 232 animator->set_preemption_strategy( | |
| 233 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
| 234 scoped_ptr<ui::LayerAnimationSequence> animation_sequence( | |
| 235 new ui::LayerAnimationSequence(screen_rotation.release())); | |
| 236 animator->StartAnimation(animation_sequence.release()); | |
| 237 } | |
| 238 | |
| 239 // The old layer will also be transformed into the new orientation. We will | |
| 240 // translate it so that the old layer's center point aligns with the new | |
| 241 // orientation's center point and use that center point as the pivot for the | |
| 242 // rotation animation. | |
| 243 gfx::Transform translate_transform = | |
| 244 layer_cleanup_observer->GetRootLayer()->GetTargetTransform(); | |
| 245 translate_transform.Translate( | |
| 246 (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, | |
| 247 (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); | |
| 248 layer_cleanup_observer->GetRootLayer()->SetTransform(translate_transform); | |
| 249 | |
| 250 scoped_ptr<ScreenRotationAnimation> screen_rotation( | |
| 251 new ScreenRotationAnimation( | |
| 252 layer_cleanup_observer->GetRootLayer(), | |
| 253 (rotation_degrees + rotation_degree_offset) * rotation_factor, | |
| 254 rotation_degree_offset * rotation_factor, | |
| 255 gfx::Point3F(1.0f, 1.0f, 1.0f), old_layer_target_scale, pivot, | |
| 256 duration)); | |
| 257 | |
| 258 screen_rotation->set_tween_type(tween_type); | |
| 259 screen_rotation->set_target_opacity(0.0f); | |
| 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_(Shell::GetInstance()->display_manager()->GetDisplayForId( | |
| 277 display_id)) { | |
| 278 } | |
| 279 | |
| 280 ScreenRotationAnimator::ScreenRotationAnimator(gfx::Display display) | |
| 281 : display_(display) { | |
| 282 } | |
| 283 | |
| 284 ScreenRotationAnimator::~ScreenRotationAnimator() { | |
| 285 } | |
| 286 | |
| 287 void ScreenRotationAnimator::Rotate(gfx::Display::Rotation new_rotation) { | |
| 288 const gfx::Display::Rotation current_rotation = | |
| 289 GetCurrentRotation(display_.id()); | |
| 290 | |
| 291 if (current_rotation == new_rotation) | |
| 292 return; | |
| 293 | |
| 294 const std::string switch_value = | |
| 295 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | |
| 296 switches::kAshEnableScreenRotationAnimation); | |
| 297 | |
| 298 if ("" == switch_value) { | |
|
oshima
2015/03/04 21:31:35
switch_value.empty()
bruthig
2015/03/06 16:00:33
Done.
| |
| 299 Shell::GetInstance()->display_manager()->SetDisplayRotation(display_.id(), | |
| 300 new_rotation); | |
| 301 return; | |
| 302 } | |
| 303 | |
| 304 if ("partial-rotation" == switch_value || | |
| 305 "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.
| |
| 306 RotateScreen( | |
| 307 display_, new_rotation, | |
| 308 base::TimeDelta::FromMilliseconds("partial-rotation" == switch_value | |
| 309 ? kRotationDurationInMs | |
| 310 : kRotationDurationSlowInMs), | |
| 311 kPartialRotationDegrees, Is180DegreeFlip(current_rotation, new_rotation) | |
| 312 ? 180 - kPartialRotationDegrees | |
| 313 : 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.
| |
| 314 gfx::Tween::FAST_OUT_LINEAR_IN, false /* should_scale */); | |
| 315 } else if ("full-rotation" == switch_value || | |
| 316 "full-rotation-slow" == switch_value) { | |
| 317 RotateScreen(display_, new_rotation, base::TimeDelta::FromMilliseconds( | |
| 318 "full-rotation" == switch_value | |
| 319 ? kRotationDurationInMs | |
| 320 : kRotationDurationSlowInMs), | |
|
oshima
2015/03/04 21:31:35
ditto
bruthig
2015/03/06 16:00:33
Done.
| |
| 321 Is180DegreeFlip(current_rotation, new_rotation) ? 180 : 90, | |
| 322 0 /* rotation_degree_offset */, gfx::Tween::FAST_OUT_LINEAR_IN, | |
| 323 true /* should_scale */); | |
| 324 } else { | |
| 325 DCHECK(false); | |
|
oshima
2015/03/04 21:31:35
NOTREACHED()
bruthig
2015/03/06 16:00:33
Done.
| |
| 326 } | |
| 327 } | |
| 328 | |
| 329 } // namespace ash | |
| OLD | NEW |