Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: content/renderer/scheduler/renderer_scheduler_impl.h

Issue 1025323003: Introduce a SchedulerHelper in content/child/scheduler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 #ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 5 #ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
6 #define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 6 #define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
7 7
8 #include "base/atomicops.h" 8 #include "base/atomicops.h"
9 #include "base/synchronization/lock.h" 9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h" 10 #include "content/child/scheduler/scheduler_helper.h"
11 #include "cc/test/test_now_source.h"
12 #include "content/renderer/scheduler/cancelable_closure_holder.h"
13 #include "content/renderer/scheduler/deadline_task_runner.h" 11 #include "content/renderer/scheduler/deadline_task_runner.h"
14 #include "content/renderer/scheduler/renderer_scheduler.h" 12 #include "content/renderer/scheduler/renderer_scheduler.h"
15 #include "content/renderer/scheduler/single_thread_idle_task_runner.h"
16 #include "content/renderer/scheduler/task_queue_manager.h"
17 13
18 namespace base { 14 namespace base {
19 namespace trace_event { 15 namespace trace_event {
20 class ConvertableToTraceFormat; 16 class ConvertableToTraceFormat;
21 } 17 }
22 } 18 }
23 19
24 namespace content { 20 namespace content {
25 21
26 class RendererTaskQueueSelector; 22 class CONTENT_EXPORT RendererSchedulerImpl
27 class NestableSingleThreadTaskRunner; 23 : public RendererScheduler,
28 24 public SchedulerHelper::SchedulerHelperDelegate {
29 class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler {
30 public: 25 public:
31 RendererSchedulerImpl( 26 RendererSchedulerImpl(
32 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner); 27 scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner);
33 ~RendererSchedulerImpl() override; 28 ~RendererSchedulerImpl() override;
34 29
35 // RendererScheduler implementation: 30 // RendererScheduler implementation:
36 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override; 31 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override;
32 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override;
37 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override; 33 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override;
38 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override;
39 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override; 34 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override;
40 void WillBeginFrame(const cc::BeginFrameArgs& args) override; 35 void WillBeginFrame(const cc::BeginFrameArgs& args) override;
41 void BeginFrameNotExpectedSoon() override; 36 void BeginFrameNotExpectedSoon() override;
42 void DidCommitFrameToCompositor() override; 37 void DidCommitFrameToCompositor() override;
43 void DidReceiveInputEventOnCompositorThread( 38 void DidReceiveInputEventOnCompositorThread(
44 const blink::WebInputEvent& web_input_event) override; 39 const blink::WebInputEvent& web_input_event) override;
45 void DidAnimateForInputOnCompositorThread() override; 40 void DidAnimateForInputOnCompositorThread() override;
46 bool CanExceedIdleDeadlineIfRequired() const override;
47 bool IsHighPriorityWorkAnticipated() override; 41 bool IsHighPriorityWorkAnticipated() override;
48 bool ShouldYieldForHighPriorityWork() override; 42 bool ShouldYieldForHighPriorityWork() override;
43 bool CanExceedIdleDeadlineIfRequired() const override;
49 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; 44 void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override;
50 void RemoveTaskObserver( 45 void RemoveTaskObserver(
51 base::MessageLoop::TaskObserver* task_observer) override; 46 base::MessageLoop::TaskObserver* task_observer) override;
52 void Shutdown() override; 47 void Shutdown() override;
53 48
54 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); 49 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source);
55 void SetWorkBatchSizeForTesting(size_t work_batch_size); 50 void SetWorkBatchSizeForTesting(size_t work_batch_size);
51 base::TimeTicks CurrentIdleTaskDeadlineForTesting() const;
56 52
57 private: 53 private:
58 friend class RendererSchedulerImplTest; 54 friend class RendererSchedulerImplTest;
59 friend class RendererSchedulerImplForTest; 55 friend class RendererSchedulerImplForTest;
60 56
61 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. 57 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum.
62 enum QueueId { 58 enum QueueId {
63 DEFAULT_TASK_QUEUE, 59 COMPOSITOR_TASK_QUEUE = SchedulerHelper::TASK_QUEUE_COUNT,
64 COMPOSITOR_TASK_QUEUE,
65 LOADING_TASK_QUEUE, 60 LOADING_TASK_QUEUE,
66 IDLE_TASK_QUEUE,
67 CONTROL_TASK_QUEUE,
68 CONTROL_TASK_AFTER_WAKEUP_QUEUE,
69 // Must be the last entry. 61 // Must be the last entry.
70 TASK_QUEUE_COUNT, 62 TASK_QUEUE_COUNT,
71 }; 63 };
72 64
73 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. 65 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum.
74 enum class Policy { 66 enum class Policy {
75 NORMAL, 67 NORMAL,
76 COMPOSITOR_PRIORITY, 68 COMPOSITOR_PRIORITY,
77 TOUCHSTART_PRIORITY, 69 TOUCHSTART_PRIORITY,
78 }; 70 };
79 71
80 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this 72 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this
81 // enum. 73 // enum.
82 enum class InputStreamState { 74 enum class InputStreamState {
83 INACTIVE, 75 INACTIVE,
84 ACTIVE, 76 ACTIVE,
85 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE 77 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 }; 78 };
87 79
88 // Keep RendererSchedulerImpl::IdlePeriodStateToString in sync with this enum.
89 enum class IdlePeriodState {
90 NOT_IN_IDLE_PERIOD,
91 IN_SHORT_IDLE_PERIOD,
92 IN_LONG_IDLE_PERIOD,
93 IN_LONG_IDLE_PERIOD_WITH_MAX_DEADLINE,
94 ENDING_LONG_IDLE_PERIOD
95 };
96
97 class PollableNeedsUpdateFlag { 80 class PollableNeedsUpdateFlag {
98 public: 81 public:
99 PollableNeedsUpdateFlag(base::Lock* write_lock); 82 PollableNeedsUpdateFlag(base::Lock* write_lock);
100 ~PollableNeedsUpdateFlag(); 83 ~PollableNeedsUpdateFlag();
101 84
102 // Set the flag. May only be called if |write_lock| is held. 85 // Set the flag. May only be called if |write_lock| is held.
103 void SetWhileLocked(bool value); 86 void SetWhileLocked(bool value);
104 87
105 // Returns true iff the flag is set to true. 88 // Returns true iff the flag is set to true.
106 bool IsSet() const; 89 bool IsSet() const;
107 90
108 private: 91 private:
109 base::subtle::Atomic32 flag_; 92 base::subtle::Atomic32 flag_;
110 base::Lock* write_lock_; // Not owned. 93 base::Lock* write_lock_; // Not owned.
111 94
112 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag); 95 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
113 }; 96 };
114 97
98 // SchedulerHelperDelegate implementation:
99 bool CanEnterLongIdlePeriod(
100 base::TimeTicks now,
101 base::TimeDelta* next_long_idle_period_delay_out) override;
102
115 // Returns the serialized scheduler state for tracing. 103 // Returns the serialized scheduler state for tracing.
116 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked( 104 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked(
117 base::TimeTicks optional_now) const; 105 base::TimeTicks optional_now) const;
118 static const char* TaskQueueIdToString(QueueId queue_id); 106 static const char* TaskQueueIdToString(QueueId queue_id);
119 static const char* PolicyToString(Policy policy); 107 static const char* PolicyToString(Policy policy);
120 static const char* InputStreamStateToString(InputStreamState state); 108 static const char* InputStreamStateToString(InputStreamState state);
121 static const char* IdlePeriodStateToString(IdlePeriodState state);
122 109
123 static InputStreamState ComputeNewInputStreamState( 110 static InputStreamState ComputeNewInputStreamState(
124 InputStreamState current_state, 111 InputStreamState current_state,
125 blink::WebInputEvent::Type new_input_event, 112 blink::WebInputEvent::Type new_input_event,
126 blink::WebInputEvent::Type last_input_event); 113 blink::WebInputEvent::Type last_input_event);
127 114
128 // The time we should stay in a priority-escalated mode after an input event. 115 // The time we should stay in a priority-escalated mode after an input event.
129 static const int kPriorityEscalationAfterInputMillis = 100; 116 static const int kPriorityEscalationAfterInputMillis = 100;
130 117
131 // The maximum length of an idle period.
132 static const int kMaximumIdlePeriodMillis = 50;
133
134 // The minimum delay to wait between retrying to initiate a long idle time.
135 static const int kRetryInitiateLongIdlePeriodDelayMillis = 1;
136
137 // IdleTaskDeadlineSupplier Implementation:
138 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const;
139
140 // Returns the current scheduler policy. Must be called from the main thread. 118 // Returns the current scheduler policy. Must be called from the main thread.
141 Policy SchedulerPolicy() const; 119 Policy SchedulerPolicy() const;
142 120
143 // Schedules an immediate PolicyUpdate, if there isn't one already pending and 121 // Schedules an immediate PolicyUpdate, if there isn't one already pending and
144 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be 122 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be
145 // locked. 123 // locked.
146 void EnsureUrgentPolicyUpdatePostedOnMainThread( 124 void EnsureUrgentPolicyUpdatePostedOnMainThread(
147 const tracked_objects::Location& from_here); 125 const tracked_objects::Location& from_here);
148 126
149 // Update the policy if a new signal has arrived. Must be called from the main 127 // Update the policy if a new signal has arrived. Must be called from the main
(...skipping 17 matching lines...) Expand all
167 base::TimeDelta* new_policy_duration); 145 base::TimeDelta* new_policy_duration);
168 146
169 // An input event of some sort happened, the policy may need updating. 147 // An input event of some sort happened, the policy may need updating.
170 void UpdateForInputEvent(blink::WebInputEvent::Type type); 148 void UpdateForInputEvent(blink::WebInputEvent::Type type);
171 149
172 // Called when a previously queued input event was processed. 150 // Called when a previously queued input event was processed.
173 // |begin_frame_time|, if non-zero, identifies the frame time at which the 151 // |begin_frame_time|, if non-zero, identifies the frame time at which the
174 // input was processed. 152 // input was processed.
175 void DidProcessInputEvent(base::TimeTicks begin_frame_time); 153 void DidProcessInputEvent(base::TimeTicks begin_frame_time);
176 154
177 // Returns the new idle period state for the next long idle period. Fills in 155 SchedulerHelper helper_;
178 // |next_long_idle_period_delay_out| with the next time we should try to
179 // initiate the next idle period.
180 IdlePeriodState ComputeNewLongIdlePeriodState(
181 const base::TimeTicks now,
182 base::TimeDelta* next_long_idle_period_delay_out);
183 156
184 // Initiate a long idle period.
185 void InitiateLongIdlePeriod();
186 void InitiateLongIdlePeriodAfterWakeup();
187
188 // Start and end an idle period.
189 void StartIdlePeriod(IdlePeriodState new_idle_period_state);
190 void EndIdlePeriod();
191
192 // Returns true if |state| represents being within an idle period state.
193 static bool IsInIdlePeriod(IdlePeriodState state);
194
195 base::TimeTicks Now() const;
196
197 base::ThreadChecker main_thread_checker_;
198 scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_;
199 scoped_ptr<TaskQueueManager> task_queue_manager_;
200 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_; 157 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
201 scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_;
202 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
203 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; 158 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
204 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_; 159 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
205 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
206 160
207 base::Closure update_policy_closure_; 161 base::Closure update_policy_closure_;
208 DeadlineTaskRunner delayed_update_policy_runner_; 162 DeadlineTaskRunner delayed_update_policy_runner_;
209 CancelableClosureHolder end_idle_period_closure_;
210 CancelableClosureHolder initiate_next_long_idle_period_closure_;
211 CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_;
212 163
213 // Don't access current_policy_ directly, instead use SchedulerPolicy(). 164 // Don't access current_policy_ directly, instead use SchedulerPolicy().
214 Policy current_policy_; 165 Policy current_policy_;
215 IdlePeriodState idle_period_state_; 166 base::TimeTicks current_policy_expiration_time_;
216 167
217 base::TimeTicks estimated_next_frame_begin_; 168 base::TimeTicks estimated_next_frame_begin_;
218 base::TimeTicks current_policy_expiration_time_;
219 169
220 // The incoming_signals_lock_ mutex protects access to all variables in the 170 // The incoming_signals_lock_ mutex protects access to all variables in the
221 // (contiguous) block below. 171 // (contiguous) block below.
222 base::Lock incoming_signals_lock_; 172 base::Lock incoming_signals_lock_;
223 base::TimeTicks last_input_receipt_time_on_compositor_; 173 base::TimeTicks last_input_receipt_time_on_compositor_;
224 base::TimeTicks last_input_process_time_on_main_; 174 base::TimeTicks last_input_process_time_on_main_;
225 blink::WebInputEvent::Type last_input_type_; 175 blink::WebInputEvent::Type last_input_type_;
226 InputStreamState input_stream_state_; 176 InputStreamState input_stream_state_;
227 PollableNeedsUpdateFlag policy_may_need_update_; 177 PollableNeedsUpdateFlag policy_may_need_update_;
228 178
229 scoped_refptr<cc::TestNowSource> time_source_;
230
231 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_;
232 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; 179 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
233 180
234 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); 181 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
235 }; 182 };
236 183
237 } // namespace content 184 } // namespace content
238 185
239 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 186 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
OLDNEW
« no previous file with comments | « content/renderer/scheduler/renderer_scheduler.cc ('k') | content/renderer/scheduler/renderer_scheduler_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698