CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

How to wait for render completed?

Open Bodekaer opened this issue 11 years ago • 23 comments

We're having a small challenge, where we would like to know when a CEF render has been completed. Do you have any recommendations on this?

E.g. I'm hoping for something like an event that's raised after each render update is completed:

 browser.RenderCompleted += () .... ;
 browser.ExecuteScriptAsync('document.body.innerHTML = ...);

Any tips?

Bodekaer avatar Feb 15 '15 10:02 Bodekaer

Typically I'd hook NavStateChanged and when IsLoading == false then the page has finished loading all of it's resources.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L876

There's also FrameLoadEnd, though it has some caviets and the upstream suggestion was that checking IsLoading was more reliable https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L57

Background: http://magpcss.org/ceforum/viewtopic.php?f=6&t=12361&p=23612&hilit=OnLoadingStateChange#p23612

I'd be surprised if there was anything related to rendering as it's not really a finite process, moving the mouse over a link, or pressing a key will cause additional frames to be rendered.

amaitland avatar Feb 15 '15 10:02 amaitland

Yeah, the loading events are good, and works fine. It's just that the FrameLoadEnd is called usually 10-20 milliseconds before the rendering is completed, which causes a tiny (yet annoying :) flicker effect.

I'd be ok with managing the rendering events. E.g. is there a way to see when the WPF Image source is updated and fully displayed?

Bodekaer avatar Feb 15 '15 11:02 Bodekaer

E.g. here we call Bitmap.Invalidate(). Any way to know when the Bitmap has been redrawn? https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L838

Bodekaer avatar Feb 15 '15 11:02 Bodekaer

E.g. here we call Bitmap.Invalidate(). Any way to know when the Bitmap has been redrawn? https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L838

If you put a breakpoint there you'll see that for even the simplest page that's called multiple times, once for each frame. So it's not really an accurate representation.

Out of curiosity did you try NavStateChanged? Also maybe can you use FrameLoadStart to inject a dom ready style handler and then callback into your .Net code?

amaitland avatar Feb 15 '15 11:02 amaitland

The public api documentation for 2171 is finally up to date, so you can try searching though there for a possible CEF extension point. It's usually the first place I start when searching for something (though up until just recently it was very out of date).

http://magpcss.org/ceforum/apidocs3/index-all.html

amaitland avatar Feb 15 '15 11:02 amaitland

It's ok that it's called several times actually. I just need to know when the first call is made, after I make a JS call to change the HTML. I've tried NavStateChanged and it's the same issue.

I think it might not be related to CEF actually, but rather the WPF Bitmap update we make is async, and thus causes a tiny delay of a few milliseconds. https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/ChromiumWebBrowser.cs#L812

Perhaps I can run that line in a synchronous way, or get an event when the bitmap is finished updating the UI?

Bodekaer avatar Feb 15 '15 11:02 Bodekaer

BitmapSource has some events, unsure if they'd work with the way we're copying directly into the buffer and calling Invalidate.

I have some more rendering tweaks waiting in a different branch, basically making the current InteropBitmap implementation pluggable. Gives a couple of extension points, including the ability to override the Invalidate wrapper method, maybe useful for you?

https://github.com/amaitland/CefSharp/compare/improvements/more-wpf-rendering-tweaks?expand=1#diff-9741d2fa2e2b11d82b23397b8af8b3b3R32

amaitland avatar Feb 15 '15 12:02 amaitland

Changes are now formally known as PR #813

amaitland avatar Feb 15 '15 12:02 amaitland

Ahh, that looks promising yes :) Basically, I hope then that when this line is completed: https://github.com/amaitland/CefSharp/compare/improvements/more-wpf-rendering-tweaks?expand=1#diff-5fce8b0a7a296ec9534272f46cadbfbcR45

It also means that any Image.ImageSource that is bound to this bitmap, will instantly update, without any async delay. Might require a little testing. I'd be happy to try it out when you feel it's ready.

