Windows Phone and Direct2D

I was quite surprised when the veil of secrecy over the Windows Phone 8 SDK was finally lifted and it was revealed that its API had in fact very little in common with the Windows 8 API.

Windows 8 essentially provides two APIs for writing (Metro) apps. There’s the XAML API and then there’s Direct3D. Yes, you can incorporate them in various ways but that’s beside the point. The first revelation was that Windows Phone 8 does not share Windows 8’s XAML API. There are various political/marketing/technical reasons for this that I won’t go into here, but needless to say I was not pleased. What a missed opportunity. I can only hope they will correct this very soon. But fair enough it is what it is so moving on.

I then looked further and noticed with some relief that Windows Phone 8 supports essentially the same Direct3D 11 API that Windows 8 does. There are some restrictions but it’s the same API. Great! I can write a native C++ app using Direct3D and by extension Direct2D. Direct2D is after all just a user-mode library built on top of Direct3D, but a very important one at that. It powers such notable apps as Internet Explorer on both Windows and Windows Phone.

Then to my horror, I get to the bottom of this page and I see this:

unavailable

Not deterred I downloaded the SDK, wrote the basic CoreApplication plumbing for a Direct3D app, wrapped the swap chain buffer in a Direct2D bitmap, set it as the target of the Direct2D device context, and dressed up some pixels.

Apart from some swap chain restrictions imposed by Direct3D 11 on the phone, the app is the same as what I would write for Windows 8. To get it to work I needed to pull in the headers and libs since the Windows Phone SDK does not include those for Direct2D and DirectWrite but that’s it. I fired up the emulator and sure enough, it worked:

WindowClipping (2)

That looked promising but being a skeptic I didn’t trust this as proof of concept. The emulator runs on Hyper-V, which does not yet emulate ARM so it has to be an x86 build of the Windows Phone. It is possible, I speculated, that somehow something is different enough on the actual devices that it won’t work. So I waited.

Well tonight my Windows Phone 8 device finally arrived and within minutes I had my USB cable out and deployed this exact same app to the phone and look at what I see in front of me right now:

IMG_3661

It just works. So I’m left wondering. Why is Direct2D “entirely unavailable” for phone applications? Direct2D is an incredibly powerful graphics library that would allow developers to write more compelling apps far more quickly for the Windows Phone.

I love the Windows Phone. It is an incredibly polished consumer device. It is based on the incredible Windows 8 operating system. But the official API leaves me wanting more.

Update: I’ve asked around at Microsoft. Apparently, you won’t be able to get into the app store if you use Direct2D. You’ve been warned.

Update 2: Some inside and outside of Microsoft have claimed or speculated that this was a hack. This is not a jailbroken phone. I did not copy any DLLs from Windows 8/RT. I have a Windows Phone Dev Center account that I used to deploy to the phone. I wrote the code using only Visual C++ 2012. I didn’t bother posting any code as I had my doubts about this getting past our friendly app store curators. If anything the point here is just that Windows Phone 8 has much more in common with Windows 8 than the Windows Phone API lets on.

Update 3: I’ve been asked to provide source code. Here it is. It’s basically the same as what you would write for Windows 8. There’s no Direct2D debug layer and the Direct3D swap chain is very different. In the follow-up to my Direct2D Fundamentals course at Pluralsight, I will be describing this code in detail, in particular the relationship between Direct2D, Direct3D and XAML, and how it plays out on Windows 8, RT and Windows Phone 8. So stay tuned.

Update 4: I can also confirm that the Windows Imaging Component (WIC) as well as the Windows Animation Manager both work great on Windows Phone 8. For the details of how to get it all to work, please check out my latest course: Direct2D Fundamentals – Part 2.

 

12 thoughts on “Windows Phone and Direct2D

  1. xyzzer

    I’m wondering if a Direct2D app would for whatever reason be rejected from Windows Phone Store. I’m almost sure Silverlight on Windows Phone is actually rendered using Direct2D (it would not make sense otherwise), but perhaps there is a reason why they would not want an app implemented directly in D2D published.

    Reply
    1. Dan

      The Silverlight XAML stack does not facilitate D2D (neither does the native XAML on Win8 … just for font rendering).

      Reply
      1. Kenny Kerr Post author

        That’s right. The various graphics stacks are only historically related. They are technically independent implementations. There is an increasing dependency on DirectWrite but that’s about it at this point.

      2. xyzzer

        Fair enough.

        Silverlight and Jupiter might not technically be dependent on D2D, DWrite or WIC libraries, but I am almost sure there is a huge amount of code sharing between these. I have been working on my own version of the composition engine for WinRT XAML to implement the missing WriteableBitmap.Render() method in D2D (http://winrtxamltoolkit.codeplex.com/SourceControl/changeset/view/fe0226ad68bb#WinRTXamlToolkit.Composition%2fRenderers%2fPathRenderer.cs) and the similarity of the APIs and identical rendering results I am getting point at large parts of the the rendering code being identical.

      3. Kenny Kerr Post author

        Oh, I don’t doubt that. There is much code sharing going on and certainly a lot of the Direct2D API was inspired by WPF and Silverlight. I just don’t want people to confuse this with a runtime dependency.

      4. xyzzer

        Sure and it is enlightening to learn that it doesn’t actually use D2D. Perhaps it uses the code stripping out the public APIs and some other overhead..

  2. guest

    just downloaded the pdb for Windows.UI.Xaml.dll and listed the modules using dia2dump.

    d2d related:

    e:\…\d2daccelerated.obj
    e:\…\d2dprinttarget.obj
    e:\…\d2dprintingdata.obj
    e:\…\d2dacceleratedprimitives.obj
    e:\…\d2dacceleratedrt.obj
    e:\…\d2dacceleratedbrushes.obj
    e:\…\d2dtextdrawingcontext.obj

    dwrite:

    e:\…\dwritefontandscriptservices.obj
    e:\…\dwritefontcollection.obj
    e:\…\dwritetextanalyzer.obj
    e:\…\dwritefontface.obj
    e:\…\dwriterenderingparams.obj
    e:\…\dwriteglyphrunanalysis.obj
    e:\…\dwritefontfamily.obj

    so not very much.

    Reply
      1. Kenny Kerr Post author

        Native XAML on Windows 8 uses DirectWrite for text layout and Direct2D for rendering the text. The phone’s XAML stack is older and does not yet use Direct2D or DirectWrite.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s