ScriptedAnimationController.cpp [plain text]
#include "config.h"
#include "ScriptedAnimationController.h"
#if ENABLE(REQUEST_ANIMATION_FRAME)
#include "DisplayRefreshMonitor.h"
#include "DisplayRefreshMonitorManager.h"
#include "Document.h"
#include "DocumentLoader.h"
#include "FrameView.h"
#include "InspectorInstrumentation.h"
#include "RequestAnimationFrameCallback.h"
#include "Settings.h"
#include <wtf/Ref.h>
#if USE(REQUEST_ANIMATION_FRAME_TIMER)
#include <algorithm>
#include <wtf/CurrentTime.h>
#define MinimumAnimationInterval 0.015
#define MinimumThrottledAnimationInterval 10
#endif
namespace WebCore {
ScriptedAnimationController::ScriptedAnimationController(Document* document, PlatformDisplayID displayID)
: m_document(document)
#if USE(REQUEST_ANIMATION_FRAME_TIMER)
, m_animationTimer(this, &ScriptedAnimationController::animationTimerFired)
#endif
{
windowScreenDidChange(displayID);
}
ScriptedAnimationController::~ScriptedAnimationController()
{
}
void ScriptedAnimationController::suspend()
{
++m_suspendCount;
}
void ScriptedAnimationController::resume()
{
if (m_suspendCount > 0)
--m_suspendCount;
if (!m_suspendCount && m_callbacks.size())
scheduleAnimation();
}
void ScriptedAnimationController::setThrottled(bool isThrottled)
{
#if USE(REQUEST_ANIMATION_FRAME_TIMER) && USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
if (m_isThrottled == isThrottled)
return;
m_isThrottled = isThrottled;
if (m_animationTimer.isActive()) {
m_animationTimer.stop();
scheduleAnimation();
}
#else
UNUSED_PARAM(isThrottled);
#endif
}
ScriptedAnimationController::CallbackId ScriptedAnimationController::registerCallback(PassRefPtr<RequestAnimationFrameCallback> callback)
{
ScriptedAnimationController::CallbackId id = ++m_nextCallbackId;
callback->m_firedOrCancelled = false;
callback->m_id = id;
m_callbacks.append(callback);
InspectorInstrumentation::didRequestAnimationFrame(m_document, id);
if (!m_suspendCount)
scheduleAnimation();
return id;
}
void ScriptedAnimationController::cancelCallback(CallbackId id)
{
for (size_t i = 0; i < m_callbacks.size(); ++i) {
if (m_callbacks[i]->m_id == id) {
m_callbacks[i]->m_firedOrCancelled = true;
InspectorInstrumentation::didCancelAnimationFrame(m_document, id);
m_callbacks.remove(i);
return;
}
}
}
void ScriptedAnimationController::serviceScriptedAnimations(double monotonicTimeNow)
{
if (!m_callbacks.size() || m_suspendCount || (m_document->settings() && !m_document->settings()->requestAnimationFrameEnabled()))
return;
double highResNowMs = 1000.0 * m_document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(monotonicTimeNow);
double legacyHighResNowMs = 1000.0 * m_document->loader()->timing()->monotonicTimeToPseudoWallTime(monotonicTimeNow);
CallbackList callbacks(m_callbacks);
Ref<ScriptedAnimationController> protect(*this);
for (size_t i = 0; i < callbacks.size(); ++i) {
RequestAnimationFrameCallback* callback = callbacks[i].get();
if (!callback->m_firedOrCancelled) {
callback->m_firedOrCancelled = true;
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireAnimationFrame(m_document, callback->m_id);
if (callback->m_useLegacyTimeBase)
callback->handleEvent(legacyHighResNowMs);
else
callback->handleEvent(highResNowMs);
InspectorInstrumentation::didFireAnimationFrame(cookie);
}
}
for (size_t i = 0; i < m_callbacks.size();) {
if (m_callbacks[i]->m_firedOrCancelled)
m_callbacks.remove(i);
else
++i;
}
if (m_callbacks.size())
scheduleAnimation();
}
void ScriptedAnimationController::windowScreenDidChange(PlatformDisplayID displayID)
{
if (m_document->settings() && !m_document->settings()->requestAnimationFrameEnabled())
return;
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
DisplayRefreshMonitorManager::sharedManager().windowScreenDidChange(displayID, *this);
#else
UNUSED_PARAM(displayID);
#endif
}
void ScriptedAnimationController::scheduleAnimation()
{
if (!m_document || (m_document->settings() && !m_document->settings()->requestAnimationFrameEnabled()))
return;
#if USE(REQUEST_ANIMATION_FRAME_TIMER)
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
if (!m_isUsingTimer && !m_isThrottled) {
if (DisplayRefreshMonitorManager::sharedManager().scheduleAnimation(*this))
return;
m_isUsingTimer = true;
}
#endif
if (m_animationTimer.isActive())
return;
double animationInterval = MinimumAnimationInterval;
#if USE(REQUEST_ANIMATION_FRAME_TIMER) && USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
if (m_isThrottled)
animationInterval = MinimumThrottledAnimationInterval;
#endif
double scheduleDelay = std::max<double>(animationInterval - (monotonicallyIncreasingTime() - m_lastAnimationFrameTimeMonotonic), 0);
m_animationTimer.startOneShot(scheduleDelay);
#else
if (FrameView* frameView = m_document->view())
frameView->scheduleAnimation();
#endif
}
#if USE(REQUEST_ANIMATION_FRAME_TIMER)
void ScriptedAnimationController::animationTimerFired(Timer&)
{
m_lastAnimationFrameTimeMonotonic = monotonicallyIncreasingTime();
serviceScriptedAnimations(m_lastAnimationFrameTimeMonotonic);
}
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
void ScriptedAnimationController::displayRefreshFired(double monotonicTimeNow)
{
serviceScriptedAnimations(monotonicTimeNow);
}
#endif
#endif
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
RefPtr<DisplayRefreshMonitor> ScriptedAnimationController::createDisplayRefreshMonitor(PlatformDisplayID displayID) const
{
if (!m_document->page())
return nullptr;
if (auto monitor = m_document->page()->chrome().client().createDisplayRefreshMonitor(displayID))
return monitor;
return DisplayRefreshMonitor::createDefaultDisplayRefreshMonitor(displayID);
}
#endif
}
#endif