summaryrefslogtreecommitdiff
path: root/platform/ios/app/MBXFrameTimeGraphView.m
blob: e881eb9f4832414b7df8a9872e3a7ee6bd44b8a7 (plain)
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
136
137
138
139
140
141
142
143
144
145
#import "MBXFrameTimeGraphView.h"

const CGFloat MBXFrameTimeExaggeration = 4.f * 1000.f;
const CGFloat MBXFrameTimeBarWidth = 4.f;

@interface MBXFrameTimeGraphView ()

@property (nonatomic) CAScrollLayer *scrollLayer;
@property (nonatomic) CAShapeLayer *thresholdLayer;
@property (nonatomic) CGFloat currentX;
@property (nonatomic) NSMutableArray<CAShapeLayer *> *barLayers;

@property (nonatomic) UIColor *safeColor;
@property (nonatomic) UIColor *warningColor;
@property (nonatomic) UIColor *dangerColor;

@end

@implementation MBXFrameTimeGraphView

- (instancetype)init {
    if (self = [super init]) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    self.userInteractionEnabled = NO;
    self.layer.opacity = 0.9f;

    self.scrollLayer = [CAScrollLayer layer];
    self.scrollLayer.scrollMode = kCAScrollHorizontally;
    self.scrollLayer.masksToBounds = YES;
    [self.layer addSublayer:self.scrollLayer];

    self.thresholdLayer = [CAShapeLayer layer];
    self.thresholdLayer.fillColor = [UIColor darkGrayColor].CGColor;
    [self.layer addSublayer:self.thresholdLayer];

    self.barLayers = [NSMutableArray array];

    self.safeColor = [UIColor colorWithRed:(CGFloat)(0.f/255.f) green:(CGFloat)(190.f/255.f) blue:(CGFloat)(123.f/255.f) alpha:1.f];
    self.warningColor = [UIColor colorWithRed:(CGFloat)(255.f/255.f) green:(CGFloat)(154.f/255.f) blue:(CGFloat)(82.f/255.f) alpha:1.f];
    self.dangerColor = [UIColor colorWithRed:(CGFloat)(255.f/255.f) green:(CGFloat)(91.f/255.f) blue:(CGFloat)(86.f/255.f) alpha:1.f];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    if (!CGRectEqualToRect(self.scrollLayer.frame, self.bounds)) {
        self.scrollLayer.frame = self.bounds;

        CGRect thresholdLineRect = CGRectMake(0, self.frame.size.height - [self renderDurationTargetMilliseconds], self.frame.size.width, 1);
        
        {
            CGPathRef path = CGPathCreateWithRect(thresholdLineRect, nil);        
            self.thresholdLayer.path = path;
            CGPathRelease(path);
        }
    }
}

- (void)updatePathWithFrameDuration:(CFTimeInterval)frameDuration {
    [CATransaction begin];
    [CATransaction setDisableActions:YES];

    self.currentX += MBXFrameTimeBarWidth;

    CAShapeLayer *bar = [self barWithFrameDuration:frameDuration];
    bar.position = CGPointMake(self.currentX, self.frame.size.height);

    [self.scrollLayer addSublayer:bar];
    [self.barLayers addObject:bar];

    [self.scrollLayer scrollToPoint:CGPointMake(self.currentX - self.frame.size.width, 0)];

    [self removeStaleBars];

    [CATransaction commit];
}

- (CGFloat)renderDurationTargetMilliseconds {
    CGFloat maximumFramesPerSecond;
    if (@available(iOS 10.3, *)) {
        maximumFramesPerSecond = UIScreen.mainScreen.maximumFramesPerSecond;
    } else {
        // Not always strictly accurate, but works as an expedient approximation.
        maximumFramesPerSecond = 60;
    }

    CGFloat target = (1.0 / maximumFramesPerSecond) * MBXFrameTimeExaggeration;
    return [self roundedFloat:target];
}

- (CGFloat)roundedFloat:(CGFloat)f {
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
    CGFloat scaleFactor = [UIScreen mainScreen].nativeScale;
#elif TARGET_OS_MAC
    CGFloat scaleFactor = [NSScreen mainScreen].backingScaleFactor;
#endif
    return round(f * scaleFactor) / scaleFactor;
}

- (CAShapeLayer *)barWithFrameDuration:(CFTimeInterval)frameDuration {
    CAShapeLayer *bar = [CAShapeLayer layer];

    CGRect barRect = CGRectMake(0, 0, MBXFrameTimeBarWidth, -(fminf(frameDuration * MBXFrameTimeExaggeration, self.frame.size.height)));
    UIBezierPath *barPath = [UIBezierPath bezierPathWithRect:barRect];
    bar.path = barPath.CGPath;
    bar.fillColor = [self colorForFrameDuration:frameDuration].CGColor;

    return bar;
}

- (UIColor *)colorForFrameDuration:(CFTimeInterval)frameDuration {
    CGFloat renderDurationTargetMilliseconds = [self renderDurationTargetMilliseconds];
    frameDuration *= MBXFrameTimeExaggeration;

    if (frameDuration < renderDurationTargetMilliseconds && frameDuration > (renderDurationTargetMilliseconds * 0.75)) {
        return self.warningColor;
    } else if (frameDuration > renderDurationTargetMilliseconds) {
        return self.dangerColor;
    } else {
        return self.safeColor;
    }
}

- (void)removeStaleBars {
    if (self.barLayers.count > (self.frame.size.width / MBXFrameTimeBarWidth * 3)) {
        NSRange staleBarsRange = NSMakeRange(0, self.frame.size.width / MBXFrameTimeBarWidth);
        NSArray *staleBars = [self.barLayers subarrayWithRange:staleBarsRange];
        [staleBars makeObjectsPerformSelector:@selector(removeFromSuperlayer)];
        [self.barLayers removeObjectsInRange:staleBarsRange];
    }
}

@end