Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Instance Methods | List of all members
FlutterVSyncWaiter Class Reference

#include <FlutterVSyncWaiter.h>

Inheritance diagram for FlutterVSyncWaiter:
<FlutterDisplayLinkDelegate>

Instance Methods

(instancetype) - initWithDisplayLink:block:
 
(void) - waitForVSync:
 
(void) - processDisplayLink:targetTimestamp: [implementation]
 
(void) - onDisplayLink:targetTimestamp: [implementation]
 
(void) - dealloc [implementation]
 

Detailed Description

Definition at line 8 of file FlutterVSyncWaiter.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 42 of file FlutterVSyncWaiter.mm.

179 {
180 if (_pendingBaton.has_value()) {
181 FML_LOG(WARNING) << "Deallocating FlutterVSyncWaiter with a pending vsync";
182 }
#define FML_LOG(severity)
Definition logging.h:82

◆ 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 same thread as -waitForVSync:.

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;
55 // Get at least one callback to initialize _lastTargetTimestamp.
56 _displayLink.paused = NO;
57 _warmUpFrame = YES;
58 }
void(^ _block)(CFTimeInterval, CFTimeInterval, uintptr_t)
BOOL _warmUpFrame
#define FML_DCHECK(condition)
Definition logging.h:103
CADisplayLink * _displayLink

◆ onDisplayLink:targetTimestamp:

- (void) onDisplayLink: (CFTimeInterval)  timestamp
targetTimestamp: (CFTimeInterval)  targetTimestamp 
implementation

Reimplemented from <FlutterDisplayLinkDelegate>.

Definition at line 42 of file FlutterVSyncWaiter.mm.

95 :(CFTimeInterval)timestamp targetTimestamp:(CFTimeInterval)targetTimestamp {
96 @synchronized(self) {
97 if (_runLoop == nil) {
98 // Initial vsync - timestamp will be used to determine vsync phase.
99 _lastTargetTimestamp = targetTimestamp;
100 _displayLink.paused = YES;
101 } else {
102 [_runLoop performBlock:^{
103 [self processDisplayLink:timestamp targetTimestamp:targetTimestamp];
104 }];
105 }
NSRunLoop * _runLoop
CFTimeInterval _lastTargetTimestamp

◆ processDisplayLink:targetTimestamp:

- (void) processDisplayLink: (CFTimeInterval)  timestamp
targetTimestamp: (CFTimeInterval)  targetTimestamp 
implementation

Definition at line 42 of file FlutterVSyncWaiter.mm.

63 :(CFTimeInterval)timestamp
64 targetTimestamp:(CFTimeInterval)targetTimestamp {
65 FML_DCHECK([NSRunLoop currentRunLoop] == _runLoop);
66
67 _lastTargetTimestamp = targetTimestamp;
68
69 // CVDisplayLink callback is called one and a half frame before the target
70 // timestamp. That can cause frame-pacing issues if the frame is rendered too early,
71 // it may also trigger frame start before events are processed.
72 CFTimeInterval minStart = targetTimestamp - _displayLink.nominalOutputRefreshPeriod;
73 CFTimeInterval current = CACurrentMediaTime();
74 CFTimeInterval remaining = std::max(minStart - current - kTimerLatencyCompensation, 0.0);
75
76 TRACE_VSYNC("DisplayLinkCallback-Original", _pendingBaton.value_or(0));
77
78 NSTimer* timer = [NSTimer
79 timerWithTimeInterval:remaining
80 repeats:NO
81 block:^(NSTimer* _Nonnull timer) {
82 if (!_pendingBaton.has_value()) {
83 TRACE_VSYNC("DisplayLinkPaused", size_t(0));
84 _displayLink.paused = YES;
85 return;
86 }
87 TRACE_VSYNC("DisplayLinkCallback-Delayed", _pendingBaton.value_or(0));
88 _block(minStart, targetTimestamp, *_pendingBaton);
89 _pendingBaton = std::nullopt;
90 }];
static const CFTimeInterval kTimerLatencyCompensation
#define TRACE_VSYNC(event_type, baton)

◆ waitForVSync:

- (void) waitForVSync: (uintptr_t)  baton

Schedules |baton| to be signaled on next display refresh. The block provided in the initializer will be invoked on same thread as this method (there must be a run loop associated with current thread).

Definition at line 42 of file FlutterVSyncWaiter.mm.

110 :(uintptr_t)baton {
111 // CVDisplayLink start -> callback latency is two frames, there is
112 // no need to delay the warm-up frame.
113 if (_warmUpFrame) {
114 _warmUpFrame = NO;
115 TRACE_VSYNC("WarmUpFrame", baton);
116 [[NSRunLoop currentRunLoop] performBlock:^{
117 CFTimeInterval now = CACurrentMediaTime();
118 _block(now, now, baton);
119 }];
120 return;
121 }
122
123 // RunLoop is accessed both from main thread and from the display link thread.
124 @synchronized(self) {
125 if (_runLoop == nil) {
126 _runLoop = [NSRunLoop currentRunLoop];
127 }
128 }
129
130 FML_DCHECK(_runLoop == [NSRunLoop currentRunLoop]);
131 if (_pendingBaton.has_value()) {
132 FML_LOG(WARNING) << "Engine requested vsync while another was pending";
133 _block(0, 0, *_pendingBaton);
134 _pendingBaton = std::nullopt;
135 }
136
137 TRACE_VSYNC("VSyncRequest", _pendingBaton.value_or(0));
138
139 CFTimeInterval tick_interval = _displayLink.nominalOutputRefreshPeriod;
140 if (_displayLink.paused || tick_interval == 0) {
141 // When starting display link the first notification will come in the middle
142 // of next frame, which would incur a whole frame period of latency.
143 // To avoid that, first vsync notification will be fired using a timer
144 // scheduled to fire where the next frame is expected to start.
145 // Also use a timer if display link does not belong to any display
146 // (nominalOutputRefreshPeriod being 0)
147
148 // Start of the vsync interval.
149 CFTimeInterval start = CACurrentMediaTime();
150
151 // Timer delay is calculated as the time to the next frame start.
152 CFTimeInterval delay = 0;
153
154 if (tick_interval != 0 && _lastTargetTimestamp != 0) {
155 CFTimeInterval phase = fmod(_lastTargetTimestamp, tick_interval);
156 CFTimeInterval now = start;
157 start = now - (fmod(now, tick_interval)) + phase;
158 if (start < now) {
159 start += tick_interval;
160 }
161 delay = std::max(start - now - kTimerLatencyCompensation, 0.0);
162 }
163
164 NSTimer* timer = [NSTimer timerWithTimeInterval:delay
165 repeats:NO
166 block:^(NSTimer* timer) {
167 CFTimeInterval targetTimestamp =
168 start + tick_interval;
169 TRACE_VSYNC("SynthesizedInitialVSync", baton);
170 _block(start, targetTimestamp, baton);
171 }];
172 [_runLoop addTimer:timer forMode:NSRunLoopCommonModes];
173 _displayLink.paused = NO;
174 } else {
175 _pendingBaton = baton;

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