diff --git a/CefSharp.Wpf/CefSettings.cs b/CefSharp.Wpf/CefSettings.cs
index b37fe2825..f9d3af16e 100644
--- a/CefSharp.Wpf/CefSettings.cs
+++ b/CefSharp.Wpf/CefSettings.cs
@@ -21,10 +21,6 @@ namespace CefSharp.Wpf
//With OSR rendering it's fairly common for this to improve scrolling performace
//https://peter.sh/experiments/chromium-command-line-switches/#disable-threaded-scrolling
//CefCommandLineArgs.Add("disable-threaded-scrolling");
-
- //Disable GPU Compositing
- //Issue https://github.com/cefsharp/CefSharp/issues/3114
- CefCommandLineArgs.Add("disable-gpu-compositing");
}
}
}
diff --git a/CefSharp.Wpf/CefSharp.Wpf.csproj b/CefSharp.Wpf/CefSharp.Wpf.csproj
index f5ace3e9e..2cdb4d732 100644
--- a/CefSharp.Wpf/CefSharp.Wpf.csproj
+++ b/CefSharp.Wpf/CefSharp.Wpf.csproj
@@ -46,7 +46,7 @@
MinimumRecommendedRules.ruleset
bin\x64\Release\CefSharp.Wpf.XML
false
- true
+ true
true
@@ -68,7 +68,7 @@
MinimumRecommendedRules.ruleset
bin\x86\Release\CefSharp.Wpf.XML
false
- true
+ true
@@ -115,6 +115,7 @@
+
diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs
index bd87ef30f..fd515ec61 100644
--- a/CefSharp.Wpf/ChromiumWebBrowser.cs
+++ b/CefSharp.Wpf/ChromiumWebBrowser.cs
@@ -2442,7 +2442,7 @@ namespace CefSharp.Wpf
//as to not override their implementation.
//TODO: Add support for RenderHandler changing the DPI rather
//than creating a new instance (allow users to be notified of DPI change).
- if (RenderHandler == null || RenderHandler is WritableBitmapRenderHandler || RenderHandler is DirectWritableBitmapRenderHandler)
+ if (RenderHandler == null || RenderHandler is AllocHGlobalWritableBitmapRenderHandler || RenderHandler is DirectWritableBitmapRenderHandler)
{
const int DefaultDpi = 96;
var scale = DefaultDpi * DpiScaleFactor;
@@ -2454,7 +2454,7 @@ namespace CefSharp.Wpf
}
else
{
- RenderHandler = new WritableBitmapRenderHandler(scale, scale);
+ RenderHandler = new AllocHGlobalWritableBitmapRenderHandler(scale, scale);
}
oldRenderHandler?.Dispose();
diff --git a/CefSharp.Wpf/Rendering/AllocHGlobalWritableBitmapRenderHandler.cs b/CefSharp.Wpf/Rendering/AllocHGlobalWritableBitmapRenderHandler.cs
new file mode 100644
index 000000000..16e1370a0
--- /dev/null
+++ b/CefSharp.Wpf/Rendering/AllocHGlobalWritableBitmapRenderHandler.cs
@@ -0,0 +1,225 @@
+// Copyright © 2020 The CefSharp Authors. All rights reserved.
+//
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using Rect = CefSharp.Structs.Rect;
+
+namespace CefSharp.Wpf.Rendering
+{
+ ///
+ /// AllocHGlobalWritableBitmapRenderHandler - creates/updates an WritableBitmap
+ /// Uses to allocate memory for
+ /// double buffering when the size matches or creates a new WritableBitmap
+ /// when required.
+ ///
+ ///
+ public class AllocHGlobalWritableBitmapRenderHandler : IRenderHandler
+ {
+ private readonly double dpiX;
+ private readonly double dpiY;
+ private readonly PaintElement view;
+ private readonly PaintElement popup;
+ private readonly DispatcherPriority dispatcherPriority;
+ private readonly object lockObject = new object();
+
+ ///
+ /// The value for disposal, if it's 1 (one) then this instance is either disposed
+ /// or in the process of getting disposed
+ ///
+ private int disposeSignaled;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The dpi x.
+ /// The dpi y.
+ /// if true then only the direct rectangle will be updated, otherwise the whole bitmap will be redrawn
+ /// priority at which the bitmap will be updated on the UI thread
+ public AllocHGlobalWritableBitmapRenderHandler(double dpiX, double dpiY, bool invalidateDirtyRect = true, DispatcherPriority dispatcherPriority = DispatcherPriority.Render)
+ {
+ this.dpiX = dpiX;
+ this.dpiY = dpiY;
+ this.dispatcherPriority = dispatcherPriority;
+
+ view = new PaintElement(dpiX, dpiY, invalidateDirtyRect);
+ popup = new PaintElement(dpiX, dpiY, invalidateDirtyRect);
+ }
+
+ ///
+ /// Gets a value indicating whether this instance is disposed.
+ ///
+ /// if this instance is disposed; otherwise, .
+ public bool IsDisposed
+ {
+ get
+ {
+ return Interlocked.CompareExchange(ref disposeSignaled, 1, 1) == 1;
+ }
+ }
+
+ ///
+ /// Releases all resources used by the object
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// Releases unmanaged and - optionally - managed resources for the
+ ///
+ /// to release both managed and unmanaged resources; to release only unmanaged resources.
+ protected virtual void Dispose(bool disposing)
+ {
+ if (Interlocked.CompareExchange(ref disposeSignaled, 1, 0) != 0)
+ {
+ return;
+ }
+
+ if (!disposing)
+ {
+ return;
+ }
+
+ lock (lockObject)
+ {
+ view?.Dispose();
+ popup?.Dispose();
+ }
+ }
+
+ void IRenderHandler.OnAcceleratedPaint(bool isPopup, Rect dirtyRect, IntPtr sharedHandle)
+ {
+ //NOT USED
+ }
+
+ void IRenderHandler.OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height, Image image)
+ {
+ lock (lockObject)
+ {
+ if (IsDisposed || image.Dispatcher.HasShutdownStarted)
+ {
+ return;
+ }
+
+ var paintElement = isPopup ? popup : view;
+ paintElement?.UpdateBuffer(dirtyRect, buffer, width, height, image);
+ paintElement?.UpdateImage(lockObject);
+ }
+ }
+
+ //TODO: No nested classes and better name for this class
+ ///
+ /// Details of the bitmap to be rendered
+ ///
+ private class PaintElement
+ {
+ private readonly double dpiX;
+ private readonly double dpiY;
+ private Image image;
+ private int width;
+ private int height;
+ private Rect dirtyRect;
+ private IntPtr buffer;
+ private int bufferSize;
+ private int imageSize;
+ private readonly bool invalidateDirtyRect;
+
+ internal PaintElement(double dpiX, double dpiY, bool invalidateDirtyRect)
+ {
+ this.dpiX = dpiX;
+ this.dpiY = dpiY;
+ this.invalidateDirtyRect = invalidateDirtyRect;
+ }
+
+ internal void Dispose()
+ {
+ image = null;
+
+ if (buffer != IntPtr.Zero)
+ {
+ Marshal.FreeHGlobal(buffer);
+ buffer = IntPtr.Zero;
+ }
+ }
+
+ internal void UpdateBuffer(Rect dirtyRect, IntPtr sourceBuffer, int width, int height, Image image)
+ {
+ imageSize = (width * height) * AbstractRenderHandler.BytesPerPixel;
+
+ if (bufferSize < imageSize)
+ {
+ Marshal.FreeHGlobal(buffer);
+ buffer = Marshal.AllocHGlobal(imageSize);
+ bufferSize = imageSize;
+ }
+
+ this.width = width;
+ this.height = height;
+ this.dirtyRect = dirtyRect;
+
+ NativeMethodWrapper.MemoryCopy(buffer, sourceBuffer, imageSize);
+
+ this.image = image;
+ }
+
+ internal void UpdateImage(object lockObject)
+ {
+ image.Dispatcher.BeginInvoke((Action)(() =>
+ {
+ lock (lockObject)
+ {
+ if (image != null)
+ {
+ var bitmap = image.Source as WriteableBitmap;
+ var createNewBitmap = bitmap == null || bitmap.PixelWidth != width || bitmap.PixelHeight != height;
+ if (createNewBitmap)
+ {
+ if (image.Source != null)
+ {
+ image.Source = null;
+ GC.Collect(1);
+ }
+
+ image.Source = bitmap = new WriteableBitmap(width, height, dpiX, dpiY, AbstractRenderHandler.PixelFormat, null);
+ }
+
+ if (bitmap != null)
+ {
+ //By default we'll only update the dirty rect, for those that run into a MILERR_WIN32ERROR Exception (#2035)
+ //it's desirably to either upgrade to a newer .Net version (only client runtime needs to be installed, not compiled
+ //against a newer version. Or invalidate the whole bitmap
+ if (invalidateDirtyRect)
+ {
+ // Update the dirty region
+ var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height);
+
+ bitmap.Lock();
+ bitmap.WritePixels(sourceRect, buffer, imageSize, width * AbstractRenderHandler.BytesPerPixel, sourceRect.X, sourceRect.Y);
+ bitmap.Unlock();
+ }
+ else
+ {
+ // Update whole bitmap
+ var sourceRect = new Int32Rect(0, 0, width, height);
+
+ bitmap.Lock();
+ bitmap.WritePixels(sourceRect, buffer, imageSize, width * AbstractRenderHandler.BytesPerPixel, sourceRect.X, sourceRect.Y);
+ bitmap.Unlock();
+ }
+ }
+ }
+ }
+ }));
+ }
+ };
+ }
+}
diff --git a/CefSharp.Wpf/Rendering/Experimental/AllocHGlobalWritableBitmapRenderHandler.cs b/CefSharp.Wpf/Rendering/Experimental/AllocHGlobalWritableBitmapRenderHandler.cs
new file mode 100644
index 000000000..0cc7797af
--- /dev/null
+++ b/CefSharp.Wpf/Rendering/Experimental/AllocHGlobalWritableBitmapRenderHandler.cs
@@ -0,0 +1,225 @@
+// Copyright © 2020 The CefSharp Authors. All rights reserved.
+//
+// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+
+using System;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media.Imaging;
+using System.Windows.Threading;
+using Rect = CefSharp.Structs.Rect;
+
+namespace CefSharp.Wpf.Rendering.Experimental
+{
+ ///
+ /// AllocHGlobalWritableBitmapRenderHandler - creates/updates an WritableBitmap
+ /// Uses to allocate memory for
+ /// double buffering when the size matches or creates a new WritableBitmap
+ /// when required.
+ ///
+ ///
+ public class AllocHGlobalWritableBitmapRenderHandler : IRenderHandler
+ {
+ private readonly double dpiX;
+ private readonly double dpiY;
+ private readonly PaintElement view;
+ private readonly PaintElement popup;
+ private readonly DispatcherPriority dispatcherPriority;
+ private readonly object lockObject = new object();
+
+ ///
+ /// The value for disposal, if it's 1 (one) then this instance is either disposed
+ /// or in the process of getting disposed
+ ///
+ private int disposeSignaled;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The dpi x.
+ /// The dpi y.
+ /// if true then only the direct rectangle will be updated, otherwise the whole bitmap will be redrawn
+ /// priority at which the bitmap will be updated on the UI thread
+ public AllocHGlobalWritableBitmapRenderHandler(double dpiX, double dpiY, bool invalidateDirtyRect = true, DispatcherPriority dispatcherPriority = DispatcherPriority.Render)
+ {
+ this.dpiX = dpiX;
+ this.dpiY = dpiY;
+ this.dispatcherPriority = dispatcherPriority;
+
+ view = new PaintElement(dpiX, dpiY, invalidateDirtyRect);
+ popup = new PaintElement(dpiX, dpiY, invalidateDirtyRect);
+ }
+
+ ///
+ /// Gets a value indicating whether this instance is disposed.
+ ///
+ /// if this instance is disposed; otherwise, .
+ public bool IsDisposed
+ {
+ get
+ {
+ return Interlocked.CompareExchange(ref disposeSignaled, 1, 1) == 1;
+ }
+ }
+
+ ///
+ /// Releases all resources used by the object
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// Releases unmanaged and - optionally - managed resources for the
+ ///
+ /// to release both managed and unmanaged resources; to release only unmanaged resources.
+ protected virtual void Dispose(bool disposing)
+ {
+ if (Interlocked.CompareExchange(ref disposeSignaled, 1, 0) != 0)
+ {
+ return;
+ }
+
+ if (!disposing)
+ {
+ return;
+ }
+
+ lock (lockObject)
+ {
+ view?.Dispose();
+ popup?.Dispose();
+ }
+ }
+
+ void IRenderHandler.OnAcceleratedPaint(bool isPopup, Rect dirtyRect, IntPtr sharedHandle)
+ {
+ //NOT USED
+ }
+
+ void IRenderHandler.OnPaint(bool isPopup, Rect dirtyRect, IntPtr buffer, int width, int height, Image image)
+ {
+ lock (lockObject)
+ {
+ if (IsDisposed || image.Dispatcher.HasShutdownStarted)
+ {
+ return;
+ }
+
+ var paintElement = isPopup ? popup : view;
+ paintElement?.UpdateBuffer(dirtyRect, buffer, width, height, image);
+ paintElement?.UpdateImage(lockObject);
+ }
+ }
+
+ //TODO: No nested classes and better name for this class
+ ///
+ /// Details of the bitmap to be rendered
+ ///
+ private class PaintElement
+ {
+ private readonly double dpiX;
+ private readonly double dpiY;
+ private Image image;
+ private int width;
+ private int height;
+ private Rect dirtyRect;
+ private IntPtr buffer;
+ private int bufferSize;
+ private int imageSize;
+ private readonly bool invalidateDirtyRect;
+
+ internal PaintElement(double dpiX, double dpiY, bool invalidateDirtyRect)
+ {
+ this.dpiX = dpiX;
+ this.dpiY = dpiY;
+ this.invalidateDirtyRect = invalidateDirtyRect;
+ }
+
+ internal void Dispose()
+ {
+ image = null;
+
+ if (buffer != IntPtr.Zero)
+ {
+ Marshal.FreeHGlobal(buffer);
+ buffer = IntPtr.Zero;
+ }
+ }
+
+ internal void UpdateBuffer(Rect dirtyRect, IntPtr sourceBuffer, int width, int height, Image image)
+ {
+ imageSize = (width * height) * AbstractRenderHandler.BytesPerPixel;
+
+ if (bufferSize < imageSize)
+ {
+ Marshal.FreeHGlobal(buffer);
+ buffer = Marshal.AllocHGlobal(imageSize);
+ bufferSize = imageSize;
+ }
+
+ this.width = width;
+ this.height = height;
+ this.dirtyRect = dirtyRect;
+
+ NativeMethodWrapper.MemoryCopy(buffer, sourceBuffer, imageSize);
+
+ this.image = image;
+ }
+
+ internal void UpdateImage(object lockObject)
+ {
+ image.Dispatcher.BeginInvoke((Action)(() =>
+ {
+ lock (lockObject)
+ {
+ if (image != null)
+ {
+ var bitmap = image.Source as WriteableBitmap;
+ var createNewBitmap = bitmap == null || bitmap.PixelWidth != width || bitmap.PixelHeight != height;
+ if (createNewBitmap)
+ {
+ if (image.Source != null)
+ {
+ image.Source = null;
+ GC.Collect(1);
+ }
+
+ image.Source = bitmap = new WriteableBitmap(width, height, dpiX, dpiY, AbstractRenderHandler.PixelFormat, null);
+ }
+
+ if (bitmap != null)
+ {
+ //By default we'll only update the dirty rect, for those that run into a MILERR_WIN32ERROR Exception (#2035)
+ //it's desirably to either upgrade to a newer .Net version (only client runtime needs to be installed, not compiled
+ //against a newer version. Or invalidate the whole bitmap
+ if (invalidateDirtyRect)
+ {
+ // Update the dirty region
+ var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height);
+
+ bitmap.Lock();
+ bitmap.WritePixels(sourceRect, buffer, imageSize, width * AbstractRenderHandler.BytesPerPixel, sourceRect.X, sourceRect.Y);
+ bitmap.Unlock();
+ }
+ else
+ {
+ // Update whole bitmap
+ var sourceRect = new Int32Rect(0, 0, width, height);
+
+ bitmap.Lock();
+ bitmap.WritePixels(sourceRect, buffer, imageSize, width * AbstractRenderHandler.BytesPerPixel, sourceRect.X, sourceRect.Y);
+ bitmap.Unlock();
+ }
+ }
+ }
+ }
+ }));
+ }
+ };
+ }
+}