MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


What Is Quartz (or Why Can't Windows Do That)

by Matthew Russell
10/11/2005
Quartz
Quartz is the heart and soul of Mac OS X's graphics layer, which directly supports the defining features of the Aqua desktop experience. Quartz is largely based upon Adobe's PDF specification, but it has roots tracing all the way back to PostScript. The two defining components of Quartz are Quartz Compositor and Quartz 2D. Quartz Compositor is OS X's powerful window server, and Quartz 2D is the two-dimensional drawing engine that's often referred to as Core Graphics. Although Quartz 2D is accessible through the Application Services umbrella framework, Tiger introduced Quartz Composer: an alternative way to explore the power of Quartz through a powerful visual programming environment.

In this article:

  1. And Out of the PostScript Came Quartz
  2. Quartz Compositor and Quartz 2D
  3. Visual Programming with Quartz Composer
  4. Universal Access Freebies

Showing off the bells and whistles of Mac OS X's user interface is one way of responding to the "What's so great about a Mac?" question that comes our way every now and then. The next time you get put on the spot, just minimize a video that's playing a few times with the genie effect, and then follow up with some puffs of smoke by removing icons from the dock. By the time you've replaced the icons and are getting back to your work via an elegant exposé transition, you're likely to hear a grumble that indicates you should hand over the keyboard for a little while. That's when you move into a guest account with a fast user switch to let your inquisitive friend do a little exploring. (But as you do the fast user switch, pretend that you didn't notice the amazing cube rotation that just took place.)

Or if you don't feel like showing off, you could just say "Quartz" and be done with it.

And Out of the PostScript Came Quartz

In the early 1980s, Adobe had just been founded and was developing a cutting-edge technology called PostScript. At the time, PostScript was a huge innovation, because instead of specifying how an already rendered image should appear, it specified how the image should be rendered for display. Although the difference might seem subtle, it would soon revolutionize the printing industry and eventually be applied to on-screen imaging.

In 1985, Apple's LaserWriter became the first laser printer on the market to ship with PostScript, and along with Aldus Pagemaker and the Macintosh, it spawned the desktop publishing revolution. Although PostScript went on to dominate the realm of printed media, NeXT realized that PostScript's power was only being partially realized and worked closely with Adobe to produce a variant of PostScript called Display PostScript for on-screen display. The concept of using PostScript to draw on the screen eventually became known as the digital paper metaphor, and NeXT used it extensively in its NeXTSTEP operating system.

After Apple acquired NeXT, the digital paper metaphor played a paramount role in the development of Mac OS X. But while the abstraction remained the same, Adobe's PDF specification (an enhanced subset of PostScript) was chosen as the primary foundation, instead of PostScript itself. Eventually, all of the hard work paid off and Apple introduced Quartz--the heart and soul of OS X's graphics layer, which so distinctively defines OS X.

Quartz Compositor and Quartz 2D

Although Quartz is a catch-all phrase referring to the graphics layer that sits on top of Darwin, it really encompasses two distinct, but closely related, components: a window server called Quartz Compositor and a graphics library called Quartz 2D. The Mac OS X Technology Overview and Introduction to Quartz 2D Programming Guide provide two complementary views of how these components fit into the overall graphics architecture.

graphics environment graphics environment
Two complementary views of how Quartz Compositor and Quartz 2D fit into the overall Mac OS X graphics architecture. (Most of the Quartz 2D API is exposed in the Core Graphics framework.)

Mac OS X Tiger in a Nutshell

Related Reading

Mac OS X Tiger in a Nutshell
A Desktop Quick Reference
By Andy Lester, Chris Stone, Chuck Toporek, Jason McIntosh

Quartz Compositor

Traditional windowing systems rely on a model that requires each pixel on the screen map to the desktop or to exactly one window. This approach is called a "switch model" because the window server repeatedly switches through all of the applications running and gives them each a turn to update their windows. This method is tried and true, but has some serious limitations if you try to do anything fancy like cutouts or transparency.

Consider the problem of updating windows with holes in them using an underlying switch model. Depending on implementation details, you could conceivably determine what would be visible in the window's hole and try to redraw this area, but even then the situation gets awfully messy for even remotely complicated circumstances. For example, if the background window were a movie or animation, you'd almost certainly experience some serious lag--not to mention waste a lot of resources in the process.

Quartz Compositor is a departure from the traditional switch-based approach in that it has applications draw onto an area known as a backing store instead of drawing directly to the frame buffer that gets rendered on the screen. Applications may draw onto their backing store using any supported method available, which facilitates and encourages the use of graphics frameworks such as OpenGL, CoreImage, CoreVideo, and QuickTime. Quartz Compositor composites each window's backing store together to create a single unified display, which it writes to the frame buffer using Quartz 2D. Since it doesn't care how the images in the backing stores get drawn, Quartz Compositor is said to be agnostic with respect to its drawing model.

Because the compositing approach allows pixels to be shared automatically by multiple windows and the desktop, the chore of designing a window with a hole in it now becomes trivial. When combined with support for transparency, its drawing model enables some amazing effects such as round transparent windows, drop shadows, nice rounded corners, the dashboard ripple, and the genie effect.

round transparent window with hole
If you cut out a portion of the image used for the custom view, you can modify Apple's round transparent window example to demonstrate a window with a hole in it .

To say that all of this compositing requires a lot of processing power would be an understatement, but that's where Quartz Extreme fits in. Quartz Extreme is basically the marketing term that describes the hardware acceleration that's gained by moving Quartz Compositor's workload to the Graphics Processing Unit (GPU). Since GPUs are designed to accelerate OpenGL graphics, Quartz Extreme is further realized by treating the contents of each window as a two-dimensional OpenGL texture. This enables the GPU to process intense calculations such as shading, and frees the CPU for other work.

For a nice overview of how Quartz has evolved since the conception of OS X, check out this overview by Ars Technica. Or if you're interested in learning more about specific differences between Quartz Compositor and another windowing system, such as X11, take a look at this Slashdot thread. It provides a nice starting point for a self-guided journey into the world of advanced graphical techniques such as the Porter-Duff compositing, mesh warping, and Affine transforms that Quartz uses to strut its stuff.

Quartz 2D

Quartz 2D commonly refers to both the OS X two-dimensional drawing engine and the drawing engine's client API. You might also hear Quartz 2D and Core Graphics used synonymously since much of Quartz 2D is exposed in the Core Graphics framework, which falls under the Application Services umbrella framework. Because Quartz is rooted in the PDF specification, it shouldn't come as any surprise to you that Quartz 2D provides a rich API for drawing to OpenGL graphics contexts, PDF and PostScript documents, and raster graphics (bitmaps), in addition to the screen.

Quartz is highly customizable and includes the ability to specify whether or not to anti-alias graphics and text, manage color with ColorSync, internally compress data, and specify the level of transparency among other things. If you're interested in enriching your Cocoa repertoire with some Quartz 2D skills or just want to know more about how it enhances your Aqua desktop experience, Mac DevCenter's A Simple Drawing Sample in Quartz 2D and Introduction to Quartz 2D for QuickDraw Programmers tutorials are great places to start before progressing into Apple's Introduction to Quartz 2D Programming Guide. Mac DevCenter also features a nice tutorial for using Python bindings to get a handle on Quartz 2D.

According to the latest Ars Technica review, Tiger comes loaded with a major enhancement called Quartz 2D Extreme, although this feature is disabled by default as of OS X 10.4.2. When Apple officially flips the switch (presumably using a software update if you have the right hardware), it appears that Quartz 2D Extreme will shake up the current architecture by moving Quartz 2D calculations onto the GPU, where OpenGL will crunch the numbers. Since much of Quartz Compositor's work is already done on the GPU, this makes perfectly good sense and would create another win-win situation by further freeing the CPU's load for other work.

Visual Programming with Quartz Composer

If all of that talk about Quartz 2D sounds pretty intense, you'll be glad to know that there's still a lot of fun that you can have without writing a single line of code. Tiger comes packed with Quartz Composer, a powerful visual programming environment that you can use to explore Quartz and create amazing demos that can also be turned into screen savers. Assuming you have the developer tools installed, you'll find Quartz Composer at /Developer/Applications/Graphics Tools. Apple's Introduction to Quartz Composer Programming Guide is the canonical starting point for development, but you'll find an assortment of slick examples out on the net if you do some scouring. Here are some particularly impressive samples of what you can do in Quartz Composer in a relatively short period of time.

brick cube
Follow Apple's tutorial to create a glowing cube that rotates. Augment it to float around the screen and then save it as a screen saver.

Universal Access Freebies

To wrap up our discussion of Quartz, let's take a moment to look at some important, yet often overlooked, features that it brings to our desktop experience via the Universal Access panel in System Preferences.

Start out in the Seeing tab and make sure the "Enable access for assistive devices" box is checked. After enabling the zoom feature, use the ⌘⌥- and ⌘⌥= shortcut keys to zoom in and out. Under the Options button, you'll find a lot of settings that you can fine tune. The ones specifying how the screen should scroll with regard to your mouse pointer are particularly interesting. Being able to zoom in on anything that's displayed on the screen is a powerful feature, and Quartz brings it to us at no additional charge.

universal access pane
Use the often-overlooked Universal Access pane to zoom, adjust contrast, or manipulate the color by inverting and/or shading to a grayscale.

In the Display section, you'll find some other interesting features that Quartz makes possible. Checking the "Use grayscale" option yields the expected effect, but enabling the "White on Black" option might just impress you, give your eyes a break, and extend the amount of work you can get done when you go unplugged. Unfortunately, you'll notice that screen shots you take while using grayscale or "White on Black" don't reflect the change of colors. (Talk back and say why not to earn a gold star for the day.) Under the Hearing section, you'll see an option to make the screen flash any time that there's an alert. Who would've thought?

Final Thoughts

Hopefully you now know a lot more than you ever thought you would about all of the magic that empowers your Mac to have the most advanced user interface on the planet. Quartz really is an engineering marvel, and it's only going to keep getting better.

Matthew Russell is a computer scientist from middle Tennessee; and serves Digital Reasoning Systems as the Director of Advanced Technology. Hacking and writing are two activities essential to his renaissance man regimen.


Return to the Mac DevCenter

Copyright © 2009 O'Reilly Media, Inc.