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 "base/threading/thread_checker.h"
11 #include "cc/test/test_now_source.h"
12 #include "content/renderer/scheduler/cancelable_closure_holder.h"
13 #include "content/renderer/scheduler/renderer_scheduler.h"
14 #include "content/renderer/scheduler/single_thread_idle_task_runner.h"
15 #include "content/renderer/scheduler/task_queue_manager.h"
18 namespace trace_event
{
19 class ConvertableToTraceFormat
;
25 class RendererTaskQueueSelector
;
27 class CONTENT_EXPORT RendererSchedulerImpl
: public RendererScheduler
{
29 RendererSchedulerImpl(
30 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
);
31 ~RendererSchedulerImpl() override
;
33 // RendererScheduler implementation:
34 scoped_refptr
<base::SingleThreadTaskRunner
> DefaultTaskRunner() override
;
35 scoped_refptr
<base::SingleThreadTaskRunner
> CompositorTaskRunner() override
;
36 scoped_refptr
<SingleThreadIdleTaskRunner
> IdleTaskRunner() override
;
37 scoped_refptr
<base::SingleThreadTaskRunner
> LoadingTaskRunner() override
;
38 void WillBeginFrame(const cc::BeginFrameArgs
& args
) override
;
39 void BeginFrameNotExpectedSoon() override
;
40 void DidCommitFrameToCompositor() override
;
41 void DidReceiveInputEventOnCompositorThread(
42 const blink::WebInputEvent
& web_input_event
) override
;
43 void DidAnimateForInputOnCompositorThread() override
;
44 bool IsHighPriorityWorkAnticipated() override
;
45 bool ShouldYieldForHighPriorityWork() override
;
46 void AddTaskObserver(base::MessageLoop::TaskObserver
* task_observer
) override
;
47 void RemoveTaskObserver(
48 base::MessageLoop::TaskObserver
* task_observer
) override
;
49 void Shutdown() override
;
51 void SetTimeSourceForTesting(scoped_refptr
<cc::TestNowSource
> time_source
);
52 void SetWorkBatchSizeForTesting(size_t work_batch_size
);
55 friend class RendererSchedulerImplTest
;
57 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum.
60 COMPOSITOR_TASK_QUEUE
,
64 CONTROL_TASK_AFTER_WAKEUP_QUEUE
,
65 // Must be the last entry.
75 enum class InputStreamState
{
78 ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE
81 class PollableNeedsUpdateFlag
{
83 PollableNeedsUpdateFlag(base::Lock
* write_lock
);
84 ~PollableNeedsUpdateFlag();
86 // Set the flag. May only be called if |write_lock| is held.
87 void SetWhileLocked(bool value
);
89 // Returns true iff the flag is set to true.
93 base::subtle::Atomic32 flag_
;
94 base::Lock
* write_lock_
; // Not owned.
96 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag
);
99 // Returns the serialized scheduler state for tracing.
100 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValueLocked(
101 base::TimeTicks optional_now
) const;
102 static const char* TaskQueueIdToString(QueueId queue_id
);
103 static const char* PolicyToString(Policy policy
);
104 static const char* InputStreamStateToString(InputStreamState state
);
106 static InputStreamState
ComputeNewInputStreamState(
107 InputStreamState current_state
,
108 blink::WebInputEvent::Type new_input_event
,
109 blink::WebInputEvent::Type last_input_event
);
111 // The time we should stay in a priority-escalated mode after an input event.
112 static const int kPriorityEscalationAfterInputMillis
= 100;
114 // IdleTaskDeadlineSupplier Implementation:
115 void CurrentIdleTaskDeadlineCallback(base::TimeTicks
* deadline_out
) const;
117 // Returns the current scheduler policy. Must be called from the main thread.
118 Policy
SchedulerPolicy() const;
120 // Posts a call to UpdatePolicy on the control runner to be run after |delay|
121 void PostUpdatePolicyOnControlRunner(base::TimeDelta delay
);
123 // Update the policy if a new signal has arrived. Must be called from the main
125 void MaybeUpdatePolicy();
127 // Updates the scheduler policy. Must be called from the main thread.
130 // Helper for computing the new policy. |new_policy_duration| will be filled
131 // with the amount of time after which the policy should be updated again. If
132 // the duration is zero, a new policy update will not be scheduled. Must be
133 // called with |incoming_signals_lock_| held.
134 Policy
ComputeNewPolicy(base::TimeDelta
* new_policy_duration
);
136 // An input event of some sort happened, the policy may need updating.
137 void UpdateForInputEvent(blink::WebInputEvent::Type type
);
139 // Called when a previously queued input event was processed.
140 // |begin_frame_time|, if non-zero, identifies the frame time at which the
141 // input was processed.
142 void DidProcessInputEvent(base::TimeTicks begin_frame_time
);
144 // Start and end an idle period.
145 void StartIdlePeriod();
146 void EndIdlePeriod();
148 base::TimeTicks
Now() const;
150 base::ThreadChecker main_thread_checker_
;
151 scoped_ptr
<RendererTaskQueueSelector
> renderer_task_queue_selector_
;
152 scoped_ptr
<TaskQueueManager
> task_queue_manager_
;
153 scoped_refptr
<base::SingleThreadTaskRunner
> control_task_runner_
;
154 scoped_refptr
<base::SingleThreadTaskRunner
> control_task_after_wakeup_runner_
;
155 scoped_refptr
<base::SingleThreadTaskRunner
> default_task_runner_
;
156 scoped_refptr
<base::SingleThreadTaskRunner
> compositor_task_runner_
;
157 scoped_refptr
<base::SingleThreadTaskRunner
> loading_task_runner_
;
158 scoped_refptr
<SingleThreadIdleTaskRunner
> idle_task_runner_
;
160 base::Closure update_policy_closure_
;
161 CancelableClosureHolder end_idle_period_closure_
;
163 // Don't access current_policy_ directly, instead use SchedulerPolicy().
164 Policy current_policy_
;
166 base::TimeTicks estimated_next_frame_begin_
;
168 // The incoming_signals_lock_ mutex protects access to all variables in the
169 // (contiguous) block below.
170 base::Lock incoming_signals_lock_
;
171 base::TimeTicks last_input_receipt_time_on_compositor_
;
172 base::TimeTicks last_input_process_time_on_main_
;
173 blink::WebInputEvent::Type last_input_type_
;
174 InputStreamState input_stream_state_
;
175 PollableNeedsUpdateFlag policy_may_need_update_
;
177 scoped_refptr
<cc::TestNowSource
> time_source_
;
179 base::WeakPtr
<RendererSchedulerImpl
> weak_renderer_scheduler_ptr_
;
180 base::WeakPtrFactory
<RendererSchedulerImpl
> weak_factory_
;
182 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl
);
185 } // namespace content
187 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_