9 #include "flutter/fml/logging.h"
10 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
11 #import "flutter/shell/platform/darwin/macos/InternalFlutterSwift/InternalFlutterSwift.h"
14 #if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
15 #define VSYNC_TRACING_ENABLED 1
18 #if VSYNC_TRACING_ENABLED
19 #include <OSLog/OSLog.h>
23 #define TRACE_VSYNC(event_type, baton) \
25 static os_log_t log = os_log_create("FlutterVSync", "PointsOfInterest"); \
26 _Pragma("GCC diagnostic push") _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \
27 os_signpost_event_emit(log, OS_SIGNPOST_ID_EXCLUSIVE, event_type, "baton %lx", baton); \
28 _Pragma("GCC diagnostic pop") \
31 #define TRACE_VSYNC(event_type, baton) \
44 std::optional<std::uintptr_t> _pendingBaton;
46 void (^
_block)(CFTimeInterval, CFTimeInterval, uintptr_t);
52 block:(
void (^)(CFTimeInterval timestamp,
53 CFTimeInterval targetTimestamp,
54 uintptr_t baton))block {
55 FML_DCHECK([NSThread isMainThread]);
56 if (
self = [super init]) {
69 - (void)onDisplayLink:(CFTimeInterval)timestamp targetTimestamp:(CFTimeInterval)targetTimestamp {
80 CFTimeInterval minStart = targetTimestamp -
_displayLink.nominalOutputRefreshPeriod;
81 CFTimeInterval current = CACurrentMediaTime();
84 TRACE_VSYNC(
"DisplayLinkCallback-Original", _pendingBaton.value_or(0));
86 [FlutterRunLoop.mainRunLoop
87 performAfterDelay:remaining
89 if (!_pendingBaton.has_value()) {
90 TRACE_VSYNC("DisplayLinkPaused", size_t(0));
91 _displayLink.paused = YES;
94 TRACE_VSYNC("DisplayLinkCallback-Delayed", _pendingBaton.value_or(0));
95 _block(minStart, targetTimestamp, *_pendingBaton);
96 _pendingBaton = std::nullopt;
102 - (void)waitForVSync:(uintptr_t)baton {
103 FML_DCHECK([NSThread isMainThread]);
109 CFTimeInterval now = CACurrentMediaTime();
114 if (_pendingBaton.has_value()) {
115 [FlutterLogger logWarning:@"Engine requested vsync while another was pending"];
116 _block(0, 0, *_pendingBaton);
117 _pendingBaton = std::nullopt;
120 TRACE_VSYNC(
"VSyncRequest", _pendingBaton.value_or(0));
132 CFTimeInterval start = CACurrentMediaTime();
135 CFTimeInterval delay = 0;
139 CFTimeInterval now = start;
140 start = now - (fmod(now, tick_interval)) + phase;
142 start += tick_interval;
147 [FlutterRunLoop.mainRunLoop performAfterDelay:delay
149 CFTimeInterval targetTime = start + tick_interval;
150 TRACE_VSYNC("SynthesizedInitialVSync", baton);
151 _block(start, targetTime, baton);
155 _pendingBaton = baton;
163 if (_pendingBaton.has_value()) {
164 CFTimeInterval now = CACurrentMediaTime();
165 _block(now, now, _pendingBaton.value());
166 _pendingBaton = std::nullopt;
169 [_displayLink invalidate];
FlutterDisplayLink * _displayLink
static const CFTimeInterval kTimerLatencyCompensation
#define TRACE_VSYNC(event_type, baton)
CFTimeInterval _lastTargetTimestamp
void(^ _block)(CFTimeInterval, CFTimeInterval, uintptr_t)
CFTimeInterval nominalOutputRefreshPeriod
BOOL paused
Pauses and resumes the display link.
id< FlutterDisplayLinkDelegate > delegate