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 "content/renderer/scheduler/renderer_scheduler.h"
12 #include "content/renderer/scheduler/single_thread_idle_task_runner.h"
13 #include "content/renderer/scheduler/task_queue_manager.h"
17 class RendererTaskQueueSelector;
19 class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler {
21 RendererSchedulerImpl(
22 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
23 ~RendererSchedulerImpl() override;
25 // RendererScheduler implementation:
26 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override;
27 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override;
28 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override;
29 void WillBeginFrame(const cc::BeginFrameArgs& args) override;
30 void DidCommitFrameToCompositor() override;
31 void DidReceiveInputEventOnCompositorThread() override;
32 bool ShouldYieldForHighPriorityWork() override;
33 void Shutdown() override;
36 // Virtual for testing.
37 virtual base::TimeTicks Now() const;
40 friend class RendererSchedulerImplTest;
44 COMPOSITOR_TASK_QUEUE,
47 // Must be the last entry.
52 NORMAL_PRIORITY_POLICY,
53 COMPOSITOR_PRIORITY_POLICY,
56 class PollableNeedsUpdateFlag {
58 PollableNeedsUpdateFlag(base::Lock* write_lock);
59 ~PollableNeedsUpdateFlag();
61 // Set the flag. May only be called if |write_lock| is held.
62 void SetLocked(bool value);
64 // Returns true iff the flag was set to true.
68 base::subtle::Atomic32 flag_;
69 base::Lock* write_lock_; // Not owned.
70 base::ThreadChecker thread_checker_;
72 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
75 // The time we should stay in CompositorPriority mode for after a touch event.
76 static const int kCompositorPriorityAfterTouchMillis = 100;
78 // IdleTaskDeadlineSupplier Implementation:
79 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const;
81 // Returns the current scheduler policy. Must be called from the main thread.
82 Policy SchedulerPolicy() const;
84 // Posts a call to UpdatePolicy on the control runner to be run after |delay|
85 void PostUpdatePolicyOnControlRunner(base::TimeDelta delay);
87 // Update the policy if a new signal has arrived. Must be called from the main
89 void MaybeUpdatePolicy();
91 // Updates the scheduler policy. Must be called from the main thread.
94 // Start and end an idle period.
95 void StartIdlePeriod();
98 base::ThreadChecker main_thread_checker_;
99 scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_;
100 scoped_ptr<TaskQueueManager> task_queue_manager_;
101 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
102 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
103 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
104 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
106 base::Closure update_policy_closure_;
107 base::Closure end_idle_period_closure_;
109 // Don't access current_policy_ directly, instead use SchedulerPolicy().
110 Policy current_policy_;
112 base::TimeTicks estimated_next_frame_begin_;
114 // The incoming_signals_lock_ mutex protects access to last_input_time_
115 // and write access to policy_may_need_update_.
116 base::Lock incoming_signals_lock_;
117 base::TimeTicks last_input_time_;
118 PollableNeedsUpdateFlag policy_may_need_update_;
120 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_;
121 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
123 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
126 } // namespace content
128 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_