DisplayRefreshMonitorMac.cpp [plain text]
#include "config.h"
#include "DisplayRefreshMonitorMac.h"
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
#include <QuartzCore/QuartzCore.h>
#include <wtf/CurrentTime.h>
#include <wtf/MainThread.h>
namespace WebCore {
DisplayRefreshMonitorMac::DisplayRefreshMonitorMac(PlatformDisplayID displayID)
: DisplayRefreshMonitor(displayID)
, m_displayLink(nullptr)
{
}
DisplayRefreshMonitorMac::~DisplayRefreshMonitorMac()
{
if (m_displayLink) {
CVDisplayLinkStop(m_displayLink);
CVDisplayLinkRelease(m_displayLink);
m_displayLink = nullptr;
}
cancelCallOnMainThread(DisplayRefreshMonitor::handleDisplayRefreshedNotificationOnMainThread, this);
}
static CVReturn displayLinkCallback(CVDisplayLinkRef, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags, CVOptionFlags*, void* data)
{
DisplayRefreshMonitorMac* monitor = static_cast<DisplayRefreshMonitorMac*>(data);
double nowSeconds = static_cast<double>(now->videoTime) / static_cast<double>(now->videoTimeScale);
double outputTimeSeconds = static_cast<double>(outputTime->videoTime) / static_cast<double>(outputTime->videoTimeScale);
monitor->displayLinkFired(nowSeconds, outputTimeSeconds);
return kCVReturnSuccess;
}
bool DisplayRefreshMonitorMac::requestRefreshCallback()
{
if (!isActive())
return false;
if (!m_displayLink) {
setIsActive(false);
CVReturn error = CVDisplayLinkCreateWithCGDisplay(displayID(), &m_displayLink);
if (error)
return false;
error = CVDisplayLinkSetOutputCallback(m_displayLink, displayLinkCallback, this);
if (error)
return false;
error = CVDisplayLinkStart(m_displayLink);
if (error)
return false;
setIsActive(true);
}
MutexLocker lock(mutex());
setIsScheduled(true);
return true;
}
void DisplayRefreshMonitorMac::displayLinkFired(double nowSeconds, double outputTimeSeconds)
{
MutexLocker lock(mutex());
if (!isPreviousFrameDone())
return;
setIsPreviousFrameDone(false);
double webKitMonotonicNow = monotonicallyIncreasingTime();
double timeUntilOutput = outputTimeSeconds - nowSeconds;
setMonotonicAnimationStartTime(webKitMonotonicNow + timeUntilOutput);
callOnMainThread(handleDisplayRefreshedNotificationOnMainThread, this);
}
}
#endif // USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)