Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for Feedback: Improving Startup Latency in X11 #16033

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/Avalonia.X11/X11CursorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Platform.Internal;
using Avalonia.SourceGenerator;
using Avalonia.Utilities;

#nullable enable

Expand Down Expand Up @@ -50,15 +47,14 @@ internal partial class X11CursorFactory : ICursorFactory
{StandardCursorType.TopRightCorner, CursorFontShape.XC_top_right_corner},
};

[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));

// 78 = number of items in CursorFontShape enum
// Unlikely to change, but, do we have a Src Gen for this?
_cursors = new Dictionary<CursorFontShape, IntPtr>(78);
}

public ICursorImpl GetCursor(StandardCursorType cursorType)
Expand All @@ -71,8 +67,8 @@ public ICursorImpl GetCursor(StandardCursorType cursorType)
else
{
handle = s_mapping.TryGetValue(cursorType, out var shape)
? _cursors[shape]
: _cursors[CursorFontShape.XC_left_ptr];
? GetCursorHandleLazy(shape)
: GetCursorHandleLazy(CursorFontShape.XC_left_ptr);
}
return new CursorImpl(handle);
}
Expand Down Expand Up @@ -144,6 +140,14 @@ public ILockedFramebuffer Lock()

public IFramebufferRenderTarget CreateFramebufferRenderTarget() => new FuncFramebufferRenderTarget(Lock);
}

private nint GetCursorHandleLazy(CursorFontShape shape)
{
if (!_cursors.TryGetValue(shape, out var handle))
_cursors[shape] = handle = XLib.XCreateFontCursor(_display, shape);

return handle;
}
}

internal class CursorImpl : ICursorImpl
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Controls.Platform;
using Avalonia.FreeDesktop;
using Avalonia.FreeDesktop.DBusIme;
Expand Down
Loading