From 987dd6a35842acde9d540fc42dfe4a2f34fd2ddf Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Tue, 28 Feb 2023 01:27:38 -0800 Subject: [PATCH] fix: Support 120 FPS in `RCTFPSGraph` (#35543) Summary: Currently the `RCTFPSGraph` component is hardcoded/capped at a Frame Rate of 60. Since there are phones that support more than 60 FPS (newer iPhones can do 120 FPS), and there might be other use-cases for the RCTFPSGraph (I use it in VisionCamera to show Camera FPS), this PR changes the scale to also support higher FPS by adjusting it on the fly (when a new maximum arrives) ## Changelog [iOS] [Fixed] - Support 120 FPS or more in `RCTFPSGraph` Pull Request resolved: https://github.com/facebook/react-native/pull/35543 Test Plan: Before: ![IMG_1075](https://user-images.githubusercontent.com/15199031/205340761-12954d36-82dd-4102-868a-b7234fdfc21c.jpg) After: ![IMG_1074](https://user-images.githubusercontent.com/15199031/205340790-092bfa57-c291-418b-9ce3-2a8d2389436a.jpg) Reviewed By: rshest Differential Revision: D43573750 Pulled By: sammy-SC fbshipit-source-id: 7d64fcee35c0c29dfb618f1f02945584d1cab1e0 --- React/CoreModules/RCTFPSGraph.m | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/React/CoreModules/RCTFPSGraph.m b/React/CoreModules/RCTFPSGraph.m index 23ef7cf69e8..6beaf7d16f1 100644 --- a/React/CoreModules/RCTFPSGraph.m +++ b/React/CoreModules/RCTFPSGraph.m @@ -32,6 +32,7 @@ NSUInteger _minFPS; NSUInteger _length; NSUInteger _height; + CGFloat _scale; } - (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color @@ -43,6 +44,7 @@ _minFPS = 60; _length = (NSUInteger)floor(frame.size.width); _height = (NSUInteger)floor(frame.size.height); + _scale = 60.0 / (CGFloat)_height; _frames = calloc(sizeof(CGFloat), _length); _color = color; @@ -97,11 +99,14 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder) self->_label.text = [NSString stringWithFormat:@"%lu", (unsigned long)self->_FPS]; }); - CGFloat scale = 60.0 / (CGFloat)_height; + CGFloat previousScale = _scale; + CGFloat targetFps = MAX(_maxFPS, 60.0); + _scale = targetFps / (CGFloat)_height; for (NSUInteger i = 0; i < _length - 1; i++) { - _frames[i] = _frames[i + 1]; + // Move each Frame back one position and adjust to new scale (if there is a new scale) + _frames[i] = _frames[i + 1] * previousScale / _scale; } - _frames[_length - 1] = (double)_FPS / scale; + _frames[_length - 1] = (double)_FPS / _scale; CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, (CGFloat)_height);