Chromium Code Reviews| Index: content/child/scheduler/scheduler_helper.h | 
| diff --git a/content/renderer/scheduler/renderer_scheduler_impl.h b/content/child/scheduler/scheduler_helper.h | 
| similarity index 30% | 
| copy from content/renderer/scheduler/renderer_scheduler_impl.h | 
| copy to content/child/scheduler/scheduler_helper.h | 
| index 7e3ec6c7dfb9936e4c1f6a6de100c232caa04313..8c63aad429c77b1eb46474a31a9d902f232751c3 100644 | 
| --- a/content/renderer/scheduler/renderer_scheduler_impl.h | 
| +++ b/content/child/scheduler/scheduler_helper.h | 
| @@ -1,68 +1,91 @@ | 
| -// Copyright 2014 The Chromium Authors. All rights reserved. | 
| +// Copyright 2015 The Chromium Authors. All rights reserved. | 
| // Use of this source code is governed by a BSD-style license that can be | 
| // found in the LICENSE file. | 
| -#ifndef CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | 
| -#define CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | 
| +#ifndef CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_ | 
| +#define CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_ | 
| -#include "base/atomicops.h" | 
| -#include "base/synchronization/lock.h" | 
| -#include "base/threading/thread_checker.h" | 
| #include "cc/test/test_now_source.h" | 
| -#include "content/renderer/scheduler/cancelable_closure_holder.h" | 
| -#include "content/renderer/scheduler/deadline_task_runner.h" | 
| -#include "content/renderer/scheduler/renderer_scheduler.h" | 
| -#include "content/renderer/scheduler/single_thread_idle_task_runner.h" | 
| -#include "content/renderer/scheduler/task_queue_manager.h" | 
| - | 
| -namespace base { | 
| -namespace trace_event { | 
| -class ConvertableToTraceFormat; | 
| -} | 
| -} | 
| +#include "content/child/scheduler/cancelable_closure_holder.h" | 
| +#include "content/child/scheduler/single_thread_idle_task_runner.h" | 
| +#include "content/child/scheduler/task_queue_manager.h" | 
| namespace content { | 
| -class RendererTaskQueueSelector; | 
| +class PrioritizingTaskQueueSelector; | 
| class NestableSingleThreadTaskRunner; | 
| -class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { | 
| +// Common scheduler functionality for Default and Idle tasks. | 
| +class CONTENT_EXPORT SchedulerHelper { | 
| public: | 
| - RendererSchedulerImpl( | 
| - scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner); | 
| - ~RendererSchedulerImpl() override; | 
| - | 
| - // RendererScheduler implementation: | 
| - scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner() override; | 
| - scoped_refptr<base::SingleThreadTaskRunner> CompositorTaskRunner() override; | 
| - scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner() override; | 
| - scoped_refptr<base::SingleThreadTaskRunner> LoadingTaskRunner() override; | 
| - void WillBeginFrame(const cc::BeginFrameArgs& args) override; | 
| - void BeginFrameNotExpectedSoon() override; | 
| - void DidCommitFrameToCompositor() override; | 
| - void DidReceiveInputEventOnCompositorThread( | 
| - const blink::WebInputEvent& web_input_event) override; | 
| - void DidAnimateForInputOnCompositorThread() override; | 
| - bool CanExceedIdleDeadlineIfRequired() const override; | 
| - bool IsHighPriorityWorkAnticipated() override; | 
| - bool ShouldYieldForHighPriorityWork() override; | 
| - void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer) override; | 
| - void RemoveTaskObserver( | 
| - base::MessageLoop::TaskObserver* task_observer) override; | 
| - void Shutdown() override; | 
| + // Used to by scheduler implementations to customize idle behaviour. | 
| + class SchedulerHelperDelegate { | 
| + public: | 
| + SchedulerHelperDelegate(); | 
| + virtual ~SchedulerHelperDelegate(); | 
| - void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); | 
| - void SetWorkBatchSizeForTesting(size_t work_batch_size); | 
| + // If it's ok to enter a Long Idle period, return true. Otherwise return | 
| + // false and set next_long_idle_period_delay_out so we know when to try | 
| + // again. | 
| + virtual bool CanEnterLongIdlePeriod( | 
| + base::TimeTicks now, | 
| + base::TimeDelta* next_long_idle_period_delay_out) = 0; | 
| - private: | 
| - friend class RendererSchedulerImplTest; | 
| - friend class RendererSchedulerImplForTest; | 
| + private: | 
| + DISALLOW_COPY_AND_ASSIGN(SchedulerHelperDelegate); | 
| + }; | 
| - // Keep RendererSchedulerImpl::TaskQueueIdToString in sync with this enum. | 
| + // NOTE |total_task_queue_count| must be >= TASK_QUEUE_COUNT. | 
| + // Category strings must have application lifetime (statics or | 
| + // literals). They may not include " chars. | 
| + SchedulerHelper( | 
| + scoped_refptr<NestableSingleThreadTaskRunner> main_task_runner, | 
| + SchedulerHelperDelegate* scheduler_helper_delegate, | 
| + const char* tracing_category, | 
| + const char* disabled_by_default_tracing_category, | 
| + size_t total_task_queue_count); | 
| + ~SchedulerHelper(); | 
| + | 
| + // Returns the default task runner. | 
| + scoped_refptr<base::SingleThreadTaskRunner> DefaultTaskRunner(); | 
| + | 
| + // Returns the idle task runner. Tasks posted to this runner may be reordered | 
| + // relative to other task types and may be starved for an arbitrarily long | 
| + // time if no idle time is available. | 
| + scoped_refptr<SingleThreadIdleTaskRunner> IdleTaskRunner(); | 
| + | 
| + // Returns the control task runner. Tasks posted to this runner are executed | 
| + // with the highest priority. Care must be taken to avoid starvation of other | 
| + // task queues. | 
| + scoped_refptr<base::SingleThreadTaskRunner> ControlTaskRunner(); | 
| + | 
| + // Returns true if a currently running idle task could exceed its deadline | 
| + // without impacting user experience too much. This should only be used if | 
| + // there is a task which cannot be pre-empted and is likely to take longer | 
| + // than the largest expected idle task deadline. It should NOT be polled to | 
| + // check whether more work can be performed on the current idle task after | 
| + // its deadline has expired - post a new idle task for the continuation of the | 
| + // work in this case. | 
| + // Must be called from the thread this class was created on. | 
| + bool CanExceedIdleDeadlineIfRequired() const; | 
| + | 
| + // Adds or removes a task observer from the scheduler. The observer will be | 
| + // notified before and after every executed task. These functions can only be | 
| + // called on the thread this class was created on. | 
| + void AddTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 
| + void RemoveTaskObserver(base::MessageLoop::TaskObserver* task_observer); | 
| + | 
| + // Shuts down the scheduler by dropping any remaining pending work in the work | 
| + // queues. After this call any work posted to the task runners will be | 
| + // silently dropped. | 
| + void Shutdown(); | 
| + | 
| + // Returns true if Shutdown() has been called. Otherwise returns false. | 
| + bool IsShutdown() const { return !task_queue_manager_.get(); } | 
| + | 
| + // Keep SchedulerHelper::TaskQueueIdToString in sync with this enum. | 
| enum QueueId { | 
| DEFAULT_TASK_QUEUE, | 
| - COMPOSITOR_TASK_QUEUE, | 
| - LOADING_TASK_QUEUE, | 
| IDLE_TASK_QUEUE, | 
| CONTROL_TASK_QUEUE, | 
| CONTROL_TASK_AFTER_WAKEUP_QUEUE, | 
| @@ -70,22 +93,7 @@ class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { | 
| TASK_QUEUE_COUNT, | 
| }; | 
| - // Keep RendererSchedulerImpl::PolicyToString in sync with this enum. | 
| - enum class Policy { | 
| - NORMAL, | 
| - COMPOSITOR_PRIORITY, | 
| - TOUCHSTART_PRIORITY, | 
| - }; | 
| - | 
| - // Keep RendererSchedulerImpl::InputStreamStateToString in sync with this | 
| - // enum. | 
| - enum class InputStreamState { | 
| - INACTIVE, | 
| - ACTIVE, | 
| - ACTIVE_AND_AWAITING_TOUCHSTART_RESPONSE | 
| - }; | 
| - | 
| - // Keep RendererSchedulerImpl::IdlePeriodStateToString in sync with this enum. | 
| + // Keep SchedulerHelper::IdlePeriodStateToString in sync with this enum. | 
| enum class IdlePeriodState { | 
| NOT_IN_IDLE_PERIOD, | 
| IN_SHORT_IDLE_PERIOD, | 
| @@ -94,40 +102,9 @@ class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { | 
| ENDING_LONG_IDLE_PERIOD | 
| }; | 
| - class PollableNeedsUpdateFlag { | 
| - public: | 
| - PollableNeedsUpdateFlag(base::Lock* write_lock); | 
| - ~PollableNeedsUpdateFlag(); | 
| - | 
| - // Set the flag. May only be called if |write_lock| is held. | 
| - void SetWhileLocked(bool value); | 
| - | 
| - // Returns true iff the flag is set to true. | 
| - bool IsSet() const; | 
| - | 
| - private: | 
| - base::subtle::Atomic32 flag_; | 
| - base::Lock* write_lock_; // Not owned. | 
| - | 
| - DISALLOW_COPY_AND_ASSIGN(PollableNeedsUpdateFlag); | 
| - }; | 
| - | 
| - // Returns the serialized scheduler state for tracing. | 
| - scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValueLocked( | 
| - base::TimeTicks optional_now) const; | 
| static const char* TaskQueueIdToString(QueueId queue_id); | 
| - static const char* PolicyToString(Policy policy); | 
| - static const char* InputStreamStateToString(InputStreamState state); | 
| static const char* IdlePeriodStateToString(IdlePeriodState state); | 
| - static InputStreamState ComputeNewInputStreamState( | 
| - InputStreamState current_state, | 
| - blink::WebInputEvent::Type new_input_event, | 
| - blink::WebInputEvent::Type last_input_event); | 
| - | 
| - // The time we should stay in a priority-escalated mode after an input event. | 
| - static const int kPriorityEscalationAfterInputMillis = 100; | 
| - | 
| // The maximum length of an idle period. | 
| static const int kMaximumIdlePeriodMillis = 50; | 
| @@ -137,43 +114,6 @@ class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { | 
| // IdleTaskDeadlineSupplier Implementation: | 
| void CurrentIdleTaskDeadlineCallback(base::TimeTicks* deadline_out) const; | 
| - // Returns the current scheduler policy. Must be called from the main thread. | 
| - Policy SchedulerPolicy() const; | 
| - | 
| - // Schedules an immediate PolicyUpdate, if there isn't one already pending and | 
| - // sets |policy_may_need_update_|. Note |incoming_signals_lock_| must be | 
| - // locked. | 
| - void EnsureUrgentPolicyUpdatePostedOnMainThread( | 
| - const tracked_objects::Location& from_here); | 
| - | 
| - // Update the policy if a new signal has arrived. Must be called from the main | 
| - // thread. | 
| - void MaybeUpdatePolicy(); | 
| - | 
| - // Locks |incoming_signals_lock_| and updates the scheduler policy. | 
| - // Must be called from the main thread. | 
| - void UpdatePolicy(); | 
| - virtual void UpdatePolicyLocked(); | 
| - | 
| - // Returns the amount of time left in the current input escalated priority | 
| - // policy. | 
| - base::TimeDelta TimeLeftInInputEscalatedPolicy(base::TimeTicks now) const; | 
| - | 
| - // Helper for computing the new policy. |new_policy_duration| will be filled | 
| - // with the amount of time after which the policy should be updated again. If | 
| - // the duration is zero, a new policy update will not be scheduled. Must be | 
| - // called with |incoming_signals_lock_| held. | 
| - Policy ComputeNewPolicy(base::TimeTicks now, | 
| - base::TimeDelta* new_policy_duration); | 
| - | 
| - // An input event of some sort happened, the policy may need updating. | 
| - void UpdateForInputEvent(blink::WebInputEvent::Type type); | 
| - | 
| - // Called when a previously queued input event was processed. | 
| - // |begin_frame_time|, if non-zero, identifies the frame time at which the | 
| - // input was processed. | 
| - void DidProcessInputEvent(base::TimeTicks begin_frame_time); | 
| - | 
| // Returns the new idle period state for the next long idle period. Fills in | 
| // |next_long_idle_period_delay_out| with the next time we should try to | 
| // initiate the next idle period. | 
| @@ -185,55 +125,62 @@ class CONTENT_EXPORT RendererSchedulerImpl : public RendererScheduler { | 
| void InitiateLongIdlePeriod(); | 
| void InitiateLongIdlePeriodAfterWakeup(); | 
| - // Start and end an idle period. | 
| - void StartIdlePeriod(IdlePeriodState new_idle_period_state); | 
| + // Start and end an idle period. For short idle periods, a task will be posted | 
| + // to end the idle period at |estimated_end_of_idle|. | 
| + void StartIdlePeriod(IdlePeriodState new_idle_period_state, | 
| + base::TimeTicks now, | 
| + base::TimeTicks estimated_end_of_idle); | 
| 
 
rmcilroy
2015/03/30 17:53:57
nit - let's just make this end_of_idle (drop the '
 
alex clarke (OOO till 29th)
2015/03/31 10:18:34
Done.
 
 | 
| + | 
| void EndIdlePeriod(); | 
| // Returns true if |state| represents being within an idle period state. | 
| static bool IsInIdlePeriod(IdlePeriodState state); | 
| + void CheckOnValidThread() const { | 
| + DCHECK(thread_checker_.CalledOnValidThread()); | 
| + } | 
| + | 
| + // Accessor methods. | 
| base::TimeTicks Now() const; | 
| + IdlePeriodState SchedulerIdlePeriodState() const; | 
| + PrioritizingTaskQueueSelector* SchedulerTaskQueueSelector() const; | 
| + TaskQueueManager* SchedulerTaskQueueManager() const; | 
| + | 
| + // Test helpers. | 
| + void SetTimeSourceForTesting(scoped_refptr<cc::TestNowSource> time_source); | 
| + void SetWorkBatchSizeForTesting(size_t work_batch_size); | 
| + | 
| + private: | 
| + friend class SchedulerHelperTest; | 
| - base::ThreadChecker main_thread_checker_; | 
| - scoped_ptr<RendererTaskQueueSelector> renderer_task_queue_selector_; | 
| + base::ThreadChecker thread_checker_; | 
| + scoped_ptr<PrioritizingTaskQueueSelector> task_queue_selector_; | 
| scoped_ptr<TaskQueueManager> task_queue_manager_; | 
| - scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_; | 
| - scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_; | 
| - scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; | 
| - scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | 
| - scoped_refptr<base::SingleThreadTaskRunner> loading_task_runner_; | 
| - scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; | 
| - base::Closure update_policy_closure_; | 
| - DeadlineTaskRunner delayed_update_policy_runner_; | 
| CancelableClosureHolder end_idle_period_closure_; | 
| CancelableClosureHolder initiate_next_long_idle_period_closure_; | 
| CancelableClosureHolder initiate_next_long_idle_period_after_wakeup_closure_; | 
| - // Don't access current_policy_ directly, instead use SchedulerPolicy(). | 
| - Policy current_policy_; | 
| IdlePeriodState idle_period_state_; | 
| + SchedulerHelperDelegate* scheduler_helper_delegate_; // NOT OWNED | 
| - base::TimeTicks estimated_next_frame_begin_; | 
| - base::TimeTicks current_policy_expiration_time_; | 
| - | 
| - // The incoming_signals_lock_ mutex protects access to all variables in the | 
| - // (contiguous) block below. | 
| - base::Lock incoming_signals_lock_; | 
| - base::TimeTicks last_input_receipt_time_on_compositor_; | 
| - base::TimeTicks last_input_process_time_on_main_; | 
| - blink::WebInputEvent::Type last_input_type_; | 
| - InputStreamState input_stream_state_; | 
| - PollableNeedsUpdateFlag policy_may_need_update_; | 
| + scoped_refptr<base::SingleThreadTaskRunner> control_task_runner_; | 
| + scoped_refptr<base::SingleThreadTaskRunner> control_task_after_wakeup_runner_; | 
| + scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_; | 
| + scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_; | 
| + base::TimeTicks estimated_end_of_idle_; | 
| 
 
rmcilroy
2015/03/30 17:53:58
Now that this is no longer estimated let's just ca
 
alex clarke (OOO till 29th)
2015/03/31 10:18:34
Done.
 
 | 
| scoped_refptr<cc::TestNowSource> time_source_; | 
| - base::WeakPtr<RendererSchedulerImpl> weak_renderer_scheduler_ptr_; | 
| - base::WeakPtrFactory<RendererSchedulerImpl> weak_factory_; | 
| + const char* tracing_category_; | 
| + const char* disabled_by_default_tracing_category_; | 
| + | 
| + base::WeakPtr<SchedulerHelper> weak_scheduler_ptr_; | 
| + base::WeakPtrFactory<SchedulerHelper> weak_factory_; | 
| - DISALLOW_COPY_AND_ASSIGN(RendererSchedulerImpl); | 
| + DISALLOW_COPY_AND_ASSIGN(SchedulerHelper); | 
| }; | 
| } // namespace content | 
| -#endif // CONTENT_RENDERER_SCHEDULER_RENDERER_SCHEDULER_IMPL_H_ | 
| +#endif // CONTENT_CHILD_SCHEDULER_SCHEDULER_HELPER_H_ |