5#import "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h"
9#include <Foundation/Foundation.h>
10#include <UIKit/UIKit.h>
11#include <mach/mach_time.h>
13#include "flutter/common/task_runners.h"
14#include "flutter/fml/logging.h"
15#include "flutter/fml/memory/task_runner_checker.h"
16#include "flutter/fml/trace_event.h"
19@property(nonatomic, assign, readonly)
double refreshRate;
29 auto callback = [this](std::unique_ptr<flutter::FrameTimingsRecorder> recorder) {
34 client_ = [[
VSyncClient alloc] initWithTaskRunner:task_runners_.GetUITaskRunner()
48 max_refresh_rate_ = new_max_refresh_rate;
66- (instancetype)initWithTaskRunner:(
fml::RefPtr<
fml::TaskRunner>)task_runner
67 callback:(
flutter::VsyncWaiter::Callback)callback {
72 _allowPauseAfterVsync = YES;
74 _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
77 [
self setMaxRefreshRate:DisplayLinkManager.displayRefreshRate];
81 task_runner->PostTask([localDisplayLink]() {
82 [localDisplayLink addToRunLoop:NSRunLoop.currentRunLoop forMode:NSRunLoopCommonModes];
89- (void)setMaxRefreshRate:(
double)refreshRate {
93 double maxFrameRate = fmax(refreshRate, 60);
94 double minFrameRate = fmax(maxFrameRate / 2, 60);
95 if (@available(iOS 15.0, *)) {
97 CAFrameRateRangeMake(minFrameRate, maxFrameRate, maxFrameRate);
111- (void)onDisplayLink:(CADisplayLink*)link {
112 CFTimeInterval delay = CACurrentMediaTime() -
link.timestamp;
122 std::unique_ptr<flutter::FrameTimingsRecorder> recorder =
123 std::make_unique<flutter::FrameTimingsRecorder>();
125 _refreshRate =
round(1 / (frame_target_time - frame_start_time).ToSecondsF());
127 recorder->RecordVsync(frame_start_time, frame_target_time);
128 if (_allowPauseAfterVsync) {
131 _callback(std::move(recorder));
135 [_displayLink invalidate];
139- (CADisplayLink*)getDisplayLink {
148 CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:[[[
self class] alloc] init]
149 selector:@selector(onDisplayLink:)];
150 displayLink.paused = YES;
151 auto preferredFPS = displayLink.preferredFramesPerSecond;
158 if (preferredFPS != 0) {
162 return UIScreen.mainScreen.maximumFramesPerSecond;
165- (void)onDisplayLink:(CADisplayLink*)link {
170 return [[NSBundle.mainBundle objectForInfoDictionaryKey:@"CADisableMinimumFrameDurationOnPhone"]
static void round(SkPoint *p)
~VsyncWaiterIOS() override
double GetRefreshRate() const override
void AwaitVSync() override
VsyncWaiterIOS(const flutter::TaskRunners &task_runners)
std::function< void(std::unique_ptr< FrameTimingsRecorder >)> Callback
void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks=true)
constexpr int64_t ToMicroseconds() const
TimeDelta ToEpochDelta() const
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
BOOL maxRefreshRateEnabledOnIPhone
double displayRefreshRate
The display refresh rate used for reporting purposes. The engine does not care about this for frame s...
void setMaxRefreshRate:(double refreshRate)
void invalidate()
Call invalidate before releasing this object to remove from runloops.
#define TRACE_EVENT2_INT(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
CADisplayLink * _displayLink
static const double kRefreshRateDiffToIgnore