3

I'm using CADisplayLink on an iOS app with a renderOneFrame method/handler. My understanding is this is triggered every screen refresh, i.e. 60Hz.

However my app is struggling to run at 60FPS and with more features to add, I worry that on older hardware this isn't feasible. Rather than have it running at variable rate, I'd rather render at 30FPS and give myself more breathing room.

Is there a way to make this happen easily?

This seems to be the core of it, but I'm a C++ coder porting to iOS so it's a bit confusing (I copied it from a sample I found):

- (void)go {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    mLastFrameTime = 1;
    mStartTime = 0;

    try {
        app = new MyApp()
    } catch( Ogre::Exception& e ) {
        std::cerr << "An exception has occurred: " <<
        e.getFullDescription().c_str() << std::endl;
    }

    mDate = [[NSDate alloc] init];
    mLastFrameTime = -[mDate timeIntervalSinceNow];

    mDisplayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(renderOneFrame:)];
    [mDisplayLink setFrameInterval:mLastFrameTime];
    [mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [pool release];
}
4

1 回答 1

2

This is what Apple's documentation says about the CADisplayLink property frameInterval:

The default value is 1, which results in your application being notified at the refresh rate of the display. If the value is set to a value larger than 1, the display link notifies your application at a fraction of the native refresh rate. For example, setting the interval to 2 causes the display link to fire every other frame, providing half the frame rate.

Setting this value to less than 1 results in undefined behavior and is a programmer error.

In your example, you're most likely setting the frame interval to a very small number, easily less than 1. You should be setting it to just 1 if you want to make it update with every refresh of the screen. Otherwise you should set it to 2 to get half of that.

Note however, it doesn't specifically mention anything about what the frame rate is. As far as I can tell, all current and existing iOS devices have a screen refresh rate of 60 Hz, but there's nothing to say Apple won't someday release one that has a refresh rate of 120 Hz. So right now, setting the frameInterval to 2 will yield a frame rate of 30 fps, but on some future devices it could give a frame rate of 60 fps. Chances are the device will be fast enough to handle your drawing code at that frame rate anyway, but it's just an FYI. Also there doesn't appear to be any way programmatically to just retrieve that frame rate value, but you could always measure the time between drawing frames to see what the time interval is between frames and do some math to get the frame rate.

于 2014-01-22T18:22:17.073 回答