OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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 #include "webkit/renderer/compositor_bindings/webkit_time.h" |
| 6 |
| 7 #include <limits> |
| 8 |
| 9 #include "base/float_util.h" |
| 10 |
| 11 namespace webkit { |
| 12 |
| 13 base::TimeTicks FromWebKitTime(double monotonic_time) { |
| 14 // Outside of 2**53 the conversion to/from double isn't presevative. |
| 15 |
| 16 if (monotonic_time == 0 || base::IsNaN(monotonic_time)) |
| 17 return base::TimeTicks(); // Preserve 0 so we can tell it doesn't exist. |
| 18 |
| 19 double d_microseconds = |
| 20 monotonic_time * static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 21 |
| 22 // Check double isn't too big for an int64 |
| 23 if (d_microseconds >= static_cast<double>(std::numeric_limits<int64>::max())) |
| 24 return base::TimeTicks::FromInternalValue( |
| 25 std::numeric_limits<int64>::max()); |
| 26 if (d_microseconds <= static_cast<double>(std::numeric_limits<int64>::min())) |
| 27 return base::TimeTicks::FromInternalValue( |
| 28 std::numeric_limits<int64>::min()); |
| 29 |
| 30 int64 i_microseconds = static_cast<int64>(d_microseconds); |
| 31 int64 epoch_ticks_t0 = base::TimeTicks::UnixEpoch().ToInternalValue(); |
| 32 |
| 33 // Make sure the Epoch adjustment won't cause over/under flow. |
| 34 if (epoch_ticks_t0 > 0 && |
| 35 i_microseconds >= std::numeric_limits<int64>::max() - epoch_ticks_t0) |
| 36 return base::TimeTicks::FromInternalValue( |
| 37 std::numeric_limits<int64>::max()); |
| 38 if (epoch_ticks_t0 < 0 && |
| 39 i_microseconds <= std::numeric_limits<int64>::min() - epoch_ticks_t0) |
| 40 return base::TimeTicks::FromInternalValue( |
| 41 std::numeric_limits<int64>::min()); |
| 42 |
| 43 return base::TimeTicks::FromInternalValue(i_microseconds + epoch_ticks_t0); |
| 44 } |
| 45 |
| 46 double ToWebKitTime(base::TimeTicks monotonic_time) { |
| 47 if (monotonic_time.is_null()) |
| 48 return 0; // Preserve 0 so we can tell it doesn't exist. |
| 49 |
| 50 int64 ticks = monotonic_time.ToInternalValue(); |
| 51 if (ticks == std::numeric_limits<int64>::max()) |
| 52 return std::numeric_limits<double>::infinity(); |
| 53 if (ticks == std::numeric_limits<int64>::min()) |
| 54 return -std::numeric_limits<double>::infinity(); |
| 55 |
| 56 return (static_cast<double>(ticks - |
| 57 base::TimeTicks::UnixEpoch().ToInternalValue()) / |
| 58 static_cast<double>(base::Time::kMicrosecondsPerSecond)); |
| 59 } |
| 60 |
| 61 } // namespace webkit |
OLD | NEW |