From 33ae4793556d84798cf82c84958772aef55f468f Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Wed, 12 Jul 2023 17:34:15 -0500 Subject: [PATCH] Update to Avalonia@54d18061 (Jun 30 2023) --- .../AvaloniaNativePlatformExtensions.cs | 8 +- .../Avalonia.Skia/WriteableBitmapImpl.cs | 20 ++-- .../Avalonia.Win32/FramebufferManager.cs | 8 +- .../Avalonia.Win32/Win32Platform.cs | 10 +- .../Avalonia.Win32/WindowFramebuffer.cs | 102 ------------------ .../Avalonia.X11/X11CursorFactory.cs | 10 +- .../Avalonia.X11/X11Framebuffer.cs | 5 +- .../Avalonia.X11/X11Platform.cs | 5 +- src/Modern.WindowKit/IRuntimePlatform.cs | 16 +-- .../StandardRuntimePlatform.cs | 12 +-- src/Modern.WindowKit/UnmanagedBlob.cs | 2 +- 11 files changed, 38 insertions(+), 160 deletions(-) delete mode 100644 src/Modern.WindowKit/Avalonia.Win32/WindowFramebuffer.cs diff --git a/src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatformExtensions.cs b/src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatformExtensions.cs index 25ee07c..b1d11b9 100644 --- a/src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatformExtensions.cs +++ b/src/Modern.WindowKit/Avalonia.Mac/AvaloniaNativePlatformExtensions.cs @@ -9,7 +9,9 @@ public static class AvaloniaNativePlatformExtensions { //public static AppBuilder UseAvaloniaNative(this AppBuilder builder) //{ - // builder.UseWindowingSubsystem(() => + // builder + // .UseStandardRuntimePlatformSubsystem() + // .UseWindowingSubsystem(() => // { // var platform = AvaloniaNativePlatform.Initialize( // AvaloniaLocator.Current.GetService() ?? @@ -28,7 +30,7 @@ public static class AvaloniaNativePlatformExtensions public enum AvaloniaNativeRenderingMode { - /// + /// /// Avalonia would try to use native OpenGL with GPU rendering. /// OpenGl = 1, @@ -43,7 +45,7 @@ public enum AvaloniaNativeRenderingMode Metal = 3 } - /// + /// /// OSX backend options. /// public class AvaloniaNativePlatformOptions diff --git a/src/Modern.WindowKit/Avalonia.Skia/WriteableBitmapImpl.cs b/src/Modern.WindowKit/Avalonia.Skia/WriteableBitmapImpl.cs index 1d1a34f..71d9a04 100644 --- a/src/Modern.WindowKit/Avalonia.Skia/WriteableBitmapImpl.cs +++ b/src/Modern.WindowKit/Avalonia.Skia/WriteableBitmapImpl.cs @@ -3,6 +3,7 @@ using System.Threading; using Modern.WindowKit.Media.Imaging; using Modern.WindowKit.Platform; +using Modern.WindowKit.Platform.Internal; using Modern.WindowKit.Skia.Helpers; using SkiaSharp; @@ -97,21 +98,12 @@ public WriteableBitmapImpl(PixelSize size, Vector dpi, PixelFormat format, Alpha SKColorType colorType = format.ToSkColorType(); SKAlphaType alphaType = alphaFormat.ToSkAlphaType(); - var runtimePlatform = AvaloniaGlobals.GetService(); + _bitmap = new SKBitmap(); - if (runtimePlatform != null) - { - _bitmap = new SKBitmap(); - - var nfo = new SKImageInfo(size.Width, size.Height, colorType, alphaType); - var blob = runtimePlatform.AllocBlob(nfo.BytesSize); + var nfo = new SKImageInfo(size.Width, size.Height, colorType, alphaType); + var blob = new UnmanagedBlob(nfo.BytesSize); - _bitmap.InstallPixels(nfo, blob.Address, nfo.RowBytes, s_releaseDelegate, blob); - } - else - { - _bitmap = new SKBitmap(size.Width, size.Height, colorType, alphaType); - } + _bitmap.InstallPixels(nfo, blob.Address, nfo.RowBytes, s_releaseDelegate, blob); _bitmap.Erase(SKColor.Empty); } @@ -176,7 +168,7 @@ public SKImage GetSnapshot() /// Blob. private static void ReleaseProc(IntPtr address, object ctx) { - ((IUnmanagedBlob)ctx).Dispose(); + ((UnmanagedBlob)ctx).Dispose(); } /// diff --git a/src/Modern.WindowKit/Avalonia.Win32/FramebufferManager.cs b/src/Modern.WindowKit/Avalonia.Win32/FramebufferManager.cs index 86e26d7..f8f8369 100644 --- a/src/Modern.WindowKit/Avalonia.Win32/FramebufferManager.cs +++ b/src/Modern.WindowKit/Avalonia.Win32/FramebufferManager.cs @@ -2,6 +2,7 @@ using System.Threading; using Modern.WindowKit.Controls.Platform.Surfaces; using Modern.WindowKit.Platform; +using Modern.WindowKit.Platform.Internal; using Modern.WindowKit.Win32.Interop; namespace Modern.WindowKit.Win32 @@ -107,8 +108,7 @@ private Vector GetCurrentDpi() private static FramebufferData AllocateFramebufferData(int width, int height) { - var service = AvaloniaGlobals.GetRequiredService(); - var bitmapBlob = service.AllocBlob(width * height * _bytesPerPixel); + var bitmapBlob = new UnmanagedBlob(width * height * _bytesPerPixel); return new FramebufferData(bitmapBlob, width, height); } @@ -157,7 +157,7 @@ private static bool DrawToWindow(IntPtr hWnd, FramebufferData framebufferData, i private readonly struct FramebufferData { - public IUnmanagedBlob Data { get; } + public UnmanagedBlob Data { get; } public PixelSize Size { get; } @@ -165,7 +165,7 @@ private readonly struct FramebufferData public UnmanagedMethods.BITMAPINFOHEADER Header { get; } - public FramebufferData(IUnmanagedBlob data, int width, int height) + public FramebufferData(UnmanagedBlob data, int width, int height) { Data = data; Size = new PixelSize(width, height); diff --git a/src/Modern.WindowKit/Avalonia.Win32/Win32Platform.cs b/src/Modern.WindowKit/Avalonia.Win32/Win32Platform.cs index dbbcec1..8585a69 100644 --- a/src/Modern.WindowKit/Avalonia.Win32/Win32Platform.cs +++ b/src/Modern.WindowKit/Avalonia.Win32/Win32Platform.cs @@ -24,10 +24,12 @@ namespace Modern.WindowKit //{ // public static AppBuilder UseWin32(this AppBuilder builder) // { - // return builder.UseWindowingSubsystem( - // () => Win32.Win32Platform.Initialize( - // AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions()), - // "Win32"); + // return builder + // .UseStandardRuntimePlatformSubsystem() + // .UseWindowingSubsystem( + // () => Win32.Win32Platform.Initialize( + // AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions()), + // "Win32"); // } //} } diff --git a/src/Modern.WindowKit/Avalonia.Win32/WindowFramebuffer.cs b/src/Modern.WindowKit/Avalonia.Win32/WindowFramebuffer.cs deleted file mode 100644 index 96d6694..0000000 --- a/src/Modern.WindowKit/Avalonia.Win32/WindowFramebuffer.cs +++ /dev/null @@ -1,102 +0,0 @@ -#nullable disable - -using System; -using Modern.WindowKit.Platform; -using Modern.WindowKit.Win32.Interop; -using PixelFormat = Modern.WindowKit.Platform.PixelFormat; - -namespace Modern.WindowKit.Win32 -{ - internal class WindowFramebuffer : ILockedFramebuffer - { - private readonly IntPtr _handle; - private IUnmanagedBlob _bitmapBlob; - private UnmanagedMethods.BITMAPINFOHEADER _bmpInfo; - - public WindowFramebuffer(IntPtr handle, PixelSize size) - { - - if (size.Width <= 0) - throw new ArgumentException("Width is less than zero"); - if (size.Height <= 0) - throw new ArgumentException("Height is less than zero"); - _handle = handle; - _bmpInfo.Init(); - _bmpInfo.biPlanes = 1; - _bmpInfo.biBitCount = 32; - _bmpInfo.Init(); - _bmpInfo.biWidth = size.Width; - _bmpInfo.biHeight = -size.Height; - _bitmapBlob = AvaloniaGlobals.GetRequiredService().AllocBlob(size.Width * size.Height * 4); - } - - ~WindowFramebuffer() - { - Deallocate(); - } - - public IntPtr Address => _bitmapBlob.Address; - public int RowBytes => Size.Width * 4; - public PixelFormat Format => PixelFormat.Bgra8888; - - public Vector Dpi - { - get - { - if (UnmanagedMethods.ShCoreAvailable) - { - uint dpix, dpiy; - - var monitor = UnmanagedMethods.MonitorFromWindow(_handle, - UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST); - - if (UnmanagedMethods.GetDpiForMonitor( - monitor, - UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, - out dpix, - out dpiy) == 0) - { - return new Vector(dpix, dpiy); - } - } - return new Vector(96, 96); - } - } - - public PixelSize Size => new PixelSize(_bmpInfo.biWidth, -_bmpInfo.biHeight); - - public void DrawToDevice(IntPtr hDC, int destX = 0, int destY = 0, int srcX = 0, int srcY = 0, int width = -1, - int height = -1) - { - if (width == -1) - width = Size.Width; - if (height == -1) - height = Size.Height; - UnmanagedMethods.SetDIBitsToDevice(hDC, destX, destY, (uint) width, (uint) height, srcX, srcY, - 0, (uint)Size.Height, _bitmapBlob.Address, ref _bmpInfo, 0); - } - - public bool DrawToWindow(IntPtr hWnd, int destX = 0, int destY = 0, int srcX = 0, int srcY = 0, int width = -1, - int height = -1) - { - if (_bitmapBlob.IsDisposed) - throw new ObjectDisposedException("Framebuffer"); - if (hWnd == IntPtr.Zero) - return false; - IntPtr hDC = UnmanagedMethods.GetDC(hWnd); - if (hDC == IntPtr.Zero) - return false; - DrawToDevice(hDC, destX, destY, srcX, srcY, width, height); - UnmanagedMethods.ReleaseDC(hWnd, hDC); - return true; - } - - public void Dispose() - { - //It's not an *actual* dispose. This call means "We are done drawing" - DrawToWindow(_handle); - } - - public void Deallocate() => _bitmapBlob.Dispose(); - } -} diff --git a/src/Modern.WindowKit/Avalonia.X11/X11CursorFactory.cs b/src/Modern.WindowKit/Avalonia.X11/X11CursorFactory.cs index dbe3cff..c01025d 100644 --- a/src/Modern.WindowKit/Avalonia.X11/X11CursorFactory.cs +++ b/src/Modern.WindowKit/Avalonia.X11/X11CursorFactory.cs @@ -5,6 +5,7 @@ using Modern.WindowKit.Controls.Platform.Surfaces; using Modern.WindowKit.Input; using Modern.WindowKit.Platform; +using Modern.WindowKit.Platform.Internal; //using Modern.WindowKit.SourceGenerator; using Modern.WindowKit.Utilities; @@ -92,18 +93,17 @@ private static IntPtr GetNullCursor(IntPtr display) //private unsafe class XImageCursor : CursorImpl, IFramebufferPlatformSurface, IPlatformHandle //{ // private readonly PixelSize _pixelSize; - // private readonly IUnmanagedBlob _blob; + // private readonly UnmanagedBlob _blob; // public XImageCursor(IntPtr display, IBitmapImpl bitmap, PixelPoint hotSpot) // { // var size = Marshal.SizeOf() + // (bitmap.PixelSize.Width * bitmap.PixelSize.Height * 4); - // var runtimePlatform = AvaloniaGlobals.GetRequiredService(); // var platformRenderInterface = AvaloniaLocator.Current.GetRequiredService(); // _pixelSize = bitmap.PixelSize; - // _blob = runtimePlatform.AllocBlob(size); - + // _blob = new UnmanagedBlob(size); + // var image = (XcursorImage*)_blob.Address; // image->version = 1; // image->size = Marshal.SizeOf(); @@ -117,7 +117,7 @@ private static IntPtr GetNullCursor(IntPtr display) // using (var renderTarget = cpuContext.CreateRenderTarget(new[] { this })) // using (var ctx = renderTarget.CreateDrawingContext()) // { - // var r = new Rect(_pixelSize.ToSize(1)); + // var r = new Rect(_pixelSize.ToSize(1)); // ctx.DrawBitmap(bitmap, 1, r, r); // } diff --git a/src/Modern.WindowKit/Avalonia.X11/X11Framebuffer.cs b/src/Modern.WindowKit/Avalonia.X11/X11Framebuffer.cs index 05a9d62..72f0367 100644 --- a/src/Modern.WindowKit/Avalonia.X11/X11Framebuffer.cs +++ b/src/Modern.WindowKit/Avalonia.X11/X11Framebuffer.cs @@ -1,6 +1,7 @@ using System; using System.IO; using Modern.WindowKit.Platform; +using Modern.WindowKit.Platform.Internal; using SkiaSharp; using static Modern.WindowKit.X11.XLib; namespace Modern.WindowKit.X11 @@ -10,7 +11,7 @@ internal class X11Framebuffer : ILockedFramebuffer private readonly IntPtr _display; private readonly IntPtr _xid; private readonly int _depth; - private IUnmanagedBlob _blob; + private UnmanagedBlob _blob; public X11Framebuffer(IntPtr display, IntPtr xid, int depth, int width, int height, double factor) { @@ -25,7 +26,7 @@ public X11Framebuffer(IntPtr display, IntPtr xid, int depth, int width, int heig RowBytes = width * 4; Dpi = new Vector(96, 96) * factor; Format = PixelFormat.Bgra8888; - _blob = AvaloniaGlobals.GetRequiredService().AllocBlob(RowBytes * height); + _blob = new UnmanagedBlob(RowBytes * height); Address = _blob.Address; } diff --git a/src/Modern.WindowKit/Avalonia.X11/X11Platform.cs b/src/Modern.WindowKit/Avalonia.X11/X11Platform.cs index ab92a98..31f97f8 100644 --- a/src/Modern.WindowKit/Avalonia.X11/X11Platform.cs +++ b/src/Modern.WindowKit/Avalonia.X11/X11Platform.cs @@ -344,7 +344,9 @@ public static class AvaloniaX11PlatformExtensions { //public static AppBuilder UseX11(this AppBuilder builder) //{ - // builder.UseWindowingSubsystem(() => + // builder + // .UseStandardRuntimePlatformSubsystem() + // .UseWindowingSubsystem(() => // new AvaloniaX11Platform().Initialize(AvaloniaLocator.Current.GetService() ?? // new X11PlatformOptions())); // return builder; @@ -353,5 +355,4 @@ public static class AvaloniaX11PlatformExtensions public static void InitializeX11Platform(X11PlatformOptions options = null) => new AvaloniaX11Platform().Initialize(options ?? new X11PlatformOptions()); } - } diff --git a/src/Modern.WindowKit/IRuntimePlatform.cs b/src/Modern.WindowKit/IRuntimePlatform.cs index a029f5b..3ee1592 100644 --- a/src/Modern.WindowKit/IRuntimePlatform.cs +++ b/src/Modern.WindowKit/IRuntimePlatform.cs @@ -3,24 +3,13 @@ namespace Modern.WindowKit.Platform { - [Unstable] + [PrivateApi] public interface IRuntimePlatform { - IDisposable StartSystemTimer(TimeSpan interval, Action tick); RuntimePlatformInfo GetRuntimeInfo(); - IUnmanagedBlob AllocBlob(int size); } - [Unstable] - public interface IUnmanagedBlob : IDisposable - { - IntPtr Address { get; } - int Size { get; } - bool IsDisposed { get; } - - } - - [Unstable] + [PrivateApi] public record struct RuntimePlatformInfo { public FormFactorType FormFactor => IsDesktop ? FormFactorType.Desktop : @@ -29,7 +18,6 @@ public record struct RuntimePlatformInfo public bool IsMobile { get; set; } } - [Unstable] public enum FormFactorType { Unknown, diff --git a/src/Modern.WindowKit/StandardRuntimePlatform.cs b/src/Modern.WindowKit/StandardRuntimePlatform.cs index d468a32..6ee324a 100644 --- a/src/Modern.WindowKit/StandardRuntimePlatform.cs +++ b/src/Modern.WindowKit/StandardRuntimePlatform.cs @@ -1,26 +1,20 @@ using System; using System.Threading; using Modern.WindowKit.Compatibility; +using Modern.WindowKit.Metadata; using Modern.WindowKit.Platform.Internal; namespace Modern.WindowKit.Platform { + [PrivateApi] internal class StandardRuntimePlatform : IRuntimePlatform - { - public IDisposable StartSystemTimer(TimeSpan interval, Action tick) { - return new Timer(_ => tick(), null, interval, interval); - } - - public IUnmanagedBlob AllocBlob(int size) => new UnmanagedBlob(size); - private static readonly RuntimePlatformInfo s_info = new() { IsDesktop = OperatingSystemEx.IsWindows() || OperatingSystemEx.IsMacOS() || OperatingSystemEx.IsLinux(), IsMobile = OperatingSystemEx.IsAndroid() || OperatingSystemEx.IsIOS() }; - - + public virtual RuntimePlatformInfo GetRuntimeInfo() => s_info; } } diff --git a/src/Modern.WindowKit/UnmanagedBlob.cs b/src/Modern.WindowKit/UnmanagedBlob.cs index 9ac946a..854a158 100644 --- a/src/Modern.WindowKit/UnmanagedBlob.cs +++ b/src/Modern.WindowKit/UnmanagedBlob.cs @@ -6,7 +6,7 @@ namespace Modern.WindowKit.Platform.Internal; -internal class UnmanagedBlob : IUnmanagedBlob +internal class UnmanagedBlob { private IntPtr _address; private readonly object _lock = new object();