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

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: Sami's suggestions 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 void CurrentIdleTaskDeadlineCallbackForTesting(
rmcilroy 2015/03/27 14:34:41 nit - GetCurrentIdleTaskDeadlineCallbackForTesting
alex clarke (OOO till 29th) 2015/03/27 16:09:17 Done.
52 base::TimeTicks* deadline_out) const;
56 53
57 private: 54 private:
58 friend class RendererSchedulerImplTest; 55 friend class RendererSchedulerImplTest;
59 friend class RendererSchedulerImplForTest; 56 friend class RendererSchedulerImplForTest;
60 57
61 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. 58 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum.
62 enum QueueId { 59 enum QueueId {
63 DEFAULT_TASK_QUEUE, 60 COMPOSITOR_TASK_QUEUE = SchedulerHelper::TASK_QUEUE_COUNT,
64 COMPOSITOR_TASK_QUEUE,
65 LOADING_TASK_QUEUE, 61 LOADING_TASK_QUEUE,
66 IDLE_TASK_QUEUE,
67 CONTROL_TASK_QUEUE,
68 CONTROL_TASK_AFTER_WAKEUP_QUEUE,
69 // Must be the last entry. 62 // Must be the last entry.
70 TASK_QUEUE_COUNT, 63 TASK_QUEUE_COUNT,
71 }; 64 };
72 65
73 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. 66 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum.
74 enum class Policy { 67 enum class Policy {
75 NORMAL, 68 NORMAL,
76 COMPOSITOR_PRIORITY, 69 COMPOSITOR_PRIORITY,
77 TOUCHSTART_PRIORITY, 70 TOUCHSTART_PRIORITY,
78 }; 71 };
79 72
80 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this 73 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this
81 // enum. 74 // enum.
82 enum class InputStreamState { 75 enum class InputStreamState {
83 INACTIVE, 76 INACTIVE,
84 ACTIVE, 77 ACTIVE,
85 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE 78 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 }; 79 };
87 80
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 { 81 class PollableNeedsUpdateFlag {
98 public: 82 public:
99 PollableNeedsUpdateFlag(base::Lock* write_lock); 83 PollableNeedsUpdateFlag(base::Lock* write_lock);
100 ~PollableNeedsUpdateFlag(); 84 ~PollableNeedsUpdateFlag();
101 85
102 // Set the flag. May only be called if |write_lock| is held. 86 // Set the flag. May only be called if |write_lock| is held.
103 void SetWhileLocked(bool value); 87 void SetWhileLocked(bool value);
104 88
105 // Returns true iff the flag is set to true. 89 // Returns true iff the flag is set to true.
106 bool IsSet() const; 90 bool IsSet() const;
107 91
108 private: 92 private:
109 base::subtle::Atomic32 flag_; 93 base::subtle::Atomic32 flag_;
110 base::Lock* write_lock_; // Not owned. 94 base::Lock* write_lock_; // Not owned.
111 95
112 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag); 96 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
113 }; 97 };
114 98
99 // SchedulerHelperDelegate implementation:
100 bool CanEnterLongIdlePeriod(
101 base::TimeTicks now,
102 base::TimeDelta* next_long_idle_period_delay_out) override;
103
115 // Returns the serialized scheduler state for tracing. 104 // Returns the serialized scheduler state for tracing.
116 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked( 105 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked(
117 base::TimeTicks optional_now) const; 106 base::TimeTicks optional_now) const;
118 static const char* TaskQueueIdToString(QueueId queue_id); 107 static const char* TaskQueueIdToString(QueueId queue_id);
119 static const char* PolicyToString(Policy policy); 108 static const char* PolicyToString(Policy policy);
120 static const char* InputStreamStateToString(InputStreamState state); 109 static const char* InputStreamStateToString(InputStreamState state);
121 static const char* IdlePeriodStateToString(IdlePeriodState state);
122 110
123 static InputStreamState ComputeNewInputStreamState( 111 static InputStreamState ComputeNewInputStreamState(
124 InputStreamState current_state, 112 InputStreamState current_state,
125 blink::WebInputEvent::Type new_input_event, 113 blink::WebInputEvent::Type new_input_event,
126 blink::WebInputEvent::Type last_input_event); 114 blink::WebInputEvent::Type last_input_event);
127 115
128 // The time we should stay in a priority-escalated mode after an input event. 116 // The time we should stay in a priority-escalated mode after an input event.
129 static const int kPriorityEscalationAfterInputMillis = 100; 117 static const int kPriorityEscalationAfterInputMillis = 100;
130 118
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. 119 // Returns the current scheduler policy. Must be called from the main thread.
141 Policy SchedulerPolicy() const; 120 Policy SchedulerPolicy() const;
142 121
143 // Schedules an immediate PolicyUpdate, if there isn't one already pending and 122 // Schedules an immediate PolicyUpdate, if there isn't one already pending and
144 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be 123 // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be
145 // locked. 124 // locked.
146 void EnsureUrgentPolicyUpdatePostedOnMainThread( 125 void EnsureUrgentPolicyUpdatePostedOnMainThread(
147 const tracked_objects::Location& from_here); 126 const tracked_objects::Location& from_here);
148 127
149 // Update the policy if a new signal has arrived. Must be called from the main 128 // 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); 146 base::TimeDelta* new_policy_duration);
168 147
169 // An input event of some sort happened, the policy may need updating. 148 // An input event of some sort happened, the policy may need updating.
170 void UpdateForInputEvent(blink::WebInputEvent::Type type); 149 void UpdateForInputEvent(blink::WebInputEvent::Type type);
171 150
172 // Called when a previously queued input event was processed. 151 // Called when a previously queued input event was processed.
173 // |begin_frame_time|, if non-zero, identifies the frame time at which the 152 // |begin_frame_time|, if non-zero, identifies the frame time at which the
174 // input was processed. 153 // input was processed.
175 void DidProcessInputEvent(base::TimeTicks begin_frame_time); 154 void DidProcessInputEvent(base::TimeTicks begin_frame_time);
176 155
177 // Returns the new idle period state for the next long idle period. Fills in 156 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 157
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_; 158 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_; 159 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
204 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_; 160 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
205 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
206 161
207 base::Closure update_policy_closure_; 162 base::Closure update_policy_closure_;
208 DeadlineTaskRunner delayed_update_policy_runner_; 163 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 164
213 // Don't access current_policy_ directly, instead use SchedulerPolicy(). 165 // Don't access current_policy_ directly, instead use SchedulerPolicy().
214 Policy current_policy_; 166 Policy current_policy_;
215 IdlePeriodState idle_period_state_;
216
217 base::TimeTicks estimated_next_frame_begin_;
218 base::TimeTicks current_policy_expiration_time_; 167 base::TimeTicks current_policy_expiration_time_;
219 168
220 // The incoming_signals_lock_ mutex protects access to all variables in the 169 // The incoming_signals_lock_ mutex protects access to all variables in the
221 // (contiguous) block below. 170 // (contiguous) block below.
222 base::Lock incoming_signals_lock_; 171 base::Lock incoming_signals_lock_;
223 base::TimeTicks last_input_receipt_time_on_compositor_; 172 base::TimeTicks last_input_receipt_time_on_compositor_;
224 base::TimeTicks last_input_process_time_on_main_; 173 base::TimeTicks last_input_process_time_on_main_;
225 blink::WebInputEvent::Type last_input_type_; 174 blink::WebInputEvent::Type last_input_type_;
226 InputStreamState input_stream_state_; 175 InputStreamState input_stream_state_;
227 PollableNeedsUpdateFlag policy_may_need_update_; 176 PollableNeedsUpdateFlag policy_may_need_update_;
228 177
229 scoped_refptr<cc::TestNowSource> time_source_;
230
231 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_;
232 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; 178 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
233 179
234 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); 180 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
235 }; 181 };
236 182
237 } // namespace content 183 } // namespace content
238 184
239 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ 185 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698