Strange behavior of CGLayerCreateWithContext on iPad 2 vs on the New iPad?

438 views Asked by At

In a Single View App, FooView is the main view. In FooView's drawRect, if I do

if (!self.layer1) {
    CGContextRef context = UIGraphicsGetCurrentContext();
    NSLog(@"current context is drawRect is %@", context);

    self.layer1 = CGLayerCreateWithContext(context, self.bounds.size, NULL);
    NSLog(@"layer1 is %@", self.layer1);

    self.context1 = CGLayerGetContext(self.layer1);

    CGContextSetFillColorWithColor(self.context1, [[UIColor orangeColor] CGColor]);
}

CGContextRef context = UIGraphicsGetCurrentContext();

NSDate *start = [NSDate date];
CGContextDrawLayerAtPoint(context, CGPointZero, self.layer1);
NSTimeInterval timeInterval = -[start timeIntervalSinceNow];
NSLog(@"It took %f to draw", timeInterval);

And I am using a CADisplayLink to invoke a method that calls [self.view setNeedsDisplay] in the view controller, so that drawRect is invoked every 1 frame, or 5 frame, or 20 frame, using the frameInterval property.

On iPad2, the time to draw when the CGLayer is filled with hundreds of 6x6 points rectangle, is about 0.011 to 0.012 second, as printed by the NSLog statement. This is capable of 60fps. (60fps is 0.01666 second per frame)

If I change the line to:

    self.layer1 = CGLayerCreateWithContext(NULL, self.bounds.size, NULL);

(using NULL instead of context). Then the time is about 0.014 second, which is still good for 60 fps.

But on the New iPad, the opposite is happening: if context was used on that line, then the time is a whopping 0.17 second... which is not good for even 6 fps. If NULL is used, then the time is 0.038, which is not good for 60 fps, but at least it is good for 20 fps, which is still acceptable. Does any one know why it is behaving like this, and how to make it work well or work better on all platform of iPad, iPad 2 and the New iPad?

If calling CGLayerCreateWithContext with NULL gave a GPU cached graphics context, and calling with context gave a bitmap context, then why on iPad 2, it is actually faster if calling CGLayerCreateWithContext with context?

(I am using iOS 5.1.1 on the New iPad, and iOS 5.1 on iPad 2... which shouldn't be that much a difference... and I don't want to upgrade iPad 2 so that I can have another device with a different iOS version to test against my app).

0

There are 0 answers