Research on iOS off screen rendering

GPU rendering mechanism:

CPU calculate the display content submitted to the GPU, after the completion of GPU rendering rendering results into the frame buffer, then the video controller will be in accordance with the VSync signal line reads the frame buffer data, through the digital analog conversion may be passed to display.

GPU screen rendering has the following two ways:

  • On-Screen Rendering
    means the current screen rendering, referring to the GPU rendering operation is currently used to display the screen buffer.
  • Off-Screen Rendering
    means off screen rendering, referring to the GPU in the current screen buffer to open a new buffer for rendering. Special off screen rendering:
    if you are not in the current screen buffer GPU rendering is known as off screen rendering, then there is another special off screen rendering: CPU rendering.
    if we rewrite the drawRect method and use any Core Graphics technology to draw the operation, it involves the CPU rendering. The entire rendering process by the CPU in the App synchronization to complete the
    rendering of the last bitmap handed over by the GPU for display. Note: the
    CoreGraphic is thread safe, so you can make asynchronous rendering, display when put back to the main thread, a simple asynchronous rendering process is as follows – (void) display {dispatch_async (backgroundQueue, ^{CGContextRef CTX = CGBitmapContextCreate (…); / / draw in context… CGImageRef img = CGBitmapContextCreateImage (CTX) CFRelease; (CTX); dispatch_async (mainQueue, ^{layer.contents = img;});}});

Off screen rendering

When you set the following properties, you will trigger off screen rendering:

  • ShouldRasterize (grating)
  • Masks (mask)
  • Shadows (shadow)
  • Edge antialiasing (anti aliasing)
  • Group opacity (opaque)
  • Complex shapes such as rounded corners
  • Gradient

One of the shouldRasterize (rasterization) is a special kind of:
rasterization concept: the map into a grid composed of images.
rasterization: each element corresponds to a pixel in the frame buffer. ShouldRasterize = YES in other attributes to trigger off screen rendering at the same time, will be rasterized content cached, if layer and its corresponding sublayers did not change, can be directly reused in the next frame. ShouldRasterize = YES, which will implicitly create a bitmap, a variety of shadow mask and other effects will also be saved to the bitmap and cache, thereby reducing the frequency of rendering (not vector). Equivalent to the operation of the GPU is turned to the CPU, the bitmap cache, direct reading multiplexing. When you are using rasterization, you can turn on the “Color Hits and Misses Red” to check if the rasterization operation is a good choice in this scenario. Green indicates that the cache is multiplexed, and red indicates that the cache is created repeatedly. If the rasterization layer becomes too red, rasterization may not be of much use. The bitmap cache is removed from memory and re created too frequently, which means that the cache is too late to rebuild. It is possible to select a smaller and deeper layer structure to reduce the rendering time. Note:
for the constant changes in the content, this time do not open, otherwise it will cause the waste of performance as we schedule often dealing with TableViewCell, because TableViewCell redraw is very frequent (because Cell, multiplexing) if the Cell content is not changing, Cell needs to redraw, if set cell.layer grating. Will result in a large number of off screen rendering, reducing graphics performance.

Why use off screen rendering

When using rounded corners, shadows, and masks, the blend of the layer properties is specified to be rendered on the screen before it is not pre synthesized, so that the screen rendering is required.

Off screen rendering does not mean that the software is drawn, but it means that the layer must be rendered in an out of screen context (CPU or GPU) before being displayed.

So when using off screen rendering time will be very easy to cause the performance of consumption, because of off screen rendering in OPENGL to create a separate screen in memory buffers and rendering, and the screen buffer with the current context switching the screen buffer is consumption performance.

Instruments monitor off screen rendering

Instruments’s Core Animation tool has several options related to off screen rendering:

  • Color Offscreen-Rendered Yellow
    will be open to those who need to screen rendering layer highlighted yellow, which means that the yellow layer may have performance problems.
  • Color Hits Green and Misses Red
    if the shouldRasterize is set to YES, the rendering results are cached, if the layer is green, said these caches are multiplexed; if the red represents the cache will be repeated to create, which means the existence of the problem.

Optimization on iOS version

IOS 9 before UIimageView and UIButton set the corner will trigger off screen rendering

IOS 9 after the UIButton set corner will trigger off screen rendering, and the UIImageView PNG image set corner will not trigger off screen rendering, if the other shadow effects such as the settings will trigger the screen rendering.

This may be apple is aware of the screen rendering will produce performance problems, so you can not produce a screen rendering of the apple does not have to screen rendering.

Reference from:
off screen rendering learning notes
off screen rendering