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 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 7 | #include "ash/ash_switches.h" |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 8 | #include "ash/display/window_tree_host_manager.h" |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 9 | #include "ash/public/cpp/shell_window_ids.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 10 | #include "ash/rotator/screen_rotation_animation.h" |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 11 | #include "ash/rotator/screen_rotation_animator_observer.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 12 | #include "ash/shell.h" |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 13 | #include "ash/utility/transformer_util.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 14 | #include "base/command_line.h" |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 16 | #include "base/metrics/histogram_macros.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 17 | #include "base/time/time.h" |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 18 | #include "cc/output/copy_output_request.h" |
| 19 | #include "cc/output/copy_output_result.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 20 | #include "ui/aura/window.h" |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 21 | #include "ui/compositor/callback_layer_animation_observer.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 22 | #include "ui/compositor/layer.h" |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 23 | #include "ui/compositor/layer_animation_element.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 24 | #include "ui/compositor/layer_animation_sequence.h" |
| 25 | #include "ui/compositor/layer_animator.h" |
| 26 | #include "ui/compositor/layer_owner.h" |
| 27 | #include "ui/compositor/layer_tree_owner.h" |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 28 | #include "ui/display/display.h" |
rjkroege | 72f8154f | 2016-10-29 00:49:02 | [diff] [blame] | 29 | #include "ui/display/manager/display_manager.h" |
rjkroege | 259c0188 | 2016-08-30 19:29:50 | [diff] [blame] | 30 | #include "ui/display/manager/managed_display_info.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 31 | #include "ui/gfx/animation/tween.h" |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 32 | #include "ui/gfx/geometry/point.h" |
| 33 | #include "ui/gfx/geometry/rect.h" |
| 34 | #include "ui/gfx/geometry/rect_f.h" |
| 35 | #include "ui/gfx/transform.h" |
| 36 | #include "ui/gfx/transform_util.h" |
| 37 | #include "ui/wm/core/window_util.h" |
| 38 | |
| 39 | namespace ash { |
| 40 | |
| 41 | namespace { |
| 42 | |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 43 | // The number of degrees that the rotation animations animate through. |
| 44 | const int kRotationDegrees = 20; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 45 | |
bruthig | 2ce3f23 | 2015-04-02 15:21:35 | [diff] [blame] | 46 | // The time it takes for the rotation animations to run. |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 47 | const int kRotationDurationInMs = 250; |
| 48 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 49 | // The rotation factors. |
| 50 | const int kCounterClockWiseRotationFactor = 1; |
| 51 | const int kClockWiseRotationFactor = -1; |
| 52 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 53 | display::Display::Rotation GetCurrentScreenRotation(int64_t display_id) { |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 54 | return Shell::Get() |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 55 | ->display_manager() |
| 56 | ->GetDisplayInfo(display_id) |
jonross | d01de7f | 2015-04-23 19:52:00 | [diff] [blame] | 57 | .GetActiveRotation(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 58 | } |
| 59 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 60 | bool IsDisplayIdValid(int64_t display_id) { |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 61 | return Shell::Get()->display_manager()->IsDisplayIdValid(display_id); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // 180 degree rotations should animate clock-wise. |
| 65 | int GetRotationFactor(display::Display::Rotation initial_rotation, |
| 66 | display::Display::Rotation new_rotation) { |
| 67 | return (initial_rotation + 3) % 4 == new_rotation |
| 68 | ? kCounterClockWiseRotationFactor |
| 69 | : kClockWiseRotationFactor; |
| 70 | } |
| 71 | |
| 72 | aura::Window* GetRootWindow(int64_t display_id) { |
sky | cb4be5b | 2017-04-06 17:52:45 | [diff] [blame] | 73 | return Shell::Get()->window_tree_host_manager()->GetRootWindowForDisplayId( |
| 74 | display_id); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 75 | } |
| 76 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 77 | aura::Window* GetScreenRotationContainer(aura::Window* root_window) { |
| 78 | return root_window->GetChildById(kShellWindowId_ScreenRotationContainer); |
| 79 | } |
| 80 | |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 81 | // Returns true if the rotation between |initial_rotation| and |new_rotation| is |
| 82 | // 180 degrees. |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 83 | bool Is180DegreeFlip(display::Display::Rotation initial_rotation, |
| 84 | display::Display::Rotation new_rotation) { |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 85 | return (initial_rotation + 2) % 4 == new_rotation; |
| 86 | } |
| 87 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 88 | // Returns the initial degrees the old layer animation to begin with. |
| 89 | int GetInitialDegrees(display::Display::Rotation initial_rotation, |
| 90 | display::Display::Rotation new_rotation) { |
| 91 | return (Is180DegreeFlip(initial_rotation, new_rotation) ? 180 : 90); |
| 92 | } |
| 93 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 94 | void AddLayerAtTopOfWindowLayers(aura::Window* root_window, ui::Layer* layer) { |
| 95 | // Add the cloned/copied layer tree into the root, so it will be rendered. |
| 96 | root_window->layer()->Add(layer); |
| 97 | root_window->layer()->StackAtTop(layer); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 98 | } |
| 99 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 100 | void AddLayerBelowWindowLayer(aura::Window* root_window, |
| 101 | ui::Layer* top_layer, |
| 102 | ui::Layer* layer) { |
| 103 | // Add the cloned/copied layer tree into the root, so it will be rendered. |
| 104 | root_window->layer()->Add(layer); |
| 105 | root_window->layer()->StackBelow(layer, top_layer); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 106 | } |
| 107 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 108 | // The Callback will be invoked when all animation sequences have |
| 109 | // finished. |observer| will be destroyed after invoking the Callback if it |
| 110 | // returns true. |
| 111 | bool AnimationEndedCallback( |
| 112 | base::WeakPtr<ScreenRotationAnimator> animator, |
| 113 | const ui::CallbackLayerAnimationObserver& observer) { |
| 114 | if (animator) |
| 115 | animator->ProcessAnimationQueue(); |
| 116 | return true; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 117 | } |
| 118 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 119 | // Creates a Transform for the old layer in screen rotation animation. |
| 120 | gfx::Transform CreateScreenRotationOldLayerTransformForDisplay( |
| 121 | display::Display::Rotation old_rotation, |
| 122 | display::Display::Rotation new_rotation, |
| 123 | const display::Display& display) { |
| 124 | gfx::Transform inverse; |
| 125 | CHECK(CreateRotationTransform(old_rotation, new_rotation, display) |
| 126 | .GetInverse(&inverse)); |
| 127 | return inverse; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 128 | } |
| 129 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 130 | // The |request_id| changed since last copy request, which means a |
| 131 | // new rotation stated, we need to ignore this copy result. |
| 132 | bool IgnoreCopyResult(int64_t request_id, int64_t current_request_id) { |
| 133 | DCHECK(request_id <= current_request_id); |
| 134 | return request_id < current_request_id; |
| 135 | } |
| 136 | |
| 137 | // In the following cases, abort rotation: |
| 138 | // 1) if the display was removed, |
| 139 | // 2) the copy request has been canceled or failed. It would fail if, |
| 140 | // for examples: a) The layer is removed from the compositor and destroye |
| 141 | // before committing the request to the compositor. b) The compositor is |
| 142 | // shutdown. |
| 143 | bool AbortRotation(int64_t display_id, cc::CopyOutputResult* result) { |
| 144 | return !IsDisplayIdValid(display_id) || result->IsEmpty(); |
| 145 | } |
| 146 | |
| 147 | // Creates a black mask layer and returns the |layer_owner|. |
| 148 | std::unique_ptr<ui::LayerOwner> CreateBlackMaskLayerOwner( |
| 149 | const gfx::Rect& rect) { |
| 150 | std::unique_ptr<ui::Layer> black_mask_layer = |
| 151 | base::MakeUnique<ui::Layer>(ui::LAYER_SOLID_COLOR); |
| 152 | black_mask_layer->SetBounds(rect); |
| 153 | black_mask_layer->SetColor(SK_ColorBLACK); |
| 154 | std::unique_ptr<ui::LayerOwner> black_mask_layer_owner = |
| 155 | base::MakeUnique<ui::LayerOwner>(); |
| 156 | black_mask_layer_owner->SetLayer(std::move(black_mask_layer)); |
| 157 | return black_mask_layer_owner; |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 158 | } |
| 159 | |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 160 | class ScreenRotationAnimationMetricsReporter |
| 161 | : public ui::AnimationMetricsReporter { |
| 162 | public: |
| 163 | ScreenRotationAnimationMetricsReporter() {} |
| 164 | ~ScreenRotationAnimationMetricsReporter() override {} |
| 165 | |
| 166 | void Report(int value) override { |
| 167 | UMA_HISTOGRAM_PERCENTAGE("Ash.Rotation.AnimationSmoothness", value); |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); |
| 172 | }; |
| 173 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 174 | } // namespace |
jonross | 7351feb | 2015-04-23 22:10:26 | [diff] [blame] | 175 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 176 | ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) |
| 177 | : display_id_(display_id), |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 178 | screen_rotation_state_(IDLE), |
wutao | 88733df | 2017-04-13 19:39:06 | [diff] [blame] | 179 | rotation_request_id_(0), |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 180 | metrics_reporter_( |
| 181 | base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 182 | disable_animation_timers_for_test_(false), |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 183 | has_switch_ash_enable_smooth_screen_rotation_( |
| 184 | base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 185 | switches::kAshEnableSmoothScreenRotation)), |
| 186 | root_window_(GetRootWindow(display_id_)), |
| 187 | screen_rotation_container_layer_( |
| 188 | GetScreenRotationContainer(root_window_)->layer()), |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 189 | weak_factory_(this) {} |
| 190 | |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 191 | ScreenRotationAnimator::~ScreenRotationAnimator() { |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 192 | // To prevent a call to |AnimationEndedCallback()| from calling a method on |
| 193 | // the |animator_|. |
wutao | 3f08f81 | 2017-03-25 03:06:33 | [diff] [blame] | 194 | weak_factory_.InvalidateWeakPtrs(); |
| 195 | |
| 196 | // Explicitly reset the |old_layer_tree_owner_| and |metrics_reporter_| in |
| 197 | // order to make sure |metrics_reporter_| outlives the attached animation |
| 198 | // sequence. |
| 199 | old_layer_tree_owner_.reset(); |
| 200 | metrics_reporter_.reset(); |
| 201 | } |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 202 | |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 203 | void ScreenRotationAnimator::StartRotationAnimation( |
| 204 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 205 | const display::Display::Rotation current_rotation = |
| 206 | GetCurrentScreenRotation(display_id_); |
| 207 | if (current_rotation == rotation_request->new_rotation) { |
| 208 | // We need to call |ProcessAnimationQueue()| to prepare for next rotation |
| 209 | // request. |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 210 | ProcessAnimationQueue(); |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 214 | rotation_request->old_rotation = current_rotation; |
| 215 | if (has_switch_ash_enable_smooth_screen_rotation_) { |
| 216 | std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
| 217 | cc::CopyOutputRequest::CreateRequest( |
| 218 | CreateAfterCopyCallbackBeforeRotation(std::move(rotation_request))); |
| 219 | RequestCopyScreenRotationContainerLayer(std::move(copy_output_request)); |
| 220 | screen_rotation_state_ = COPY_REQUESTED; |
| 221 | } else { |
| 222 | StartSlowAnimation(std::move(rotation_request)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void ScreenRotationAnimator::StartSlowAnimation( |
| 227 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 228 | CreateOldLayerTreeForSlowAnimation(); |
| 229 | SetRotation(rotation_request->old_rotation, rotation_request->new_rotation, |
| 230 | rotation_request->source); |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 231 | AnimateRotation(std::move(rotation_request)); |
| 232 | } |
| 233 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 234 | void ScreenRotationAnimator::SetRotation( |
| 235 | display::Display::Rotation old_rotation, |
| 236 | display::Display::Rotation new_rotation, |
| 237 | display::Display::RotationSource source) { |
| 238 | Shell::Get()->display_manager()->SetDisplayRotation(display_id_, new_rotation, |
| 239 | source); |
| 240 | const display::Display display = |
| 241 | Shell::Get()->display_manager()->GetDisplayForId(display_id_); |
| 242 | old_layer_tree_owner_->root()->SetTransform( |
| 243 | CreateScreenRotationOldLayerTransformForDisplay(old_rotation, |
| 244 | new_rotation, display)); |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 245 | } |
| 246 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 247 | void ScreenRotationAnimator::RequestCopyScreenRotationContainerLayer( |
| 248 | std::unique_ptr<cc::CopyOutputRequest> copy_output_request) { |
| 249 | copy_output_request->set_area( |
| 250 | gfx::Rect(screen_rotation_container_layer_->size())); |
| 251 | screen_rotation_container_layer_->RequestCopyOfOutput( |
| 252 | std::move(copy_output_request)); |
| 253 | } |
| 254 | |
| 255 | ScreenRotationAnimator::CopyCallback |
| 256 | ScreenRotationAnimator::CreateAfterCopyCallbackBeforeRotation( |
| 257 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 258 | return base::Bind(&ScreenRotationAnimator:: |
| 259 | OnScreenRotationContainerLayerCopiedBeforeRotation, |
| 260 | weak_factory_.GetWeakPtr(), |
| 261 | base::Passed(&rotation_request)); |
| 262 | } |
| 263 | |
| 264 | ScreenRotationAnimator::CopyCallback |
| 265 | ScreenRotationAnimator::CreateAfterCopyCallbackAfterRotation( |
| 266 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 267 | return base::Bind(&ScreenRotationAnimator:: |
| 268 | OnScreenRotationContainerLayerCopiedAfterRotation, |
| 269 | weak_factory_.GetWeakPtr(), |
| 270 | base::Passed(&rotation_request)); |
| 271 | } |
| 272 | |
| 273 | void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedBeforeRotation( |
| 274 | std::unique_ptr<ScreenRotationRequest> rotation_request, |
| 275 | std::unique_ptr<cc::CopyOutputResult> result) { |
| 276 | if (IgnoreCopyResult(rotation_request->id, rotation_request_id_)) |
| 277 | return; |
| 278 | if (AbortRotation(display_id_, result.get())) { |
| 279 | ProcessAnimationQueue(); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | old_layer_tree_owner_ = CopyLayerTree(std::move(result)); |
| 284 | AddLayerAtTopOfWindowLayers(root_window_, old_layer_tree_owner_->root()); |
| 285 | SetRotation(rotation_request->old_rotation, rotation_request->new_rotation, |
| 286 | rotation_request->source); |
| 287 | std::unique_ptr<cc::CopyOutputRequest> copy_output_request = |
| 288 | cc::CopyOutputRequest::CreateRequest( |
| 289 | CreateAfterCopyCallbackAfterRotation(std::move(rotation_request))); |
| 290 | RequestCopyScreenRotationContainerLayer(std::move(copy_output_request)); |
| 291 | } |
| 292 | |
| 293 | void ScreenRotationAnimator::OnScreenRotationContainerLayerCopiedAfterRotation( |
| 294 | std::unique_ptr<ScreenRotationRequest> rotation_request, |
| 295 | std::unique_ptr<cc::CopyOutputResult> result) { |
| 296 | if (IgnoreCopyResult(rotation_request->id, rotation_request_id_)) |
| 297 | return; |
| 298 | if (AbortRotation(display_id_, result.get())) { |
| 299 | ProcessAnimationQueue(); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | new_layer_tree_owner_ = CopyLayerTree(std::move(result)); |
| 304 | AddLayerBelowWindowLayer(root_window_, old_layer_tree_owner_->root(), |
| 305 | new_layer_tree_owner_->root()); |
| 306 | AnimateRotation(std::move(rotation_request)); |
| 307 | } |
| 308 | |
| 309 | void ScreenRotationAnimator::CreateOldLayerTreeForSlowAnimation() { |
| 310 | old_layer_tree_owner_ = ::wm::RecreateLayers(root_window_); |
| 311 | // |screen_rotation_container_layer_| needs update after |RecreateLayers()|. |
| 312 | screen_rotation_container_layer_ = |
| 313 | GetScreenRotationContainer(root_window_)->layer(); |
| 314 | AddLayerAtTopOfWindowLayers(root_window_, old_layer_tree_owner_->root()); |
| 315 | } |
| 316 | |
| 317 | std::unique_ptr<ui::LayerTreeOwner> ScreenRotationAnimator::CopyLayerTree( |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 318 | std::unique_ptr<cc::CopyOutputResult> result) { |
| 319 | cc::TextureMailbox texture_mailbox; |
| 320 | std::unique_ptr<cc::SingleReleaseCallback> release_callback; |
| 321 | result->TakeTexture(&texture_mailbox, &release_callback); |
| 322 | DCHECK(texture_mailbox.IsTexture()); |
| 323 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 324 | const gfx::Rect rect(screen_rotation_container_layer_->size()); |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 325 | std::unique_ptr<ui::Layer> copy_layer = base::MakeUnique<ui::Layer>(); |
| 326 | copy_layer->SetBounds(rect); |
| 327 | copy_layer->SetTextureMailbox(texture_mailbox, std::move(release_callback), |
| 328 | rect.size()); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 329 | return base::MakeUnique<ui::LayerTreeOwner>(std::move(copy_layer)); |
wutao | 2c0ca18 | 2017-04-06 22:40:54 | [diff] [blame] | 330 | } |
| 331 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 332 | void ScreenRotationAnimator::AnimateRotation( |
| 333 | std::unique_ptr<ScreenRotationRequest> rotation_request) { |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 334 | screen_rotation_state_ = ROTATING; |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 335 | const int rotation_factor = GetRotationFactor(rotation_request->old_rotation, |
| 336 | rotation_request->new_rotation); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 337 | const int old_layer_initial_rotation_degrees = GetInitialDegrees( |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 338 | rotation_request->old_rotation, rotation_request->new_rotation); |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 339 | const base::TimeDelta duration = |
| 340 | base::TimeDelta::FromMilliseconds(kRotationDurationInMs); |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 341 | const gfx::Tween::Type tween_type = gfx::Tween::FAST_OUT_LINEAR_IN; |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 342 | const gfx::Rect rotated_screen_bounds = root_window_->GetTargetBounds(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 343 | const gfx::Point pivot = gfx::Point(rotated_screen_bounds.width() / 2, |
| 344 | rotated_screen_bounds.height() / 2); |
| 345 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 346 | ui::Layer* new_root_layer; |
| 347 | if (new_layer_tree_owner_ && has_switch_ash_enable_smooth_screen_rotation_) { |
| 348 | new_root_layer = new_layer_tree_owner_->root(); |
| 349 | // Add a black mask layer on top of |screen_rotation_container_layer_|. |
| 350 | black_mask_layer_owner_ = CreateBlackMaskLayerOwner( |
| 351 | gfx::Rect(screen_rotation_container_layer_->size())); |
| 352 | AddLayerBelowWindowLayer(root_window_, new_root_layer, |
| 353 | black_mask_layer_owner_->layer()); |
| 354 | } else { |
| 355 | new_root_layer = screen_rotation_container_layer_; |
| 356 | } |
| 357 | |
| 358 | std::unique_ptr<ScreenRotationAnimation> new_layer_screen_rotation = |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 359 | base::MakeUnique<ScreenRotationAnimation>( |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 360 | new_root_layer, kRotationDegrees * rotation_factor, |
| 361 | 0 /* end_degrees */, new_root_layer->opacity(), |
| 362 | new_root_layer->opacity() /* target_opacity */, pivot, duration, |
| 363 | tween_type); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 364 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 365 | ui::LayerAnimator* new_layer_animator = new_root_layer->GetAnimator(); |
| 366 | new_layer_animator->set_preemption_strategy( |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 367 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 368 | std::unique_ptr<ui::LayerAnimationSequence> new_layer_animation_sequence = |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 369 | base::MakeUnique<ui::LayerAnimationSequence>( |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 370 | std::move(new_layer_screen_rotation)); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 371 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 372 | ui::Layer* old_root_layer = old_layer_tree_owner_->root(); |
| 373 | const gfx::Rect original_screen_bounds = old_root_layer->GetTargetBounds(); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 374 | // The old layer will also be transformed into the new orientation. We will |
| 375 | // translate it so that the old layer's center point aligns with the new |
| 376 | // orientation's center point and use that center point as the pivot for the |
| 377 | // rotation animation. |
| 378 | gfx::Transform translate_transform; |
| 379 | translate_transform.Translate( |
| 380 | (rotated_screen_bounds.width() - original_screen_bounds.width()) / 2, |
| 381 | (rotated_screen_bounds.height() - original_screen_bounds.height()) / 2); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 382 | old_root_layer->SetTransform(translate_transform); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 383 | |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 384 | std::unique_ptr<ScreenRotationAnimation> old_layer_screen_rotation = |
ratsunny | bd5087e | 2016-12-17 05:19:25 | [diff] [blame] | 385 | base::MakeUnique<ScreenRotationAnimation>( |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 386 | old_root_layer, old_layer_initial_rotation_degrees * rotation_factor, |
bruthig | aff7d0f | 2015-09-01 17:46:55 | [diff] [blame] | 387 | (old_layer_initial_rotation_degrees - kRotationDegrees) * |
| 388 | rotation_factor, |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 389 | old_root_layer->opacity(), 0.0f /* target_opacity */, pivot, duration, |
| 390 | tween_type); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 391 | |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 392 | ui::LayerAnimator* old_layer_animator = old_root_layer->GetAnimator(); |
| 393 | old_layer_animator->set_preemption_strategy( |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 394 | ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS); |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 395 | std::unique_ptr<ui::LayerAnimationSequence> old_layer_animation_sequence = |
| 396 | base::MakeUnique<ui::LayerAnimationSequence>( |
| 397 | std::move(old_layer_screen_rotation)); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 398 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 399 | // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
| 400 | // control the animation. |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 401 | if (disable_animation_timers_for_test_) { |
| 402 | if (new_layer_tree_owner_) |
| 403 | new_layer_animator->set_disable_timer_for_test(true); |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 404 | old_layer_animator->set_disable_timer_for_test(true); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 405 | } |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 406 | old_layer_animation_sequence->SetAnimationMetricsReporter( |
| 407 | metrics_reporter_.get()); |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 408 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 409 | // Add an observer so that the cloned/copied layers can be cleaned up with the |
| 410 | // animation completes/aborts. |
| 411 | ui::CallbackLayerAnimationObserver* observer = |
| 412 | new ui::CallbackLayerAnimationObserver( |
| 413 | base::Bind(&AnimationEndedCallback, weak_factory_.GetWeakPtr())); |
| 414 | if (new_layer_tree_owner_) |
| 415 | new_layer_animator->AddObserver(observer); |
| 416 | new_layer_animator->StartAnimation(new_layer_animation_sequence.release()); |
| 417 | old_layer_animator->AddObserver(observer); |
| 418 | old_layer_animator->StartAnimation(old_layer_animation_sequence.release()); |
| 419 | observer->SetActive(); |
bruthig | f57a7bba | 2015-04-24 21:19:31 | [diff] [blame] | 420 | } |
| 421 | |
oshima | f84b0da72 | 2016-04-27 19:47:19 | [diff] [blame] | 422 | void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
| 423 | display::Display::RotationSource source) { |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 424 | // |rotation_request_id_| is used to skip stale requests. Before the layer |
| 425 | // CopyOutputResult callback called, there could have new rotation request. |
| 426 | // Increases |rotation_request_id_| for each new request and in the callback, |
| 427 | // we compare the |rotation_request.id| and |rotation_request_id_| to |
| 428 | // determine the stale status. |
wutao | 88733df | 2017-04-13 19:39:06 | [diff] [blame] | 429 | rotation_request_id_++; |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 430 | std::unique_ptr<ScreenRotationRequest> rotation_request = |
wutao | 88733df | 2017-04-13 19:39:06 | [diff] [blame] | 431 | base::MakeUnique<ScreenRotationRequest>(rotation_request_id_, |
| 432 | new_rotation, source); |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 433 | switch (screen_rotation_state_) { |
| 434 | case IDLE: |
wutao | 88733df | 2017-04-13 19:39:06 | [diff] [blame] | 435 | case COPY_REQUESTED: |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 436 | StartRotationAnimation(std::move(rotation_request)); |
| 437 | break; |
| 438 | case ROTATING: |
| 439 | last_pending_request_ = std::move(rotation_request); |
| 440 | // The pending request will be processed when the |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 441 | // |AnimationEndedCallback()| should be called after |StopAnimating()|. |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 442 | StopAnimating(); |
| 443 | break; |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
| 447 | void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( |
| 448 | ScreenRotationAnimatorObserver* observer) { |
| 449 | screen_rotation_animator_observers_.AddObserver(observer); |
| 450 | } |
| 451 | |
| 452 | void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( |
| 453 | ScreenRotationAnimatorObserver* observer) { |
| 454 | screen_rotation_animator_observers_.RemoveObserver(observer); |
| 455 | } |
| 456 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 457 | void ScreenRotationAnimator::ProcessAnimationQueue() { |
wutao | 4a48553 | 2017-04-11 00:04:28 | [diff] [blame] | 458 | screen_rotation_state_ = IDLE; |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 459 | if (IsDisplayIdValid(display_id_) && black_mask_layer_owner_) |
| 460 | root_window_->layer()->Remove(black_mask_layer_owner_->layer()); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 461 | old_layer_tree_owner_.reset(); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 462 | new_layer_tree_owner_.reset(); |
| 463 | black_mask_layer_owner_.reset(); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 464 | if (last_pending_request_ && IsDisplayIdValid(display_id_)) { |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 465 | StartRotationAnimation(std::move(last_pending_request_)); |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 469 | // This is only used in test to notify animator observer. |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 470 | for (auto& observer : screen_rotation_animator_observers_) |
| 471 | observer.OnScreenRotationAnimationFinished(this); |
| 472 | } |
| 473 | |
wutao | 6ddafa3 | 2017-03-18 03:10:08 | [diff] [blame] | 474 | void ScreenRotationAnimator::StopAnimating() { |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 475 | // |old_layer_tree_owner_| new_layer_tree_owner_| could be nullptr if another |
| 476 | // the rotation request comes before the copy request finished. |
wutao | 3357608 | 2017-04-14 18:51:10 | [diff] [blame] | 477 | if (old_layer_tree_owner_) |
| 478 | old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
wutao | 6d39411 | 2017-04-20 02:17:35 | [diff] [blame^] | 479 | if (new_layer_tree_owner_) |
| 480 | new_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
| 481 | if (IsDisplayIdValid(display_id_) && black_mask_layer_owner_) |
| 482 | root_window_->layer()->Remove(black_mask_layer_owner_->layer()); |
bruthig | 37f9cad0 | 2015-03-12 22:28:50 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | } // namespace ash |