IOS interview question: talk about off screen rendering in IOS?

Time:2022-1-8
IOS interview question: talk about off screen rendering in IOS?

Come on, IOS

GPU rendering mechanism: the CPU calculates the display content and submits it to the GPU. After GPU rendering, the rendering result is put into the frame buffer. Then, the video controller will read the data of the frame buffer line by line according to the Vsync signal and transmit it to the display for display through possible digital to analog conversion.

There are two ways of GPU screen rendering:

  • 1) On screen rendering means the current screen rendering, which means that the GPU rendering operation is performed in the screen buffer currently used for display.

  • 2) Off screen rendering means off screen rendering, which means that the GPU opens a new buffer outside the current screen buffer for rendering.
    Special off screen rendering: if the rendering that is not performed in the current screen buffer of the GPU is called off screen rendering, there is another special “off screen rendering” method: CPU rendering. If we rewrite the drawRect method and draw using any core graphics technology, CPU rendering is involved. The whole rendering process is completed synchronously by the CPU in the app, and the rendered bitmap is finally handed over to the GPU for display. Note: core graphics is usually thread safe, so it can be drawn asynchronously, and then put back to the main thread when displaying. A simple asynchronous drawing process is roughly 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;
      });
      });
      }
      Trigger method of off screen rendering:

1) Should rasterize (rasterization), rasterization is a special kind. Rasterization concept: convert a graph into an image composed of grids. Rasterization feature: each element corresponds to a pixel in the frame buffer. Shouldrasterize = yes when other attributes trigger off screen rendering, the rasterized content will be cached. If the corresponding layer and its sublayers are not changed, they can be reused directly in the next frame. Shouldrasterize = yes this will implicitly create a bitmap, and various shadow masks and other effects will also be saved to the bitmap and cached, so as to reduce the frequency of rendering. The equivalent of rasterization is to transfer the GPU operation to the CPU, generate bitmap cache, and directly read and reuse. When you use rasterization, you can turn on color hits green and misses red to check whether rasterization is a good choice in this scene. Green indicates that the cache is reused, and red indicates that the cache is repeatedly created. If rasterized layers turn red too often, rasterization may not be of much use for optimization. The bitmap cache is deleted from memory and recreated too often. Red indicates that the cache is rebuilt too late. You can select a smaller and deeper layer structure for rasterization to try to reduce rendering time. For frequently changing content, do not open it at this time, otherwise it will cause a waste of performance. For example, for tableviewcells that we often deal with, because the redrawing of tableviewcells is very frequent (because of the reuse of cells), if the content of cells changes constantly, the cells need to be redrawn constantly. If cell. Is set at this time If the layer can be rasterized, it will cause a lot of off screen rendering and reduce the graphics performance.

  • 2) Masks
  • 3) Shadows
  • 4) Edge antialiasing
  • 5) Group opacity
  • 6) Complex shapes, fillet settings, etc
  • 7) Gradual change
    Why off screen rendering is used: when fillet, shadow and mask are used, the mixture of layer attributes is specified that it cannot be drawn directly on the screen before pre synthesis (before the start of the next Vsync signal), so off screen rendering needs to be aroused. Off screen rendering does not mean software drawing, but it means that the layer must be rendered in an off screen context (whether CPU or GPU) before being displayed. Therefore, it is easy to cause performance consumption when using off screen rendering, because off screen rendering will create an off screen buffer in memory and render, and the context switching between off screen buffer and current screen buffer is very performance consuming. Due to the vertical synchronization mechanism, if the CPU or GPU fails to complete the content submission within a Vsync time, that frame will be discarded and displayed again for the next opportunity. At this time, the display screen will keep the previous content unchanged. That’s why the interface is stuck.

Instruments monitor off screen rendering:

  • 1) Color off screen rendered yellow. When on, the layers that need off screen rendering will be highlighted as yellow, which means that the yellow layer may have performance problems.

  • 2) Color hits green and misses red, if shouldrasterize is set to yes, the corresponding rendering results will be cached. If the layer is green, it means that these caches are reused; If it is red, it indicates that the cache will be created repeatedly, which indicates that there is a performance problem there.
    Optimization on IOS version:

  • 1) Before IOS 9.0, uiimageview and uibutton settings will trigger off screen rendering.

  • 2) After IOS 9.0, setting rounded corners in uibutton will trigger off screen rendering, while setting rounded corners in PNG pictures in uiimageview will not trigger off screen rendering. If other shadow effects are set, off screen rendering will still be triggered.

Data recommendation

A friend presented a copy of the latest IOS interview questions, collected some information he thought was good, shared it with you, and added our communication group100 190 6160, to get a detailed interview information from a large factory, so as to add a guarantee for your job hopping.

IOS interview question: talk about off screen rendering in IOS?

Next:Full collection of new IOS interview questions (constantly updated at present)

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]