Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
vsync_waiter_ios.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
10
12
13// When calculating refresh rate diffrence, anything within 0.1 fps is ignored.
14const static double kRefreshRateDiffToIgnore = 0.1;
15
16namespace flutter {
17
19 : VsyncWaiter(task_runners) {
20 auto vsyncCallback = ^(CFTimeInterval startTime, CFTimeInterval targetTime) {
21 // Compute delay using the same CACurrentMediaTime() clock.
22 CFTimeInterval delay = CACurrentMediaTime() - startTime;
23 if (delay < 0.0) {
24 delay = 0.0;
25 }
26
27 // Align the start time to the C++ steady_clock used by fml::TimePoint.
29
30 // Snap to the nearest whole Hz value to avoid floating point errors.
31 CFTimeInterval duration =
32 VsyncWaiterIOS::SnapDuration(targetTime - startTime, max_refresh_rate_);
33
34 // Align target time to the C++ steady_clock used by fml::TimePoint.
35 fml::TimePoint target_time = start_time + fml::TimeDelta::FromSecondsF(duration);
36 FireCallback(start_time, target_time, true);
37 };
38 FlutterFMLTaskRunner* uiTaskRunner =
39 [[FlutterFMLTaskRunner alloc] initWithTaskRunner:task_runners_.GetUITaskRunner()];
40 client_ = [[FlutterVSyncClient alloc]
41 initWithTaskRunner:uiTaskRunner
44 callback:vsyncCallback];
46}
47
49 // This way, we will get no more callbacks from the display link that holds a weak (non-nilling)
50 // reference to this C++ object.
51 [client_ invalidate];
52}
53
54void VsyncWaiterIOS::AwaitVSync() {
55 double new_max_refresh_rate = FlutterDisplayLinkManager.displayRefreshRate;
56 if (fabs(new_max_refresh_rate - max_refresh_rate_) > kRefreshRateDiffToIgnore) {
57 max_refresh_rate_ = new_max_refresh_rate;
58 [client_ setMaxRefreshRate:max_refresh_rate_];
59 }
60 [client_ await];
61}
62
63// |VariableRefreshRateReporter|
65 return client_.refreshRate;
66}
67
68CFTimeInterval VsyncWaiterIOS::SnapDuration(CFTimeInterval duration, double max_refresh_rate) {
69 if (duration > 0.0) {
70 double roundedRefreshRate = round(1.0 / duration);
71 return 1.0 / roundedRefreshRate;
72 }
73 double fallbackRefreshRate = max_refresh_rate > 0.0 ? max_refresh_rate : 60.0;
74 return 1.0 / fallbackRefreshRate;
75}
76
77} // namespace flutter
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks=true)
const TaskRunners task_runners_
static CFTimeInterval SnapDuration(CFTimeInterval duration, double max_refresh_rate)
double GetRefreshRate() const override
VsyncWaiterIOS(const flutter::TaskRunners &task_runners)
static constexpr TimeDelta FromSecondsF(double seconds)
Definition time_delta.h:53
static TimePoint Now()
Definition time_point.cc:49
FlutterDesktopBinaryReply callback
A client that wraps a CADisplayLink to deliver synchronized vsync signals.
double refreshRate
The current display refresh rate in Hertz, rounded to the nearest integer value.
FLUTTER_ASSERT_ARC static const double kRefreshRateDiffToIgnore