@ -1,4 +1,6 @@
using System ;
using System.Diagnostics.Tracing ;
using System.Linq ;
using Avalonia.OpenGL ;
using Avalonia.Platform ;
using Avalonia.Win32.DirectX ;
@ -6,72 +8,88 @@ using Avalonia.Win32.OpenGl;
using Avalonia.Win32.OpenGl.Angle ;
using Avalonia.Win32.WinRT.Composition ;
namespace Avalonia.Win32
namespace Avalonia.Win32 ;
static class Win32GlManager
{
static class Win32GlManager
public static IPlatformGraphics ? Initialize ( )
{
public static IPlatformGraphics ? Initialize ( )
{
var gl = InitializeCore ( ) ;
if ( gl is not null )
{
AvaloniaLocator . CurrentMutable . Bind < IPlatformGraphics > ( ) . ToConstant ( gl ) ;
}
var gl = InitializeCore ( ) ;
return gl ;
if ( gl is not null )
{
AvaloniaLocator . CurrentMutable . Bind < IPlatformGraphics > ( ) . ToConstant ( gl ) ;
}
return gl ;
}
private static IPlatformGraphics ? InitializeCore ( )
private static IPlatformGraphics ? InitializeCore ( )
{
var opts = AvaloniaLocator . Current . GetService < Win32PlatformOptions > ( ) ? ? new Win32PlatformOptions ( ) ;
if ( opts . RenderingMode is null | | ! opts . RenderingMode . Any ( ) )
{
var opts = AvaloniaLocator . Current . GetService < Win32PlatformOptions > ( ) ? ? new Win32PlatformOptions ( ) ;
throw new InvalidOperationException ( $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.RenderingMode)} must not be empty or null" ) ;
}
var winVersion = Win32Platform . WindowsVersion ;
var renderingMode = opts . RenderingMode ? ?
( winVersion > PlatformConstants . Windows7 ? Win32RenderingMode . AngleEgl : Win32RenderingMode . Software ) ;
if ( renderingMode = = Win32RenderingMode . Wgl )
foreach ( var renderingMode in opts . RenderingMode )
{
if ( renderingMode = = Win32RenderingMode . Software )
{
var wgl = WglPlatformOpenGlInterface . TryCreate ( ) ;
return wgl ;
return null ;
}
if ( renderingMode = = Win32RenderingMode . AngleEgl )
{
var egl = AngleWin32PlatformGraphics . TryCreate ( AvaloniaLocator . Current . GetService < AngleOptions > ( ) ? ?
new ( ) ) ;
var egl = AngleWin32PlatformGraphics . TryCreate ( AvaloniaLocator . Current . GetService < AngleOptions > ( ) ? ? new ( ) ) ;
if ( egl ! = null & & egl . PlatformApi = = AngleOptions . PlatformApi . DirectX11 )
{
AvaloniaLocator . CurrentMutable . Bind < IPlatformGraphicsOpenGlContextFactory > ( )
. ToConstant ( egl ) ;
var compositionMode = opts . CompositionMode ? ?
( WinUiCompositorConnection . IsSupported ( ) ? Win32CompositionMode . WinUIComposition
//: DirectCompositionConnection.IsSupported() ? Win32CompositionMode.DirectComposition
: Win32CompositionMode . RedirectionSurface ) ;
TryRegisterComposition ( opts ) ;
return egl ;
}
}
switch ( compositionMode )
{
case Win32CompositionMode . WinUIComposition :
if ( ! WinUiCompositorConnection . TryCreateAndRegister ( ) )
{
//goto case Win32CompositionMode.DirectComposition;
}
break ;
//case Win32CompositionMode.DirectComposition:
//DirectCompositionConnection.TryCreateAndRegister();
//break;
case Win32CompositionMode . LowLatencyDxgiSwapChain :
DxgiConnection . TryCreateAndRegister ( ) ;
break ;
}
if ( renderingMode = = Win32RenderingMode . Wgl )
{
if ( WglPlatformOpenGlInterface . TryCreate ( ) is { } wgl )
{
return wgl ;
}
}
}
throw new InvalidOperationException ( $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.RenderingMode)} has a value of \" { string . Join ( ", " , opts . RenderingMode ) } \ ", but no options were applied." ) ;
}
private static void TryRegisterComposition ( Win32PlatformOptions opts )
{
if ( opts . CompositionMode is null | | ! opts . CompositionMode . Any ( ) )
{
throw new InvalidOperationException ( $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CompositionMode)} must not be empty or null" ) ;
}
return egl ;
foreach ( var compositionMode in opts . CompositionMode )
{
if ( compositionMode = = Win32CompositionMode . RedirectionSurface )
{
return ;
}
return null ;
if ( compositionMode = = Win32CompositionMode . WinUIComposition
& & WinUiCompositorConnection . IsSupported ( )
& & WinUiCompositorConnection . TryCreateAndRegister ( ) )
{
return ;
}
if ( compositionMode = = Win32CompositionMode . LowLatencyDxgiSwapChain
& & DxgiConnection . TryCreateAndRegister ( ) )
{
return ;
}
}
throw new InvalidOperationException ( $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CompositionMode)} has a value of \" { string . Join ( ", " , opts . CompositionMode ) } \ ", but no options were applied." ) ;
}
}