15

I would like to share my experience from using self.layer.shouldRasterize = YES; flag on UIViews.

I have a UIView class hierarchy that has self.layer.shouldRasterize turned ON in order to improve scrolling performance (all of them have STATIC subviews that are larger than the screen of the device).

Today in one of the subclasses I used CAEmitterLayer to produce nice particle effects.

The performance is really poor although the number of particles was really low (50 particles).

What is the cause of this problem?

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
Summon
  • 968
  • 1
  • 8
  • 19

2 Answers2

39

I'll just quote Apple Doc and explain:

@property BOOL shouldRasterize

When the value of this property is YES, the layer is rendered as a bitmap in its local coordinate space and then composited to the destination with any other content. Shadow effects and any filters in the filters property are rasterized and included in the bitmap. However, the current opacity of the layer is not rasterized. If the rasterized bitmap requires scaling during compositing, the filters in the minificationFilter and magnificationFilter properties are applied as needed.

So basically when shouldRasterize is set to YES, every pixel that will compose the layer is calculated and the whole layer is cached as a bitmap.

  • When will you benefit from it ?

When you only need to draw it once. That means when you need just pure "simple" animation (eg moving, transform, scaling...) because CoreAnimation will actually use that layer without redrawing it every frame. It's a very powerful feature to cache complex layers (with shadows and corner radius) combined with CoreAnimation.

  • When will it kill you framerate ?

When your layer is redisplayed many times, because on top of the drawing that is already taking effect, the shouldRasterize will process all pixels to cache the bitmap data.

So the real question you should ask yourself is this : "On which layer am I applying the shouldRasterize to YES ? And how often is this layer redrawn ?"

Hope this was clear enough.

apouche
  • 9,703
  • 6
  • 40
  • 45
  • 1
    I am new to iOS. So is shouldRasterize=YES good if I use it for custom UITableViewCells? Thank you for this great answer by the way. :) @apouche – GangstaGraham May 28 '13 at 16:30
  • 3
    i would say most like no, it's not a good idea to rasterize a UITableViewCell for two reasons: 1. If you use a reuse identifier, the tableview cell will be redrawn many time during scrolling (and therefore the shouldRasterize will penalize it even more). 2: you won't benefit from it since it's very uncommon to animate a UITableViewCell. – apouche May 29 '13 at 12:37
  • @apouche wait, are you're saying to use shouldRasterize if you do scaling? will that not just scale the bitmap cache and then for result in a ugly looking image? (scaling up for exemple?) – Hugues BR Dec 08 '15 at 20:26
13

Turning OFF self.layer.shouldRasterize increases performance to normal levels.

Why is that?

According to a video on apple's developers site (I cannot remember the video, help please?) the rule for self.layer.shouldRasterize is that simple: If all of your subviews are static (their position, contents etc, are not changing or animating) then it is beneficiary to turn self.layer.shouldRasterize ON. On the other side if any of the subviews are changing then the framework needs to re-cache the view hierarchy and this is a huge bottleneck. Under the hood the bottleneck is the memory copying between CPU and GPU.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Summon
  • 968
  • 1
  • 8
  • 19