Skip to content

Commit cbde8a8

Browse files
committed
Manually revert OnPaint rewrite changes that
Accidentally committed in fb05314
1 parent 4b422da commit cbde8a8

File tree

1 file changed

+79
-51
lines changed

1 file changed

+79
-51
lines changed

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,9 @@ public IRequestContext RequestContext
269269
public event EventHandler<LoadingStateChangedEventArgs> LoadingStateChanged;
270270

271271
/// <summary>
272-
/// Raised every time <see cref="IRenderWebBrowser.OnPaint"/> is called. You can access the underlying buffer, though it's
273-
/// preferable to either override <see cref="OnPaint"/> or implement your own <see cref="IBitmapFactory"/> as there is no outwardly
274-
/// accessible locking (locking is done within the default <see cref="IBitmapFactory"/> implementations).
275-
/// 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
272+
/// Raised before each render cycle, and allows you to adjust the bitmap before it's rendered/applied
276273
/// </summary>
277-
public event EventHandler<PaintEventArgs> Paint;
274+
public event EventHandler<RenderingEventArgs> Rendering;
278275

279276
/// <summary>
280277
/// Navigates to the previous page in the browser history. Will automatically be enabled/disabled depending on the
@@ -468,7 +465,7 @@ private void NoInliningConstructor()
468465

469466
ResourceHandlerFactory = new DefaultResourceHandlerFactory();
470467
BrowserSettings = new BrowserSettings();
471-
BitmapFactory = new InteropBitmapFactory();
468+
BitmapFactory = new BitmapFactory();
472469

473470
WpfKeyboardHandler = new WpfKeyboardHandler(this);
474471

@@ -519,7 +516,7 @@ protected virtual void Dispose(bool isDisposing)
519516
FrameLoadEnd = null;
520517
LoadError = null;
521518
LoadingStateChanged = null;
522-
Paint = null;
519+
Rendering = null;
523520

524521
if (isDisposing)
525522
{
@@ -655,6 +652,33 @@ bool IRenderWebBrowser.GetScreenPoint(int viewX, int viewY, out int screenX, out
655652
return true;
656653
}
657654

655+
/// Creates the BitmapInfo instance used for rendering. Two instances
656+
/// will be created, one will be used for the popup
657+
/// </summary>
658+
/// <param name="isPopup">if set to <c>true</c> [is popup].</param>
659+
/// <returns>BitmapInfo.</returns>
660+
/// <exception cref="System.Exception">BitmapFactory cannot be null</exception>
661+
BitmapInfo IRenderWebBrowser.CreateBitmapInfo(bool isPopup)
662+
{
663+
return CreateBitmapInfo(isPopup);
664+
}
665+
666+
/// <summary>
667+
/// Creates the BitmapInfo instance used for rendering. Two instances
668+
/// will be created, one will be used for the popup
669+
/// </summary>
670+
/// <param name="isPopup">if set to <c>true</c> [is popup].</param>
671+
/// <returns>BitmapInfo.</returns>
672+
/// <exception cref="System.Exception">BitmapFactory cannot be null</exception>
673+
protected virtual BitmapInfo CreateBitmapInfo(bool isPopup)
674+
{
675+
if (BitmapFactory == null)
676+
{
677+
throw new Exception("BitmapFactory cannot be null");
678+
}
679+
return BitmapFactory.CreateBitmap(isPopup, DpiScaleFactor);
680+
}
681+
658682
/// <summary>
659683
/// Starts the dragging.
660684
/// </summary>
@@ -702,46 +726,52 @@ void IRenderWebBrowser.UpdateDragCursor(DragOperationsMask operation)
702726

703727
/// <summary>
704728
/// Called when an element should be painted.
729+
/// Pixel values passed to this method are scaled relative to view coordinates based on the value of
730+
/// ScreenInfo.DeviceScaleFactor returned from GetScreenInfo. bitmapInfo.IsPopup indicates whether the element is the view
731+
/// or the popup widget. BitmapInfo.DirtyRect contains the set of rectangles in pixel coordinates that need to be
732+
/// repainted. The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin.
733+
/// The underlying buffer is copied into the back buffer and is accessible via BackBufferHandle
705734
/// </summary>
706-
/// <param name="type">indicates whether the element is the view or the popup widget.</param>
707-
/// <param name="dirtyRect">contains the set of rectangles in pixel coordinates that need to be repainted</param>
708-
/// <param name="buffer">The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin</param>
709-
/// <param name="width">width</param>
710-
/// <param name="height">height</param>
711-
void IRenderWebBrowser.OnPaint(PaintElementType type, Rect dirtyRect, IntPtr buffer, int width, int height)
735+
/// <param name="bitmapInfo">information about the bitmap to be rendered</param>
736+
void IRenderWebBrowser.OnPaint(BitmapInfo bitmapInfo)
712737
{
713-
OnPaint(type == PaintElementType.Popup, dirtyRect, buffer, width, height);
738+
OnPaint(bitmapInfo);
714739
}
715740

716741
/// <summary>
717-
/// Called when an element should be painted. Pixel values passed to this method are scaled relative to view coordinates based on the
718-
/// value of <see cref="ScreenInfo.DeviceScaleFactor"/> returned from <see cref="IRenderWebBrowser.GetScreenInfo"/>. To override the default behaviour
719-
/// override this method or implement your own <see cref="IBitmapFactory"/> and assign to <see cref="BitmapFactory"/>
720-
/// Called on the CEF UI Thread
742+
/// Called when an element should be painted.
743+
/// Pixel values passed to this method are scaled relative to view coordinates based on the value of
744+
/// ScreenInfo.DeviceScaleFactor returned from GetScreenInfo. bitmapInfo.IsPopup indicates whether the element is the view
745+
/// or the popup widget. BitmapInfo.DirtyRect contains the set of rectangles in pixel coordinates that need to be
746+
/// repainted. The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin.
747+
/// The underlying buffer is copied into the back buffer and is accessible via BackBufferHandle
721748
/// </summary>
722-
/// <param name="type">indicates whether the element is the view or the popup widget.</param>
723-
/// <param name="dirtyRect">contains the set of rectangles in pixel coordinates that need to be repainted</param>
724-
/// <param name="buffer">The bitmap will be will be width * height *4 bytes in size and represents a BGRA image with an upper-left origin</param>
725-
/// <param name="width">width</param>
726-
/// <param name="height">height</param>
727-
protected virtual void OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height)
749+
/// <param name="bitmapInfo">information about the bitmap to be rendered</param>
750+
protected virtual void OnPaint(BitmapInfo bitmapInfo)
728751
{
729-
var paint = Paint;
730-
if (paint != null)
752+
UiThreadRunAsync(delegate
731753
{
732-
var args = new PaintEventArgs(isPopup, dirtyRect, buffer, width, height);
754+
lock (bitmapInfo.BitmapLock)
755+
{
756+
var wpfBitmapInfo = (WpfBitmapInfo)bitmapInfo;
757+
// Inform parents that the browser rendering is updating
758+
OnRendering(this, wpfBitmapInfo);
733759

734-
paint(this, args);
760+
// Now update the WPF image
761+
if (wpfBitmapInfo.CreateNewBitmap)
762+
{
763+
var img = bitmapInfo.IsPopup ? popupImage : image;
735764

736-
if(args.Handled)
737-
{
738-
return;
739-
}
740-
}
765+
img.Source = null;
766+
GC.Collect(1);
741767

742-
var img = isPopup ? popupImage : image;
768+
img.Source = wpfBitmapInfo.CreateBitmap();
769+
}
743770

744-
BitmapFactory.CreateOrUpdateBitmap(isPopup, buffer, dirtyRect, width, height, img);
771+
wpfBitmapInfo.Invalidate();
772+
}
773+
},
774+
DispatcherPriority.Render);
745775
}
746776

