From 29d3c7670be216f704f001ee6e28efc0adbe2e83 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 25 Jun 2023 23:26:45 -0400 Subject: [PATCH] Restructure Win32PlatformOptions options --- samples/ControlCatalog.NetCore/Program.cs | 3 +- src/Windows/Avalonia.Win32/Win32GlManager.cs | 37 ++++-- src/Windows/Avalonia.Win32/Win32Platform.cs | 74 ------------ .../Avalonia.Win32/Win32PlatformOptions.cs | 114 ++++++++++++++++++ .../WinUiCompositedWindowSurface.cs | 2 +- .../Composition/WinUiCompositionShared.cs | 1 + .../Composition/WinUiCompositorConnection.cs | 31 ++--- 7 files changed, 160 insertions(+), 102 deletions(-) create mode 100644 src/Windows/Avalonia.Win32/Win32PlatformOptions.cs diff --git a/samples/ControlCatalog.NetCore/Program.cs b/samples/ControlCatalog.NetCore/Program.cs index 5249a4fb41..99d507c817 100644 --- a/samples/ControlCatalog.NetCore/Program.cs +++ b/samples/ControlCatalog.NetCore/Program.cs @@ -110,8 +110,7 @@ namespace ControlCatalog.NetCore { builder.With(new Win32PlatformOptions() { - UseLowLatencyDxgiSwapChain = true, - UseWindowsUIComposition = false + CompositionMode = Win32CompositionMode.LowLatencyDxgiSwapChain }); return builder.StartWithClassicDesktopLifetime(args); } diff --git a/src/Windows/Avalonia.Win32/Win32GlManager.cs b/src/Windows/Avalonia.Win32/Win32GlManager.cs index c26ce5fd45..49eb369149 100644 --- a/src/Windows/Avalonia.Win32/Win32GlManager.cs +++ b/src/Windows/Avalonia.Win32/Win32GlManager.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Win32.DirectX; @@ -23,15 +24,19 @@ namespace Avalonia.Win32 private static IPlatformGraphics? InitializeCore() { - var opts = AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions(); - if (opts.UseWgl) + + var winVersion = Win32Platform.WindowsVersion; + var renderingMode = opts.RenderingMode ?? + (winVersion > PlatformConstants.Windows7 ? Win32RenderingMode.AngleEgl : Win32RenderingMode.Software); + + if (renderingMode == Win32RenderingMode.Wgl) { var wgl = WglPlatformOpenGlInterface.TryCreate(); return wgl; } - if (opts.AllowEglInitialization ?? Win32Platform.WindowsVersion > PlatformConstants.Windows7) + if (renderingMode == Win32RenderingMode.AngleEgl) { var egl = AngleWin32PlatformGraphics.TryCreate(AvaloniaLocator.Current.GetService() ?? new()); @@ -40,14 +45,26 @@ namespace Avalonia.Win32 { AvaloniaLocator.CurrentMutable.Bind() .ToConstant(egl); - - if (opts.UseWindowsUIComposition) - { - WinUiCompositorConnection.TryCreateAndRegister(); - } - else if (opts.UseLowLatencyDxgiSwapChain) + + var compositionMode = opts.CompositionMode ?? + (WinUiCompositorConnection.IsSupported() ? Win32CompositionMode.WinUIComposition + //: DirectCompositionConnection.IsSupported() ? Win32CompositionMode.DirectComposition + : Win32CompositionMode.RedirectionSurface); + + switch (compositionMode) { - DxgiConnection.TryCreateAndRegister(); + case Win32CompositionMode.WinUIComposition: + if (!WinUiCompositorConnection.TryCreateAndRegister()) + { + //goto case Win32CompositionMode.DirectComposition; + } + break; + //case Win32CompositionMode.DirectComposition: + //DirectCompositionConnection.TryCreateAndRegister(); + //break; + case Win32CompositionMode.LowLatencyDxgiSwapChain: + DxgiConnection.TryCreateAndRegister(); + break; } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index e9019803be..aaf6fed624 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -10,7 +9,6 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Platform; using Avalonia.Input; using Avalonia.Input.Platform; -using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Rendering.Composition; @@ -31,78 +29,6 @@ namespace Avalonia "Win32"); } } - - /// - /// Platform-specific options which apply to Windows. - /// - public class Win32PlatformOptions - { - /// - /// Enables ANGLE for Windows. For every Windows version that is above Windows 7, the default is true otherwise it's false. - /// - /// - /// GPU rendering will not be enabled if this is set to false. - /// - public bool? AllowEglInitialization { get; set; } - - /// - /// Embeds popups to the window when set to true. The default value is false. - /// - public bool OverlayPopups { get; set; } - - /// - /// Avalonia would try to use native Widows OpenGL when set to true. The default value is false. - /// - public bool UseWgl { get; set; } - - public IList WglProfiles { get; set; } = new List - { - new GlVersion(GlProfileType.OpenGL, 4, 0), - new GlVersion(GlProfileType.OpenGL, 3, 2), - }; - - /// - /// Render Avalonia to a Texture inside the Windows.UI.Composition tree. - /// This setting is true by default. - /// - /// - /// Supported on Windows 10 build 16299 and above. Ignored on other versions. - /// This is recommended if you need to use AcrylicBlur or acrylic in your applications. - /// - public bool UseWindowsUIComposition { get; set; } = true; - - /// - /// When enabled, create rounded corner blur brushes - /// If set to null the brushes will be created using default settings (sharp corners) - /// This can be useful when you need a rounded-corner blurred Windows 10 app, or borderless Windows 11 app - /// - public float? CompositionBackdropCornerRadius { get; set; } - - /// - /// When is active, renders Avalonia through a low-latency Dxgi Swapchain. - /// Requires Feature Level 11_3 to be active, Windows 8.1+ Any Subversion. - /// This is only recommended if low input latency is desirable, and there is no need for the transparency - /// and stylings / blurrings offered by
- /// This is mutually exclusive with - /// which if active will override this setting. - /// This setting is false by default. - ///
- public bool UseLowLatencyDxgiSwapChain { get; set; } - - /// - /// Render directly on the UI thread instead of using a dedicated render thread. - /// Only applicable if both and - /// are false. - /// This setting is only recommended for interop with systems that must render on the UI thread, such as WPF. - /// This setting is false by default. - /// - public bool ShouldRenderOnUIThread { get; set; } - - /// - /// Provides a way to use a custom-implemented graphics context such as a custom ISkiaGpu - /// - public IPlatformGraphics? CustomPlatformGraphics { get; set; } - } } namespace Avalonia.Win32 diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs new file mode 100644 index 0000000000..aaebedb8c5 --- /dev/null +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -0,0 +1,114 @@ +using System.Collections.Generic; +using Avalonia.OpenGL; +using Avalonia.Platform; + +namespace Avalonia; + +public enum Win32RenderingMode +{ + /// + /// Avalonia is rendered into a framebuffer. + /// + Software = 1, + + /// + /// Enables ANGLE EGL for Windows with GPU rendering. + /// + AngleEgl = 2, + + /// + /// Avalonia would try to use native Widows OpenGL with GPU rendering. + /// + Wgl +} + +public enum Win32CompositionMode +{ + /// + /// Render Avalonia to a texture inside the Windows.UI.Composition tree. + /// + /// + /// Supported on Windows 10 build 17134 and above. Ignored on other versions. + /// This is recommended option, as it allows window acrylic effects and high refresh rate rendering. + /// + WinUIComposition = 1, + + // /// + // /// Render Avalonia to a texture inside the DirectComposition tree. + // /// + // /// + // /// Supported on Windows 8 and above. Ignored on other versions. + // /// + // DirectComposition = 2, + + /// + /// When is active, renders Avalonia through a low-latency Dxgi Swapchain. + /// Requires Feature Level 11_3 to be active, Windows 8.1+ Any Subversion. + /// This is only recommended if low input latency is desirable, and there is no need for the transparency + /// and styling / blurring offered by
. + ///
+ LowLatencyDxgiSwapChain = 3, + + /// + /// The window renders to a redirection surface. + /// + /// + /// This option is kept only for compatibility with older systems. Some Avalonia features might not work. + /// + RedirectionSurface, +} + +/// +/// Platform-specific options which apply to Windows. +/// +public class Win32PlatformOptions +{ + /// + /// Embeds popups to the window when set to true. The default value is false. + /// + public bool OverlayPopups { get; set; } + + /// + /// Avalonia window rendering mode. + /// On Windows 8 and newer default value is , + /// otherwise. + /// + public Win32RenderingMode? RenderingMode { get; set; } + + /// + /// Avalonia window composition mode. + /// On Windows 8 and newer default value is , + /// otherwise. + /// + public Win32CompositionMode? CompositionMode { get; set; } + + /// + /// When is set to , create rounded corner blur brushes + /// If set to null the brushes will be created using default settings (sharp corners) + /// This can be useful when you need a rounded-corner blurred Windows 10 app, or borderless Windows 11 app. + /// + public float? WinUICompositionBackdropCornerRadius { get; set; } + + /// + /// Render directly on the UI thread instead of using a dedicated render thread. + /// Only applicable if is set to . + /// This setting is only recommended for interop with systems that must render on the UI thread, such as WPF. + /// This setting is false by default. + /// + public bool ShouldRenderOnUIThread { get; set; } + + /// + /// Windows OpenGL profiles used when is set to . + /// This setting is 4.0 and 3.2 by default. + /// + public IList WglProfiles { get; set; } = new List + { + new(GlProfileType.OpenGL, 4, 0), new(GlProfileType.OpenGL, 3, 2) + }; + + /// + /// Provides a way to use a custom-implemented graphics context such as a custom ISkiaGpu. + /// When this property set and are completely ignored. + /// + public IPlatformGraphics? CustomPlatformGraphics { get; set; } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs index 95ef338f08..8da154dc83 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs @@ -23,7 +23,7 @@ namespace Avalonia.Win32.WinRT.Composition public IDirect3D11TextureRenderTarget CreateRenderTarget(IPlatformGraphicsContext context, IntPtr d3dDevice) { var cornerRadius = AvaloniaLocator.Current.GetService() - ?.CompositionBackdropCornerRadius; + ?.WinUICompositionBackdropCornerRadius; _window ??= new WinUiCompositedWindow(_info, _shared, cornerRadius); _window.SetBlur(_blurEffect); diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs index f17805fba3..b3a328d097 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs @@ -12,6 +12,7 @@ internal class WinUiCompositionShared : IDisposable public ICompositionBrush? MicaBrush { get; } public object SyncRoot { get; } = new(); + public static readonly Version MinWinCompositionVersion = new(10, 0, 17134); public static readonly Version MinAcrylicVersion = new(10, 0, 15063); public static readonly Version MinHostBackdropVersion = new(10, 0, 22000); diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs index 754af86c06..3e5384715c 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs @@ -112,35 +112,36 @@ internal class WinUiCompositorConnection : IRenderTimer } } - public static void TryCreateAndRegister() + public static bool IsSupported() { - const int majorRequired = 10; - const int buildRequired = 17134; - - var majorInstalled = Win32Platform.WindowsVersion.Major; - var buildInstalled = Win32Platform.WindowsVersion.Build; - - if (majorInstalled >= majorRequired && - buildInstalled >= buildRequired) + return Win32Platform.WindowsVersion >= WinUiCompositionShared.MinWinCompositionVersion; + } + + public static bool TryCreateAndRegister() + { + if (Win32Platform.WindowsVersion >= WinUiCompositionShared.MinWinCompositionVersion) { try { TryCreateAndRegisterCore(); - return; + return true; } catch (Exception e) { Logger.TryGet(LogEventLevel.Error, "WinUIComposition") ?.Log(null, "Unable to initialize WinUI compositor: {0}", e); - } } + else + { + var osVersionNotice = + $"Windows {WinUiCompositionShared.MinWinCompositionVersion} is required. Your machine has Windows {Win32Platform.WindowsVersion} installed."; - var osVersionNotice = - $"Windows {majorRequired} Build {buildRequired} is required. Your machine has Windows {majorInstalled} Build {buildInstalled} installed."; + Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, + $"Unable to initialize WinUI compositor: {osVersionNotice}"); + } - Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, - $"Unable to initialize WinUI compositor: {osVersionNotice}"); + return false; } public WinUiCompositedWindowSurface CreateSurface(EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo info) => new(_shared, info);