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 committed_frame_number_(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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 | 523 |
553 void Compositor::SetLayerTreeDebugState( | 524 void Compositor::SetLayerTreeDebugState( |
554 const cc::LayerTreeDebugState& debug_state) { | 525 const cc::LayerTreeDebugState& debug_state) { |
555 host_->SetDebugState(debug_state); | 526 host_->SetDebugState(debug_state); |
556 } | 527 } |
557 | 528 |
558 const cc::RendererSettings& Compositor::GetRendererSettings() const { | 529 const cc::RendererSettings& Compositor::GetRendererSettings() const { |
559 return host_->GetSettings().renderer_settings; | 530 return host_->GetSettings().renderer_settings; |
560 } | 531 } |
561 | 532 |
562 scoped_refptr<CompositorLock> Compositor::GetCompositorLock() { | 533 std::unique_ptr<CompositorLock> Compositor::GetCompositorLock( |
563 if (!compositor_lock_) { | 534 CompositorLockClient* client, |
564 compositor_lock_ = new CompositorLock(this); | 535 base::TimeDelta timeout) { |
536 if (active_locks_.empty()) { | |
565 host_->SetDeferCommits(true); | 537 host_->SetDeferCommits(true); |
566 for (auto& observer : observer_list_) | 538 for (auto& observer : observer_list_) |
567 observer.OnCompositingLockStateChanged(this); | 539 observer.OnCompositingLockStateChanged(this); |
540 | |
541 if (!timeout.is_zero()) { | |
542 // The timeout task uses an independent WeakPtrFactory that is invalidated | |
543 // when all locks are ended to prevent the timeout from leaking into | |
544 // another lock that should have its own timeout. | |
545 task_runner_->PostDelayedTask( | |
546 FROM_HERE, | |
547 base::Bind(&Compositor::TimeoutLocks, | |
548 lock_timeout_weak_ptr_factory_.GetWeakPtr()), | |
549 timeout); | |
550 } | |
568 } | 551 } |
569 return compositor_lock_; | 552 // This uses the main WeakPtrFactory to break the connection from the lock to |
553 // the Compositor when the Compositor is destroyed. | |
554 auto lock = | |
555 base::MakeUnique<CompositorLock>(client, weak_ptr_factory_.GetWeakPtr()); | |
556 active_locks_.push_back(lock.get()); | |
danakj
2017/03/28 23:16:13
Oh, this change makes OnCompositingLockStateChange
| |
557 return lock; | |
570 } | 558 } |
571 | 559 |
572 void Compositor::UnlockCompositor() { | 560 void Compositor::RemoveCompositorLock(CompositorLock* lock) { |
573 DCHECK(compositor_lock_); | 561 base::Erase(active_locks_, lock); |
574 compositor_lock_ = NULL; | 562 if (active_locks_.empty()) { |
575 host_->SetDeferCommits(false); | 563 host_->SetDeferCommits(false); |
576 for (auto& observer : observer_list_) | 564 for (auto& observer : observer_list_) |
577 observer.OnCompositingLockStateChanged(this); | 565 observer.OnCompositingLockStateChanged(this); |
566 lock_timeout_weak_ptr_factory_.InvalidateWeakPtrs(); | |
567 } | |
578 } | 568 } |
579 | 569 |
580 void Compositor::CancelCompositorLock() { | 570 void Compositor::TimeoutLocks() { |
581 if (compositor_lock_) | 571 // Make a copy, we're going to cause |active_locks_| to become |
582 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()); | |
583 } | 577 } |
584 | 578 |
585 } // namespace ui | 579 } // namespace ui |
OLD | NEW |