Flutter iOS Embedder
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 
8 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
10 
12 
13 // When calculating refresh rate diffrence, anything within 0.1 fps is ignored.
14 const static double kRefreshRateDiffToIgnore = 0.1;
15 
16 namespace flutter {
17 
18 VsyncWaiterIOS::VsyncWaiterIOS(const flutter::TaskRunners& task_runners,
19  FlutterDisplayLinkManager* display_link_manager)
20  : VsyncWaiter(task_runners), display_link_manager_(display_link_manager) {
21  FML_DCHECK(display_link_manager);
22  auto vsyncCallback = ^(CFTimeInterval startTime, CFTimeInterval targetTime) {
23  if (!waiting_for_vsync_) {
24  [client_ pause];
25  return;
26  }
27  waiting_for_vsync_ = false;
28 
29  // Compute delay using the same CACurrentMediaTime() clock.
30  CFTimeInterval delay = CACurrentMediaTime() - startTime;
31  if (delay < 0.0) {
32  delay = 0.0;
33  }
34 
35  // Align the start time to the C++ steady_clock used by fml::TimePoint.
36  fml::TimePoint start_time = fml::TimePoint::Now() - fml::TimeDelta::FromSecondsF(delay);
37 
38  // Snap to the nearest whole Hz value to avoid floating point errors.
39  CFTimeInterval duration =
40  VsyncWaiterIOS::SnapDuration(targetTime - startTime, max_refresh_rate_);
41 
42  // Align target time to the C++ steady_clock used by fml::TimePoint.
43  fml::TimePoint target_time = start_time + fml::TimeDelta::FromSecondsF(duration);
44  FireCallback(start_time, target_time);
45  };
46  FlutterFMLTaskRunner* uiTaskRunner =
47  [[FlutterFMLTaskRunner alloc] initWithTaskRunner:task_runners_.GetUITaskRunner()];
48  client_ = [[FlutterVSyncClient alloc]
49  initWithTaskRunner:uiTaskRunner
50  isVariableRefreshRateEnabled:display_link_manager_.maxRefreshRateEnabledOnIPhone
51  maxRefreshRate:display_link_manager_.displayRefreshRate
52  callback:vsyncCallback];
53  client_.allowPauseAfterVsync = NO;
54  max_refresh_rate_ = display_link_manager_.displayRefreshRate;
55 }
56 
58  // This way, we will get no more callbacks from the display link that holds a weak (non-nilling)
59  // reference to this C++ object.
60  [client_ invalidate];
61 }
62 
64  double new_max_refresh_rate = display_link_manager_.displayRefreshRate;
65  if (fabs(new_max_refresh_rate - max_refresh_rate_) > kRefreshRateDiffToIgnore) {
66  max_refresh_rate_ = new_max_refresh_rate;
67  [client_ setMaxRefreshRate:max_refresh_rate_];
68  }
69 
70  if (client_.displayLink.paused) {
71  [client_ await];
72  if (dispatching_event_) {
73  // This vsync request came synchronously while dispatching UIKit event and
74  // there is an entire frame ahead - the vsync callback can be short-circuited.
75  const fml::TimePoint frame_start_time = fml::TimePoint::Now();
76  const fml::TimePoint frame_target_time =
77  frame_start_time + fml::TimeDelta::FromSecondsF(SnapDuration(0.0, max_refresh_rate_));
78  FireCallback(frame_start_time, frame_target_time);
79  } else {
80  // This vsync request came at an unknown phase of the frame so wait for
81  // the next display link callback.
82  waiting_for_vsync_ = true;
83  }
84  } else {
85  waiting_for_vsync_ = true;
86  }
87 }
88 
89 // |VariableRefreshRateReporter|
91  return client_.refreshRate;
92 }
93 
94 CFTimeInterval VsyncWaiterIOS::SnapDuration(CFTimeInterval duration, double max_refresh_rate) {
95  if (duration > 0.0) {
96  double roundedRefreshRate = round(1.0 / duration);
97  return 1.0 / roundedRefreshRate;
98  }
99  double fallbackRefreshRate = max_refresh_rate > 0.0 ? max_refresh_rate : 60.0;
100  return 1.0 / fallbackRefreshRate;
101 }
102 
103 } // namespace flutter
static CFTimeInterval SnapDuration(CFTimeInterval duration, double max_refresh_rate)
VsyncWaiterIOS(const flutter::TaskRunners &task_runners, FlutterDisplayLinkManager *display_link_manager)
double GetRefreshRate() const override
void AwaitVSync() override
FLUTTER_ASSERT_ARC static const double kRefreshRateDiffToIgnore