152
These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling Corbin Dunn AppKit Software Engineer Raleigh Ledet AppKit Software Engineer

Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

On Mac OS X

Session 215

Optimizing Drawing and Scrolling

Corbin DunnAppKit Software Engineer

Raleigh LedetAppKit Software Engineer

Page 2: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling
Page 3: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimizing AppKit Drawing

Layer-Backed View Drawingwith Core Animation

Responsive Scrolling

Magnification

Page 4: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Best practicesOptimizing AppKit Drawing

Page 5: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

• You are probably already doing this

- (void)drawRect:(NSRect)dirtyRect {

[NSColor.redColor set]; NSRectFill(dirtyRect);

}

Page 6: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

•And dirtying just the appropriate areas [myView setNeedsDisplayInRect:smallDirtyRect];

•And not [myView setNeedsDisplay:YES];

Page 7: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

NSView

Page 8: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

NSView

setNeedsDisplayInRect:

Page 9: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

NSView

setNeedsDisplayInRect:setNeedsDisplayInRect:

Page 10: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

NSView Dirty Rect

Page 11: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

•Utilize -[NSView getRectsBeingDrawn:count:]- (void)drawRect:(NSRect)dirtyRect {

const NSRect *rectsBeingDrawn = NULL; NSInteger rectsBeingDrawnCount = 0; [self getRectsBeingDrawn:&rectsBeingDrawn count:&rectsBeingDrawnCount]; [NSColor.redColor set]; // Set invariants outside of a loop for (NSInteger i = 0; i < rectsBeingDrawnCount; i++) { NSRectFill(rectsBeingDrawn[i]); }

}

Page 12: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimize -drawRect:Optimizing AppKit Drawing

•Or use -needsToDrawRect:

- (void)drawRect:(NSRect)dirtyRect {

NSRect redRect = NSMakeRect(...); if ([self needsToDrawRect:redRect]) {

[NSColor.redColor set]; NSRectFill(redRect); }

}

Page 13: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Performant operationsOptimizing AppKit Drawing

•Only do drawing in -drawRect:■ No network calls■ No image allocation or loading■ No file access■ No layout (adding/removing subviews)

Page 14: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Performant operationsOptimizing AppKit Drawing

•Only do drawing in -drawRect:■ No network calls■ No image allocation or loading■ No file access■ No layout (adding/removing subviews)

•Hiding views may be faster than adding/removing them■ Utilize setHidden: when necessary■ Exceptions: Layer-backed views

Page 15: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Cache images loaded with -imageNamed:Optimizing AppKit Drawing

- (void)drawRect:(NSRect)dirtyRect {

if (_myImage == nil) { _myImage = [[NSImage imageNamed:@"MyImage"] retain]; }

[_myImage drawInRect:self.imageRect]; }

Page 16: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Avoid image allocation when drawingOptimizing AppKit Drawing

•Use NSOperationQueue to asynchronously load images

[MyOperationQueue addOperationWithBlock:^(void) { NSImage *image = [[NSImage alloc] initWithContentsOfURL:url]; // Access the CGImage to pre-warm it and fault it in [image CGImageForProposedRect:... context: hints:];

// Do the update and redisplay on the main thread [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) { myView.image = image; [myView setNeedsDisplayInRect:myView.imageRect]; }];

}];

Page 17: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Avoid image allocation when drawingOptimizing AppKit Drawing

•Use NSOperationQueue to asynchronously load images

[MyOperationQueue addOperationWithBlock:^(void) { NSImage *image = [[NSImage alloc] initWithContentsOfURL:url]; // Access the CGImage to pre-warm it and fault it in [image CGImageForProposedRect:... context: hints:];

// Do the update and redisplay on the main thread [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) { myView.image = image; [myView setNeedsDisplayInRect:myView.imageRect]; }];

}];

Expensive work done on background thread

Page 18: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Avoid image allocation when drawingOptimizing AppKit Drawing

•Use NSOperationQueue to asynchronously load images

[MyOperationQueue addOperationWithBlock:^(void) { NSImage *image = [[NSImage alloc] initWithContentsOfURL:url]; // Access the CGImage to pre-warm it and fault it in [image CGImageForProposedRect:... context: hints:];

// Do the update and redisplay on the main thread [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) { myView.image = image; [myView setNeedsDisplayInRect:myView.imageRect]; }];

}];

Dispatch UI work done to the main thread

