Skip to content

Commit

Permalink
Changed: An alternative attempt with lazy cursor generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Jun 15, 2024
1 parent cf30995 commit d9a2ff6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
25 changes: 14 additions & 11 deletions src/Avalonia.X11/X11CursorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Platform.Internal;
using Avalonia.SourceGenerator;
using Avalonia.Utilities;

#nullable enable
Expand Down Expand Up @@ -53,18 +52,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);

var cursorShapes = GetAllCursorShapes();
_cursors = new Dictionary<CursorFontShape, IntPtr>(cursorShapes.Length);
foreach (var shape in cursorShapes)
_cursors[shape] = XLib.XCreateFontCursor(_display, shape);

// 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 @@ -77,8 +72,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 @@ -150,6 +145,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
11 changes: 2 additions & 9 deletions src/Avalonia.X11/X11Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
using System.Collections.Generic;
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;
using Avalonia.FreeDesktop.DBusIme;
Expand Down Expand Up @@ -59,18 +55,15 @@ 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 Down Expand Up @@ -103,7 +96,7 @@ public void Initialize(X11PlatformOptions options)
XI2 = xi2;
}

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

0 comments on commit d9a2ff6

Please sign in to comment.