Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Per-monitor DPI-Aware: black window glitch with NVIDIA Optimus

I would like to make a Per-Monitor DPI-Aware Direct2D application. I have extended Microsoft's "First Direct2D Program" example to handle WM_DPICHANGED as explained in Kenny Kerr's MSDN article. This works when both monitors use one video card, but suffers from a glitch when using my laptop's NVIDIA Optimus setup.

I can reproduce the same glitch by running the Per-Monitor Aware WPF Sample with these steps:

  • Different DPI scaling factor on each monitor.
  • Optimus enabled (laptop display on integrated graphics, external monitor on Quadro card)
  • The app starts on the primary display - the external monitor on the right.
  • Drag it over to the left, and it properly handles DPI change.
  • Drag it back to the right. When it handles DPI change, the portion of the window on the right monitor becomes black. The portion on the left is still drawn properly.

(See video of this bug)

I have only seen this bug with the above example app, and when I try to add Kerr's WM_DPICHANGED handler to a simpler example. Suspiciously, I have seen other apps (Chrome, Visual Studio itself) show a similar black window, but only temporarily, if I drag between monitors and maximize them very quickly.

So - is anyone familiar with this glitch? Is it some bug in my display drivers? Or is there something other apps do to rectify it, which the example code does not?

like image 700
Lack Avatar asked Jul 03 '16 16:07

Lack


1 Answers

NVIDIA Optimus you say? Those drivers are buggy as sin. Try initializing the render target with the D2D1_PRESENT_OPTIONS_RETAIN_CONTENTS flag.

This was something that took me forever to figure out back in 2014 when I was finishing up Paint.NET 4.0, and I've still got a comment in the code warning me to never turn it off:

private PresentOptions hwndPresentOptions = 
    PresentOptions.Immediately | 
    PresentOptions.RetainContents; // If we don't use RetainContents, then we get awful
                                   // black flickering and mouse trails on some hardware
                                   // (e.g. NVIDIA Optimus)
like image 62
Rick Brewster Avatar answered Nov 16 '22 09:11

Rick Brewster