The shouldRasterize attribute of CALayer is probably unknown to many people, and even if it is, they don’t know what it means when it is turned on. Today we are going to talk about it.
1. What is rasterization
Rasterize, the Chinese word for rasterization. This translation only makes people more confused about what rasterization is.
Let’s take a look at shouldRasterize in the official Apple documentation:
A Boolean that indicates whether the layer is rendered as a bitmap before compositing. Animatable
Boolean value indicating whether layer is rendered as a bitmap prior to composition. Can be animated
In other words, rasterization is the process of converting layer into bitmap.
As for what bitmaps are, see Wikipedia for an explanation (probably over the wall).
Compositing is the process of putting together different bitmaps to create the image you eventually see on the screen.
We can actually think of a bitmap as the final result of a layer tree or render tree computed at a certain level. After the synthesis is completed, a bitmap is finally synthesized from multiple bitmaps.
2. Rendering process
To understand rasterization thoroughly, you also need to understand the rendering process of an interface in iOS. Rendering is a complex process that takes a lot of space to explain, but we’ll make it short:
First, CPU calculates the content to be displayed (including view creation, layout calculation, image decoding, etc.) to get the layer tree, and then converts the layer tree into a rendering tree through a series of calculations. The information of the rendering tree will be submitted to GPU. After the GPU passes six stages of work, the data calculated by CPU and GPU will be displayed on every pixel of the screen. Rasterization occurs in one of the six stages.
Understanding the rendering process is still important to understand rasterization, so here are a few articles for you: Drawing pixels to the screen. Objc.io China., ibireme. iOS tips for keeping the interface smooth. zhihu
What it means to turn shouldRasterize on.
3.1 Reduce view hierarchy
ShouldRasterize =YES; shouldRasterize=YES; shouldRasterize=YES The CALayer layer structure is flattened into a bitmap.
3.2 Cache images to improve performance
ShouldRasterize will draw the layer to an off-screen image, which will then be cached. Caching is an important feature that determines almost all of the scenarios for shouldRasterize.
If there are many sublayers or if there are complex effects to apply, shouldRasterize is much more rewarding than drawing all the frames for all the transactions. But rasterizing the raw image takes time and consumes extra memory. Therefore, it is necessary to make a choice according to the actual situation.
shouldRasterize
From the understanding of shouldRasterize above, it is not difficult to figure out how shouldRasterize should be used.
4.1 Enable shouldRasterize to improve the performance of complex hierarchical views
The image is cached because shouldRasterize is enabled. This greatly reduces the burden on the GPU. Imagine a table View cell with a very complex hierarchy. When sliding a TableView, the GPU requires a lot of compositing, which can lead to performance issues. We can try shouldRasterize on the cell layer to improve performance.
4.2 Enable shouldRasterize to improve animation performance
When animating a complex view, shouldRasterize can also be enabled to prevent the GPU from recomposing every frame.
4.3 Enable shouldRasterize to improve off-screen rendering performance.
Still in a table View cell, if a shadow effect is used, it will trigger an off-screen rendering:
//dequeue cell
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; . //settext shadow cell.textLabel.backgroundColor = [UIColor clearColor]; cell.textLabel.layer.shadowOffset = CGSizeMake(0, 2); Cell. TextLabel. Layer. ShadowOpacity = 0.5;"Copy the code
The characters and heads of each line don’t need to change with each frame refresh, so it looks like the UITableViewCell layer is perfect for caching. We can use shouldRasterize to cache the layer content. This will render the layer once it is off screen and save the result until it is updated the next time it is used:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//dequeue cell
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; . //settext 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;
}
Copy the code
4.4 Actually measure rather than guess
Before using the above means of optimization, first through the actual machine test, to determine the occurrence of performance problems. After optimization, real machine testing is also required to confirm that the performance problem is solved.
It doesn’t make much sense to turn shouldRasterize on in a layer that is always changing, because cached items are always being recreated, and rasterization costs performance, which can be counterproductive.
Here are a few common measurements provided by development tools:
-
Instrument’s Core Animation can monitor the performance of the Core Animation. We can use it to monitor FPS and other metrics.
-
Xcode->Debug->View Debuging->Rendering->Color Offscrenn-rended Yellow can Yellow the layer that triggered the off-screen Rendering.
-
Xcode->Debug->View Debuging->Rendering->Hits Green and Misses Red is the indicator that shows when we try to improve performance by shouldRasterize. When shouldRasterize is used, the time-consuming layer drawing is cached and rendered as a simple flat image. This option highlights rasterized layers in red when caching is regenerated, and green when caching is reused. If the cache is frequently regenerated (too much red), this means that rasterization can have a negative performance impact.
Reference:
[1] Draw pixels onto the screen. Objc.io China.
[2] Ibireme. IOS tips for keeping the interface smooth.
[3] How to understand the concepts of rasterization. zhihu
[4] How do iOS Native, Big Front-end and Flutter render, geek time
[5] Ibireme. Understand RunLoop in depth.
[6] Nick Lockwood. IOS Core Animation: Advanced Techniques Chinese version