James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 <string> |
| 9 | |
| 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "base/values.h" |
| 12 | #include "cc/base/cc_export.h" |
| 13 | |
| 14 | namespace base { |
| 15 | namespace debug { |
| 16 | class TracedValue; |
| 17 | } |
| 18 | class SingleThreadTaskRunner; |
| 19 | } |
| 20 | |
| 21 | namespace cc { |
| 22 | |
| 23 | class CC_EXPORT TimeSourceClient { |
| 24 | public: |
| 25 | virtual void OnTimerTick() = 0; |
| 26 | |
| 27 | protected: |
| 28 | virtual ~TimeSourceClient() {} |
| 29 | }; |
| 30 | |
| 31 | // This timer implements a time source that achieves the specified interval |
| 32 | // in face of millisecond-precision delayed callbacks and random queueing |
| 33 | // delays. DelayBasedTimeSource uses base::TimeTicks::Now as its timebase. |
| 34 | class CC_EXPORT DelayBasedTimeSource |
| 35 | : public base::RefCounted<DelayBasedTimeSource> { |
| 36 | public: |
| 37 | static scoped_refptr<DelayBasedTimeSource> Create( |
| 38 | base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner); |
| 39 | |
| 40 | virtual void SetClient(TimeSourceClient* client); |
| 41 | |
| 42 | // TimeSource implementation |
| 43 | virtual void SetTimebaseAndInterval(base::TimeTicks timebase, |
| 44 | base::TimeDelta interval); |
| 45 | base::TimeDelta Interval() const { return next_parameters_.interval; } |
| 46 | |
| 47 | virtual base::TimeTicks SetActive(bool active); |
| 48 | virtual bool Active() const; |
| 49 | |
| 50 | // Get the last and next tick times. NextTickTime() returns null when |
| 51 | // inactive. |
| 52 | virtual base::TimeTicks LastTickTime() const; |
| 53 | virtual base::TimeTicks NextTickTime() const; |
| 54 | |
| 55 | // Virtual for testing. |
| 56 | virtual base::TimeTicks Now() const; |
| 57 | |
| 58 | virtual void AsValueInto(base::debug::TracedValue* dict) const; |
| 59 | |
| 60 | protected: |
| 61 | DelayBasedTimeSource(base::TimeDelta interval, |
| 62 | base::SingleThreadTaskRunner* task_runner); |
| 63 | virtual ~DelayBasedTimeSource(); |
| 64 | |
| 65 | virtual std::string TypeString() const; |
| 66 | |
| 67 | base::TimeTicks NextTickTarget(base::TimeTicks now); |
| 68 | void PostNextTickTask(base::TimeTicks now); |
| 69 | void OnTimerFired(); |
| 70 | |
| 71 | struct Parameters { |
| 72 | Parameters(base::TimeDelta interval, base::TimeTicks tick_target) |
| 73 | : interval(interval), tick_target(tick_target) {} |
| 74 | base::TimeDelta interval; |
| 75 | base::TimeTicks tick_target; |
| 76 | }; |
| 77 | |
| 78 | TimeSourceClient* client_; |
| 79 | base::TimeTicks last_tick_time_; |
| 80 | |
| 81 | // current_parameters_ should only be written by PostNextTickTask. |
| 82 | // next_parameters_ will take effect on the next call to PostNextTickTask. |
| 83 | // Maintaining a pending set of parameters allows NextTickTime() to always |
| 84 | // reflect the actual time we expect OnTimerFired to be called. |
| 85 | Parameters current_parameters_; |
| 86 | Parameters next_parameters_; |
| 87 | |
| 88 | bool active_; |
| 89 | |
| 90 | base::SingleThreadTaskRunner* task_runner_; |
| 91 | base::WeakPtrFactory<DelayBasedTimeSource> weak_factory_; |
| 92 | |
| 93 | private: |
| 94 | friend class base::RefCounted<DelayBasedTimeSource>; |
| 95 | DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSource); |
| 96 | }; |
| 97 | |
Elliot Glaysher | eae4929 | 2015-01-28 10:47:32 -0800 | [diff] [blame] | 98 | // DelayBasedTimeSource that once used base::TimeTicks::HighResNow as its time |
| 99 | // source, but is now a no-op. |
| 100 | // TODO(brianderson): Remove along with gfx::/FrameTime.http://crbug.com/447329 |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 101 | class DelayBasedTimeSourceHighRes : public DelayBasedTimeSource { |
| 102 | public: |
| 103 | static scoped_refptr<DelayBasedTimeSourceHighRes> Create( |
| 104 | base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner); |
| 105 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 106 | base::TimeTicks Now() const override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | DelayBasedTimeSourceHighRes(base::TimeDelta interval, |
| 110 | base::SingleThreadTaskRunner* task_runner); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 111 | ~DelayBasedTimeSourceHighRes() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 112 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 113 | std::string TypeString() const override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | DISALLOW_COPY_AND_ASSIGN(DelayBasedTimeSourceHighRes); |
| 117 | }; |
| 118 | |
| 119 | } // namespace cc |
| 120 | |
| 121 | #endif // CC_SCHEDULER_DELAY_BASED_TIME_SOURCE_H_ |