Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

johan.mattsson.

macrumors newbie
Original poster
Mar 4, 2015
1
0
Hi

I have written an application that works well on older versions Mac OS X but a custom NSView flickers when it is drawn on Yosemite.

My drawing method looks like this:
Code:
- (void)drawRect: (NSRect) rect {
	cairo_surface_t* surface;
	cairo_surface_t* surface_alpha;
	cairo_t* cr;
	cairo_t* cr_alpha;
	NSRect allocation = [self bounds];
	
	CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];

	surface = cairo_quartz_surface_create_for_cg_context (context, 
														  (int)allocation.size.width, 
														  (int)allocation.size.height);
	cr = cairo_create (surface);
	
	surface_alpha = cairo_surface_create_similar (surface, 
												  CAIRO_CONTENT_COLOR_ALPHA, 
												  (int)allocation.size.width, 
												  (int)allocation.size.height);
	
	cr_alpha = cairo_create (surface_alpha);
	
	cairo_save (cr);
	cairo_set_source_rgba (cr, 1, 1, 1, 1);
	cairo_rectangle (cr, 0, 0, allocation.size.width, allocation.size.height);
	cairo_restore (cr);

	// Draw many objects here, it can take 250ms.
	
	cairo_set_source_surface (cr, surface_alpha, 0, 0);
	cairo_paint (cr);
 
	cairo_destroy (cr_alpha);
	cairo_surface_destroy (surface_alpha);

	cairo_destroy (cr);
	cairo_surface_destroy (surface);
}

Should I implement double buffering and how is that done in cocoa?
 
Hi

I have written an application that works well on older versions Mac OS X but a custom NSView flickers when it is drawn on Yosemite.

Should I implement double buffering and how is that done in cocoa?

As noted in Apple's documentation https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/GraphicsContexts/GraphicsContexts.html most windows are already double-buffered, so creating your own buffer could be problematic. See Apple's note at that link:
warningApple.png


That entire chapter, Cocoa Drawing Guide, is worth a complete read. Doing your own buffering is possible but it could be a battle unless the programmer has a clear understanding of what Apple's code is already doing. When in doubt try it first without your own buffer and work from there.

The code comment indicates lots of objects are drawn. This is sometimes an indication a design needs to be changed to reduce the impact on that view ( i.e. maybe an additional view ). In any case, just seeing the drawRect: method is not enough to identify the problem. Presumably the code is not animating with CALayer. It would be helpful to see details like OS X version, Xcode version, and the header for this custom view.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.