| Index: sky/engine/platform/SharedTimer.cpp
|
| diff --git a/sky/engine/platform/SharedTimer.cpp b/sky/engine/platform/SharedTimer.cpp
|
| index 84fb7e1b90447324850d963249a1a0fcaf160286..90312a1811d00066e2d11de1b6ecdaffa7fb9fe6 100644
|
| --- a/sky/engine/platform/SharedTimer.cpp
|
| +++ b/sky/engine/platform/SharedTimer.cpp
|
| @@ -1,48 +1,95 @@
|
| -/*
|
| - * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
|
| - *
|
| - * Redistribution and use in source and binary forms, with or without
|
| - * modification, are permitted provided that the following conditions
|
| - * are met:
|
| - * 1. Redistributions of source code must retain the above copyright
|
| - * notice, this list of conditions and the following disclaimer.
|
| - * 2. Redistributions in binary form must reproduce the above copyright
|
| - * notice, this list of conditions and the following disclaimer in the
|
| - * documentation and/or other materials provided with the distribution.
|
| - *
|
| - * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
| - * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
| - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
| - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
| - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
| - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
| - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| - */
|
| +// Copyright 2015 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 "sky/engine/config.h"
|
| #include "sky/engine/platform/SharedTimer.h"
|
|
|
| -#include "sky/engine/public/platform/Platform.h"
|
| +#include "base/time/time.h"
|
| +#include "base/timer/timer.h"
|
| +#include "sky/engine/wtf/CurrentTime.h"
|
|
|
| namespace blink {
|
| +namespace {
|
| +
|
| +class SharedTimerImpl {
|
| + WTF_MAKE_NONCOPYABLE(SharedTimerImpl);
|
| +public:
|
| + SharedTimerImpl()
|
| + : m_sharedTimerFunction(nullptr)
|
| + , m_sharedTimerFireTime(0)
|
| + {
|
| + }
|
| +
|
| + static SharedTimerImpl& instance()
|
| + {
|
| + DEFINE_STATIC_LOCAL(SharedTimerImpl, instance, ());
|
| + return instance;
|
| + }
|
| +
|
| + void setSharedTimerFiredFunction(void (*function)())
|
| + {
|
| + m_sharedTimerFunction = function;
|
| + }
|
| +
|
| + void setSharedTimerFireInterval(double intervalSeconds)
|
| + {
|
| + double now = monotonicallyIncreasingTime();
|
| + m_sharedTimerFireTime = intervalSeconds + now;
|
| +
|
| + // By converting between double and int64 representation, we run the risk
|
| + // of losing precision due to rounding errors. Performing computations in
|
| + // microseconds reduces this risk somewhat. But there still is the potential
|
| + // of us computing a fire time for the timer that is shorter than what we
|
| + // need.
|
| + // As the event loop will check event deadlines prior to actually firing
|
| + // them, there is a risk of needlessly rescheduling events and of
|
| + // needlessly looping if sleep times are too short even by small amounts.
|
| + // This results in measurable performance degradation unless we use ceil() to
|
| + // always round up the sleep times.
|
| + int64 interval = static_cast<int64>(ceil(intervalSeconds * base::Time::kMillisecondsPerSecond)
|
| + * base::Time::kMicrosecondsPerMillisecond);
|
| +
|
| + if (interval < 0)
|
| + interval = 0;
|
| +
|
| + m_sharedTimer.Stop();
|
| + m_sharedTimer.Start(FROM_HERE,
|
| + base::TimeDelta::FromMicroseconds(interval), this, &SharedTimerImpl::timerFired);
|
| + }
|
| +
|
| + void stopSharedTimer()
|
| + {
|
| + m_sharedTimer.Stop();
|
| + }
|
| +
|
| +private:
|
| + void timerFired()
|
| + {
|
| + if (m_sharedTimerFunction)
|
| + m_sharedTimerFunction();
|
| + }
|
| +
|
| + base::OneShotTimer<SharedTimerImpl> m_sharedTimer;
|
| + void (*m_sharedTimerFunction)();
|
| + double m_sharedTimerFireTime;
|
| +};
|
| +
|
| +}
|
|
|
| void setSharedTimerFiredFunction(void (*f)())
|
| {
|
| - blink::Platform::current()->setSharedTimerFiredFunction(f);
|
| + SharedTimerImpl::instance().setSharedTimerFiredFunction(f);
|
| }
|
|
|
| void setSharedTimerFireInterval(double interval)
|
| {
|
| - blink::Platform::current()->setSharedTimerFireInterval(interval);
|
| + SharedTimerImpl::instance().setSharedTimerFireInterval(interval);
|
| }
|
|
|
| void stopSharedTimer()
|
| {
|
| - blink::Platform::current()->stopSharedTimer();
|
| + SharedTimerImpl::instance().stopSharedTimer();
|
| }
|
|
|
| } // namespace blink
|
|
|