| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/rotator/screen_rotation_animator.h" | 5 #include "ash/rotator/screen_rotation_animator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 | 183 |
| 184 private: | 184 private: |
| 185 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); | 185 DISALLOW_COPY_AND_ASSIGN(ScreenRotationAnimationMetricsReporter); |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 } // namespace | 188 } // namespace |
| 189 | 189 |
| 190 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) | 190 ScreenRotationAnimator::ScreenRotationAnimator(int64_t display_id) |
| 191 : display_id_(display_id), | 191 : display_id_(display_id), |
| 192 screen_rotation_state_(IDLE), | 192 screen_rotation_state_(IDLE), |
| 193 rotation_request_id_(0), |
| 193 metrics_reporter_( | 194 metrics_reporter_( |
| 194 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), | 195 base::MakeUnique<ScreenRotationAnimationMetricsReporter>()), |
| 195 disable_animation_timers_for_test_(false), | 196 disable_animation_timers_for_test_(false), |
| 196 weak_factory_(this) {} | 197 weak_factory_(this) {} |
| 197 | 198 |
| 198 ScreenRotationAnimator::~ScreenRotationAnimator() { | 199 ScreenRotationAnimator::~ScreenRotationAnimator() { |
| 199 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from | 200 // To prevent a call to |LayerCleanupObserver::OnLayerAnimationAborted()| from |
| 200 // calling a method on the |animator_|. | 201 // calling a method on the |animator_|. |
| 201 weak_factory_.InvalidateWeakPtrs(); | 202 weak_factory_.InvalidateWeakPtrs(); |
| 202 | 203 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 ScreenRotationAnimator::CreateAfterCopyCallback( | 235 ScreenRotationAnimator::CreateAfterCopyCallback( |
| 235 std::unique_ptr<ScreenRotationRequest> rotation_request) { | 236 std::unique_ptr<ScreenRotationRequest> rotation_request) { |
| 236 return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation, | 237 return base::Bind(&ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation, |
| 237 weak_factory_.GetWeakPtr(), | 238 weak_factory_.GetWeakPtr(), |
| 238 base::Passed(&rotation_request)); | 239 base::Passed(&rotation_request)); |
| 239 } | 240 } |
| 240 | 241 |
| 241 void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( | 242 void ScreenRotationAnimator::OnRootLayerCopiedBeforeRotation( |
| 242 std::unique_ptr<ScreenRotationRequest> rotation_request, | 243 std::unique_ptr<ScreenRotationRequest> rotation_request, |
| 243 std::unique_ptr<cc::CopyOutputResult> result) { | 244 std::unique_ptr<cc::CopyOutputResult> result) { |
| 245 DCHECK(rotation_request->id <= rotation_request_id_); |
| 246 // The |rotation_request_id_| changed since last copy request, which means a |
| 247 // new rotation stated, we need to ignore this copy result. |
| 248 if (rotation_request->id < rotation_request_id_) |
| 249 return; |
| 250 |
| 244 // In the following cases, abort rotation: | 251 // In the following cases, abort rotation: |
| 245 // 1) if the display was removed, | 252 // 1) if the display was removed, |
| 246 // 2) the copy request has been canceled or failed. It would fail if, | 253 // 2) the copy request has been canceled or failed. It would fail if, |
| 247 // for examples: a) The layer is removed from the compositor and destroye | 254 // for examples: a) The layer is removed from the compositor and destroye |
| 248 // before committing the request to the compositor. b) The compositor is | 255 // before committing the request to the compositor. b) The compositor is |
| 249 // shutdown. | 256 // shutdown. |
| 250 if (!IsDisplayIdValid(display_id_) || result->IsEmpty()) { | 257 if (!IsDisplayIdValid(display_id_) || result->IsEmpty()) { |
| 251 ProcessAnimationQueue(); | 258 ProcessAnimationQueue(); |
| 252 rotation_request.reset(); | |
| 253 return; | 259 return; |
| 254 } | 260 } |
| 255 | 261 |
| 256 CopyOldLayerTree(std::move(result)); | 262 CopyOldLayerTree(std::move(result)); |
| 257 AnimateRotation(std::move(rotation_request)); | 263 AnimateRotation(std::move(rotation_request)); |
| 258 } | 264 } |
| 259 | 265 |
| 260 void ScreenRotationAnimator::CreateOldLayerTree() { | 266 void ScreenRotationAnimator::CreateOldLayerTree() { |
| 261 old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_)); | 267 old_layer_tree_owner_ = ::wm::RecreateLayers(GetRootWindow(display_id_)); |
| 262 } | 268 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); | 368 base::MakeUnique<ui::LayerAnimationSequence>(std::move(screen_rotation)); |
| 363 // Add an observer so that the cloned layers can be cleaned up with the | 369 // Add an observer so that the cloned layers can be cleaned up with the |
| 364 // animation completes/aborts. | 370 // animation completes/aborts. |
| 365 animation_sequence->AddObserver(old_layer_cleanup_observer.release()); | 371 animation_sequence->AddObserver(old_layer_cleanup_observer.release()); |
| 366 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to | 372 // In unit test, we can use ash::test::ScreenRotationAnimatorTestApi to |
| 367 // control the animation. | 373 // control the animation. |
| 368 if (disable_animation_timers_for_test_) | 374 if (disable_animation_timers_for_test_) |
| 369 animator->set_disable_timer_for_test(true); | 375 animator->set_disable_timer_for_test(true); |
| 370 animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); | 376 animation_sequence->SetAnimationMetricsReporter(metrics_reporter_.get()); |
| 371 animator->StartAnimation(animation_sequence.release()); | 377 animator->StartAnimation(animation_sequence.release()); |
| 372 | |
| 373 rotation_request.reset(); | |
| 374 } | 378 } |
| 375 | 379 |
| 376 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, | 380 void ScreenRotationAnimator::Rotate(display::Display::Rotation new_rotation, |
| 377 display::Display::RotationSource source) { | 381 display::Display::RotationSource source) { |
| 378 if (GetCurrentScreenRotation(display_id_) == new_rotation) | 382 if (GetCurrentScreenRotation(display_id_) == new_rotation) |
| 379 return; | 383 return; |
| 380 | 384 |
| 385 rotation_request_id_++; |
| 381 std::unique_ptr<ScreenRotationRequest> rotation_request = | 386 std::unique_ptr<ScreenRotationRequest> rotation_request = |
| 382 base::MakeUnique<ScreenRotationRequest>(new_rotation, source); | 387 base::MakeUnique<ScreenRotationRequest>(rotation_request_id_, |
| 388 new_rotation, source); |
| 383 | 389 |
| 384 switch (screen_rotation_state_) { | 390 switch (screen_rotation_state_) { |
| 385 case IDLE: | 391 case IDLE: |
| 392 case COPY_REQUESTED: |
| 386 StartRotationAnimation(std::move(rotation_request)); | 393 StartRotationAnimation(std::move(rotation_request)); |
| 387 break; | 394 break; |
| 388 case ROTATING: | 395 case ROTATING: |
| 389 last_pending_request_ = std::move(rotation_request); | 396 last_pending_request_ = std::move(rotation_request); |
| 390 // The pending request will be processed when the | 397 // The pending request will be processed when the |
| 391 // OnLayerAnimation(Ended|Aborted) methods should be called after | 398 // OnLayerAnimation(Ended|Aborted) methods should be called after |
| 392 // |StopAnimating()|. | 399 // |StopAnimating()|. |
| 393 StopAnimating(); | 400 StopAnimating(); |
| 394 break; | 401 break; |
| 395 case COPY_REQUESTED: | |
| 396 // TODO(wutao): We need to handle this otherwise the device will be in | |
| 397 // inconsistent state. | |
| 398 break; | |
| 399 } | 402 } |
| 400 } | 403 } |
| 401 | 404 |
| 402 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( | 405 void ScreenRotationAnimator::AddScreenRotationAnimatorObserver( |
| 403 ScreenRotationAnimatorObserver* observer) { | 406 ScreenRotationAnimatorObserver* observer) { |
| 404 screen_rotation_animator_observers_.AddObserver(observer); | 407 screen_rotation_animator_observers_.AddObserver(observer); |
| 405 } | 408 } |
| 406 | 409 |
| 407 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( | 410 void ScreenRotationAnimator::RemoveScreenRotationAnimatorObserver( |
| 408 ScreenRotationAnimatorObserver* observer) { | 411 ScreenRotationAnimatorObserver* observer) { |
| 409 screen_rotation_animator_observers_.RemoveObserver(observer); | 412 screen_rotation_animator_observers_.RemoveObserver(observer); |
| 410 } | 413 } |
| 411 | 414 |
| 412 void ScreenRotationAnimator::ProcessAnimationQueue() { | 415 void ScreenRotationAnimator::ProcessAnimationQueue() { |
| 413 screen_rotation_state_ = IDLE; | 416 screen_rotation_state_ = IDLE; |
| 414 old_layer_tree_owner_.reset(); | 417 old_layer_tree_owner_.reset(); |
| 415 if (last_pending_request_ && IsDisplayIdValid(display_id_)) { | 418 if (last_pending_request_ && IsDisplayIdValid(display_id_)) { |
| 416 std::unique_ptr<ScreenRotationRequest> rotation_request = | 419 display::Display::Rotation new_rotation = |
| 417 std::move(last_pending_request_); | 420 last_pending_request_->new_rotation; |
| 418 Rotate(rotation_request->new_rotation, rotation_request->source); | 421 display::Display::RotationSource source = last_pending_request_->source; |
| 419 rotation_request.reset(); | 422 last_pending_request_.reset(); |
| 423 Rotate(new_rotation, source); |
| 420 return; | 424 return; |
| 421 } | 425 } |
| 422 | 426 |
| 423 for (auto& observer : screen_rotation_animator_observers_) | 427 for (auto& observer : screen_rotation_animator_observers_) |
| 424 observer.OnScreenRotationAnimationFinished(this); | 428 observer.OnScreenRotationAnimationFinished(this); |
| 425 } | 429 } |
| 426 | 430 |
| 427 void ScreenRotationAnimator::set_disable_animation_timers_for_test( | 431 void ScreenRotationAnimator::set_disable_animation_timers_for_test( |
| 428 bool disable_timers) { | 432 bool disable_timers) { |
| 429 disable_animation_timers_for_test_ = disable_timers; | 433 disable_animation_timers_for_test_ = disable_timers; |
| 430 } | 434 } |
| 431 | 435 |
| 432 void ScreenRotationAnimator::StopAnimating() { | 436 void ScreenRotationAnimator::StopAnimating() { |
| 433 aura::Window* root_window = GetRootWindow(display_id_); | 437 aura::Window* root_window = GetRootWindow(display_id_); |
| 434 for (ui::Layer* child_layer : root_window->layer()->children()) { | 438 for (ui::Layer* child_layer : root_window->layer()->children()) { |
| 435 if (child_layer == old_layer_tree_owner_->root()) | 439 if (child_layer == old_layer_tree_owner_->root()) |
| 436 continue; | 440 continue; |
| 437 | 441 |
| 438 child_layer->GetAnimator()->StopAnimating(); | 442 child_layer->GetAnimator()->StopAnimating(); |
| 439 } | 443 } |
| 440 | 444 |
| 441 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); | 445 old_layer_tree_owner_->root()->GetAnimator()->StopAnimating(); |
| 442 } | 446 } |
| 443 | 447 |
| 444 } // namespace ash | 448 } // namespace ash |
| OLD | NEW |