TweetFollow Us on Twitter

Autumn 91 - BE OUR GUEST

BE OUR GUEST

GWORLDS AND NUBUS MEMORY

FORREST TANAKA AND PAUL SNIVELY

[IMAGE 095-098_Guest_column1.GIF]

In Developer Technical Support, we're asked from time to time how to make a GWorld so that its pixel image uses memory on a NuBusTM card rather than memory in the application's heap. The idea is to create a GWorld, put the address of the card into the GWorld, use QuickDraw to draw into the GWorld, which effectively draws into the NuBus card's memory, and display the resulting image on the screen. Doing this in a way that works well with the 8*24 GC version of QuickDraw and with whatever QuickDraw brews up in the future isn't possible without breaking a few guidelines. We'll talk about the reasons for this and what you can do instead. For the more cavalier among you, we'll also talk about the least offensive method of coercing a GWorld so that it uses memory on your NuBus card.

NewGWorld allocates off-screen buffers simply by using the same Memory Manager calls that you can make. To allocate the memory, NewGWorld simply calls NewHandle to allocate the buffer in your application's heap unless you have the useTempMem bit set, in which case it allocates the buffer in temporary memory. It then tries to move the buffer as high in your heap as possible by calling MoveHHi. That's really all there is to it. The GWorld's pixMap, GDevice, and CGrafPort are allocated similarly-- they're all allocated in your heap using regular Memory Manager calls with no special options, patches, or other nefarious tricks.

None of this changes when you have the 8*24 GC software active--all memory is still allocated out of your application's heap. Once you start drawing into the GWorld, though, the GC software can copy the parts of the GWorld to the 8*24 GC memory. The GWorld and its parts still occupy your heap's memory though, regardless of whether it's cached on the 8*24 GC card.

If you have a NuBus card with gobs of memory, NewGWorld can't take advantage of it because the Memory Manager calls that it uses can't allocate memory on NuBus memory cards. There are no options to NewGWorld or any other GWorld calls that let you say, "There's lots of memory over on this NuBus card, all for you." While GWorlds are absolutely fantastic for creating off-screen drawing environments for most of the usual kinds of situations, they're just not appropriate if you want complete control over where or how the parts of a GWorld are allocated.

QuickDraw is the only thing that's supposed to know how GWorlds are constructed. We know that they're CGrafPorts and we can get their pixMap, GDevice, and off-screen buffer, but we shouldn't make any assumptions about how they were allocated and where they are. For example, we know that the off-screen buffer is allocated as a handle now, but that won't necessarily be the case in the future. There's no guaranteed way to tell which way it was allocated, or even if NewGWorld uses the Memory Manager to allocate it at all (which it always does currently, of course). Even the GWorld's CGrafPort is allocated as a handle that just happens to be always locked. If you try to dispose of a GWorld in which you've modified the baseAddr, you'll need DisposeGWorld to make sure everything is deallocated properly, but its behavior is undefined when it tries to deallocate the off- screen buffer. So if you want to use the memory on your NuBus memory card and feel comfortable that you're not relying on something that could change, you're going to have to create your own off-screen drawing environment by creating an off-screen pixMap, a color table if your off-screen drawing environment uses indexed colors, a GDevice, and a CGrafPort. The April 1989 edition of Macintosh Technical Note #120, "Drawing Into an Off-Screen Pixel Map," covers creating your own off-screen pixMap, CGrafPort, and color table, but it requires you to have the same depth and the equivalent color table that the screen has, so it just steals a screen's GDevice. We think it's always a good idea to create your own GDevice when you draw off screen. If you use a screen's GDevice for drawing off screen, you have to depend on that GDevice's depth and color table. By creating your own GDevice, your off-screen drawing environment can use any depth and color table you want at any time and still be insulated from whatever changes the user makes with the Monitors control panel.

To create your own GDevice, it's better not to use NewGDevice because it always creates the GDevice in the system heap; it's better to keep your data structures in your own heap so that they don't get orphaned if your application quits unexpectedly and that precious system heap space is preserved. Here's what you should set each of your GDevice's fields to be:

