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"
15 namespace trace_event
{
16 class ConvertableToTraceFormat
;
22 class CONTENT_EXPORT RendererSchedulerImpl
23 : public RendererScheduler
,
24 public SchedulerHelper::SchedulerHelperDelegate
{
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;
59 friend class RendererSchedulerImplTest
;
60 friend class RendererSchedulerImplForTest
;
62 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum.
64 COMPOSITOR_TASK_QUEUE
= SchedulerHelper::TASK_QUEUE_COUNT
,
67 // Must be the last entry.
71 // Keep RendererSchedulerImpl::PolicyToString in sync with this enum.
78 // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this
80 enum class InputStreamState
{
83 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
86 class PollableNeedsUpdateFlag
{
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.
98 base::subtle::Atomic32 flag_
;
99 base::Lock
* write_lock_
; // Not owned.
101 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag
);
104 // SchedulerHelperDelegate implementation:
105 bool CanEnterLongIdlePeriod(
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
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
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.
148 // Like UpdatePolicy, except it doesn't early out.
149 void ForceUpdatePolicy();
151 enum class UpdateType
{
152 MAY_EARLY_OUT_IF_POLICY_UNCHANGED
,
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
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_