Page 19: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Don’t do layout or invalidation in drawingOptimizing AppKit Drawing

- (void)viewWillDraw { [self addSubview:newSubview]; [self setNeedsDisplayInRect:coolRect];}

- (void)drawRect:(NSRect)dirtyRect { [self addSubview:newSubview]; [self setNeedsDisplayInRect:coolRect]; [NSColor.redColor set]; NSRectFill(coolRect);}

Page 20: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Faster compositingOptimizing AppKit Drawing

• Say YES to isOpaque when possible■ Assuming the view is really opaque!

- (BOOL)isOpaque { return YES;}

Page 21: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Override -wantsDefaultClippingOptimizing AppKit Drawing

• -wantsDefaultClipping defaults to returning YES• Return NO if you don’t need clipping

■ Must constrain drawing to the -getRectsBeingDrawn:count:

- (BOOL)wantsDefaultClipping { return NO; }

Page 22: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Methods AppKit frequently callsAvoid Overriding Certain Methods

•All of the “gState” methods- (NSInteger)gState;- (void)allocateGState;- (oneway void)releaseGState;- (void)setUpGState;- (void)renewGState;

• Sometimes used to know when some state changes■ Such as the view global position in the window

• Prefer to use:■ NSViewFrameDidChangeNotification■ NSViewBoundsDidChangeNotification

Page 23: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Best practices with Core AnimationLayer-Backed View Drawing

Page 24: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Utilize Lion and Mountain Lion APIEffectively Using Layer-Backed NSViews

• See “WWDC 2012 Layer-Backed Views” • layerContentsRedrawPolicy• updateLayer / wantsUpdateLayer

Page 25: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Redrawing Layer-Backed Views

• This property tells when AppKit should mark the layer as needing display■ NSViewLayerContentsRedrawDuringViewResize ■ NSViewLayerContentsRedrawOnSetNeedsDisplay ■ NSViewLayerContentsRedrawBeforeViewResize ■ NSViewLayerContentsRedrawNever

Lion introduced -[NSView layerContentsRedrawPolicy]

Page 26: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

NSViewLayerContentsRedrawOnSetNeedsDisplayRedrawing Layer-Backed Views

•Doing: [view setNeedsDisplay:YES]■ Means “invalidate the layer and lazily redraw”

•AppKit does not call setNeedsDisplay: when the frame changes!•NOT the default value

■ Therefore, you MUST set it!

Page 27: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Since adding -wantsUpdateLayer

CGContextRef backing store made

-drawLayer:inContext:

-[NSView drawRect:]

layer.contents

then calls AppKit’s

AppKit then calls

AppKit Layer Drawing/Updating

CALayer needs to draw

CA calls AppKit’s -displayLayer:

-[NSView updateLayer]

layer.contents

Yes

AppKit then calls

No

-[NSView wantsUpdateLayer]?

Page 28: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Since adding -wantsUpdateLayer

CGContextRef backing store made

-drawLayer:inContext:

-[NSView drawRect:]

layer.contents

then calls AppKit’s

AppKit then calls

CALayer needs to draw

CA calls AppKit’s -displayLayer:

-[NSView updateLayer]

layer.contents

Yes

AppKit then calls

No

-[NSView wantsUpdateLayer]?

AppKit Layer Drawing/Updating

Page 29: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Since adding -wantsUpdateLayer

CGContextRef backing store made

-drawLayer:inContext:

-[NSView drawRect:]

layer.contents

then calls AppKit’s

AppKit then calls

CALayer needs to draw

CA calls AppKit’s -displayLayer:

-[NSView updateLayer]

layer.contents

Yes

AppKit then calls

No

-[NSView wantsUpdateLayer]?

AppKit Layer Drawing/Updating

Page 30: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Use -wantsUpdateLayer and -updateLayerImproving Layer-Backed Memory Use

- (BOOL)wantsUpdateLayer { return YES; }- (void)updateLayer { self.layer.backgroundColor = NSColor.whiteColor.CGColor; self.layer.borderColor = NSColor.redColor.CGColor;}

Page 31: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Avoid Expensive Core Animation Properties

•Avoid these properties if possible@property CGFloat cornerRadius;@property(retain) CALayer *mask;@property(copy) NSArray *filters;@property(copy) NSArray *backgroundFilters;

Page 32: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Utilize Opaque Views When Possible

• Return YES from [NSView isOpaque]layer.opaque = YES; // Implicitly set for you

Page 33: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

