iOS CALayer.shouldRasterize
[iOS debug Color Hits Green and Misses Red]
Rasterization = off-screen Rendered + off-screen buffer(cache)
shouldRasterize = true
Advantage - For complex layer(with subviews, sublayers and other effects) which will be moved(scroll etc). It is simple to work with cache instead of redrawing it every time
Disadvantage - not dynamic. If view changes boundaries(width, height, shadow, cornerRadius, color, internal content...) it makes extra work - cache is regenerated. Frequent regeneration of cache has an impact on perfomance
UIKit pre-rendered view off-screen(sublayers, subviews will be taken into account, and a single bitmap will be created, after that all others effects(like opacity, mask...) will be applied and save(cache) it to RAM(until it needs to be updated), when position of layer is changed - UIKit reads it(cached layer) from RAM directly (without rendering from scratch(it is not simple task, for example calculating color blending layers...))
It moves focus from GPU to RAM which demands some extra work on a start but will give a execration during the using
view1.layer.shouldRasterize = true
view1.layer.rasterizationScale = UIScreen.main.scale
Another example is to use shouldRasterize = true
in UITableView
, when you have a complex view with a lot of shadows etc + scroll it is create a performance slump, that is why shouldRasterize = true
has a place to be. Here you will see that cells are cashed but a new one will be regenerated
Also please note that every cache has:
- size
- stale property. If cache was not used it can be removed