1// Copyright 2011 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. 4 5#ifndef CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ 6#define CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ 7 8#include "base/memory/weak_ptr.h" 9#include "cc/base/cc_export.h" 10#include "cc/scheduler/time_source.h" 11 12namespace base { class SingleThreadTaskRunner; } 13 14namespace cc { 15 16// This timer implements a time source that achieves the specified interval 17// in face of millisecond-precision delayed callbacks and random queueing 18// delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase. 19class CC_EXPORT DelayBasedTimeSource : public TimeSource { 20 public: 21 static scoped_refptr<DelayBasedTimeSource> Create( 22 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner); 23 24 virtual void SetClient(TimeSourceClient* client) OVERRIDE; 25 26 // TimeSource implementation 27 virtual void SetTimebaseAndInterval(base::TimeTicks timebase, 28 base::TimeDelta interval) OVERRIDE; 29 30 virtual base::TimeTicks SetActive(bool active) OVERRIDE; 31 virtual bool Active() const OVERRIDE; 32 33 // Get the last and next tick times. nextTimeTime() returns null when 34 // inactive. 35 virtual base::TimeTicks LastTickTime() OVERRIDE; 36 virtual base::TimeTicks NextTickTime() OVERRIDE; 37 38 // Virtual for testing. 39 virtual base::TimeTicks Now() const; 40 41 protected: 42 DelayBasedTimeSource(base::TimeDelta interval, 43 base::SingleThreadTaskRunner* task_runner); 44 virtual ~DelayBasedTimeSource(); 45 46 base::TimeTicks NextTickTarget(base::TimeTicks now); 47 void PostNextTickTask(base::TimeTicks now); 48 void OnTimerFired(); 49 50 struct Parameters { 51 Parameters(base::TimeDelta interval, base::TimeTicks tick_target) 52 : interval(interval), tick_target(tick_target) {} 53 base::TimeDelta interval; 54 base::TimeTicks tick_target; 55 }; 56 57 TimeSourceClient* client_; 58 base::TimeTicks last_tick_time_; 59 60 // current_parameters_ should only be written by PostNextTickTask. 61 // next_parameters_ will take effect on the next call to PostNextTickTask. 62 // Maintaining a pending set of parameters allows NextTickTime() to always 63 // reflect the actual time we expect OnTimerFired to be called. 64 Parameters current_parameters_; 65 Parameters next_parameters_; 66 67 bool active_; 68 69 base::SingleThreadTaskRunner* task_runner_; 70 base::WeakPtrFactory<DelayBasedTimeSource> weak_factory_; 71 72 private: 73 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource); 74}; 75 76// DelayBasedTimeSource uses base::TimeTicks::HighResNow as its timebase. 77class DelayBasedTimeSourceHighRes : public DelayBasedTimeSource { 78 public: 79 static scoped_refptr<DelayBasedTimeSourceHighRes> Create( 80 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner); 81 82 virtual base::TimeTicks Now() const OVERRIDE; 83 84 protected: 85 DelayBasedTimeSourceHighRes(base::TimeDelta interval, 86 base::SingleThreadTaskRunner* task_runner); 87 virtual ~DelayBasedTimeSourceHighRes(); 88 89 private: 90 DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSourceHighRes); 91}; 92 93} // namespace cc 94 95#endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ 96