Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get an CGLayer from an HIView?




On May 31, 2006, at 1:55 PM, Olaf Krzikalla wrote:

Hi,

simply create the actual CGLayer on the first invocation of
(kEventClassControl, kEventControlDraw) and reuse that layer so long
as the display environment for the view doesn't change.  Depending on
how the layer is used, it may be to your advantage to recreate the
layer when the display environment changes (such as when the window
containing the view moves to another screen).

That's what I'm doing now. But two things are bothering me:
1. Due to the above approach there is a conceptional flaw in my software architecture (lets say, it's not as simple as it could be). Actually, I want to cache an image in a CGLayer immediately after I've loaded or created it. But I had to introduce some sort of an 'inbetween-structure' to transfer the creation of the CGLayer to the draw event handling.

2. The performance of the draw event handling becomes unpredictable. Creating a CGLayer with some content is a rather costly operation (compared to copying the layer to a CGContext), which I better should make outside the draw event (nobody bothers, if some animation stops during some loading process or the like, but everybody does (including me) if it occassionally happens somewhen else).

As you've noticed, the peculiarity of working with CGLayers is that they require you to pass them a context to use as a model when they are created. The CGLayer will look at the attributes of that context and will set itself up so that it can reproduce its image on that context quickly.  

The general problem with mixing HIViews and CGLayers is the fact that the HIView doesn't know what context it will be drawing itself on until it actually draws.  In most cases you would hope it is going to draw to the screen, but you don't know which screen that might be (on a computer with many displays).  Or it could be the case that the HIView is going to be drawn somewhere else entirely (say with HIViewCreateOffscreenImage).

In short, using a CGLayer to cache the contents of an HIView may not be the best approach.  (however, CGLayer would still be a handy tool use when rubber stamping a lot of copies of an image, say a radio button or something, into a window)

I can think of two workarounds.  One is to create the CGLayer from the context returned by CGDisplayGetDrawingContext. I guess you will have to capture the display, create the layer from the context, and then release the display.  Every time the display configuration changes, you will probably want to recreate your cached layers.

Another approach in this case would be to bypass layers altogether and use an offscreen bitmap context. To optimize the drawing of the cached image for the screen, you could set up the CGBitmapContext to use the same color space as the system display.


The bitmap context need only live long enough to draw your image into the cached pixel map and then you could get rid of it (unless you want to draw into that same pixel map again later). When you want to draw the cached data you could create a CGImage from the same pixel map that the CGBitmapContext is used.  You could then repeatedly draw that CGImage in the HIView drawing call.

The tricky part is that the CGImage is only good so long as the data it was created from doesn't change.  If you keep the CGBitmapContext around and draw in the pixel map after a CGImage has been created on it, you should recreate it.

The disadvantage of this approach is that you may not be able to supply as many optimizations to your offscreen pixel map that the system might be able to apply to a CGLayer.

Scott


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartz-dev/email@hidden

This email sent to email@hidden

References: 
 >How to get an CGLayer from an HIView? (From: "Olaf Krzikalla" <email@hidden>)
 >Re: How to get an CGLayer from an HIView? (From: Scott Thompson <email@hidden>)
 >Re: How to get an CGLayer from an HIView? (From: "Olaf Krzikalla" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.