Graphics Reference
In-Depth Information
Wherever possible, try to avoid making changes to the frame of a view that contains text,
because it will cause the text to be redrawn. For example, if you need to display a static
block of text in the corner of a layer that frequently changes size, put the text in a sublayer
instead.
Rasterization
We mentioned the shouldRasterize property of CALayer in Chapter 4, “Visual Effects,”
as a way to solve blending glitches with overlapping translucent layers, and again in
Chapter 12, “Tuning for Speed,” as a performance optimization technique when drawing
complex layer subtrees.
Enabling the shouldRasterize property causes the layer to be drawn into an offscreen
image. That image will then be cached and drawn in place of the actual layer's contents and
sublayers. If there are a lot of sublayers or they have complex effects applied, this is
generally less expensive than redrawing everything every frame. But it takes time to
generate that rasterized image initially, and it will consume additional memory.
Rasterizing can provide a big performance boost when used appropriately (as you saw in
Chapter 12), but it's very important to avoid rasterizing layers whose content changes every
frame because it will negate any caching benefit, and actually make the performance worse.
To test whether you are using rasterization appropriately, use the Color Hits Green and
Misses Red instrument to see if the rasterized image cache is being frequently flushed
(which would indicate that the layer is either not a good candidate for rasterization, or that
you are unwittingly making changes to it that are unnecessarily causing it to be redrawn).
Offscreen Rendering
Offscreen rendering is invoked whenever the combination of layer properties that have
been specified mean that the layer cannot be drawn directly to the screen without pre-
compositing. Offscreen rendering does not necessarily imply software drawing, but it
means that the layer must first be rendered (either by the CPU or GPU) into an offscreen
context before being displayed. The layer attributes that trigger offscreen rendering are as
follows:
Rounded corners (when combined with masksToBounds )
Layer masks
Drop shadows
Offscreen rendering is similar to what happens when we enable rasterization, except that
the drawing is not normally as expensive as rasterizing a layer, the sublayers are not
affected, and the result is not cached, so there is no long-term memory hit as a result. Too
many layers being rendered offscreen will impact performance significantly, however.
 
Search WWH ::




Custom Search