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: cache or draw direct?




On May 12, 2005, at 6:08 PM, Steve Mills wrote:

I'm looking at updating our ruler control. Each document window gets a horizontal ruler across the top and a vertical ruler across the side. They change their displayed origin based on the scale and scroll position of the document within the window. They look like a standard ruler; a bunch of ticks with text labels at major dimensions. They also display guides that follow the mouse, and icons that show the position of guides or tabs and paragraph margins for text flows.

In most respects they sound very similar to the rulers that Cocoa provides for a text view.  (see the ruler in the TextEdit application).

What I'm toying with is changing the gworlds to CGImageRefs or CGBitmapContextRefs. (I currently can't figure that out, because one of the docs says you can draw a CGBitmapContextRef with CGContextDrawImage, but that just crashes required a cast too).

You shouldn't be casting a CGBitmapContextRef to a CGImageRef.  The two are completely different objects. What the docs are probably trying to imply is that you can create a block of memory to hold a pixel buffer.  You can then draw in that pixel buffer using a CGBitmapContextRef.  Once you have finished drawing, you can create a CGImageRef from the same pixel buffer and draw the pixels using CGContextDrawImage.

Using this technique, you could draw the common parts of your ruler in an offscreen bitmap and then create a CGImage for that bitmap.  Whenever you wanted to draw those pixels again... you sijply draw the image.

Can anyone tell me if it would be faster to draw all the background stuff (gray gradient, ticks, labels) straight to the window context every time, or would caching it in some type of Quartz "offscreen" and drawing that to the window context be faster? Keep in mind that each ruler can show a few hundred ticks and a couple dozen text labels when zoomed way out, so that's a lot of calls to draw each line as a path and each label via HIThemeDrawTextBox.

Well... using HIThemeDrawTextBox may not be the best choice.  You might get better performance using ATSUI directly.  However, I can't imagine that you are going to come up with a ruler based on simple lines that is going to tax Quartz 2D.  Particularly if you use CGContextAddLines effectively.

Still, if it would make you feel better to cache some things off, you can do so using the technique I mentioned above.

You could also record the drawings into a PDF in memory and replay that PDF every time you wanted to draw the ruler.

I don't know much about the internals of Quartz 2D, but it sure seems like blitting a bitmap would be much faster than hundreds of individual path strokes.

Our min OS is 10.3, so any 10.4 calls are out.

That's a shame really because what you are describing is exactly what CGLayer is designed for.

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: 
 >cache or draw direct? (From: "Steve Mills" <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.