IOS performance optimized for off screen rendering

  • 2020-12-26 05:53:55
  • OfStack

GPU screens are rendered in two ways:

On-Screen Rendering

Means the current screen rendering, meaning that GPU's rendering operation is performed in the screen buffer currently used for display.

Off-Screen Rendering

Off-screen rendering refers to GPU creating a new buffer outside the current screen buffer for rendering operations.

Special off-screen rendering:

If you call any render that is not in the current screen buffer of GPU an off-screen render, there is another special "off-screen render" method: CPU render.
If we rewrite the drawRect method and draw using any of Core Graphics's techniques, we are involved in CPU rendering. The entire rendering process is synchronized by CPU within App

When finished, the rendered bitmap is finally sent to GPU for display. (CPU Render -- > GPU display)

When will off-screen rendering be invoked:

When using rounded corners, shadows, and masks, the mix of layer properties is specified so that it cannot be drawn directly onto the screen without precomposition, so the off-screen rendering needs to be evoked.

Why off-screen rendering costs performance:

Off-screen rendering does not mean software rendering, but it does mean that layers must be rendered in an off-screen context (whether CPU or GPU) before being displayed.
So when using off-screen rendering, it is very easy to have a performance penalty, because in OPENGL off-screen rendering creates a separate off-screen buffer in memory for rendering, and switching between off-screen buffer and the current on-screen buffer context is very performance-intensive.

Use Instruments to monitor off-screen rendering

Instruments's Core Animation tool has several check options related to off-screen rendering:
Color Offscreen-Rendered Yellow

Turning on highlights those layers that need to be rendered off-screen to yellow, which means that yellow layers may have performance problems.

Color Hits Green and Misses Red

If shouldRasterize is set to YES, the corresponding render results will be cached. If the layer is green, the cache will be reused. If it's red, it means the cache will be created repeatedly, which means there is a performance problem.

iOS version of the optimization

Before iOS 9.0, both UIimageView and UIButton set rounded corners to trigger off-screen rendering.

After iOS 9.0, rounded corners in UIButton will trigger off screen rendering, while rounded corners in UIImageView will not trigger off screen rendering if you set other shadow effects or something like that.

This is probably because Apple is also aware of the performance issues associated with off-screen rendering, so they don't have to render off-screen anywhere they can.

Through this article, I hope to help you, thank you for your support to this site!

Related articles: