Skip to content

Commit

Permalink
Make BitmapInfo abstract and implement a strongly typed version in WP…
Browse files Browse the repository at this point in the history
…F and OffScreen
  • Loading branch information
amaitland committed Dec 16, 2014
1 parent 25203c5 commit 520349d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
11 changes: 5 additions & 6 deletions CefSharp.Core/Internals/RenderClientAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ namespace CefSharp
ClientAdapter(webBrowserInternal, onAfterBrowserCreated),
_webBrowserInternal(webBrowserInternal)
{
MainBitmapInfo = gcnew BitmapInfo();
PopupBitmapInfo = gcnew BitmapInfo();
PopupBitmapInfo->IsPopup = true;

_renderWebBrowser = dynamic_cast<IRenderWebBrowser^>(webBrowserInternal);

MainBitmapInfo = _renderWebBrowser->CreateBitmapInfo(false);
PopupBitmapInfo = _renderWebBrowser->CreateBitmapInfo(true);
}

~RenderClientAdapter()
Expand Down Expand Up @@ -184,8 +183,8 @@ namespace CefSharp
bitmapInfo->Width != newWidth ||
bitmapInfo->Height != newHeight)
{
//Clear the reference to InteropBitmap so a new one is created by InvokeRenderAsync
bitmapInfo->InteropBitmap = nullptr;
//Clear the reference to Bitmap so a new one is created by InvokeRenderAsync
bitmapInfo->ClearBitmap();

//Release the current handles (if not null)
ReleaseBitmapHandlers(backBufferHandle, fileMappingHandle);
Expand Down
1 change: 1 addition & 0 deletions CefSharp.OffScreen/CefSharp.OffScreen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ChromiumWebBrowser.cs" />
<Compile Include="GdiBitmapInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions CefSharp.OffScreen/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ int IRenderWebBrowser.Height
get { return size.Height; }
}

public BitmapInfo CreateBitmapInfo(bool isPopup)
{
return new GdiBitmapInfo { IsPopup = isPopup };
}

void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo)
{
lock (bitmapLock)
Expand Down
15 changes: 15 additions & 0 deletions CefSharp.OffScreen/GdiBitmapInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Drawing;
using CefSharp.Internals;

namespace CefSharp.OffScreen
{
public class GdiBitmapInfo : BitmapInfo
{
public Bitmap Bitmap { get; set; }

public override void ClearBitmap()
{
Bitmap = null;
}
}
}
1 change: 1 addition & 0 deletions CefSharp.Wpf/CefSharp.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<ItemGroup>
<Compile Include="DelegateCommand.cs" />
<Compile Include="DisposableEventWrapper.cs" />
<Compile Include="InteropBitmapInfo.cs" />
<Compile Include="IWpfWebBrowser.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
Expand Down
14 changes: 10 additions & 4 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,18 +664,24 @@ private IntPtr SourceHook(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam
return IntPtr.Zero;
}

public BitmapInfo CreateBitmapInfo(bool isPopup)
{
return new InteropBitmapInfo { IsPopup = isPopup };
}

void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo)
{
UiThreadRunAsync(delegate
{
lock (bitmapInfo.BitmapLock)
{
var interopBitmapInfo = (InteropBitmapInfo)bitmapInfo;
// Inform parents that the browser rendering is updating
OnRendering(this, bitmapInfo);
OnRendering(this, interopBitmapInfo);

var img = bitmapInfo.IsPopup ? popupImage : image;
// Now update the WPF image
var bitmap = bitmapInfo.InteropBitmap as InteropBitmap;
var bitmap = interopBitmapInfo.InteropBitmap;
if (bitmap == null)
{
img.Source = null;
Expand All @@ -688,7 +694,7 @@ void IRenderWebBrowser.InvokeRenderAsync(BitmapInfo bitmapInfo)
bitmapInfo.Width, bitmapInfo.Height, PixelFormat, stride, 0);
img.Source = bitmap;

bitmapInfo.InteropBitmap = bitmap;
interopBitmapInfo.InteropBitmap = bitmap;
}

bitmap.Invalidate();
Expand Down Expand Up @@ -1247,7 +1253,7 @@ void IRenderWebBrowser.SetCursor(IntPtr handle)
/// <summary>
/// Raises Rendering event
/// </summary>
protected virtual void OnRendering(object sender, BitmapInfo bitmapInfo)
protected virtual void OnRendering(object sender, InteropBitmapInfo bitmapInfo)
{
var rendering = Rendering;
if (rendering != null)
Expand Down
16 changes: 16 additions & 0 deletions CefSharp.Wpf/InteropBitmapInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Windows.Interop;
using CefSharp.Internals;

namespace CefSharp.Wpf
{
public class InteropBitmapInfo : BitmapInfo
{
// Cannot be InteropBitmap since we really don't want CefSharp to be dependent on WPF libraries.
public InteropBitmap InteropBitmap { get; set; }

public override void ClearBitmap()
{
InteropBitmap = null;
}
}
}
5 changes: 2 additions & 3 deletions CefSharp.Wpf/RenderingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using CefSharp.Internals;

namespace CefSharp.Wpf
{
Expand All @@ -12,15 +11,15 @@ namespace CefSharp.Wpf
/// </summary>
public class RenderingEventArgs : EventArgs
{
public RenderingEventArgs(BitmapInfo bitmapInfo)
public RenderingEventArgs(InteropBitmapInfo bitmapInfo)
{
BitmapInfo = bitmapInfo;
}

/// <summary>
/// The bitmap info being rendered.
/// </summary>
public BitmapInfo BitmapInfo { get; private set; }
public InteropBitmapInfo BitmapInfo { get; private set; }

}
}
7 changes: 3 additions & 4 deletions CefSharp/Internals/BitmapInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace CefSharp.Internals
{
public class BitmapInfo
public abstract class BitmapInfo
{
public object BitmapLock;
public IntPtr BackBufferHandle;
Expand All @@ -17,10 +17,9 @@ public class BitmapInfo

public IntPtr FileMappingHandle { get; set; }

// Cannot be InteropBitmap since we really don't want CefSharp to be dependent on WPF libraries.
public object InteropBitmap;
public abstract void ClearBitmap();

public BitmapInfo()
protected BitmapInfo()
{
BitmapLock = new object();
}
Expand Down
1 change: 1 addition & 0 deletions CefSharp/Internals/IRenderWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IRenderWebBrowser : IWebBrowserInternal
int Width { get; }
int Height { get; }

BitmapInfo CreateBitmapInfo(bool isPopup);
void InvokeRenderAsync(BitmapInfo bitmapInfo);

void SetCursor(IntPtr cursor);
Expand Down

0 comments on commit 520349d

Please sign in to comment.