|
| 1 | +// |
| 2 | +// RRFPSBar.m |
| 3 | +// |
| 4 | +// Created by Rolandas Razma on 07/03/2013. |
| 5 | +// Copyright 2013 Rolandas Razma. All rights reserved. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person |
| 8 | +// obtaining a copy of this software and associated documentation |
| 9 | +// files (the "Software"), to deal in the Software without |
| 10 | +// restriction, including without limitation the rights to use, |
| 11 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the |
| 13 | +// Software is furnished to do so, subject to the following |
| 14 | +// conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be |
| 17 | +// included in all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 21 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 23 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 24 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 25 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 27 | +// |
| 28 | + |
| 29 | + |
| 30 | +#import "RRFPSBar.h" |
| 31 | +#import <QuartzCore/QuartzCore.h> |
| 32 | + |
| 33 | + |
| 34 | +@implementation RRFPSBar { |
| 35 | + CADisplayLink *_displayLink; |
| 36 | + NSUInteger _historyDTLength; |
| 37 | + NSUInteger _maxHistoryDTLength; |
| 38 | + CFTimeInterval *_historyDT; |
| 39 | + CFTimeInterval _displayLinkTickTimeLast; |
| 40 | + CFTimeInterval _lastUIUpdateTime; |
| 41 | + |
| 42 | + CATextLayer *_fpsTextLayer; |
| 43 | + CAShapeLayer *_linesLayer; |
| 44 | + CAShapeLayer *_chartLayer; |
| 45 | + |
| 46 | + BOOL _showsAverage; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +#pragma mark - |
| 51 | +#pragma mark NSObject |
| 52 | + |
| 53 | + |
| 54 | +- (void)dealloc { |
| 55 | + [_displayLink setPaused:YES]; |
| 56 | + [_displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; |
| 57 | + free(_historyDT); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +- (id)init { |
| 62 | + if( (self = [super initWithFrame:[[UIApplication sharedApplication] statusBarFrame]]) ){ |
| 63 | + |
| 64 | + _maxHistoryDTLength = (NSInteger)CGRectGetWidth(self.bounds); |
| 65 | + _historyDT = malloc(sizeof(CFTimeInterval) * _maxHistoryDTLength); |
| 66 | + _historyDTLength = 0; |
| 67 | + _displayLinkTickTimeLast= CACurrentMediaTime(); |
| 68 | + |
| 69 | + [self setWindowLevel: UIWindowLevelStatusBar +1.0f]; |
| 70 | + [self setBackgroundColor:[UIColor clearColor]]; |
| 71 | + |
| 72 | + [[NSNotificationCenter defaultCenter] addObserver: self |
| 73 | + selector: @selector(applicationDidBecomeActiveNotification) |
| 74 | + name: UIApplicationDidBecomeActiveNotification |
| 75 | + object: nil]; |
| 76 | + |
| 77 | + [[NSNotificationCenter defaultCenter] addObserver: self |
| 78 | + selector: @selector(applicationWillResignActiveNotification) |
| 79 | + name: UIApplicationWillResignActiveNotification |
| 80 | + object: nil]; |
| 81 | + |
| 82 | + // Track FPS using display link |
| 83 | + _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick)]; |
| 84 | + [_displayLink setPaused:YES]; |
| 85 | + [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes]; |
| 86 | + |
| 87 | + // Lines Layer |
| 88 | + _linesLayer = [CAShapeLayer layer]; |
| 89 | + [_linesLayer setFrame: self.bounds]; |
| 90 | + [_linesLayer setStrokeColor:[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.5f].CGColor]; |
| 91 | + [_linesLayer setContentsScale: [UIScreen mainScreen].scale]; |
| 92 | + |
| 93 | + UIBezierPath *path = [UIBezierPath bezierPath]; |
| 94 | + [path moveToPoint:CGPointMake(0.0f, self.frame.size.height)]; |
| 95 | + [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)]; |
| 96 | + [path closePath]; |
| 97 | + |
| 98 | + [_linesLayer setPath:path.CGPath]; |
| 99 | + |
| 100 | + [self.layer addSublayer:_linesLayer]; |
| 101 | + |
| 102 | + // Chart Layer |
| 103 | + _chartLayer = [CAShapeLayer layer]; |
| 104 | + [_chartLayer setFrame: self.bounds]; |
| 105 | + [_chartLayer setStrokeColor: [UIColor redColor].CGColor]; |
| 106 | + [_chartLayer setContentsScale: [UIScreen mainScreen].scale]; |
| 107 | + [self.layer addSublayer:_chartLayer]; |
| 108 | + |
| 109 | + // Info Layer |
| 110 | + _fpsTextLayer = [CATextLayer layer]; |
| 111 | + [_fpsTextLayer setFrame: CGRectMake(CGRectGetWidth(self.bounds) - 120, 0, 120.0f, self.bounds.size.height)]; |
| 112 | + [_fpsTextLayer setFont:CGFontCreateWithFontName((CFStringRef)[UIFont boldSystemFontOfSize:12].fontName)]; |
| 113 | + [_fpsTextLayer setFontSize: 12.0f]; |
| 114 | + [_fpsTextLayer setForegroundColor: [UIColor redColor].CGColor]; |
| 115 | + [_fpsTextLayer setAlignmentMode:kCAAlignmentCenter]; |
| 116 | + |
| 117 | + [_fpsTextLayer setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5].CGColor]; |
| 118 | + [_fpsTextLayer setContentsScale: [UIScreen mainScreen].scale]; |
| 119 | + [self.layer addSublayer:_fpsTextLayer]; |
| 120 | + |
| 121 | + // Draw asynchronously on iOS6+ |
| 122 | + if( [_chartLayer respondsToSelector:@selector(setDrawsAsynchronously:)] ){ |
| 123 | + [_linesLayer setDrawsAsynchronously:YES]; |
| 124 | + [_chartLayer setDrawsAsynchronously:YES]; |
| 125 | + [_fpsTextLayer setDrawsAsynchronously:YES]; |
| 126 | + } |
| 127 | + |
| 128 | + [self setDesiredChartUpdateInterval: 1.0f /60.0f]; |
| 129 | + } |
| 130 | + return self; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +#pragma mark - |
| 135 | +#pragma mark RRFPSBar |
| 136 | + |
| 137 | + |
| 138 | ++ (RRFPSBar *)sharedInstance { |
| 139 | + static RRFPSBar *_sharedInstance; |
| 140 | + static dispatch_once_t onceToken; |
| 141 | + dispatch_once(&onceToken, ^{ |
| 142 | + _sharedInstance = [[RRFPSBar alloc] init]; |
| 143 | + if([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) |
| 144 | + _sharedInstance.rootViewController = [UIViewController new]; // iOS 9 requires rootViewController for any window |
| 145 | + }); |
| 146 | + return _sharedInstance; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +- (void)applicationDidBecomeActiveNotification { |
| 151 | + [_displayLink setPaused:NO]; |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | +- (void)applicationWillResignActiveNotification { |
| 156 | + [_displayLink setPaused:YES]; |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +- (void)displayLinkTick { |
| 161 | + |
| 162 | + // Shift up the buffer |
| 163 | + for (NSUInteger i = _historyDTLength; i >= 1; i-- ) { |
| 164 | + _historyDT[i] = _historyDT[i -1]; |
| 165 | + } |
| 166 | + |
| 167 | + // Store new state |
| 168 | + _historyDT[0] = _displayLink.timestamp -_displayLinkTickTimeLast; |
| 169 | + |
| 170 | + // Update length if there is more place |
| 171 | + if ( _historyDTLength < _maxHistoryDTLength - 1) _historyDTLength++; |
| 172 | + |
| 173 | + // Store last timestamp |
| 174 | + _displayLinkTickTimeLast = _displayLink.timestamp; |
| 175 | + |
| 176 | + // Update UI |
| 177 | + CFTimeInterval timeSinceLastUIUpdate = _displayLinkTickTimeLast -_lastUIUpdateTime; |
| 178 | + if( _historyDT[0] < 0.1f && timeSinceLastUIUpdate >= _desiredChartUpdateInterval ){ |
| 179 | + [self updateChartAndText]; |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | + |
| 184 | +- (void)updateChartAndText { |
| 185 | + |
| 186 | + UIBezierPath *path = [UIBezierPath bezierPath]; |
| 187 | + [path moveToPoint:CGPointZero]; |
| 188 | + |
| 189 | + CFTimeInterval maxDT = CGFLOAT_MIN; |
| 190 | + CFTimeInterval avgDT = 0.0f; |
| 191 | + |
| 192 | + for( NSUInteger i=0; i<=_historyDTLength; i++ ){ |
| 193 | + maxDT = MAX(maxDT, _historyDT[i]); |
| 194 | + avgDT += _historyDT[i]; |
| 195 | + |
| 196 | + CGFloat fraction = roundf(1.0f /(float)_historyDT[i]) /60.0f; |
| 197 | + CGFloat y = _chartLayer.frame.size.height -_chartLayer.frame.size.height *fraction; |
| 198 | + y = MAX(0.0f, MIN(_chartLayer.frame.size.height, y)); |
| 199 | + |
| 200 | + [path addLineToPoint:CGPointMake(i +1.0f, y)]; |
| 201 | + } |
| 202 | + |
| 203 | + [path addLineToPoint:CGPointMake(_historyDTLength, 0)]; |
| 204 | + |
| 205 | + avgDT /= _historyDTLength; |
| 206 | + _chartLayer.path = path.CGPath; |
| 207 | + |
| 208 | + CFTimeInterval minFPS = roundf(1.0f /(float)maxDT); |
| 209 | + CFTimeInterval avgFPS = roundf(1.0f /(float)avgDT); |
| 210 | + |
| 211 | + NSString *text; |
| 212 | + if( _showsAverage ){ |
| 213 | + text = [NSString stringWithFormat:@"low: %.f | avg: %.f", minFPS, avgFPS]; |
| 214 | + } else { |
| 215 | + text = [NSString stringWithFormat:@"low %.f", minFPS]; |
| 216 | + } |
| 217 | + |
| 218 | + [_fpsTextLayer setString: text]; |
| 219 | + |
| 220 | + _lastUIUpdateTime = _displayLinkTickTimeLast; |
| 221 | +} |
| 222 | + |
| 223 | + |
| 224 | +@end |
0 commit comments