Skip to content

Commit

Permalink
Update to Avalonia@fddb07e4 (Jun 30 2023)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpobst committed Jul 12, 2023
1 parent 584506a commit 974a1d9
Show file tree
Hide file tree
Showing 23 changed files with 516 additions and 223 deletions.
2 changes: 2 additions & 0 deletions build/update-avalonia.csx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ CopyFile ("Avalonia.Controls/AcrylicPlatformCompensationLevels.cs", "AcrylicPlat
CopyFile ("Avalonia.Native/AvaloniaNativeApplicationPlatform.cs", "Avalonia.Mac/AvaloniaNativeApplicationPlatform.cs");
CopyFile ("Avalonia.Native/AvaloniaNativePlatform.cs", "Avalonia.Mac/AvaloniaNativePlatform.cs");
CopyFile ("Avalonia.Native/AvaloniaNativePlatformExtensions.cs", "Avalonia.Mac/AvaloniaNativePlatformExtensions.cs");
CopyFile ("Avalonia.Native/AvnDispatcher.cs", "Avalonia.Mac/AvnDispatcher.cs");
CopyFile ("Avalonia.Native/CallbackBase.cs", "Avalonia.Mac/CallbackBase.cs");
CopyFile ("Avalonia.Native/ClipboardImpl.cs", "Avalonia.Mac/ClipboardImpl.cs");
CopyFile ("Avalonia.Native/Cursor.cs", "Avalonia.Mac/Cursor.cs");
Expand Down Expand Up @@ -178,6 +179,7 @@ CopyFile ("Windows/Avalonia.Win32/Input/WindowsKeyboardDevice.cs", "Avalonia.Win
CopyFile ("Windows/Avalonia.Win32/Input/WindowsMouseDevice.cs", "Avalonia.Win32/WindowsMouseDevice.cs");
CopyFile ("Windows/Avalonia.Win32/WinScreen.cs", "Avalonia.Win32/WinScreen.cs");
CopyFile ("Windows/Avalonia.Win32/Win32DispatcherImpl.cs", "Avalonia.Win32/Win32DispatcherImpl.cs");
CopyFile ("Windows/Avalonia.Win32/Win32PlatformOptions.cs", "Avalonia.Win32/Win32PlatformOptions.cs");
CopyFile ("Windows/Avalonia.Win32/Win32PlatformSettings.cs", "Avalonia.Win32/Win32PlatformSettings.cs");
CopyFile ("Windows/Avalonia.Win32/Win32StorageProvider.cs", "Avalonia.Win32/Win32StorageProvider.cs");
CopyFile ("Windows/Avalonia.Win32/Win32TypeExtensions.cs", "Avalonia.Win32/Win32TypeExtensions.cs");
Expand Down
43 changes: 33 additions & 10 deletions src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ partial class AvaloniaNativePlatform : IWindowingPlatform
{
private readonly IAvaloniaNativeFactory _factory;
private AvaloniaNativePlatformOptions? _options;
//private AvaloniaNativeGlPlatformGraphics? _platformGl;
//private IPlatformGraphics? _platformGraphics;

[DllImport("libAvaloniaNative")]
static extern IntPtr CreateAvaloniaNative();
Expand Down Expand Up @@ -93,7 +93,7 @@ void DoInitialize(AvaloniaNativePlatformOptions options)
if (_factory.MacOptions != null)
_factory.MacOptions.SetDisableAppDelegate(macOpts.DisableAvaloniaAppDelegate ? 1 : 0);

_factory.Initialize(new GCHandleDeallocator(), applicationPlatform);
_factory.Initialize(new GCHandleDeallocator(), applicationPlatform, new AvnDispatcher());

if (_factory.MacOptions != null)
{
Expand Down Expand Up @@ -124,22 +124,45 @@ void DoInitialize(AvaloniaNativePlatformOptions options)

//AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(hotkeys);

//if (_options.UseGpu)
//foreach (var mode in _options.RenderingMode)
//{
// try
// if (mode == AvaloniaNativeRenderingMode.OpenGl)
// {
// _platformGl = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay());
// AvaloniaLocator.CurrentMutable
// .Bind<IPlatformGraphics>().ToConstant(_platformGl);

// try
// {
// _platformGraphics = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay());
// break;
// }
// catch (Exception)
// {
// // ignored
// }
//}
#pragma warning disable CS0618
// else if (mode == AvaloniaNativeRenderingMode.Metal)
#pragma warning restore CS0618
// {
// try
// {
// var metal = new MetalPlatformGraphics(_factory);
// metal.CreateContext().Dispose();
// _platformGraphics = metal;
// }
// catch
// {
// // Ignored
// }
// }
// else if (mode == AvaloniaNativeRenderingMode.Software)
// break;
//}

//if (_platformGraphics != null)
// AvaloniaLocator.CurrentMutable
// .Bind<IPlatformGraphics>().ToConstant(_platformGraphics);

//Compositor = new Compositor(_platformGl, true);

//Compositor = new Compositor(_platformGraphics, true);
}

//public ITrayIconImpl CreateTrayIcon()
Expand All @@ -149,7 +172,7 @@ void DoInitialize(AvaloniaNativePlatformOptions options)

//public IWindowImpl CreateWindow()
//{
// return new WindowImpl(_factory, _options, _platformGl);
// return new WindowImpl(_factory, _options);
//}

public IWindowImpl CreateEmbeddableWindow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Modern.WindowKit.Controls;
using Modern.WindowKit.Native;

Expand All @@ -25,16 +26,44 @@ public static class AvaloniaNativePlatformExtensions
//}
}

public enum AvaloniaNativeRenderingMode
{
/// </summary>
/// Avalonia would try to use native OpenGL with GPU rendering.
/// </summary>
OpenGl = 1,
/// <summary>
/// Avalonia is rendered into a framebuffer.
/// </summary>
Software = 2,
/// <summary>
/// Avalonia would try to use Metal with GPU rendering.
/// </summary>
[Obsolete("Experimental, unstable, not for production usage")]
Metal = 3
}

/// <summary>
/// OSX backend options.
/// </summary>
public class AvaloniaNativePlatformOptions
{
/// <summary>
/// Determines whether to use GPU for rendering in your project. The default value is true.
/// Gets or sets Avalonia rendering modes with fallbacks.
/// The first element in the array has the highest priority.
/// The default value is: <see cref="AvaloniaNativeRenderingMode.OpenGl"/>, <see cref="AvaloniaNativeRenderingMode.Software"/>.
/// </summary>
public bool UseGpu { get; set; } = true;

/// <remarks>
/// If application should work on as wide range of devices as possible,
/// at least add <see cref="AvaloniaNativeRenderingMode.Software"/> as a fallback value.
/// </remarks>
/// <exception cref="System.InvalidOperationException">Thrown if no values were matched.</exception>
public IReadOnlyList<AvaloniaNativeRenderingMode> RenderingMode { get; set; } = new[]
{
AvaloniaNativeRenderingMode.OpenGl,
AvaloniaNativeRenderingMode.Software
};

/// <summary>
/// Embeds popups to the window when set to true. The default value is false.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/Modern.WindowKit/Avalonia.Mac/AvnDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Modern.WindowKit.Mac.Interop;
using Modern.WindowKit.Threading;
using Modern.WindowKit.MicroCom;

namespace Modern.WindowKit.Native;

class AvnDispatcher : NativeCallbackBase, IAvnDispatcher
{
public void Post(IAvnActionCallback cb)
{
var callback = cb.CloneReference();
Dispatcher.UIThread.Post(() =>
{
using (callback)
callback.Run();
}, DispatcherPriority.Send);
}
}

49 changes: 13 additions & 36 deletions src/Modern.WindowKit/Avalonia.Mac/DeferredFramebuffer.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System.Runtime.InteropServices;
using Modern.WindowKit.Controls.Platform.Surfaces;
using Modern.WindowKit.Mac.Interop;
using Modern.WindowKit.Platform;

namespace Modern.WindowKit.Native
{
internal unsafe class DeferredFramebuffer : ILockedFramebuffer
{
private readonly Func<Action<IAvnWindowBase>, bool> _lockWindow;

public DeferredFramebuffer(Func<Action<IAvnWindowBase>, bool> lockWindow,
private readonly IAvnSoftwareRenderTarget _renderTarget;
private readonly Action<Action<IAvnWindowBase>> _lockWindow;

public DeferredFramebuffer(IAvnSoftwareRenderTarget renderTarget, Action<Action<IAvnWindowBase>> lockWindow,
int width, int height, Vector dpi)
{
_renderTarget = renderTarget;
_lockWindow = lockWindow;
Address = Marshal.AllocHGlobal(width * height * 4);
Size = new PixelSize(width, height);
Expand All @@ -27,54 +30,28 @@ public DeferredFramebuffer(Func<Action<IAvnWindowBase>, bool> lockWindow,
public Vector Dpi { get; set; }
public PixelFormat Format { get; set; }

class Disposer : NativeCallbackBase
{
private IntPtr _ptr;

public Disposer(IntPtr ptr)
{
_ptr = ptr;
}

protected override void Destroyed()
{
if(_ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(_ptr);
_ptr = IntPtr.Zero;
}
}
}

public void Dispose()
{
if (Address == IntPtr.Zero)
return;

if (!_lockWindow(win =>
_lockWindow(win =>
{
var fb = new AvnFramebuffer
{
Data = Address.ToPointer(),
Dpi = new AvnVector
{
X = Dpi.X,
Y = Dpi.Y
},
Dpi = new AvnVector { X = Dpi.X, Y = Dpi.Y },
Width = Size.Width,
Height = Size.Height,
PixelFormat = (AvnPixelFormat)Format.FormatEnum,
Stride = RowBytes
};
using (var d = new Disposer(Address))
{
win.ThreadSafeSetSwRenderedFrame(&fb, d);
}
}))
{
Marshal.FreeHGlobal(Address);
}
_renderTarget.SetFrame(&fb);
});

Marshal.FreeHGlobal(Address);

Address = IntPtr.Zero;
}
Expand Down
12 changes: 3 additions & 9 deletions src/Modern.WindowKit/Avalonia.Mac/PopupImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@ namespace Modern.WindowKit.Native
{
class PopupImpl : WindowBaseImpl, IPopupImpl
{
private readonly AvaloniaNativePlatformOptions _opts;
//private readonly AvaloniaNativeGlPlatformGraphics _glFeature;
private readonly IWindowBaseImpl _parent;

public PopupImpl(IAvaloniaNativeFactory factory,
AvaloniaNativePlatformOptions opts,
//AvaloniaNativeGlPlatformGraphics glFeature,
IWindowBaseImpl parent) : base(factory, opts)
IWindowBaseImpl parent) : base(factory)
{
_opts = opts;
//_glFeature = glFeature;
_parent = parent;
using (var e = new PopupEvents(this))
{
Init(factory.CreatePopup(e, null), factory.CreateScreens());
Init(factory.CreatePopup(e), factory.CreateScreens());
}
PopupPositioner = new ManagedPopupPositioner(new ManagedPopupPositionerPopupImplHelper(parent, MoveResize));
}
Expand Down Expand Up @@ -68,7 +62,7 @@ public override void Show(bool activate, bool isDialog)
base.Show(false, isDialog);
}

public override IPopupImpl CreatePopup() => new PopupImpl(_factory, _opts, this);
public override IPopupImpl CreatePopup() => new PopupImpl(_factory, this);

public void SetWindowManagerAddShadowHint(bool enabled)
{
Expand Down
10 changes: 4 additions & 6 deletions src/Modern.WindowKit/Avalonia.Mac/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,22 @@ namespace Modern.WindowKit.Native
internal partial class WindowImpl : WindowBaseImpl, IWindowImpl
{
private readonly AvaloniaNativePlatformOptions _opts;
//private readonly AvaloniaNativeGlPlatformGraphics _glFeature;
//private readonly AvaloniaNativeGlPlatformGraphics _graphics;
IAvnWindow _native;
private double _extendTitleBarHeight = -1;
//private DoubleClickHelper _doubleClickHelper;
//private readonly ITopLevelNativeMenuExporter _nativeMenuExporter;
//private readonly AvaloniaNativeTextInputMethod _inputMethod;
private bool _canResize = true;

internal WindowImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts
) : base(factory, opts)
internal WindowImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts) : base(factory)
{
_opts = opts;
//_glFeature = glFeature;
//_doubleClickHelper = new DoubleClickHelper();

using (var e = new WindowEvents(this))
{
Init(_native = factory.CreateWindow(e, null), factory.CreateScreens());
Init(_native = factory.CreateWindow(e), factory.CreateScreens());
}

//_nativeMenuExporter = new AvaloniaNativeMenuExporter(_native, factory);
Expand Down Expand Up @@ -215,7 +213,7 @@ public void ShowTaskbarIcon(bool value)
public void Move(PixelPoint point) => Position = point;

//public override IPopupImpl CreatePopup() =>
// _opts.OverlayPopups ? null : new PopupImpl(_factory, _opts, _glFeature, this);
// _opts.OverlayPopups ? null : new PopupImpl(_factory, this);

public Action GotInputWhenDisabled { get; set; }

Expand Down
Loading

0 comments on commit 974a1d9

Please sign in to comment.