gdRefNumYour GDevice has no driver, so just set this to 0.
gdIDIt doesn't matter what you set this to; you might as well set it to 0.
gdTypeSet to 2 if your off-screen pixMap uses direct colors (16 or 32 bits per pixel) or 0 if it uses a color table (1 through 8 bits per pixel).
gdITableAllocate a small (maybe just 2-byte) handle for this field. After you're done setting up this GDevice and your off-screen pixMap, color table (if any), and CGrafPort, set this GDevice as the current GDevice by calling SetGDevice, and then call MakeITable, passing it nil for both the color table and inverse table parameters, and 0 for the preferred inverse table resolution.
gdResPrefWe reckon that more than 99.9% of all inverse tables out there have a resolution of 4. Unless you have some reason not to, we'd recommend the same here.
gdSearchProcSet to nil. Use AddSearch if you want to use a SearchProc.
gdCompProcSet to nil. Use AddComp if you want to use a CompProc.
gdFlagsSet to 0 initially, and then use SetDeviceAttribute after you've set up the rest of this GDevice.
gdPMapSet to be a handle to your off-screen pixMap.
gdRefConSet to whatever you want.
gdNextGDSet to nil.
gdRectSet to be equal to your off-screen pixMap's bounds.
gdModeSet to -1. Why? We're not sure. This is intended for GDevices with drivers anyway.
gdCCBytesSet to 0.
gdCCDepthSet to 0.
gdCCXDataSet to 0.
gdCCXMaskSet to 0.
gdReservedSet to 0.

For gdFlags, you should use SetDeviceAttribute to set the noDriver bit. You should also set the gDevType bit to 1 if you're using two bits per pixel or more, but it can be left at 0 if you're using only one bit per pixel.

The other big difference from the technique shown in Technical Note #120 is that the off-screen pixel image shouldn't be allocated. Instead, just point the baseAddr field of your off-screen pixMap at your NuBus card's memory. You should also set the pmVersion field of your off-screen pixMap to be the constant baseAddr32 (equal to 4). That tells Color QuickDraw to use 32-bit addressing mode to access your off-screen buffer, and that's a requirement if your off-screen pixel image is located on a NuBus card.

When you want to draw into your off-screen pixMap, save the current port with a call to GetPort and the current GDevice with a call to GetGDevice. Then set the current port to the off-screen CGrafPort with a call to SetPort, and set the current GDevice to the off-screen GDevice with a call to SetGDevice. Now all QuickDraw commands are drawn off screen and the resulting images are in your NuBus card's memory. To switch back to drawing on screen, set the current port and GDevice back to the port and GDevice that you saved earlier. Easy!

Even with all this, there might still be a reason to use GWorlds to draw into a NuBus memory card. You might just want some quick and dirty way to get an off-screen drawing environment that uses your NuBus memory card and don't care whether it works with future system software releases or not. We'll talk about that next and also discuss the issues that you have to be careful about when you do this.

First, create a GWorld using NewGWorld as usual. If you want to, pass it a color table, or you can just pass it nil if you want it to make the default color table. For the GWorld flags, make sure you pass only the keepLocal flag. This makes sure that all the pieces of the GWorld are kept in your own heap rather than being cached into the 8*24 GC card, even when you draw into it. That way, you avoid running into any conceivable conflicts with GC QuickDraw over where the GWorld really is. There's no way to tell NewGWorld not to allocate the pixel image, so you might want to make the bounds rectangle small and then make it bigger later so that your heap isn't hit up for a lot of memory that you don't even want. Don't pass it an empty rectangle because NewGWorld just gives you a paramErr in that case. Call GetGWorldDevice to get a handle to your GWorld's GDevice and save it for later.

Now it's time to have the new GWorld use your NuBus card's memory. The baseAddr of your GWorld's pixMap is allocated as a handle, and it has to be thrown out. Call GetPixBaseAddr with a handle to your GWorld's pixMap to get a pointer to the pixel image that NewGWorld allocated for you. Call RecoverHandle with that pointer to get a handle to the pixel image, and then call DisposHandle to get rid of it. Now put the address of your NuBus board into the baseAddr of your GWorld's pixMap. Then set the pmVersion field of your GWorld's pixMap to the constant baseAddr32. That tells Color QuickDraw that the baseAddr of the pixMap is a 32-bit address and so it should switch to 32-bit addressing mode whenever it draws into your GWorld.

