Re: drawing in a separate thread
Re: drawing in a separate thread
- Subject: Re: drawing in a separate thread
- From: Jean-Daniel Dupas <email@hidden>
- Date: Sat, 3 May 2008 15:22:03 +0200
Le 3 mai 08 à 14:52, Graham Cox a écrit :
On 3 May 2008, at 9:36 pm, Duncan wrote:
If you're going to do your drawing in a separate thread, you'll
need to remember WHAT to draw. I wouldn't call it "hackish", I'd
call it the cost of doing business that way.
If you implement a job queue, add the list of dirty rectangles to
each job object. -drawRect would be where you'd add your jobs to
the queue. It cold call getRectsBeingDrawn:count to get the list of
dirty rects, then add that list to the job object. Then when the
worker thread picks up a job, it would get the list of dirty
rectangles from the job object instead of calling
getRectsBeingDrawn:count. That should be pretty straightforward.
This is pretty much what I've done. When the need to draw is
flagged, it copies the rects from getRectsBeingDrawn: and saves them
in the invocation which is queued. When the thread invokes drawing,
it passes the rects back to the view which now overrides
needsToDrawRect: and uses these saved rects to figure out what needs
to draw. Lower level drawing code is none the wiser.
That's all working pretty well as such now, performance seems as
least on a par with synchronous drawing, possibly better though I've
yet to measure it.
Now another issue rears its head ;-)
Consider the user dragging an object. Because drawRect: returns
quickly now, the user is able to move the object beyond the update
area before the thread gets a chance to draw it - so I'm seeing
edges of objects are being clipped out during fast motion. I can't
think of any way around this at the moment - any ideas? (I tried
triggering the draw thread on a setNeedsDisplayInRect: but that's
much worse - it queues so many updates it has an eerie slo-mo
effect! It catches up eventually, but it's clearly not a solution).
G.
Why not merging your drawing invocation instead of queuing them.
Each time a drawRect is called on your view, get the rects that need
to be refreshed, and add them to a single array of rect.
One each loop, the drawing thread will picks all rects in the list and
draw the view one time. Like this, if there is overlapping rects in
the queue it will refresh them only once.
_______________________________________________
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