From 73894a592dff01f8e140f2c667ebbd04f894d9bc Mon Sep 17 00:00:00 2001 From: amaitland Date: Sun, 18 Mar 2018 00:07:37 +1000 Subject: [PATCH] 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) --- CefSharp.Wpf/CefSharp.Wpf.csproj | 1 + .../IncreaseBufferInteropBitmapFactory.cs | 162 ++++++++++++++++++ .../Rendering/InteropBitmapFactory.cs | 63 +++---- 3 files changed, 197 insertions(+), 29 deletions(-) create mode 100644 CefSharp.Wpf/Rendering/Experimental/IncreaseBufferInteropBitmapFactory.cs diff --git a/CefSharp.Wpf/CefSharp.Wpf.csproj b/CefSharp.Wpf/CefSharp.Wpf.csproj index ae0e6ff1d..a38adea9c 100644 --- a/CefSharp.Wpf/CefSharp.Wpf.csproj +++ b/CefSharp.Wpf/CefSharp.Wpf.csproj @@ -92,6 +92,7 @@ + diff --git a/CefSharp.Wpf/Rendering/Experimental/IncreaseBufferInteropBitmapFactory.cs b/CefSharp.Wpf/Rendering/Experimental/IncreaseBufferInteropBitmapFactory.cs new file mode 100644 index 000000000..4e859aec3 --- /dev/null +++ b/CefSharp.Wpf/Rendering/Experimental/IncreaseBufferInteropBitmapFactory.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 +{ + /// + /// InteropBitmapFactory - creates/updates an InteropBitmap + /// Uses a MemoryMappedFile for double buffering, only ever creates a new buffer + /// when size increases + /// + /// + public class IncreaseBufferInteropBitmapFactory : IBitmapFactory + { + [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)] + private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count); + + /// + /// The pixel format + /// + 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; + + /// + /// Initializes a new instance of the class. + /// + /// priority at which the bitmap will be updated on the UI thread + 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; + } + } + } +} diff --git a/CefSharp.Wpf/Rendering/InteropBitmapFactory.cs b/CefSharp.Wpf/Rendering/InteropBitmapFactory.cs index 8f5d76753..1140e0e0e 100644 --- a/CefSharp.Wpf/Rendering/InteropBitmapFactory.cs +++ b/CefSharp.Wpf/Rendering/InteropBitmapFactory.cs @@ -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)