Browse Source

Make IRuntimePlatform private API and remove some members that are always have single implementation

pull/11930/head
Max Katz 3 years ago
parent
commit
cb33529af4
  1. 15
      src/Avalonia.Base/Platform/IRuntimePlatform.cs
  2. 2
      src/Avalonia.Base/Platform/Internal/UnmanagedBlob.cs
  3. 12
      src/Avalonia.Base/Platform/StandardRuntimePlatform.cs
  4. 8
      src/Avalonia.Base/Rendering/DefaultRenderTimer.cs
  5. 6
      src/Avalonia.X11/X11CursorFactory.cs
  6. 5
      src/Avalonia.X11/X11Framebuffer.cs
  7. 32
      src/Browser/Avalonia.Browser/WindowingPlatform.cs
  8. 20
      src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs
  9. 8
      src/Windows/Avalonia.Win32/FramebufferManager.cs

15
src/Avalonia.Base/Platform/IRuntimePlatform.cs

@ -3,24 +3,13 @@ using Avalonia.Metadata;
namespace Avalonia.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 :

2
src/Avalonia.Base/Platform/Internal/UnmanagedBlob.cs

@ -6,7 +6,7 @@ using System.Threading;
namespace Avalonia.Platform.Internal;
internal class UnmanagedBlob : IUnmanagedBlob
internal class UnmanagedBlob
{
private IntPtr _address;
private readonly object _lock = new object();

12
src/Avalonia.Base/Platform/StandardRuntimePlatform.cs

@ -1,26 +1,20 @@
using System;
using System.Threading;
using Avalonia.Compatibility;
using Avalonia.Metadata;
using Avalonia.Platform.Internal;
namespace Avalonia.Platform
{
[PrivateApi]
public 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;
}
}

8
src/Avalonia.Base/Rendering/DefaultRenderTimer.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using Avalonia.Metadata;
using Avalonia.Platform;
@ -14,7 +15,6 @@ namespace Avalonia.Rendering
[PrivateApi]
public class DefaultRenderTimer : IRenderTimer
{
private IRuntimePlatform? _runtime;
private int _subscriberCount;
private Action<TimeSpan>? _tick;
private IDisposable? _subscription;
@ -80,11 +80,9 @@ namespace Avalonia.Rendering
/// </remarks>
protected virtual IDisposable StartCore(Action<TimeSpan> tick)
{
_runtime ??= AvaloniaLocator.Current.GetRequiredService<IRuntimePlatform>();
var interval = TimeSpan.FromSeconds(1.0 / FramesPerSecond);
return _runtime.StartSystemTimer(
TimeSpan.FromSeconds(1.0 / FramesPerSecond),
() => tick(TimeSpan.FromMilliseconds(Environment.TickCount)));
return new Timer(_ => tick(TimeSpan.FromMilliseconds(Environment.TickCount)), null, interval, interval);
}
/// <summary>

6
src/Avalonia.X11/X11CursorFactory.cs

@ -5,6 +5,7 @@ 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;
@ -92,17 +93,16 @@ namespace Avalonia.X11
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<XcursorImage>() +
(bitmap.PixelSize.Width * bitmap.PixelSize.Height * 4);
var runtimePlatform = AvaloniaLocator.Current.GetRequiredService<IRuntimePlatform>();
var platformRenderInterface = AvaloniaLocator.Current.GetRequiredService<IPlatformRenderInterface>();
_pixelSize = bitmap.PixelSize;
_blob = runtimePlatform.AllocBlob(size);
_blob = new UnmanagedBlob(size);
var image = (XcursorImage*)_blob.Address;
image->version = 1;

5
src/Avalonia.X11/X11Framebuffer.cs

@ -1,6 +1,7 @@
using System;
using System.IO;
using Avalonia.Platform;
using Avalonia.Platform.Internal;
using SkiaSharp;
using static Avalonia.X11.XLib;
namespace Avalonia.X11
@ -10,7 +11,7 @@ namespace Avalonia.X11
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 @@ namespace Avalonia.X11
RowBytes = width * 4;
Dpi = new Vector(96, 96) * factor;
Format = PixelFormat.Bgra8888;
_blob = AvaloniaLocator.Current.GetRequiredService<IRuntimePlatform>().AllocBlob(RowBytes * height);
_blob = new UnmanagedBlob(RowBytes * height);
Address = _blob.Address;
}

32
src/Browser/Avalonia.Browser/WindowingPlatform.cs

@ -57,12 +57,11 @@ namespace Avalonia.Browser
public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick)
{
return GetRuntimePlatform()
.StartSystemTimer(interval, () =>
{
Dispatcher.UIThread.RunJobs(priority);
tick();
});
return new Timer(_ =>
{
Dispatcher.UIThread.RunJobs(priority);
tick();
}, null, interval, interval);
}
public void Signal(DispatcherPriority priority)
@ -71,18 +70,16 @@ namespace Avalonia.Browser
return;
_signaled = true;
var interval = TimeSpan.FromMilliseconds(1);
IDisposable? disp = null;
disp = new Timer(_ =>
{
_signaled = false;
disp?.Dispose();
disp = GetRuntimePlatform()
.StartSystemTimer(TimeSpan.FromMilliseconds(1),
() =>
{
_signaled = false;
disp?.Dispose();
Signaled?.Invoke(null);
});
Signaled?.Invoke(null);
}, null, interval, interval);
}
public bool CurrentThreadIsLoopThread
@ -94,10 +91,5 @@ namespace Avalonia.Browser
}
public event Action<DispatcherPriority?>? Signaled;
private static IRuntimePlatform GetRuntimePlatform()
{
return AvaloniaLocator.Current.GetRequiredService<IRuntimePlatform>();
}
}
}

20
src/Skia/Avalonia.Skia/WriteableBitmapImpl.cs

@ -3,6 +3,7 @@ using System.IO;
using System.Threading;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Platform.Internal;
using Avalonia.Skia.Helpers;
using SkiaSharp;
@ -97,21 +98,12 @@ namespace Avalonia.Skia
SKColorType colorType = format.ToSkColorType();
SKAlphaType alphaType = alphaFormat.ToSkAlphaType();
var runtimePlatform = AvaloniaLocator.Current.GetService<IRuntimePlatform>();
_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 @@ namespace Avalonia.Skia
/// <param name="ctx">Blob.</param>
private static void ReleaseProc(IntPtr address, object ctx)
{
((IUnmanagedBlob)ctx).Dispose();
((UnmanagedBlob)ctx).Dispose();
}
/// <summary>

8
src/Windows/Avalonia.Win32/FramebufferManager.cs

@ -2,6 +2,7 @@
using System.Threading;
using Avalonia.Controls.Platform.Surfaces;
using Avalonia.Platform;
using Avalonia.Platform.Internal;
using Avalonia.Win32.Interop;
namespace Avalonia.Win32
@ -105,8 +106,7 @@ namespace Avalonia.Win32
private static FramebufferData AllocateFramebufferData(int width, int height)
{
var service = AvaloniaLocator.Current.GetRequiredService<IRuntimePlatform>();
var bitmapBlob = service.AllocBlob(width * height * _bytesPerPixel);
var bitmapBlob = new UnmanagedBlob(width * height * _bytesPerPixel);
return new FramebufferData(bitmapBlob, width, height);
}
@ -155,7 +155,7 @@ namespace Avalonia.Win32
private readonly struct FramebufferData
{
public IUnmanagedBlob Data { get; }
public UnmanagedBlob Data { get; }
public PixelSize Size { get; }
@ -163,7 +163,7 @@ namespace Avalonia.Win32
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);

Loading…
Cancel
Save