From 4cae6fa29a07a238273453cef9d18e4171d430e8 Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Fri, 20 Dec 2013 01:59:06 +0200 Subject: Add tiles per second counter (averaged over last five seconds) Change-Id: I11e282c1a72dbc6b41a5a89229065983b41eb65b --- .../TiledLibreOffice/TiledLibreOffice/TiledView.m | 63 +++++++++++++++++++++- .../TiledLibreOffice/TiledLibreOffice/View.h | 2 + .../TiledLibreOffice/TiledLibreOffice/View.m | 17 ++++++ 3 files changed, 80 insertions(+), 2 deletions(-) (limited to 'ios/experimental/TiledLibreOffice') diff --git a/ios/experimental/TiledLibreOffice/TiledLibreOffice/TiledView.m b/ios/experimental/TiledLibreOffice/TiledLibreOffice/TiledView.m index 57a13fb2c723..3756cd244d02 100644 --- a/ios/experimental/TiledLibreOffice/TiledLibreOffice/TiledView.m +++ b/ios/experimental/TiledLibreOffice/TiledLibreOffice/TiledView.m @@ -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 diff --git a/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.h b/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.h index 3043632d03c9..041471d40071 100644 --- a/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.h +++ b/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.h @@ -10,6 +10,8 @@ @interface View : UIScrollView +@property UILabel *tpsLabel; + @end // vim:set shiftwidth=4 softtabstop=4 expandtab: diff --git a/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.m b/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.m index 9291b4561369..8f4741b397f7 100644 --- a/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.m +++ b/ios/experimental/TiledLibreOffice/TiledLibreOffice/View.m @@ -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: -- cgit