Skip to content

Commit

Permalink
fix(xamlhost): Ensure proper render surface initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Feb 23, 2023
1 parent 82fc8e7 commit 0405a16
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 13 deletions.
13 changes: 12 additions & 1 deletion src/SamplesApp/UnoIslands.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@
</ListView>

<Grid x:Name="DetailsPane" DataContext="{Binding SelectedItem}" Grid.Column="1">
<xamlHost:UnoXamlHost InitialTypeName="UnoIslands.MainPage" />
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox x:Name="contentLoaded"
IsChecked="true"
Content="UnoXamlHost Loaded ?"
Checked="contentLoaded_Checked"
Unchecked="contentLoaded_Unchecked" />
<ContentControl Grid.Row="1" x:Name="hostContainer">
<xamlHost:UnoXamlHost x:Name="xamlHost" InitialTypeName="UnoIslands.MainPage" />
</ContentControl>
</Grid>
</Grid>
</Window>
29 changes: 28 additions & 1 deletion src/SamplesApp/UnoIslands.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.IO;
using System;
using System.IO;
using System.Windows;
using Newtonsoft.Json;
using Uno.UI.Skia.Platform;
using Uno.UI.XamlHost.Skia.Wpf;
using UnoIslands.Skia.Wpf;

namespace UnoIslands.WPF
Expand All @@ -11,11 +13,36 @@ namespace UnoIslands.WPF
/// </summary>
public partial class MainWindow : Window
{
UnoXamlHost _host;

public MainWindow()
{
InitializeComponent();

DataContext = new MainWindowViewModel();

_host = xamlHost;
}

private void contentLoaded_Checked(object sender, RoutedEventArgs e)
=> ChangeLoadedState();

private void contentLoaded_Unchecked(object sender, RoutedEventArgs e)
=> ChangeLoadedState();

private void ChangeLoadedState()
{
if (hostContainer is not null)
{
if (contentLoaded.IsChecked ?? false)
{
hostContainer.Content = _host;
}
else
{
hostContainer.Content = null;
}
}
}
}
}
53 changes: 46 additions & 7 deletions src/Uno.UI.Runtime.Skia.Wpf/Rendering/OpenGLWpfRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable

#nullable enable

using System;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -39,7 +40,23 @@ public OpenGLWpfRenderer(IWpfHost host)
public bool Initialize()
{
// Get the window from the wpf control
_hwnd = new WindowInteropHelper(Window.GetWindow(_hostControl)).Handle;
var hwnd = new WindowInteropHelper(Window.GetWindow(_hostControl)).Handle;

if (hwnd != 0 && hwnd == _hwnd)
{
if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Surface already initialized on the same window");
}

return true;
}
else
{
Release();
}

_hwnd = hwnd;

// Get the device context for the window
_hdc = NativeMethods.GetDC(_hwnd);
Expand Down Expand Up @@ -228,20 +245,42 @@ private GRContext TryBuildGRContext()

internal static GRContext CreateGRGLContext()
{
var glInterface = GRGlInterface.Create()
?? throw new NotSupportedException($"OpenGL is not supported in this system");
throw new NotSupportedException($"OpenGL is not supported in this system");

//var glInterface = GRGlInterface.Create()
// ?? throw new NotSupportedException($"OpenGL is not supported in this system");

var context = GRContext.CreateGl(glInterface)
?? throw new NotSupportedException($"OpenGL is not supported in this system (failed to create context)");
//var context = GRContext.CreateGl(glInterface)
// ?? throw new NotSupportedException($"OpenGL is not supported in this system (failed to create context)");

return context;
//return context;
}

public void Dispose()
{
Release();
}

private void Release()
{
// Cleanup resources
NativeMethods.wglDeleteContext(_glContext);
NativeMethods.ReleaseDC(_hwnd, _hdc);

_glContext = 0;
_hwnd = 0;
_hdc = IntPtr.Zero;

_grContext?.Dispose();
_grContext = null;

_renderTarget?.Dispose();
_renderTarget = null;

_surface?.Dispose();
_surface = null;

_backBuffer = null;
}

public SKSize CanvasSize
Expand Down
22 changes: 18 additions & 4 deletions src/Uno.UI.XamlHost.Skia.Wpf/UnoXamlHostBase.host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,30 @@ private void InitializeHost()

_designMode = DesignerProperties.GetIsInDesignMode(this);

InitializeRenderer();
SetupRenderer();
_hostPointerHandler = new HostPointerHandler(this);

Loaded += UnoXamlHostBase_Loaded;
Unloaded += UnoXamlHostBase_Unloaded;
}

private void UnoXamlHostBase_Unloaded(object sender, RoutedEventArgs e)
{
}

private void InitializeRenderer()
private void UnoXamlHostBase_Loaded(object sender, RoutedEventArgs e)
{
if (RenderSurfaceType is null)
if (!(_renderer?.Initialize() ?? false))
{
RenderSurfaceType = Uno.UI.Skia.RenderSurfaceType.OpenGL;
RenderSurfaceType = Uno.UI.Skia.RenderSurfaceType.Software;
SetupRenderer();
_renderer?.Initialize();
}
}

private void SetupRenderer()
{
RenderSurfaceType ??= Uno.UI.Skia.RenderSurfaceType.OpenGL;

_renderer = RenderSurfaceType switch
{
Expand Down

0 comments on commit 0405a16

Please sign in to comment.