What is Offscreen rendering ?

Offscreen rendering (software rendering) happens when it is necessary to do the drawing in software (offscreen) before it can be handed over to the GPU. It is the biggest bottlenecks to graphics performance. Hardware does not handles text rendering and advanced compositions with masks and shadows

With shadow
With shadow
No shadow
No shadow

It used to be like this:

Offscreen-rendering is one of the worst defined topics in iOS rendering. (biggest bottlenecks to graphics performance)

When Apple’s UIKit engineers refer to offscreen-rendering, it has a very specific meaning, and a ton of third-party iOS dev blogs are getting it wrong (Before WWDC 2014).

Sounds great on paper, but just doesn’t always work in practice
Have you ever heard GPU(hardware accelerated) is faster then CPU(software rendering) ? It’s true, but if we calculate only in one frame (~0.016s). So, we need to get through which are Hardware accelerated and which are not.

The following will trigger offscreen rendering(Software rendering):

• Any layer with a mask (layer.mask)
• Any layer with layer.masksToBounds / view.clipsToBounds being true
• Any layer with layer.allowsGroupOpacity set to YES and layer.opacity is less than 1.0
• Any layer with a drop shadow (layer.shadow).
• Any layer with layer.shouldRasterize being true
• Any layer with layer.cornerRadius, layer.edgeAntialiasingMask, layer.allowsEdgeAntialiasing
• Any layer with layer.borderWith and layer.borderColor?
• Text (any kind, including UILabel, CATextLayer, Core Text, etc).
• Most of the drawing you do with CGContext in drawRect:. Even an empty implementation will be rendered offscreen.

Most other things are hardware accelerated, so they are much faster. However, this may not mean what you think it does (faster in ~0.016s ?). Any of the above types of drawing are slow compared to hardware accelerated drawing, however they don’t necessarily slow down your app because they don’t need to happen every frame.

Pixels Software Stack
Pixels Software Stack

Drawing a drop shadow on a view is slow the first time, but after it is drawn it is cached, and is only redrawn if the view changes size or shape

The view typically isn’t redrawn every frame, it is drawn once and then cached, so the performance after the view is first set up is no worse, unless the bounds change or you call setNeedsDisplay on it.

For large, complex views that don’t change every frame, drawing them once in software (after which they are cached and don’t need to be redrawn) will yield better performance than having the hardware re-composite them every frame, even though it will be slower to draw the first time.

End
With early iOS hardware, it was reasonable to say “do everything in drawRect.” Nowadays GPUs are better, and UIKit has features like shouldRasterize. Today, it’s a balancing act between the time spent in drawRect, the number of off-screen passes, and the amount of blending. For the full details, watch the 2014 WWDC session 419, “Advanced Graphics and Animation for iOS Apps.”

Next we will discuss on: Views that must be redrawn constantly

Leave a Comment

Your email address will not be published. Required fields are marked *