From cbde8a82deea6c528aa4ef876565d27301920160 Mon Sep 17 00:00:00 2001 From: amaitland Date: Mon, 22 Jan 2018 12:12:05 +1000 Subject: [PATCH] Manually revert OnPaint rewrite changes that Accidentally committed in https://github.com/cefsharp/CefSharp/commit/fb053142d7ae988ed9faf2bdbb6de4e320b1bde6 --- CefSharp.Wpf/ChromiumWebBrowser.cs | 134 +++++++++++++++++------------ 1 file changed, 81 insertions(+), 53 deletions(-) diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 2bdeb5e05..ed29445c3 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -269,12 +269,9 @@ namespace CefSharp.Wpf public event EventHandler LoadingStateChanged; /// - /// Raised every time is called. You can access the underlying buffer, though it's - /// preferable to either override or implement your own as there is no outwardly - /// accessible locking (locking is done within the default implementations). - /// It's important to note this event is fired on a CEF UI thread, which by default is not the same as your application UI thread + /// Raised before each render cycle, and allows you to adjust the bitmap before it's rendered/applied /// - public event EventHandler Paint; + public event EventHandler Rendering; /// /// Navigates to the previous page in the browser history. Will automatically be enabled/disabled depending on the @@ -468,7 +465,7 @@ namespace CefSharp.Wpf ResourceHandlerFactory = new DefaultResourceHandlerFactory(); BrowserSettings = new BrowserSettings(); - BitmapFactory = new InteropBitmapFactory(); + BitmapFactory = new BitmapFactory(); WpfKeyboardHandler = new WpfKeyboardHandler(this); @@ -519,7 +516,7 @@ namespace CefSharp.Wpf FrameLoadEnd = null; LoadError = null; LoadingStateChanged = null; - Paint = null; + Rendering = null; if (isDisposing) { @@ -655,6 +652,33 @@ namespace CefSharp.Wpf return true; } + /// Creates the BitmapInfo instance used for rendering. Two instances + /// will be created, one will be used for the popup + /// + /// if set to true [is popup]. + /// BitmapInfo. + /// BitmapFactory cannot be null + BitmapInfo IRenderWebBrowser.CreateBitmapInfo(bool isPopup) + { + return CreateBitmapInfo(isPopup); + } + + /// + /// Creates the BitmapInfo instance used for rendering. Two instances + /// will be created, one will be used for the popup + /// + /// if set to true [is popup]. + /// BitmapInfo. + /// BitmapFactory cannot be null + protected virtual BitmapInfo CreateBitmapInfo(bool isPopup) + { + if (BitmapFactory == null) + { + throw new Exception("BitmapFactory cannot be null"); + } + return BitmapFactory.CreateBitmap(isPopup, DpiScaleFactor); + } + /// /// Starts the dragging. /// @@ -702,46 +726,52 @@ namespace CefSharp.Wpf /// /// Called when an element should be painted. + /// Pixel values passed to this method are scaled relative to view coordinates based on the value of + /// ScreenInfo.DeviceScaleFactor returned from GetScreenInfo. bitmapInfo.IsPopup indicates whether the element is the view + /// or the popup widget. BitmapInfo.DirtyRect contains the set of rectangles in pixel coordinates that need to be + /// repainted. The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin. + /// The underlying buffer is copied into the back buffer and is accessible via BackBufferHandle /// - /// indicates whether the element is the view or the popup widget. - /// contains the set of rectangles in pixel coordinates that need to be repainted - /// The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin - /// width - /// height - void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height) + /// information about the bitmap to be rendered + void IRenderWebBrowser.OnPaint(BitmapInfo bitmapInfo) { - OnPaint(type == PaintElementType.Popup, dirtyRect, buffer, width, height); + OnPaint(bitmapInfo); } /// - /// Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates based on the - /// value of returned from . To override the default behaviour - /// override this method or implement your own and assign to - /// Called on the CEF UI Thread + /// Called when an element should be painted. + /// Pixel values passed to this method are scaled relative to view coordinates based on the value of + /// ScreenInfo.DeviceScaleFactor returned from GetScreenInfo. bitmapInfo.IsPopup indicates whether the element is the view + /// or the popup widget. BitmapInfo.DirtyRect contains the set of rectangles in pixel coordinates that need to be + /// repainted. The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin. + /// The underlying buffer is copied into the back buffer and is accessible via BackBufferHandle /// - /// indicates whether the element is the view or the popup widget. - /// contains the set of rectangles in pixel coordinates that need to be repainted - /// The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin - /// width - /// height - protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height) + /// information about the bitmap to be rendered + protected virtual void OnPaint(BitmapInfo bitmapInfo) { - var paint = Paint; - if (paint != null) + UiThreadRunAsync(delegate { - var args = new PaintEventArgs(isPopup, dirtyRect, buffer, width, height); - - paint(this, args); - - if(args.Handled) + lock (bitmapInfo.BitmapLock) { - return; + var wpfBitmapInfo = (WpfBitmapInfo)bitmapInfo; + // Inform parents that the browser rendering is updating + OnRendering(this, wpfBitmapInfo); + + // Now update the WPF image + if (wpfBitmapInfo.CreateNewBitmap) + { + var img = bitmapInfo.IsPopup ? popupImage : image; + + img.Source = null; + GC.Collect(1); + + img.Source = wpfBitmapInfo.CreateBitmap(); + } + + wpfBitmapInfo.Invalidate(); } - } - - var img = isPopup ? popupImage : image; - - BitmapFactory.CreateOrUpdateBitmap(isPopup, buffer, dirtyRect, width, height, img); + }, + DispatcherPriority.Render); } /// @@ -1541,22 +1571,6 @@ namespace CefSharp.Wpf browser.GetHost().NotifyScreenInfoChanged(); } - //Ignore this for custom bitmap factories - if (BitmapFactory.GetType() == typeof(WritableBitmapFactory) || BitmapFactory.GetType() == typeof(InteropBitmapFactory)) - { - if (DpiScaleFactor > 1.0 && BitmapFactory.GetType() != typeof(WritableBitmapFactory)) - { - const int DefaultDpi = 96; - var scale = DefaultDpi * DpiScaleFactor; - - BitmapFactory = new WritableBitmapFactory(scale, scale); - } - else if (DpiScaleFactor == 1.0 && BitmapFactory.GetType() != typeof(InteropBitmapFactory)) - { - BitmapFactory = new InteropBitmapFactory(); - } - } - var window = source.RootVisual as Window; if(window != null) { @@ -2278,6 +2292,20 @@ namespace CefSharp.Wpf get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; } } + /// + /// Raises Rendering event + /// + /// The sender. + /// The bitmap information. + protected virtual void OnRendering(object sender, WpfBitmapInfo bitmapInfo) + { + var rendering = Rendering; + if (rendering != null) + { + rendering(sender, new RenderingEventArgs(bitmapInfo)); + } + } + /// /// Returns the current IBrowser Instance ///