Flutter macOS Embedder
FlutterVSyncWaiter Class Reference

#import <FlutterVSyncWaiter.h>

Inheritance diagram for FlutterVSyncWaiter:

Instance Methods

(instancetype) - initWithDisplayLink:block:
 
(void) - waitForVSync:
 

Detailed Description

Definition at line 8 of file FlutterVSyncWaiter.h.

Method Documentation

◆ initWithDisplayLink:block:

- (instancetype) initWithDisplayLink: (FlutterDisplayLink*)  displayLink
block: (void(^)(CFTimeInterval timestamp, CFTimeInterval targetTimestamp, uintptr_t baton))  block 

Creates new waiter instance tied to provided NSView. This function must be called on the main thread.

Provided |block| will be invoked on main thread.

Definition at line 42 of file FlutterVSyncWaiter.mm.

45  :(FlutterDisplayLink*)displayLink
46  block:(void (^)(CFTimeInterval timestamp,
47  CFTimeInterval targetTimestamp,
48  uintptr_t baton))block {
49  FML_DCHECK([NSThread isMainThread]);
50  if (self = [super init]) {
51  _block = block;
52 
53  _displayLink = displayLink;
54  _displayLink.delegate = self;
55  // Get at least one callback to initialize _lastTargetTimestamp.
56  _displayLink.paused = NO;
57  _warmUpFrame = YES;
58  }
FlutterDisplayLink * _displayLink
void(^ _block)(CFTimeInterval, CFTimeInterval, uintptr_t)
BOOL _warmUpFrame

◆ waitForVSync:

- (void) waitForVSync: (uintptr_t)  baton

Schedules |baton| to be signaled on next display refresh. This function must be called on the main thread.

Definition at line 42 of file FlutterVSyncWaiter.mm.

96  :(uintptr_t)baton {
97  FML_DCHECK([NSThread isMainThread]);
98  // CVDisplayLink start -> callback latency is two frames, there is
99  // no need to delay the warm-up frame.
100  if (_warmUpFrame) {
101  _warmUpFrame = NO;
102  TRACE_VSYNC("WarmUpFrame", baton);
103  CFTimeInterval now = CACurrentMediaTime();
104  _block(now, now, baton);
105  return;
106  }
107 
108  if (_pendingBaton.has_value()) {
109  FML_LOG(WARNING) << "Engine requested vsync while another was pending";
110  _block(0, 0, *_pendingBaton);
111  _pendingBaton = std::nullopt;
112  }
113 
114  TRACE_VSYNC("VSyncRequest", _pendingBaton.value_or(0));
115 
116  CFTimeInterval tick_interval = _displayLink.nominalOutputRefreshPeriod;
117  if (_displayLink.paused || tick_interval == 0) {
118  // When starting display link the first notification will come in the middle
119  // of next frame, which would incur a whole frame period of latency.
120  // To avoid that, first vsync notification will be fired using a timer
121  // scheduled to fire where the next frame is expected to start.
122  // Also use a timer if display link does not belong to any display
123  // (nominalOutputRefreshPeriod being 0)
124 
125  // Start of the vsync interval.
126  CFTimeInterval start = CACurrentMediaTime();
127 
128  // Timer delay is calculated as the time to the next frame start.
129  CFTimeInterval delay = 0;
130 
131  if (tick_interval != 0 && _lastTargetTimestamp != 0) {
132  CFTimeInterval phase = fmod(_lastTargetTimestamp, tick_interval);
133  CFTimeInterval now = start;
134  start = now - (fmod(now, tick_interval)) + phase;
135  if (start < now) {
136  start += tick_interval;
137  }
138  delay = std::max(start - now - kTimerLatencyCompensation, 0.0);
139  }
140 
141  [FlutterRunLoop.mainRunLoop
142  performBlock:^{
143  CFTimeInterval targetTimestamp = start + tick_interval;
144  TRACE_VSYNC("SynthesizedInitialVSync", baton);
145  _block(start, targetTimestamp, baton);
146  }
147  afterDelay:delay];
148  _displayLink.paused = NO;
149  } else {
150  _pendingBaton = baton;
static const CFTimeInterval kTimerLatencyCompensation
#define TRACE_VSYNC(event_type, baton)
CFTimeInterval _lastTargetTimestamp
void performBlock:afterDelay:(void(^ block)(void),[afterDelay] NSTimeInterval delay)

Referenced by TEST_F().


The documentation for this class was generated from the following files: