[iOS] Avoid jitter and laggy when user is dragging on iOS Promotion devices (flutter/engine#35592)
This commit is contained in:
@@ -65,6 +65,14 @@ typedef struct MouseState {
|
||||
@property(nonatomic, assign) double targetViewInsetBottom;
|
||||
@property(nonatomic, retain) VSyncClient* keyboardAnimationVSyncClient;
|
||||
|
||||
/// VSyncClient for touch events delivery frame rate correction.
|
||||
///
|
||||
/// On promotion devices(eg: iPhone13 Pro), the delivery frame rate of touch events is 60HZ
|
||||
/// but the frame rate of rendering is 120HZ, which is different and will leads jitter and laggy.
|
||||
/// With this VSyncClient, it can correct the delivery frame rate of touch events to let it keep
|
||||
/// the same with frame rate of rendering.
|
||||
@property(nonatomic, retain) VSyncClient* touchRateCorrectionVSyncClient;
|
||||
|
||||
/*
|
||||
* Mouse and trackpad gesture recognizers
|
||||
*/
|
||||
@@ -680,6 +688,9 @@ static void SendFakeTouchEvent(FlutterEngine* engine,
|
||||
// Register internal plugins.
|
||||
[self addInternalPlugins];
|
||||
|
||||
// Create a vsync client to correct delivery frame rate of touch events if needed.
|
||||
[self createTouchRateCorrectionVSyncClientIfNeeded];
|
||||
|
||||
if (@available(iOS 13.4, *)) {
|
||||
_hoverGestureRecognizer =
|
||||
[[UIHoverGestureRecognizer alloc] initWithTarget:self action:@selector(hoverEvent:)];
|
||||
@@ -842,6 +853,7 @@ static void SendFakeTouchEvent(FlutterEngine* engine,
|
||||
[self deregisterNotifications];
|
||||
|
||||
[self invalidateKeyboardAnimationVSyncClient];
|
||||
[self invalidateTouchRateCorrectionVSyncClient];
|
||||
_scrollView.get().delegate = nil;
|
||||
_hoverGestureRecognizer.delegate = nil;
|
||||
[_hoverGestureRecognizer release];
|
||||
@@ -975,6 +987,9 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
|
||||
}
|
||||
}
|
||||
|
||||
// Activate or pause the correction of delivery frame rate of touch events.
|
||||
[self triggerTouchRateCorrectionIfNeeded:touches];
|
||||
|
||||
const CGFloat scale = [UIScreen mainScreen].scale;
|
||||
auto packet =
|
||||
std::make_unique<flutter::PointerDataPacket>(touches.count + touches_to_remove_count);
|
||||
@@ -1120,6 +1135,63 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
|
||||
[self dispatchTouches:touches pointerDataChangeOverride:&cancel event:nullptr];
|
||||
}
|
||||
|
||||
#pragma mark - Touch events rate correction
|
||||
|
||||
- (void)createTouchRateCorrectionVSyncClientIfNeeded {
|
||||
if (_touchRateCorrectionVSyncClient != nil) {
|
||||
return;
|
||||
}
|
||||
|
||||
double displayRefreshRate = [DisplayLinkManager displayRefreshRate];
|
||||
const double epsilon = 0.1;
|
||||
if (displayRefreshRate < 60.0 + epsilon) { // displayRefreshRate <= 60.0
|
||||
|
||||
// If current device's max frame rate is not larger than 60HZ, the delivery rate of touch events
|
||||
// is the same with render vsync rate. So it is unnecessary to create
|
||||
// _touchRateCorrectionVSyncClient to correct touch callback's rate.
|
||||
return;
|
||||
}
|
||||
|
||||
flutter::Shell& shell = [_engine.get() shell];
|
||||
auto callback = [](std::unique_ptr<flutter::FrameTimingsRecorder> recorder) {
|
||||
// Do nothing in this block. Just trigger system to callback touch events with correct rate.
|
||||
};
|
||||
_touchRateCorrectionVSyncClient =
|
||||
[[VSyncClient alloc] initWithTaskRunner:shell.GetTaskRunners().GetPlatformTaskRunner()
|
||||
callback:callback];
|
||||
_touchRateCorrectionVSyncClient.allowPauseAfterVsync = NO;
|
||||
}
|
||||
|
||||
- (void)triggerTouchRateCorrectionIfNeeded:(NSSet*)touches {
|
||||
if (_touchRateCorrectionVSyncClient == nil) {
|
||||
// If the _touchRateCorrectionVSyncClient is not created, means current devices doesn't
|
||||
// need to correct the touch rate. So just return.
|
||||
return;
|
||||
}
|
||||
|
||||
// As long as there is a touch's phase is UITouchPhaseBegan or UITouchPhaseMoved,
|
||||
// activate the correction. Otherwise pause the correction.
|
||||
BOOL isUserInteracting = NO;
|
||||
for (UITouch* touch in touches) {
|
||||
if (touch.phase == UITouchPhaseBegan || touch.phase == UITouchPhaseMoved) {
|
||||
isUserInteracting = YES;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isUserInteracting && [_engine.get() viewController] == self) {
|
||||
[_touchRateCorrectionVSyncClient await];
|
||||
} else {
|
||||
[_touchRateCorrectionVSyncClient pause];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)invalidateTouchRateCorrectionVSyncClient {
|
||||
[_touchRateCorrectionVSyncClient invalidate];
|
||||
[_touchRateCorrectionVSyncClient release];
|
||||
_touchRateCorrectionVSyncClient = nil;
|
||||
}
|
||||
|
||||
#pragma mark - Handle view resizing
|
||||
|
||||
- (void)updateViewportMetrics {
|
||||
|
||||
@@ -114,6 +114,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc;
|
||||
|
||||
@property(nonatomic, assign) double targetViewInsetBottom;
|
||||
|
||||
- (void)createTouchRateCorrectionVSyncClientIfNeeded;
|
||||
- (void)surfaceUpdated:(BOOL)appeared;
|
||||
- (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences;
|
||||
- (void)handlePressEvent:(FlutterUIPressProxy*)press
|
||||
@@ -160,6 +161,18 @@ extern NSNotificationName const FlutterViewControllerWillDealloc;
|
||||
self.messageSent = nil;
|
||||
}
|
||||
|
||||
- (void)testViewDidLoadWillInvokeCreateTouchRateCorrectionVSyncClient {
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
FlutterViewController* viewControllerMock = OCMPartialMock(viewController);
|
||||
[viewControllerMock loadView];
|
||||
[viewControllerMock viewDidLoad];
|
||||
OCMVerify([viewControllerMock createTouchRateCorrectionVSyncClientIfNeeded]);
|
||||
}
|
||||
|
||||
- (void)testStartKeyboardAnimationWillInvokeSetupKeyboardAnimationVsyncClient {
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
|
||||
@@ -11,6 +11,12 @@
|
||||
|
||||
FLUTTER_ASSERT_NOT_ARC
|
||||
|
||||
@interface UITouch ()
|
||||
|
||||
@property(nonatomic, readwrite) UITouchPhase phase;
|
||||
|
||||
@end
|
||||
|
||||
@interface VSyncClient (Testing)
|
||||
|
||||
- (CADisplayLink*)getDisplayLink;
|
||||
@@ -22,7 +28,11 @@ FLUTTER_ASSERT_NOT_ARC
|
||||
@property(nonatomic, assign) double targetViewInsetBottom;
|
||||
@property(nonatomic, retain) VSyncClient* keyboardAnimationVSyncClient;
|
||||
|
||||
@property(nonatomic, retain) VSyncClient* touchRateCorrectionVSyncClient;
|
||||
|
||||
- (void)createTouchRateCorrectionVSyncClientIfNeeded;
|
||||
- (void)setupKeyboardAnimationVsyncClient;
|
||||
- (void)triggerTouchRateCorrectionIfNeeded:(NSSet*)touches;
|
||||
|
||||
@end
|
||||
|
||||
@@ -56,4 +66,111 @@ FLUTTER_ASSERT_NOT_ARC
|
||||
}
|
||||
}
|
||||
|
||||
- (void)
|
||||
testCreateTouchRateCorrectionVSyncClientWillCreateVsyncClientWhenRefreshRateIsLargerThan60HZ {
|
||||
id mockDisplayLinkManager = [OCMockObject mockForClass:[DisplayLinkManager class]];
|
||||
double maxFrameRate = 120;
|
||||
[[[mockDisplayLinkManager stub] andReturnValue:@(maxFrameRate)] displayRefreshRate];
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
[viewController createTouchRateCorrectionVSyncClientIfNeeded];
|
||||
XCTAssertNotNil(viewController.touchRateCorrectionVSyncClient);
|
||||
}
|
||||
|
||||
- (void)testCreateTouchRateCorrectionVSyncClientWillNotCreateNewVSyncClientWhenClientAlreadyExists {
|
||||
id mockDisplayLinkManager = [OCMockObject mockForClass:[DisplayLinkManager class]];
|
||||
double maxFrameRate = 120;
|
||||
[[[mockDisplayLinkManager stub] andReturnValue:@(maxFrameRate)] displayRefreshRate];
|
||||
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
[viewController createTouchRateCorrectionVSyncClientIfNeeded];
|
||||
VSyncClient* clientBefore = viewController.touchRateCorrectionVSyncClient;
|
||||
XCTAssertNotNil(clientBefore);
|
||||
|
||||
[viewController createTouchRateCorrectionVSyncClientIfNeeded];
|
||||
VSyncClient* clientAfter = viewController.touchRateCorrectionVSyncClient;
|
||||
XCTAssertNotNil(clientAfter);
|
||||
|
||||
XCTAssertTrue(clientBefore == clientAfter);
|
||||
}
|
||||
|
||||
- (void)testCreateTouchRateCorrectionVSyncClientWillNotCreateVsyncClientWhenRefreshRateIs60HZ {
|
||||
id mockDisplayLinkManager = [OCMockObject mockForClass:[DisplayLinkManager class]];
|
||||
double maxFrameRate = 60;
|
||||
[[[mockDisplayLinkManager stub] andReturnValue:@(maxFrameRate)] displayRefreshRate];
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
[viewController createTouchRateCorrectionVSyncClientIfNeeded];
|
||||
XCTAssertNil(viewController.touchRateCorrectionVSyncClient);
|
||||
}
|
||||
|
||||
- (void)testTriggerTouchRateCorrectionVSyncClientCorrectly {
|
||||
id mockDisplayLinkManager = [OCMockObject mockForClass:[DisplayLinkManager class]];
|
||||
double maxFrameRate = 120;
|
||||
[[[mockDisplayLinkManager stub] andReturnValue:@(maxFrameRate)] displayRefreshRate];
|
||||
FlutterEngine* engine = [[FlutterEngine alloc] init];
|
||||
[engine runWithEntrypoint:nil];
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
|
||||
nibName:nil
|
||||
bundle:nil];
|
||||
[viewController loadView];
|
||||
[viewController viewDidLoad];
|
||||
|
||||
VSyncClient* client = viewController.touchRateCorrectionVSyncClient;
|
||||
CADisplayLink* link = [client getDisplayLink];
|
||||
|
||||
UITouch* fakeTouchBegan = [[UITouch alloc] init];
|
||||
fakeTouchBegan.phase = UITouchPhaseBegan;
|
||||
|
||||
UITouch* fakeTouchMove = [[UITouch alloc] init];
|
||||
fakeTouchMove.phase = UITouchPhaseMoved;
|
||||
|
||||
UITouch* fakeTouchEnd = [[UITouch alloc] init];
|
||||
fakeTouchEnd.phase = UITouchPhaseEnded;
|
||||
|
||||
UITouch* fakeTouchCancelled = [[UITouch alloc] init];
|
||||
fakeTouchCancelled.phase = UITouchPhaseCancelled;
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc] initWithObjects:fakeTouchBegan, nil]];
|
||||
XCTAssertFalse(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc] initWithObjects:fakeTouchEnd, nil]];
|
||||
XCTAssertTrue(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc] initWithObjects:fakeTouchMove, nil]];
|
||||
XCTAssertFalse(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc] initWithObjects:fakeTouchCancelled, nil]];
|
||||
XCTAssertTrue(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc]
|
||||
initWithObjects:fakeTouchBegan, fakeTouchEnd, nil]];
|
||||
XCTAssertFalse(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc] initWithObjects:fakeTouchEnd,
|
||||
fakeTouchCancelled, nil]];
|
||||
XCTAssertTrue(link.isPaused);
|
||||
|
||||
[viewController
|
||||
triggerTouchRateCorrectionIfNeeded:[[NSSet alloc]
|
||||
initWithObjects:fakeTouchMove, fakeTouchEnd, nil]];
|
||||
XCTAssertFalse(link.isPaused);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -116,4 +116,23 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(std::string name) {
|
||||
[vsyncClient release];
|
||||
}
|
||||
|
||||
- (void)testAwaitAndPauseWillWorkCorrectly {
|
||||
auto thread_task_runner = CreateNewThread("VsyncWaiterIosTest");
|
||||
VSyncClient* vsyncClient = [[[VSyncClient alloc]
|
||||
initWithTaskRunner:thread_task_runner
|
||||
callback:[](std::unique_ptr<flutter::FrameTimingsRecorder> recorder) {}]
|
||||
autorelease];
|
||||
|
||||
CADisplayLink* link = [vsyncClient getDisplayLink];
|
||||
XCTAssertTrue(link.isPaused);
|
||||
|
||||
[vsyncClient await];
|
||||
XCTAssertFalse(link.isPaused);
|
||||
|
||||
[vsyncClient pause];
|
||||
XCTAssertTrue(link.isPaused);
|
||||
|
||||
[vsyncClient release];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -45,6 +45,8 @@
|
||||
|
||||
- (void)await;
|
||||
|
||||
- (void)pause;
|
||||
|
||||
- (void)invalidate;
|
||||
|
||||
- (double)getRefreshRate;
|
||||
|
||||
@@ -97,6 +97,10 @@ double VsyncWaiterIOS::GetRefreshRate() const {
|
||||
display_link_.get().paused = NO;
|
||||
}
|
||||
|
||||
- (void)pause {
|
||||
display_link_.get().paused = YES;
|
||||
}
|
||||
|
||||
- (void)onDisplayLink:(CADisplayLink*)link {
|
||||
TRACE_EVENT0("flutter", "VSYNC");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user