I am curious about why the frametime follows a cyclic pattern and almost all time after every three or four frames is 16.94 ms and not 16.666. ? And sometimes a framtime is 16.39 and almost always are followed by 16.94.
Even if I comment out the draw-call and update-call in onDrawFrame this is the same. I am aware of that the the performance is not linear to the work the GPU has to do but why this cyclic behaviour and why cannot the framtime always be 16.666 or at the least very close. I measure the average framtime and its approx. 16.725 and this means that some frames are lost and I think I can see some glitches in the animation of my game.
#Override
public void onDrawFrame(GL10 unused) {
//GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//lm.getLevel().update();
//lm.getLevel().draw();
long stop = System.currentTimeMillis();
nbFrames++;
if (stop - start >= 1000) {
if (frameCntr > 300) {
float frameTime = (float) 1000 / nbFrames;
totFrameTime += frameTime;
meanDivisor++;
//System.out.println("" + (float) 1000 / nbFrames);
System.out.println("medel tid per frame = " + totFrameTime / meanDivisor + "ft = " + (float) 1000 / nbFrames);
}
nbFrames = 0;
start += 1000;
}
frameCntr++;
}
Important to note - I measure frametime, not frames per second.
m.out: medel tid per frame = 16.666666ft = 16.666666
08-16 19:47:54.184 18083-18153/app.gl.se.bricksbreaker170810 I/System.out:
08-16 19:47:58.178 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.760828ft = 16.949152
08-16 19:47:59.189 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.708344ft = 16.393442
08-16 19:48:00.188 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.703135ft = 16.666666
08-16 19:48:01.176 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.73047ft = 16.949152
08-16 19:48:02.181 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.72409ft = 16.666666
08-16 19:48:03.186 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.71887ft = 16.666666
08-16 19:48:04.189 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.714521ft = 16.666666
08-16 19:48:05.190 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.71084ft = 16.666666
08-16 19:48:06.177 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.727863ft = 16.949152
08-16 19:48:07.181 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723783ft = 16.666666
08-16 19:48:08.185 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.720213ft = 16.666666
08-16 19:48:09.190 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.717062ft = 16.666666
08-16 19:48:10.175 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.729958ft = 16.949152
08-16 19:48:11.179 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.726625ft = 16.666666
08-16 19:48:12.183 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723627ft = 16.666666
08-16 19:48:13.187 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.720913ft = 16.666666
08-16 19:48:14.190 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.718447ft = 16.666666
08-16 19:48:15.190 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.716196ft = 16.666666
08-16 19:48:16.179 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.725903ft = 16.949152
08-16 19:48:17.183 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723534ft = 16.666666
08-16 19:48:18.187 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.721346ft = 16.666666
08-16 19:48:19.190 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.71932ft = 16.666666
08-16 19:48:20.177 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.727528ft = 16.949152
08-16 19:48:21.180 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.72543ft = 16.666666
08-16 19:48:22.183 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.72347ft = 16.666666
08-16 19:48:23.187 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.721638ft = 16.666666
08-16 19:48:24.191 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.719921ft = 16.666666
08-16 19:48:25.175 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.726868ft = 16.949152
08-16 19:48:26.181 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.725098ft = 16.666666
08-16 19:48:27.182 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723429ft = 16.666666
08-16 19:48:28.186 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.721853ft = 16.666666
08-16 19:48:29.175 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.727997ft = 16.949152
08-16 19:48:30.178 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.726383ft = 16.666666
08-16 19:48:31.182 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.724852ft = 16.666666
08-16 19:48:32.185 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723398ft = 16.666666
08-16 19:48:33.189 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.722015ft = 16.666666
08-16 19:48:34.175 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.727423ft = 16.949152
08-16 19:48:35.176 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.726011ft = 16.666666
08-16 19:48:36.182 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.724663ft = 16.666666
08-16 19:48:37.185 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.723373ft = 16.666666
08-16 19:48:38.187 18083-18153/app.gl.se.bricksbreaker170810 I/System.out: medel tid per frame = 16.722141ft = 16.666666
DO I have a bottleneck in my app or is this average frametime of 16.72 normal?
As I mentioned above- I have commented out the draw and update of the game but the frametime is the same, that is still 16.725 so its no use to show the source of the draw and update of the game
Average frame time of 16.72 gives you 59.8Hz which is perfectly normal for a 60Hz consumer grade display and GPU. It is not uncommon for electronics to operate at varying frequencies. Just get some software for checking CPU, GPU and bus frequencies and you will know what I´m talking about.
Related
I am trying to split video into multiple photos, but when I use AVAssetImageGenerator I can only get photo for 0.0s, 0.5s, 0.1s etc
But how can I get e.g. 10 photos per second?
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
var times = [NSValue]()
for i in 0..<5 {
let t = Double(i) / 5 * 60
times.append(NSValue(time: CMTimeMake(Int64(t), 60)))
}
imageGenerator.generateCGImagesAsynchronously(forTimes: times) { (time, imageRef, time2, result, error) in
Resulting frame times
time time2
0.0 0.0
0.2 0.0
0.4 0.5005
0.6 0.5005
0.8 1.001
It's is fixed by
imageGenerator.requestedTimeToleranceAfter = CMTimeMake(1, 60)
imageGenerator.requestedTimeToleranceBefore = CMTimeMake(1, 60)
I'm making a macOS app and I've got this code used to rotate an image:
didSet {
let degreesToRotate = oldValue - phoneXRotation
phoneImageView.rotate(byDegrees: CGFloat(degreesToRotate))
degreesLabel.isHidden = false
degreesLabel.stringValue = "\(phoneXRotation)"
if phoneXRotation > 70 {
statusLabel.stringValue = "Phone orientation: Right Tilt"
} else if phoneXRotation < -70 {
statusLabel.stringValue = "Phone orientation: Left Tilt"
} else {
statusLabel.stringValue = "Phone orientation: Flat"
}
}
The app will randomly crash in an lldb error. If I comment out the third line that rotate the phone, I have no problems. The conversion from Int to CGFloat shouldn't crash. Any ideas?
Here is the stack trace:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x00007fff8989b44e AppKit`-[NSApplication _crashOnException:] + 109
frame #1: 0x00007fff8996ea32 AppKit`__37+[NSDisplayCycle currentDisplayCycle]_block_invoke.31 + 708
frame #2: 0x00007fff8ba62d37 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #3: 0x00007fff8ba62ca7 CoreFoundation`__CFRunLoopDoObservers + 391
frame #4: 0x00007fff8ba436d9 CoreFoundation`__CFRunLoopRun + 873
frame #5: 0x00007fff8ba43114 CoreFoundation`CFRunLoopRunSpecific + 420
frame #6: 0x00007fff8afa3ebc HIToolbox`RunCurrentEventLoopInMode + 240
frame #7: 0x00007fff8afa3cf1 HIToolbox`ReceiveNextEventCommon + 432
frame #8: 0x00007fff8afa3b26 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 71
frame #9: 0x00007fff8953ca54 AppKit`_DPSNextEvent + 1120
frame #10: 0x00007fff89cb87ee AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
frame #11: 0x00007fff895313db AppKit`-[NSApplication run] + 926
frame #12: 0x00007fff894fbe0e AppKit`NSApplicationMain + 1237
* frame #13: 0x000000010000691d Dataspeed Mac Project`main at AppDelegate.swift:12
frame #14: 0x00007fffa11f1235 libdyld.dylib`start + 1
frame #15: 0x00007fffa11f1235 libdyld.dylib`start + 1
My code below creates a Texture from an image name from file lookups in a plist like (Duck1_1,Duck1_2,Duck1_3,etc) it loops through them then adds them to a mutable array. The mutable array is then used to animate with the textures stored and played with an SKAction. This works fine for the animations until 30-40 are played then it crashes and says too many files open and it can't open the files anymore. Any ideas on how to stop this from happening?
thanks
//loadPlistForAnimationWithNames function
NSMutableArray *animationToReturn = [NSMutableArray array];
for(NSString *frameNumber in animationFrameNumbers) {
NSString *frameName = [NSString stringWithFormat:#"%#%#.png",animationFramePrefix,frameNumber];
if(frameName!=nil)
[animationToReturn addObject:[SKTexture textureWithImageNamed:frameName]];
}
SKAction *action=nil;
action=[SKAction animateWithTextures:animationToReturn timePerFrame:animationDelay resize:NO restore:NO];
SKAction *repeatAction=nil;
repeatAction = [SKAction repeatAction:action count:1];
[animationToReturn removeAllObjects];
action=nil;
animationFrameNumbers=nil;
return repeatAction;
//Call to loadPlistForAnimationWithNames and run animation
SKAction *action=[animalObject loadPlistForAnimationWithNames:#"duckAnim1" andClassName:NSStringFromClass([self class])];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:#"Duck1_1"];
sprite.position=CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
sprite.size=self.frame.size;
[sprite setZPosition:3];
[self addChild:sprite];
[sprite runAction:action];
Here is the Xcode error I get when it crashes:
: ImageIO: CGImageReadCreateDataWithMappedFile 'open' failed '/private/var/mobile/Containers/Bundle/Application/782E98C3-0452-43EF-BF4D-401773D5FDCD/WildNoizesLite.app/Duck1_1.png'
error = 24 (Too many open files)
Backtrace:
* thread #1: tid = 0x301c4, 0x00000001944ab270 libsystem_kernel.dylib__pthread_kill + 8, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00000001944ab270 libsystem_kernel.dylib__pthread_kill + 8
frame #1: 0x0000000194549170 libsystem_pthread.dylibpthread_kill + 112
frame #2: 0x0000000194422b18 libsystem_c.dylibabort + 112
frame #3: 0x00000001934b5418 libc++abi.dylibabort_message + 116
frame #4: 0x00000001934d4b8c libc++abi.dylibdefault_terminate_handler() + 304
frame #5: 0x0000000193ce83c0 libobjc.A.dylib_objc_terminate() + 128
frame #6: 0x00000001934d1bb4 libc++abi.dylibstd::__terminate(void (*)()) + 16
frame #7: 0x00000001934d173c libc++abi.dylib__cxa_rethrow + 144
frame #8: 0x0000000193ce8294 libobjc.A.dylibobjc_exception_rethrow + 44
frame #9: 0x0000000181ef5384 CoreFoundationCFRunLoopRunSpecific + 572
frame #10: 0x000000018b94b6fc GraphicsServicesGSEventRunModal + 168
frame #11: 0x0000000186af2f40 UIKitUIApplicationMain + 1488
* frame #12: 0x0000000100174fa0 Wild Noizesmain(argc=1, argv=0x000000016fd6f9d8) + 124 at main.m:16
frame #13: 0x0000000194392a08 libdyld.dylib`start + 4
I'm working on a menubar application and I've made a custom (borderless) NSWindow which I've added programmatically. For some reason when I try to resize the window, it freezes and I'm unable to resize the window and open the menu-bar menu. However I can still move the window but not click things in it. The weird thing is that there is no error message at all, even though I turned on breakpoints and NSZombieMode. So I've no idea what the problem could be.
// customWindow.m
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag {
// Using NSBorderlessWindowMask results in a window without a title bar.
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask | NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self != nil) {
if (![[SharedClass sharedManager] lastColor]) {
[self setBackgroundColor:[NSColor colorWithRed:(float)252/255 green:(float)249/255 blue:(float)151/255 alpha:1]];
[[SharedClass sharedManager] setLastColor:self.backgroundColor];
} else {
[self setBackgroundColor: [[SharedClass sharedManager] lastColor]];
}
[self setLevel:NSNormalWindowLevel];
// Start with no transparency for all drawing into the window
// Turn off opacity so that the parts of the window that are not drawn into are transparent.
[self setOpaque:NO];
// [self setBackgroundColor:[NSColor clearColor]];
[self setHasShadow:YES];
[self setMovableByWindowBackground:YES];
[self makeKeyAndOrderFront:theTextView];
panel = [NSColorPanel sharedColorPanel];
[panel setTarget:self];
[panel setColor:self.backgroundColor];
[panel setAction:#selector(colorUpdate:)];
[panel setShowsAlpha:YES];
[self setDelegate:self];
NSRect cFrame = [self frame];
NSView *theView = [[NSView alloc] initWithFrame:cFrame];
[self setContentView:theView];
[self setMinSize:CGSizeMake(100, 100)];
}
return self;
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize {
exit.frame = NSMakeRect(2,(frameSize.height-12), 10, 10);
palette.frame = NSMakeRect((frameSize.width-24),(frameSize.height-12), 10, 10);
locker.frame = NSMakeRect((frameSize.width-12),(frameSize.height-12), 10, 10);
theTextView.frame = NSRectFromCGRect(CGRectMake(0, 8, frameSize.width, (frameSize.height-20)));
return frameSize;
}
- (void)windowDidResize:(NSNotification *)notification{
NSLog(#"resized");
}
Stack trace :
0 DDHotKey 0x00000001000044ed -[CustomWindow windowDidResize:] + 61
1 CoreFoundation 0x00007fff9034245c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
2 CoreFoundation 0x00007fff90232634 _CFXNotificationPost + 3140
3 Foundation 0x00007fff8d7f59d1 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
4 AppKit 0x00007fff89117770 -[NSWindow _setFrameCommon:display:stashSize:] + 2885
5 AppKit 0x00007fff891d4b73 -[NSWindow setFrame:display:animate:] + 641
6 DDHotKey 0x000000010000a8c0 -[DDHotKeyAppDelegate setFrame:animated:] + 416
7 DDHotKey 0x00000001000091ae __53-[DDHotKeyAppDelegate applicationDidFinishLaunching:]_block_invoke + 2318
8 AppKit 0x00007fff895e5233 GlobalObserverHandler + 117
9 HIToolbox 0x00007fff88d0ab6c _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1260
10 HIToolbox 0x00007fff88d09fae _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 386
11 HIToolbox 0x00007fff88d09e22 SendEventToEventTargetWithOptions + 43
12 HIToolbox 0x00007fff88d46a9e _ZL29ToolboxEventDispatcherHandlerP25OpaqueEventHandlerCallRefP14OpaqueEventRefPv + 1762
13 HIToolbox 0x00007fff88d0b295 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 3093
14 HIToolbox 0x00007fff88d09fae _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 386
15 HIToolbox 0x00007fff88d1fcb6 SendEventToEventTarget + 40
16 AppKit 0x00007fff890d71a2 _DPSNextEvent + 3001
17 AppKit 0x00007fff890d5f68 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
18 AppKit 0x00007fff890cbbf3 -[NSApplication run] + 594
19 AppKit 0x00007fff89048354 NSApplicationMain + 1832
20 DDHotKey 0x0000000100007312 main + 34
21 libdyld.dylib 0x00007fff8bd335c9 start + 1
I maintain a legacy OSX application that instead of calling NSApplicationMain() to process events, runs an event loop on the main thread that looks like this:
...
do {
event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:distantPast
inMode: NSDefaultRunLoopMode dequeue:YES];
[self handleEvent:event];
}
} while(event != nil);
...
Everything works until I activate the application's menu. Once the menu is active, nextEventMatchingMask blocks forever. I can see in lldb that osx starts another modal event loop (carbon?) which causes the cococa's one to block:
frame #2: 0x00007fff92748eb4 CoreFoundation`__CFRunLoopServiceMachPort + 212
frame #3: 0x00007fff9274837b CoreFoundation`__CFRunLoopRun + 1371
frame #4: 0x00007fff92747bd8 CoreFoundation`CFRunLoopRunSpecific + 296
frame #5: 0x00007fff8c11c56f HIToolbox`RunCurrentEventLoopInMode + 235
frame #6: 0x00007fff8c11c2ea HIToolbox`ReceiveNextEventCommon + 431
frame #7: 0x00007fff8c17568a HIToolbox`AcquireNextEventInMode + 54
frame #8: 0x00007fff8c172e2b HIToolbox`IsUserStillTracking(MenuSelectData*, unsigned char*) + 173
frame #9: 0x00007fff8c172a41 HIToolbox`TrackMenuCommon(MenuSelectData&, unsigned char*) + 1661
frame #10: 0x00007fff8c1803c1 HIToolbox`MenuSelectCore(MenuData*, Point, double, unsigned int, OpaqueMenuRef**, unsigned short*) + 510
frame #11: 0x00007fff8c1800fe HIToolbox`_HandleMenuSelection2 + 446
frame #12: 0x00007fff8a9a8de0 AppKit`_NSHandleCarbonMenuEvent + 277
frame #13: 0x00007fff8a8dfd0d AppKit`_DPSNextEvent + 1828
frame #14: 0x00007fff8a8def68 AppKit`-[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 346
Is there anything I can do to prevent this behaviour ?
Unfortunately I cannot rewrite the application so I need a quick and dirty solution to run the main thread while the menu is active.