If you passed NewGWorld a rectangle that's smaller than you actually want, you can now set it to the real size. Set the bounds rectangle of your GWorld's pixMap and the portRect rectangle of your GWorld's CGrafPort to the rectangle that you really wanted. Also, set the visRgn of the CGrafPort and the gdRect field of your GWorld's GDevice to that same rectangle. Your GWorld is ready for use!

Now the bad news. Many of the GWorld routines assume that the baseAddr field is either a real handle or a copy of the handle's master pointer. Because the pointer in the baseAddr field isn't a master pointer, those routines can crash when they expect one. Setting the pmVersion field doesn't help in most cases; these routines just assume that the GWorld's pixel image was allocated by NewGWorld, which is a reasonable assumption. What this implies is that you can no longer call many of the GWorld routines to maintain your GWorld without a risk of crashing. When you call SetGWorld for your GWorld, you should pass it the GWorld's GDevice instead of nil (that's why we recommended that you save the GWorld's GDevice after calling NewGWorld). For safety's sake, don't call any of the following:

LockPixels
UnlockPixels
AllowPurgePixels
NoPurgePixels
GetPixelsState
SetPixelsState
UpdateGWorld
GetGWorldDevice

You can call DisposeGWorld because it won't get hung up trying to deallocate the pixel image on your NuBus card; setting your pmVersion to baseAddr32 makes this possible. Of course, since all these GWorld routines are off limits, almost all the benefits of having a GWorld at all are gone as well.

Another piece of bad news is that this doesn't take advantage of the speed benefits of using GWorlds with an 8*24 GC card. Most of the speed benefit of using GWorlds with GC QuickDraw is that the GWorld's pixel image is allocated on the 8*24 GC card itself, and so the image data doesn't have to take the time to move across NuBus. If your GWorld draws into a NuBus memory card, the image data has to be moved across NuBus, and so that speed benefit is gone.

The last bit of bad news is that even if you follow all of this, you're still not guaranteed that it will still work in future system software or future video card releases. As we said earlier, this should only be done if you don't care whether it works on future system software releases or not. The description above breaks a lot of rules: don't assume that the pixel image is allocated as a handle; don't set the baseAddr of a GWorld; don't change the dimensions of a GWorld without UpdateGWorld; and don't set the pmVersion field of a GWorld.

You have your choices when you want to use QuickDraw to draw off screen into the memory of a NuBus video card. You can be safe for future compatibility by creating your own off-screen drawing environment from scratch, or you can modify a GWorld so that it uses your NuBus card's memory at the risk of breaking on future systems and at the cost of losing most of the benefits of GWorlds. If you choose the first method and you have no existing routines to create off-screen drawing environments, it's worth it to take a look at Skippy White's Famous High-Level Off-Screen Map Routines in DTS Sample Code #15 on theDeveloper CD Series disc. You can see these routines in action in DTS Sample Code #16. These routines are GWorld-like to some extent, except this time you have the great benefit of source code!



REFERENCES

  • "About 32-Bit Addressing," Konstantin Othmer, develop Issue 6, Spring 1991, pp. 36-37.
  • "Deaccelerated _CopyBits & 8*24 GC QuickDraw," Guillermo Ortiz, Macintosh Technical Note #289, January 1991.
  • "Drawing Into an Off-Screen Pixel Map," Jim Friedlander, Rick Blair, and Rich Collyer, Macintosh Technical Note #120, April 1989.
  • Inside Macintosh Volume VI, Graphics Devices Manager chapter, Addison-Wesley, 1991.
  • Inside Macintosh Volume V, Color Manager chapter, Addison-Wesley, 1988.


FORREST TANAKA has been in Developer Technical Support just shy of two years after a stint with unemployment and trying to get a job at Apple. Before that, he got a BSCS while writing Macintosh device drivers for scanners and writing utility software for a PBX. Now he's working with anything that makes images appear on the Macintosh's screen while avoiding anything that makes images appear on paper. Whenever he's not working, eating, sleeping, watching TV, reading, or watching a movie, he's out riding his bike and wondering whether he should shave his legs. *

