From e69e0a33ddcaca35e0032d81197d06d1a458e5b9 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Fri, 27 May 2022 22:33:26 +0400 Subject: [PATCH] Fix fps --- submodules/Display/Source/ListView.swift | 7 ++++ .../UIViewController+Navigation.h | 6 ++++ .../UIViewController+Navigation.m | 32 +++++++++++++++++-- .../AnimatableLayers/LOTLayerContainer.m | 4 +++ .../Classes/Private/LOTAnimationView.m | 4 +++ 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/submodules/Display/Source/ListView.swift b/submodules/Display/Source/ListView.swift index ca4750161d..76da5e4752 100644 --- a/submodules/Display/Source/ListView.swift +++ b/submodules/Display/Source/ListView.swift @@ -807,6 +807,12 @@ open class ListView: ASDisplayNode, UIScrollViewAccessibilityDelegate, UIGesture } public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { + if #available(iOS 15.0, *) { + if let scrollDisplayLink = self.scroller.value(forKey: "_scrollHeartbeat") as? CADisplayLink { + let _ = scrollDisplayLink + } + } + self.isDragging = false if decelerate { self.lastContentOffsetTimestamp = CACurrentMediaTime() @@ -886,6 +892,7 @@ open class ListView: ASDisplayNode, UIScrollViewAccessibilityDelegate, UIGesture public func scrollViewDidScroll(_ scrollView: UIScrollView) { self.updateScrollViewDidScroll(scrollView, synchronous: self.defaultToSynchronousTransactionWhileScrolling) + scrollView.fixScrollDisplayLink() } private var generalAccumulatedDeltaY: CGFloat = 0.0 diff --git a/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.h b/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.h index 6ef3dd6b6a..08303289d3 100644 --- a/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.h +++ b/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.h @@ -48,3 +48,9 @@ void applyKeyboardAutocorrection(UITextView * _Nonnull textView); @property (nonatomic, copy) UIInterfaceOrientationMask (^ _Nullable supportedOrientations)(void); @end + +@interface UIScrollView (FrameRateRangeOverride) + +- (void)fixScrollDisplayLink; + +@end diff --git a/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.m b/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.m index df1438bfce..302959802a 100644 --- a/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.m +++ b/submodules/UIKitRuntimeUtils/Source/UIKitRuntimeUtils/UIViewController+Navigation.m @@ -43,6 +43,7 @@ static const void *inputAccessoryHeightProviderKey = &inputAccessoryHeightProvid static const void *interactiveTransitionGestureRecognizerTestKey = &interactiveTransitionGestureRecognizerTestKey; static const void *UIViewControllerHintWillBePresentedInPreviewingContextKey = &UIViewControllerHintWillBePresentedInPreviewingContextKey; static const void *disablesInteractiveModalDismissKey = &disablesInteractiveModalDismissKey; +static const void *forceFullRefreshRateKey = &forceFullRefreshRateKey; static bool notyfyingShiftState = false; @@ -99,14 +100,41 @@ static bool notyfyingShiftState = false; @implementation CADisplayLink (FrameRateRangeOverride) - (void)_65087dc8_setPreferredFrameRateRange:(CAFrameRateRange)range API_AVAILABLE(ios(15.0)) { - float maxFps = [UIScreen mainScreen].maximumFramesPerSecond; - range = CAFrameRateRangeMake(maxFps, maxFps, maxFps); + if ([self associatedObjectForKey:forceFullRefreshRateKey] != nil) { + float maxFps = [UIScreen mainScreen].maximumFramesPerSecond; + range = CAFrameRateRangeMake(maxFps, maxFps, maxFps); + } [self _65087dc8_setPreferredFrameRateRange:range]; } @end +@implementation UIScrollView (FrameRateRangeOverride) + +- (void)fixScrollDisplayLink { + static NSString *scrollHeartbeatKey = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + scrollHeartbeatKey = [NSString stringWithFormat:@"_%@", @"scrollHeartbeat"]; + }); + + id value = [self valueForKey:scrollHeartbeatKey]; + if ([value isKindOfClass:[CADisplayLink class]]) { + CADisplayLink *displayLink = (CADisplayLink *)value; + if ([displayLink associatedObjectForKey:forceFullRefreshRateKey] == nil) { + [displayLink setAssociatedObject:@true forKey:forceFullRefreshRateKey]; + + if (@available(iOS 15.0, *)) { + float maxFps = [UIScreen mainScreen].maximumFramesPerSecond; + [displayLink setPreferredFrameRateRange:CAFrameRateRangeMake(maxFps, maxFps, maxFps)]; + } + } + } +} + +@end + @implementation UIViewController (Navigation) + (void)load diff --git a/submodules/lottie-ios/lottie-ios/Classes/AnimatableLayers/LOTLayerContainer.m b/submodules/lottie-ios/lottie-ios/Classes/AnimatableLayers/LOTLayerContainer.m index 12a3630f00..a931a329db 100644 --- a/submodules/lottie-ios/lottie-ios/Classes/AnimatableLayers/LOTLayerContainer.m +++ b/submodules/lottie-ios/lottie-ios/Classes/AnimatableLayers/LOTLayerContainer.m @@ -216,6 +216,10 @@ animationWithKeyPath:event]; theAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; theAnimation.fromValue = [[self presentationLayer] valueForKey:event]; + if (@available(iOS 15.0, *)) { + float maxFps = UIScreen.mainScreen.maximumFramesPerSecond; + [theAnimation setPreferredFrameRateRange:CAFrameRateRangeMake(maxFps, maxFps, maxFps)]; + } return theAnimation; } return [super actionForKey:event]; diff --git a/submodules/lottie-ios/lottie-ios/Classes/Private/LOTAnimationView.m b/submodules/lottie-ios/lottie-ios/Classes/Private/LOTAnimationView.m index 6dca8a4aba..4a55bad73c 100644 --- a/submodules/lottie-ios/lottie-ios/Classes/Private/LOTAnimationView.m +++ b/submodules/lottie-ios/lottie-ios/Classes/Private/LOTAnimationView.m @@ -363,6 +363,10 @@ static NSString * const kCompContainerAnimationKey = @"play"; } else { NSTimeInterval duration = (ABS(toEndFrame.floatValue - fromStartFrame.floatValue) / _sceneModel.framerate.floatValue); CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"currentFrame"]; + if (@available(iOS 15.0, *)) { + float maxFps = UIScreen.mainScreen.maximumFramesPerSecond; + [animation setPreferredFrameRateRange:CAFrameRateRangeMake(maxFps, maxFps, maxFps)]; + } animation.speed = _animationSpeed; animation.fromValue = fromStartFrame; animation.toValue = toEndFrame;