[iOS] Migrate VSyncClient to a pure Obj-C implementation (#186166)
Migrates `FlutterVSyncClient` from a hybrid C++/Obj-C implementation to
a pure Objective-C implementation by removing C++ types from its
interface and implementation.
Previously, `FlutterVSyncClient` stored a C++
`flutter::VsyncWaiter::Callback` and used a temporary
`std::unique_ptr<flutter::FrameTimingsRecorder>` to propagate vsync
timestamps. This was redundant:
* `FlutterVSyncClient` allocated a temporary `FrameTimingsRecorder` and
recorded vsync times.
* It passed this to a C++ lambda wrapper.
* The lambda extracted the start/target times as seconds and passed them
to the Obj-C block callback.
* The temporary recorder was immediately discarded.
* `VsyncWaiterIOS` converted the seconds back to `fml::TimePoint`.
* `VsyncWaiter::FireCallback` allocated *another* `FrameTimingsRecorder`
and recorded the same times again.
We now:
* manage the callback as pure Obj-C block that accepts raw
`CFTimeInterval` (double seconds) directly from `CADisplayLink`.
* Remove the redundant `FrameTimingsRecorder` and C++ lambda wrapper.
* Rely solely on `VsyncWaiter::FireCallback` to handle the final
`FrameTimingsRecorder` creation and recording, which it already does.
No test changes because this introduces no semantic change and is
covered by existing tests recently added in:
https://github.com/flutter/flutter/blob/master/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VSyncClientTest.swift
## Pre-launch Checklist
- [X] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [X] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [X] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [X] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [X] I signed the [CLA].
- [X] I listed at least one issue that this PR fixes in the description
above.
- [X] I updated/added relevant documentation (doc comments with `///`).
- [X] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [X] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [X] All existing and new tests are passing.
If you need help, consider asking for advice on the #hackers-new channel
on [Discord].
If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.
**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterVSyncClient.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterVSyncClient.mm
index 57c744c..b484855 100644
--- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterVSyncClient.mm
+++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterVSyncClient.mm
@@ -3,7 +3,6 @@
// found in the LICENSE file.
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterVSyncClient.h"
-#include "flutter/shell/common/vsync_waiter.h"
#import <UIKit/UIKit.h>
@@ -16,7 +15,7 @@
NSString* const kCADisableMinimumFrameDurationOnPhoneKey = @"CADisableMinimumFrameDurationOnPhone";
@implementation FlutterVSyncClient {
- flutter::VsyncWaiter::Callback _callback;
+ void (^_callback)(CFTimeInterval startTime, CFTimeInterval targetTime);
CADisplayLink* _displayLink;
BOOL _isVariableRefreshRateEnabled;
}
@@ -35,11 +34,7 @@
_refreshRate = maxRefreshRate;
_isVariableRefreshRateEnabled = isVariableRefreshRateEnabled;
_allowPauseAfterVsync = YES;
- _callback = [callback](std::unique_ptr<flutter::FrameTimingsRecorder> recorder) {
- double start_time_seconds = recorder->GetVsyncStartTime().ToEpochDelta().ToSecondsF();
- double target_time_seconds = recorder->GetVsyncTargetTime().ToEpochDelta().ToSecondsF();
- callback(start_time_seconds, target_time_seconds);
- };
+ _callback = callback;
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplayLink:)];
_displayLink.paused = YES;
@@ -82,28 +77,17 @@
}
- (void)onDisplayLink:(CADisplayLink*)link {
- CFTimeInterval delay = CACurrentMediaTime() - link.timestamp;
- fml::TimePoint frame_start_time = fml::TimePoint::Now() - fml::TimeDelta::FromSecondsF(delay);
+ [FlutterTracing tracePlatformVsyncWithStartTime:link.timestamp targetTime:link.targetTimestamp];
CFTimeInterval duration = link.targetTimestamp - link.timestamp;
- fml::TimePoint frame_target_time = frame_start_time + fml::TimeDelta::FromSecondsF(duration);
-
- [FlutterTracing tracePlatformVsyncWithStartTime:frame_start_time.ToEpochDelta().ToSecondsF()
- targetTime:frame_target_time.ToEpochDelta().ToSecondsF()];
-
- std::unique_ptr<flutter::FrameTimingsRecorder> recorder =
- std::make_unique<flutter::FrameTimingsRecorder>();
-
if (duration > 0) {
_refreshRate = round(1 / duration);
}
- recorder->RecordVsync(frame_start_time, frame_target_time);
-
if (_allowPauseAfterVsync) {
link.paused = YES;
}
- _callback(std::move(recorder));
+ _callback(link.timestamp, link.targetTimestamp);
}
- (void)dealloc {