PAUL SNIVELY, formerly of Apple's DTS group, came to Apple from ICOM Simulations, Inc., the land of the TMON debugger. He wrote the TMON 2.8 User's Guide and has written for MacTutor magazine. His interests include natural- language processing, knowledge representation, adventure-game programming, horror and suspense, hiking, camping, spelunking, and other things better left unsaid.*

For information about inverse tables, see pages 137 through 139 in the Color Manager chapter of Inside Macintosh Volume V.*

Thanks to Guillermo Ortiz for reviewing this column.*

We welcome guest columns from readers who have something interesting or useful to say. Send your column idea or draft to Caroline Rose at Apple Computer, Inc., 20525 Mariani Avenue, M/S 75-2B, Cupertino, CA 95014 (AppleLink: CROSE).*

 
AAPL
$102.13
Apple Inc.
+1.24
MSFT
$44.87
Microsoft Corpora
-0.14
GOOG
$571.00
Google Inc.
-6.86

MacTech Search:
Community Search:

Software Updates via MacUpdate

Viber 4.2.2 - Send messages and make cal...
Viber lets you send free messages and make free calls to other Viber users, on any device and network, in any country! Viber syncs your contacts, messages and call history with your mobile device,... Read more
Cocktail 7.6 - General maintenance and o...
Cocktail is a general purpose utility for OS X that lets you clean, repair and optimize your Mac. It is a powerful digital toolset that helps hundreds of thousands of Mac users around the world get... Read more
LaunchBar 6.1 - Powerful file/URL/email...
LaunchBar is an award-winning productivity utility that offers an amazingly intuitive and efficient way to search and access any kind of information stored on your computer or on the Web. It provides... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
BBEdit 10.5.12 - Powerful text and HTML...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more
Microsoft Office 2011 14.4.4 - Popular p...
Microsoft Office 2011 helps you create professional documents and presentations. And since Office for Mac 2011 is compatible with Office for Windows, you can work on documents with virtually anyone... Read more
TextWrangler 4.5.10 - Free general purpo...
TextWrangler is the powerful general purpose text editor, and Unix and server administrator's tool. Oh, and also, like the best things in life, it's free. TextWrangler is the "little brother" to... Read more
BitTorrent Sync 1.4.72 - Sync files secu...
BitTorrent Sync allows you to sync unlimited files between your own devices, or share a folder with friends and family to automatically sync anything. File transfers are encrypted. Your information... Read more
Cyberduck 4.5.2 - FTP and SFTP browser....
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Tinderbox 6.0.3 - Store and organize you...
Tinderbox is a personal content management assistant. It stores your notes, ideas, and plans. It can help you organize and understand them. And Tinderbox helps you share ideas through Web journals... Read more

Latest Forum Discussions

See All

ALONE... (Games)
ALONE... 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: ALONE is a handcrafted, intense survival journey through space. Navigate caves, rip through rocky debris, dodge rocks and comets... | Read more »
Almightree: The Last Dreamer (Games)
Almightree: The Last Dreamer 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: The world is shattering and you are the only hope to restore the balance. A thrilling and challenging 3D puzzle... | Read more »
The Nightmare Cooperative (Games)
The Nightmare Cooperative 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: Fiendishly difficult! Adorably cute! Utterly engrossing!How much gold can you get before your entire team is... | Read more »
Mobile Convolution (Music)
Mobile Convolution 1.0.0 Device: iOS Universal Category: Music Price: $9.99, Version: 1.0.0 (iTunes) Description: | Read more »
Invaders! From Outer Space Review
Invaders! From Outer Space Review By Rob Thomas on August 27th, 2014 Our Rating: :: RETRO NOSTALGIAUniversal App - Designed for iPhone and iPad It’s a shame that Invaders! doesn’t offer deeper gameplay, as this retro-inspired... | Read more »
Spooklands Review
Spooklands Review By Jennifer Allen on August 27th, 2014 Our Rating: :: ONE-TOUCH SHOOTERUniversal App - Designed for iPhone and iPad One-touch simultaneously controls your direction and your weapon in this unique arena shooter.   | Read more »
Heroes of Order & Chaos Add Twitch I...
Heroes of Order & Chaos Add Twitch Integration, New Heroes, and More Posted by Ellis Spice on August 27th, 2014 [ permalink ] | Read more »
Foodie Yama Review
Foodie Yama Review By Jennifer Allen on August 27th, 2014 Our Rating: :: BRIEFLY HOOKSUniversal App - Designed for iPhone and iPad Foodie Yama will draw you in for a brief while, and you’ll never be entirely sure why.   | Read more »
Spotify Connect Turns One, Now Supports...
Spotify Connect Turns One, Now Supports New Devices Posted by Ellis Spice on August 27th, 2014 [ permalink ] Universal App - Designed for iPhone and iPad | Read more »
The Rise of PicsaStock and How You Can M...
We all take plenty of photos, right? That’s the joy of having a reasonably powerful camera in your pocket, thanks to your trusty iPhone and a bevy of similarly useful apps. Wouldn’t it be great to make some money out of those snaps? While your... | Read more »