Bodekaer avatar Feb 15 '15 13:02 Bodekaer

My testing shows that InteropBitmap is slightly faster, so from a performance point of view I'd be sticking with that. It's nice to have options though :smile: The WritableBitmap implementation is based on CefGlue so it should be pretty close to being ready for prime time.

https://bitbucket.org/xilium/xilium.cefglue/src/335450e6011dcaeb5afd5fd890fb6f0677d28325/CefGlue.WPF/WpfCefBrowser.cs?at=default#cl-786

You maybe able to just override Invalidate of the InteropBitmap.

amaitland avatar Feb 15 '15 13:02 amaitland

Cef has reportedly moved to a unified region update for DirtyRects, so the Cef.Glue code isn't quite a 1:1 translation.

amaitland avatar Feb 15 '15 13:02 amaitland

:+1:

Bodekaer avatar Feb 15 '15 13:02 Bodekaer

@Bodekaer Did PR #813 prove useful?

amaitland avatar Feb 19 '15 06:02 amaitland

Will test it soon. Probably today. I very much hope it'll work out :)

Bodekaer avatar Feb 23 '15 14:02 Bodekaer

Hey @amaitland, I was testing a bit here today, and noticed that with the latest master branch, if I go to e.g. https://www.google.com/doodles and scroll all the way down to the bottom of the page, it feels very sluggish/lagging, as it's updating the page and adding more items. I'm not sure if it's helpful, but it does seem like the UI is freezing during the updates sometimes.

Let me know if I can help debug any further.

Bodekaer avatar Feb 25 '15 21:02 Bodekaer

Hey Amaitland, I've done some more testing in Mailbird, and the results are promising! Seems to be less flicker at this stage, and seems to run very smoothly. I'll need to do more testing to fully be sure it all works well, but I'm happy about the results. Great work.

Question: Can I somehow know each time a render-cycle as finished with the new setup? E.g. can I listen to updates/changes to the rendered image via an event or similar?

Since the rendering in CEF is async, I still need to know when a render cycle has completed. That way, I will simply hide the browser, for the few milliseconds that it takes it to render, and that way I can completely remove any flickr.

Bodekaer avatar Feb 25 '15 22:02 Bodekaer

I was testing a bit here today, and noticed that with the latest master branch, if I go to e.g. https://www.google.com/doodles and scroll all the way down to the bottom of the page, it feels very sluggish/lagging, as it's updating the page and adding more items.

Are you still seeing problems? Which type of rendering are you using? What graphics card? Is your project using .Net 4.5? Potentially a custom version of InteropBitmap can be implemented with dirty rect invalidation that should be even faster than either of other two.

If you extend InteropBitmapInfo can you add your own event to Invalidate? That's the trigger for the UI to update, I believe it's currently executed on the UI thread.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs#L32

In general I only refactored the InteropBitmap solution, made a few tweaks to when things are called in an effort to speed them up. The WritableBitmap version is totally optional.

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/Rendering/BitmapFactory.cs#L13

amaitland avatar Feb 25 '15 23:02 amaitland

Not really experiencing any problems no. Seems to work fine.

Yeah, I did look into overriding Invalidate(). The challenge here is that it just starts the re-render process. I still don't know when the render process completed.

Is there e.g. a IsNoLongerInvalid event :)

Bodekaer avatar Feb 27 '15 21:02 Bodekaer

@amaitland, is there perhaps a way for me to clear the image? Turn it into a white blank image? Basically I would want to clear the current image/rendering, until the next render cycle is completed.

E.g. if I could call this, it would make me very happy :)

     image.Clear();
     image.Invalidate();

Bodekaer avatar Mar 03 '15 00:03 Bodekaer

Override InteropBitmapInfo.Invalidate()?

https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf/Rendering/InteropBitmapInfo.cs#L32

amaitland avatar Mar 03 '15 01:03 amaitland