Index: webkit/renderer/compositor_bindings/webkit_time.cc |
diff --git a/webkit/renderer/compositor_bindings/webkit_time.cc b/webkit/renderer/compositor_bindings/webkit_time.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..52e802aa63980adb9c0bced62b979f00e2f22403 |
--- /dev/null |
+++ b/webkit/renderer/compositor_bindings/webkit_time.cc |
@@ -0,0 +1,61 @@ |
+// Copyright 2012 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. |
+ |
+#include "webkit/renderer/compositor_bindings/webkit_time.h" |
+ |
+#include <limits> |
+ |
+#include "base/float_util.h" |
+ |
+namespace webkit { |
+ |
+base::TimeTicks FromWebKitTime(double monotonic_time) { |
+ // Outside of 2**53 the conversion to/from double isn't presevative. |
+ |
+ if (monotonic_time == 0 || base::IsNaN(monotonic_time)) |
+ return base::TimeTicks(); // Preserve 0 so we can tell it doesn't exist. |
+ |
+ double d_microseconds = |
+ monotonic_time * static_cast<double>(base::Time::kMicrosecondsPerSecond); |
+ |
+ // Check double isn't too big for an int64 |
+ if (d_microseconds >= static_cast<double>(std::numeric_limits<int64>::max())) |
+ return base::TimeTicks::FromInternalValue( |
+ std::numeric_limits<int64>::max()); |
+ if (d_microseconds <= static_cast<double>(std::numeric_limits<int64>::min())) |
+ return base::TimeTicks::FromInternalValue( |
+ std::numeric_limits<int64>::min()); |
+ |
+ int64 i_microseconds = static_cast<int64>(d_microseconds); |
+ int64 epoch_ticks_t0 = base::TimeTicks::UnixEpoch().ToInternalValue(); |
+ |
+ // Make sure the Epoch adjustment won't cause over/under flow. |
+ if (epoch_ticks_t0 > 0 && |
+ i_microseconds >= std::numeric_limits<int64>::max() - epoch_ticks_t0) |
+ return base::TimeTicks::FromInternalValue( |
+ std::numeric_limits<int64>::max()); |
+ if (epoch_ticks_t0 < 0 && |
+ i_microseconds <= std::numeric_limits<int64>::min() - epoch_ticks_t0) |
+ return base::TimeTicks::FromInternalValue( |
+ std::numeric_limits<int64>::min()); |
+ |
+ return base::TimeTicks::FromInternalValue(i_microseconds + epoch_ticks_t0); |
+} |
+ |
+double ToWebKitTime(base::TimeTicks monotonic_time) { |
+ if (monotonic_time.is_null()) |
+ return 0; // Preserve 0 so we can tell it doesn't exist. |
+ |
+ int64 ticks = monotonic_time.ToInternalValue(); |
+ if (ticks == std::numeric_limits<int64>::max()) |
+ return std::numeric_limits<double>::infinity(); |
+ if (ticks == std::numeric_limits<int64>::min()) |
+ return -std::numeric_limits<double>::infinity(); |
+ |
+ return (static_cast<double>(ticks - |
+ base::TimeTicks::UnixEpoch().ToInternalValue()) / |
+ static_cast<double>(base::Time::kMicrosecondsPerSecond)); |
+} |
+ |
+} // namespace webkit |