Page 34: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

NSClipView

Page 35: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

NSScrollViewNSClipView

Page 36: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

Special AppKit “Tile Layer”

Page 37: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

Page 38: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

Only tiles in the visible region are drawn.*

Page 39: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

Page 40: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

You can still use -drawRect:Large Layer Drawing in AppKit

drawRect: drawRect: drawRect: drawRect:

drawRect: drawRect: drawRect: drawRect:

drawRect: drawRect: drawRect: drawRect:

drawRect: drawRect: drawRect: drawRect:

Page 41: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Tiles are intelligent sizesLarge Layer Drawing in AppKit

NSScrollView

Page 42: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Typical layer-backed viewsReduce Your Layer Count

• Layer-backing a parent view implicitly creates layers for children views

NSView

NSViewsetWantsLayer:YES

NSView

NSView

Electric Bug

Hello World!

Page 43: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Typical layer-backed viewsReduce Your Layer Count

• Layer-backing a parent view implicitly creates layers for children views

Electric Bug

Hello World!

CALayer

CALayer

CALayer

CALayer

Page 44: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Issues with having lots of layersReduce Your Layer Count

• Potentially a high memory cost■ Each subview may have its own backing store (image)■ Overlapping subviews can waste memory

• Potentially high composition cost•Hidden layers still have a composition cost

■ Removing them may be better than hiding■ One or two is okay, but hiding hundreds is not good

Page 45: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

New API: canDrawSubviewsIntoLayerReduce Your Layer Count

@interface NSView ...

- (void)setCanDrawSubviewsIntoLayer:(BOOL)flag NS_AVAILABLE_MAC(10_9);- (BOOL)canDrawSubviewsIntoLayer NS_AVAILABLE_MAC(10_9);

@end

Page 46: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

New API: canDrawSubviewsIntoLayerReduce Your Layer Count

•All children NSViews are drawn into a single CALayer

Electric Bug

Hello World!

setWantsLayer:YESsetCanDrawSubviewsIntoLayer:YES

One CALayer

Page 47: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

New API: canDrawSubviewsIntoLayerReduce Your Layer Count

• -drawRect: is utilized for every view!

Each view drawn with -drawRect:

-updateLayer is not used

Electric Bug

Hello World!

setWantsLayer:YESsetCanDrawSubviewsIntoLayer:YES

Page 48: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

New API: canDrawSubviewsIntoLayerReduce Your Layer Count

• Individual subviews can opt-in to having their own layer

CALayer

[button setWantsLayer:YES]Electric Bug

Hello World!

setWantsLayer:YESsetCanDrawSubviewsIntoLayer:YES

Page 49: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Useful in NSTableViewReduce Your Layer Count

• Reduces all row subviews into a single layer• Row animations will be done with Core Animation

setWantsLayer:YES on the NSScrollView

Page 50: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Useful in NSTableViewReduce Your Layer Count

• Reduces all row subviews into a single layer• Row animations will be done with Core Animation

setCanDrawSubviewsIntoLayer:YESon each NSTableRowView

setWantsLayer:YES on the NSScrollView

Page 51: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Useful in NSTableViewReduce Your Layer Count

• Reduces all row subviews into a single layer• Row animations will be done with Core Animation

setCanDrawSubviewsIntoLayer:YESon each NSTableRowView

setWantsLayer:YES on the NSScrollView

For text to have font smoothing,the text must be drawn

into an opaque area

Page 52: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive Scrolling

Raleigh Ledet

Page 53: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Demo

Page 54: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

GoalsResponsive Scrolling

• Fluid• Smooth •Non-stuttering

Page 55: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

Page 56: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

Visible rect

Page 57: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

Overdraw

Overdraw

Visible rect

Page 58: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

Overdraw

Overdraw

Visible rect

Page 59: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

•Overdraw• Event Model•API•Adoption

Page 60: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingOverdraw

Page 61: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

•Main thread driven• -drawRect: called with non-visible rects

Page 62: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Idle prefetchOverdraw

Visible rect

Page 63: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Idle prefetchOverdraw

drawRect:

Visible rect

Page 64: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Idle prefetchOverdraw

Visible rect

Overdraw

Page 65: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Idle prefetchOverdraw

drawRect:

Visible rect

Overdraw

Page 66: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

Idle prefetchOverdraw

Visible rect

Overdraw

Page 67: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

Idle prefetchOverdraw

Visible rect

Overdraw

