preface

Short step without thousands of miles, not small streams into rivers and seas. Learning is like rowing upstream; not to advance is to drop back. I’m a hauler who drifts from platform to platform. Study half an hour a day, health and happiness for a lifetime. Today I’m going to talk about UI related interview questions. Nonsense not to say, directly to everyone dry goods, I hope to help you, excellent people have been praised.

UIView and CALayer

The single responsibility principle

UIView provides content for CALayer and handles events like touches and participates in the response chain

CALayer is responsible for displaying content contents

2. Event Delivery and View response chain:

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

Three, image display principle

1.CPU: outputs the bitmap

2.GPU: Layer rendering, texture synthesis

3. Put the result into 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. The screen is displayed

CPU work

1.Layout: UI Layout, text calculation

2. Display: drawing

2. 3. How to Play guitar

4.Com MIT: Bitmap submission

GPU Rendering Pipeline (OpenGL)

Vertex shading, pixel assembly, rasterization, fragment shading, fragment processing

4. UI is stuck and frames are dropped

The iOS device’s hardware clock emits Vsync (vertical sync signal), and the App’s CPU calculates what to display on the screen

The calculated content is submitted to the GPU for rendering. The GPU then submits the rendering results to the frame buffer, which will be cached when the next VSync arrives

The frames of the dash are displayed on the screen. That is, the display of a frame is determined by both the CPU and GPU.

Generally speaking, the page slides smoothly at 60 FPS, that is, 60 frames update in 1s, that is, every 16.7ms to produce a frame, and if the CPU

If the processing time combined with GPU exceeds 16.7ms, frames will be dropped or even stutter.

5. Sliding optimization scheme

CPU:

Place the following operations in the child thread

1. Create, adjust, and destroy objects

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

3. Pre-rendering (asynchronous rendering of text, image decoding, etc.)

GPU:

Texture rendering, view blending

Generally, when encountering performance problems, consider the following questions:

Is it limited by CPU or GPU?

Is there any unnecessary CPU rendering?

Are there too many off-screen rendering operations?

Is there too much layer blending?

Are there strange image formats or sizes?

Are there expensive views or effects involved?

Does the view hierarchy make sense?

6. UI drawing principle

Asynchronous drawing:

[self.layer.delegate displayLayer: ]

The proxy is responsible for generating the corresponding bitmap

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

7. Off-screen rendering

On-screen Rendering: The Rendering operation of the GPU is performed in the on-screen buffer currently used for display

Off-screen Rendering: Off-screen Rendering, which is divided into CPU off-screen Rendering and GPU off-screen Rendering. GPU off-screen rendering refers to the GPU

Create a new buffer outside the current screen buffer for rendering

What should be avoided is GPU off-screen rendering

When will GPU off-screen rendering trigger?

Rounded corners (when used with maskToBounds), layer masks, shadows, Settings

Why avoid GPU off-screen rendering?

The GPU needs to do additional rendering operations. Usually the GPU is fast in rendering, but offscreen-render is involved

The situation may be a little different, because the process of opening up a new buffer to render and then drawing to the current screen needs to be done

Switch between onScreen and offscreen context, this process can be expensive, involving OpenGL pipeline and

Barrier and offscreen-render are involved in every frame, so mishandling will definitely have an impact on performance. In addition, off-screen rendering will increase the workload of GPU, which may lead to the processing time of CPU+GPU exceeding 16.7ms, resulting in frame drop lag. so

Minimize offscreen-render layers if possible