• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
fast, threaded drawing to NSImageView from C ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fast, threaded drawing to NSImageView from C ?


  • Subject: fast, threaded drawing to NSImageView from C ?
  • From: Rene Limberger <email@hidden>
  • Date: Tue, 30 Oct 2007 12:10:43 -0800

I am trying to wrap an existing C/C++ program with a Cocoa UI. The just of it is that my C/C++ code, which itself is multithreaded, will make asynchronous calls to a method called SetBucket(), which basically hands the computed results (pixels) back to cocoa for drawing.

First, i use detachDrawingThread to run my main C/C++ work in a separate thread. My worker thread itself might create several other threads which each might call SetBucket. In set bucket, i am making a new bitmap rep and fill it with the bucket pixels, then i draw the new bucket/tile onto the static NSImage i keep. Then i hand the updated NSImage to the NSImageView and ask the view to redraw in the NSRect i just updated. The reason we keep a static NSImage is that we want to allocate the image only once, and "accumulate" all the tiles thruout the processing, which eventually will result in complete picture.

This code "sorta" works if my worker thread only runs a single thread. However, if my worker thread itself spawns multiple threads itself, i am getting all sorts of erratic behavior. crashing, "class NSImage autoreleased with no pool in place - just leaking" messages, no redraw, partial redraw, etc. We have tried various thread locking in SetBucket but that does not seem to make any difference.

So for starters, does my code below remotely make sense? Is this the right approach? Our goal is to get the fastest tile update in our view as possible.

any suggestions welcome.
thanks in advance!
-r

static NSWindow*    g_window = NULL;
static NSImageView* g_view   = NULL;
static NSImage*     g_image  = NULL;

WindowCreate(...)
{
   ....
   g_image = [[NSImage alloc] initWithSize:NSMakeSize(hsize, vsize)]];
}

SetBucket(...)
{
   NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:nil ...];

   unsigned char* p = [rep bitmapData];

   for (y)
      for (x)
         *p++ = col;

   [g_image lockFocus];
   [rep drawAtPoint: NSMakePoint(bucket_origin[0], bucket_origin[1])];
   [g_image unlockFocus];
   [rep release];

   [g_view performSelectorOnMainThread:@selector(setImage:)
                            withObject:g_image
                         waitUntilDone:NO];

   [g_view setNeedsDisplayInRect:
           NSMakeRect(bucket_origin[0], bucket_origin[1], bucket_size[0], bucket_size[1])];

}

- (void)doWork:(id)obj
{
   //window & view come in via nib
   g_window = window;
   g_view   = view;

   // call C/C++ to do work
   goDoWork();
}

- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
   //launch new thread to do our C/C++ work
   [NSApplication detachDrawingThread:@selector(doWork:) toTarget:self withObject:nil];
}
_______________________________________________

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

  • Follow-Ups:
    • Re: fast, threaded drawing to NSImageView from C ?
      • From: Bill Bumgarner <email@hidden>
  • Prev by Date: Re: "Cocoa Programming for Mac OS X" for Leopard
  • Next by Date: Re: FSCatalogInfo randomness
  • Previous by thread: Re: problems building on 10.5 for 10.4
  • Next by thread: Re: fast, threaded drawing to NSImageView from C ?
  • Index(es):
    • Date
    • Thread