mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-06 04:00:07 +00:00
WPF - Add AllocHGlobalWritableBitmapRenderHandler
A port of WritableBitmapRenderHandler to use Marshal.AllocHGlobal instead of MemoryMappedFile Remove disable-gpu-compositing command line arg Resovles #3114
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<DocumentationFile>bin\x64\Release\CefSharp.Wpf.XML</DocumentationFile>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -68,7 +68,7 @@
|
||||
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
|
||||
<DocumentationFile>bin\x86\Release\CefSharp.Wpf.XML</DocumentationFile>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
@@ -115,6 +115,7 @@
|
||||
<Compile Include="ChromiumWebBrowser.cs" />
|
||||
<Compile Include="PaintEventArgs.cs" />
|
||||
<Compile Include="Rendering\AbstractRenderHandler.cs" />
|
||||
<Compile Include="Rendering\AllocHGlobalWritableBitmapRenderHandler.cs" />
|
||||
<Compile Include="Rendering\Experimental\CompositionTargetRenderHandler.cs" />
|
||||
<Compile Include="Rendering\DirectWritableBitmapRenderHandler.cs" />
|
||||
<Compile Include="Rendering\Experimental\ByteArrayWritableBitmapRenderHandler.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();
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// AllocHGlobalWritableBitmapRenderHandler - creates/updates an WritableBitmap
|
||||
/// Uses <see cref="Marshal.AllocHGlobal"/> to allocate memory for
|
||||
/// double buffering when the size matches or creates a new WritableBitmap
|
||||
/// when required.
|
||||
/// </summary>
|
||||
/// <seealso cref="IRenderHandler" />
|
||||
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();
|
||||
|
||||
/// <summary>
|
||||
/// The value for disposal, if it's 1 (one) then this instance is either disposed
|
||||
/// or in the process of getting disposed
|
||||
/// </summary>
|
||||
private int disposeSignaled;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AllocHGlobalWritableBitmapRenderHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dpiX">The dpi x.</param>
|
||||
/// <param name="dpiY">The dpi y.</param>
|
||||
/// <param name="invalidateDirtyRect">if true then only the direct rectangle will be updated, otherwise the whole bitmap will be redrawn</param>
|
||||
/// <param name="dispatcherPriority">priority at which the bitmap will be updated on the UI thread</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this instance is disposed.
|
||||
/// </summary>
|
||||
/// <value><see langword="true"/> if this instance is disposed; otherwise, <see langword="true"/>.</value>
|
||||
public bool IsDisposed
|
||||
{
|
||||
get
|
||||
{
|
||||
return Interlocked.CompareExchange(ref disposeSignaled, 1, 1) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases all resources used by the <see cref="AbstractRenderHandler"/> object
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and - optionally - managed resources for the <see cref="AbstractRenderHandler"/>
|
||||
/// </summary>
|
||||
/// <param name="disposing"><see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</param>
|
||||
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
|
||||
/// <summary>
|
||||
/// Details of the bitmap to be rendered
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// AllocHGlobalWritableBitmapRenderHandler - creates/updates an WritableBitmap
|
||||
/// Uses <see cref="Marshal.AllocHGlobal"/> to allocate memory for
|
||||
/// double buffering when the size matches or creates a new WritableBitmap
|
||||
/// when required.
|
||||
/// </summary>
|
||||
/// <seealso cref="IRenderHandler" />
|
||||
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();
|
||||
|
||||
/// <summary>
|
||||
/// The value for disposal, if it's 1 (one) then this instance is either disposed
|
||||
/// or in the process of getting disposed
|
||||
/// </summary>
|
||||
private int disposeSignaled;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WritableBitmapRenderHandler"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dpiX">The dpi x.</param>
|
||||
/// <param name="dpiY">The dpi y.</param>
|
||||
/// <param name="invalidateDirtyRect">if true then only the direct rectangle will be updated, otherwise the whole bitmap will be redrawn</param>
|
||||
/// <param name="dispatcherPriority">priority at which the bitmap will be updated on the UI thread</param>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this instance is disposed.
|
||||
/// </summary>
|
||||
/// <value><see langword="true"/> if this instance is disposed; otherwise, <see langword="true"/>.</value>
|
||||
public bool IsDisposed
|
||||
{
|
||||
get
|
||||
{
|
||||
return Interlocked.CompareExchange(ref disposeSignaled, 1, 1) == 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases all resources used by the <see cref="AbstractRenderHandler"/> object
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases unmanaged and - optionally - managed resources for the <see cref="AbstractRenderHandler"/>
|
||||
/// </summary>
|
||||
/// <param name="disposing"><see langword="true" /> to release both managed and unmanaged resources; <see langword="false" /> to release only unmanaged resources.</param>
|
||||
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
|
||||
/// <summary>
|
||||
/// Details of the bitmap to be rendered
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user