2

I have an OpenGL ES2-based app which runs smoothly at 30fps on an iPad 2, but on an iPad 1 it's a bit jerky. I want to modify my app to use a default frame rate of 20fps on the iPad 1, which I've already verified makes it feel much smoother on that model.

What's a good way to detect the iPad 1's lower performance? Should I just look for more than one CPU core (and how do I detect that) or maybe the processor speed or total system memory? I know it's bad to look at device strings so I'm avoiding that. I've considered having my drawing code simply detect that it isn't keeping up with the frame rate and throttling it back, but that has complications I'd rather avoid (ie, falling back on an iPad 2 just because of a transient load spike, then having to add even more code to re-try the higher frame rate just in case that happens).

John Stephen
  • 7,625
  • 2
  • 31
  • 45
  • See [here](http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine) for the CPU count. – Georg Fritzsche Sep 14 '11 at 20:09

2 Answers2

2

Maybe you should try sysctl for this.

- (NSUInteger) getSysInfo: (uint) typeSpecifier
{
    size_t size = sizeof(int);
    int results;
    int mib[2] = {CTL_HW, typeSpecifier};
    sysctl(mib, 2, &results, &size, NULL, 0);
    return (NSUInteger) results;
}

- (NSUInteger) cpuFrequency
{
    return [self getSysInfo:HW_CPU_FREQ];
}

- (NSUInteger) busFrequency
{
    return [self getSysInfo:HW_BUS_FREQ];
}

See Erica Sadun's UIDevice+Extension category (from which this code is extracted).

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • I've decided to just query for the number of CPUs to switch my target framerate. I was hoping for something more elegant somehow, but this is much better than sniffing device strings. Thanks for the help! – John Stephen Sep 15 '11 at 00:06
0

Did you look at the displayLinkWithTarget:selector: method of UIScreen and the associated CADisplayLink class?

I personally never used it, but it seems to be the solution to synchronize your framerate with the refresh rate of the screen. This way you will be able to adapt your framerate to its ideal value, and update your display only when necessary.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • Yes, that's how I control the animation rate. My problem is in selecting the divisor for 30fps or 20fps (a divisor of 2 or 3 respectively). – John Stephen Sep 14 '11 at 19:16
  • I don't get it, why don't you schedule you display redrawing code in the selector you associate to the CADisplayLink, so that it is recomputed only when needed (which will automatically be optimal depending on the iPad performance), instead of forcing yourself a fps value? (and handling your loop yourself probably?) – AliSoftware Sep 14 '11 at 19:40
  • When you use a CADisplayLink (and I do), you can call setFrameInterval on it to divide the full frame rate of 60fps by an integer value, allowing rates such as 30fps (divide by two), 20fps (divide by 3), etc. My app can't update at 60fps, so I divide by two and have CADisplayLink update me at 30fps. On an iPad 1 this is still too high though and I want to use a divisor of 3 to achieve 20fps, which runs fine. – John Stephen Sep 14 '11 at 19:57
  • 1
    By the way, you're assuming that CADisplayLink calls you with some optimal frame rate but that's not how it works. It calls you each time the display controller refreshes, which is 60fps nominally. If you attempt to redraw at the full rate but you can't actually draw within the 16ms before the next frame then you'll get stuttering and jerky motion. – John Stephen Sep 14 '11 at 19:59
  • Oh ok sorry I didn't get you already used CADisplayLink (you didn't mention in your question). Then I can't see a nice solution for this (except some tricks like relying on the device model which is not very clean as you mentionned)... – AliSoftware Sep 14 '11 at 20:02
  • Me again, may have a solution: use sysctl. Will do another answer for details. – AliSoftware Sep 14 '11 at 20:03
  • And thanks for the precision on CADisplayLink not calling at "optimal frame rate", I wasn't aware of this (as I never used it) ;) – AliSoftware Sep 14 '11 at 20:13