Graphics Reference
In-Depth Information
Reducing Layer Count
Due to the overhead of allocating layers, preprocessing them, packaging them up to be sent
over IPC to the render server, and then converting them to OpenGL geometry, there is a
practical upper limit on the number of layers you can display onscreen at once.
The exact limit will depend on the iOS device, the type of layer, and the layer contents and
properties, but in general once the number of layers runs into the hundreds or thousands,
you are going to start to see performance problems even if the layers themselves are not
doing anything particularly expensive.
Clipping
Before doing any other kind of optimization to your layers, the first thing to check is that
you are not creating and attaching layers to the window if they will not be visible. Layers
might be invisible for a variety of reasons, such as the following:
They lie outside of the bounds of the screen, or the bounds of their parent layer.
They are completely obscured by another opaque layer.
They are fully transparent.
Core Animation does a fairly good job of culling layers that aren't going to contribute to
the visible scene, but your code can usually determine if a layer isn't going to be needed
earlier than Core Animation can. Ideally, you want to determine this before the layer object
is ever created, to avoid the overhead of creating and configuring the layer unnecessarily.
Let's try an example. Listing 15.3 shows the code for creating a simple scrolling 3D matrix
of layers. This looks pretty cool, especially when it's moving (see Figure 15.1), but it's not
particularly expensive to draw because each layer is just a simple colored rectangle.
Listing 15.3 Drawing a 3D Matrix of Layers
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#define WIDTH 10
#define HEIGHT 10
#define DEPTH 10
#define SIZE 100
#define SPACING 150
#define CAMERA_DISTANCE 500
@interface ViewController ()
 
Search WWH ::




Custom Search