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

Unified Diff: webkit/renderer/compositor_bindings/webkit_time.cc

Issue 192433002: Creating utility for converting TimeTicks to WebKit double and use it in the Chrome compositor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 9 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
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

Powered by Google App Engine
This is Rietveld 408576698