Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Unified Diff: ui/gfx/frame_time.h

Issue 26880010: gfx: Add FrameTime and DisplayTime classes (Closed) Base URL: http://git.chromium.org/chromium/src.git@checkHighResNow4
Patch Set: WIP Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/animation/throb_animation.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/frame_time.h
diff --git a/ui/gfx/frame_time.h b/ui/gfx/frame_time.h
index 8d143eed2c5f51b734917041e97411724f980bc2..2ffa72ee0d026563602985a29cb70161aadb1118 100644
--- a/ui/gfx/frame_time.h
+++ b/ui/gfx/frame_time.h
@@ -10,26 +10,145 @@
namespace gfx {
-// FrameTime::Now() should be used to get timestamps with a timebase that
-// is consistent across the graphics stack.
+// FrameTime should be used for getting the current timestamp in
+// the same timebase as DisplayTime and for representing things like deadlines
+// and animation start/end times, which may not occur exactly on a vsync
+// boundary.
class FrameTime {
public:
- static base::TimeTicks Now() {
- if (TimestampsAreHighRes())
- return base::TimeTicks::HighResNow();
- return base::TimeTicks::Now();
+ FrameTime() : ticks_(0) {
}
#if defined(OS_WIN)
static base::TimeTicks FromQPCValue(LONGLONG qpc_value) {
- DCHECK(TimestampsAreHighRes());
+ DCHECK(FrameTime::IsHighRes());
return base::TimeTicks::FromQPCValue(qpc_value);
}
#endif
- static bool TimestampsAreHighRes() {
+ static bool IsHighRes() {
return base::TimeTicks::IsHighResNowFastAndReliable();
}
+
+ static FrameTime Now() {
+ if (IsHighRes())
+ return FrameTime(base::TimeTicks::HighResNow().ToInternalValue());
+ return FrameTime(base::TimeTicks::Now().ToInternalValue());
+ }
+
+ // Returns true if this object has not been initialized.
+ bool is_null() const {
+ return ticks_ == 0;
+ }
+
+ double Unsafe_InSecondsF() const {
+ return static_cast<double>(ticks_) / base::Time::kMicrosecondsPerSecond;
+ }
+
+ double Unsafe_InMillisecondsF() const {
+ return static_cast<double>(ticks_) /
+ base::Time::kMicrosecondsPerMillisecond;
+ }
+
+ static FrameTime Unsafe_FromSecondsF(double seconds) {
+ return FrameTime(seconds * base::Time::kMicrosecondsPerSecond);
+ }
+
+ static FrameTime Unsafe_FromTimeTicks(base::TimeTicks time_ticks) {
+ return FrameTime(time_ticks.ToInternalValue());
+ }
+
+
+ FrameTime& operator=(FrameTime other) {
+ ticks_ = other.ticks_;
+ return *this;
+ }
+
+ // Compute the difference between two times.
+ base::TimeDelta operator-(FrameTime other) const {
+ DCHECK(!other.is_null()) << "Please do not convert time to a float.";
+ return base::TimeDelta::FromInternalValue(ticks_ - other.ticks_);
+ }
+
+ // Modify by some time delta.
+ FrameTime& operator+=(base::TimeDelta delta) {
+ ticks_ += delta.ToInternalValue();
+ return *this;
+ }
+ FrameTime& operator-=(base::TimeDelta delta) {
+ ticks_ -= delta.ToInternalValue();
+ return *this;
+ }
+
+ // Return a new FrameTime modified by some delta.
+ FrameTime operator+(base::TimeDelta delta) const {
+ return FrameTime(ticks_ + delta.ToInternalValue());
+ }
+ FrameTime operator-(base::TimeDelta delta) const {
+ return FrameTime(ticks_ - delta.ToInternalValue());
+ }
+
+ // Comparison operators
+ bool operator==(FrameTime other) const {
+ return ticks_ == other.ticks_;
+ }
+ bool operator!=(FrameTime other) const {
+ return ticks_ != other.ticks_;
+ }
+ bool operator<(FrameTime other) const {
+ return ticks_ < other.ticks_;
+ }
+ bool operator<=(FrameTime other) const {
+ return ticks_ <= other.ticks_;
+ }
+ bool operator>(FrameTime other) const {
+ return ticks_ > other.ticks_;
+ }
+ bool operator>=(FrameTime other) const {
+ return ticks_ >= other.ticks_;
+ }
+
+ protected:
+ // Tick count in microseconds.
+ int64 ticks_;
+
+ // Please use Now() to create a new object. This is for internal use.
+ // Ticks is in microseconds.
+ explicit FrameTime(int64 ticks) : ticks_(ticks) {
+ }
+
+ friend class DisplayTime;
+};
+
+// DisplayTime should be used to represent a timestamp that should be aligned
+// with the display vsync phase.
+class DisplayTime : public FrameTime {
+ public:
+ DisplayTime() : FrameTime(0) {
+ }
+
+ static DisplayTime Unsafe_CreateFromTimeTicks(base::TimeTicks time_ticks) {
+ return DisplayTime(time_ticks.ToInternalValue());
+ }
+
+ static DisplayTime Unsafe_CreateFromFrameTime(FrameTime fractional) {
+ return DisplayTime(fractional.ticks_);
+ }
+
+ protected:
+ explicit DisplayTime(int64 ticks)
+ : FrameTime(ticks) {}
+
+ private:
+ // Hide non const methods of FrameTime
+ DisplayTime& operator+=(base::TimeDelta delta) {
+ NOTREACHED();
+ return *this;
+ }
+ DisplayTime& operator-=(base::TimeDelta delta) {
+ NOTREACHED();
+ return *this;
+ }
};
}
« no previous file with comments | « ui/gfx/animation/throb_animation.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698