| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/compositor/compositor.h" | 5 #include "ui/compositor/compositor.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/stl_util.h" |
| 17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 18 #include "base/sys_info.h" | 19 #include "base/sys_info.h" |
| 19 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
| 20 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 21 #include "cc/animation/animation_host.h" | 22 #include "cc/animation/animation_host.h" |
| 22 #include "cc/animation/animation_id_provider.h" | 23 #include "cc/animation/animation_id_provider.h" |
| 23 #include "cc/animation/animation_timeline.h" | 24 #include "cc/animation/animation_timeline.h" |
| 24 #include "cc/base/switches.h" | 25 #include "cc/base/switches.h" |
| 25 #include "cc/input/input_handler.h" | 26 #include "cc/input/input_handler.h" |
| 26 #include "cc/layers/layer.h" | 27 #include "cc/layers/layer.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 39 #include "ui/compositor/dip_util.h" | 40 #include "ui/compositor/dip_util.h" |
| 40 #include "ui/compositor/layer.h" | 41 #include "ui/compositor/layer.h" |
| 41 #include "ui/compositor/layer_animator_collection.h" | 42 #include "ui/compositor/layer_animator_collection.h" |
| 42 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 43 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
| 43 #include "ui/display/display_switches.h" | 44 #include "ui/display/display_switches.h" |
| 44 #include "ui/gfx/icc_profile.h" | 45 #include "ui/gfx/icc_profile.h" |
| 45 #include "ui/gl/gl_switches.h" | 46 #include "ui/gl/gl_switches.h" |
| 46 | 47 |
| 47 namespace { | 48 namespace { |
| 48 | 49 |
| 49 const double kDefaultRefreshRate = 60.0; | 50 constexpr double kDefaultRefreshRate = 60.0; |
| 50 const double kTestRefreshRate = 200.0; | 51 constexpr double kTestRefreshRate = 200.0; |
| 51 | 52 |
| 52 } // namespace | 53 } // namespace |
| 53 | 54 |
| 54 namespace ui { | 55 namespace ui { |
| 55 | 56 |
| 56 CompositorLock::CompositorLock(Compositor* compositor) | |
| 57 : compositor_(compositor) { | |
| 58 if (compositor_->locks_will_time_out_) { | |
| 59 compositor_->task_runner_->PostDelayedTask( | |
| 60 FROM_HERE, | |
| 61 base::Bind(&CompositorLock::CancelLock, AsWeakPtr()), | |
| 62 base::TimeDelta::FromMilliseconds(kCompositorLockTimeoutMs)); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 CompositorLock::~CompositorLock() { | |
| 67 CancelLock(); | |
| 68 } | |
| 69 | |
| 70 void CompositorLock::CancelLock() { | |
| 71 if (!compositor_) | |
| 72 return; | |
| 73 compositor_->UnlockCompositor(); | |
| 74 compositor_ = NULL; | |
| 75 } | |
| 76 | |
| 77 Compositor::Compositor(const cc::FrameSinkId& frame_sink_id, | 57 Compositor::Compositor(const cc::FrameSinkId& frame_sink_id, |
| 78 ui::ContextFactory* context_factory, | 58 ui::ContextFactory* context_factory, |
| 79 ui::ContextFactoryPrivate* context_factory_private, | 59 ui::ContextFactoryPrivate* context_factory_private, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 60 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 81 : context_factory_(context_factory), | 61 : context_factory_(context_factory), |
| 82 context_factory_private_(context_factory_private), | 62 context_factory_private_(context_factory_private), |
| 83 root_layer_(NULL), | |
| 84 widget_(gfx::kNullAcceleratedWidget), | |
| 85 activated_frame_count_(0), | |
| 86 widget_valid_(false), | |
| 87 compositor_frame_sink_requested_(false), | |
| 88 frame_sink_id_(frame_sink_id), | 63 frame_sink_id_(frame_sink_id), |
| 89 task_runner_(task_runner), | 64 task_runner_(task_runner), |
| 90 vsync_manager_(new CompositorVSyncManager()), | 65 vsync_manager_(new CompositorVSyncManager()), |
| 91 device_scale_factor_(0.0f), | |
| 92 locks_will_time_out_(true), | |
| 93 compositor_lock_(NULL), | |
| 94 layer_animator_collection_(this), | 66 layer_animator_collection_(this), |
| 95 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this), |
| 68 lock_timeout_weak_ptr_factory_(this) { |
| 96 if (context_factory_private) { | 69 if (context_factory_private) { |
| 97 context_factory_private->GetSurfaceManager()->RegisterFrameSinkId( | 70 context_factory_private->GetSurfaceManager()->RegisterFrameSinkId( |
| 98 frame_sink_id_); | 71 frame_sink_id_); |
| 99 } | 72 } |
| 100 root_web_layer_ = cc::Layer::Create(); | 73 root_web_layer_ = cc::Layer::Create(); |
| 101 | 74 |
| 102 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 75 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 103 | 76 |
| 104 cc::LayerTreeSettings settings; | 77 cc::LayerTreeSettings settings; |
| 105 | 78 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 | 194 |
| 222 if (command_line->HasSwitch(switches::kUISlowAnimations)) { | 195 if (command_line->HasSwitch(switches::kUISlowAnimations)) { |
| 223 slow_animations_ = base::MakeUnique<ScopedAnimationDurationScaleMode>( | 196 slow_animations_ = base::MakeUnique<ScopedAnimationDurationScaleMode>( |
| 224 ScopedAnimationDurationScaleMode::SLOW_DURATION); | 197 ScopedAnimationDurationScaleMode::SLOW_DURATION); |
| 225 } | 198 } |
| 226 } | 199 } |
| 227 | 200 |
| 228 Compositor::~Compositor() { | 201 Compositor::~Compositor() { |
| 229 TRACE_EVENT0("shutdown", "Compositor::destructor"); | 202 TRACE_EVENT0("shutdown", "Compositor::destructor"); |
| 230 | 203 |
| 231 CancelCompositorLock(); | |
| 232 DCHECK(!compositor_lock_); | |
| 233 | |
| 234 for (auto& observer : observer_list_) | 204 for (auto& observer : observer_list_) |
| 235 observer.OnCompositingShuttingDown(this); | 205 observer.OnCompositingShuttingDown(this); |
| 236 | 206 |
| 237 for (auto& observer : animation_observer_list_) | 207 for (auto& observer : animation_observer_list_) |
| 238 observer.OnCompositingShuttingDown(this); | 208 observer.OnCompositingShuttingDown(this); |
| 239 | 209 |
| 240 if (root_layer_) | 210 if (root_layer_) |
| 241 root_layer_->ResetCompositor(); | 211 root_layer_->ResetCompositor(); |
| 242 | 212 |
| 243 if (animation_timeline_) | 213 if (animation_timeline_) |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 CompositorAnimationObserver* observer) { | 451 CompositorAnimationObserver* observer) { |
| 482 animation_observer_list_.RemoveObserver(observer); | 452 animation_observer_list_.RemoveObserver(observer); |
| 483 } | 453 } |
| 484 | 454 |
| 485 bool Compositor::HasAnimationObserver( | 455 bool Compositor::HasAnimationObserver( |
| 486 const CompositorAnimationObserver* observer) const { | 456 const CompositorAnimationObserver* observer) const { |
| 487 return animation_observer_list_.HasObserver(observer); | 457 return animation_observer_list_.HasObserver(observer); |
| 488 } | 458 } |
| 489 | 459 |
| 490 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { | 460 void Compositor::BeginMainFrame(const cc::BeginFrameArgs& args) { |
| 461 DCHECK(!IsLocked()); |
| 491 for (auto& observer : animation_observer_list_) | 462 for (auto& observer : animation_observer_list_) |
| 492 observer.OnAnimationStep(args.frame_time); | 463 observer.OnAnimationStep(args.frame_time); |
| 493 if (animation_observer_list_.might_have_observers()) | 464 if (animation_observer_list_.might_have_observers()) |
| 494 host_->SetNeedsAnimate(); | 465 host_->SetNeedsAnimate(); |
| 495 } | 466 } |
| 496 | 467 |
| 497 void Compositor::BeginMainFrameNotExpectedSoon() { | 468 void Compositor::BeginMainFrameNotExpectedSoon() { |
| 498 } | 469 } |
| 499 | 470 |
| 500 static void SendDamagedRectsRecursive(ui::Layer* layer) { | 471 static void SendDamagedRectsRecursive(ui::Layer* layer) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 | 521 |
| 551 void Compositor::SetLayerTreeDebugState( | 522 void Compositor::SetLayerTreeDebugState( |
| 552 const cc::LayerTreeDebugState& debug_state) { | 523 const cc::LayerTreeDebugState& debug_state) { |
| 553 host_->SetDebugState(debug_state); | 524 host_->SetDebugState(debug_state); |
| 554 } | 525 } |
| 555 | 526 |
| 556 const cc::RendererSettings& Compositor::GetRendererSettings() const { | 527 const cc::RendererSettings& Compositor::GetRendererSettings() const { |
| 557 return host_->GetSettings().renderer_settings; | 528 return host_->GetSettings().renderer_settings; |
| 558 } | 529 } |
| 559 | 530 |
| 560 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 531 std::unique_ptr<CompositorLock> Compositor::GetCompositorLock( |
| 561 if (!compositor_lock_) { | 532 CompositorLockClient* client, |
| 562 compositor_lock_ = new CompositorLock(this); | 533 base::TimeDelta timeout) { |
| 534 // This uses the main WeakPtrFactory to break the connection from the lock to |
| 535 // the Compositor when the Compositor is destroyed. |
| 536 auto lock = |
| 537 base::MakeUnique<CompositorLock>(client, weak_ptr_factory_.GetWeakPtr()); |
| 538 bool was_empty = active_locks_.empty(); |
| 539 active_locks_.push_back(lock.get()); |
| 540 |
| 541 if (was_empty) { |
| 563 host_->SetDeferCommits(true); | 542 host_->SetDeferCommits(true); |
| 564 for (auto& observer : observer_list_) | 543 for (auto& observer : observer_list_) |
| 565 observer.OnCompositingLockStateChanged(this); | 544 observer.OnCompositingLockStateChanged(this); |
| 545 |
| 546 if (!timeout.is_zero()) { |
| 547 // The timeout task uses an independent WeakPtrFactory that is invalidated |
| 548 // when all locks are ended to prevent the timeout from leaking into |
| 549 // another lock that should have its own timeout. |
| 550 task_runner_->PostDelayedTask( |
| 551 FROM_HERE, |
| 552 base::Bind(&Compositor::TimeoutLocks, |
| 553 lock_timeout_weak_ptr_factory_.GetWeakPtr()), |
| 554 timeout); |
| 555 } |
| 566 } | 556 } |
| 567 return compositor_lock_; | 557 return lock; |
| 568 } | 558 } |
| 569 | 559 |
| 570 void Compositor::UnlockCompositor() { | 560 void Compositor::RemoveCompositorLock(CompositorLock* lock) { |
| 571 DCHECK(compositor_lock_); | 561 base::Erase(active_locks_, lock); |
| 572 compositor_lock_ = NULL; | 562 if (active_locks_.empty()) { |
| 573 host_->SetDeferCommits(false); | 563 host_->SetDeferCommits(false); |
| 574 for (auto& observer : observer_list_) | 564 for (auto& observer : observer_list_) |
| 575 observer.OnCompositingLockStateChanged(this); | 565 observer.OnCompositingLockStateChanged(this); |
| 566 lock_timeout_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 567 } |
| 576 } | 568 } |
| 577 | 569 |
| 578 void Compositor::CancelCompositorLock() { | 570 void Compositor::TimeoutLocks() { |
| 579 if (compositor_lock_) | 571 // Make a copy, we're going to cause |active_locks_| to become |
| 580 compositor_lock_->CancelLock(); | 572 // empty. |
| 573 std::vector<CompositorLock*> locks = active_locks_; |
| 574 for (auto* lock : locks) |
| 575 lock->TimeoutLock(); |
| 576 DCHECK(active_locks_.empty()); |
| 581 } | 577 } |
| 582 | 578 |
| 583 } // namespace ui | 579 } // namespace ui |
| OLD | NEW |