Create another layer of abstraction around BitmapInfo - Allow for future possibility of implementing WritableBitmap in WPF

This commit is contained in:
amaitland
2015-01-16 12:22:40 +10:00
parent 4562b984ab
commit c2242e5158
8 changed files with 66 additions and 26 deletions
+1 -1
View File
@@ -343,7 +343,7 @@ namespace CefSharp.OffScreen
public BitmapInfo CreateBitmapInfo(bool isPopup)
{
//The bitmap buffer is 32 BPP
return new GdiBitmapInfo { IsPopup = isPopup, BytesPerPixel = 4, BitmapLock = bitmapLock };
return new GdiBitmapInfo { IsPopup = isPopup, BitmapLock = bitmapLock };
}
void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo)
+11 -1
View File
@@ -8,7 +8,17 @@ namespace CefSharp.OffScreen
{
public class GdiBitmapInfo : BitmapInfo
{
public bool IsCleared { get; set; }
public bool IsCleared { get; private set; }
public GdiBitmapInfo()
{
BytesPerPixel = 4;
}
public override bool CreateNewBitmap
{
get { return IsCleared; }
}
public override void ClearBitmap()
{
+1
View File
@@ -102,6 +102,7 @@
<Compile Include="ChromiumWebBrowser.cs" />
<Compile Include="RenderingEventArgs.cs" />
<Compile Include="WM.cs" />
<Compile Include="WpfBitmapInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CefSharp.Core\CefSharp.Core.vcxproj">
+7 -16
View File
@@ -20,8 +20,6 @@ namespace CefSharp.Wpf
{
public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrowser
{
private static readonly PixelFormat PixelFormat = PixelFormats.Bgra32;
private static readonly int BytesPerPixel = PixelFormat.BitsPerPixel / 8;
private HwndSource source;
private HwndSourceHook sourceHook;
private DispatcherTimer tooltipTimer;
@@ -636,7 +634,7 @@ namespace CefSharp.Wpf
public BitmapInfo CreateBitmapInfo(bool isPopup)
{
return new InteropBitmapInfo { IsPopup = isPopup, BytesPerPixel = BytesPerPixel };
return new InteropBitmapInfo { IsPopup = isPopup };
}
void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo)
@@ -645,29 +643,22 @@ namespace CefSharp.Wpf
{
lock (bitmapInfo.BitmapLock)
{
var interopBitmapInfo = (InteropBitmapInfo)bitmapInfo;
var wpfBitmapInfo = (WpfBitmapInfo)bitmapInfo;
// Inform parents that the browser rendering is updating
OnRendering(this, interopBitmapInfo);
OnRendering(this, wpfBitmapInfo);
// Now update the WPF image
var bitmap = interopBitmapInfo.InteropBitmap;
if (bitmap == null)
if (wpfBitmapInfo.CreateNewBitmap)
{
var img = bitmapInfo.IsPopup ? popupImage : image;
img.Source = null;
GC.Collect(1);
var stride = bitmapInfo.Width * bitmapInfo.BytesPerPixel;
bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(bitmapInfo.FileMappingHandle,
bitmapInfo.Width, bitmapInfo.Height, PixelFormat, stride, 0);
img.Source = bitmap;
interopBitmapInfo.InteropBitmap = bitmap;
img.Source = wpfBitmapInfo.CreateBitmap();
}
bitmap.Invalidate();
wpfBitmapInfo.Invalidate();
}
},
DispatcherPriority.Render);
@@ -1230,7 +1221,7 @@ namespace CefSharp.Wpf
/// <summary>
/// Raises Rendering event
/// </summary>
protected virtual void OnRendering(object sender, InteropBitmapInfo bitmapInfo)
protected virtual void OnRendering(object sender, WpfBitmapInfo bitmapInfo)
{
var rendering = Rendering;
if (rendering != null)
+31 -5
View File
@@ -1,16 +1,42 @@
using System.Windows.Interop;
using CefSharp.Internals;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace CefSharp.Wpf
{
public class InteropBitmapInfo : BitmapInfo
public class InteropBitmapInfo : WpfBitmapInfo
{
// Cannot be InteropBitmap since we really don't want CefSharp to be dependent on WPF libraries.
public InteropBitmap InteropBitmap { get; set; }
private static readonly PixelFormat PixelFormat = PixelFormats.Bgra32;
public InteropBitmap Bitmap { get; private set; }
public InteropBitmapInfo()
{
BytesPerPixel = PixelFormat.BitsPerPixel / 8;
}
public override bool CreateNewBitmap
{
get { return Bitmap == null; }
}
public override void ClearBitmap()
{
InteropBitmap = null;
Bitmap = null;
}
public override void Invalidate()
{
Bitmap.Invalidate();
}
public override BitmapSource CreateBitmap()
{
var stride = Width * BytesPerPixel;
Bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(FileMappingHandle, Width, Height, PixelFormat, stride, 0);
return Bitmap;
}
}
}
+2 -2
View File
@@ -11,7 +11,7 @@ namespace CefSharp.Wpf
/// </summary>
public class RenderingEventArgs : EventArgs
{
public RenderingEventArgs(InteropBitmapInfo bitmapInfo)
public RenderingEventArgs(WpfBitmapInfo bitmapInfo)
{
BitmapInfo = bitmapInfo;
}
@@ -19,7 +19,7 @@ namespace CefSharp.Wpf
/// <summary>
/// The bitmap info being rendered.
/// </summary>
public InteropBitmapInfo BitmapInfo { get; private set; }
public WpfBitmapInfo BitmapInfo { get; private set; }
}
}
+11
View File
@@ -0,0 +1,11 @@
using System.Windows.Media.Imaging;
using CefSharp.Internals;
namespace CefSharp.Wpf
{
public abstract class WpfBitmapInfo : BitmapInfo
{
public abstract void Invalidate();
public abstract BitmapSource CreateBitmap();
}
}
+2 -1
View File
@@ -17,8 +17,9 @@ namespace CefSharp.Internals
public IntPtr FileMappingHandle { get; set; }
public int BytesPerPixel { get; set; }
public int BytesPerPixel { get; protected set; }
public abstract bool CreateNewBitmap { get; }
public abstract void ClearBitmap();
protected BitmapInfo()