[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / scheduler / renderer_scheduler_impl.h
blobe3b3b7dbd38448117653c48034597ec61b12a902
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"
17 namespace base {
18 namespace trace_event {
19 class ConvertableToTraceFormat;
22 // TODO(ssid): remove these aliases after the tracing clients are moved to the
23 // new trace_event namespace. See crbug.com/451032. ETA: March 2015
24 namespace debug {
25 using ::base::trace_event::ConvertableToTraceFormat;
27 } // namespace base
29 namespace content {
31 class RendererTaskQueueSelector;
33 class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler {
34 public:
35 RendererSchedulerImpl(
36 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner);
37 ~RendererSchedulerImpl() override;
39 // RendererScheduler implementation:
40 scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override;
41 scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override;
42 scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override;
43 scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override;
44 void WillBeginFrame(const cc::BeginFrameArgs& args) override;
45 void DidCommitFrameToCompositor() override;
46 void DidReceiveInputEventOnCompositorThread(
47 blink::WebInputEvent::Type type) override;
48 void DidAnimateForInputOnCompositorThread() override;
49 bool ShouldYieldForHighPriorityWork() override;
50 void Shutdown() override;
52 void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source);
54 private:
55 friend class RendererSchedulerImplTest;
57 // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum.
58 enum QueueId {
59 DEFAULT_TASK_QUEUE,
60 COMPOSITOR_TASK_QUEUE,
61 IDLE_TASK_QUEUE,
62 CONTROL_TASK_QUEUE,
63 LOADING_TASK_QUEUE,
64 // Must be the last entry.
65 TASK_QUEUE_COUNT,
68 enum Policy {
69 NORMAL_PRIORITY_POLICY,
70 COMPOSITOR_PRIORITY_POLICY,
73 class PollableNeedsUpdateFlag {
74 public:
75 PollableNeedsUpdateFlag(base::Lock* write_lock);
76 ~PollableNeedsUpdateFlag();
78 // Set the flag. May only be called if |write_lock| is held.
79 void SetLocked(bool value);
81 // Returns true iff the flag was set to true.
82 bool IsSet() const;
84 private:
85 base::subtle::Atomic32 flag_;
86 base::Lock* write_lock_; // Not owned.
87 base::ThreadChecker thread_checker_;
89 DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag);
92 // Returns the serialized scheduler state for tracing.
93 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValueLocked(
94 base::TimeTicks optional_now) const;
95 static const char* TaskQueueIdToString(QueueId queue_id);
96 static const char* PolicyToString(Policy policy);
98 // The time we should stay in CompositorPriority mode for after a touch event.
99 static const int kCompositorPriorityAfterTouchMillis = 100;
101 // IdleTaskDeadlineSupplier Implementation:
102 void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const;
104 // Returns the current scheduler policy. Must be called from the main thread.
105 Policy SchedulerPolicy() const;
107 // Posts a call to UpdatePolicy on the control runner to be run after |delay|
108 void PostUpdatePolicyOnControlRunner(base::TimeDelta delay);
110 // Update the policy if a new signal has arrived. Must be called from the main
111 // thread.
112 void MaybeUpdatePolicy();
114 // Updates the scheduler policy. Must be called from the main thread.
115 void UpdatePolicy();
117 // An input event of some sort happened, the policy may need updating.
118 void UpdateForInputEvent();
120 // Start and end an idle period.
121 void StartIdlePeriod();
122 void EndIdlePeriod();
124 base::TimeTicks Now() const;
126 base::ThreadChecker main_thread_checker_;
127 scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_;
128 scoped_ptr<TaskQueueManager> task_queue_manager_;
129 scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_;
130 scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_;
131 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
132 scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_;
133 scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
135 base::Closure update_policy_closure_;
136 CancelableClosureHolder end_idle_period_closure_;
138 // Don't access current_policy_ directly, instead use SchedulerPolicy().
139 Policy current_policy_;
141 base::TimeTicks estimated_next_frame_begin_;
143 // The incoming_signals_lock_ mutex protects access to last_input_time_
144 // and write access to policy_may_need_update_.
145 base::Lock incoming_signals_lock_;
146 base::TimeTicks last_input_time_;
147 PollableNeedsUpdateFlag policy_may_need_update_;
149 scoped_refptr<cc::TestNowSource> time_source_;
151 base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_;
152 base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_;
154 DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl);
157 } // namespace content
159 #endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_