Implementing a Multitasking-aware OpenGL ES Application
Starting in iOS 4, applications can continue to run when a user switches to another application. An application must be modified to run in the background; applications designed for iOS 3.x and earlier are terminated when a developer switches to another application. For an overall discussion of multitasking on iOS, see “App States and Multitasking”.
An OpenGL ES application must perform additional work when it is moved into the background. If an application handles these tasks improperly, it may be terminated by iOS instead. Also, applications may want to free OpenGL ES resources so that those resources are made available to the foreground application.
Background Applications May Not Execute Commands on the Graphics Hardware
An OpenGL ES application is terminated if it attempts to execute OpenGL ES commands on the graphics hardware. This not only refers to calls made to OpenGL ES while your application is in the background, but also refers to previously submitted commands that have not yet completed. The main reason for preventing background applications from processing OpenGL ES commands is to make the graphics processor completely available to the frontmost application. The frontmost application should always present a great experience to the user. Allowing background applications to hog the graphics processor might prevent that. Your application must ensure that all previously submitted commands have been finished prior to moving into the background.
Here’s how to implement this in your application:
In your application delegate’s
applicationWillResignActive:method, your application should stop its animation timer (if any), place itself into a known good state, and then call the
In your application delegate’s
applicationDidEnterBackground:method, your application may want to delete some of its OpenGL ES objects to make memory and resources available to the foreground application. Call the
glFinishfunction to ensure that the resources are removed immediately.
Once your application exits its
applicationDidEnterBackground:method, it must not make any new OpenGL ES calls. If your application makes an OpenGL ES call, it is terminated by iOS.
In your application’s
applicationWillEnterForeground:method, recreate any objects your application disposed of and restart your animation timer.
To summarize, your application needs to call the
glFinish function to ensure that all previously submitted commands are drained from the command buffer and are executed by OpenGL ES. After it moves into the background, it must avoid all use of OpenGL ES until it moves back to the foreground.
Delete Easily Recreated Resources Before Moving to the Background
Your application is never required to free up OpenGL ES objects when it moves into the background. Usually, your application should avoid disposing of its content. Consider two scenarios:
A user is playing your game and exits your application briefly to check their calendar. When they return to your game, the game’s resources are still in memory, allowing your game to resume immediately.
Your OpenGL ES application is in the background when the user launches another OpenGL ES application. If that application needs more memory than is available on the devices, your application is silently and automatically terminated without requiring your application to perform any additional work.
Your goal should be to design your application to be a good citizen. Your application should keep the time it takes to move to the foreground short while also reducing its memory footprint while it is in the background.
Here’s how your application should handle the two scenarios:
Your application should keep textures, models and other assets in memory; resources that take a long time to recreate should never be disposed of when your application moves into the background.
Your application should dispose of objects that can be quickly and easily recreated. Look for objects that consume large amounts of memory.
A class of easy targets is the framebuffers your application allocates to hold rendering results. When your application is in the background, it is not visible to the user and may not render any new content using OpenGL ES. That means the memory consumed by your application’s framebuffers is allocated, but is not useful. Also, the contents of the framebuffers are transitory; most applications recreate the contents of the framebuffer every time they render a new frame. This makes renderbuffers a memory-intensive resource that can be easily recreated—a good candidate for an object that can be disposed of when moving into the background.
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)