eonil
eonil

Reputation: 85975

Any optimization method suggestion for Core Animation applications?

I'm testing animation method for magazine like media-centric app. My goal is

I'm considering Core Animation or OpenGL.

OpenGL is definitely faster, but integrating video playback within GL sprites is impossible yet as I know. (it requires video to texture feature)

So I'm digging Core Animation. But the performance was too bad. I tried simple gravity simulation with 256 of 11x10px, alpha-blended bitmap sprites. And I got only about

The bottleneck is obviously on CPU code.

As I know, CA uses GL for compositing, and I used just single tiny bitmap. So this result is unreasonable. CA framework itself has huge overhead, but I can't figure out where it is, and how to fix.

I tried optimization. But there was only a few options. I tried explicit transaction, removing all additional interpolations. However, the performance did not get better. I tried explicit animation, but it was too hard to understand it's behavior. Maybe the bottleneck is there.

Any optimization method suggestion?

Here's my ticking source code: (tick called with CADisplayLink):

- (void)tick
{
 [CATransaction begin];
 [CATransaction setAnimationDuration:0.0f];

 CGRect bounds = [hostLayer bounds];
 CGFloat gravity = +9.8f * 0.1f;

 for (TestParticleSprite *tspr in spriteLayers)
 {
  CGSize mtn = [tspr motion];
  CGPoint ctr = [tspr position];

  mtn.height += gravity;  
  ctr.x  += mtn.width;
  ctr.y  += mtn.height;

  CGFloat over = ctr.y - bounds.size.height;
  if (over > 0.0f)
  {
   // Hit the ground!
   ctr.y  = bounds.size.height - over;  // Bounce.
   mtn.height *= -1.0f;       // Bounce.
//   mtn.width *= 0.95f;       // Lose energy.
//   mtn.height *= 0.95f;       // Lose energy.
  }

  [tspr setMotion:mtn];
  [tspr setPosition:ctr];
  [tspr removeAllAnimations];

//  // Tried explicit animation, but it was unable to make it work.
//  CATransform3D  t  = CATransform3DMakeTranslation(ctr.x, ctr.y, 0.0f);
//  CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
//  [anim setAdditive:NO];
//  [anim setCumulative:NO];
////  [anim setFromValue:[NSValue valueWithCATransform3D:t]];
//  [anim setToValue:[NSValue valueWithCATransform3D:t]];
//  [tspr addAnimation:anim forKey:nil];
 }

 [CATransaction commit];
}

Upvotes: 3

Views: 672

Answers (1)

Paul R
Paul R

Reputation: 212969

Run your code under Shark (part of the CHUD tools) and see where most of the time is being spent - that should give you a clue as to where to focus your optimisation efforts.

Upvotes: 1

Related Questions