Add tiles per second counter (averaged over last five seconds)

Change-Id: I11e282c1a72dbc6b41a5a89229065983b41eb65b
This commit is contained in:
Tor Lillqvist
2013-12-20 01:59:06 +02:00
parent 18397efd14
commit 4cae6fa29a
3 changed files with 80 additions and 2 deletions

View File

@@ -19,6 +19,60 @@
@implementation TiledView
static const int NTIMESTAMPS = 100;
static const CFTimeInterval AVERAGINGTIME = 5;
static CFTimeInterval tileTimestamps[NTIMESTAMPS];
static int curFirstTimestamp = 0;
static int curNextTimestamp = 0;
static void dropOldTimestamps(CFTimeInterval now)
{
// Drop too old timestamps
while (curFirstTimestamp != curNextTimestamp && now - tileTimestamps[curFirstTimestamp] >= AVERAGINGTIME)
curFirstTimestamp = (curFirstTimestamp + 1) % NTIMESTAMPS;
}
static void updateTilesPerSecond(UILabel *label)
{
int n = (curNextTimestamp < curFirstTimestamp) ?
(NTIMESTAMPS - (curFirstTimestamp - curNextTimestamp))
: ((curNextTimestamp - curFirstTimestamp));
// NSLog(@"first:%d next:%d n:%d", curFirstTimestamp, curNextTimestamp, n);
double tps = n/AVERAGINGTIME;
[label setText:[NSString stringWithFormat:@"%.0f tiles/second", tps]];
}
- (void)didRenderTile
{
CFTimeInterval now = CACurrentMediaTime();
@synchronized(self) {
dropOldTimestamps(now);
// Add new timestamp
tileTimestamps[curNextTimestamp] = now;
// Let next added replace newest if array full
if (curFirstTimestamp != (curNextTimestamp + 1) % NTIMESTAMPS)
curNextTimestamp = (curNextTimestamp + 1) % NTIMESTAMPS;
updateTilesPerSecond(((View *) [self superview]).tpsLabel);
}
}
- (void)updateTilesPerSecond
{
CFTimeInterval now = CACurrentMediaTime();
@synchronized(self) {
dropOldTimestamps(now);
updateTilesPerSecond(((View *) [self superview]).tpsLabel);
}
}
- (id)initWithFrame:(CGRect)frame scale:(CGFloat)scale maxZoom:(int)maxZoom
{
self = [super initWithFrame:frame];
@@ -28,6 +82,8 @@
catl.tileSize = CGSizeMake(512, 512);
catl.levelsOfDetail = log2(maxZoom) + 1;
catl.levelsOfDetailBias = catl.levelsOfDetail - 1;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTilesPerSecond) userInfo:nil repeats:YES];
}
return self;
}
@@ -44,8 +100,9 @@
// expected it to be called with a bbox of 256x256.
CGRect bb = CGContextGetClipBoundingBox(ctx);
double zoomScale = [(View *) [self superview] zoomScale];
CATiledLayer *catl = (CATiledLayer*) [self layer];
// double zoomScale = [(View *) [self superview] zoomScale];
// CATiledLayer *catl = (CATiledLayer*) [self layer];
CGContextSaveGState(ctx);
@@ -70,6 +127,8 @@
CGPointMake(bb.origin.x/self.scale, bb.origin.y/self.scale),
CGSizeMake(bb.size.width/self.scale, bb.size.height/self.scale));
[self didRenderTile];
CGContextRestoreGState(ctx);
// I am a bit confused about what tiles exactly I am drawing, so

View File

@@ -10,6 +10,8 @@
@interface View : UIScrollView <UIScrollViewDelegate>
@property UILabel *tpsLabel;
@end
// vim:set shiftwidth=4 softtabstop=4 expandtab:

View File

@@ -37,6 +37,14 @@
self.subView = [[TiledView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width*docAspectRatio) scale:widthScale maxZoom:MAXZOOM];
[self addSubview:self.subView];
UILabel *tpsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 40)];
[tpsLabel setFont:[UIFont systemFontOfSize:38]];
[tpsLabel setBackgroundColor: [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]];
[tpsLabel setTextColor: [UIColor colorWithRed:1 green:1 blue:0 alpha:1]];
[tpsLabel setTextAlignment: NSTextAlignmentRight];
[self addSubview:tpsLabel];
self.tpsLabel = tpsLabel;
}
return self;
}
@@ -46,6 +54,15 @@
return self.subView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = ((View*) scrollView).tpsLabel.frame;
frame.origin.x = 20 + scrollView.contentOffset.x;
frame.origin.y = 20 + scrollView.contentOffset.y;
((View *) scrollView).tpsLabel.frame = frame;
}
@end
// vim:set shiftwidth=4 softtabstop=4 expandtab: