25 settembre 2009

Hidden Gems of Snow Leopard: IOSurface

Snow Leopard may have looked not so different from its predecessor from the average user point of view: however, for developers like myself, a lot of things have changed, some well advertised (say, GCD, OpenCL and 64 bits), some still to discover. A good overview can be found at Ars Technica.
As one of the long overdue issues, not to mention all the limitations derived from its venerable age, Apple has introduced a first step to the future of the old Quicktime (that will stay in 32 bit universe) with the new Quicktime X: however, Quicktime isn't so easy to replace in one shot, and it's still present in the system, transparently invoked by Quicktime X (or, for us developers, by QTKit) whenever it's needed.
But, how can a 64-bit software (like the Quicktime X Player, or the Finder itself) use a 32-bit library? The answer is, it doesn't, the technique used behind the scenes is far more interesting: when a 64-bit software needs a frame from a movie it can't process otherwise, a specific software is launched (you'll see it in the Activity Monitor as QTKitServer-(process-ID) process-name) that gives back the frames to the 64-bit app.
Hey, isn't that nice? Graphics passed from one process to another, how can they do that? The answer looks like it's in a new framework, IOSurface.

Disclaimer: the following statements are the result of personal experimentation: as such, they don't represent any official documentation nor endorsement.

IOSurface is included in the new public frameworks, but no mention of it exists in the official documentation: looking at the various C headers, however (not only in IOSurface.framework, but overall in the graphics libraries - Spotlight's your friend), it's possible to have a glimpse at its capabilities.

Putting together some sample code

A good example of how IOSurface works could be a quick and dirty implementation of a QTKitServer-lookalike, that plays any Quicktime movie in a 32-bit faceless application, and its 64-bit companion that shows the frames in an OpenGL view. More in detail, a IOSurface can be attached to many kinds of graphic surfaces and passed around to different tasks, that makes it the perfect candidate for our own version of a QTKitServer-clone. The link to the Xcode project is below - 10.6-only, of course.

The faceless movie player

Now, let's see how to create frames on IOSurfaces. For a start, we can create Core Video pixel buffers (one of the possibilities to define an offscreen destination for QT movies - see the excellent QTCoreVideo sample projects) that will have IOSurfaces bound to them: when we create the QTPixelBufferContext, introducing the proper items in the optional dictionary will instruct Core Video to attach IOSurfaces to each pixel buffer we'll get back. Each CVPixelBuffer we'll get from Core Video will then be asked for the related IOSurfaceRef: IOSurfaceRefs are the references to use inside the same application, and each surface has also a unique IOSurfaceID that can be referred to in other processes to obtain a local IOSurfaceRef.
For the sample I've put together, I've used the simplest way of passing IOSurfaces, i.e. asking them to be created global and passing around the IDs: not the ideal solution in the long term, but the other way (i.e. passing them through Mach ports) looks more complex and prone to errors to implement without the docs.
The small CLI app gets as the only argument the movie to play, and passes back the IDs of the surfaces through a simple pipe. Using the kIOSurfaceIsGlobal option puts also a limit in the consumer side: as the CLI doesn't know anything about the consumer, surfaces will be reused as soon as possible, so they'll have to be consumed at once. Binding them to Mach ports, however, would force the framework to create new surfaces until the previous ports are deallocated.

The 64-bit GUI application

Our 64-bit app is a very simple GUI: nothing really special here, a movie is chosen and passed to our faceless app launched in background as a NSTask, whose output is captured and parsed for IOSurfaceIDs. The interesting part is in the few lines that get the IOSurfaceID and build up a texture that we can use: the new call is CGLTexImageIOSurface2D, that is meant to be the IOSurface equivalent of glTexImage2D used in regular OpenGL to upload images.

Notes
The code is only good as a demo of the capabilities and for experimenting, in many aspects a real-world solution will use very different techniques!


Other posts on the same argument:

9 Comments:

At 25/9/09 17:09, Anonymous vade said...

Thanks very much for this!

 
At 2/10/09 22:07, Anonymous Anonimo said...

Thank you so much for the sample code. I am in the process of developing an IOSurface application and your code has really helped me.

 
At 5/10/09 09:58, Blogger George Toledo said...

This is great stuff, to say the least.

 
At 6/10/09 16:57, Blogger reachconection said...

I downloaded your application, and I couldn't compile it. I have so much questions regarding IOSurfaces. You seem like the only person I can find on the Internet who might be able to help. I posted my complete question on Stack Overflow. Please see if you can help.



http://stackoverflow.com/questions/1522151/need-help-with-iosurface-opengl-for-max-os-x-snow-leopard-application

 
At 6/10/09 21:29, Blogger Paolo said...

A compilation error sometimes happens as Xcode gets confused and tries to compile the 32 bit CLI application as 64 bit: that of course will not work. Usually, compiling the CLI first and subsequently the GUI will solve the issue.

 
At 2/11/09 23:12, Anonymous Anonimo said...

Make sure you turn off "Build Active ARchitecture Only" flag in the build property dialog for IOSurfaceCLI target. Then it will not give you compilation error any more.

Great article by the way!

 
At 30/11/09 09:45, Blogger emaskew said...

Intanto, bentornato!

Ti leggevo spesso, anche se non commentavo molto, e la sparizione per quasi due anni mi ha lasciato un po' orfano di cose interessanti da leggere..

Questo post (ed i due seguenti) riportano le cose alla normalità con interessantissimi argomenti!

 
At 22/12/09 08:49, Anonymous Anonimo said...

Thank you! Great stuff!

 
At 27/12/10 07:44, Anonymous jlstrecker said...

Thanks for the example. I'm having trouble getting it to work on 10.6.5, though. In the IOSurfaceTest application, nothing is showing up in the viewer; it stays gray. In the QC plugin, the image output is the right size but it's just a white rectangle. There are no errors in the console. Any idea what might be the problem?

 

Posta un commento

Links to this post:

Crea un link

<< Home