// 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 "flutter/shell/platform/darwin/desktop/vsync_waiter_mac.h" #include #include "flutter/fml/logging.h" namespace shell { #define link_ (reinterpret_cast(opaque_)) VsyncWaiterMac::VsyncWaiterMac(blink::TaskRunners task_runners) : VsyncWaiter(std::move(task_runners)), opaque_(nullptr) { // Create the link. CVDisplayLinkRef link = nullptr; CVDisplayLinkCreateWithActiveCGDisplays(&link); opaque_ = link; // Set the output callback. CVDisplayLinkSetOutputCallback( link_, [](CVDisplayLinkRef link, const CVTimeStamp* now, const CVTimeStamp* output, CVOptionFlags flags_in, CVOptionFlags* flags_out, void* context) -> CVReturn { OnDisplayLink(context); return kCVReturnSuccess; }, this); } VsyncWaiterMac::~VsyncWaiterMac() { CVDisplayLinkRelease(link_); } void VsyncWaiterMac::OnDisplayLink(void* context) { reinterpret_cast(context)->OnDisplayLink(); } void VsyncWaiterMac::OnDisplayLink() { fml::TimePoint frame_start_time = fml::TimePoint::Now(); fml::TimePoint frame_target_time = frame_start_time + fml::TimeDelta::FromSecondsF( CVDisplayLinkGetActualOutputVideoRefreshPeriod(link_)); CVDisplayLinkStop(link_); FireCallback(frame_start_time, frame_target_time); } void VsyncWaiterMac::AwaitVSync() { CVDisplayLinkStart(link_); } } // namespace shell