UIView and CALayer

Single responsibility principle

The UIView provides content for the CALayer, handles events such as touches, and participates in the response chain

The CALayer is responsible for displaying contents

Event passing and view response chain:

If the event is passed all the way to UIAppliction and not handled, it will be ignored

Three, image display principle

1.CPU: Outputs the bitmap

2.GPU: Layer rendering, texture composition

3. Put the result in the frame buffer

4. The video controller extracts the screen display content of the frame buffer before the specified time according to the Vsync signal

5. Display on the screen

CPU work

1.Layout: UI Layout, text calculation

2. Display: drawing

3.Prepare: Picture decoding

4.Com MIT: Submit bitmap

GPU Rendering Pipeline (OpenGL)

Vertex coloring, pixel assembly, rasterization, segment coloring, segment processing

Four, THE REASON of UI lag frame drop

The hardware clock of the iOS device will emit a Vsync signal, and then the App’s CPU will calculate the content to be displayed on the screen, and then submit the calculated content to the GPU for rendering. The GPU then submits the render result to the frame buffer and waits until the next VSync arrives to display the frame from the buffer to the screen. That is, the display of a frame is determined by both the CPU and the GPU.

In general, the slide flow is 60 FPS, which means 60 frames per second of update, which means one frame every 16.7ms. If the CPU and GPU combined processing time is longer than 16.7ms, frames will drop or even lag.

5. Sliding optimization scheme

CPU:

Place the following operations in the child thread

1. Object creation, adjustment, and destruction

2. Pre-typesetting (layout calculation, text calculation, cache height, etc.)

3. Pre-rendering (text and other asynchronous drawing, image decoding, etc.)

GPU:

Texture rendering, view blending

When you typically encounter performance problems, consider the following:

1. Is it limited by CPU or GPU?

2. Is there unnecessary CPU rendering?

3. Is there too much off-screen rendering?

4. Is there too much layer blending?

5. Are there any strange image formats or sizes?

6. Does it involve expensive views or effects?

7. Is the view hierarchy reasonable?

Vi. UI rendering principle

First of all, as a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public account: Programming Daxin. No matter you are a small white or a big ox, you are welcome to enter. (The group will provide a collection of free study books and interview questions and answer documents for free!)

Asynchronous drawing:

[self.layer.delegate displayLayer: ]

The agent is responsible for generating the corresponding bitmap

Set the bitmap as the value of the layer.contents property

Vii. Off-screen rendering

On-screen Rendering: Current Screen Rendering, where the GPU renders within the Screen buffer currently used for display

Off-screen Rendering: It is divided into CPU off-screen Rendering and GPU off-screen Rendering. GPU off-screen rendering means that the GPU creates a new buffer outside the current screen buffer for rendering. What should be avoided as far as possible is GPU off-screen rendering

When will GPU off-screen render trigger?

Rounded corners (when used with maskToBounds), Layer Masks, Shadows, Settings

Why avoid GPU off-screen rendering?

The GPU needs to do additional render operations. Usually, the GPU is very fast when rendering, but when it comes to offscreen-render, the situation may be a little different, because it needs to open up an extra buffer for rendering, and then the process of drawing to the current screen needs to switch between onscreen and offscreen context. This process will be expensive, involving OpenGL pipeline and barrier, and offscreen-render will be involved in every frame, so improper processing will definitely have a certain impact on performance. On the other

Because off-screen rendering will increase the workload of the GPU, the processing time of CPU+GPU may exceed 16.7ms, resulting in frame drop. So minimize the number of layers offscreen-render if possible