Page 68: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

•Main thread driven• -drawRect: called with non-visible rects•AppKit balances overdraw amount with memory and power usage

Page 69: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

•Main thread driven• -drawRect: called with non-visible rects•AppKit balances overdraw amount with memory and power usage•API if you need more control@property NSRect preparedContentRect;- (void)prepareContentInRect:(NSRect)rect;

Page 70: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

Page 71: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

Page 72: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

Page 73: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

Page 74: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

subView

Page 75: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

subView

Page 76: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:rect];

}

Page 77: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:previousRect];

}

Page 78: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:previousRect];

}

Page 79: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Controlling overdrawOverdraw

- (void)prepareContentInRect:(NSRect)rect {// prepare as needed[super prepareContentInRect:previousRect];

}

Page 80: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Invalidating non-visible contentOverdraw

[documentView setNeedsDisplayInRect:rect];

Page 81: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Invalidating non-visible contentOverdraw

[documentView setNeedsDisplayInRect:rect];

Page 82: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Resetting overdrawOverdraw

docView.preparedContentRect = [docView visibleRect];

Page 83: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

API - Resetting overdrawOverdraw

docView.preparedContentRect = [docView visibleRect];

Page 84: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Overdraw

•Main thread driven• -drawRect: called with non-visible rects•AppKit balances overdraw amount with memory and power usage•API if you need more control@property NSRect preparedContentRect;- (void)prepareContentInRect:(NSRect)rect;

Page 85: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingEvent Model

Page 86: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

NSScrollViewscrollWheel:

TraditionalEvent Model

Event Queue hitTest: Responder Chain

scrollWheel:

Each scroll wheel event is independent

Main thread run loop

Page 87: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsiveEvent Model

Scroll wheel events tracked concurrently

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue hitTest: Responder Chain

scrollWheel:

Event Queue

Main thread run loop

Page 88: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsiveEvent Model

Scroll wheel events tracked concurrently

Event Queue

Main thread run loopNSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue

Page 89: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Main thread run loop

Event Queue

Page 90: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Main thread run loop

Event Queue

Page 91: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Main thread run loop

S

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue

Page 92: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Main thread run loop

S

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue

Page 93: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Main thread run loop

S

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue

Page 94: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Main thread run loop

S

ResponsiveEvent Model

NSScrollViewscrollWheel:

Concurrent Tracking Thread

Event Queue

Page 95: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

OverviewResponsive Scrolling

• Concurrent event tracking•What is on screen may not match visibleRect•Not a silver bullet

Page 96: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAPI

Page 97: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed when scrolling occursAPI

•Observe clip view bounds change notificationsNSClipView *clipView = [scrollView contentView];[clipView setPostsBoundsChangedNotifications:YES];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsChanged:) name:NSViewBoundsDidChangeNotification object: clipView];

Page 98: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed when scrolling occursAPI

•Observe clip view bounds change notificationsNSClipView *clipView = [scrollView contentView];[clipView setPostsBoundsChangedNotifications:YES];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsChanged:) name:NSViewBoundsDidChangeNotification object: clipView];

Page 99: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed of user scrollingAPI

• Live scroll notifications

NSScrollViewWillStartLiveScroll

NSScrollViewDidLiveScroll

NSScrollViewDidEndLiveScroll

Page 100: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed of user scrollingAPI

• Live scroll notifications

NSScrollViewWillStartLiveScroll

NSScrollViewDidLiveScroll

NSScrollViewDidEndLiveScroll

Page 101: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed of user scrollingAPI

• Live scroll notifications

NSScrollViewWillStartLiveScroll

NSScrollViewDidLiveScroll

NSScrollViewDidEndLiveScroll

Page 102: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed of user scrollingAPI

• Live scroll notifications

NSScrollViewWillStartLiveScroll

NSScrollViewDidLiveScroll

NSScrollViewDidEndLiveScroll

Page 103: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Getting informed of user scrollingAPI

• Live scroll notifications

NSScrollViewWillStartLiveScroll

NSScrollViewDidLiveScroll

NSScrollViewDidEndLiveScroll

Page 104: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Floating contentNSScrollView API

• Floating subviews

- (void)addFloatingSubview:(NSView *)view forAxis:(NSEventGestureAxis)axis;

Page 105: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Floating contentNSScrollView API

• Floating subviews

- (void)addFloatingSubview:(NSView *)view forAxis:(NSEventGestureAxis)axis;

