forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTFPSGraph.mm
More file actions
135 lines (110 loc) · 3.31 KB
/
RCTFPSGraph.mm
File metadata and controls
135 lines (110 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTFPSGraph.h>
#import <React/RCTAssert.h>
#import <React/RCTUIKit.h> // [macOS]
#if TARGET_OS_OSX // [macOS
#import <QuartzCore/QuartzCore.h>
#endif // macOS]
#if RCT_DEV
@interface RCTFPSGraph ()
@property (nonatomic, strong, readonly) CAShapeLayer *graph;
@property (nonatomic, strong, readonly) RCTUILabel *label; // [macOS]
@end
@implementation RCTFPSGraph {
CAShapeLayer *_graph;
RCTUILabel *_label; // [macOS]
CGFloat *_frames;
RCTUIColor *_color; // [macOS]
NSTimeInterval _prevTime;
NSUInteger _frameCount;
NSUInteger _FPS;
NSUInteger _maxFPS;
NSUInteger _minFPS;
NSUInteger _length;
NSUInteger _height;
CGFloat _scale;
}
- (instancetype)initWithFrame:(CGRect)frame color:(RCTUIColor *)color // [macOS]
{
if ((self = [super initWithFrame:frame])) {
#if TARGET_OS_OSX // [macOS
self.wantsLayer = YES;
#endif // macOS]
_frameCount = -1;
_prevTime = -1;
_maxFPS = 0;
_minFPS = 60;
_length = (NSUInteger)floor(frame.size.width);
_height = (NSUInteger)floor(frame.size.height);
_scale = 60.0 / (CGFloat)_height;
_frames = (CGFloat *)calloc(sizeof(CGFloat), _length);
_color = color;
[self.layer addSublayer:self.graph];
[self addSubview:self.label];
}
return self;
}
- (void)dealloc
{
free(_frames);
}
RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
- (CAShapeLayer *)graph
{
if (!_graph) {
_graph = [CAShapeLayer new];
_graph.frame = self.bounds;
_graph.backgroundColor = [_color colorWithAlphaComponent:0.2].CGColor;
_graph.fillColor = _color.CGColor;
}
return _graph;
}
- (RCTUILabel *)label // [macOS]
{
if (!_label) {
_label = [[RCTUILabel alloc] initWithFrame:self.bounds];
_label.font = [UIFont boldSystemFontOfSize:13];
_label.textAlignment = NSTextAlignmentCenter;
}
return _label;
}
- (void)onTick:(NSTimeInterval)timestamp
{
_frameCount++;
if (_prevTime == -1) {
_prevTime = timestamp;
} else if (timestamp - _prevTime >= 1) {
_FPS = round((double)_frameCount / (timestamp - _prevTime));
_minFPS = MIN(_minFPS, _FPS);
_maxFPS = MAX(_maxFPS, _FPS);
dispatch_async(dispatch_get_main_queue(), ^{
self->_label.text = [NSString stringWithFormat:@"%lu", (unsigned long)self->_FPS];
});
CGFloat previousScale = _scale;
CGFloat targetFps = MAX(_maxFPS, 60.0);
_scale = targetFps / (CGFloat)_height;
for (NSUInteger i = 0; i < _length - 1; i++) {
// 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;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, (CGFloat)_height);
for (NSUInteger i = 0; i < _length; i++) {
CGPathAddLineToPoint(path, NULL, (CGFloat)i, (double)_height - _frames[i]);
}
CGPathAddLineToPoint(path, NULL, (CGFloat)_length - 1, (CGFloat)_height);
_graph.path = path;
CGPathRelease(path);
_prevTime = timestamp;
_frameCount = 0;
}
}
@end
#endif