747777
/// <summary>
@@ -1541,22 +1571,6 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
15411571
browser.GetHost().NotifyScreenInfoChanged();
15421572
}
15431573

1544-
//Ignore this for custom bitmap factories
1545-
if (BitmapFactory.GetType() == typeof(WritableBitmapFactory) || BitmapFactory.GetType() == typeof(InteropBitmapFactory))
1546-
{
1547-
if (DpiScaleFactor > 1.0 && BitmapFactory.GetType() != typeof(WritableBitmapFactory))
1548-
{
1549-
const int DefaultDpi = 96;
1550-
var scale = DefaultDpi * DpiScaleFactor;
1551-
1552-
BitmapFactory = new WritableBitmapFactory(scale, scale);
1553-
}
1554-
else if (DpiScaleFactor == 1.0 && BitmapFactory.GetType() != typeof(InteropBitmapFactory))
1555-
{
1556-
BitmapFactory = new InteropBitmapFactory();
1557-
}
1558-
}
1559-
15601574
var window = source.RootVisual as Window;
15611575
if(window != null)
15621576
{
@@ -2278,6 +2292,20 @@ public IJavascriptObjectRepository JavascriptObjectRepository
22782292
get { return managedCefBrowserAdapter == null ? null : managedCefBrowserAdapter.JavascriptObjectRepository; }
22792293
}
22802294

2295+
/// <summary>
2296+
/// Raises Rendering event
2297+
/// </summary>
2298+
/// <param name="sender">The sender.</param>
2299+
/// <param name="bitmapInfo">The bitmap information.</param>
2300+
protected virtual void OnRendering(object sender, WpfBitmapInfo bitmapInfo)
2301+
{
2302+
var rendering = Rendering;
2303+
if (rendering != null)
2304+
{
2305+
rendering(sender, new RenderingEventArgs(bitmapInfo));
2306+
}
2307+
}
2308+
22812309
/// <summary>
22822310
/// Returns the current IBrowser Instance
22832311
/// </summary>

0 commit comments

Comments
 (0)