Page 106: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

Page 107: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

• Linked on 10.8 or later•Window alpha must be 1.0•Document must not have an OpenGL context

Page 108: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

•Automatic

Page 109: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

•AutomaticNSScrollView NSClipView Document View

Page 110: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

•Automatic

• Explicit API+ (BOOL)isCompatibleWithResponsiveScrolling;

NSScrollView NSClipView Document View

Page 111: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

•Do not override -scrollWheel: -lockFocus:

Page 112: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption

• Traditional drawing ■ copiesOnScroll must be YES■ isOpaque must return YES for document view

- Or -• Layer-back the scroll view

Page 113: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption: Layer-Backing

•NSScrollView or ancestor- (void)setWantsLayer:(BOOL)flag;- (BOOL)wantsLayer;

Page 114: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption: Layer-Backing

•NSScrollView or ancestor- (void)setWantsLayer:(BOOL)flag;- (BOOL)wantsLayer;

Page 115: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingAdoption: Layer-Backing

• Collapsing layers of document view or children- (void)setCanDrawSubviewsIntoLayer:(BOOL)flag;

Page 116: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Xcode Support

Page 117: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Xcode Support

Page 118: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Traditional scrollingXcode Support

Page 119: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Responsive scrollingXcode Support

Page 120: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

SummaryAdoption

•Automatic when possible

Page 121: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

SummaryAdoption

•Automatic when possible• Explicitly opt in as last resort

Page 122: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

SummaryAdoption

•Automatic when possible• Explicitly opt in as last resort• Layer-backed vs. traditional drawing

Page 123: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

SummaryAdoption

•Automatic when possible• Explicitly opt in as last resort• Layer-backed vs. traditional drawing•Use Xcode to verify

Page 124: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

NSScrollViewMagnification

Page 125: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsivenessMagnification

•NSScrollView supports magnification@property BOOL allowsMagnification NS_AVAILABLE_MAC(10_8);

Page 126: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsivenessMagnification

•NSScrollView supports magnification

Page 127: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

ResponsivenessMagnification

•NSScrollView supports magnification

Page 128: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven

Page 129: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw

Page 130: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content

Page 131: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content

Page 132: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends

Page 133: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends

Page 134: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends• Pause for new drawing

`

Page 135: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends• Pause for new drawing

`

Page 136: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends• Pause for new drawing

`

Page 137: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

MagnificationResponsiveness

• Still main thread driven• Likely have overdraw•During gesture we scale existing content• Visible rect redrawn when gesture ends• Pause for new drawing

`

Page 138: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

• -drawRect: speed is crucial

ResponsivenessMagnification

Page 139: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

• -drawRect: speed is crucial• Live magnification notificationsNSScrollViewWillStartLiveMagnifyNotification NS_AVAILABLE_MAC(10_8);NSScrollViewDidEndLiveMagnifyNotification NS_AVAILABLE_MAC(10_8);

ResponsivenessMagnification

Page 140: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

Page 141: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

Page 142: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

Page 143: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

• Replacement API- (NSRect)constrainBoundsRect:(NSRect)proposedBounds;

Page 144: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

• Replacement API- (NSRect)constrainBoundsRect:(NSRect)proposedBounds;

Page 145: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Centering clip viewsMagnification

•Deprecated API- (NSPoint)constrainScrollPoint:(NSPoint)newOrigin;

• Replacement API- (NSRect)constrainBoundsRect:(NSRect)proposedBounds;

Page 146: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Conclusion

Page 147: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling
Page 148: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Optimizing AppKit Drawing

Layer-Backed View Drawingwith Core Animation

Responsive Scrolling

Magnification

Page 149: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

More Information

Jake BehrensApp Frameworks [email protected]

DocumentationCore Animation Programming Guidehttp://developer.apple.com/

Apple Developer Forumshttp://devforums.apple.com

Page 150: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Related Sessions

Best Practices for Cocoa Animation MarinaWednesday 2:00PM

Page 151: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling

Labs

NSTableView, NSView, and Cocoa Lab Frameworks Lab AThursday 10:15AM

Cocoa Animations, Drawing, and Cocoa Lab Frameworks Lab AFriday 9:00AM

Page 152: Optimizing Drawing and Scrolling · These are confidential sessions—please refrain from streaming, blogging, or taking pictures On Mac OS X Session 215 Optimizing Drawing and Scrolling