Threaded drawing
Threaded drawing
- Subject: Threaded drawing
- From: Graham Cox <email@hidden>
- Date: Wed, 04 Dec 2013 21:33:16 +0100
Another question of a general nature.
NSView has -setCanDrawConcurrently, which, when you also permit this at the window level, will handle its -drawRect refreshes on a background thread. I’ve been experimenting with this to see if it yields any worthwhile gains, but it doesn’t appear so - there seems to be a lock on the main thread that waits until the drawing thread has finished, presumably to synchronise the final window update. That being the case, it doesn’t improve responsiveness over drawing on the main thread, so I wonder whether there’s much point to this?
But anyway, it got me thinking how threads *could* be used to draw more quickly.
First, only drawing the refresh rects helps a lot, because I do a lot of work to not only avoid drawing what doesn’t need to be refreshed, but there’s a spatial hash to rapidly exclude objects that fall outside of these update rects without having to iterate over the whole lot testing for intersection. This shows a massive benefit when you’re zoomed well into a view, where most of the objects aren’t visible.
But that leaves those annoying cases when you have the whole view to redraw. I wondered if it would be worth dividing up the view into rects and rendering each one on a separate thread. The problem seems to me to be that they’d all be drawing into the same CGContext, and I wonder how well that could work - e.g. one thread could set a clip ready for its next drawing operation and another could then change that clip so they’d all be tripping over each other, even though they were all drawing into a different part of the context. If access to the context were synchronised, then that would end up serialising all the drawing so there wouldn’t be any gain.
Has anyone trod this path? It would be useful to know whether there’s anything that can be done along these lines, because rendering 10,000 or more objects is just taking too darn long!
—Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden