Roll src/third_party/WebKit afc1431:5a99872 (svn 194212:194220)
[chromium-blink-merge.git] / content / renderer / scheduler / renderer_scheduler_impl.h
blob04c9ab888edc6639ccfa8b59fb515f4e6b7d2152
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.
5 #ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
6 #define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_
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"
14 namespace base {
15 namespace trace_event {
16 class ConvertableToTraceFormat;
20 namespace content {
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;
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;
54 SchedulerHelper* GetSchedulerHelperForTesting();
55 void SetWorkBatchSizeForTesting(size_t work_batch_size);
56 base::TimeTicks CurrentIdleTaskDeadlineForTesting() const;
58 private:
59 friend class RendererSchedulerImplTest;
60 friend class RendererSchedulerImplForTest;
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,
71 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum.
72 enum class Policy {
73 NORMAL,
74 COMPOSITOR_PRIORITY,
75 TOUCHSTART_PRIORITY,
78 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this
79 // enum.
80 enum class InputStreamState {
81 INACTIVE,
82 ACTIVE,
83 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 class PollableNeedsUpdateFlag {
87 public:
88 PollableNeedsUpdateFlag(base::Lock* write_lock);
89 ~PollableNeedsUpdateFlag();
91 // Set the flag. May only be called if |write_lock| is held.
92 void SetWhileLocked(bool value);
94 // Returns true iff the flag is set to true.
95 bool IsSet() const;
97 private:
98 base::subtle::Atomic32 flag_;
99 base::Lock* write_lock_; // Not owned.
101 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
104 // SchedulerHelperDelegate implementation:
105 bool CanEnterLongIdlePeriod(
106 base::TimeTicks now,
107 base::TimeDelta* next_long_idle_period_delay_out) override;
108 void IsNotQuiescent() override {}
110 void EndIdlePeriod();
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);
119 static InputStreamState ComputeNewInputStreamState(
120 InputStreamState current_state,
121 blink::WebInputEvent::Type new_input_event,
122 blink::WebInputEvent::Type last_input_event);
124 // The time we should stay in a priority-escalated mode after an input event.
125 static const int kPriorityEscalationAfterInputMillis = 100;
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;
131 // Returns the current scheduler policy. Must be called from the main thread.
132 Policy SchedulerPolicy() const;
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);
140 // Update the policy if a new signal has arrived. Must be called from the main
141 // thread.
142 void MaybeUpdatePolicy();
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();
148 // Like UpdatePolicy, except it doesn't early out.
149 void ForceUpdatePolicy();
151 enum class UpdateType {
152 MAY_EARLY_OUT_IF_POLICY_UNCHANGED,
153 FORCE_UPDATE,
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);
160 // Returns the amount of time left in the current input escalated priority
161 // policy.
162 base::TimeDelta TimeLeftInInputEscalatedPolicy(base::TimeTicks now) const;
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);
171 // An input event of some sort happened, the policy may need updating.
172 void UpdateForInputEvent(blink::WebInputEvent::Type type);
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);
179 SchedulerHelper helper_;
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_;
186 base::Closure update_policy_closure_;
187 DeadlineTaskRunner delayed_update_policy_runner_;
188 CancelableClosureHolder end_renderer_hidden_idle_period_closure_;
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_;
195 base::TimeTicks estimated_next_frame_begin_;
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.
207 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
209 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
212 } // namespace content
214 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_