How to use shouldRasterize property performance tuning

CALayerThe shouldRasterizeproperty may be a lot of people have not heard even heard of do not know what significance of this property is turned on, what scenarios. Today we'll take for the Road he said.

1, what is rasterized

rasterize, Chinese is rasterized. This translation but people more confused, rasterization is something.

Let's take a look shouldRasterizeto explain Apple official document:

A Boolean that indicates whether the layer is rendered as a bitmap before compositing. Animatable

Layer prior to the synthesis indicates whether a bitmap is rendered Boolean value. Animation can

That is called rasterization process is to layer into a bitmap.

As for what is a bitmap, you can see Wikipedia (may be over the wall).

Synthesis, is a different bitmap put together to create a process you end up seeing the image on the screen.

In fact, we can be understood as a bitmap layer tree after tree or rendering the final result of a level calculated. After completion of the synthesis, eventually synthesized a plurality of bitmap a bitmap.

2, the rendering process

To completely understand rasterization, but also to understand the process of rendering the interface of iOS. Rendering is a complex process, in order to say it needs a lot of space, long story short here:

First CPU to calculate the content to be displayed (including creating views, layout calculation, the picture decoding, etc.) to give a layer tree and layer tree will turn into a series of calculations by the render tree, the tree will render the information submitted to the GPU. After six stages of work through the GPU, the CPU and the data on GPU computing display screen for each pixel. The six stages have rasterization.

Understanding the rendering process for understanding the rasterization is still very important, here are a few articles for your reference: Drawing pixels on the screen objc.io China. , Ibireme iOS interface to maintain smooth technique. , How to understand the concept of rasterization. Know almost

3, open shouldRasterize what it means.

3.1 reduce the view hierarchy

A set of CALayer shouldRasterize = YES, advance to the equivalent of a CALayer rasterization. The layer structure was shot CALayer flat, into a bitmap.

3.2 buffer to obtain an image, improve performance

Enable shouldRasterize property will draw to a layer other than a screen image, then this image will be cached . Cached is an important feature that determines almost all scenarios of shouldRasterize.

If there are many sub-layers or have complex effects application, open all painted frame shouldRasterize proportion of all transactions will be much more worthwhile. But rasterized original image takes time, but also consumes additional memory. So it is necessary to choose according to the actual situation.

4, shouldRasterize application scenarios

By understanding the above shouldRasterize, not hard shouldRasterize how to apply.

4.1 Enabling shouldRasterize enhance the performance of complex hierarchical view

Since the image shouldRasterize been enabled are cached. This greatly reduces the burden on the GPU. Imagine a cell in a table view, there are very complex hierarchical structure. When the sliding tableview, GPU requires a lot of synthesis, which may cause performance problems. We can try to open the layer of the cell of shouldRasterize improve performance.

4.2 Enabling shouldRasterize animation to enhance performance

A complex view of the hierarchy do animation, you can also enable GPU shouldRasterize avoid re-synthesized every frame.

4.3 Enabling shouldRasterize improve off-screen rendering performance.

Still in the cell in a table view, if a shadow effect, it will trigger off-screen rendering:

    //dequeue cell
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    ...
    //set text shadow
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.layer.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.layer.shadowOpacity = 0.5;”
复制代码

Each row of characters and avatars do not need to change at the time of each frame refresh, so it looks UITableViewCell layer is very suitable for caching. We can use shouldRasterize to cache the contents of the layer. This will allow the layer off-screen rendering time after and then saved results until the next time to take advantage of the update:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    //dequeue cell
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    ...
    //set text shadow
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.layer.shadowOffset = CGSizeMake(0, 2);
    cell.textLabel.layer.shadowOpacity = 0.5;
    //rasterize
    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
    return cell;
}
复制代码

4.4 actual measurements rather than guessing

Prior to optimize the use of the above means, first by actual real test to determine performance problems. After optimization, but also real test, make sure to solve performance problems.

Open shouldRasterize in a frequently changing layer is of little significance, because the cache will always be re-created, but also rasterization consumption performance, chances are counterproductive.

Here is the development of several commonly used measurement tool provides:

  • Instrument can monitor the performance of Core Animation Core Animation, and we can use it to monitor the FPS and other indicators.

  • Xcode->Debug->View Debuging->Rendering->Color Offscrenn-rended YellowCan with yellow trigger the off-screen rendering layer.

  • Xcode->Debug->View Debuging->Rendering->Hits Green and Misses RedThis metric reflects our improved performance through open shouldRasterize is not the time to try to achieve the desired effect. When using shouldRasterize property, time-consuming layer draw will be cached, and then as a simple flat picture show. When the cache regeneration of this option is to use red to rasterize been highlighted, the cache is reused, then it will be highlighted in green. If the cache, then frequent regeneration (red too), it means rasterization may have a negative impact on performance.

reference:

[1] to draw pixels on the screen. Objc.io China.

[2] ibireme. IOS interface to maintain smooth skills.

[3] How to understand the concept of rasterization. Know almost

[4] Dai Ming .iOS developed the first master class 42 say "iOS native, large front and Flutter how are rendered? . Time Geeks

[5] ibireme. Depth understanding RunLoop.

[6] Nick Lockwood iOS Core Animation:. Advanced Techniques Chinese translation

Reproduced in: https: //juejin.im/post/5d0a099ff265da1b8811e055

Guess you like

Origin blog.csdn.net/weixin_34290631/article/details/93182309