Software rendering mode - WPF
Asked Answered
F

3

21

I have a WPF user control for which I need to force rendering in RenderMode.SoftwareOnly. Since I am using .NET 3.5, I had to do something like this:

var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
if (hwndSource != null)
{
    hwndSource.CompositionTarget.RenderMode = RenderMode.SoftwareOnly;        
}

But this is not working on my application, The WPF program is crashing on few machines and turning off the hardware acceleration at the registry level seems to fix the issue.

The above code is written in the Loaded event of the window. If I am correct, Loaded event happens after the controls are rendered (MSDN). So does it make sense to have the above code in that event? If not, which event would be appropriate for it?

Also, will setting RenderMode on a visual affects its children? Or do I need to set this specifically for each child elements?

Feeling answered 9/2, 2011 at 22:2 Comment(1)
Hi, I'm curious. Has the dispatcher-solution helped you?Carey
D
35

Here's what we did:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    if (ForceSoftwareRendering)
    {
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        HwndTarget hwndTarget = hwndSource.CompositionTarget;
        hwndTarget.RenderMode = RenderMode.SoftwareOnly;
    }
}

It worked OK for us, EXCEPT... This needs to be done for every Window. In .NET 3.5 there was no way to make the setting take effect application-wide. And there are some windows that you won't have as much control over - for example, right-click "context" windows. We found that there was no good solution for .NET 3.5 except the registry setting.

Edited

Here's the logic we used to determine when to force software rendering. It was suggested by a Microsoft support engineer.

public bool ForceSoftwareRendering 
{
    get 
    { 
        int renderingTier = (System.Windows.Media.RenderCapability.Tier >> 16);
        return renderingTier == 0;
    }
}

In .NET 4 Microsoft added an application-wide setting that works perfectly for us. Its a much better option because you don't need to set it on every window. You just set it once and it applies to all windows.

System.Windows.Media.RenderOptions.ProcessRenderMode

Edited

The new .NET 4.0 property can be set at application startup like this:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        if (ForceSoftwareRendering)
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
    }
}
Dandy answered 9/2, 2011 at 22:23 Comment(5)
Where does "ForceSoftwareRendering" come from? Did you find a way to detect which machines would crash with software rendering?Hypothecate
@EduardoWada I'll update the answer to include this.Dandy
does anyone know something the same but for WinForms ?Ponytail
@user1722669, WinForms cannot use HW acceleration.Ophicleide
I have rendering problems in my WPF application that are solved by using SoftwareOnly. However, the System.Windows.Media.RenderCapability.Tier returns 0x20000 for this machine so the check says the machine should have supported it.Formenti
N
24

You can also disable hardware rendering for the whole process by putting the next line in the application startup handler:

RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

It is also possible to switch during runtime

Ninurta answered 13/6, 2012 at 9:32 Comment(1)
+1 for possible to switch during run-time. This is not immediately obvious from MSDN but it is important information.Unexceptional
C
4

event -problem
For the missing hwnd-source, try the following:

    Dispatcher.BeginInvoke(new Action(delegate {               
       HwndSource hwndSource = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
            if (null == hwndSource) {
                throw new InvalidOperationException("No HWND");
            }
            HwndTarget hwndTarget = hwndSource.CompositionTarget;
            hwndTarget.RenderMode = RenderMode.SoftwareOnly;

  }),System.Windows.Threading.DispatcherPriority.ContextIdle, null);

scope of RenderMode
As far as I know, there is only one Win32-window for each WPF window and all the rest is rendered native in WPF. That's why I think that setting RenderMode concerns all content in the window the visual was in. The scope is in this case window-wide.

Carey answered 9/2, 2011 at 22:32 Comment(2)
thanks. can you explain why Dispatcher is required in this case?Feeling
@appu: It is not required - But if the hwnd is not present at the execution time of the loaded event (if this is the problem), then waiting explicitely until the system has finished all it's important task (such as rendering:) may help. I already have seen a lot of uncommon scenarios with issues that could be resolved this way. But it's only an idea, I don't can guarantee that it works. By the way, I use the above code without the dispatcher and had not a problem up to now. But I only use it in one app and there it is a seldom used option. Therefore this statement surely is not representative.Carey

© 2022 - 2024 — McMap. All rights reserved.