From f1878b0f90c39f6a52aa6b7295da2ea9ca5bec61 Mon Sep 17 00:00:00 2001 From: luckysmg <2539699336@qq.com> Date: Thu, 8 Sep 2022 06:36:21 +0800 Subject: [PATCH] [iOS] Avoid jitter and laggy when user is dragging on iOS Promotion devices (flutter/engine#35592) --- .../framework/Source/FlutterViewController.mm | 72 +++++++++++ .../Source/FlutterViewControllerTest.mm | 13 ++ .../Source/FlutterViewControllerTest_mrc.mm | 117 ++++++++++++++++++ .../framework/Source/VsyncWaiterIosTest.mm | 19 +++ .../ios/framework/Source/vsync_waiter_ios.h | 2 + .../ios/framework/Source/vsync_waiter_ios.mm | 4 + 6 files changed, 227 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 5aaeb546d5..8719855a7e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -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(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 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 { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index d3f4de66f8..cbdcf3cf69 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -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]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest_mrc.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest_mrc.mm index f4cc21706e..e18b0b55c9 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest_mrc.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest_mrc.mm @@ -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 diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm index 4ac0529808..15d3f6df65 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm @@ -116,4 +116,23 @@ fml::RefPtr 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 recorder) {}] + autorelease]; + + CADisplayLink* link = [vsyncClient getDisplayLink]; + XCTAssertTrue(link.isPaused); + + [vsyncClient await]; + XCTAssertFalse(link.isPaused); + + [vsyncClient pause]; + XCTAssertTrue(link.isPaused); + + [vsyncClient release]; +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h index 9185fb0097..8178f845b8 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h @@ -45,6 +45,8 @@ - (void)await; +- (void)pause; + - (void)invalidate; - (double)getRefreshRate; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm index 439ba868ff..63954c9c59 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.mm @@ -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");