Price Scanner via MacPrices.net

12-Inch MacBook Air Coming in 4Q14 or 2015 –...
Digitimes’ Aaron Lee and Joseph Tsai report that according to Taiwan-based upstream supply chain insiders, Apple plans to launch a thinner MacBook model either at year end 2014 or in 2015, and that... Read more
Sapphire Screen “Most Wanted” iPhone 6 New Fe...
According to the ‘uSell.com iPhone Most Wanted Survey’ — a representative survey of 1,000 U.S. smartphone users conducted by used iPhone marketplace uSell.com — close to half of all smartphone users... Read more
The iPad’s Real Competitive Challenger (Not S...
It’s been my contention for some time that the iPad is suffering from something of an identity crisis, and I suspect that may be a factor in slackening sales this year. Apple can’t seem to decide... Read more
13-inch 2.6GHz/256GB Retina MacBook Pro on sa...
B&H Photo has the 13″ 2.6GHz/256GB Retina MacBook Pro on sale for $1379 including free shipping plus NY sales tax only. Their price is $120 off MSRP. Read more
Life Inventory iOS Apps – Learn to Know Thyse...
James Hollender’s Life Inventory apps s are now on sale with 20% off thru Labor Day, 09/01/2014. This is a great opportunity to get started on that Moral Inventory you’ve been putting off doing for... Read more
Pocket Watch, LLC. Reveals Cloud Server For P...
Beaumont, Texas based Pocket Watch, LLC. has announced the availability of its new ActivePrint Cloud Server Powered by Raspberry Pi. With this small standalone box almost any USB printer or available... Read more
902it Simplifies Area Code Changes For Nova S...
The east coast Canadian provinces of Nova Scotia and Prince Edward Island are phasing in 10 digit telephone dialing, to be fully in place by November, in order to accommodate a second area code to... Read more
Boomerang iPad Stand Mounts Your iPad Anywher...
Boomerang, a Mountable Stand with Multiple Viewing Angles, is now available for iPad Air. Boomerang combines several functions that aim to expand your iPad’s potential in one, elegant product. The... Read more
Retina MacBook Pros available starting at $10...
The Apple Store has Apple Certified Refurbished 13″ and 15″ MacBook Pros available starting at $929. Apple’s one-year warranty is standard, and shipping is free: - 13″ 2.5GHz MacBook Pros (4GB RAM/... Read more
Apple 27-inch Thunderbolt Display (refurbishe...
The Apple Store has Apple Certified Refurbished 27″ Thunderbolt Displays available for $799 including free shipping. That’s $200 off the cost of new models. Read more

Jobs Board

*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager / Business Analyst, WW *Appl...
…a senior project manager / business analyst to work within our Worldwide Apple Fulfillment Operations and the Business Process Re-engineering team. This role will work Read more
Position Opening at *Apple* - Apple (United...
…customers purchase our products, you're the one who helps them get more out of their new Apple technology. Your day in the Apple Store is filled with a range of Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.