From 2c635b830aa500a4db5a71b7fdc5be20b348e6c9 Mon Sep 17 00:00:00 2001 From: Sewer56 Date: Sat, 15 Jun 2024 20:52:42 +0100 Subject: [PATCH] Changed: An alternative attempt with lazy cursor generation. --- src/Avalonia.X11/X11CursorFactory.cs | 30 +++++++++++++--------------- src/Avalonia.X11/X11Platform.cs | 14 +++---------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/Avalonia.X11/X11CursorFactory.cs b/src/Avalonia.X11/X11CursorFactory.cs index 124f5fb2736..bd0413b1db8 100644 --- a/src/Avalonia.X11/X11CursorFactory.cs +++ b/src/Avalonia.X11/X11CursorFactory.cs @@ -1,16 +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; using Avalonia.Platform.Internal; -using Avalonia.SourceGenerator; -using Avalonia.Utilities; #nullable enable @@ -53,18 +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); - - var cursorShapes = GetAllCursorShapes(); - _cursors = new Dictionary(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(78); } public ICursorImpl GetCursor(StandardCursorType cursorType) @@ -77,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); } @@ -150,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 diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 8df5a82d89b..61f0b853b93 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -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; @@ -59,18 +55,14 @@ 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 @@ -103,7 +95,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().ToConstant(graphics);