WPF OSR Rendering - InteropBitmapFactory split the experimental resize on increase option into IncreaseBufferInteropBitmapFactory

Also some minor threading improvements and buffer validation (check if closed and invalid)
This commit is contained in:
amaitland
2018-03-18 00:07:37 +10:00
parent c37dbcd06a
commit 73894a592d
3 changed files with 197 additions and 29 deletions
+1
View File
@@ -92,6 +92,7 @@
<AppDesigner Include="Properties\" />
<Compile Include="ChromiumWebBrowser.cs" />
<Compile Include="PaintEventArgs.cs" />
<Compile Include="Rendering\Experimental\IncreaseBufferInteropBitmapFactory.cs" />
<Compile Include="Rendering\InteropBitmapFactory.cs" />
<Compile Include="Rendering\WritableBitmapFactory.cs" />
<Compile Include="WM.cs" />
@@ -0,0 +1,162 @@
// Copyright © 2010-2017 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.IO.MemoryMappedFiles;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Runtime.InteropServices;
using System.Windows.Threading;
namespace CefSharp.Wpf.Rendering.Experimental
{
/// <summary>
/// InteropBitmapFactory - creates/updates an InteropBitmap
/// Uses a MemoryMappedFile for double buffering, only ever creates a new buffer
/// when size increases
/// </summary>
/// <seealso cref="CefSharp.IBitmapFactory" />
public class IncreaseBufferInteropBitmapFactory : IBitmapFactory
{
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
/// <summary>
/// The pixel format
/// </summary>
private static readonly PixelFormat PixelFormat = PixelFormats.Bgra32;
private static int BytesPerPixel = PixelFormat.BitsPerPixel / 8;
private object lockObject = new object();
private Size viewSize;
private Size popupSize;
private DispatcherPriority dispatcherPriority;
private MemoryMappedFile viewMemoryMappedFile;
private MemoryMappedFile popupMemoryMappedFile;
private MemoryMappedViewAccessor viewMemoryMappedViewAccessor;
private MemoryMappedViewAccessor popupMemoryMappedViewAccessor;
/// <summary>
/// Initializes a new instance of the <see cref="InteropBitmapFactory"/> class.
/// </summary>
/// <param name="dispatcherPriority">priority at which the bitmap will be updated on the UI thread</param>
public IncreaseBufferInteropBitmapFactory(DispatcherPriority dispatcherPriority = DispatcherPriority.Render)
{
this.dispatcherPriority = dispatcherPriority;
}
public void Dispose()
{
ReleaseMemoryMappedView(ref popupMemoryMappedFile, ref popupMemoryMappedViewAccessor);
ReleaseMemoryMappedView(ref viewMemoryMappedFile, ref viewMemoryMappedViewAccessor);
}
void IBitmapFactory.CreateOrUpdateBitmap(bool isPopup, IntPtr buffer, Rect dirtyRect, int width, int height, Image image)
{
if (isPopup)
{
CreateOrUpdateBitmap(isPopup, buffer, dirtyRect, width, height, image, ref popupSize, ref popupMemoryMappedFile, ref popupMemoryMappedViewAccessor);
}
else
{
CreateOrUpdateBitmap(isPopup, buffer, dirtyRect, width, height, image, ref viewSize, ref viewMemoryMappedFile, ref viewMemoryMappedViewAccessor);
}
}
private void CreateOrUpdateBitmap(bool isPopup, IntPtr buffer, Rect dirtyRect, int width, int height, Image image, ref Size currentSize, ref MemoryMappedFile mappedFile, ref MemoryMappedViewAccessor viewAccessor)
{
if (image.Dispatcher.HasShutdownStarted)
{
return;
}
var createNewBitmap = false;
lock (lockObject)
{
int pixels = width * height;
int numberOfBytes = pixels * BytesPerPixel;
createNewBitmap = mappedFile == null || currentSize.Height != height || currentSize.Width != width;
if (createNewBitmap)
{
//If the MemoryMappedFile is smaller than we need then create a larger one
//If it's larger then we need then rather than going through the costly expense of
//allocating a new one we'll just use the old one and only access the number of bytes we require.
if (viewAccessor == null || viewAccessor.Capacity < numberOfBytes)
{
ReleaseMemoryMappedView(ref mappedFile, ref viewAccessor);
mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite);
viewAccessor = mappedFile.CreateViewAccessor();
}
currentSize.Height = height;
currentSize.Width = width;
}
//TODO: Performance analysis to determine which is the fastest memory copy function
//NativeMethodWrapper.CopyMemoryUsingHandle(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, numberOfBytes);
CopyMemory(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, (uint)numberOfBytes);
//Take a reference to the backBufferHandle, once we're on the UI thread we need to check if it's still valid
var backBufferHandle = mappedFile.SafeMemoryMappedFileHandle;
image.Dispatcher.BeginInvoke((Action)(() =>
{
lock (lockObject)
{
if (backBufferHandle.IsClosed || backBufferHandle.IsInvalid)
{
return;
}
if (createNewBitmap)
{
if (image.Source != null)
{
image.Source = null;
GC.Collect(1);
}
var stride = width * BytesPerPixel;
var bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(backBufferHandle.DangerousGetHandle(), width, height, PixelFormat, stride, 0);
image.Source = bitmap;
}
else
{
if (image.Source != null)
{
var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height);
var bitmap = (InteropBitmap)image.Source;
bitmap.Invalidate(sourceRect);
}
}
}
}), dispatcherPriority);
}
}
private void ReleaseMemoryMappedView(ref MemoryMappedFile mappedFile, ref MemoryMappedViewAccessor stream)
{
if (stream != null)
{
stream.Dispose();
stream = null;
}
if (mappedFile != null)
{
mappedFile.Dispose();
mappedFile = null;
}
}
}
}
+34 -29
View File
@@ -86,17 +86,11 @@ namespace CefSharp.Wpf.Rendering
if (createNewBitmap)
{
//If the MemoryMappedFile is smaller than we need then create a larger one
//If it's larger then we need then rather than going through the costly expense of
//allocating a new one we'll just use the old one and only access the number of bytes we require.
if (viewAccessor == null || viewAccessor.Capacity < numberOfBytes)
{
ReleaseMemoryMappedView(ref mappedFile, ref viewAccessor);
ReleaseMemoryMappedView(ref mappedFile, ref viewAccessor);
mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite);
mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite);
viewAccessor = mappedFile.CreateViewAccessor();
}
viewAccessor = mappedFile.CreateViewAccessor();
currentSize.Height = height;
currentSize.Width = width;
@@ -105,34 +99,45 @@ namespace CefSharp.Wpf.Rendering
//TODO: Performance analysis to determine which is the fastest memory copy function
//NativeMethodWrapper.CopyMemoryUsingHandle(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, numberOfBytes);
CopyMemory(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, (uint)numberOfBytes);
}
image.Dispatcher.BeginInvoke((Action)(() =>
{
lock (lockObject)
//Take a reference to the backBufferHandle, once we're on the UI thread we need to check if it's still valid
var backBufferHandle = mappedFile.SafeMemoryMappedFileHandle;
//Invoke on the WPF UI Thread
image.Dispatcher.BeginInvoke((Action)(() =>
{
if (createNewBitmap)
lock (lockObject)
{
if (image.Source != null)
if (backBufferHandle.IsClosed || backBufferHandle.IsInvalid)
{
image.Source = null;
GC.Collect(1);
return;
}
var backBufferHandle = isPopup ? popupMemoryMappedFile.SafeMemoryMappedFileHandle : viewMemoryMappedFile.SafeMemoryMappedFileHandle;
if (createNewBitmap)
{
if (image.Source != null)
{
image.Source = null;
//TODO: Is this still required in newer versions of .Net?
GC.Collect(1);
}
var stride = width * BytesPerPixel;
var bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(backBufferHandle.DangerousGetHandle(), width, height, PixelFormat, stride, 0);
image.Source = bitmap;
var stride = width * BytesPerPixel;
var bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(backBufferHandle.DangerousGetHandle(), width, height, PixelFormat, stride, 0);
image.Source = bitmap;
}
else
{
if (image.Source != null)
{
var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height);
var bitmap = (InteropBitmap)image.Source;
bitmap.Invalidate(sourceRect);
}
}
}
else
{
var sourceRect = new Int32Rect(dirtyRect.X, dirtyRect.Y, dirtyRect.Width, dirtyRect.Height);
var bitmap = (InteropBitmap)image.Source;
bitmap.Invalidate(sourceRect);
}
}
}), dispatcherPriority);
}), dispatcherPriority);
}
}
private void ReleaseMemoryMappedView(ref MemoryMappedFile mappedFile, ref MemoryMappedViewAccessor stream)