CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

WPF - Rendering not refreshed at maximum framerate.

Open valhentai opened this issue 7 years ago • 18 comments
trafficstars

I noticed a strange behavior in the WPF CefSharp browser. When watching a video (YouTube for example), the video is not totaly fluid. but if something else is hapenning in the application, like an animation, the video is completly fluid. My theory is that the animation cause WPF do redraw at 60 FPS and it cause the CefSharp browser to redraw at this rate. When no animation is running, the default CefSharp redraw rate is smalller.

I tried differents values for the CefCommandLineArgs --off-screen-frame-rate ( 25, 30 , 60, 120 ) but the result is the same.

I am using The WPF nuget version 63.0.0 on WIndows 10. I use the following command line

settings.CefCommandLineArgs.Add("enable-gpu", "1");
settings.CefCommandLineArgs.Add("enable-webgl", "1");
settings.CefCommandLineArgs.Add("enable-begin-frame-scheduling", "1");
settings.CefCommandLineArgs.Add("--off-screen-frame-rate", "60");

with gpu disabled, the result is the same.

valhentai avatar Feb 12 '18 17:02 valhentai

This may not be due to CefSharp as it is just a wrapper around CEF. If you re-read the new issue template you should see a section on running the CEF sample application (cefclient) with offscreen rendering parameters. Could you try this and report back what you find?

Did you search old issues to see if this issue has been reported previously, and if so what did you find?

jankurianski avatar Feb 12 '18 23:02 jankurianski

I tried differents values for the CefCommandLineArgs --off-screen-frame-rate ( 25, 30 , 60, 120 ) but the result is the same.

It's my understanding that command line option is only implemented in the cefclient reference application. You can adjust the frame rate through BrowserSettings or a method on the IBrowserHost, you'll have to look up the API Doc or search the source to find the example property/method.

http://cefsharp.github.io/api/

amaitland avatar Feb 12 '18 23:02 amaitland

I ran the cefclient sample with the command cefclient.exe -off-screen-rendering-enabled and video run at full speed.

I can't reproduce the issue with Cef alone because it is linked to WPF. When the Wpf application is drawing something, the browser rendering is refreshed at full speed.

I tried BrowserSettings = new BrowserSettings() { WindowlessFrameRate = 60, }; but i change nothing.

I display something under the mouse cursor using a DrawingVisual and just passing the mouse over the application without clicking or without being above the browser is enough to have the browser render speedup.

valhentai avatar Feb 13 '18 08:02 valhentai

Do you think it's a problem with CefSharp? To me it sounds more of a problem with WPF.

amaitland avatar Feb 13 '18 08:02 amaitland

The rendering used for WPF has been rewritine, so you should have a much easier time debugging

https://github.com/cefsharp/CefSharp/issues/2237

amaitland avatar Feb 13 '18 09:02 amaitland

Maybe Changing the dispatcher priority in InteropBitmapFactory and WritableBitmapFactory will solve the problem. I already had a similar problem where a Render priority gave me a slugish rendering. putting the priority to DataBind gave me 60 FPS rendering.

I will download the CefSharp source and try this.

valhentai avatar Feb 13 '18 09:02 valhentai

If that is the case then the priority should be made configurable.

amaitland avatar Feb 13 '18 09:02 amaitland

I changed the priority of all the Dispatcher call I found in the WPF solution but nothing changed.

It seem to be a bug from WPF. The OnPaint() function in ChromiumWebBrowser is called with the same frequency when the rendering is slugish and when it is not.

valhentai avatar Feb 14 '18 14:02 valhentai

What version of .net do you have installed? You can try the different bitmap implementations, see if one is better. Have you tested on a different machine to verify it's not just a GPU driver issue?

amaitland avatar Feb 17 '18 06:02 amaitland

Can you provide a code sample of an animation you are using that forces the browser to render at a different speed?

amaitland avatar Mar 07 '18 01:03 amaitland

I just do

var rec = new Rectangle { Width = 100, Height = 100 };
var rotate = new RotateTransform();
rec.RenderTransform = rotate;
var anim = new DoubleAnimation
  {
        From = 0,
       To = 360,
       RepeatBehavior = RepeatBehavior.Forever
 };
rotate.BeginAnimation(RotateTransform.AngleProperty, anim);

the animated rectangle can be placed anywhere in the application.

I tried it on different machine with NVidia GPU (geforce GTX 960 and geforce GT 750M )

valhentai avatar Mar 07 '18 10:03 valhentai

Thanks

amaitland avatar Mar 07 '18 10:03 amaitland

https://github.com/dotnet/wpf/issues/1908 describes a similar behaviour in WPF.

amaitland avatar Apr 06 '20 02:04 amaitland

Adding an animation appears to workaround the issue.

If we have one or more active Rendering events it's the same as having an active animation so we know that we'll need to render another frame.

As per https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Media/MediaContext.cs,595

Using CompositionTarget.Rendering may improve performance if we are constantly updating the UI. There is already an experimental implementation of the CompositionTargetRenderHandler

Assign a new instance to http://cefsharp.github.io/api/79.1.x/html/P_CefSharp_Wpf_ChromiumWebBrowser_RenderHandler.htm

browser.RenderHandler = new CompositionTargetRenderHandler(browser, browser.DpiScaleFactor, browser.DpiScaleFactor);

amaitland avatar Apr 06 '20 02:04 amaitland

I have same problem, I have two windows computes.

first is xps15, run sample on that it can load page right.

second is Lenovo, run sample it load page to blank, when you Resize windows, the page show right.

image

the problem video

Dev-Wiki avatar Oct 18 '21 13:10 Dev-Wiki

WPF the browser stops redrawing when running on Intel Iris Xe Integrated GPU(11th Gen). Intel have released an updated driver, see https://www.intel.com/content/www/us/en/support/articles/000058136/graphics/graphics-for-11th-generation-intel-processors.html See dotnet/wpf#3817 for workaround if you are unable to install the updated driver.

As per the release notes https://github.com/cefsharp/CefSharp/releases/tag/v94.4.20

amaitland avatar Oct 18 '21 20:10 amaitland

@amaitland think you~

Dev-Wiki avatar Oct 19 '21 01:10 Dev-Wiki

@valhentai mentioned this solution and and said it didn't work

I tried BrowserSettings = new BrowserSettings() { WindowlessFrameRate = 60, }; but i change nothing.

It worked for me! Now getting 60 fps from my webgl2 web application.

BrowserSettings settings = new BrowserSettings();
settings.WindowlessFrameRate = 60;
settings.WebGl = CefState.Enabled;
ChromiumWebBrowser chrome_browser = new ChromiumWebBrowser(url)
{
    BrowserSettings = settings
};

barvaz2 avatar Jan 06 '22 21:01 barvaz2