Direct2D Windows Store app has distorted rendering (Windows 8.0)

There is a situation where your Direct2D rendering can be distorted.  This has been fixed in Windows 8.1 if you retarget your app for Windows 8.1 and rebuild it.  This problem occurs when you create your DirectX surfaces in one orientation of the device and before you attach the context to the Page, you rotate the device 90 degrees.  The problem is more easily created if you do something between the creation of the DirectX attributes and attach it (like read the application stored state).  The first thing you can do is eliminate or minimize the code that executes between the initialization in the DirectX code and attaching the Content to the current window!  The issue is easily recreated in the debugger as well.  To illustrate the problem and solution I will walk you through the problem and solution using the default app generated for a Direct2D Windows Store application in Visual Studio.

Recreate the Problem

Create a new Direct2D Windows Store app as pictured below:

image

Put a breakpoint here:

image

Build and run the app on a device that supports orientation change (or use the Simulator).

When you hit the breakpoint below, rotate the device 90 degrees and hit continue and you will see the display is wrong

screenshot_09062013_102945

Root Cause

This problem manifests itself because the DirectXPage is initialized in Landscape mode.  If between the time the page is created and the page is set as the Content for the Current Window the sizing is all off.

Solution

I considered something quite complicated in the beginning to try and track for orientation change and recreate the DirectX swap chain and resources but in reality the same problem could still manifest itself if the display rotates again!

The solution I chose is simple actually!

  • Open the Application Manifest and set the Supported Orientations to Landscape only.  This will prevent the Window from accepting orientation changes.

image

  • After attaching the DirectXPage to the Window Content, then allow all rotations with this line of code.

image

Note… you need to include the Display Namespace: using namespace Windows::Graphics::Display;

  • Now when you try to repro this issue you will notice you cannot!

Conclusion

Your are able to minimize the chance of the display being distorted by ensuring the minimizing the time between initializing DirectX and attaching to the window Content.  You can also use the trick above.  This does not solve the issue however it the user starts in portrait mode and rotates to landscape.  The best way to fix this issue is to wait for Windows 8.1 RTM and rebuild your app for that platform.

Let me know if this helps you out and follow us on @WSDevSol!

Jeff