OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 #ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | |
6 #define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | |
7 | |
8 #include "base/atomicops.h" | |
9 #include "base/synchronization/lock.h" | |
10 #include "content/child/scheduler/scheduler_helper.h" | |
11 #include "content/renderer/scheduler/deadline_task_runner.h" | |
12 #include "content/renderer/scheduler/renderer_scheduler.h" | |
13 | |
14 namespace base { | |
15 namespace trace_event { | |
16 class ConvertableToTraceFormat; | |
17 } | |
18 } | |
19 | |
20 namespace content { | |
21 | |
22 class CONTENT_EXPORT RendererSchedulerImpl | |
23 : public RendererScheduler, | |
24 public SchedulerHelper::SchedulerHelperDelegate { | |
25 public: | |
26 RendererSchedulerImpl( | |
27 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner); | |
28 ~RendererSchedulerImpl() override; | |
29 | |
30 // RendererScheduler implementation: | |
31 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override; | |
32 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override; | |
33 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override; | |
34 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override; | |
35 scoped_refptr<base::SingleThreadTaskRunner> TimerTaskRunner() override; | |
36 void WillBeginFrame(const cc::BeginFrameArgs& args) override; | |
37 void BeginFrameNotExpectedSoon() override; | |
38 void DidCommitFrameToCompositor() override; | |
39 void DidReceiveInputEventOnCompositorThread( | |
40 const blink::WebInputEvent& web_input_event) override; | |
41 void DidAnimateForInputOnCompositorThread() override; | |
42 void OnRendererHidden() override; | |
43 void OnRendererVisible() override; | |
44 bool IsHighPriorityWorkAnticipated() override; | |
45 bool ShouldYieldForHighPriorityWork() override; | |
46 bool CanExceedIdleDeadlineIfRequired() const override; | |
47 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | |
48 void RemoveTaskObserver( | |
49 base::MessageLoop::TaskObserver* task_observer) override; | |
50 void Shutdown() override; | |
51 void SuspendTimerQueue() override; | |
52 void ResumeTimerQueue() override; | |
53 | |
54 SchedulerHelper* GetSchedulerHelperForTesting(); | |
55 void SetWorkBatchSizeForTesting(size_t work_batch_size); | |
56 base::TimeTicks CurrentIdleTaskDeadlineForTesting() const; | |
57 | |
58 private: | |
59 friend class RendererSchedulerImplTest; | |
60 friend class RendererSchedulerImplForTest; | |
61 | |
62 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. | |
63 enum QueueId { | |
64 COMPOSITOR_TASK_QUEUE = SchedulerHelper::TASK_QUEUE_COUNT, | |
65 LOADING_TASK_QUEUE, | |
66 TIMER_TASK_QUEUE, | |
67 // Must be the last entry. | |
68 TASK_QUEUE_COUNT, | |
69 }; | |
70 | |
71 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. | |
72 enum class Policy { | |
73 NORMAL, | |
74 COMPOSITOR_PRIORITY, | |
75 TOUCHSTART_PRIORITY, | |
76 }; | |
77 | |
78 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this | |
79 // enum. | |
80 enum class InputStreamState { | |
81 INACTIVE, | |
82 ACTIVE, | |
83 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE | |
84 }; | |
85 | |
86 class PollableNeedsUpdateFlag { | |
87 public: | |
88 PollableNeedsUpdateFlag(base::Lock* write_lock); | |
89 ~PollableNeedsUpdateFlag(); | |
90 | |
91 // Set the flag. May only be called if |write_lock| is held. | |
92 void SetWhileLocked(bool value); | |
93 | |
94 // Returns true iff the flag is set to true. | |
95 bool IsSet() const; | |
96 | |
97 private: | |
98 base::subtle::Atomic32 flag_; | |
99 base::Lock* write_lock_; // Not owned. | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag); | |
102 }; | |
103 | |
104 // SchedulerHelperDelegate implementation: | |
105 bool CanEnterLongIdlePeriod( | |
106 base::TimeTicks now, | |
107 base::TimeDelta* next_long_idle_period_delay_out) override; | |
108 void IsNotQuiescent() override {} | |
109 | |
110 void EndIdlePeriod(); | |
111 | |
112 // Returns the serialized scheduler state for tracing. | |
113 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked( | |
114 base::TimeTicks optional_now) const; | |
115 static const char* TaskQueueIdToString(QueueId queue_id); | |
116 static const char* PolicyToString(Policy policy); | |
117 static const char* InputStreamStateToString(InputStreamState state); | |
118 | |
119 static InputStreamState ComputeNewInputStreamState( | |
120 InputStreamState current_state, | |
121 blink::WebInputEvent::Type new_input_event, | |
122 blink::WebInputEvent::Type last_input_event); | |
123 | |
124 // The time we should stay in a priority-escalated mode after an input event. | |
125 static const int kPriorityEscalationAfterInputMillis = 100; | |
126 | |
127 // The amount of time which idle periods can continue being scheduled when the | |
128 // renderer has been hidden, before going to sleep for good. | |
129 static const int kEndIdleWhenHiddenDelayMillis = 10000; | |
130 | |
131 // Returns the current scheduler policy. Must be called from the main thread. | |
132 Policy SchedulerPolicy() const; | |
133 | |
134 // Schedules an immediate PolicyUpdate, if there isn't one already pending and | |
135 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be | |
136 // locked. | |
137 void EnsureUrgentPolicyUpdatePostedOnMainThread( | |
138 const tracked_objects::Location& from_here); | |
139 | |
140 // Update the policy if a new signal has arrived. Must be called from the main | |
141 // thread. | |
142 void MaybeUpdatePolicy(); | |
143 | |
144 // Locks |incoming_signals_lock_| and updates the scheduler policy. May early | |
145 // out if the policy is unchanged. Must be called from the main thread. | |
146 void UpdatePolicy(); | |
147 | |
148 // Like UpdatePolicy, except it doesn't early out. | |
149 void ForceUpdatePolicy(); | |
150 | |
151 enum class UpdateType { | |
152 MAY_EARLY_OUT_IF_POLICY_UNCHANGED, | |
153 FORCE_UPDATE, | |
154 }; | |
155 | |
156 // The implelemtation of UpdatePolicy & ForceUpdatePolicy. It is allowed to | |
157 // early out if |update_type| is MAY_EARLY_OUT_IF_POLICY_UNCHANGED. | |
158 virtual void UpdatePolicyLocked(UpdateType update_type); | |
159 | |
160 // Returns the amount of time left in the current input escalated priority | |
161 // policy. | |
162 base::TimeDelta TimeLeftInInputEscalatedPolicy(base::TimeTicks now) const; | |
163 | |
164 // Helper for computing the new policy. |new_policy_duration| will be filled | |
165 // with the amount of time after which the policy should be updated again. If | |
166 // the duration is zero, a new policy update will not be scheduled. Must be | |
167 // called with |incoming_signals_lock_| held. | |
168 Policy ComputeNewPolicy(base::TimeTicks now, | |
169 base::TimeDelta* new_policy_duration); | |
170 | |
171 // An input event of some sort happened, the policy may need updating. | |
172 void UpdateForInputEvent(blink::WebInputEvent::Type type); | |
173 | |
174 // Called when a previously queued input event was processed. | |
175 // |begin_frame_time|, if non-zero, identifies the frame time at which the | |
176 // input was processed. | |
177 void DidProcessInputEvent(base::TimeTicks begin_frame_time); | |
178 | |
179 SchedulerHelper helper_; | |
180 | |
181 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_; | |
182 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | |
183 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_; | |
184 scoped_refptr<base::SingleThreadTaskRunner> timer_task_runner_; | |
185 | |
186 base::Closure update_policy_closure_; | |
187 DeadlineTaskRunner delayed_update_policy_runner_; | |
188 CancelableClosureHolder end_renderer_hidden_idle_period_closure_; | |
189 | |
190 // Don't access current_policy_ directly, instead use SchedulerPolicy(). | |
191 Policy current_policy_; | |
192 base::TimeTicks current_policy_expiration_time_; | |
193 bool renderer_hidden_; | |
194 | |
195 base::TimeTicks estimated_next_frame_begin_; | |
196 | |
197 // The incoming_signals_lock_ mutex protects access to all variables in the | |
198 // (contiguous) block below. | |
199 base::Lock incoming_signals_lock_; | |
200 base::TimeTicks last_input_receipt_time_on_compositor_; | |
201 base::TimeTicks last_input_process_time_on_main_; | |
202 blink::WebInputEvent::Type last_input_type_; | |
203 InputStreamState input_stream_state_; | |
204 PollableNeedsUpdateFlag policy_may_need_update_; | |
205 int timer_queue_suspend_count_; // TIMER_TASK_QUEUE suspended if non-zero. | |
206 | |
207 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); | |
210 }; | |
211 | |
212 } // namespace content | |
213 | |
214 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | |
OLD | NEW |