| OLD | NEW |
| 1 /* | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions | |
| 6 * are met: | |
| 7 * 1. Redistributions of source code must retain the above copyright | |
| 8 * notice, this list of conditions and the following disclaimer. | |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer in the | |
| 11 * documentation and/or other materials provided with the distribution. | |
| 12 * | |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 24 */ | |
| 25 | 4 |
| 26 #include "sky/engine/config.h" | 5 #include "sky/engine/config.h" |
| 27 #include "sky/engine/platform/SharedTimer.h" | 6 #include "sky/engine/platform/SharedTimer.h" |
| 28 | 7 |
| 29 #include "sky/engine/public/platform/Platform.h" | 8 #include "base/time/time.h" |
| 9 #include "base/timer/timer.h" |
| 10 #include "sky/engine/wtf/CurrentTime.h" |
| 30 | 11 |
| 31 namespace blink { | 12 namespace blink { |
| 13 namespace { |
| 14 |
| 15 class SharedTimerImpl { |
| 16 WTF_MAKE_NONCOPYABLE(SharedTimerImpl); |
| 17 public: |
| 18 SharedTimerImpl() |
| 19 : m_sharedTimerFunction(nullptr) |
| 20 , m_sharedTimerFireTime(0) |
| 21 { |
| 22 } |
| 23 |
| 24 static SharedTimerImpl& instance() |
| 25 { |
| 26 DEFINE_STATIC_LOCAL(SharedTimerImpl, instance, ()); |
| 27 return instance; |
| 28 } |
| 29 |
| 30 void setSharedTimerFiredFunction(void (*function)()) |
| 31 { |
| 32 m_sharedTimerFunction = function; |
| 33 } |
| 34 |
| 35 void setSharedTimerFireInterval(double intervalSeconds) |
| 36 { |
| 37 double now = monotonicallyIncreasingTime(); |
| 38 m_sharedTimerFireTime = intervalSeconds + now; |
| 39 |
| 40 // By converting between double and int64 representation, we run the ris
k |
| 41 // of losing precision due to rounding errors. Performing computations i
n |
| 42 // microseconds reduces this risk somewhat. But there still is the poten
tial |
| 43 // of us computing a fire time for the timer that is shorter than what w
e |
| 44 // need. |
| 45 // As the event loop will check event deadlines prior to actually firing |
| 46 // them, there is a risk of needlessly rescheduling events and of |
| 47 // needlessly looping if sleep times are too short even by small amounts
. |
| 48 // This results in measurable performance degradation unless we use ceil
() to |
| 49 // always round up the sleep times. |
| 50 int64 interval = static_cast<int64>(ceil(intervalSeconds * base::Time::k
MillisecondsPerSecond) |
| 51 * base::Time::kMicrosecondsPerMillisecond); |
| 52 |
| 53 if (interval < 0) |
| 54 interval = 0; |
| 55 |
| 56 m_sharedTimer.Stop(); |
| 57 m_sharedTimer.Start(FROM_HERE, |
| 58 base::TimeDelta::FromMicroseconds(interval), this, &SharedTimerImpl:
:timerFired); |
| 59 } |
| 60 |
| 61 void stopSharedTimer() |
| 62 { |
| 63 m_sharedTimer.Stop(); |
| 64 } |
| 65 |
| 66 private: |
| 67 void timerFired() |
| 68 { |
| 69 if (m_sharedTimerFunction) |
| 70 m_sharedTimerFunction(); |
| 71 } |
| 72 |
| 73 base::OneShotTimer<SharedTimerImpl> m_sharedTimer; |
| 74 void (*m_sharedTimerFunction)(); |
| 75 double m_sharedTimerFireTime; |
| 76 }; |
| 77 |
| 78 } |
| 32 | 79 |
| 33 void setSharedTimerFiredFunction(void (*f)()) | 80 void setSharedTimerFiredFunction(void (*f)()) |
| 34 { | 81 { |
| 35 blink::Platform::current()->setSharedTimerFiredFunction(f); | 82 SharedTimerImpl::instance().setSharedTimerFiredFunction(f); |
| 36 } | 83 } |
| 37 | 84 |
| 38 void setSharedTimerFireInterval(double interval) | 85 void setSharedTimerFireInterval(double interval) |
| 39 { | 86 { |
| 40 blink::Platform::current()->setSharedTimerFireInterval(interval); | 87 SharedTimerImpl::instance().setSharedTimerFireInterval(interval); |
| 41 } | 88 } |
| 42 | 89 |
| 43 void stopSharedTimer() | 90 void stopSharedTimer() |
| 44 { | 91 { |
| 45 blink::Platform::current()->stopSharedTimer(); | 92 SharedTimerImpl::instance().stopSharedTimer(); |
| 46 } | 93 } |
| 47 | 94 |
| 48 } // namespace blink | 95 } // namespace blink |
| OLD | NEW |