From 80381276c31737a442635131c6f5831cbc484581 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Sat, 15 Jun 2024 00:04:50 +0100 Subject: [PATCH] Improve: X11 Startup time with asynchronous Gfx Startup Before: 80-100 After: 66-81 --- src/Avalonia.X11/X11CursorFactory.cs | 12 +++++++++--- src/Avalonia.X11/X11Platform.cs | 16 +++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.X11/X11CursorFactory.cs b/src/Avalonia.X11/X11CursorFactory.cs index 22254ada183..124f5fb2736 100644 --- a/src/Avalonia.X11/X11CursorFactory.cs +++ b/src/Avalonia.X11/X11CursorFactory.cs @@ -1,7 +1,10 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Threading.Tasks; using Avalonia.Controls.Platform.Surfaces; using Avalonia.Input; using Avalonia.Platform; @@ -52,13 +55,16 @@ internal partial class X11CursorFactory : ICursorFactory [GenerateEnumValueList] private static partial CursorFontShape[] GetAllCursorShapes(); - + public X11CursorFactory(IntPtr display) { _display = display; _nullCursor = GetNullCursor(display); - _cursors = GetAllCursorShapes() - .ToDictionary(id => id, id => XLib.XCreateFontCursor(_display, id)); + + var cursorShapes = GetAllCursorShapes(); + _cursors = new Dictionary(cursorShapes.Length); + foreach (var shape in cursorShapes) + _cursors[shape] = XLib.XCreateFontCursor(_display, shape); } public ICursorImpl GetCursor(StandardCursorType cursorType) diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 26046402a16..190b33b8c05 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Reflection; using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Platform; using Avalonia.FreeDesktop; @@ -57,15 +59,18 @@ public void Initialize(X11PlatformOptions options) Display = XOpenDisplay(IntPtr.Zero); if (Display == IntPtr.Zero) throw new Exception("XOpenDisplay failed"); + DeferredDisplay = XOpenDisplay(IntPtr.Zero); if (DeferredDisplay == IntPtr.Zero) throw new Exception("XOpenDisplay failed"); - + + Info = new X11Info(Display, DeferredDisplay, useXim); + var graphicsTask = Task.Run(() => InitializeGraphics(options, Info)); OrphanedWindow = XCreateSimpleWindow(Display, XDefaultRootWindow(Display), 0, 0, 1, 1, 0, IntPtr.Zero, IntPtr.Zero); + XError.Init(); - - Info = new X11Info(Display, DeferredDisplay, useXim); + Globals = new X11Globals(this); Resources = new XResources(this); //TODO: log @@ -83,7 +88,6 @@ public void Initialize(X11PlatformOptions options) .Bind().ToConstant(new PlatformHotkeyConfiguration(KeyModifiers.Control)) .Bind().ToConstant(new KeyGestureFormatInfo(new Dictionary() { }, meta: "Super")) .Bind().ToFunc(() => KeyboardDevice) - .Bind().ToConstant(new X11CursorFactory(Display)) .Bind().ToConstant(new X11Clipboard(this)) .Bind().ToSingleton() .Bind().ToConstant(new X11IconLoader()) @@ -97,8 +101,10 @@ public void Initialize(X11PlatformOptions options) if (xi2.Init(this)) XI2 = xi2; } + + AvaloniaLocator.CurrentMutable.Bind().ToConstant(new X11CursorFactory(Display)); - var graphics = InitializeGraphics(options, Info); + var graphics = graphicsTask.Result; if (graphics is not null) { AvaloniaLocator.CurrentMutable.Bind().ToConstant(graphics);