OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_FRAME_TIME_H | 5 #ifndef UI_GFX_FRAME_TIME_H |
6 #define UI_GFX_FRAME_TIME_H | 6 #define UI_GFX_FRAME_TIME_H |
7 | 7 |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
11 namespace gfx { | 11 namespace gfx { |
12 | 12 |
13 // FrameTime::Now() should be used to get timestamps with a timebase that | 13 // FrameTime should be used for getting the current timestamp in |
14 // is consistent across the graphics stack. | 14 // the same timebase as DisplayTime and for representing things like deadlines |
| 15 // and animation start/end times, which may not occur exactly on a vsync |
| 16 // boundary. |
15 class FrameTime { | 17 class FrameTime { |
16 public: | 18 public: |
17 static base::TimeTicks Now() { | 19 FrameTime() : ticks_(0) { |
18 if (TimestampsAreHighRes()) | |
19 return base::TimeTicks::HighResNow(); | |
20 return base::TimeTicks::Now(); | |
21 } | 20 } |
22 | 21 |
23 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
24 static base::TimeTicks FromQPCValue(LONGLONG qpc_value) { | 23 static base::TimeTicks FromQPCValue(LONGLONG qpc_value) { |
25 DCHECK(TimestampsAreHighRes()); | 24 DCHECK(FrameTime::IsHighRes()); |
26 return base::TimeTicks::FromQPCValue(qpc_value); | 25 return base::TimeTicks::FromQPCValue(qpc_value); |
27 } | 26 } |
28 #endif | 27 #endif |
29 | 28 |
30 static bool TimestampsAreHighRes() { | 29 static bool IsHighRes() { |
31 return base::TimeTicks::IsHighResNowFastAndReliable(); | 30 return base::TimeTicks::IsHighResNowFastAndReliable(); |
32 } | 31 } |
| 32 |
| 33 static FrameTime Now() { |
| 34 if (IsHighRes()) |
| 35 return FrameTime(base::TimeTicks::HighResNow().ToInternalValue()); |
| 36 return FrameTime(base::TimeTicks::Now().ToInternalValue()); |
| 37 } |
| 38 |
| 39 // Returns true if this object has not been initialized. |
| 40 bool is_null() const { |
| 41 return ticks_ == 0; |
| 42 } |
| 43 |
| 44 double Unsafe_InSecondsF() const { |
| 45 return static_cast<double>(ticks_) / base::Time::kMicrosecondsPerSecond; |
| 46 } |
| 47 |
| 48 double Unsafe_InMillisecondsF() const { |
| 49 return static_cast<double>(ticks_) / |
| 50 base::Time::kMicrosecondsPerMillisecond; |
| 51 } |
| 52 |
| 53 static FrameTime Unsafe_FromSecondsF(double seconds) { |
| 54 return FrameTime(seconds * base::Time::kMicrosecondsPerSecond); |
| 55 } |
| 56 |
| 57 static FrameTime Unsafe_FromTimeTicks(base::TimeTicks time_ticks) { |
| 58 return FrameTime(time_ticks.ToInternalValue()); |
| 59 } |
| 60 |
| 61 |
| 62 FrameTime& operator=(FrameTime other) { |
| 63 ticks_ = other.ticks_; |
| 64 return *this; |
| 65 } |
| 66 |
| 67 // Compute the difference between two times. |
| 68 base::TimeDelta operator-(FrameTime other) const { |
| 69 DCHECK(!other.is_null()) << "Please do not convert time to a float."; |
| 70 return base::TimeDelta::FromInternalValue(ticks_ - other.ticks_); |
| 71 } |
| 72 |
| 73 // Modify by some time delta. |
| 74 FrameTime& operator+=(base::TimeDelta delta) { |
| 75 ticks_ += delta.ToInternalValue(); |
| 76 return *this; |
| 77 } |
| 78 FrameTime& operator-=(base::TimeDelta delta) { |
| 79 ticks_ -= delta.ToInternalValue(); |
| 80 return *this; |
| 81 } |
| 82 |
| 83 // Return a new FrameTime modified by some delta. |
| 84 FrameTime operator+(base::TimeDelta delta) const { |
| 85 return FrameTime(ticks_ + delta.ToInternalValue()); |
| 86 } |
| 87 FrameTime operator-(base::TimeDelta delta) const { |
| 88 return FrameTime(ticks_ - delta.ToInternalValue()); |
| 89 } |
| 90 |
| 91 // Comparison operators |
| 92 bool operator==(FrameTime other) const { |
| 93 return ticks_ == other.ticks_; |
| 94 } |
| 95 bool operator!=(FrameTime other) const { |
| 96 return ticks_ != other.ticks_; |
| 97 } |
| 98 bool operator<(FrameTime other) const { |
| 99 return ticks_ < other.ticks_; |
| 100 } |
| 101 bool operator<=(FrameTime other) const { |
| 102 return ticks_ <= other.ticks_; |
| 103 } |
| 104 bool operator>(FrameTime other) const { |
| 105 return ticks_ > other.ticks_; |
| 106 } |
| 107 bool operator>=(FrameTime other) const { |
| 108 return ticks_ >= other.ticks_; |
| 109 } |
| 110 |
| 111 protected: |
| 112 // Tick count in microseconds. |
| 113 int64 ticks_; |
| 114 |
| 115 // Please use Now() to create a new object. This is for internal use. |
| 116 // Ticks is in microseconds. |
| 117 explicit FrameTime(int64 ticks) : ticks_(ticks) { |
| 118 } |
| 119 |
| 120 friend class DisplayTime; |
| 121 }; |
| 122 |
| 123 // DisplayTime should be used to represent a timestamp that should be aligned |
| 124 // with the display vsync phase. |
| 125 class DisplayTime : public FrameTime { |
| 126 public: |
| 127 DisplayTime() : FrameTime(0) { |
| 128 } |
| 129 |
| 130 static DisplayTime Unsafe_CreateFromTimeTicks(base::TimeTicks time_ticks) { |
| 131 return DisplayTime(time_ticks.ToInternalValue()); |
| 132 } |
| 133 |
| 134 static DisplayTime Unsafe_CreateFromFrameTime(FrameTime fractional) { |
| 135 return DisplayTime(fractional.ticks_); |
| 136 } |
| 137 |
| 138 protected: |
| 139 explicit DisplayTime(int64 ticks) |
| 140 : FrameTime(ticks) {} |
| 141 |
| 142 private: |
| 143 // Hide non const methods of FrameTime |
| 144 DisplayTime& operator+=(base::TimeDelta delta) { |
| 145 NOTREACHED(); |
| 146 return *this; |
| 147 } |
| 148 DisplayTime& operator-=(base::TimeDelta delta) { |
| 149 NOTREACHED(); |
| 150 return *this; |
| 151 } |
33 }; | 152 }; |
34 | 153 |
35 } | 154 } |
36 | 155 |
37 #endif // UI_GFX_FRAME_TIME_H | 156 #endif // UI_GFX_FRAME_TIME_H |
OLD | NEW |