2

I have a view which implements freehand drawing, but I have a small problem. I noticed on the iPad 3 that everything went to hell, so I tried to update my drawing code (probably as I should have done in the first place) to only update the portion that was stroked. However, the first stroke after open, and the first stroke after about 10 seconds of idle are extremely slow. After everything is "warmed up" it is smooth as butter and only takes about 0.15ms per drawRect. I don't know why, but the whole view rectangle is getting marked as dirty for the first drawRect, and the first drawRect after idle (then it takes about 150 ms to update). The stack trace shows that my rectangle is being overridden by CABackingStoreUpdate_

I tried not drawing the layer if the rectangle was huge, but then my entire context goes blank (will reappear as I draw over the old areas like a lotto ticket). Does anyone have any idea what goes on with UIGraphicsGetCurrentContext()? That's the only place I can imagine the trouble is. That is, my views context got yanked by the context genie so it needs to render itself fully again. Is there any setting I can use to persist the same context? Or is there something else going on here...there is no need for it to update the full rectangle after the initial display.

My drawRect is very simple:

- (void)drawRect:(CGRect)rect
{
    CGContextRef c = mDrawingLayer ? CGLayerGetContext(mDrawingLayer) : NULL;
    if(!mDrawingLayer)
    {
        c = UIGraphicsGetCurrentContext();
        mDrawingLayer = CGLayerCreateWithContext(c, self.bounds.size, NULL);
        c = CGLayerGetContext(mDrawingLayer);
        CGContextSetAllowsAntialiasing(c, true);
        CGContextSetShouldAntialias(c, true);
        CGContextSetLineCap(c, kCGLineCapRound);
        CGContextSetLineJoin(c, kCGLineJoinRound);
    }

    if(mClearFlag)
    {
        CGContextClearRect(c, self.bounds);
        mClearFlag = NO;
    }

    CGContextStrokePath(c);
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    CGContextDrawLayerInRect(UIGraphicsGetCurrentContext(), self.bounds, mDrawingLayer);
    NSLog(@"%.2fms : %f x %f", (CFAbsoluteTimeGetCurrent() - startTime)*1000.f,  rect.size.width, rect.size.height);

}
3
  • You might try overriding setNeedsDisplay and setNeedsDisplayInRect: so you can put a break point and to see if something is invalidating your entire view unexpectedly. Commented Jun 26, 2012 at 1:30
  • (Also, very interesting question.) Commented Jun 26, 2012 at 1:41
  • @JesseRusak Very clever idea, but the invalidations are not a result of calling either :( (They only gets called at the beginning and never again for the full rect)
    – borrrden
    Commented Jun 26, 2012 at 2:24

1 Answer 1

3

I found a useful thread on on the Apple Dev Forums describing this exact problem. It only exists since iOS 5.0 and the theory is that it is because Apple introduced a double buffering system, so the first two drawRects will always be full. However, there is no explanation for why this will happen again after idle. The theory is that the underlying buffer is not guaranteed by the GPU, and this will be discarded at whim and need to be recreated. The solution (until Apple issues some kind of real solution) is to ping the buffer so that it won't be released:

mDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(pingRect)];
[mDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

- (void)pingRect
{
    //Already drawing
    if(mTouchCount > 0) return;

    //Even touching just one pixel will keep the buffer alive
    [self setNeedsDisplayInRect:CGRectMake(0, 0, 1, 1)];
}

The only weakness is if the user keeps their finger perfectly still for more than 5 seconds, but I think that is an acceptable risk.

EDIT Interesting update. It turns out simply calling setNeedsDisplay is enough to keep the buffer alive, even if it returns immediately. So I added this to my drawRect method:

- (void)drawRect:(CGRect)rect
{
   if(rect.size.width == 1.f)
       return;
    //...
}

Hopefully, it will curb the power usage that this refresh method will surely increase.

5
  • You might want to call that less frequently for power-usage reasons. Commented Jun 26, 2012 at 1:41
  • 1
    @JesseRusak I was concerned about the same thing, but it doesn't work unless it is 60 hz (Although that only results in one call to full drawRect, so I assume that without 60 hz it misses a buffer in the double buffer system)
    – borrrden
    Commented Jun 26, 2012 at 2:31
  • Huh. Wow. That's pretty crazy. Commented Jun 26, 2012 at 10:42
  • CPU usage is tiny and only runs when the app is active, should have no effect on battery.
    – jjxtra
    Commented Oct 15, 2012 at 1:36
  • This causes terrible performance if there is any OpenGL rendering going on just FYI...
    – jjxtra
    Commented Nov 12, 2012 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.