Skip to content

Commit

Permalink
Improve: X11 Startup time with asynchronous Gfx Startup
Browse files Browse the repository at this point in the history
Before: 80-100
After: 66-81
  • Loading branch information
Sewer56 committed Jun 14, 2024
1 parent 99cf2f8 commit 8038127
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/Avalonia.X11/X11CursorFactory.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<CursorFontShape, IntPtr>(cursorShapes.Length);
foreach (var shape in cursorShapes)
_cursors[shape] = XLib.XCreateFontCursor(_display, shape);
}

public ICursorImpl GetCursor(StandardCursorType cursorType)
Expand Down
16 changes: 11 additions & 5 deletions src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -83,7 +88,6 @@ public void Initialize(X11PlatformOptions options)
.Bind<PlatformHotkeyConfiguration>().ToConstant(new PlatformHotkeyConfiguration(KeyModifiers.Control))
.Bind<KeyGestureFormatInfo>().ToConstant(new KeyGestureFormatInfo(new Dictionary<Key, string>() { }, meta: "Super"))
.Bind<IKeyboardDevice>().ToFunc(() => KeyboardDevice)
.Bind<ICursorFactory>().ToConstant(new X11CursorFactory(Display))
.Bind<IClipboard>().ToConstant(new X11Clipboard(this))
.Bind<IPlatformSettings>().ToSingleton<DBusPlatformSettings>()
.Bind<IPlatformIconLoader>().ToConstant(new X11IconLoader())
Expand All @@ -97,8 +101,10 @@ public void Initialize(X11PlatformOptions options)
if (xi2.Init(this))
XI2 = xi2;
}

AvaloniaLocator.CurrentMutable.Bind<ICursorFactory>().ToConstant(new X11CursorFactory(Display));

var graphics = InitializeGraphics(options, Info);
var graphics = graphicsTask.Result;
if (graphics is not null)
{
AvaloniaLocator.CurrentMutable.Bind<IPlatformGraphics>().ToConstant(graphics);
Expand Down

0 comments on commit 8038127

Please sign in to comment.