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> |
dcheng | cbf0d9d | 2015-12-27 22:49:23 | [diff] [blame] | 8 | #include <utility> |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 11 | #include "ash/display/window_tree_host_manager.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 12 | #include "ash/rotator/screen_rotation_animation.h" |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 13 | #include "ash/rotator/screen_rotation_animator_observer.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 14 | #include "ash/shell.h" |
| 15 | #include "base/command_line.h" |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 16 | #include "base/memory/ptr_util.h" |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 17 | #include "base/metrics/histogram_macros.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 18 | #include "base/time/time.h" |
| 19 | #include "ui/aura/window.h" |
| 20 | #include "ui/compositor/layer.h" |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 21 | #include "ui/compositor/layer_animation_element.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 22 | #include "ui/compositor/layer_animation_observer.h" |
| 23 | #include "ui/compositor/layer_animation_sequence.h" |
| 24 | #include "ui/compositor/layer_animator.h" |
| 25 | #include "ui/compositor/layer_owner.h" |
| 26 | #include "ui/compositor/layer_tree_owner.h" |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 27 | #include "ui/display/display.h" |
rjkroege | 72f8154f | 2016-10-29 00:49:02 | [diff] [blame] | 28 | #include "ui/display/manager/display_manager.h" |
rjkroege | 259c0188 | 2016-08-30 19:29:50 | [diff] [blame] | 29 | #include "ui/display/manager/managed_display_info.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 30 | #include "ui/gfx/animation/tween.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 31 | #include "ui/gfx/geometry/point.h" |
| 32 | #include "ui/gfx/geometry/rect.h" |
| 33 | #include "ui/gfx/geometry/rect_f.h" |
| 34 | #include "ui/gfx/transform.h" |
| 35 | #include "ui/gfx/transform_util.h" |
| 36 | #include "ui/wm/core/window_util.h" |
| 37 | |
| 38 | namespace ash { |
| 39 | |
| 40 | namespace { |
| 41 | |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 42 | // The number of degrees that the rotation animations animate through. |
| 43 | const int kRotationDegrees = 20; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 44 | |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 45 | // The time it takes for the rotation animations to run. |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 46 | const int kRotationDurationInMs = 250; |
| 47 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 48 | // The rotation factors. |
| 49 | const int kCounterClockWiseRotationFactor = 1; |
| 50 | const int kClockWiseRotationFactor = -1; |
| 51 | |
| 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) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 61 | return Shell::GetInstance() |
| 62 | ->display_manager() |
| 63 | ->GetDisplayInfo(display_id) |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 64 | .GetActiveRotation(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 65 | } |
| 66 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 67 | bool IsDisplayIdValid(int64_t display_id) { |
| 68 | return Shell::GetInstance()->display_manager()->IsDisplayIdValid(display_id); |
| 69 | } |
| 70 | |
| 71 | // 180 degree rotations should animate clock-wise. |
| 72 | int GetRotationFactor(display::Display::Rotation initial_rotation, |
| 73 | display::Display::Rotation new_rotation) { |
| 74 | return (initial_rotation + 3) % 4 == new_rotation |
| 75 | ? kCounterClockWiseRotationFactor |
| 76 | : kClockWiseRotationFactor; |
| 77 | } |
| 78 | |
| 79 | aura::Window* GetRootWindow(int64_t display_id) { |
| 80 | return Shell::GetInstance() |
| 81 | ->window_tree_host_manager() |
| 82 | ->GetRootWindowForDisplayId(display_id); |
| 83 | } |
| 84 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 85 | // Returns true if the rotation between |initial_rotation| and |new_rotation| is |
| 86 | // 180 degrees. |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 87 | bool Is180DegreeFlip(display::Display::Rotation initial_rotation, |
| 88 | display::Display::Rotation new_rotation) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 89 | return (initial_rotation + 2) % 4 == new_rotation; |
| 90 | } |
| 91 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 92 | // Returns the initial degrees the old layer animation to begin with. |
| 93 | int GetInitialDegrees(display::Display::Rotation initial_rotation, |
| 94 | display::Display::Rotation new_rotation) { |
| 95 | return (Is180DegreeFlip(initial_rotation, new_rotation) ? 180 : 90); |
| 96 | } |
| 97 | |
| 98 | // A LayerAnimationObserver that will destroy the contained LayerTreeOwner |
| 99 | // when notified that a layer animation has ended or was aborted. |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 100 | class LayerCleanupObserver : public ui::LayerAnimationObserver { |
| 101 | public: |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 102 | // Takes WeakPtr of ScreenRotationAnimator. |this| may outlive the |animator_| |
| 103 | // instance and the |animator_| isn't detaching itself as an observer when |
| 104 | // being destroyed. However, ideally, when |animator_| is destroying, |
| 105 | // deleting |old_layer_tree_owner_| will trigger OnLayerAnimationAborted and |
| 106 | // delete |this| before |animator_| deleted. |
| 107 | explicit LayerCleanupObserver(base::WeakPtr<ScreenRotationAnimator> animator); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 108 | ~LayerCleanupObserver() override; |
| 109 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 110 | // ui::LayerAnimationObserver: |
| 111 | void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override; |
| 112 | void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override; |
| 113 | void OnLayerAnimationScheduled( |
| 114 | ui::LayerAnimationSequence* sequence) override {} |
| 115 | |
| 116 | protected: |
| 117 | // ui::LayerAnimationObserver: |
| 118 | bool RequiresNotificationWhenAnimatorDestroyed() const override { |
| 119 | return true; |
| 120 | } |
| 121 | void OnAttachedToSequence(ui::LayerAnimationSequence* sequence) override; |
| 122 | void OnDetachedFromSequence(ui::LayerAnimationSequence* sequence) override; |
| 123 | |
| 124 | private: |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 125 | base::WeakPtr<ScreenRotationAnimator> animator_; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 126 | |
| 127 | // The LayerAnimationSequence that |this| has been attached to. Defaults to |
| 128 | // nullptr. |
| 129 | ui::LayerAnimationSequence* sequence_; |
| 130 | |
| 131 | DISALLOW_COPY_AND_ASSIGN(LayerCleanupObserver); |
| 132 | }; |
| 133 | |
| 134 | LayerCleanupObserver::LayerCleanupObserver( |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 135 | base::WeakPtr<ScreenRotationAnimator> animator) |
| 136 | : animator_(animator), sequence_(nullptr) {} |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 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); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void LayerCleanupObserver::OnLayerAnimationEnded( |
| 146 | ui::LayerAnimationSequence* sequence) { |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 147 | if (animator_) |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 148 | animator_->ProcessAnimationQueue(); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 149 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 150 | delete this; |
| 151 | } |
| 152 | |
| 153 | void LayerCleanupObserver::OnLayerAnimationAborted( |
| 154 | ui::LayerAnimationSequence* sequence) { |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 155 | if (animator_) |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 156 | animator_->ProcessAnimationQueue(); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 157 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 158 | delete this; |
| 159 | } |
| 160 | |
| 161 | void LayerCleanupObserver::OnAttachedToSequence( |
| 162 | ui::LayerAnimationSequence* sequence) { |
| 163 | sequence_ = sequence; |
| 164 | } |
| 165 | |
| 166 | void LayerCleanupObserver::OnDetachedFromSequence( |
| 167 | ui::LayerAnimationSequence* sequence) { |
| 168 | DCHECK_EQ(sequence, sequence_); |
| 169 | sequence_ = nullptr; |
| 170 | } |
| 171 | |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 172 | class ScreenRotationAnimationMetricsReporter |
| 173 | : public ui::AnimationMetricsReporter { |
| 174 | public: |
| 175 | ScreenRotationAnimationMetricsReporter() {} |
| 176 | ~ScreenRotationAnimationMetricsReporter() override {} |
| 177 | |
| 178 | void Report(int value) override { |
| 179 | UMA_HISTOGRAM_PERCENTAGE("Ash.Rotation.AnimationSmoothness", value); |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); |
| 184 | }; |
| 185 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 186 | } // namespace |
jonross | 7351feb | 2015-04-23 22:10:26 | [diff] [blame] | 187 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 188 | struct ScreenRotationAnimator::ScreenRotationRequest { |
| 189 | ScreenRotationRequest(display::Display::Rotation to_rotation, |
| 190 | display::Display::RotationSource from_source) |
| 191 | : new_rotation(to_rotation), source(from_source) {} |
| 192 | display::Display::Rotation new_rotation; |
| 193 | display::Display::RotationSource source; |
| 194 | }; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 195 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 196 | ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) |
| 197 | : display_id_(display_id), |
| 198 | is_rotating_(false), |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 199 | metrics_reporter_( |
| 200 | base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 201 | disable_animation_timers_for_test_(false), |
| 202 | weak_factory_(this) {} |
| 203 | |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 204 | ScreenRotationAnimator::~ScreenRotationAnimator() { |
| 205 | // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from |
| 206 | // calling a method on the |animator_|. |
| 207 | weak_factory_.InvalidateWeakPtrs(); |
| 208 | |
| 209 | // Explicitly reset the |old_layer_tree_owner_| and |metrics_reporter_| in |
| 210 | // order to make sure |metrics_reporter_| outlives the attached animation |
| 211 | // sequence. |
| 212 | old_layer_tree_owner_.reset(); |
| 213 | metrics_reporter_.reset(); |
| 214 | } |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 215 | |
| 216 | void ScreenRotationAnimator::AnimateRotation( |
| 217 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 218 | aura::Window* root_window = GetRootWindow(display_id_); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 219 | |
danakj | 335602b | 2015-08-31 23:35:03 | [diff] [blame] | 220 | const gfx::Rect original_screen_bounds = root_window->GetTargetBounds(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 221 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 222 | const int rotation_factor = GetRotationFactor( |
| 223 | GetCurrentScreenRotation(display_id_), rotation_request->new_rotation); |
| 224 | |
| 225 | const int old_layer_initial_rotation_degrees = GetInitialDegrees( |
| 226 | GetCurrentScreenRotation(display_id_), rotation_request->new_rotation); |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 227 | |
| 228 | const base::TimeDelta duration = |
| 229 | base::TimeDelta::FromMilliseconds(kRotationDurationInMs); |
| 230 | |
| 231 | const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; |
| 232 | |
dcheng | a9454747 | 2016-04-08 08:41:11 | [diff] [blame] | 233 | std::unique_ptr<ui::LayerTreeOwner> old_layer_tree = |
domlaskowski | 4b33bfe | 2016-10-27 00:34:22 | [diff] [blame] | 234 | ::wm::RecreateLayers(root_window); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 235 | old_layer_tree->root()->set_name("ScreenRotationAnimator:old_layer_tree"); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 236 | |
| 237 | // Add the cloned layer tree in to the root, so it will be rendered. |
| 238 | root_window->layer()->Add(old_layer_tree->root()); |
| 239 | root_window->layer()->StackAtTop(old_layer_tree->root()); |
| 240 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 241 | old_layer_tree_owner_ = std::move(old_layer_tree); |
| 242 | std::unique_ptr<LayerCleanupObserver> old_layer_cleanup_observer( |
| 243 | new LayerCleanupObserver(weak_factory_.GetWeakPtr())); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 244 | |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 245 | Shell::GetInstance()->display_manager()->SetDisplayRotation( |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 246 | display_id_, rotation_request->new_rotation, rotation_request->source); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 247 | |
danakj | 335602b | 2015-08-31 23:35:03 | [diff] [blame] | 248 | const gfx::Rect rotated_screen_bounds = root_window->GetTargetBounds(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 249 | const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
| 250 | rotated_screen_bounds.height() / 2); |
| 251 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 252 | ui::Layer* old_root_layer = old_layer_tree_owner_->root(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 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. |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 261 | if (child_layer == old_root_layer) |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 262 | continue; |
| 263 | |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 264 | std::unique_ptr<ScreenRotationAnimation> screen_rotation = |
| 265 | base::MakeUnique<ScreenRotationAnimation>( |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 266 | child_layer, kRotationDegrees * rotation_factor, |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 267 | 0 /* end_degrees */, child_layer->opacity(), |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 268 | 1.0f /* target_opacity */, pivot, duration, tween_type); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 269 | |
| 270 | ui::LayerAnimator* animator = child_layer->GetAnimator(); |
| 271 | animator->set_preemption_strategy( |
| 272 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 273 | std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = |
| 274 | base::MakeUnique<ui::LayerAnimationSequence>( |
| 275 | std::move(screen_rotation)); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 276 | animator->StartAnimation(animation_sequence.release()); |
| 277 | } |
| 278 | |
| 279 | // 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 |
| 281 | // orientation's center point and use that center point as the pivot for the |
| 282 | // rotation animation. |
| 283 | gfx::Transform translate_transform; |
| 284 | translate_transform.Translate( |
| 285 | (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
| 286 | (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 287 | old_root_layer->SetTransform(translate_transform); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 288 | |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 289 | std::unique_ptr<ScreenRotationAnimation> screen_rotation = |
| 290 | base::MakeUnique<ScreenRotationAnimation>( |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 291 | old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 292 | (old_layer_initial_rotation_degrees - kRotationDegrees) * |
| 293 | rotation_factor, |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 294 | old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, |
| 295 | tween_type); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 296 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 297 | ui::LayerAnimator* animator = old_root_layer->GetAnimator(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 298 | animator->set_preemption_strategy( |
| 299 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 300 | std::unique_ptr<ui::LayerAnimationSequence> animation_sequence = |
| 301 | base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 302 | // Add an observer so that the cloned layers can be cleaned up with the |
| 303 | // animation completes/aborts. |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 304 | animation_sequence->AddObserver(old_layer_cleanup_observer.release()); |
| 305 | // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
| 306 | // control the animation. |
| 307 | if (disable_animation_timers_for_test_) |
| 308 | animator->set_disable_timer_for_test(true); |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame^] | 309 | animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 310 | animator->StartAnimation(animation_sequence.release()); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 311 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 312 | rotation_request.reset(); |
bruthig | f57a7bba | 2015-04-24 21:19:31 | [diff] [blame] | 313 | } |
| 314 | |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 315 | void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
| 316 | display::Display::RotationSource source) { |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 317 | if (GetCurrentScreenRotation(display_id_) == new_rotation) |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 318 | return; |
| 319 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 320 | std::unique_ptr<ScreenRotationRequest> rotation_request = |
| 321 | base::MakeUnique<ScreenRotationRequest>(new_rotation, source); |
| 322 | |
| 323 | if (is_rotating_) { |
| 324 | last_pending_request_ = std::move(rotation_request); |
| 325 | // The pending request will be processed when the |
| 326 | // OnLayerAnimation(Ended|Aborted) methods should be called after |
| 327 | // StopAnimating(). |
| 328 | StopAnimating(); |
| 329 | } else { |
| 330 | is_rotating_ = true; |
| 331 | AnimateRotation(std::move(rotation_request)); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( |
| 336 | ScreenRotationAnimatorObserver* observer) { |
| 337 | screen_rotation_animator_observers_.AddObserver(observer); |
| 338 | } |
| 339 | |
| 340 | void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( |
| 341 | ScreenRotationAnimatorObserver* observer) { |
| 342 | screen_rotation_animator_observers_.RemoveObserver(observer); |
| 343 | } |
| 344 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 345 | void ScreenRotationAnimator::ProcessAnimationQueue() { |
| 346 | is_rotating_ = false; |
| 347 | old_layer_tree_owner_.reset(); |
| 348 | if (last_pending_request_ && IsDisplayIdValid(display_id_)) { |
| 349 | std::unique_ptr<ScreenRotationRequest> rotation_request = |
| 350 | std::move(last_pending_request_); |
| 351 | Rotate(rotation_request->new_rotation, rotation_request->source); |
| 352 | rotation_request.reset(); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | for (auto& observer : screen_rotation_animator_observers_) |
| 357 | observer.OnScreenRotationAnimationFinished(this); |
| 358 | } |
| 359 | |
| 360 | void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
| 361 | bool disable_timers) { |
| 362 | disable_animation_timers_for_test_ = disable_timers; |
| 363 | } |
| 364 | |
| 365 | void ScreenRotationAnimator::StopAnimating() { |
| 366 | aura::Window* root_window = GetRootWindow(display_id_); |
| 367 | for (ui::Layer* child_layer : root_window->layer()->children()) { |
| 368 | if (child_layer == old_layer_tree_owner_->root()) |
| 369 | continue; |
| 370 | |
| 371 | child_layer->GetAnimator()->StopAnimating(); |
| 372 | } |
| 373 | |
| 374 | old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | } // namespace ash |