From 66bebd0c1b5a5c4ece8c08c675c0ef926b41e3c9 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 5 Sep 2018 16:45:27 +0200 Subject: [PATCH 01/67] Initial --- .../Avalonia.Direct2D1/Direct2D1Platform.cs | 91 ++++++++++--------- .../ExternalRenderTarget.cs | 22 +---- .../FramebufferShimRenderTarget.cs | 27 +----- .../Avalonia.Direct2D1/HwndRenderTarget.cs | 10 +- .../Media/Direct2D1FontCollectionCache.cs | 13 +-- .../Media/DrawingContextImpl.cs | 10 +- .../Media/FormattedTextImpl.cs | 4 +- .../Avalonia.Direct2D1/Media/GeometryImpl.cs | 3 +- .../Media/Imaging/BitmapImpl.cs | 7 -- .../Media/Imaging/D2DBitmapImpl.cs | 28 +++--- .../Imaging/D2DRenderTargetBitmapImpl.cs | 39 +++----- .../Media/Imaging/WicBitmapImpl.cs | 33 +++---- .../Imaging/WicRenderTargetBitmapImpl.cs | 22 ++--- .../Media/Imaging/WriteableWicBitmapImpl.cs | 8 +- .../Media/StreamGeometryImpl.cs | 6 +- .../Avalonia.Direct2D1/RenderTarget.cs | 19 +--- .../SwapChainRenderTarget.cs | 55 +++-------- 17 files changed, 132 insertions(+), 265 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index 296edcb2d9..437bae6fd1 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -4,14 +4,12 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using Avalonia.Direct2D1.Media; -using Avalonia.Media; -using Avalonia.Platform; using Avalonia.Controls; using Avalonia.Controls.Platform.Surfaces; +using Avalonia.Direct2D1.Media; using Avalonia.Direct2D1.Media.Imaging; -using Avalonia.Rendering; +using Avalonia.Media; +using Avalonia.Platform; namespace Avalonia { @@ -31,15 +29,16 @@ namespace Avalonia.Direct2D1 { private static readonly Direct2D1Platform s_instance = new Direct2D1Platform(); - private static SharpDX.Direct2D1.Factory s_d2D1Factory; + public static SharpDX.Direct2D1.Factory1 Direct2D1Factory { get; private set; } - private static SharpDX.DirectWrite.Factory s_dwfactory; + public static SharpDX.Direct2D1.Device1 Direct2D1Device { get; private set; } - private static SharpDX.WIC.ImagingFactory s_imagingFactory; + public static SharpDX.DirectWrite.Factory1 DirectWriteFactory { get; private set; } - private static SharpDX.DXGI.Device s_dxgiDevice; + public static SharpDX.WIC.ImagingFactory ImagingFactory { get; private set; } + + public static SharpDX.DXGI.Device1 DxgiDevice { get; private set; } - private static SharpDX.Direct2D1.Device s_d2D1Device; private static readonly object s_initLock = new object(); private static bool s_initialized = false; @@ -49,13 +48,14 @@ namespace Avalonia.Direct2D1 lock (s_initLock) { if (s_initialized) + { return; + } #if DEBUG try { - s_d2D1Factory = - - new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, + Direct2D1Factory = new SharpDX.Direct2D1.Factory1( + SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.Error); } catch @@ -63,12 +63,19 @@ namespace Avalonia.Direct2D1 // } #endif - s_dwfactory = new SharpDX.DirectWrite.Factory(); - s_imagingFactory = new SharpDX.WIC.ImagingFactory(); - if (s_d2D1Factory == null) - s_d2D1Factory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, + if (Direct2D1Factory == null) + { + Direct2D1Factory = new SharpDX.Direct2D1.Factory1( + SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.None); + } + using (var factory = new SharpDX.DirectWrite.Factory()) + { + DirectWriteFactory = factory.QueryInterface(); + } + + ImagingFactory = new SharpDX.WIC.ImagingFactory(); var featureLevels = new[] { @@ -83,17 +90,18 @@ namespace Avalonia.Direct2D1 using (var d3dDevice = new SharpDX.Direct3D11.Device( SharpDX.Direct3D.DriverType.Hardware, - SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | + SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport, featureLevels)) { - s_dxgiDevice = d3dDevice.QueryInterface(); + DxgiDevice = d3dDevice.QueryInterface(); } - using (var factory1 = s_d2D1Factory.QueryInterface()) + using (var device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice)) { - s_d2D1Device = new SharpDX.Direct2D1.Device(factory1, s_dxgiDevice); + Direct2D1Device = device.QueryInterface(); } + s_initialized = true; } } @@ -101,19 +109,13 @@ namespace Avalonia.Direct2D1 public static void Initialize() { InitializeDirect2D(); - AvaloniaLocator.CurrentMutable - .Bind().ToConstant(s_instance) - .BindToSelf(s_d2D1Factory) - .BindToSelf(s_dwfactory) - .BindToSelf(s_imagingFactory) - .BindToSelf(s_dxgiDevice) - .BindToSelf(s_d2D1Device); + AvaloniaLocator.CurrentMutable.Bind().ToConstant(s_instance); SharpDX.Configuration.EnableReleaseOnFinalizer = true; } public IBitmapImpl CreateBitmap(int width, int height) { - return new WicBitmapImpl(s_imagingFactory, width, height); + return new WicBitmapImpl(width, height); } public IFormattedTextImpl CreateFormattedText( @@ -140,14 +142,22 @@ namespace Avalonia.Direct2D1 if (s is IPlatformHandle nativeWindow) { if (nativeWindow.HandleDescriptor != "HWND") + { throw new NotSupportedException("Don't know how to create a Direct2D1 renderer from " + nativeWindow.HandleDescriptor); + } + return new HwndRenderTarget(nativeWindow); } if (s is IExternalDirect2DRenderTargetSurface external) - return new ExternalRenderTarget(external, s_dwfactory, s_imagingFactory); + { + return new ExternalRenderTarget(external); + } + if (s is IFramebufferPlatformSurface fb) - return new FramebufferShimRenderTarget(fb, s_imagingFactory, s_d2D1Factory, s_dwfactory); + { + return new FramebufferShimRenderTarget(fb); + } } throw new NotSupportedException("Don't know how to create a Direct2D1 renderer from any of provided surfaces"); } @@ -158,19 +168,12 @@ namespace Avalonia.Direct2D1 double dpiX, double dpiY) { - return new WicRenderTargetBitmapImpl( - s_imagingFactory, - s_d2D1Factory, - s_dwfactory, - width, - height, - dpiX, - dpiY); + return new WicRenderTargetBitmapImpl(width, height, dpiX, dpiY); } public IWriteableBitmapImpl CreateWriteableBitmap(int width, int height, PixelFormat? format = null) { - return new WriteableWicBitmapImpl(s_imagingFactory, width, height, format); + return new WriteableWicBitmapImpl(width, height, format); } public IStreamGeometryImpl CreateStreamGeometry() @@ -180,17 +183,17 @@ namespace Avalonia.Direct2D1 public IBitmapImpl LoadBitmap(string fileName) { - return new WicBitmapImpl(s_imagingFactory, fileName); + return new WicBitmapImpl(fileName); } public IBitmapImpl LoadBitmap(Stream stream) { - return new WicBitmapImpl(s_imagingFactory, stream); + return new WicBitmapImpl(stream); } public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, int width, int height, int stride) { - return new WicBitmapImpl(s_imagingFactory, format, data, width, height, stride); + return new WicBitmapImpl(format, data, width, height, stride); } } -} \ No newline at end of file +} diff --git a/src/Windows/Avalonia.Direct2D1/ExternalRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/ExternalRenderTarget.cs index 176cedd377..aad50331d2 100644 --- a/src/Windows/Avalonia.Direct2D1/ExternalRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/ExternalRenderTarget.cs @@ -1,27 +1,19 @@ -using System; -using Avalonia.Direct2D1.Media; +using Avalonia.Direct2D1.Media; using Avalonia.Direct2D1.Media.Imaging; using Avalonia.Platform; using Avalonia.Rendering; using SharpDX; -using DirectWriteFactory = SharpDX.DirectWrite.Factory; namespace Avalonia.Direct2D1 { class ExternalRenderTarget : IRenderTarget, ILayerFactory { private readonly IExternalDirect2DRenderTargetSurface _externalRenderTargetProvider; - private readonly DirectWriteFactory _dwFactory; - private readonly SharpDX.WIC.ImagingFactory _wicFactory; public ExternalRenderTarget( - IExternalDirect2DRenderTargetSurface externalRenderTargetProvider, - DirectWriteFactory dwFactory, - SharpDX.WIC.ImagingFactory wicFactory) + IExternalDirect2DRenderTargetSurface externalRenderTargetProvider) { _externalRenderTargetProvider = externalRenderTargetProvider; - _dwFactory = dwFactory; - _wicFactory = wicFactory; } public void Dispose() @@ -33,7 +25,7 @@ namespace Avalonia.Direct2D1 { var target = _externalRenderTargetProvider.GetOrCreateRenderTarget(); _externalRenderTargetProvider.BeforeDrawing(); - return new DrawingContextImpl(visualBrushRenderer, null, target, _dwFactory, _wicFactory, null, () => + return new DrawingContextImpl(visualBrushRenderer, null, target, null, () => { try { @@ -48,12 +40,8 @@ namespace Avalonia.Direct2D1 public IRenderTargetBitmapImpl CreateLayer(Size size) { - var target = _externalRenderTargetProvider.GetOrCreateRenderTarget(); - return D2DRenderTargetBitmapImpl.CreateCompatible( - _wicFactory, - _dwFactory, - target, - size); + var renderTarget = _externalRenderTargetProvider.GetOrCreateRenderTarget(); + return D2DRenderTargetBitmapImpl.CreateCompatible(renderTarget, size); } } } diff --git a/src/Windows/Avalonia.Direct2D1/FramebufferShimRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/FramebufferShimRenderTarget.cs index 523cfeed46..5ae174083c 100644 --- a/src/Windows/Avalonia.Direct2D1/FramebufferShimRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/FramebufferShimRenderTarget.cs @@ -1,14 +1,9 @@ using System; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Text; using Avalonia.Controls.Platform.Surfaces; using Avalonia.Direct2D1.Media; -using Avalonia.Direct2D1.Media.Imaging; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Win32.Interop; -using SharpDX.Direct2D1; using SharpDX.WIC; using PixelFormat = Avalonia.Platform.PixelFormat; @@ -17,22 +12,14 @@ namespace Avalonia.Direct2D1 class FramebufferShimRenderTarget : IRenderTarget { private readonly IFramebufferPlatformSurface _surface; - private readonly ImagingFactory _imagingFactory; - private readonly Factory _d2DFactory; - private readonly SharpDX.DirectWrite.Factory _dwriteFactory; - public FramebufferShimRenderTarget(IFramebufferPlatformSurface surface, - ImagingFactory imagingFactory, Factory d2dFactory, SharpDX.DirectWrite.Factory dwriteFactory) + public FramebufferShimRenderTarget(IFramebufferPlatformSurface surface) { _surface = surface; - _imagingFactory = imagingFactory; - _d2DFactory = d2dFactory; - _dwriteFactory = dwriteFactory; } public void Dispose() - { - + { } public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) @@ -44,7 +31,7 @@ namespace Avalonia.Direct2D1 throw new ArgumentException("Unsupported pixel format: " + locked.Format); } - return new FramebufferShim(locked, _imagingFactory, _d2DFactory, _dwriteFactory) + return new FramebufferShim(locked) .CreateDrawingContext(visualBrushRenderer); } @@ -52,10 +39,8 @@ namespace Avalonia.Direct2D1 { private readonly ILockedFramebuffer _target; - public FramebufferShim(ILockedFramebuffer target, - ImagingFactory imagingFactory, Factory d2dFactory, SharpDX.DirectWrite.Factory dwriteFactory - ) : base(imagingFactory, d2dFactory, dwriteFactory, - target.Width, target.Height, target.Dpi.X, target.Dpi.Y, target.Format) + public FramebufferShim(ILockedFramebuffer target) : + base(target.Width, target.Height, target.Dpi.X, target.Dpi.Y, target.Format) { _target = target; } @@ -76,10 +61,8 @@ namespace Avalonia.Direct2D1 } Dispose(); _target.Dispose(); - }); } } - } } diff --git a/src/Windows/Avalonia.Direct2D1/HwndRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/HwndRenderTarget.cs index 49402d54b9..589f85f208 100644 --- a/src/Windows/Avalonia.Direct2D1/HwndRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/HwndRenderTarget.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Avalonia.Controls.Platform.Surfaces; -using Avalonia.Platform; +using Avalonia.Platform; using Avalonia.Win32.Interop; using SharpDX; using SharpDX.DXGI; @@ -22,7 +16,7 @@ namespace Avalonia.Direct2D1 protected override SwapChain1 CreateSwapChain(Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc) { - return new SwapChain1(dxgiFactory, DxgiDevice, _window.Handle, ref swapChainDesc); + return new SwapChain1(dxgiFactory, Direct2D1Platform.DxgiDevice, _window.Handle, ref swapChainDesc); } protected override Size2F GetWindowDpi() diff --git a/src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs b/src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs index 4199c73c54..d60aa15a5e 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Direct2D1FontCollectionCache.cs @@ -7,16 +7,13 @@ namespace Avalonia.Direct2D1.Media internal static class Direct2D1FontCollectionCache { private static readonly ConcurrentDictionary s_cachedCollections; - private static readonly SharpDX.DirectWrite.Factory s_factory; private static readonly SharpDX.DirectWrite.FontCollection s_installedFontCollection; static Direct2D1FontCollectionCache() { s_cachedCollections = new ConcurrentDictionary(); - s_factory = AvaloniaLocator.Current.GetService(); - - s_installedFontCollection = s_factory.GetSystemFontCollection(false); + s_installedFontCollection = Direct2D1Platform.DirectWriteFactory.GetSystemFontCollection(false); } public static SharpDX.DirectWrite.TextFormat GetTextFormat(Typeface typeface) @@ -39,7 +36,7 @@ namespace Avalonia.Direct2D1.Media } return new SharpDX.DirectWrite.TextFormat( - s_factory, + Direct2D1Platform.DirectWriteFactory, fontFamilyName, fontCollection, (SharpDX.DirectWrite.FontWeight)typeface.Weight, @@ -57,9 +54,9 @@ namespace Avalonia.Direct2D1.Media { var assets = FontFamilyLoader.LoadFontAssets(key); - var fontLoader = new DWriteResourceFontLoader(s_factory, assets); + var fontLoader = new DWriteResourceFontLoader(Direct2D1Platform.DirectWriteFactory, assets); - return new SharpDX.DirectWrite.FontCollection(s_factory, fontLoader, fontLoader.Key); + return new SharpDX.DirectWrite.FontCollection(Direct2D1Platform.DirectWriteFactory, fontLoader, fontLoader.Key); } } -} \ No newline at end of file +} diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index ae5dd3ae13..3aec6c6eb1 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -24,8 +24,6 @@ namespace Avalonia.Direct2D1.Media private readonly SharpDX.Direct2D1.RenderTarget _renderTarget; private readonly SharpDX.DXGI.SwapChain1 _swapChain; private readonly Action _finishedCallback; - private readonly SharpDX.WIC.ImagingFactory _imagingFactory; - private SharpDX.DirectWrite.Factory _directWriteFactory; /// /// Initializes a new instance of the class. @@ -36,16 +34,12 @@ namespace Avalonia.Direct2D1.Media /// An object to use to create layers. May be null, in which case a /// will created when a new layer is requested. /// - /// The DirectWrite factory. - /// The WIC imaging factory. /// An optional swap chain associated with this drawing context. /// An optional delegate to be called when context is disposed. public DrawingContextImpl( IVisualBrushRenderer visualBrushRenderer, ILayerFactory layerFactory, SharpDX.Direct2D1.RenderTarget renderTarget, - SharpDX.DirectWrite.Factory directWriteFactory, - SharpDX.WIC.ImagingFactory imagingFactory, SharpDX.DXGI.SwapChain1 swapChain = null, Action finishedCallback = null) { @@ -54,8 +48,6 @@ namespace Avalonia.Direct2D1.Media _renderTarget = renderTarget; _swapChain = swapChain; _finishedCallback = finishedCallback; - _directWriteFactory = directWriteFactory; - _imagingFactory = imagingFactory; _renderTarget.BeginDraw(); } @@ -443,7 +435,7 @@ namespace Avalonia.Direct2D1.Media return new ImageBrushImpl( visualBrush, _renderTarget, - new D2DBitmapImpl(_imagingFactory, intermediate.Bitmap), + new D2DBitmapImpl(intermediate.Bitmap), destinationSize); } } diff --git a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs index c3fe60e790..09b249e19f 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs @@ -21,15 +21,13 @@ namespace Avalonia.Direct2D1.Media { Text = text; - var factory = AvaloniaLocator.Current.GetService(); - var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface); textFormat.WordWrapping = wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap; TextLayout = new DWrite.TextLayout( - factory, + Direct2D1Platform.DirectWriteFactory, Text ?? string.Empty, textFormat, (float)constraint.Width, diff --git a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs index 120ab71ead..7c8ddaca3f 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/GeometryImpl.cs @@ -53,10 +53,9 @@ namespace Avalonia.Direct2D1.Media public ITransformedGeometryImpl WithTransform(Matrix transform) { - var factory = AvaloniaLocator.Current.GetService(); return new TransformedGeometryImpl( new TransformedGeometry( - factory, + Direct2D1Platform.Direct2D1Factory, GetSourceGeometry(), transform.ToDirect2D()), this); diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/BitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/BitmapImpl.cs index d58f023391..30af01283a 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/BitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/BitmapImpl.cs @@ -1,19 +1,12 @@ using System; using System.IO; using Avalonia.Platform; -using SharpDX.WIC; using D2DBitmap = SharpDX.Direct2D1.Bitmap; namespace Avalonia.Direct2D1.Media { public abstract class BitmapImpl : IBitmapImpl, IDisposable { - public BitmapImpl(ImagingFactory imagingFactory) - { - WicImagingFactory = imagingFactory; - } - - public ImagingFactory WicImagingFactory { get; } public abstract int PixelWidth { get; } public abstract int PixelHeight { get; } diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs index 6713cb13be..139100490c 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs @@ -1,7 +1,6 @@ using System; using System.IO; using SharpDX.Direct2D1; -using WICFactory = SharpDX.WIC.ImagingFactory; using ImagingFactory2 = SharpDX.WIC.ImagingFactory2; using ImageParameters = SharpDX.WIC.ImageParameters; using PngBitmapEncoder = SharpDX.WIC.PngBitmapEncoder; @@ -13,52 +12,51 @@ namespace Avalonia.Direct2D1.Media /// public class D2DBitmapImpl : BitmapImpl { - private Bitmap _direct2D; + private Bitmap _direct2DBitmap; /// /// Initialize a new instance of the class /// with a bitmap backed by GPU memory. /// - /// The image factory to use when saving out this bitmap. /// The GPU bitmap. /// /// This bitmap must be either from the same render target, /// or if the render target is a , /// the device associated with this context, to be renderable. /// - public D2DBitmapImpl(WICFactory imagingFactory, Bitmap d2DBitmap) - : base(imagingFactory) + public D2DBitmapImpl(Bitmap d2DBitmap) { - _direct2D = d2DBitmap ?? throw new ArgumentNullException(nameof(d2DBitmap)); + _direct2DBitmap = d2DBitmap ?? throw new ArgumentNullException(nameof(d2DBitmap)); } - public override int PixelWidth => _direct2D.PixelSize.Width; - public override int PixelHeight => _direct2D.PixelSize.Height; + public override int PixelWidth => _direct2DBitmap.PixelSize.Width; + public override int PixelHeight => _direct2DBitmap.PixelSize.Height; public override void Dispose() { base.Dispose(); - _direct2D.Dispose(); + _direct2DBitmap.Dispose(); } public override OptionalDispose GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget target) { - return new OptionalDispose(_direct2D, false); + return new OptionalDispose(_direct2DBitmap, false); } public override void Save(Stream stream) { - using (var encoder = new PngBitmapEncoder(WicImagingFactory, stream)) + using (var encoder = new PngBitmapEncoder(Direct2D1Platform.ImagingFactory, stream)) using (var frameEncode = new SharpDX.WIC.BitmapFrameEncode(encoder)) - using (var imageEncoder = new SharpDX.WIC.ImageEncoder((ImagingFactory2)WicImagingFactory, null)) + //ToDo: Not supported under Windows 7! + using (var imageEncoder = new SharpDX.WIC.ImageEncoder((ImagingFactory2)Direct2D1Platform.ImagingFactory, null)) { var parameters = new ImageParameters( new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), - _direct2D.DotsPerInch.Width, - _direct2D.DotsPerInch.Height, + _direct2DBitmap.DotsPerInch.Width, + _direct2DBitmap.DotsPerInch.Height, 0, 0, PixelWidth, PixelHeight); - imageEncoder.WriteFrame(_direct2D, frameEncode, parameters); + imageEncoder.WriteFrame(_direct2DBitmap, frameEncode, parameters); frameEncode.Commit(); encoder.Commit(); } diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs index 2843848fac..7ea303345a 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs @@ -1,35 +1,25 @@ -using System; -using Avalonia.Platform; +using Avalonia.Platform; using Avalonia.Rendering; using SharpDX; using SharpDX.Direct2D1; -using SharpDX.WIC; using D2DBitmap = SharpDX.Direct2D1.Bitmap; -using DirectWriteFactory = SharpDX.DirectWrite.Factory; namespace Avalonia.Direct2D1.Media.Imaging { public class D2DRenderTargetBitmapImpl : D2DBitmapImpl, IRenderTargetBitmapImpl, ILayerFactory { - private readonly DirectWriteFactory _dwriteFactory; - private readonly BitmapRenderTarget _target; + private readonly BitmapRenderTarget _renderTarget; - public D2DRenderTargetBitmapImpl( - ImagingFactory imagingFactory, - DirectWriteFactory dwriteFactory, - BitmapRenderTarget target) - : base(imagingFactory, target.Bitmap) + public D2DRenderTargetBitmapImpl(BitmapRenderTarget renderTarget) + : base(renderTarget.Bitmap) { - _dwriteFactory = dwriteFactory; - _target = target; + _renderTarget = renderTarget; } - public override int PixelWidth => _target.PixelSize.Width; - public override int PixelHeight => _target.PixelSize.Height; + public override int PixelWidth => _renderTarget.PixelSize.Width; + public override int PixelHeight => _renderTarget.PixelSize.Height; public static D2DRenderTargetBitmapImpl CreateCompatible( - ImagingFactory imagingFactory, - DirectWriteFactory dwriteFactory, SharpDX.Direct2D1.RenderTarget renderTarget, Size size) { @@ -37,32 +27,27 @@ namespace Avalonia.Direct2D1.Media.Imaging renderTarget, CompatibleRenderTargetOptions.None, new Size2F((float)size.Width, (float)size.Height)); - return new D2DRenderTargetBitmapImpl(imagingFactory, dwriteFactory, bitmapRenderTarget); + return new D2DRenderTargetBitmapImpl(bitmapRenderTarget); } public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) { - return new DrawingContextImpl( - visualBrushRenderer, - this, - _target, - _dwriteFactory, - WicImagingFactory); + return new DrawingContextImpl(visualBrushRenderer, this, _renderTarget); } public IRenderTargetBitmapImpl CreateLayer(Size size) { - return CreateCompatible(WicImagingFactory, _dwriteFactory, _target, size); + return CreateCompatible(_renderTarget, size); } public override void Dispose() { - _target.Dispose(); + _renderTarget.Dispose(); } public override OptionalDispose GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget target) { - return new OptionalDispose(_target.Bitmap, false); + return new OptionalDispose(_renderTarget.Bitmap, false); } } } diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs index 371dfcfc3e..2c41f1203f 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicBitmapImpl.cs @@ -19,57 +19,52 @@ namespace Avalonia.Direct2D1.Media /// /// Initializes a new instance of the class. /// - /// The WIC imaging factory to use. /// The filename of the bitmap to load. - public WicBitmapImpl(ImagingFactory factory, string fileName) - : base(factory) + public WicBitmapImpl(string fileName) { - using (BitmapDecoder decoder = new BitmapDecoder(factory, fileName, DecodeOptions.CacheOnDemand)) + using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, fileName, DecodeOptions.CacheOnDemand)) { - WicImpl = new Bitmap(factory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand); + WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnDemand); } } /// /// Initializes a new instance of the class. /// - /// The WIC imaging factory to use. /// The stream to read the bitmap from. - public WicBitmapImpl(ImagingFactory factory, Stream stream) - : base(factory) + public WicBitmapImpl(Stream stream) { - using (BitmapDecoder decoder = new BitmapDecoder(factory, stream, DecodeOptions.CacheOnLoad)) + using (BitmapDecoder decoder = new BitmapDecoder(Direct2D1Platform.ImagingFactory, stream, DecodeOptions.CacheOnLoad)) { - WicImpl = new Bitmap(factory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad); + WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, decoder.GetFrame(0), BitmapCreateCacheOption.CacheOnLoad); } } /// /// Initializes a new instance of the class. /// - /// The WIC imaging factory to use. /// The width of the bitmap. /// The height of the bitmap. /// Pixel format - public WicBitmapImpl(ImagingFactory factory, int width, int height, APixelFormat? pixelFormat = null) - : base(factory) + public WicBitmapImpl(int width, int height, APixelFormat? pixelFormat = null) { if (!pixelFormat.HasValue) + { pixelFormat = APixelFormat.Bgra8888; + } PixelFormat = pixelFormat; WicImpl = new Bitmap( - factory, + Direct2D1Platform.ImagingFactory, width, height, pixelFormat.Value.ToWic(), BitmapCreateCacheOption.CacheOnLoad); } - public WicBitmapImpl(ImagingFactory factory, APixelFormat format, IntPtr data, int width, int height, int stride) - : base(factory) + public WicBitmapImpl(APixelFormat format, IntPtr data, int width, int height, int stride) { - WicImpl = new Bitmap(factory, width, height, format.ToWic(), BitmapCreateCacheOption.CacheOnDemand); + WicImpl = new Bitmap(Direct2D1Platform.ImagingFactory, width, height, format.ToWic(), BitmapCreateCacheOption.CacheOnDemand); PixelFormat = format; using (var l = WicImpl.Lock(BitmapLockFlags.Write)) { @@ -112,14 +107,14 @@ namespace Avalonia.Direct2D1.Media /// The Direct2D bitmap. public override OptionalDispose GetDirect2DBitmap(SharpDX.Direct2D1.RenderTarget renderTarget) { - FormatConverter converter = new FormatConverter(WicImagingFactory); + FormatConverter converter = new FormatConverter(Direct2D1Platform.ImagingFactory); converter.Initialize(WicImpl, SharpDX.WIC.PixelFormat.Format32bppPBGRA); return new OptionalDispose(D2DBitmap.FromWicBitmap(renderTarget, converter), true); } public override void Save(Stream stream) { - using (var encoder = new PngBitmapEncoder(WicImagingFactory, stream)) + using (var encoder = new PngBitmapEncoder(Direct2D1Platform.ImagingFactory, stream)) using (var frame = new BitmapFrameEncode(encoder)) { frame.Initialize(); diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicRenderTargetBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicRenderTargetBitmapImpl.cs index 0eb2608047..aa8b3ead42 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicRenderTargetBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WicRenderTargetBitmapImpl.cs @@ -5,26 +5,20 @@ using System; using Avalonia.Platform; using Avalonia.Rendering; using SharpDX.Direct2D1; -using SharpDX.WIC; -using DirectWriteFactory = SharpDX.DirectWrite.Factory; namespace Avalonia.Direct2D1.Media { public class WicRenderTargetBitmapImpl : WicBitmapImpl, IRenderTargetBitmapImpl { - private readonly DirectWriteFactory _dwriteFactory; - private readonly WicRenderTarget _target; + private readonly WicRenderTarget _renderTarget; public WicRenderTargetBitmapImpl( - ImagingFactory imagingFactory, - Factory d2dFactory, - DirectWriteFactory dwriteFactory, int width, int height, double dpiX, double dpiY, Platform.PixelFormat? pixelFormat = null) - : base(imagingFactory, width, height, pixelFormat) + : base(width, height, pixelFormat) { var props = new RenderTargetProperties { @@ -32,17 +26,16 @@ namespace Avalonia.Direct2D1.Media DpiY = (float)dpiY, }; - _target = new WicRenderTarget( - d2dFactory, + _renderTarget = new WicRenderTarget( + Direct2D1Platform.Direct2D1Factory, WicImpl, props); - - _dwriteFactory = dwriteFactory; } public override void Dispose() { - _target.Dispose(); + _renderTarget.Dispose(); + base.Dispose(); } @@ -51,8 +44,7 @@ namespace Avalonia.Direct2D1.Media public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer, Action finishedCallback) { - return new DrawingContextImpl(visualBrushRenderer, null, _target, _dwriteFactory, WicImagingFactory, - finishedCallback: finishedCallback); + return new DrawingContextImpl(visualBrushRenderer, null, _renderTarget, finishedCallback: finishedCallback); } } } diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs index fc931c32db..075fef5ab2 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Avalonia.Platform; using SharpDX.WIC; using PixelFormat = Avalonia.Platform.PixelFormat; @@ -11,8 +7,8 @@ namespace Avalonia.Direct2D1.Media.Imaging { class WriteableWicBitmapImpl : WicBitmapImpl, IWriteableBitmapImpl { - public WriteableWicBitmapImpl(ImagingFactory factory, int width, int height, PixelFormat? pixelFormat) - : base(factory, width, height, pixelFormat) + public WriteableWicBitmapImpl(int width, int height, PixelFormat? pixelFormat) + : base(width, height, pixelFormat) { } diff --git a/src/Windows/Avalonia.Direct2D1/Media/StreamGeometryImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/StreamGeometryImpl.cs index 4c1bc3d6f7..a07d5c1c72 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/StreamGeometryImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/StreamGeometryImpl.cs @@ -31,8 +31,7 @@ namespace Avalonia.Direct2D1.Media /// public IStreamGeometryImpl Clone() { - Factory factory = AvaloniaLocator.Current.GetService(); - var result = new PathGeometry(factory); + var result = new PathGeometry(Direct2D1Platform.Direct2D1Factory); var sink = result.Open(); ((PathGeometry)Geometry).Stream(sink); sink.Close(); @@ -47,8 +46,7 @@ namespace Avalonia.Direct2D1.Media private static Geometry CreateGeometry() { - Factory factory = AvaloniaLocator.Current.GetService(); - return new PathGeometry(factory); + return new PathGeometry(Direct2D1Platform.Direct2D1Factory); } } } diff --git a/src/Windows/Avalonia.Direct2D1/RenderTarget.cs b/src/Windows/Avalonia.Direct2D1/RenderTarget.cs index 6086b0c67c..a21b5571c6 100644 --- a/src/Windows/Avalonia.Direct2D1/RenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/RenderTarget.cs @@ -1,14 +1,10 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; using Avalonia.Direct2D1.Media; using Avalonia.Direct2D1.Media.Imaging; using Avalonia.Platform; using Avalonia.Rendering; -using SharpDX.Direct2D1; -using DwFactory = SharpDX.DirectWrite.Factory; -using WicFactory = SharpDX.WIC.ImagingFactory; namespace Avalonia.Direct2D1 { @@ -25,32 +21,21 @@ namespace Avalonia.Direct2D1 /// The render target. public RenderTarget(SharpDX.Direct2D1.RenderTarget renderTarget) { - Direct2DFactory = AvaloniaLocator.Current.GetService(); - DirectWriteFactory = AvaloniaLocator.Current.GetService(); - WicFactory = AvaloniaLocator.Current.GetService(); _renderTarget = renderTarget; } - public Factory Direct2DFactory { get; } - public DwFactory DirectWriteFactory { get; } - public WicFactory WicFactory { get; } - /// /// Creates a drawing context for a rendering session. /// /// An . public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) { - return new DrawingContextImpl(visualBrushRenderer, this, _renderTarget, DirectWriteFactory, WicFactory); + return new DrawingContextImpl(visualBrushRenderer, this, _renderTarget); } public IRenderTargetBitmapImpl CreateLayer(Size size) { - return D2DRenderTargetBitmapImpl.CreateCompatible( - WicFactory, - DirectWriteFactory, - _renderTarget, - size); + return D2DRenderTargetBitmapImpl.CreateCompatible(_renderTarget, size); } public void Dispose() diff --git a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs index 0a23c63498..f820129b8d 100644 --- a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs @@ -1,16 +1,14 @@ -using System; +using Avalonia.Direct2D1.Media; +using Avalonia.Direct2D1.Media.Imaging; using Avalonia.Platform; +using Avalonia.Rendering; + using SharpDX; using SharpDX.Direct2D1; using SharpDX.DXGI; -using PixelFormat = SharpDX.Direct2D1.PixelFormat; + using AlphaMode = SharpDX.Direct2D1.AlphaMode; -using Device = SharpDX.Direct2D1.Device; -using Factory = SharpDX.Direct2D1.Factory; -using Factory2 = SharpDX.DXGI.Factory2; -using Avalonia.Rendering; -using Avalonia.Direct2D1.Media; -using Avalonia.Direct2D1.Media.Imaging; +using PixelFormat = SharpDX.Direct2D1.PixelFormat; namespace Avalonia.Direct2D1 { @@ -21,23 +19,6 @@ namespace Avalonia.Direct2D1 private DeviceContext _deviceContext; private SwapChain1 _swapChain; - protected SwapChainRenderTarget() - { - DxgiDevice = AvaloniaLocator.Current.GetService(); - D2DDevice = AvaloniaLocator.Current.GetService(); - Direct2DFactory = AvaloniaLocator.Current.GetService(); - DirectWriteFactory = AvaloniaLocator.Current.GetService(); - WicImagingFactory = AvaloniaLocator.Current.GetService(); - } - - public Factory Direct2DFactory { get; } - public SharpDX.DirectWrite.Factory DirectWriteFactory { get; } - public SharpDX.WIC.ImagingFactory WicImagingFactory { get; } - - protected SharpDX.DXGI.Device DxgiDevice { get; } - - public Device D2DDevice { get; } - /// /// Creates a drawing context for a rendering session. /// @@ -54,13 +35,7 @@ namespace Avalonia.Direct2D1 CreateSwapChain(); } - return new DrawingContextImpl( - visualBrushRenderer, - this, - _deviceContext, - DirectWriteFactory, - WicImagingFactory, - _swapChain); + return new DrawingContextImpl(visualBrushRenderer, this, _deviceContext, _swapChain); } public IRenderTargetBitmapImpl CreateLayer(Size size) @@ -70,11 +45,7 @@ namespace Avalonia.Direct2D1 CreateSwapChain(); } - return D2DRenderTargetBitmapImpl.CreateCompatible( - WicImagingFactory, - DirectWriteFactory, - _deviceContext, - size); + return D2DRenderTargetBitmapImpl.CreateCompatible(_deviceContext, size); } public void Dispose() @@ -85,11 +56,11 @@ namespace Avalonia.Direct2D1 private void CreateSwapChain() { - using (var dxgiAdaptor = DxgiDevice.Adapter) - using (var dxgiFactory = dxgiAdaptor.GetParent()) + using (var dxgiAdaptor = Direct2D1Platform.DxgiDevice.Adapter) + using (var dxgiFactory = dxgiAdaptor.GetParent()) { _deviceContext?.Dispose(); - _deviceContext = new DeviceContext(D2DDevice, DeviceContextOptions.None) {DotsPerInch = _savedDpi}; + _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; var swapChainDesc = new SwapChainDescription1 { @@ -119,7 +90,7 @@ namespace Avalonia.Direct2D1 new BitmapProperties1( new PixelFormat { - AlphaMode = AlphaMode.Ignore, + AlphaMode = AlphaMode.Premultiplied, Format = Format.B8G8R8A8_UNorm }, _savedDpi.Width, @@ -131,7 +102,7 @@ namespace Avalonia.Direct2D1 } } - protected abstract SwapChain1 CreateSwapChain(Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc); + protected abstract SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc); protected abstract Size2F GetWindowDpi(); From e76b694ebdcc4b2ca79532cb30183ea8aa98fc79 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 5 Sep 2018 17:02:22 +0200 Subject: [PATCH 02/67] InterpolationMode.HighQuality support --- .../Media/DrawingContextImpl.cs | 103 ++++++++++-------- 1 file changed, 60 insertions(+), 43 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs index 3aec6c6eb1..b842f26edb 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs @@ -22,6 +22,7 @@ namespace Avalonia.Direct2D1.Media private readonly IVisualBrushRenderer _visualBrushRenderer; private readonly ILayerFactory _layerFactory; private readonly SharpDX.Direct2D1.RenderTarget _renderTarget; + private readonly DeviceContext _deviceContext; private readonly SharpDX.DXGI.SwapChain1 _swapChain; private readonly Action _finishedCallback; @@ -48,7 +49,17 @@ namespace Avalonia.Direct2D1.Media _renderTarget = renderTarget; _swapChain = swapChain; _finishedCallback = finishedCallback; - _renderTarget.BeginDraw(); + + if (_renderTarget is DeviceContext deviceContext) + { + _deviceContext = deviceContext; + } + else + { + _deviceContext = _renderTarget.QueryInterface(); + } + + _deviceContext.BeginDraw(); } /// @@ -56,14 +67,14 @@ namespace Avalonia.Direct2D1.Media /// public Matrix Transform { - get { return _renderTarget.Transform.ToAvalonia(); } - set { _renderTarget.Transform = value.ToDirect2D(); } + get { return _deviceContext.Transform.ToAvalonia(); } + set { _deviceContext.Transform = value.ToDirect2D(); } } /// public void Clear(Color color) { - _renderTarget.Clear(color.ToDirect2D()); + _deviceContext.Clear(color.ToDirect2D()); } /// @@ -72,10 +83,13 @@ namespace Avalonia.Direct2D1.Media public void Dispose() { foreach (var layer in _layerPool) + { layer.Dispose(); + } + try { - _renderTarget.EndDraw(); + _deviceContext.EndDraw(); _swapChain?.Present(1, SharpDX.DXGI.PresentFlags.None); _finishedCallback?.Invoke(); @@ -96,29 +110,32 @@ namespace Avalonia.Direct2D1.Media /// The bitmap interpolation mode. public void DrawImage(IRef source, double opacity, Rect sourceRect, Rect destRect, BitmapInterpolationMode bitmapInterpolationMode) { - using (var d2d = ((BitmapImpl)source.Item).GetDirect2DBitmap(_renderTarget)) + using (var d2d = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) { var interpolationMode = GetInterpolationMode(bitmapInterpolationMode); - _renderTarget.DrawBitmap( + _deviceContext.DrawBitmap( d2d.Value, destRect.ToSharpDX(), (float)opacity, interpolationMode, - sourceRect.ToSharpDX()); + sourceRect.ToSharpDX(), + null); } } - private static SharpDX.Direct2D1.BitmapInterpolationMode GetInterpolationMode(BitmapInterpolationMode interpolationMode) + private static InterpolationMode GetInterpolationMode(BitmapInterpolationMode interpolationMode) { switch (interpolationMode) { case BitmapInterpolationMode.LowQuality: - return SharpDX.Direct2D1.BitmapInterpolationMode.NearestNeighbor; + return InterpolationMode.NearestNeighbor; case BitmapInterpolationMode.MediumQuality: + return InterpolationMode.Linear; case BitmapInterpolationMode.HighQuality: + return InterpolationMode.HighQualityCubic; case BitmapInterpolationMode.Default: - return SharpDX.Direct2D1.BitmapInterpolationMode.Linear; + return InterpolationMode.Linear; default: throw new ArgumentOutOfRangeException(nameof(interpolationMode), interpolationMode, null); } @@ -133,17 +150,17 @@ namespace Avalonia.Direct2D1.Media /// The rect in the output to draw to. public void DrawImage(IRef source, IBrush opacityMask, Rect opacityMaskRect, Rect destRect) { - using (var d2dSource = ((BitmapImpl)source.Item).GetDirect2DBitmap(_renderTarget)) - using (var sourceBrush = new BitmapBrush(_renderTarget, d2dSource.Value)) + using (var d2dSource = ((BitmapImpl)source.Item).GetDirect2DBitmap(_deviceContext)) + using (var sourceBrush = new BitmapBrush(_deviceContext, d2dSource.Value)) using (var d2dOpacityMask = CreateBrush(opacityMask, opacityMaskRect.Size)) - using (var geometry = new SharpDX.Direct2D1.RectangleGeometry(_renderTarget.Factory, destRect.ToDirect2D())) + using (var geometry = new SharpDX.Direct2D1.RectangleGeometry(_deviceContext.Factory, destRect.ToDirect2D())) { if (d2dOpacityMask.PlatformBrush != null) { d2dOpacityMask.PlatformBrush.Transform = Matrix.CreateTranslation(opacityMaskRect.Position).ToDirect2D(); } - _renderTarget.FillGeometry( + _deviceContext.FillGeometry( geometry, sourceBrush, d2dOpacityMask.PlatformBrush); @@ -163,11 +180,11 @@ namespace Avalonia.Direct2D1.Media var size = new Rect(p1, p2).Size; using (var d2dBrush = CreateBrush(pen.Brush, size)) - using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) + using (var d2dStroke = pen.ToDirect2DStrokeStyle(_deviceContext)) { if (d2dBrush.PlatformBrush != null) { - _renderTarget.DrawLine( + _deviceContext.DrawLine( p1.ToSharpDX(), p2.ToSharpDX(), d2dBrush.PlatformBrush, @@ -193,7 +210,7 @@ namespace Avalonia.Direct2D1.Media if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry; - _renderTarget.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); + _deviceContext.FillGeometry(impl.Geometry, d2dBrush.PlatformBrush); } } } @@ -201,12 +218,12 @@ namespace Avalonia.Direct2D1.Media if (pen != null) { using (var d2dBrush = CreateBrush(pen.Brush, geometry.GetRenderBounds(pen).Size)) - using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) + using (var d2dStroke = pen.ToDirect2DStrokeStyle(_deviceContext)) { if (d2dBrush.PlatformBrush != null) { var impl = (GeometryImpl)geometry; - _renderTarget.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); + _deviceContext.DrawGeometry(impl.Geometry, d2dBrush.PlatformBrush, (float)pen.Thickness, d2dStroke); } } } @@ -221,13 +238,13 @@ namespace Avalonia.Direct2D1.Media public void DrawRectangle(Pen pen, Rect rect, float cornerRadius) { using (var brush = CreateBrush(pen.Brush, rect.Size)) - using (var d2dStroke = pen.ToDirect2DStrokeStyle(_renderTarget)) + using (var d2dStroke = pen.ToDirect2DStrokeStyle(_deviceContext)) { if (brush.PlatformBrush != null) { if (cornerRadius == 0) { - _renderTarget.DrawRectangle( + _deviceContext.DrawRectangle( rect.ToDirect2D(), brush.PlatformBrush, (float)pen.Thickness, @@ -235,7 +252,7 @@ namespace Avalonia.Direct2D1.Media } else { - _renderTarget.DrawRoundedRectangle( + _deviceContext.DrawRoundedRectangle( new RoundedRectangle { Rect = rect.ToDirect2D(), RadiusX = cornerRadius, RadiusY = cornerRadius }, brush.PlatformBrush, (float)pen.Thickness, @@ -258,7 +275,7 @@ namespace Avalonia.Direct2D1.Media var impl = (FormattedTextImpl)text; using (var brush = CreateBrush(foreground, impl.Size)) - using (var renderer = new AvaloniaTextRenderer(this, _renderTarget, brush.PlatformBrush)) + using (var renderer = new AvaloniaTextRenderer(this, _deviceContext, brush.PlatformBrush)) { if (brush.PlatformBrush != null) { @@ -282,11 +299,11 @@ namespace Avalonia.Direct2D1.Media { if (cornerRadius == 0) { - _renderTarget.FillRectangle(rect.ToDirect2D(), b.PlatformBrush); + _deviceContext.FillRectangle(rect.ToDirect2D(), b.PlatformBrush); } else { - _renderTarget.FillRoundedRectangle( + _deviceContext.FillRoundedRectangle( new RoundedRectangle { Rect = new RawRectangleF( @@ -312,7 +329,7 @@ namespace Avalonia.Direct2D1.Media else { var platform = AvaloniaLocator.Current.GetService(); - var dpi = new Vector(_renderTarget.DotsPerInch.Width, _renderTarget.DotsPerInch.Height); + var dpi = new Vector(_deviceContext.DotsPerInch.Width, _deviceContext.DotsPerInch.Height); var pixelSize = size * (dpi / 96); return platform.CreateRenderTargetBitmap( (int)pixelSize.Width, @@ -329,12 +346,12 @@ namespace Avalonia.Direct2D1.Media /// A disposable used to undo the clip rectangle. public void PushClip(Rect clip) { - _renderTarget.PushAxisAlignedClip(clip.ToSharpDX(), AntialiasMode.PerPrimitive); + _deviceContext.PushAxisAlignedClip(clip.ToSharpDX(), AntialiasMode.PerPrimitive); } public void PopClip() { - _renderTarget.PopAxisAlignedClip(); + _deviceContext.PopAxisAlignedClip(); } readonly Stack _layers = new Stack(); @@ -355,8 +372,8 @@ namespace Avalonia.Direct2D1.Media Opacity = (float)opacity, }; - var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_renderTarget); - _renderTarget.PushLayer(ref parameters, layer); + var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_deviceContext); + _deviceContext.PushLayer(ref parameters, layer); _layers.Push(layer); } @@ -374,7 +391,7 @@ namespace Avalonia.Direct2D1.Media var layer = _layers.Pop(); if (layer != null) { - _renderTarget.PopLayer(); + _deviceContext.PopLayer(); _layerPool.Push(layer); } } @@ -395,21 +412,21 @@ namespace Avalonia.Direct2D1.Media if (solidColorBrush != null) { - return new SolidColorBrushImpl(solidColorBrush, _renderTarget); + return new SolidColorBrushImpl(solidColorBrush, _deviceContext); } else if (linearGradientBrush != null) { - return new LinearGradientBrushImpl(linearGradientBrush, _renderTarget, destinationSize); + return new LinearGradientBrushImpl(linearGradientBrush, _deviceContext, destinationSize); } else if (radialGradientBrush != null) { - return new RadialGradientBrushImpl(radialGradientBrush, _renderTarget, destinationSize); + return new RadialGradientBrushImpl(radialGradientBrush, _deviceContext, destinationSize); } else if (imageBrush?.Source != null) { return new ImageBrushImpl( imageBrush, - _renderTarget, + _deviceContext, (BitmapImpl)imageBrush.Source.PlatformImpl.Item, destinationSize); } @@ -422,7 +439,7 @@ namespace Avalonia.Direct2D1.Media if (intermediateSize.Width >= 1 && intermediateSize.Height >= 1) { using (var intermediate = new BitmapRenderTarget( - _renderTarget, + _deviceContext, CompatibleRenderTargetOptions.None, intermediateSize.ToSharpDX())) { @@ -434,7 +451,7 @@ namespace Avalonia.Direct2D1.Media return new ImageBrushImpl( visualBrush, - _renderTarget, + _deviceContext, new D2DBitmapImpl(intermediate.Bitmap), destinationSize); } @@ -446,7 +463,7 @@ namespace Avalonia.Direct2D1.Media } } - return new SolidColorBrushImpl(null, _renderTarget); + return new SolidColorBrushImpl(null, _deviceContext); } public void PushGeometryClip(IGeometryImpl clip) @@ -458,8 +475,8 @@ namespace Avalonia.Direct2D1.Media Opacity = 1, GeometricMask = ((GeometryImpl)clip).Geometry }; - var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_renderTarget); - _renderTarget.PushLayer(ref parameters, layer); + var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_deviceContext); + _deviceContext.PushLayer(ref parameters, layer); _layers.Push(layer); @@ -479,8 +496,8 @@ namespace Avalonia.Direct2D1.Media Opacity = 1, OpacityBrush = CreateBrush(mask, bounds.Size).PlatformBrush }; - var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_renderTarget); - _renderTarget.PushLayer(ref parameters, layer); + var layer = _layerPool.Count != 0 ? _layerPool.Pop() : new Layer(_deviceContext); + _deviceContext.PushLayer(ref parameters, layer); _layers.Push(layer); } From 0646106f8fe9d6ead0e433024ed909405cd93c6a Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 5 Sep 2018 19:53:57 +0200 Subject: [PATCH 03/67] Introduce ResizeBuffers to prevent SwapChain recreation --- .../SwapChainRenderTarget.cs | 105 ++++++++++-------- 1 file changed, 61 insertions(+), 44 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs index f820129b8d..94d407ed0b 100644 --- a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs @@ -7,11 +7,13 @@ using SharpDX; using SharpDX.Direct2D1; using SharpDX.DXGI; -using AlphaMode = SharpDX.Direct2D1.AlphaMode; -using PixelFormat = SharpDX.Direct2D1.PixelFormat; - namespace Avalonia.Direct2D1 { + + using AlphaMode = SharpDX.Direct2D1.AlphaMode; + using DeviceContext = SharpDX.Direct2D1.DeviceContext; + using PixelFormat = SharpDX.Direct2D1.PixelFormat; + public abstract class SwapChainRenderTarget : IRenderTarget, ILayerFactory { private Size2 _savedSize; @@ -32,7 +34,8 @@ namespace Avalonia.Direct2D1 { _savedSize = size; _savedDpi = dpi; - CreateSwapChain(); + + Resize(); } return new DrawingContextImpl(visualBrushRenderer, this, _deviceContext, _swapChain); @@ -42,7 +45,7 @@ namespace Avalonia.Direct2D1 { if (_deviceContext == null) { - CreateSwapChain(); + CreateDeviceContext(); } return D2DRenderTargetBitmapImpl.CreateCompatible(_deviceContext, size); @@ -51,54 +54,68 @@ namespace Avalonia.Direct2D1 public void Dispose() { _deviceContext?.Dispose(); + _swapChain?.Dispose(); } + private void Resize() + { + _deviceContext?.Dispose(); + _deviceContext = null; + + _swapChain?.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.None); + + CreateDeviceContext(); + } + private void CreateSwapChain() { - using (var dxgiAdaptor = Direct2D1Platform.DxgiDevice.Adapter) - using (var dxgiFactory = dxgiAdaptor.GetParent()) + var swapChainDescription = new SwapChainDescription1 { - _deviceContext?.Dispose(); - _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; - - var swapChainDesc = new SwapChainDescription1 + Width = _savedSize.Width, + Height = _savedSize.Height, + Format = Format.B8G8R8A8_UNorm, + SampleDescription = new SampleDescription { - Width = _savedSize.Width, - Height = _savedSize.Height, - Format = Format.B8G8R8A8_UNorm, - Stereo = false, - SampleDescription = new SampleDescription + Count = 1, + Quality = 0, + }, + Usage = Usage.RenderTargetOutput, + BufferCount = 1, + SwapEffect = SwapEffect.Discard, + }; + + using (var dxgiAdapter = Direct2D1Platform.DxgiDevice.Adapter) + using (var dxgiFactory = dxgiAdapter.GetParent()) + { + _swapChain = CreateSwapChain(dxgiFactory, swapChainDescription); + } + } + + private void CreateDeviceContext() + { + _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = _savedDpi }; + + if (_swapChain == null) + { + CreateSwapChain(); + } + + using (var dxgiBackBuffer = _swapChain.GetBackBuffer(0)) + using (var d2dBackBuffer = new Bitmap1( + _deviceContext, + dxgiBackBuffer, + new BitmapProperties1( + new PixelFormat { - Count = 1, - Quality = 0, + AlphaMode = AlphaMode.Premultiplied, + Format = Format.B8G8R8A8_UNorm }, - Usage = Usage.RenderTargetOutput, - BufferCount = 1, - Scaling = Scaling.Stretch, - SwapEffect = SwapEffect.Discard, - Flags = 0, - }; - - _swapChain?.Dispose(); - _swapChain = CreateSwapChain(dxgiFactory, swapChainDesc); - - using (var dxgiBackBuffer = _swapChain.GetBackBuffer(0)) - using (var d2dBackBuffer = new Bitmap1( - _deviceContext, - dxgiBackBuffer, - new BitmapProperties1( - new PixelFormat - { - AlphaMode = AlphaMode.Premultiplied, - Format = Format.B8G8R8A8_UNorm - }, - _savedDpi.Width, - _savedDpi.Height, - BitmapOptions.Target | BitmapOptions.CannotDraw))) - { - _deviceContext.Target = d2dBackBuffer; - } + _savedSize.Width, + _savedSize.Height, + BitmapOptions.Target | BitmapOptions.CannotDraw))) + { + _deviceContext.Target = d2dBackBuffer; } } From 98ee428d6f4c31e5e7bfa0c2cda49a11a4a86459 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Thu, 6 Sep 2018 14:05:56 +0200 Subject: [PATCH 04/67] Merge fix --- .../SwapChainRenderTarget.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs index 000ce23cd2..ffa0f5c8bc 100644 --- a/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs +++ b/src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs @@ -1,19 +1,13 @@ -using Avalonia.Direct2D1.Media; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Direct2D1.Media; using Avalonia.Direct2D1.Media.Imaging; using Avalonia.Platform; using Avalonia.Rendering; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DXGI; -using AlphaMode = SharpDX.Direct2D1.AlphaMode; -using Device = SharpDX.Direct2D1.Device; -using Factory = SharpDX.Direct2D1.Factory; -using Factory2 = SharpDX.DXGI.Factory2; -using PixelFormat = SharpDX.Direct2D1.PixelFormat; - -using AlphaMode = SharpDX.Direct2D1.AlphaMode; -using DeviceContext = SharpDX.Direct2D1.DeviceContext; -using PixelFormat = SharpDX.Direct2D1.PixelFormat; namespace Avalonia.Direct2D1 { @@ -109,9 +103,9 @@ namespace Avalonia.Direct2D1 _deviceContext, dxgiBackBuffer, new BitmapProperties1( - new PixelFormat + new SharpDX.Direct2D1.PixelFormat { - AlphaMode = AlphaMode.Premultiplied, + AlphaMode = SharpDX.Direct2D1.AlphaMode.Premultiplied, Format = Format.B8G8R8A8_UNorm }, _savedSize.Width, From 024e0da1da398b765422344cbb768f1efef4cfe9 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Thu, 6 Sep 2018 15:47:02 +0200 Subject: [PATCH 05/67] Direct3DInterop fixes --- .../Direct3DInteropSample/MainWindow.cs | 276 ++++++++++-------- .../Avalonia.Direct2D1/Direct2D1Platform.cs | 14 +- .../Media/Imaging/D2DBitmapImpl.cs | 29 +- .../Imaging/D2DRenderTargetBitmapImpl.cs | 5 +- .../Media/Imaging/WriteableWicBitmapImpl.cs | 5 +- 5 files changed, 176 insertions(+), 153 deletions(-) diff --git a/samples/interop/Direct3DInteropSample/MainWindow.cs b/samples/interop/Direct3DInteropSample/MainWindow.cs index 19c31a3af1..065f1a285a 100644 --- a/samples/interop/Direct3DInteropSample/MainWindow.cs +++ b/samples/interop/Direct3DInteropSample/MainWindow.cs @@ -1,81 +1,83 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; + using Avalonia; using Avalonia.Controls; +using Avalonia.Direct2D1; using Avalonia.Direct2D1.Media; using Avalonia.Markup.Xaml; using Avalonia.Platform; using Avalonia.Rendering; + using SharpDX; using SharpDX.D3DCompiler; using SharpDX.Direct2D1; using SharpDX.Direct3D; using SharpDX.Direct3D11; using SharpDX.DXGI; -using SharpDX.WIC; -using SharpDX.Mathematics; + using AlphaMode = SharpDX.Direct2D1.AlphaMode; using Buffer = SharpDX.Direct3D11.Buffer; -using DeviceContext = SharpDX.Direct3D11.DeviceContext; -using Factory1 = SharpDX.DXGI.Factory1; +using DeviceContext = SharpDX.Direct2D1.DeviceContext; +using Factory2 = SharpDX.DXGI.Factory2; using InputElement = SharpDX.Direct3D11.InputElement; using Matrix = SharpDX.Matrix; using PixelFormat = SharpDX.Direct2D1.PixelFormat; +using Resource = SharpDX.Direct3D11.Resource; namespace Direct3DInteropSample { - class MainWindow : Window + public class MainWindow : Window { - private SharpDX.Direct3D11.Device _d3dDevice; - private SharpDX.DXGI.Device _dxgiDevice; - Texture2D backBuffer = null; - RenderTargetView renderView = null; - Texture2D depthBuffer = null; - DepthStencilView depthView = null; + Texture2D _backBuffer; + RenderTargetView _renderView; + Texture2D _depthBuffer; + DepthStencilView _depthView; private readonly SwapChain _swapChain; - private SwapChainDescription _desc; + private SwapChainDescription1 _desc; private Matrix _proj = Matrix.Identity; - private Matrix _view; + private readonly Matrix _view; private Buffer _contantBuffer; - private SharpDX.Direct2D1.Device _d2dDevice; - private SharpDX.Direct2D1.DeviceContext _d2dContext; - private RenderTarget _d2dRenderTarget; - private MainWindowViewModel _model; + private DeviceContext _deviceContext; + private readonly MainWindowViewModel _model; public MainWindow() { - _dxgiDevice = AvaloniaLocator.Current.GetService(); - _d3dDevice = _dxgiDevice.QueryInterface(); - _d2dDevice = AvaloniaLocator.Current.GetService(); DataContext = _model = new MainWindowViewModel(); - _desc = new SwapChainDescription() + + _desc = new SwapChainDescription1() { BufferCount = 1, - ModeDescription = - new ModeDescription((int)ClientSize.Width, (int)ClientSize.Height, - new Rational(60, 1), Format.R8G8B8A8_UNorm), - IsWindowed = true, - OutputHandle = PlatformImpl?.Handle.Handle ?? IntPtr.Zero, + Width = (int)ClientSize.Width, + Height = (int)ClientSize.Height, + Format = Format.R8G8B8A8_UNorm, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; - _swapChain = new SwapChain(new Factory1(), _d3dDevice, _desc); + using (var factory = Direct2D1Platform.DxgiDevice.Adapter.GetParent()) + { + _swapChain = new SwapChain1(factory, Direct2D1Platform.DxgiDevice, PlatformImpl?.Handle.Handle ?? IntPtr.Zero, ref _desc); + } - _d2dContext = new SharpDX.Direct2D1.DeviceContext(_d2dDevice, DeviceContextOptions.None) + _deviceContext = new DeviceContext(Direct2D1Platform.Direct2D1Device, DeviceContextOptions.None) { DotsPerInch = new Size2F(96, 96) }; CreateMesh(); + _view = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY); + this.GetObservable(ClientSizeProperty).Subscribe(Resize); + Resize(ClientSize); + AvaloniaXamlLoader.Load(this); + Background = Avalonia.Media.Brushes.Transparent; } @@ -83,29 +85,32 @@ namespace Direct3DInteropSample protected override void HandlePaint(Rect rect) { var viewProj = Matrix.Multiply(_view, _proj); - var context = _d3dDevice.ImmediateContext; + var context = Direct2D1Platform.Direct3D11Device.ImmediateContext; + // Clear views - context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0); - context.ClearRenderTargetView(renderView, Color.White); + context.ClearDepthStencilView(_depthView, DepthStencilClearFlags.Depth, 1.0f, 0); + context.ClearRenderTargetView(_renderView, Color.White); // Update WorldViewProj Matrix - var worldViewProj = Matrix.RotationX((float) _model.RotationX) * Matrix.RotationY((float) _model.RotationY) * - Matrix.RotationZ((float) _model.RotationZ) - * Matrix.Scaling((float) _model.Zoom) * viewProj; + var worldViewProj = Matrix.RotationX((float)_model.RotationX) * Matrix.RotationY((float)_model.RotationY) + * Matrix.RotationZ((float)_model.RotationZ) + * Matrix.Scaling((float)_model.Zoom) + * viewProj; worldViewProj.Transpose(); context.UpdateSubresource(ref worldViewProj, _contantBuffer); // Draw the cube context.Draw(36, 0); base.HandlePaint(rect); + // Present! _swapChain.Present(0, PresentFlags.None); } - - void CreateMesh() + private void CreateMesh() { - var device = _d3dDevice; + var device = Direct2D1Platform.Direct3D11Device; + // Compile Vertex and Pixel shaders var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "VS", "vs_4_0"); var vertexShader = new VertexShader(device, vertexShaderByteCode); @@ -114,63 +119,72 @@ namespace Direct3DInteropSample var pixelShader = new PixelShader(device, pixelShaderByteCode); var signature = ShaderSignature.GetInputSignature(vertexShaderByteCode); + + var inputElements = new[] + { + new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), + new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) + }; + // Layout from VertexShader input signature - var layout = new InputLayout(device, signature, new[] - { - new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), - new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0) - }); - + var layout = new InputLayout( + device, + signature, + inputElements); + // Instantiate Vertex buiffer from vertex data - var vertices = Buffer.Create(device, BindFlags.VertexBuffer, new[] - { - new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front - new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), - new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), - new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), - new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), - new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), - - new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), - new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), - new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), - new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), - - new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top - new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), - new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), - new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), - - new Vector4(-1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom - new Vector4( 1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), - new Vector4(-1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), - new Vector4(-1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), - new Vector4( 1.0f,-1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), - new Vector4( 1.0f,-1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), - - new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left - new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), - new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), - new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), - new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), - new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), - - new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), - new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), - new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), - new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), - new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), - }); + var vertices = Buffer.Create( + device, + BindFlags.VertexBuffer, + new[] + { + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), // Front + new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), + new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), + new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), + new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), + + new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), // BACK + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), + new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), + new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), + new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), + + new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), // Top + new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), + new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), + new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), + + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), // Bottom + new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), + new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), + new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), + new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f), + + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), // Left + new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), + new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), + new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), + new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), + new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f), + + new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), // Right + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), + new Vector4( 1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), + new Vector4( 1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), + new Vector4( 1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), + new Vector4( 1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f), + }); // Create Constant Buffer _contantBuffer = new Buffer(device, Utilities.SizeOf(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); - var context = _d3dDevice.ImmediateContext; + var context = Direct2D1Platform.Direct3D11Device.ImmediateContext; // Prepare All the stages context.InputAssembler.InputLayout = layout; @@ -181,63 +195,73 @@ namespace Direct3DInteropSample context.PixelShader.Set(pixelShader); } - void Resize(Size size) + private void Resize(Size size) { - Utilities.Dispose(ref _d2dRenderTarget); - Utilities.Dispose(ref backBuffer); - Utilities.Dispose(ref renderView); - Utilities.Dispose(ref depthBuffer); - Utilities.Dispose(ref depthView); - var context = _d3dDevice.ImmediateContext; + Utilities.Dispose(ref _deviceContext); + Utilities.Dispose(ref _backBuffer); + Utilities.Dispose(ref _renderView); + Utilities.Dispose(ref _depthBuffer); + Utilities.Dispose(ref _depthView); + var context = Direct2D1Platform.Direct3D11Device.ImmediateContext; + // Resize the backbuffer - _swapChain.ResizeBuffers(_desc.BufferCount, (int)size.Width, (int)size.Height, Format.Unknown, SwapChainFlags.None); + _swapChain.ResizeBuffers(0, 0, 0, Format.Unknown, SwapChainFlags.None); // Get the backbuffer from the swapchain - backBuffer = Texture2D.FromSwapChain(_swapChain, 0); + _backBuffer = Resource.FromSwapChain(_swapChain, 0); // Renderview on the backbuffer - renderView = new RenderTargetView(_d3dDevice, backBuffer); + _renderView = new RenderTargetView(Direct2D1Platform.Direct3D11Device, _backBuffer); // Create the depth buffer - depthBuffer = new Texture2D(_d3dDevice, new Texture2DDescription() - { - Format = Format.D32_Float_S8X24_UInt, - ArraySize = 1, - MipLevels = 1, - Width = (int)size.Width, - Height = (int)size.Height, - SampleDescription = new SampleDescription(1, 0), - Usage = ResourceUsage.Default, - BindFlags = BindFlags.DepthStencil, - CpuAccessFlags = CpuAccessFlags.None, - OptionFlags = ResourceOptionFlags.None - }); + _depthBuffer = new Texture2D( + Direct2D1Platform.Direct3D11Device, + new Texture2DDescription() + { + Format = Format.D32_Float_S8X24_UInt, + ArraySize = 1, + MipLevels = 1, + Width = (int)size.Width, + Height = (int)size.Height, + SampleDescription = new SampleDescription(1, 0), + Usage = ResourceUsage.Default, + BindFlags = BindFlags.DepthStencil, + CpuAccessFlags = CpuAccessFlags.None, + OptionFlags = ResourceOptionFlags.None + }); // Create the depth buffer view - depthView = new DepthStencilView(_d3dDevice, depthBuffer); + _depthView = new DepthStencilView(Direct2D1Platform.Direct3D11Device, _depthBuffer); // Setup targets and viewport for rendering context.Rasterizer.SetViewport(new Viewport(0, 0, (int)size.Width, (int)size.Height, 0.0f, 1.0f)); - context.OutputMerger.SetTargets(depthView, renderView); + context.OutputMerger.SetTargets(_depthView, _renderView); // Setup new projection matrix with correct aspect ratio _proj = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)(size.Width / size.Height), 0.1f, 100.0f); using (var dxgiBackBuffer = _swapChain.GetBackBuffer(0)) { - _d2dRenderTarget = new RenderTarget(AvaloniaLocator.Current.GetService() - , dxgiBackBuffer, new RenderTargetProperties + var renderTarget = new SharpDX.Direct2D1.RenderTarget( + Direct2D1Platform.Direct2D1Factory, + dxgiBackBuffer, + new RenderTargetProperties { DpiX = 96, DpiY = 96, Type = RenderTargetType.Default, - PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied) + PixelFormat = new PixelFormat( + Format.Unknown, + AlphaMode.Premultiplied) }); - } + _deviceContext = renderTarget.QueryInterface(); + + renderTarget.Dispose(); + } } - class D3DRenderTarget: IRenderTarget + private class D3DRenderTarget : IRenderTarget { private readonly MainWindow _window; @@ -245,16 +269,14 @@ namespace Direct3DInteropSample { _window = window; } + public void Dispose() { - } public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer) { - return new DrawingContextImpl(visualBrushRenderer, null, _window._d2dRenderTarget, - AvaloniaLocator.Current.GetService(), - AvaloniaLocator.Current.GetService()); + return new DrawingContextImpl(visualBrushRenderer, null, _window._deviceContext); } } diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index 437bae6fd1..38c49924bc 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -29,6 +29,8 @@ namespace Avalonia.Direct2D1 { private static readonly Direct2D1Platform s_instance = new Direct2D1Platform(); + public static SharpDX.Direct3D11.Device Direct3D11Device { get; private set; } + public static SharpDX.Direct2D1.Factory1 Direct2D1Factory { get; private set; } public static SharpDX.Direct2D1.Device1 Direct2D1Device { get; private set; } @@ -88,14 +90,12 @@ namespace Avalonia.Direct2D1 SharpDX.Direct3D.FeatureLevel.Level_9_1, }; - using (var d3dDevice = new SharpDX.Direct3D11.Device( + Direct3D11Device = new SharpDX.Direct3D11.Device( SharpDX.Direct3D.DriverType.Hardware, - SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | - SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport, - featureLevels)) - { - DxgiDevice = d3dDevice.QueryInterface(); - } + SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport, + featureLevels); + + DxgiDevice = Direct3D11Device.QueryInterface(); using (var device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice)) { diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs index 5711a79c2e..b65e32bbbb 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs @@ -1,19 +1,20 @@ using System; using System.IO; using SharpDX.Direct2D1; -using ImageParameters = SharpDX.WIC.ImageParameters; -using ImagingFactory2 = SharpDX.WIC.ImagingFactory2; -using PngBitmapEncoder = SharpDX.WIC.PngBitmapEncoder; -using WICFactory = SharpDX.WIC.ImagingFactory; namespace Avalonia.Direct2D1.Media { + using SharpDX.WIC; + + using Bitmap = SharpDX.Direct2D1.Bitmap; + using PixelFormat = SharpDX.Direct2D1.PixelFormat; + /// /// A Direct2D Bitmap implementation that uses a GPU memory bitmap as its image. /// public class D2DBitmapImpl : BitmapImpl { - private Bitmap _direct2DBitmap; + private readonly Bitmap _direct2DBitmap; /// /// Initialize a new instance of the class @@ -29,7 +30,7 @@ namespace Avalonia.Direct2D1.Media { _direct2DBitmap = d2DBitmap ?? throw new ArgumentNullException(nameof(d2DBitmap)); } - + public override int PixelWidth => _direct2DBitmap.PixelSize.Width; public override int PixelHeight => _direct2DBitmap.PixelSize.Height; @@ -47,18 +48,12 @@ namespace Avalonia.Direct2D1.Media public override void Save(Stream stream) { using (var encoder = new PngBitmapEncoder(Direct2D1Platform.ImagingFactory, stream)) - using (var frameEncode = new SharpDX.WIC.BitmapFrameEncode(encoder)) - //ToDo: Not supported under Windows 7! - using (var imageEncoder = new SharpDX.WIC.ImageEncoder((ImagingFactory2)Direct2D1Platform.ImagingFactory, null)) + using (var frame = new BitmapFrameEncode(encoder)) + using (var bitmapSource = _direct2DBitmap.QueryInterface()) { - var parameters = new ImageParameters( - new PixelFormat(SharpDX.DXGI.Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied), - _direct2DBitmap.DotsPerInch.Width, - _direct2DBitmap.DotsPerInch.Height, - 0, 0, PixelWidth, PixelHeight); - - imageEncoder.WriteFrame(_direct2DBitmap, frameEncode, parameters); - frameEncode.Commit(); + frame.Initialize(); + frame.WriteSource(bitmapSource); + frame.Commit(); encoder.Commit(); } } diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs index 7ea303345a..3646d9e9e1 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DRenderTargetBitmapImpl.cs @@ -1,4 +1,7 @@ -using Avalonia.Platform; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using Avalonia.Platform; using Avalonia.Rendering; using SharpDX; using SharpDX.Direct2D1; diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs index 075fef5ab2..5ca78ef278 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/WriteableWicBitmapImpl.cs @@ -1,4 +1,7 @@ -using System; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using Avalonia.Platform; using SharpDX.WIC; using PixelFormat = Avalonia.Platform.PixelFormat; From 2a64f133d56ac6d1272bca6cb452e964714b44af Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Fri, 7 Sep 2018 13:45:05 +0200 Subject: [PATCH 06/67] SharpDX update to current version --- build/SharpDX.props | 10 ++--- .../Direct3DInteropSample/MainWindow.cs | 2 +- .../Media/AvaloniaTextRenderer.cs | 40 ++----------------- .../Media/FormattedTextImpl.cs | 15 ++++++- 4 files changed, 24 insertions(+), 43 deletions(-) diff --git a/build/SharpDX.props b/build/SharpDX.props index 69aa817a01..fdc802f135 100644 --- a/build/SharpDX.props +++ b/build/SharpDX.props @@ -1,9 +1,9 @@  - - - - - + + + + + diff --git a/samples/interop/Direct3DInteropSample/MainWindow.cs b/samples/interop/Direct3DInteropSample/MainWindow.cs index 065f1a285a..1ac4b44a74 100644 --- a/samples/interop/Direct3DInteropSample/MainWindow.cs +++ b/samples/interop/Direct3DInteropSample/MainWindow.cs @@ -132,7 +132,7 @@ namespace Direct3DInteropSample signature, inputElements); - // Instantiate Vertex buiffer from vertex data + // Instantiate Vertex buffer from vertex data var vertices = Buffer.Create( device, BindFlags.VertexBuffer, diff --git a/src/Windows/Avalonia.Direct2D1/Media/AvaloniaTextRenderer.cs b/src/Windows/Avalonia.Direct2D1/Media/AvaloniaTextRenderer.cs index 51b4eef62d..6e6cf47254 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/AvaloniaTextRenderer.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/AvaloniaTextRenderer.cs @@ -1,7 +1,6 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. -using System; using SharpDX; using SharpDX.Direct2D1; using SharpDX.DirectWrite; @@ -9,7 +8,7 @@ using SharpDX.Mathematics.Interop; namespace Avalonia.Direct2D1.Media { - internal class AvaloniaTextRenderer : TextRenderer + internal class AvaloniaTextRenderer : TextRendererBase { private readonly DrawingContextImpl _context; @@ -27,18 +26,7 @@ namespace Avalonia.Direct2D1.Media _foreground = foreground; } - public IDisposable Shadow - { - get; - set; - } - - public void Dispose() - { - Shadow?.Dispose(); - } - - public Result DrawGlyphRun( + public override Result DrawGlyphRun( object clientDrawingContext, float baselineOriginX, float baselineOriginY, @@ -68,34 +56,14 @@ namespace Avalonia.Direct2D1.Media return Result.Ok; } - public Result DrawInlineObject(object clientDrawingContext, float originX, float originY, InlineObject inlineObject, bool isSideways, bool isRightToLeft, ComObject clientDrawingEffect) - { - throw new NotImplementedException(); - } - - public Result DrawStrikethrough(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref Strikethrough strikethrough, ComObject clientDrawingEffect) - { - throw new NotImplementedException(); - } - - public Result DrawUnderline(object clientDrawingContext, float baselineOriginX, float baselineOriginY, ref Underline underline, ComObject clientDrawingEffect) - { - throw new NotImplementedException(); - } - - public RawMatrix3x2 GetCurrentTransform(object clientDrawingContext) + public override RawMatrix3x2 GetCurrentTransform(object clientDrawingContext) { return _renderTarget.Transform; } - public float GetPixelsPerDip(object clientDrawingContext) + public override float GetPixelsPerDip(object clientDrawingContext) { return _renderTarget.DotsPerInch.Width / 96; } - - public bool IsPixelSnappingDisabled(object clientDrawingContext) - { - return false; - } } } diff --git a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs index 09b249e19f..e2c33a51f1 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs @@ -9,6 +9,8 @@ using DWrite = SharpDX.DirectWrite; namespace Avalonia.Direct2D1.Media { + using System; + public class FormattedTextImpl : IFormattedTextImpl { public FormattedTextImpl( @@ -107,7 +109,18 @@ namespace Avalonia.Direct2D1.Media private Size Measure() { - var metrics = TextLayout.Metrics; + DWrite.TextMetrics metrics; + + // SharpDX bug + try + { + metrics = TextLayout.Metrics; + } + catch (ObjectDisposedException) + { + metrics = TextLayout.Metrics; + } + var width = metrics.WidthIncludingTrailingWhitespace; if (float.IsNaN(width)) From d531b36b9631e069424f98f22057f00584311b9c Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 8 Sep 2018 19:55:36 +0200 Subject: [PATCH 07/67] Make brushes raise a Changed event. When their visual representation changes. --- src/Avalonia.Visuals/Media/Brush.cs | 36 +++++++- src/Avalonia.Visuals/Media/GradientBrush.cs | 65 +++++++++++--- src/Avalonia.Visuals/Media/GradientStop.cs | 40 ++++++--- src/Avalonia.Visuals/Media/GradientStops.cs | 23 +++++ src/Avalonia.Visuals/Media/IGradientBrush.cs | 4 +- src/Avalonia.Visuals/Media/IGradientStop.cs | 18 ++++ src/Avalonia.Visuals/Media/IMutableBrush.cs | 9 +- src/Avalonia.Visuals/Media/ImageBrush.cs | 12 ++- .../Media/Immutable/ImmutableGradientBrush.cs | 9 +- .../Media/Immutable/ImmutableGradientStop.cs | 20 +++++ .../Immutable/ImmutableLinearGradientBrush.cs | 4 +- .../Immutable/ImmutableRadialGradientBrush.cs | 4 +- .../Media/LinearGradientBrush.cs | 13 ++- .../Media/RadialGradientBrush.cs | 10 ++- src/Avalonia.Visuals/Media/SolidColorBrush.cs | 13 ++- src/Avalonia.Visuals/Media/TileBrush.cs | 7 ++ src/Avalonia.Visuals/Media/VisualBrush.cs | 12 ++- .../Controls/CustomRenderTests.cs | 2 +- .../Media/LinearGradientBrushTests.cs | 4 +- .../Media/RadialGradientBrushTests.cs | 2 +- .../Avalonia.RenderTests/OpacityMaskTests.cs | 4 +- .../Media/ImageBrushTests.cs | 27 ++++++ .../Media/LinearGradientBrushTests.cs | 86 +++++++++++++++++++ .../Media/SolidColorBrushTests.cs | 21 +++++ 24 files changed, 386 insertions(+), 59 deletions(-) create mode 100644 src/Avalonia.Visuals/Media/GradientStops.cs create mode 100644 src/Avalonia.Visuals/Media/IGradientStop.cs create mode 100644 src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs create mode 100644 tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs diff --git a/src/Avalonia.Visuals/Media/Brush.cs b/src/Avalonia.Visuals/Media/Brush.cs index eef6e1a43c..8ba7c1be04 100644 --- a/src/Avalonia.Visuals/Media/Brush.cs +++ b/src/Avalonia.Visuals/Media/Brush.cs @@ -10,7 +10,7 @@ namespace Avalonia.Media /// Describes how an area is painted. /// [TypeConverter(typeof(BrushConverter))] - public abstract class Brush : AvaloniaObject, IBrush + public abstract class Brush : AvaloniaObject, IMutableBrush { /// /// Defines the property. @@ -18,6 +18,9 @@ namespace Avalonia.Media public static readonly StyledProperty OpacityProperty = AvaloniaProperty.Register(nameof(Opacity), 1.0); + /// + public event EventHandler Changed; + /// /// Gets or sets the opacity of the brush. /// @@ -50,5 +53,36 @@ namespace Avalonia.Media throw new FormatException($"Invalid brush string: '{s}'."); } + + /// + public abstract IBrush ToImmutable(); + + /// + /// Marks a property as affecting the brush's visual representation. + /// + /// The properties. + /// + /// After a call to this method in a brush's static constructor, any change to the + /// property will cause the event to be raised on the brush. + /// + protected static void AffectsRender(params AvaloniaProperty[] properties) + where T : Brush + { + void Invalidate(AvaloniaPropertyChangedEventArgs e) + { + (e.Sender as T)?.RaiseChanged(EventArgs.Empty); + } + + foreach (var property in properties) + { + property.Changed.Subscribe(Invalidate); + } + } + + /// + /// Raises the event. + /// + /// The event args. + protected void RaiseChanged(EventArgs e) => Changed?.Invoke(this, e); } } diff --git a/src/Avalonia.Visuals/Media/GradientBrush.cs b/src/Avalonia.Visuals/Media/GradientBrush.cs index 41c3afc8c3..c123813cee 100644 --- a/src/Avalonia.Visuals/Media/GradientBrush.cs +++ b/src/Avalonia.Visuals/Media/GradientBrush.cs @@ -1,7 +1,11 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using System; using System.Collections.Generic; +using System.Collections.Specialized; +using System.ComponentModel; +using Avalonia.Collections; using Avalonia.Metadata; namespace Avalonia.Media @@ -20,35 +24,74 @@ namespace Avalonia.Media /// /// Defines the property. /// - public static readonly StyledProperty> GradientStopsProperty = - AvaloniaProperty.Register>(nameof(GradientStops)); + public static readonly StyledProperty GradientStopsProperty = + AvaloniaProperty.Register(nameof(GradientStops)); + + private IDisposable _gradientStopsSubscription; + + static GradientBrush() + { + GradientStopsProperty.Changed.Subscribe(GradientStopsChanged); + AffectsRender(SpreadMethodProperty); + } /// /// Initializes a new instance of the class. /// public GradientBrush() { - this.GradientStops = new List(); + this.GradientStops = new GradientStops(); } - /// - /// Gets or sets the brush's spread method that defines how to draw a gradient that - /// doesn't fill the bounds of the destination control. - /// + /// public GradientSpreadMethod SpreadMethod { get { return GetValue(SpreadMethodProperty); } set { SetValue(SpreadMethodProperty, value); } } - /// - /// Gets or sets the brush's gradient stops. - /// + /// [Content] - public IList GradientStops + public GradientStops GradientStops { get { return GetValue(GradientStopsProperty); } set { SetValue(GradientStopsProperty, value); } } + + /// + IReadOnlyList IGradientBrush.GradientStops => GradientStops; + + private static void GradientStopsChanged(AvaloniaPropertyChangedEventArgs e) + { + if (e.Sender is GradientBrush brush) + { + var oldValue = (GradientStops)e.OldValue; + var newValue = (GradientStops)e.NewValue; + + if (oldValue != null) + { + oldValue.CollectionChanged -= brush.GradientStopsChanged; + brush._gradientStopsSubscription.Dispose(); + } + + if (newValue != null) + { + newValue.CollectionChanged += brush.GradientStopsChanged; + brush._gradientStopsSubscription = newValue.TrackItemPropertyChanged(brush.GradientStopChanged); + } + + brush.RaiseChanged(EventArgs.Empty); + } + } + + private void GradientStopsChanged(object sender, NotifyCollectionChangedEventArgs e) + { + RaiseChanged(EventArgs.Empty); + } + + private void GradientStopChanged(Tuple e) + { + RaiseChanged(EventArgs.Empty); + } } } diff --git a/src/Avalonia.Visuals/Media/GradientStop.cs b/src/Avalonia.Visuals/Media/GradientStop.cs index 78dd32a18a..00d96a0b3c 100644 --- a/src/Avalonia.Visuals/Media/GradientStop.cs +++ b/src/Avalonia.Visuals/Media/GradientStop.cs @@ -4,10 +4,22 @@ namespace Avalonia.Media { /// - /// GradientStop + /// Describes the location and color of a transition point in a gradient. /// - public sealed class GradientStop + public sealed class GradientStop : AvaloniaObject, IGradientStop { + /// + /// Describes the property. + /// + public static StyledProperty OffsetProperty = + AvaloniaProperty.Register(nameof(Offset)); + + /// + /// Describes the property. + /// + public static StyledProperty ColorProperty = + AvaloniaProperty.Register(nameof(Color)); + /// /// Initializes a new instance of the class. /// @@ -24,16 +36,18 @@ namespace Avalonia.Media Offset = offset; } - // TODO: Make these dependency properties. - - /// - /// The offset - /// - public double Offset { get; set; } + /// + public double Offset + { + get => GetValue(OffsetProperty); + set => SetValue(OffsetProperty, value); + } - /// - /// The color - /// - public Color Color { get; set; } + /// + public Color Color + { + get => GetValue(ColorProperty); + set => SetValue(ColorProperty, value); + } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/GradientStops.cs b/src/Avalonia.Visuals/Media/GradientStops.cs new file mode 100644 index 0000000000..efc11bacd6 --- /dev/null +++ b/src/Avalonia.Visuals/Media/GradientStops.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; +using System.Linq; +using Avalonia.Collections; +using Avalonia.Media.Immutable; + +namespace Avalonia.Media +{ + /// + /// A collection of s. + /// + public class GradientStops : AvaloniaList + { + public GradientStops() + { + ResetBehavior = ResetBehavior.Remove; + } + + public IReadOnlyList ToImmutable() + { + return this.Select(x => new ImmutableGradientStop(x.Offset, x.Color)).ToList(); + } + } +} diff --git a/src/Avalonia.Visuals/Media/IGradientBrush.cs b/src/Avalonia.Visuals/Media/IGradientBrush.cs index 390ce6ee5b..18db0af660 100644 --- a/src/Avalonia.Visuals/Media/IGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/IGradientBrush.cs @@ -10,7 +10,7 @@ namespace Avalonia.Media /// /// Gets the brush's gradient stops. /// - IList GradientStops { get; } + IReadOnlyList GradientStops { get; } /// /// Gets the brush's spread method that defines how to draw a gradient that doesn't fill @@ -18,4 +18,4 @@ namespace Avalonia.Media /// GradientSpreadMethod SpreadMethod { get; } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/IGradientStop.cs b/src/Avalonia.Visuals/Media/IGradientStop.cs new file mode 100644 index 0000000000..22eb9df60d --- /dev/null +++ b/src/Avalonia.Visuals/Media/IGradientStop.cs @@ -0,0 +1,18 @@ +namespace Avalonia.Media +{ + /// + /// Describes the location and color of a transition point in a gradient. + /// + public interface IGradientStop + { + /// + /// Gets the gradient stop color. + /// + Color Color { get; } + + /// + /// Gets the gradient stop offset. + /// + double Offset { get; } + } +} diff --git a/src/Avalonia.Visuals/Media/IMutableBrush.cs b/src/Avalonia.Visuals/Media/IMutableBrush.cs index 39dd8b80c4..762731a6a8 100644 --- a/src/Avalonia.Visuals/Media/IMutableBrush.cs +++ b/src/Avalonia.Visuals/Media/IMutableBrush.cs @@ -1,10 +1,17 @@ -namespace Avalonia.Media +using System; + +namespace Avalonia.Media { /// /// Represents a mutable brush which can return an immutable clone of itself. /// public interface IMutableBrush : IBrush { + /// + /// Raised when the brush changes visually. + /// + event EventHandler Changed; + /// /// Creates an immutable clone of the brush. /// diff --git a/src/Avalonia.Visuals/Media/ImageBrush.cs b/src/Avalonia.Visuals/Media/ImageBrush.cs index fa491ed3e1..8b42a51d9f 100644 --- a/src/Avalonia.Visuals/Media/ImageBrush.cs +++ b/src/Avalonia.Visuals/Media/ImageBrush.cs @@ -2,13 +2,14 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Media.Imaging; +using Avalonia.Media.Immutable; namespace Avalonia.Media { /// /// Paints an area with an . /// - public class ImageBrush : TileBrush, IImageBrush, IMutableBrush + public class ImageBrush : TileBrush, IImageBrush { /// /// Defines the property. @@ -16,6 +17,11 @@ namespace Avalonia.Media public static readonly StyledProperty SourceProperty = AvaloniaProperty.Register(nameof(Source)); + static ImageBrush() + { + AffectsRender(SourceProperty); + } + /// /// Initializes a new instance of the class. /// @@ -42,9 +48,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableImageBrush(this); + return new ImmutableImageBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs index 6664a2b30e..1f6e3bbcfd 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientBrush.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; namespace Avalonia.Media.Immutable { @@ -15,7 +14,7 @@ namespace Avalonia.Media.Immutable /// The opacity of the brush. /// The spread method. protected ImmutableGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity, GradientSpreadMethod spreadMethod) { @@ -28,14 +27,14 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - protected ImmutableGradientBrush(IGradientBrush source) - : this(source.GradientStops.ToList(), source.Opacity, source.SpreadMethod) + protected ImmutableGradientBrush(GradientBrush source) + : this(source.GradientStops.ToImmutable(), source.Opacity, source.SpreadMethod) { } /// - public IList GradientStops { get; } + public IReadOnlyList GradientStops { get; } /// public double Opacity { get; } diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs new file mode 100644 index 0000000000..f3e2e52fd0 --- /dev/null +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableGradientStop.cs @@ -0,0 +1,20 @@ +namespace Avalonia.Media.Immutable +{ + /// + /// Describes the location and color of a transition point in a gradient. + /// + public class ImmutableGradientStop : IGradientStop + { + public ImmutableGradientStop(double offset, Color color) + { + Offset = offset; + Color = color; + } + + /// + public double Offset { get; } + + /// + public Color Color { get; } + } +} diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs index 142eb34625..912d77d763 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableLinearGradientBrush.cs @@ -16,7 +16,7 @@ namespace Avalonia.Media.Immutable /// The start point for the gradient. /// The end point for the gradient. public ImmutableLinearGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity = 1, GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad, RelativePoint? startPoint = null, @@ -31,7 +31,7 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - public ImmutableLinearGradientBrush(ILinearGradientBrush source) + public ImmutableLinearGradientBrush(LinearGradientBrush source) : base(source) { StartPoint = source.StartPoint; diff --git a/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs b/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs index f36a1cd2de..e26fbab5f5 100644 --- a/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/Immutable/ImmutableRadialGradientBrush.cs @@ -21,7 +21,7 @@ namespace Avalonia.Media.Immutable /// The horizontal and vertical radius of the outermost circle of the radial gradient. /// public ImmutableRadialGradientBrush( - IList gradientStops, + IReadOnlyList gradientStops, double opacity = 1, GradientSpreadMethod spreadMethod = GradientSpreadMethod.Pad, RelativePoint? center = null, @@ -38,7 +38,7 @@ namespace Avalonia.Media.Immutable /// Initializes a new instance of the class. /// /// The brush from which this brush's properties should be copied. - public ImmutableRadialGradientBrush(IRadialGradientBrush source) + public ImmutableRadialGradientBrush(RadialGradientBrush source) : base(source) { Center = source.Center; diff --git a/src/Avalonia.Visuals/Media/LinearGradientBrush.cs b/src/Avalonia.Visuals/Media/LinearGradientBrush.cs index d092bebf0f..14adc0e0cd 100644 --- a/src/Avalonia.Visuals/Media/LinearGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/LinearGradientBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// A brush that draws with a linear gradient. /// - public sealed class LinearGradientBrush : GradientBrush, ILinearGradientBrush, IMutableBrush + public sealed class LinearGradientBrush : GradientBrush, ILinearGradientBrush { /// /// Defines the property. @@ -24,6 +26,11 @@ namespace Avalonia.Media nameof(EndPoint), RelativePoint.BottomRight); + static LinearGradientBrush() + { + AffectsRender(StartPointProperty, EndPointProperty); + } + /// /// Gets or sets the start point for the gradient. /// @@ -43,9 +50,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableLinearGradientBrush(this); + return new ImmutableLinearGradientBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/RadialGradientBrush.cs b/src/Avalonia.Visuals/Media/RadialGradientBrush.cs index 003e2e05f9..589cd83ca1 100644 --- a/src/Avalonia.Visuals/Media/RadialGradientBrush.cs +++ b/src/Avalonia.Visuals/Media/RadialGradientBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// Paints an area with a radial gradient. /// - public sealed class RadialGradientBrush : GradientBrush, IRadialGradientBrush, IMutableBrush + public sealed class RadialGradientBrush : GradientBrush, IRadialGradientBrush { /// /// Defines the property. @@ -63,9 +65,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableRadialGradientBrush(this); + return new ImmutableRadialGradientBrush(this); } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/SolidColorBrush.cs b/src/Avalonia.Visuals/Media/SolidColorBrush.cs index d84e407cb4..32b87df56b 100644 --- a/src/Avalonia.Visuals/Media/SolidColorBrush.cs +++ b/src/Avalonia.Visuals/Media/SolidColorBrush.cs @@ -1,12 +1,14 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; + namespace Avalonia.Media { /// /// Fills an area with a solid color. /// - public class SolidColorBrush : Brush, ISolidColorBrush, IMutableBrush + public class SolidColorBrush : Brush, ISolidColorBrush { /// /// Defines the property. @@ -14,6 +16,11 @@ namespace Avalonia.Media public static readonly StyledProperty ColorProperty = AvaloniaProperty.Register(nameof(Color)); + static SolidColorBrush() + { + AffectsRender(ColorProperty); + } + /// /// Initializes a new instance of the class. /// @@ -75,9 +82,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableSolidColorBrush(this); + return new ImmutableSolidColorBrush(this); } } } diff --git a/src/Avalonia.Visuals/Media/TileBrush.cs b/src/Avalonia.Visuals/Media/TileBrush.cs index 2033754137..47f20fa285 100644 --- a/src/Avalonia.Visuals/Media/TileBrush.cs +++ b/src/Avalonia.Visuals/Media/TileBrush.cs @@ -79,6 +79,13 @@ namespace Avalonia.Media static TileBrush() { + AffectsRender( + AlignmentXProperty, + AlignmentYProperty, + DestinationRectProperty, + SourceRectProperty, + StretchProperty, + TileModeProperty); RenderOptions.BitmapInterpolationModeProperty.OverrideDefaultValue(BitmapInterpolationMode.Default); } diff --git a/src/Avalonia.Visuals/Media/VisualBrush.cs b/src/Avalonia.Visuals/Media/VisualBrush.cs index 435f4ba1b1..963ba8f4a1 100644 --- a/src/Avalonia.Visuals/Media/VisualBrush.cs +++ b/src/Avalonia.Visuals/Media/VisualBrush.cs @@ -1,6 +1,7 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media.Immutable; using Avalonia.VisualTree; namespace Avalonia.Media @@ -8,7 +9,7 @@ namespace Avalonia.Media /// /// Paints an area with an . /// - public class VisualBrush : TileBrush, IVisualBrush, IMutableBrush + public class VisualBrush : TileBrush, IVisualBrush { /// /// Defines the property. @@ -16,6 +17,11 @@ namespace Avalonia.Media public static readonly StyledProperty VisualProperty = AvaloniaProperty.Register(nameof(Visual)); + static VisualBrush() + { + AffectsRender(VisualProperty); + } + /// /// Initializes a new instance of the class. /// @@ -42,9 +48,9 @@ namespace Avalonia.Media } /// - IBrush IMutableBrush.ToImmutable() + public override IBrush ToImmutable() { - return new Immutable.ImmutableVisualBrush(this); + return new ImmutableVisualBrush(this); } } } diff --git a/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs b/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs index 8356e78cc3..6a01536b12 100644 --- a/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs +++ b/tests/Avalonia.RenderTests/Controls/CustomRenderTests.cs @@ -124,7 +124,7 @@ namespace Avalonia.Direct2D1.RenderTests.Controls { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) diff --git a/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs index 656e77fc31..a0d6f1e423 100644 --- a/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs +++ b/tests/Avalonia.RenderTests/Media/LinearGradientBrushTests.cs @@ -36,7 +36,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { StartPoint = new RelativePoint(0, 0.5, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 0.5, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } @@ -63,7 +63,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative), - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } diff --git a/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs b/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs index 0017feb106..bd1d26ce70 100644 --- a/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs +++ b/tests/Avalonia.RenderTests/Media/RadialGradientBrushTests.cs @@ -34,7 +34,7 @@ namespace Avalonia.Direct2D1.RenderTests.Media { Background = new RadialGradientBrush { - GradientStops = new[] + GradientStops = { new GradientStop { Color = Colors.Red, Offset = 0 }, new GradientStop { Color = Colors.Blue, Offset = 1 } diff --git a/tests/Avalonia.RenderTests/OpacityMaskTests.cs b/tests/Avalonia.RenderTests/OpacityMaskTests.cs index 4edf4daa13..2f01b03db6 100644 --- a/tests/Avalonia.RenderTests/OpacityMaskTests.cs +++ b/tests/Avalonia.RenderTests/OpacityMaskTests.cs @@ -29,7 +29,7 @@ namespace Avalonia.Direct2D1.RenderTests { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new List + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) @@ -65,7 +65,7 @@ namespace Avalonia.Direct2D1.RenderTests { StartPoint = new RelativePoint(0, 0, RelativeUnit.Relative), EndPoint = new RelativePoint(1, 1, RelativeUnit.Relative), - GradientStops = new List + GradientStops = { new GradientStop(Color.FromUInt32(0xffffffff), 0), new GradientStop(Color.FromUInt32(0x00ffffff), 1) diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs new file mode 100644 index 0000000000..f843a6e333 --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class ImageBrushTests + { + [Fact] + public void Changing_Source_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new ImageBrush(bitmap1); + var raised = false; + + target.Changed += (s, e) => raised = true; + target.Source = bitmap2; + + Assert.True(raised); + } + } +} diff --git a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs new file mode 100644 index 0000000000..62f53108e6 --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs @@ -0,0 +1,86 @@ +using System; +using Avalonia.Media; +using Avalonia.Media.Imaging; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class LinearGradientBrushTests + { + [Fact] + public void Changing_StartPoint_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.StartPoint = new RelativePoint(); + target.Changed += (s, e) => raised = true; + target.StartPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); + + Assert.True(raised); + } + + [Fact] + public void Changing_EndPoint_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.EndPoint = new RelativePoint(); + target.Changed += (s, e) => raised = true; + target.EndPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); + + Assert.True(raised); + } + + [Fact] + public void Changing_GradientStops_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops = new GradientStops { new GradientStop(Colors.Green, 0) }; + + Assert.True(raised); + } + + [Fact] + public void Adding_GradientStop_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops.Add(new GradientStop(Colors.Green, 1)); + + Assert.True(raised); + } + + [Fact] + public void Changing_GradientStop_Offset_Raises_Changed() + { + var bitmap1 = Mock.Of(); + var bitmap2 = Mock.Of(); + var target = new LinearGradientBrush(); + var raised = false; + + target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; + target.Changed += (s, e) => raised = true; + target.GradientStops[0].Offset = 0.5; + + Assert.True(raised); + } + } +} diff --git a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs new file mode 100644 index 0000000000..4e87b7081d --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs @@ -0,0 +1,21 @@ +using System; +using Avalonia.Media; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class SolidColorBrushTests + { + [Fact] + public void Changing_Color_Raises_Changed() + { + var target = new SolidColorBrush(Colors.Red); + var raised = false; + + target.Changed += (s, e) => raised = true; + target.Color = Colors.Green; + + Assert.True(raised); + } + } +} From 0b4e6b847170d34184288bb031d294b62a471a1b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 10:04:49 +0200 Subject: [PATCH 08/67] Make centralized RenderLoop. - Renamed `RenderLoop` to `RenderTimer` - Added new `RenderLoop` which `DeferredRenderer`s register themselves with for updates --- .../InternalPlatformThreadingInterface.cs | 2 +- src/Avalonia.Controls/TopLevel.cs | 2 +- .../Remote/PreviewerWindowingPlatform.cs | 3 +- src/Avalonia.Visuals/Avalonia.Visuals.csproj | 1 + ...ultRenderLoop.cs => DefaultRenderTimer.cs} | 9 +- .../Rendering/DeferredRenderer.cs | 44 +++---- src/Avalonia.Visuals/Rendering/IRenderLoop.cs | 17 +-- .../Rendering/IRenderLoopTask.cs | 12 ++ .../Rendering/IRenderTimer.cs | 20 ++++ src/Avalonia.Visuals/Rendering/RenderLoop.cs | 111 ++++++++++++++++++ src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs | 3 +- .../LinuxFramebufferPlatform.cs | 3 +- src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs | 5 +- .../{RenderLoop.cs => RenderTimer.cs} | 4 +- .../{RenderLoop.cs => RenderTimer.cs} | 4 +- src/Windows/Avalonia.Win32/Win32Platform.cs | 3 +- tests/Avalonia.UnitTests/TestServices.cs | 4 +- .../Rendering/DeferredRendererTests.cs | 6 +- 18 files changed, 191 insertions(+), 62 deletions(-) rename src/Avalonia.Visuals/Rendering/{DefaultRenderLoop.cs => DefaultRenderTimer.cs} (92%) create mode 100644 src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs create mode 100644 src/Avalonia.Visuals/Rendering/IRenderTimer.cs create mode 100644 src/Avalonia.Visuals/Rendering/RenderLoop.cs rename src/OSX/Avalonia.MonoMac/{RenderLoop.cs => RenderTimer.cs} (91%) rename src/Windows/Avalonia.Win32/{RenderLoop.cs => RenderTimer.cs} (88%) diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 400bf5ccc3..47e600b9c8 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -9,7 +9,7 @@ using Avalonia.Threading; namespace Avalonia.Controls.Platform { - public class InternalPlatformThreadingInterface : IPlatformThreadingInterface, IRenderLoop + public class InternalPlatformThreadingInterface : IPlatformThreadingInterface, IRenderTimer { public InternalPlatformThreadingInterface() { diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 1161ded25f..fb5b932fd8 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -96,7 +96,7 @@ namespace Avalonia.Controls _applicationLifecycle = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); - var renderLoop = TryGetService(dependencyResolver); + var renderLoop = TryGetService(dependencyResolver); Renderer = impl.CreateRenderer(this); impl.SetInputRoot(this); diff --git a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs index 01998052d9..20acc30118 100644 --- a/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs +++ b/src/Avalonia.DesignerSupport/Remote/PreviewerWindowingPlatform.cs @@ -53,7 +53,8 @@ namespace Avalonia.DesignerSupport.Remote .Bind().ToConstant(Keyboard) .Bind().ToConstant(instance) .Bind().ToConstant(threading) - .Bind().ToConstant(threading) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(threading) .Bind().ToSingleton() .Bind().ToConstant(instance) .Bind().ToSingleton(); diff --git a/src/Avalonia.Visuals/Avalonia.Visuals.csproj b/src/Avalonia.Visuals/Avalonia.Visuals.csproj index c34752a3ef..c88001cc0a 100644 --- a/src/Avalonia.Visuals/Avalonia.Visuals.csproj +++ b/src/Avalonia.Visuals/Avalonia.Visuals.csproj @@ -1,6 +1,7 @@  netstandard2.0 + Avalonia diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs similarity index 92% rename from src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs rename to src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 9cf849f59b..7ad8915bc4 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -2,18 +2,19 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Threading.Tasks; using Avalonia.Platform; namespace Avalonia.Rendering { /// - /// Defines a default render loop that uses a standard timer. + /// Defines a default render timer that uses a standard timer. /// /// /// This class may be overridden by platform implementations to use a specialized timer /// implementation. /// - public class DefaultRenderLoop : IRenderLoop + public class DefaultRenderTimer : IRenderTimer { private IRuntimePlatform _runtime; private int _subscriberCount; @@ -21,12 +22,12 @@ namespace Avalonia.Rendering private IDisposable _subscription; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// /// The number of frames per second at which the loop should run. /// - public DefaultRenderLoop(int framesPerSecond) + public DefaultRenderTimer(int framesPerSecond) { FramesPerSecond = framesPerSecond; } diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index dc1d2933d0..f937964994 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -13,6 +13,7 @@ using Avalonia.Rendering.SceneGraph; using Avalonia.Threading; using Avalonia.Utilities; using Avalonia.VisualTree; +using System.Threading.Tasks; namespace Avalonia.Rendering { @@ -20,7 +21,7 @@ namespace Avalonia.Rendering /// A renderer which renders the state of the visual tree to an intermediate scene graph /// representation which is then rendered on a rendering thread. /// - public class DeferredRenderer : RendererBase, IRenderer, IVisualBrushRenderer + public class DeferredRenderer : RendererBase, IRenderer, IRenderLoopTask, IVisualBrushRenderer { private readonly IDispatcher _dispatcher; private readonly IRenderLoop _renderLoop; @@ -149,7 +150,7 @@ namespace Avalonia.Rendering { if (!_running && _renderLoop != null) { - _renderLoop.Tick += OnRenderLoopTick; + _renderLoop.Add(this); _running = true; } } @@ -159,11 +160,23 @@ namespace Avalonia.Rendering { if (_running && _renderLoop != null) { - _renderLoop.Tick -= OnRenderLoopTick; + _renderLoop.Remove(this); _running = false; } } + bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; + + void IRenderLoopTask.Update() => UpdateScene(); + + void IRenderLoopTask.Render() + { + using (var scene = _scene?.Clone()) + { + Render(scene?.Item); + } + } + /// Size IVisualBrushRenderer.GetRenderTargetSize(IVisualBrush brush) { @@ -420,31 +433,6 @@ namespace Avalonia.Rendering } } - private void OnRenderLoopTick(object sender, EventArgs e) - { - if (Monitor.TryEnter(_rendering)) - { - try - { - if (!_updateQueued && (_dirty == null || _dirty.Count > 0)) - { - _updateQueued = true; - _dispatcher.Post(UpdateScene, DispatcherPriority.Render); - } - - using (var scene = _scene?.Clone()) - { - Render(scene?.Item); - } - } - catch { } - finally - { - Monitor.Exit(_rendering); - } - } - } - private IRef GetOverlay( IDrawingContextImpl parentContext, Size size, diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs index 36d915ddbd..bd1086d178 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs @@ -1,19 +1,8 @@ -using System; - -namespace Avalonia.Rendering +namespace Avalonia.Rendering { - /// - /// Defines the interface implemented by an application render loop. - /// public interface IRenderLoop { - /// - /// Raised when the render loop ticks to signal a new frame should be drawn. - /// - /// - /// This event can be raised on any thread; it is the responsibility of the subscriber to - /// switch execution to the right thread. - /// - event EventHandler Tick; + void Add(IRenderLoopTask i); + void Remove(IRenderLoopTask i); } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs new file mode 100644 index 0000000000..2f251a5c17 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace Avalonia.Rendering +{ + public interface IRenderLoopTask + { + bool NeedsUpdate { get; } + void Update(); + void Render(); + } +} diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs new file mode 100644 index 0000000000..2665d6dd0b --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; + +namespace Avalonia.Rendering +{ + /// + /// Defines the interface implemented by an application render timer. + /// + public interface IRenderTimer + { + /// + /// Raised when the render timer ticks to signal a new frame should be drawn. + /// + /// + /// This event can be raised on any thread; it is the responsibility of the subscriber to + /// switch execution to the right thread. + /// + event EventHandler Tick; + } +} \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs new file mode 100644 index 0000000000..25febf8187 --- /dev/null +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using Avalonia.Logging; +using Avalonia.Threading; + +namespace Avalonia.Rendering +{ + public class RenderLoop : IRenderLoop + { + private readonly IDispatcher _dispatcher; + private List _items = new List(); + private IRenderTimer _timer; + private volatile bool inTick; + + public RenderLoop() + { + _dispatcher = Dispatcher.UIThread; + } + + public RenderLoop(IRenderTimer timer, IDispatcher dispatcher) + { + _timer = timer; + _dispatcher = dispatcher; + } + + protected IRenderTimer Timer + { + get + { + if (_timer == null) + { + _timer = AvaloniaLocator.Current.GetService(); + } + + return _timer; + } + } + + public void Add(IRenderLoopTask i) + { + Contract.Requires(i != null); + Dispatcher.UIThread.VerifyAccess(); + + _items.Add(i); + + if (_items.Count == 1) + { + Timer.Tick += TimerTick; + } + } + + public void Remove(IRenderLoopTask i) + { + Contract.Requires(i != null); + Dispatcher.UIThread.VerifyAccess(); + + _items.Remove(i); + + if (_items.Count == 0) + { + Timer.Tick -= TimerTick; + } + } + + private async void TimerTick(object sender, EventArgs e) + { + if (!inTick) + { + inTick = true; + + try + { + var needsUpdate = false; + + foreach (var i in _items) + { + if (i.NeedsUpdate) + { + needsUpdate = true; + break; + } + } + + if (needsUpdate) + { + await _dispatcher.InvokeAsync(() => + { + foreach (var i in _items) + { + i.Update(); + } + }); + } + + foreach (var i in _items) + { + i.Render(); + } + } + catch (Exception ex) + { + Logger.Error(LogArea.Visual, this, "Exception in render loop: {Error}", ex); + } + finally + { + inTick = false; + } + } + } + } +} diff --git a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs index ca8a1ad3a4..bbb6a01c05 100644 --- a/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs +++ b/src/Gtk/Avalonia.Gtk3/Gtk3Platform.cs @@ -52,7 +52,8 @@ namespace Avalonia.Gtk3 .Bind().ToConstant(Instance) .Bind().ToConstant(Instance) .Bind().ToSingleton() - .Bind().ToConstant(new DefaultRenderLoop(60)) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(new DefaultRenderTimer(60)) .Bind().ToConstant(new PlatformIconLoader()); } diff --git a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs index 810be77b2b..9046c26cee 100644 --- a/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs +++ b/src/Linux/Avalonia.LinuxFramebuffer/LinuxFramebufferPlatform.cs @@ -35,7 +35,8 @@ namespace Avalonia.LinuxFramebuffer .Bind().ToConstant(KeyboardDevice) .Bind().ToSingleton() .Bind().ToConstant(Threading) - .Bind().ToConstant(Threading); + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(Threading); } internal static TopLevel Initialize(T builder, string fbdev = null) where T : AppBuilderBase, new() diff --git a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs index ba45ad8403..5dbf18b1de 100644 --- a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs +++ b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs @@ -21,6 +21,7 @@ namespace Avalonia.MonoMac private static bool s_monoMacInitialized; private static bool s_showInDock = true; private static IRenderLoop s_renderLoop; + private static IRenderTimer s_renderTimer; void DoInitialize() { @@ -35,6 +36,7 @@ namespace Avalonia.MonoMac .Bind().ToSingleton() .Bind().ToSingleton() .Bind().ToConstant(s_renderLoop) + .Bind().ToConstant(s_renderTimer) .Bind().ToConstant(PlatformThreadingInterface.Instance) /*.Bind().ToTransient()*/; } @@ -83,7 +85,8 @@ namespace Avalonia.MonoMac ThreadHelper.InitializeCocoaThreadingLocks(); App = NSApplication.SharedApplication; UpdateActivationPolicy(); - s_renderLoop = new RenderLoop(); //TODO: use CVDisplayLink + s_renderLoop = new RenderLoop(); + s_renderTimer = new RenderTimer(); //TODO: use CVDisplayLink s_monoMacInitialized = true; } diff --git a/src/OSX/Avalonia.MonoMac/RenderLoop.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs similarity index 91% rename from src/OSX/Avalonia.MonoMac/RenderLoop.cs rename to src/OSX/Avalonia.MonoMac/RenderTimer.cs index 4d1f9b4201..d2fd50484a 100644 --- a/src/OSX/Avalonia.MonoMac/RenderLoop.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -6,12 +6,12 @@ using MonoMac.Foundation; namespace Avalonia.MonoMac { //TODO: Switch to using CVDisplayLink - public class RenderLoop : IRenderLoop + public class RenderTimer : IRenderTimer { private readonly object _lock = new object(); private readonly IDisposable _timer; - public RenderLoop() + public RenderTimer() { _timer = AvaloniaLocator.Current.GetService().StartSystemTimer(new TimeSpan(0, 0, 0, 0, 1000 / 60), () => diff --git a/src/Windows/Avalonia.Win32/RenderLoop.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs similarity index 88% rename from src/Windows/Avalonia.Win32/RenderLoop.cs rename to src/Windows/Avalonia.Win32/RenderTimer.cs index 7d7befcc33..321a745fae 100644 --- a/src/Windows/Avalonia.Win32/RenderLoop.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -5,11 +5,11 @@ using Avalonia.Win32.Interop; namespace Avalonia.Win32 { - internal class RenderLoop : DefaultRenderLoop + internal class RenderTimer : DefaultRenderTimer { private UnmanagedMethods.TimeCallback timerDelegate; - public RenderLoop(int framesPerSecond) + public RenderTimer(int framesPerSecond) : base(framesPerSecond) { } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 9afb1218af..d5eb97292a 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -82,7 +82,8 @@ namespace Avalonia.Win32 .Bind().ToConstant(WindowsKeyboardDevice.Instance) .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance) - .Bind().ToConstant(new RenderLoop(60)) + .Bind().ToConstant(new RenderLoop()) + .Bind().ToConstant(new RenderTimer(60)) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); diff --git a/tests/Avalonia.UnitTests/TestServices.cs b/tests/Avalonia.UnitTests/TestServices.cs index d990defe3d..d68f1d167a 100644 --- a/tests/Avalonia.UnitTests/TestServices.cs +++ b/tests/Avalonia.UnitTests/TestServices.cs @@ -64,7 +64,7 @@ namespace Avalonia.UnitTests Func mouseDevice = null, IRuntimePlatform platform = null, IPlatformRenderInterface renderInterface = null, - IRenderLoop renderLoop = null, + IRenderTimer renderLoop = null, IScheduler scheduler = null, IStandardCursorFactory standardCursorFactory = null, IStyler styler = null, @@ -115,7 +115,7 @@ namespace Avalonia.UnitTests Func mouseDevice = null, IRuntimePlatform platform = null, IPlatformRenderInterface renderInterface = null, - IRenderLoop renderLoop = null, + IRenderTimer renderLoop = null, IScheduler scheduler = null, IStandardCursorFactory standardCursorFactory = null, IStyler styler = null, diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index 2350a31d5c..1af9a9499d 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -42,7 +42,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void First_Frame_Calls_SceneBuilder_UpdateAll() { - var loop = new Mock(); + var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -198,7 +198,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Create_Layer_For_Root() { - var loop = new Mock(); + var loop = new Mock(); var root = new TestRoot(); var rootLayer = new Mock(); @@ -374,7 +374,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering private void RunFrame(Mock loop) { - loop.Raise(x => x.Tick += null, EventArgs.Empty); + //loop.Raise(x => x.Tick += null, EventArgs.Empty); } private IRenderTargetBitmapImpl CreateLayer() From 8ec2c8f661839f7826e2e262431c0b7debe4ad0e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 10:36:35 +0200 Subject: [PATCH 09/67] Notify tick count in IRenderTimer.Tick. --- .../Platform/InternalPlatformThreadingInterface.cs | 4 ++-- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 12 ++++++------ src/Avalonia.Visuals/Rendering/IRenderTimer.cs | 2 +- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 2 +- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 4 ++-- .../Avalonia.Win32/Interop/UnmanagedMethods.cs | 3 +++ src/Windows/Avalonia.Win32/RenderTimer.cs | 8 ++++++-- 7 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 47e600b9c8..501e15653a 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -14,7 +14,7 @@ namespace Avalonia.Controls.Platform public InternalPlatformThreadingInterface() { TlsCurrentThreadIsLoopThread = true; - StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(this, new EventArgs())); + StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(Environment.TickCount)); } private readonly AutoResetEvent _signaled = new AutoResetEvent(false); @@ -105,7 +105,7 @@ namespace Avalonia.Controls.Platform public bool CurrentThreadIsLoopThread => TlsCurrentThreadIsLoopThread; public event Action Signaled; - public event EventHandler Tick; + public event Action Tick; } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 7ad8915bc4..6b16ff8038 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -18,7 +18,7 @@ namespace Avalonia.Rendering { private IRuntimePlatform _runtime; private int _subscriberCount; - private EventHandler _tick; + private Action _tick; private IDisposable _subscription; /// @@ -38,7 +38,7 @@ namespace Avalonia.Rendering public int FramesPerSecond { get; } /// - public event EventHandler Tick + public event Action Tick { add { @@ -77,14 +77,14 @@ namespace Avalonia.Rendering /// This can be overridden by platform implementations to use a specialized timer /// implementation. /// - protected virtual IDisposable StartCore(Action tick) + protected virtual IDisposable StartCore(Action tick) { if (_runtime == null) { _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), tick); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); } /// @@ -96,9 +96,9 @@ namespace Avalonia.Rendering _subscription = null; } - private void InternalTick() + private void InternalTick(long tickCount) { - _tick(this, EventArgs.Empty); + _tick(tickCount); } } } diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs index 2665d6dd0b..78f6183994 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -15,6 +15,6 @@ namespace Avalonia.Rendering /// This event can be raised on any thread; it is the responsibility of the subscriber to /// switch execution to the right thread. /// - event EventHandler Tick; + event Action Tick; } } \ No newline at end of file diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 25febf8187..bdcfdcba8e 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -62,7 +62,7 @@ namespace Avalonia.Rendering } } - private async void TimerTick(object sender, EventArgs e) + private async void TimerTick(long tickCount) { if (!inTick) { diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index d2fd50484a..71f657b690 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -20,12 +20,12 @@ namespace Avalonia.MonoMac { using (new NSAutoreleasePool()) { - Tick?.Invoke(this, EventArgs.Empty); + Tick?.Invoke(Environment.TickCount); } } }); } - public event EventHandler Tick; + public event Action Tick; } } diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index f89086ccb7..1c70b3f03d 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -972,6 +972,9 @@ namespace Avalonia.Win32.Interop [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMonitorInfo([In] IntPtr hMonitor, [Out] MONITORINFO lpmi); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); + [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PostMessageW")] public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index 321a745fae..cea1bf94a5 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -14,9 +14,13 @@ namespace Avalonia.Win32 { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { - timerDelegate = (id, uMsg, user, dw1, dw2) => tick(); + timerDelegate = (id, uMsg, user, dw1, dw2) => + { + UnmanagedMethods.QueryPerformanceCounter(out long tickCount); + tick(tickCount); + }; var handle = UnmanagedMethods.timeSetEvent( (uint)(1000 / FramesPerSecond), From 166f9f8cf01d27dd8a9afa040497c357f0e5eb6b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 28 Jun 2018 11:02:50 +0200 Subject: [PATCH 10/67] Pulse animation timer from render loop. --- src/Avalonia.Animation/Timing.cs | 35 +++++++++----------- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 4 ++- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs index 6367425911..d6a353fb34 100644 --- a/src/Avalonia.Animation/Timing.cs +++ b/src/Avalonia.Animation/Timing.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using System.Reactive.Linq; +using Avalonia.Reactive; using Avalonia.Threading; namespace Avalonia.Animation @@ -13,42 +14,38 @@ namespace Avalonia.Animation /// public static class Timing { - /// - /// The number of frames per second. - /// - public const int FramesPerSecond = 60; - - /// - /// The time span of each frame. - /// - internal static readonly TimeSpan FrameTick = TimeSpan.FromSeconds(1.0 / FramesPerSecond); + static TimerObservable _timer = new TimerObservable(); /// /// Initializes static members of the class. /// static Timing() { - var globalTimer = Observable.Interval(FrameTick, AvaloniaScheduler.Instance); - - AnimationsTimer = globalTimer - .Select(_ => GetTickCount()) + AnimationsTimer = _timer .Publish() .RefCount(); } + public static bool HasSubscriptions => _timer.HasSubscriptions; + internal static TimeSpan GetTickCount() => TimeSpan.FromMilliseconds(Environment.TickCount); /// /// Gets the animation timer. /// - /// - /// The animation timer triggers usually at 60 times per second or as - /// defined in . - /// The parameter passed to a subsciber is the current playstate of the animation. - /// internal static IObservable AnimationsTimer { get; } + + public static void Pulse(long tickCount) => _timer.Pulse(tickCount); + + private class TimerObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(long tickCount) => PublishNext(TimeSpan.FromMilliseconds(tickCount)); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index bdcfdcba8e..5cac6993ff 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -70,7 +70,7 @@ namespace Avalonia.Rendering try { - var needsUpdate = false; + var needsUpdate = Animation.Timing.HasSubscriptions; foreach (var i in _items) { @@ -85,6 +85,8 @@ namespace Avalonia.Rendering { await _dispatcher.InvokeAsync(() => { + Animation.Timing.Pulse(tickCount); + foreach (var i in _items) { i.Update(); From c7c9b0a2052488c8642fe155c80a7e4686c0dbe1 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 29 Jun 2018 23:14:00 +0200 Subject: [PATCH 11/67] Use `Stopwatch.GetTimestamp()` for tickCount. --- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index 6b16ff8038..b05ecd5456 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; +using System.Diagnostics; using System.Threading.Tasks; using Avalonia.Platform; @@ -84,7 +85,7 @@ namespace Avalonia.Rendering _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Stopwatch.GetTimestamp())); } /// From 33aec77f16313cfc6beb4f28701de4d3adc1411f Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Sat, 30 Jun 2018 18:31:35 +0800 Subject: [PATCH 12/67] Move the playstate handling to the State machine instead of being pipelined by the timer itself. Removed unused AnimationStateTimer. --- src/Avalonia.Animation/Animatable.cs | 2 +- src/Avalonia.Animation/Timing.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 8a1a17a6fc..e51103aa55 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -74,4 +74,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs index d6a353fb34..8ee78018a9 100644 --- a/src/Avalonia.Animation/Timing.cs +++ b/src/Avalonia.Animation/Timing.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation /// /// Provides global timing functions for animations. /// - public static class Timing + public class Timing { static TimerObservable _timer = new TimerObservable(); From f2ba884e0ad3d210660bb60f8e387b652aa5d33b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Jul 2018 22:45:25 -0500 Subject: [PATCH 13/67] Cleanup logic in LightweightObservableBase. --- .../Reactive/LightweightObservableBase.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Avalonia.Base/Reactive/LightweightObservableBase.cs b/src/Avalonia.Base/Reactive/LightweightObservableBase.cs index a2786d63da..41009e4cd3 100644 --- a/src/Avalonia.Base/Reactive/LightweightObservableBase.cs +++ b/src/Avalonia.Base/Reactive/LightweightObservableBase.cs @@ -82,18 +82,10 @@ namespace Avalonia.Reactive if (observers.Count == 0) { observers.TrimExcess(); + Deinitialize(); } - else - { - return; - } - } else - { - return; } } - - Deinitialize(); } } From 87e98cacf994eab709c368f283d8f58293926cfa Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 27 Jul 2018 23:57:44 -0500 Subject: [PATCH 14/67] Rewrite Win32 RenderTimer to use undeprecated APIs. --- .../Interop/UnmanagedMethods.cs | 28 +++++++++---- src/Windows/Avalonia.Win32/RenderTimer.cs | 41 +++++++++++++------ 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 1c70b3f03d..d6b95bc7b0 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -26,6 +26,9 @@ namespace Avalonia.Win32.Interop public delegate void TimeCallback(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void WaitOrTimerCallback(IntPtr lpParameter, bool timerOrWaitFired); + public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); public enum Cursor @@ -848,11 +851,25 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow); - [DllImport("Winmm.dll")] - public static extern uint timeKillEvent(uint uTimerID); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr CreateTimerQueue(); + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool DeleteTimerQueueEx(IntPtr TimerQueue, IntPtr CompletionEvent); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool CreateTimerQueueTimer( + out IntPtr phNewTimer, + IntPtr TimerQueue, + WaitOrTimerCallback Callback, + IntPtr Parameter, + uint DueTime, + uint Period, + uint Flags); - [DllImport("Winmm.dll")] - public static extern uint timeSetEvent(uint uDelay, uint uResolution, TimeCallback lpTimeProc, UIntPtr dwUser, uint fuEvent); + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool DeleteTimerQueueTimer(IntPtr TimerQueue, IntPtr Timer, IntPtr CompletionEvent); [DllImport("user32.dll")] public static extern int ToUnicode( @@ -972,9 +989,6 @@ namespace Avalonia.Win32.Interop [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetMonitorInfo([In] IntPtr hMonitor, [Out] MONITORINFO lpmi); - [DllImport("kernel32.dll", SetLastError = true)] - public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); - [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PostMessageW")] public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index cea1bf94a5..0cb107d3a9 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -1,5 +1,6 @@ using System; using System.Reactive.Disposables; +using System.Threading; using Avalonia.Rendering; using Avalonia.Win32.Interop; @@ -7,7 +8,21 @@ namespace Avalonia.Win32 { internal class RenderTimer : DefaultRenderTimer { - private UnmanagedMethods.TimeCallback timerDelegate; + private UnmanagedMethods.WaitOrTimerCallback timerDelegate; + + private static IntPtr _timerQueue; + + private static void EnsureTimerQueueCreated() + { + if (Volatile.Read(ref _timerQueue) == null) + { + var queue = UnmanagedMethods.CreateTimerQueue(); + if (Interlocked.CompareExchange(ref _timerQueue, queue, IntPtr.Zero) != IntPtr.Zero) + { + UnmanagedMethods.DeleteTimerQueueEx(queue, IntPtr.Zero); + } + } + } public RenderTimer(int framesPerSecond) : base(framesPerSecond) @@ -16,23 +31,25 @@ namespace Avalonia.Win32 protected override IDisposable StartCore(Action tick) { - timerDelegate = (id, uMsg, user, dw1, dw2) => - { - UnmanagedMethods.QueryPerformanceCounter(out long tickCount); - tick(tickCount); - }; + EnsureTimerQueueCreated(); + var msPerFrame = 1000 / FramesPerSecond; + + timerDelegate = (_, __) => tick(TimeStampToFrames()); - var handle = UnmanagedMethods.timeSetEvent( - (uint)(1000 / FramesPerSecond), - 0, + UnmanagedMethods.CreateTimerQueueTimer( + out var timer, + _timerQueue, timerDelegate, - UIntPtr.Zero, - 1); + IntPtr.Zero, + (uint)msPerFrame, + (uint)msPerFrame, + 0 + ); return Disposable.Create(() => { timerDelegate = null; - UnmanagedMethods.timeKillEvent(handle); + UnmanagedMethods.DeleteTimerQueueTimer(_timerQueue, timer, IntPtr.Zero); }); } } From e24a125ec0b4add42060e25935521e5557a644f7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Sat, 28 Jul 2018 00:49:13 -0500 Subject: [PATCH 15/67] Make MonoMac platform use DefaultRenderTimer infrastructure in its RenderTimer. --- src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs | 4 ++-- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 21 +++++++++------------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs index 5dbf18b1de..5757413b7a 100644 --- a/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs +++ b/src/OSX/Avalonia.MonoMac/MonoMacPlatform.cs @@ -86,7 +86,7 @@ namespace Avalonia.MonoMac App = NSApplication.SharedApplication; UpdateActivationPolicy(); s_renderLoop = new RenderLoop(); - s_renderTimer = new RenderTimer(); //TODO: use CVDisplayLink + s_renderTimer = new RenderTimer(60); //TODO: use CVDisplayLink s_monoMacInitialized = true; } @@ -136,4 +136,4 @@ namespace Avalonia return builder.UseWindowingSubsystem(MonoMac.MonoMacPlatform.Initialize, "MonoMac"); } } -} \ No newline at end of file +} diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index 71f657b690..22ad2e81a2 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -6,26 +6,23 @@ using MonoMac.Foundation; namespace Avalonia.MonoMac { //TODO: Switch to using CVDisplayLink - public class RenderTimer : IRenderTimer + public class RenderTimer : DefaultRenderTimer { - private readonly object _lock = new object(); - private readonly IDisposable _timer; + public RenderTimer(int framesPerSecond) : base(framesPerSecond) + { + } - public RenderTimer() + protected override IDisposable StartCore(Action tick) { - _timer = AvaloniaLocator.Current.GetService().StartSystemTimer(new TimeSpan(0, 0, 0, 0, 1000 / 60), + return AvaloniaLocator.Current.GetService().StartSystemTimer( + TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => { - lock (_lock) + using (new NSAutoreleasePool()) { - using (new NSAutoreleasePool()) - { - Tick?.Invoke(Environment.TickCount); - } + tick?.Invoke(Environment.TickCount); } }); } - - public event Action Tick; } } From 179ea4eca9666e53dfc678abb4d1f670128a3243 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 10 Aug 2018 21:38:54 +0200 Subject: [PATCH 16/67] inTick doesn't need to be volatile. --- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 5cac6993ff..076e8bbd33 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -10,7 +10,7 @@ namespace Avalonia.Rendering private readonly IDispatcher _dispatcher; private List _items = new List(); private IRenderTimer _timer; - private volatile bool inTick; + private bool inTick; public RenderLoop() { From 25b72971880093256d8773c19f950a8c470f1a9f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 10 Aug 2018 21:46:11 +0200 Subject: [PATCH 17/67] Added RenderLoop docs. --- src/Avalonia.Visuals/Rendering/IRenderLoop.cs | 22 ++++++++++++++++++- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 20 +++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs index bd1086d178..dd7442e7f8 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoop.cs @@ -1,8 +1,28 @@ namespace Avalonia.Rendering { + /// + /// The application render loop. + /// + /// + /// The render loop is responsible for advancing the animation timer and updating the scene + /// graph for visible windows. + /// public interface IRenderLoop { + /// + /// Adds an update task. + /// + /// The update task. + /// + /// Registered update tasks will be polled on each tick of the render loop after the + /// animation timer has been pulsed. + /// void Add(IRenderLoopTask i); + + /// + /// Removes an update task. + /// + /// The update task. void Remove(IRenderLoopTask i); } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 076e8bbd33..98fc12b4b1 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -5,6 +5,13 @@ using Avalonia.Threading; namespace Avalonia.Rendering { + /// + /// The application render loop. + /// + /// + /// The render loop is responsible for advancing the animation timer and updating the scene + /// graph for visible windows. + /// public class RenderLoop : IRenderLoop { private readonly IDispatcher _dispatcher; @@ -12,17 +19,28 @@ namespace Avalonia.Rendering private IRenderTimer _timer; private bool inTick; + /// + /// Initializes a new instance of the class. + /// public RenderLoop() { _dispatcher = Dispatcher.UIThread; } + /// + /// Initializes a new instance of the class. + /// + /// The render timer. + /// The UI thread dispatcher. public RenderLoop(IRenderTimer timer, IDispatcher dispatcher) { _timer = timer; _dispatcher = dispatcher; } + /// + /// Gets the render timer. + /// protected IRenderTimer Timer { get @@ -36,6 +54,7 @@ namespace Avalonia.Rendering } } + /// public void Add(IRenderLoopTask i) { Contract.Requires(i != null); @@ -49,6 +68,7 @@ namespace Avalonia.Rendering } } + /// public void Remove(IRenderLoopTask i) { Contract.Requires(i != null); From 5e1e25e6fa8a17e7ead7c55b1b0b17b73b0fbb05 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 17:31:05 -0500 Subject: [PATCH 18/67] Clean up render timers to use Environment.TickCount. --- src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs | 2 +- src/Windows/Avalonia.Win32/RenderTimer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index b05ecd5456..a83334ff5e 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -85,7 +85,7 @@ namespace Avalonia.Rendering _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Stopwatch.GetTimestamp())); + return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); } /// diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index 0cb107d3a9..c911bc3adf 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -34,7 +34,7 @@ namespace Avalonia.Win32 EnsureTimerQueueCreated(); var msPerFrame = 1000 / FramesPerSecond; - timerDelegate = (_, __) => tick(TimeStampToFrames()); + timerDelegate = (_, __) => tick(Environment.TickCount); UnmanagedMethods.CreateTimerQueueTimer( out var timer, From c5898b98af57b33b2db31a5de19680eedc5b1349 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 17:31:37 -0500 Subject: [PATCH 19/67] Remove unused field. --- .../Rendering/DeferredRenderer.cs | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index f937964994..fcc90edd43 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -32,7 +32,6 @@ namespace Avalonia.Rendering private volatile IRef _scene; private DirtyVisuals _dirty; private IRef _overlay; - private bool _updateQueued; private object _rendering = new object(); private int _lastSceneId = -1; private DisplayDirtyRects _dirtyRectsDisplay = new DisplayDirtyRects(); @@ -394,42 +393,34 @@ namespace Avalonia.Rendering private void UpdateScene() { Dispatcher.UIThread.VerifyAccess(); - - try + if (_root.IsVisible) { - if (_root.IsVisible) - { - var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root)); - var scene = sceneRef.Item; + var sceneRef = RefCountable.Create(_scene?.Item.CloneScene() ?? new Scene(_root)); + var scene = sceneRef.Item; - if (_dirty == null) - { - _dirty = new DirtyVisuals(); - _sceneBuilder.UpdateAll(scene); - } - else if (_dirty.Count > 0) + if (_dirty == null) + { + _dirty = new DirtyVisuals(); + _sceneBuilder.UpdateAll(scene); + } + else if (_dirty.Count > 0) + { + foreach (var visual in _dirty) { - foreach (var visual in _dirty) - { - _sceneBuilder.Update(scene, visual); - } + _sceneBuilder.Update(scene, visual); } + } - var oldScene = Interlocked.Exchange(ref _scene, sceneRef); - oldScene?.Dispose(); + var oldScene = Interlocked.Exchange(ref _scene, sceneRef); + oldScene?.Dispose(); - _dirty.Clear(); - (_root as IRenderRoot)?.Invalidate(new Rect(scene.Size)); - } - else - { - var oldScene = Interlocked.Exchange(ref _scene, null); - oldScene?.Dispose(); - } + _dirty.Clear(); + (_root as IRenderRoot)?.Invalidate(new Rect(scene.Size)); } - finally + else { - _updateQueued = false; + var oldScene = Interlocked.Exchange(ref _scene, null); + oldScene?.Dispose(); } } From 6fe8dba8d957648109978237d9aa665d9524f967 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:23:13 -0500 Subject: [PATCH 20/67] Remove timing class and create Clock classes that can hook into the render loop. --- src/Avalonia.Animation/AnimationInstance`1.cs | 5 +- src/Avalonia.Animation/Clock.cs | 47 +++++++++++++++++ src/Avalonia.Animation/Timing.cs | 51 ------------------- src/Avalonia.Animation/TransitionInstance.cs | 7 ++- .../Animation/RenderLoopClock.cs | 21 ++++++++ .../Rendering/DeferredRenderer.cs | 2 +- .../Rendering/IRenderLoopTask.cs | 15 +++++- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 17 +++---- src/Windows/Avalonia.Win32/Win32Platform.cs | 6 +++ 9 files changed, 101 insertions(+), 70 deletions(-) create mode 100644 src/Avalonia.Animation/Clock.cs delete mode 100644 src/Avalonia.Animation/Timing.cs create mode 100644 src/Avalonia.Visuals/Animation/RenderLoopClock.cs diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 5a72904ed2..a8d8dc73ef 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -82,8 +82,7 @@ namespace Avalonia.Animation protected override void Subscribed() { - _timerSubscription = Timing.AnimationsTimer - .Subscribe(p => this.Step(p)); + _timerSubscription = Clock.GlobalClock.Subscribe(Step); } public void Step(TimeSpan frameTick) @@ -225,4 +224,4 @@ namespace Avalonia.Animation } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs new file mode 100644 index 0000000000..4c37d663b1 --- /dev/null +++ b/src/Avalonia.Animation/Clock.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Text; +using Avalonia.Reactive; + +namespace Avalonia.Animation +{ + public class Clock : IObservable + { + public static Clock GlobalClock => AvaloniaLocator.Current.GetService(); + + private ClockObservable _observable; + + private IObservable _connectedObservable; + + public Clock() + { + _observable = new ClockObservable(); + _connectedObservable = _observable.Publish().RefCount(); + } + + public bool HasSubscriptions => _observable.HasSubscriptions; + + public TimeSpan CurrentTime { get; private set; } + + public void Pulse(long tickCount) + { + var time = TimeSpan.FromMilliseconds(tickCount); + _observable.Pulse(time); + CurrentTime = time; + } + + public IDisposable Subscribe(IObserver observer) + { + return _connectedObservable.Subscribe(observer); + } + + private class ClockObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(TimeSpan tickCount) => PublishNext(tickCount); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } + } +} diff --git a/src/Avalonia.Animation/Timing.cs b/src/Avalonia.Animation/Timing.cs deleted file mode 100644 index 8ee78018a9..0000000000 --- a/src/Avalonia.Animation/Timing.cs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) The Avalonia Project. All rights reserved. -// Licensed under the MIT license. See licence.md file in the project root for full license information. - -using System; -using System.Linq; -using System.Reactive.Linq; -using Avalonia.Reactive; -using Avalonia.Threading; - -namespace Avalonia.Animation -{ - /// - /// Provides global timing functions for animations. - /// - public class Timing - { - static TimerObservable _timer = new TimerObservable(); - - /// - /// Initializes static members of the class. - /// - static Timing() - { - AnimationsTimer = _timer - .Publish() - .RefCount(); - } - - public static bool HasSubscriptions => _timer.HasSubscriptions; - - internal static TimeSpan GetTickCount() => TimeSpan.FromMilliseconds(Environment.TickCount); - - /// - /// Gets the animation timer. - /// - internal static IObservable AnimationsTimer - { - get; - } - - public static void Pulse(long tickCount) => _timer.Pulse(tickCount); - - private class TimerObservable : LightweightObservableBase - { - public bool HasSubscriptions { get; private set; } - public void Pulse(long tickCount) => PublishNext(TimeSpan.FromMilliseconds(tickCount)); - protected override void Initialize() => HasSubscriptions = true; - protected override void Deinitialize() => HasSubscriptions = false; - } - } -} diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index e2719cb472..4c61adea28 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -45,10 +45,9 @@ namespace Avalonia.Animation protected override void Subscribed() { - startTime = Timing.GetTickCount(); - timerSubscription = Timing.AnimationsTimer - .Subscribe(t => TimerTick(t)); + startTime = Clock.GlobalClock.CurrentTime; + timerSubscription = Clock.GlobalClock.Subscribe(TimerTick); PublishNext(0.0d); } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs new file mode 100644 index 0000000000..3d166035ec --- /dev/null +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Rendering; + +namespace Avalonia.Animation +{ + public class RenderLoopClock : Clock, IRenderLoopTask + { + bool IRenderLoopTask.NeedsUpdate => HasSubscriptions; + + void IRenderLoopTask.Render() + { + } + + void IRenderLoopTask.Update(long tickCount) + { + Pulse(tickCount); + } + } +} diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index fcc90edd43..3221dd85c6 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -166,7 +166,7 @@ namespace Avalonia.Rendering bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; - void IRenderLoopTask.Update() => UpdateScene(); + void IRenderLoopTask.Update(long tickCount) => UpdateScene(); void IRenderLoopTask.Render() { diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs index 2f251a5c17..b031bf00df 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -6,7 +6,20 @@ namespace Avalonia.Rendering public interface IRenderLoopTask { bool NeedsUpdate { get; } - void Update(); + void Update(long tickCount); void Render(); } + + public class MockRenderLoopTask : IRenderLoopTask + { + public bool NeedsUpdate => true; + + public void Render() + { + } + + public void Update(long tickCount) + { + } + } } diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index 98fc12b4b1..a850b99c5e 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Threading; using Avalonia.Logging; using Avalonia.Threading; @@ -17,7 +18,7 @@ namespace Avalonia.Rendering private readonly IDispatcher _dispatcher; private List _items = new List(); private IRenderTimer _timer; - private bool inTick; + private int inTick; /// /// Initializes a new instance of the class. @@ -84,13 +85,11 @@ namespace Avalonia.Rendering private async void TimerTick(long tickCount) { - if (!inTick) + if (Interlocked.CompareExchange(ref inTick, 1, 0) == 0) { - inTick = true; - try { - var needsUpdate = Animation.Timing.HasSubscriptions; + var needsUpdate = false; foreach (var i in _items) { @@ -105,13 +104,11 @@ namespace Avalonia.Rendering { await _dispatcher.InvokeAsync(() => { - Animation.Timing.Pulse(tickCount); - foreach (var i in _items) { - i.Update(); + i.Update(tickCount); } - }); + }).ConfigureAwait(false); } foreach (var i in _items) @@ -125,7 +122,7 @@ namespace Avalonia.Rendering } finally { - inTick = false; + Interlocked.Exchange(ref inTick, 0); } } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index d5eb97292a..812a557765 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -9,6 +9,7 @@ using System.IO; using System.Reactive.Disposables; using System.Runtime.InteropServices; using System.Threading; +using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Controls.Platform; using Avalonia.Input; @@ -76,6 +77,8 @@ namespace Avalonia.Win32 public static void Initialize(bool deferredRendering = true) { + var clock = new RenderLoopClock(); + AvaloniaLocator.CurrentMutable .Bind().ToSingleton() .Bind().ToConstant(CursorFactory.Instance) @@ -84,6 +87,7 @@ namespace Avalonia.Win32 .Bind().ToConstant(s_instance) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new RenderTimer(60)) + .Bind().ToConstant(clock) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); @@ -93,6 +97,8 @@ namespace Avalonia.Win32 if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); + + AvaloniaLocator.Current.GetService().Add(clock); } public bool HasMessages() From 5c8ced89a30e3339f070d75b3f361f8342ae99fa Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:52:41 -0500 Subject: [PATCH 21/67] Move GlobalPlayState tracking to Clock. --- src/Avalonia.Animation/Animation.cs | 7 ++++++- src/Avalonia.Animation/Clock.cs | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 2c359ecac3..7b3aa06ea0 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -17,10 +17,15 @@ namespace Avalonia.Animation /// public class Animation : AvaloniaList, IAnimation { + /// /// Gets or sets the animation play state for all animations /// - public static PlayState GlobalPlayState { get; set; } = PlayState.Run; + public static PlayState GlobalPlayState + { + get => AvaloniaLocator.Current.GetService().PlayState; + set => AvaloniaLocator.Current.GetService().PlayState = value; + } /// /// Gets or sets the active time of this animation. diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 4c37d663b1..1ba6425551 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -24,6 +24,8 @@ namespace Avalonia.Animation public TimeSpan CurrentTime { get; private set; } + public PlayState PlayState { get; set; } + public void Pulse(long tickCount) { var time = TimeSpan.FromMilliseconds(tickCount); From a987c4648b77407c9b17519a52747d4868ae524a Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 22:57:06 -0500 Subject: [PATCH 22/67] Move Pause PlayState time tracking into Clock. --- src/Avalonia.Animation/AnimationInstance`1.cs | 26 +++--------------- src/Avalonia.Animation/Clock.cs | 27 ++++++++++++++++--- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index a8d8dc73ef..a2d25e4524 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -8,7 +8,7 @@ using Avalonia.Reactive; namespace Avalonia.Animation { /// - /// Handles interpolatoin and time-related functions + /// Handles interpolation and time-related functions /// for keyframe animations. /// internal class AnimationInstance : SingleSubscriberObservableBase @@ -30,8 +30,6 @@ namespace Avalonia.Animation private TimeSpan _delay; private TimeSpan _duration; private TimeSpan _firstFrameCount; - private TimeSpan _internalClock; - private TimeSpan? _previousClock; private Easings.Easing _easeFunc; private Action _onCompleteAction; private Func _interpolator; @@ -120,23 +118,6 @@ namespace Avalonia.Animation if (Animation.GlobalPlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) DoComplete(); - if (!_previousClock.HasValue) - { - _previousClock = systemTime; - _internalClock = TimeSpan.Zero; - } - else - { - if (Animation.GlobalPlayState == PlayState.Pause || _targetControl.PlayState == PlayState.Pause) - { - _previousClock = systemTime; - return; - } - var delta = systemTime - _previousClock; - _internalClock += delta.Value; - _previousClock = systemTime; - } - if (!_gotFirstKFValue) { _firstKFValue = (T)_parent.First().Value; @@ -145,7 +126,7 @@ namespace Avalonia.Animation if (!_gotFirstFrameCount) { - _firstFrameCount = _internalClock; + _firstFrameCount = systemTime; _gotFirstFrameCount = true; } } @@ -154,7 +135,7 @@ namespace Avalonia.Animation { DoPlayStatesAndTime(systemTime); - var time = _internalClock - _firstFrameCount; + var time = systemTime - _firstFrameCount; var delayEndpoint = _delay; var iterationEndpoint = delayEndpoint + _duration; @@ -179,7 +160,6 @@ namespace Avalonia.Animation } else { - _previousClock = systemTime; return; } diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 1ba6425551..ced63163c9 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -14,6 +14,9 @@ namespace Avalonia.Animation private IObservable _connectedObservable; + private TimeSpan? _previousTime; + private TimeSpan _internalTime; + public Clock() { _observable = new ClockObservable(); @@ -28,9 +31,27 @@ namespace Avalonia.Animation public void Pulse(long tickCount) { - var time = TimeSpan.FromMilliseconds(tickCount); - _observable.Pulse(time); - CurrentTime = time; + var systemTime = TimeSpan.FromMilliseconds(tickCount); + + if (!_previousTime.HasValue) + { + _previousTime = systemTime; + _internalTime = TimeSpan.Zero; + } + else + { + if (PlayState == PlayState.Pause) + { + _previousTime = systemTime; + return; + } + var delta = systemTime - _previousTime; + _internalTime += delta.Value; + _previousTime = systemTime; + } + + _observable.Pulse(_internalTime); + CurrentTime = _internalTime; } public IDisposable Subscribe(IObserver observer) From 9b9676d3fe346610fdc05eededd2c90253430bd7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:03:53 -0500 Subject: [PATCH 23/67] Enable chaining clocks. --- src/Avalonia.Animation/Clock.cs | 16 +++++++++++----- .../Animation/RenderLoopClock.cs | 2 +- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index ced63163c9..2de781ea57 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -14,25 +14,31 @@ namespace Avalonia.Animation private IObservable _connectedObservable; + private IDisposable _parentSubscription; + private TimeSpan? _previousTime; private TimeSpan _internalTime; - public Clock() + protected Clock() { _observable = new ClockObservable(); _connectedObservable = _observable.Publish().RefCount(); } + public Clock(Clock parent) + :this() + { + _parentSubscription = parent.Subscribe(Pulse); + } + public bool HasSubscriptions => _observable.HasSubscriptions; public TimeSpan CurrentTime { get; private set; } public PlayState PlayState { get; set; } - public void Pulse(long tickCount) + protected void Pulse(TimeSpan systemTime) { - var systemTime = TimeSpan.FromMilliseconds(tickCount); - if (!_previousTime.HasValue) { _previousTime = systemTime; @@ -62,7 +68,7 @@ namespace Avalonia.Animation private class ClockObservable : LightweightObservableBase { public bool HasSubscriptions { get; private set; } - public void Pulse(TimeSpan tickCount) => PublishNext(tickCount); + public void Pulse(TimeSpan time) => PublishNext(time); protected override void Initialize() => HasSubscriptions = true; protected override void Deinitialize() => HasSubscriptions = false; } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index 3d166035ec..d60d366ad4 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -15,7 +15,7 @@ namespace Avalonia.Animation void IRenderLoopTask.Update(long tickCount) { - Pulse(tickCount); + Pulse(TimeSpan.FromMilliseconds(tickCount)); } } } From bf1b78c4ab0da4ccb60a1d5456da50a2bf5092f9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:39:25 -0500 Subject: [PATCH 24/67] Enable Animations to run on a non-global clock. --- src/Avalonia.Animation/Animation.cs | 15 ++++++++++----- src/Avalonia.Animation/AnimationInstance`1.cs | 8 +++++--- src/Avalonia.Animation/Animator`1.cs | 8 ++++---- src/Avalonia.Animation/Clock.cs | 10 ++++++++++ src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Styling/Styling/Style.cs | 2 +- src/Avalonia.Visuals/Animation/RenderLoopClock.cs | 5 +++++ .../Animation/TransformAnimator.cs | 6 +++--- 9 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 7b3aa06ea0..e787143b59 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -154,12 +154,12 @@ namespace Avalonia.Animation } /// - public IDisposable Apply(Animatable control, IObservable match, Action onComplete) + public IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete) { var (animators, subscriptions) = InterpretKeyframes(control); if (animators.Count == 1) { - subscriptions.Add(animators[0].Apply(this, control, match, onComplete)); + subscriptions.Add(animators[0].Apply(this, control, clock, match, onComplete)); } else { @@ -173,7 +173,7 @@ namespace Avalonia.Animation animatorOnComplete = () => tcs.SetResult(null); completionTasks.Add(tcs.Task); } - subscriptions.Add(animator.Apply(this, control, match, animatorOnComplete)); + subscriptions.Add(animator.Apply(this, control, clock, match, animatorOnComplete)); } if (onComplete != null) @@ -185,15 +185,20 @@ namespace Avalonia.Animation } /// - public Task RunAsync(Animatable control) + public Task RunAsync(Animatable control, Clock clock = null) { + if (clock == null) + { + clock = Clock.GlobalClock; + } + var run = new TaskCompletionSource(); if (this.RepeatCount == RepeatCount.Loop) run.SetException(new InvalidOperationException("Looping animations must not use the Run method.")); IDisposable subscriptions = null; - subscriptions = this.Apply(control, Observable.Return(true), () => + subscriptions = this.Apply(control, clock, Observable.Return(true), () => { run.SetResult(null); subscriptions?.Dispose(); diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index a2d25e4524..fe84dd879a 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -34,8 +34,9 @@ namespace Avalonia.Animation private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; + private readonly Clock _clock; - public AnimationInstance(Animation animation, Animatable control, Animator animator, Action OnComplete, Func Interpolator) + public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) { if (animation.SpeedRatio <= 0) throw new InvalidOperationException("Speed ratio cannot be negative or zero."); @@ -71,6 +72,7 @@ namespace Avalonia.Animation _fillMode = animation.FillMode; _onCompleteAction = OnComplete; _interpolator = Interpolator; + _clock = clock; } protected override void Unsubscribed() @@ -80,7 +82,7 @@ namespace Avalonia.Animation protected override void Subscribed() { - _timerSubscription = Clock.GlobalClock.Subscribe(Step); + _timerSubscription = _clock.Subscribe(Step); } public void Step(TimeSpan frameTick) @@ -115,7 +117,7 @@ namespace Avalonia.Animation private void DoPlayStatesAndTime(TimeSpan systemTime) { - if (Animation.GlobalPlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index f0ef55aa9e..c699ff635a 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -32,7 +32,7 @@ namespace Avalonia.Animation } /// - public virtual IDisposable Apply(Animation animation, Animatable control, IObservable match, Action onComplete) + public virtual IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable match, Action onComplete) { if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); @@ -41,7 +41,7 @@ namespace Avalonia.Animation .Where(p => p) .Subscribe(_ => { - var timerObs = RunKeyFrames(animation, control, onComplete); + var timerObs = RunKeyFrames(animation, control, clock, onComplete); }); } @@ -101,9 +101,9 @@ namespace Avalonia.Animation /// /// Runs the KeyFrames Animation. /// - private IDisposable RunKeyFrames(Animation animation, Animatable control, Action onComplete) + private IDisposable RunKeyFrames(Animation animation, Animatable control, Clock clock, Action onComplete) { - var instance = new AnimationInstance(animation, control, this, onComplete, DoInterpolation); + var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); } diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index 2de781ea57..e8616c9694 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -58,6 +58,16 @@ namespace Avalonia.Animation _observable.Pulse(_internalTime); CurrentTime = _internalTime; + + if (PlayState == PlayState.Stop) + { + Stop(); + } + } + + protected virtual void Stop() + { + _parentSubscription?.Dispose(); } public IDisposable Subscribe(IObserver observer) diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index 1d545a322a..f726cf43dc 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -11,11 +11,11 @@ namespace Avalonia.Animation /// /// Apply the animation to the specified control /// - IDisposable Apply(Animatable control, IObservable match, Action onComplete = null); + IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete = null); /// /// Run the animation to the specified control /// - Task RunAsync(Animatable control); + Task RunAsync(Animatable control, Clock clock); } } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 9a4da35a02..134b30a555 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete); } } diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index 399be5470d..a033184588 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -120,7 +120,7 @@ namespace Avalonia.Styling obsMatch = Observable.Return(true); } - var sub = animation.Apply((Animatable)control, obsMatch); + var sub = animation.Apply((Animatable)control, Clock.GlobalClock, obsMatch); subs.Add(sub); } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index d60d366ad4..d9ee269739 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -7,6 +7,11 @@ namespace Avalonia.Animation { public class RenderLoopClock : Clock, IRenderLoopTask { + protected override void Stop() + { + AvaloniaLocator.Current.GetService().Remove(this); + } + bool IRenderLoopTask.NeedsUpdate => HasSubscriptions; void IRenderLoopTask.Render() diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 2be1226abe..4476058bfe 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation DoubleAnimator childKeyFrames; /// - public override IDisposable Apply(Animation animation, Animatable control, IObservable obsMatch, Action onComplete) + public override IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete) { var ctrl = (Visual)control; @@ -44,7 +44,7 @@ namespace Avalonia.Animation // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, obsMatch, onComplete); + return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, obsMatch, onComplete); + return childKeyFrames.Apply(animation, transform, clock, obsMatch, onComplete); } } } From b0368c80b29e7ea7fcc99f32c53252c2218fbc4b Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:43:06 -0500 Subject: [PATCH 25/67] Enable transitions to run on custom clocks. --- src/Avalonia.Animation/Animatable.cs | 2 +- src/Avalonia.Animation/ITransition.cs | 2 +- src/Avalonia.Animation/TransitionInstance.cs | 8 +++++--- src/Avalonia.Animation/Transition`1.cs | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index e51103aa55..5208356570 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -69,7 +69,7 @@ namespace Avalonia.Animation if (match != null) { - match.Apply(this, e.OldValue, e.NewValue); + match.Apply(this, Clock.GlobalClock, e.OldValue, e.NewValue); } } } diff --git a/src/Avalonia.Animation/ITransition.cs b/src/Avalonia.Animation/ITransition.cs index e2ffe7fc6e..7afaa2325a 100644 --- a/src/Avalonia.Animation/ITransition.cs +++ b/src/Avalonia.Animation/ITransition.cs @@ -13,7 +13,7 @@ namespace Avalonia.Animation /// /// Applies the transition to the specified . /// - IDisposable Apply(Animatable control, object oldValue, object newValue); + IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue); /// /// Gets the property to be animated. diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index 4c61adea28..b0c927f3cd 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -18,10 +18,12 @@ namespace Avalonia.Animation private IDisposable timerSubscription; private TimeSpan startTime; private TimeSpan duration; + private readonly Clock _clock; - public TransitionInstance(TimeSpan Duration) + public TransitionInstance(Clock clock, TimeSpan Duration) { duration = Duration; + _clock = clock; } private void TimerTick(TimeSpan t) @@ -45,8 +47,8 @@ namespace Avalonia.Animation protected override void Subscribed() { - startTime = Clock.GlobalClock.CurrentTime; - timerSubscription = Clock.GlobalClock.Subscribe(TimerTick); + startTime = _clock.CurrentTime; + timerSubscription = _clock.Subscribe(TimerTick); PublishNext(0.0d); } } diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index 4b01c54f5c..23df7f9807 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -49,9 +49,9 @@ namespace Avalonia.Animation public abstract IObservable DoTransition(IObservable progress, T oldValue, T newValue); /// - public virtual IDisposable Apply(Animatable control, object oldValue, object newValue) + public virtual IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue) { - var transition = DoTransition(new TransitionInstance(Duration), (T)oldValue, (T)newValue); + var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); } From 6a380d6591e0f07e15edcbb6ace96baabec9b287 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:49:52 -0500 Subject: [PATCH 26/67] Reorganize RenderLoopClock registration. --- src/Avalonia.Controls/AppBuilderBase.cs | 2 +- src/Avalonia.Controls/Application.cs | 7 +++++++ src/Windows/Avalonia.Win32/Win32Platform.cs | 5 ----- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/AppBuilderBase.cs b/src/Avalonia.Controls/AppBuilderBase.cs index c92d5d7694..9561282274 100644 --- a/src/Avalonia.Controls/AppBuilderBase.cs +++ b/src/Avalonia.Controls/AppBuilderBase.cs @@ -272,10 +272,10 @@ namespace Avalonia.Controls s_setupWasAlreadyCalled = true; - Instance.RegisterServices(); RuntimePlatformServicesInitializer(); WindowingSubsystemInitializer(); RenderingSubsystemInitializer(); + Instance.RegisterServices(); Instance.Initialize(); AfterSetupCallback(Self); } diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 4c549ac7d4..8f6544ccd8 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -4,12 +4,14 @@ using System; using System.Reactive.Concurrency; using System.Threading; +using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.Input.Platform; using Avalonia.Input.Raw; using Avalonia.Platform; +using Avalonia.Rendering; using Avalonia.Styling; using Avalonia.Threading; @@ -335,6 +337,11 @@ namespace Avalonia .Bind().ToConstant(AvaloniaScheduler.Instance) .Bind().ToConstant(DragDropDevice.Instance) .Bind().ToTransient(); + + var clock = new RenderLoopClock(); + AvaloniaLocator.CurrentMutable + .Bind().ToConstant(clock) + .GetService().Add(clock); } } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 812a557765..ef2dfd3c1a 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -77,8 +77,6 @@ namespace Avalonia.Win32 public static void Initialize(bool deferredRendering = true) { - var clock = new RenderLoopClock(); - AvaloniaLocator.CurrentMutable .Bind().ToSingleton() .Bind().ToConstant(CursorFactory.Instance) @@ -87,7 +85,6 @@ namespace Avalonia.Win32 .Bind().ToConstant(s_instance) .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new RenderTimer(60)) - .Bind().ToConstant(clock) .Bind().ToSingleton() .Bind().ToConstant(s_instance) .Bind().ToConstant(s_instance); @@ -97,8 +94,6 @@ namespace Avalonia.Win32 if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); - - AvaloniaLocator.Current.GetService().Add(clock); } public bool HasMessages() From 0c611b981d86e979b0989b0c5b3c15ce0a5ef81a Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Thu, 6 Sep 2018 23:57:38 -0500 Subject: [PATCH 27/67] Refactor clock types. --- src/Avalonia.Animation/Clock.cs | 71 ++---------------- src/Avalonia.Animation/ClockBase.cs | 75 +++++++++++++++++++ src/Avalonia.Animation/IClock.cs | 13 ++++ src/Avalonia.Controls/Application.cs | 2 +- .../Animation/RenderLoopClock.cs | 2 +- 5 files changed, 95 insertions(+), 68 deletions(-) create mode 100644 src/Avalonia.Animation/ClockBase.cs create mode 100644 src/Avalonia.Animation/IClock.cs diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index e8616c9694..f61a4c7db1 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -6,81 +6,20 @@ using Avalonia.Reactive; namespace Avalonia.Animation { - public class Clock : IObservable + public class Clock : ClockBase { - public static Clock GlobalClock => AvaloniaLocator.Current.GetService(); - - private ClockObservable _observable; - - private IObservable _connectedObservable; + public static IClock GlobalClock => AvaloniaLocator.Current.GetService(); private IDisposable _parentSubscription; - - private TimeSpan? _previousTime; - private TimeSpan _internalTime; - - protected Clock() - { - _observable = new ClockObservable(); - _connectedObservable = _observable.Publish().RefCount(); - } - - public Clock(Clock parent) - :this() + + public Clock(IClock parent) { _parentSubscription = parent.Subscribe(Pulse); } - public bool HasSubscriptions => _observable.HasSubscriptions; - - public TimeSpan CurrentTime { get; private set; } - - public PlayState PlayState { get; set; } - - protected void Pulse(TimeSpan systemTime) - { - if (!_previousTime.HasValue) - { - _previousTime = systemTime; - _internalTime = TimeSpan.Zero; - } - else - { - if (PlayState == PlayState.Pause) - { - _previousTime = systemTime; - return; - } - var delta = systemTime - _previousTime; - _internalTime += delta.Value; - _previousTime = systemTime; - } - - _observable.Pulse(_internalTime); - CurrentTime = _internalTime; - - if (PlayState == PlayState.Stop) - { - Stop(); - } - } - - protected virtual void Stop() + protected override void Stop() { _parentSubscription?.Dispose(); } - - public IDisposable Subscribe(IObserver observer) - { - return _connectedObservable.Subscribe(observer); - } - - private class ClockObservable : LightweightObservableBase - { - public bool HasSubscriptions { get; private set; } - public void Pulse(TimeSpan time) => PublishNext(time); - protected override void Initialize() => HasSubscriptions = true; - protected override void Deinitialize() => HasSubscriptions = false; - } } } diff --git a/src/Avalonia.Animation/ClockBase.cs b/src/Avalonia.Animation/ClockBase.cs new file mode 100644 index 0000000000..ea784269d9 --- /dev/null +++ b/src/Avalonia.Animation/ClockBase.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Text; +using Avalonia.Reactive; + +namespace Avalonia.Animation +{ + public class ClockBase : IClock + { + private ClockObservable _observable; + + private IObservable _connectedObservable; + + private TimeSpan? _previousTime; + private TimeSpan _internalTime; + + protected ClockBase() + { + _observable = new ClockObservable(); + _connectedObservable = _observable.Publish().RefCount(); + } + + public bool HasSubscriptions => _observable.HasSubscriptions; + + public TimeSpan CurrentTime { get; private set; } + + public PlayState PlayState { get; set; } + + protected void Pulse(TimeSpan systemTime) + { + if (!_previousTime.HasValue) + { + _previousTime = systemTime; + _internalTime = TimeSpan.Zero; + } + else + { + if (PlayState == PlayState.Pause) + { + _previousTime = systemTime; + return; + } + var delta = systemTime - _previousTime; + _internalTime += delta.Value; + _previousTime = systemTime; + } + + _observable.Pulse(_internalTime); + CurrentTime = _internalTime; + + if (PlayState == PlayState.Stop) + { + Stop(); + } + } + + protected virtual void Stop() + { + } + + public IDisposable Subscribe(IObserver observer) + { + return _connectedObservable.Subscribe(observer); + } + + private class ClockObservable : LightweightObservableBase + { + public bool HasSubscriptions { get; private set; } + public void Pulse(TimeSpan time) => PublishNext(time); + protected override void Initialize() => HasSubscriptions = true; + protected override void Deinitialize() => HasSubscriptions = false; + } + } +} diff --git a/src/Avalonia.Animation/IClock.cs b/src/Avalonia.Animation/IClock.cs new file mode 100644 index 0000000000..58c997841d --- /dev/null +++ b/src/Avalonia.Animation/IClock.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Animation +{ + public interface IClock : IObservable + { + bool HasSubscriptions { get; } + TimeSpan CurrentTime { get; } + PlayState PlayState { get; set; } + } +} diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 8f6544ccd8..8c03bac61a 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -340,7 +340,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable - .Bind().ToConstant(clock) + .Bind().ToConstant(clock) .GetService().Add(clock); } } diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index d9ee269739..e59b3aac0d 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -5,7 +5,7 @@ using Avalonia.Rendering; namespace Avalonia.Animation { - public class RenderLoopClock : Clock, IRenderLoopTask + public class RenderLoopClock : ClockBase, IRenderLoopTask { protected override void Stop() { From 59cad1cf86651e4ab3f3c015d1e0636c50c2ea08 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 00:02:41 -0500 Subject: [PATCH 28/67] Fix unfinished clock type refactor. --- src/Avalonia.Animation/Animatable.cs | 5 ++++- src/Avalonia.Animation/Animation.cs | 4 ++-- src/Avalonia.Animation/Animator`1.cs | 4 ++-- src/Avalonia.Animation/Clock.cs | 5 +++++ src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Animation/ITransition.cs | 2 +- src/Avalonia.Animation/Transition`1.cs | 2 +- 8 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 5208356570..03a7a32e02 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -14,7 +14,10 @@ namespace Avalonia.Animation /// Base class for all animatable objects. /// public class Animatable : AvaloniaObject - { + { + public static readonly StyledProperty ClockProperty = + AvaloniaProperty.Register(nameof(Clock), inherits: true); + /// /// Defines the property. /// diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index e787143b59..1d2a7b4559 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -154,7 +154,7 @@ namespace Avalonia.Animation } /// - public IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete) + public IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete) { var (animators, subscriptions) = InterpretKeyframes(control); if (animators.Count == 1) @@ -185,7 +185,7 @@ namespace Avalonia.Animation } /// - public Task RunAsync(Animatable control, Clock clock = null) + public Task RunAsync(Animatable control, IClock clock = null) { if (clock == null) { diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index c699ff635a..44a10db545 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -32,7 +32,7 @@ namespace Avalonia.Animation } /// - public virtual IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable match, Action onComplete) + public virtual IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete) { if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); @@ -101,7 +101,7 @@ namespace Avalonia.Animation /// /// Runs the KeyFrames Animation. /// - private IDisposable RunKeyFrames(Animation animation, Animatable control, Clock clock, Action onComplete) + private IDisposable RunKeyFrames(Animation animation, Animatable control, IClock clock, Action onComplete) { var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); diff --git a/src/Avalonia.Animation/Clock.cs b/src/Avalonia.Animation/Clock.cs index f61a4c7db1..e009c2aad5 100644 --- a/src/Avalonia.Animation/Clock.cs +++ b/src/Avalonia.Animation/Clock.cs @@ -11,6 +11,11 @@ namespace Avalonia.Animation public static IClock GlobalClock => AvaloniaLocator.Current.GetService(); private IDisposable _parentSubscription; + + public Clock() + :this(GlobalClock) + { + } public Clock(IClock parent) { diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index f726cf43dc..34b0a5d769 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -11,11 +11,11 @@ namespace Avalonia.Animation /// /// Apply the animation to the specified control /// - IDisposable Apply(Animatable control, Clock clock, IObservable match, Action onComplete = null); + IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete = null); /// /// Run the animation to the specified control /// - Task RunAsync(Animatable control, Clock clock); + Task RunAsync(Animatable control, IClock clock); } } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 134b30a555..04bad8e112 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete); } } diff --git a/src/Avalonia.Animation/ITransition.cs b/src/Avalonia.Animation/ITransition.cs index 7afaa2325a..e5d8466f04 100644 --- a/src/Avalonia.Animation/ITransition.cs +++ b/src/Avalonia.Animation/ITransition.cs @@ -13,7 +13,7 @@ namespace Avalonia.Animation /// /// Applies the transition to the specified . /// - IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue); + IDisposable Apply(Animatable control, IClock clock, object oldValue, object newValue); /// /// Gets the property to be animated. diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index 23df7f9807..b54ec8f51c 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -49,7 +49,7 @@ namespace Avalonia.Animation public abstract IObservable DoTransition(IObservable progress, T oldValue, T newValue); /// - public virtual IDisposable Apply(Animatable control, Clock clock, object oldValue, object newValue) + public virtual IDisposable Apply(Animatable control, IClock clock, object oldValue, object newValue) { var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); From ec9c61bbbe4afcb18852b4992428817d3fa111a9 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 00:11:49 -0500 Subject: [PATCH 29/67] Allow clocks to be bindable and inherited down the logical tree. --- src/Avalonia.Animation/Animatable.cs | 23 ++++--------------- src/Avalonia.Animation/AnimationInstance`1.cs | 4 ++-- src/Avalonia.Animation/TransitionInstance.cs | 2 +- src/Avalonia.Styling/Styling/Style.cs | 19 ++++++++------- .../Animation/TransformAnimator.cs | 2 +- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/src/Avalonia.Animation/Animatable.cs b/src/Avalonia.Animation/Animatable.cs index 03a7a32e02..516f383b92 100644 --- a/src/Avalonia.Animation/Animatable.cs +++ b/src/Avalonia.Animation/Animatable.cs @@ -18,25 +18,10 @@ namespace Avalonia.Animation public static readonly StyledProperty ClockProperty = AvaloniaProperty.Register(nameof(Clock), inherits: true); - /// - /// Defines the property. - /// - public static readonly DirectProperty PlayStateProperty = - AvaloniaProperty.RegisterDirect( - nameof(PlayState), - o => o.PlayState, - (o, v) => o.PlayState = v); - - private PlayState _playState = PlayState.Run; - - /// - /// Gets or sets the state of the animation for this - /// control. - /// - public PlayState PlayState + public IClock Clock { - get { return _playState; } - set { SetAndRaise(PlayStateProperty, ref _playState, value); } + get => GetValue(ClockProperty); + set => SetValue(ClockProperty, value); } /// @@ -72,7 +57,7 @@ namespace Avalonia.Animation if (match != null) { - match.Apply(this, Clock.GlobalClock, e.OldValue, e.NewValue); + match.Apply(this, Clock ?? Avalonia.Animation.Clock.GlobalClock, e.OldValue, e.NewValue); } } } diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index fe84dd879a..99ebbe752a 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -34,7 +34,7 @@ namespace Avalonia.Animation private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; - private readonly Clock _clock; + private readonly IClock _clock; public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) { @@ -117,7 +117,7 @@ namespace Avalonia.Animation private void DoPlayStatesAndTime(TimeSpan systemTime) { - if (_clock.PlayState == PlayState.Stop || _targetControl.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index b0c927f3cd..ad87ad7010 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -18,7 +18,7 @@ namespace Avalonia.Animation private IDisposable timerSubscription; private TimeSpan startTime; private TimeSpan duration; - private readonly Clock _clock; + private readonly IClock _clock; public TransitionInstance(Clock clock, TimeSpan Duration) { diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index a033184588..62b3ca72ae 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -111,17 +111,20 @@ namespace Avalonia.Styling { var subs = GetSubscriptions(control); - foreach (var animation in Animations) + if (control is Animatable animatable) { - IObservable obsMatch = match.ObservableResult; - - if (match.ImmediateResult == true) + foreach (var animation in Animations) { - obsMatch = Observable.Return(true); - } + IObservable obsMatch = match.ObservableResult; - var sub = animation.Apply((Animatable)control, Clock.GlobalClock, obsMatch); - subs.Add(sub); + if (match.ImmediateResult == true) + { + obsMatch = Observable.Return(true); + } + + var sub = animation.Apply(animatable, animatable.Clock ?? Clock.GlobalClock, obsMatch); + subs.Add(sub); + } } foreach (var setter in Setters) diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 4476058bfe..6336c49dc5 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -12,7 +12,7 @@ namespace Avalonia.Animation DoubleAnimator childKeyFrames; /// - public override IDisposable Apply(Animation animation, Animatable control, Clock clock, IObservable obsMatch, Action onComplete) + public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete) { var ctrl = (Visual)control; From 51faa94534c2ca0a0de03305f96d10a8cdb6ce8e Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 13:49:44 -0500 Subject: [PATCH 30/67] Allow users to supply custom clocks in XAML or code before animations are applied. Change AnimationsPage to show an example with a custom clock and the animations on that page. --- samples/RenderDemo/Pages/AnimationsPage.xaml | 7 +++-- .../RenderDemo/Pages/AnimationsPage.xaml.cs | 16 ++++++++++++ .../ViewModels/AnimationsPageViewModel.cs | 26 +++++-------------- src/Avalonia.Animation/Animation.cs | 10 ------- src/Avalonia.Animation/Animator`1.cs | 8 +++++- src/Avalonia.Styling/Styling/Style.cs | 2 +- .../Animation/TransformAnimator.cs | 4 +-- 7 files changed, 37 insertions(+), 36 deletions(-) diff --git a/samples/RenderDemo/Pages/AnimationsPage.xaml b/samples/RenderDemo/Pages/AnimationsPage.xaml index 5287e4e373..473807ac50 100644 --- a/samples/RenderDemo/Pages/AnimationsPage.xaml +++ b/samples/RenderDemo/Pages/AnimationsPage.xaml @@ -107,9 +107,12 @@ + + + Hover to activate Transform Keyframe Animations. - public class Animation : AvaloniaList, IAnimation { - - /// - /// Gets or sets the animation play state for all animations - /// - public static PlayState GlobalPlayState - { - get => AvaloniaLocator.Current.GetService().PlayState; - set => AvaloniaLocator.Current.GetService().PlayState = value; - } - /// /// Gets or sets the active time of this animation. /// diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 44a10db545..b68f2fc79a 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -103,7 +103,13 @@ namespace Avalonia.Animation /// private IDisposable RunKeyFrames(Animation animation, Animatable control, IClock clock, Action onComplete) { - var instance = new AnimationInstance(animation, control, this, clock, onComplete, DoInterpolation); + var instance = new AnimationInstance( + animation, + control, + this, + clock ?? control.Clock ?? Clock.GlobalClock, + onComplete, + DoInterpolation); return control.Bind((AvaloniaProperty)Property, instance, BindingPriority.Animation); } diff --git a/src/Avalonia.Styling/Styling/Style.cs b/src/Avalonia.Styling/Styling/Style.cs index 62b3ca72ae..067bb59fe9 100644 --- a/src/Avalonia.Styling/Styling/Style.cs +++ b/src/Avalonia.Styling/Styling/Style.cs @@ -122,7 +122,7 @@ namespace Avalonia.Styling obsMatch = Observable.Return(true); } - var sub = animation.Apply(animatable, animatable.Clock ?? Clock.GlobalClock, obsMatch); + var sub = animation.Apply(animatable, null, obsMatch); subs.Add(sub); } } diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 6336c49dc5..721a895900 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -44,7 +44,7 @@ namespace Avalonia.Animation // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock, obsMatch, onComplete); + return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, clock, obsMatch, onComplete); + return childKeyFrames.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); } } } From 6b0ef13027e60cd63131077a6695af14d40cb717 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 15:29:11 -0500 Subject: [PATCH 31/67] Clean up naming in TransformAnimator. --- .../Animation/TransformAnimator.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Visuals/Animation/TransformAnimator.cs b/src/Avalonia.Visuals/Animation/TransformAnimator.cs index 721a895900..64b3ebd626 100644 --- a/src/Avalonia.Visuals/Animation/TransformAnimator.cs +++ b/src/Avalonia.Visuals/Animation/TransformAnimator.cs @@ -9,7 +9,7 @@ namespace Avalonia.Animation /// public class TransformAnimator : Animator { - DoubleAnimator childKeyFrames; + DoubleAnimator childAnimator; /// public override IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete) @@ -36,15 +36,15 @@ namespace Avalonia.Animation var renderTransformType = ctrl.RenderTransform.GetType(); - if (childKeyFrames == null) + if (childAnimator == null) { - InitializeChildKeyFrames(); + InitializeChildAnimator(); } // It's a transform object so let's target that. if (renderTransformType == Property.OwnerType) { - return childKeyFrames.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); + return childAnimator.Apply(animation, ctrl.RenderTransform, clock ?? control.Clock, obsMatch, onComplete); } // It's a TransformGroup and try finding the target there. else if (renderTransformType == typeof(TransformGroup)) @@ -53,7 +53,7 @@ namespace Avalonia.Animation { if (transform.GetType() == Property.OwnerType) { - return childKeyFrames.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); + return childAnimator.Apply(animation, transform, clock ?? control.Clock, obsMatch, onComplete); } } } @@ -73,16 +73,16 @@ namespace Avalonia.Animation return null; } - void InitializeChildKeyFrames() + void InitializeChildAnimator() { - childKeyFrames = new DoubleAnimator(); + childAnimator = new DoubleAnimator(); foreach (AnimatorKeyFrame keyframe in this) { - childKeyFrames.Add(keyframe); + childAnimator.Add(keyframe); } - childKeyFrames.Property = Property; + childAnimator.Property = Property; } /// From 58a85c53c757962040abe68b7f1e00f02b3b50c3 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 16:15:06 -0500 Subject: [PATCH 32/67] Have each AnimationInstance and TransitionInstance use their own internal clock instead of relying on tracking the start time of the global clock. Use a binary search to find the correct keyframe instead of linear search. --- .../ViewModels/AnimationsPageViewModel.cs | 2 +- src/Avalonia.Animation/Animation.cs | 5 -- src/Avalonia.Animation/AnimationInstance`1.cs | 32 +++---- src/Avalonia.Animation/Animator`1.cs | 84 +++++++++++-------- src/Avalonia.Animation/ClockBase.cs | 5 +- src/Avalonia.Animation/IClock.cs | 2 - src/Avalonia.Animation/TransitionInstance.cs | 26 +++--- src/Avalonia.Animation/Transition`1.cs | 3 - 8 files changed, 76 insertions(+), 83 deletions(-) diff --git a/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs b/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs index e4276fa1b5..7b89b7944c 100644 --- a/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs +++ b/samples/RenderDemo/ViewModels/AnimationsPageViewModel.cs @@ -8,7 +8,7 @@ namespace RenderDemo.ViewModels { private bool _isPlaying = true; - private string _playStateText = "Pause all animations"; + private string _playStateText = "Pause animations on this page"; public void TogglePlayState() { diff --git a/src/Avalonia.Animation/Animation.cs b/src/Avalonia.Animation/Animation.cs index 65463f3d52..d7efc69e10 100644 --- a/src/Avalonia.Animation/Animation.cs +++ b/src/Avalonia.Animation/Animation.cs @@ -177,11 +177,6 @@ namespace Avalonia.Animation /// public Task RunAsync(Animatable control, IClock clock = null) { - if (clock == null) - { - clock = Clock.GlobalClock; - } - var run = new TaskCompletionSource(); if (this.RepeatCount == RepeatCount.Loop) diff --git a/src/Avalonia.Animation/AnimationInstance`1.cs b/src/Avalonia.Animation/AnimationInstance`1.cs index 99ebbe752a..468c9c93a4 100644 --- a/src/Avalonia.Animation/AnimationInstance`1.cs +++ b/src/Avalonia.Animation/AnimationInstance`1.cs @@ -19,7 +19,6 @@ namespace Avalonia.Animation private double _currentIteration; private bool _isLooping; private bool _gotFirstKFValue; - private bool _gotFirstFrameCount; private bool _iterationDelay; private FillMode _fillMode; private PlaybackDirection _animationDirection; @@ -29,14 +28,14 @@ namespace Avalonia.Animation private double _speedRatio; private TimeSpan _delay; private TimeSpan _duration; - private TimeSpan _firstFrameCount; private Easings.Easing _easeFunc; private Action _onCompleteAction; private Func _interpolator; private IDisposable _timerSubscription; - private readonly IClock _clock; + private readonly IClock _baseClock; + private IClock _clock; - public AnimationInstance(Animation animation, Animatable control, Animator animator, Clock clock, Action OnComplete, Func Interpolator) + public AnimationInstance(Animation animation, Animatable control, Animator animator, IClock baseClock, Action OnComplete, Func Interpolator) { if (animation.SpeedRatio <= 0) throw new InvalidOperationException("Speed ratio cannot be negative or zero."); @@ -72,16 +71,18 @@ namespace Avalonia.Animation _fillMode = animation.FillMode; _onCompleteAction = OnComplete; _interpolator = Interpolator; - _clock = clock; + _baseClock = baseClock; } protected override void Unsubscribed() { _timerSubscription?.Dispose(); + _clock.PlayState = PlayState.Stop; } protected override void Subscribed() { + _clock = new Clock(_baseClock); _timerSubscription = _clock.Subscribe(Step); } @@ -115,9 +116,9 @@ namespace Avalonia.Animation PublishNext(_lastInterpValue); } - private void DoPlayStatesAndTime(TimeSpan systemTime) + private void DoPlayStates() { - if (_clock.PlayState == PlayState.Stop) + if (_clock.PlayState == PlayState.Stop || _baseClock.PlayState == PlayState.Stop) DoComplete(); if (!_gotFirstKFValue) @@ -125,19 +126,12 @@ namespace Avalonia.Animation _firstKFValue = (T)_parent.First().Value; _gotFirstKFValue = true; } - - if (!_gotFirstFrameCount) - { - _firstFrameCount = systemTime; - _gotFirstFrameCount = true; - } } - private void InternalStep(TimeSpan systemTime) + private void InternalStep(TimeSpan time) { - DoPlayStatesAndTime(systemTime); - - var time = systemTime - _firstFrameCount; + DoPlayStates(); + var delayEndpoint = _delay; var iterationEndpoint = delayEndpoint + _duration; @@ -158,14 +152,14 @@ namespace Avalonia.Animation } //Calculate the current iteration number - _currentIteration = (int)Math.Floor((double)time.Ticks / iterationEndpoint.Ticks) + 2; + _currentIteration = (int)Math.Floor((double)((double)time.Ticks / iterationEndpoint.Ticks)) + 2; } else { return; } - time = TimeSpan.FromTicks(time.Ticks % iterationEndpoint.Ticks); + time = TimeSpan.FromTicks((long)(time.Ticks % iterationEndpoint.Ticks)); if (!_isLooping) { diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index b68f2fc79a..b79e2d9342 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -52,52 +52,72 @@ namespace Avalonia.Animation /// (i.e., the normalized time between the selected keyframes, relative to the /// time parameter). /// - /// The time parameter, relative to the total animation time - protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) + /// The time parameter, relative to the total animation time + protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double animationTime) { - AnimatorKeyFrame firstCue, lastCue ; + AnimatorKeyFrame firstKeyframe, lastKeyframe ; int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { - if (t <= 0.0) + if (animationTime <= 0.0) { - firstCue = _convertedKeyframes[0]; - lastCue = _convertedKeyframes[1]; + firstKeyframe = _convertedKeyframes[0]; + lastKeyframe = _convertedKeyframes[1]; } - else if (t >= 1.0) + else if (animationTime >= 1.0) { - firstCue = _convertedKeyframes[_convertedKeyframes.Count - 2]; - lastCue = _convertedKeyframes[_convertedKeyframes.Count - 1]; + firstKeyframe = _convertedKeyframes[_convertedKeyframes.Count - 2]; + lastKeyframe = _convertedKeyframes[_convertedKeyframes.Count - 1]; } else { - (double time, int index) maxval = (0.0d, 0); - for (int i = 0; i < _convertedKeyframes.Count; i++) - { - var comp = _convertedKeyframes[i].Cue.CueValue; - if (t >= comp) - { - maxval = (comp, i); - } - } - firstCue = _convertedKeyframes[maxval.index]; - lastCue = _convertedKeyframes[maxval.index + 1]; + int index = FindClosestBeforeKeyFrame(animationTime); + firstKeyframe = _convertedKeyframes[index]; + lastKeyframe = _convertedKeyframes[index + 1]; } } else { - firstCue = _convertedKeyframes[0]; - lastCue = _convertedKeyframes[1]; + firstKeyframe = _convertedKeyframes[0]; + lastKeyframe = _convertedKeyframes[1]; } - double t0 = firstCue.Cue.CueValue; - double t1 = lastCue.Cue.CueValue; - var intraframeTime = (t - t0) / (t1 - t0); - var firstFrameData = (firstCue.GetTypedValue(), firstCue.isNeutral); - var lastFrameData = (lastCue.GetTypedValue(), lastCue.isNeutral); + double t0 = firstKeyframe.Cue.CueValue; + double t1 = lastKeyframe.Cue.CueValue; + var intraframeTime = (animationTime - t0) / (t1 - t0); + var firstFrameData = (firstKeyframe.GetTypedValue(), firstKeyframe.isNeutral); + var lastFrameData = (lastKeyframe.GetTypedValue(), lastKeyframe.isNeutral); return (intraframeTime, new KeyFramePair(firstFrameData, lastFrameData)); } + private int FindClosestBeforeKeyFrame(double time) + { + int FindClosestBeforeKeyFrame(int startIndex, int length) + { + if (length == 0 || length == 1) + { + return startIndex; + } + + int middle = startIndex + (length / 2); + + if (_convertedKeyframes[middle].Cue.CueValue < time) + { + return FindClosestBeforeKeyFrame(middle, length - middle); + } + else if (_convertedKeyframes[middle].Cue.CueValue > time) + { + return FindClosestBeforeKeyFrame(startIndex, middle - startIndex); + } + else + { + return middle; + } + } + + return FindClosestBeforeKeyFrame(0, _convertedKeyframes.Count); + } + /// /// Runs the KeyFrames Animation. /// @@ -130,14 +150,6 @@ namespace Avalonia.Animation AddNeutralKeyFramesIfNeeded(); - var copy = _convertedKeyframes.ToList().OrderBy(p => p.Cue.CueValue); - _convertedKeyframes.Clear(); - - foreach (AnimatorKeyFrame keyframe in copy) - { - _convertedKeyframes.Add(keyframe); - } - _isVerifiedAndConverted = true; } @@ -167,7 +179,7 @@ namespace Avalonia.Animation { if (!hasStartKey) { - _convertedKeyframes.Add(new AnimatorKeyFrame(null, new Cue(0.0d)) { Value = default(T), isNeutral = true }); + _convertedKeyframes.Insert(0, new AnimatorKeyFrame(null, new Cue(0.0d)) { Value = default(T), isNeutral = true }); } if (!hasEndKey) diff --git a/src/Avalonia.Animation/ClockBase.cs b/src/Avalonia.Animation/ClockBase.cs index ea784269d9..a2b29e728e 100644 --- a/src/Avalonia.Animation/ClockBase.cs +++ b/src/Avalonia.Animation/ClockBase.cs @@ -21,9 +21,7 @@ namespace Avalonia.Animation _connectedObservable = _observable.Publish().RefCount(); } - public bool HasSubscriptions => _observable.HasSubscriptions; - - public TimeSpan CurrentTime { get; private set; } + protected bool HasSubscriptions => _observable.HasSubscriptions; public PlayState PlayState { get; set; } @@ -47,7 +45,6 @@ namespace Avalonia.Animation } _observable.Pulse(_internalTime); - CurrentTime = _internalTime; if (PlayState == PlayState.Stop) { diff --git a/src/Avalonia.Animation/IClock.cs b/src/Avalonia.Animation/IClock.cs index 58c997841d..ae44102077 100644 --- a/src/Avalonia.Animation/IClock.cs +++ b/src/Avalonia.Animation/IClock.cs @@ -6,8 +6,6 @@ namespace Avalonia.Animation { public interface IClock : IObservable { - bool HasSubscriptions { get; } - TimeSpan CurrentTime { get; } PlayState PlayState { get; set; } } } diff --git a/src/Avalonia.Animation/TransitionInstance.cs b/src/Avalonia.Animation/TransitionInstance.cs index ad87ad7010..eff2c4e9f3 100644 --- a/src/Avalonia.Animation/TransitionInstance.cs +++ b/src/Avalonia.Animation/TransitionInstance.cs @@ -15,23 +15,22 @@ namespace Avalonia.Animation /// internal class TransitionInstance : SingleSubscriberObservableBase { - private IDisposable timerSubscription; - private TimeSpan startTime; - private TimeSpan duration; - private readonly IClock _clock; + private IDisposable _timerSubscription; + private TimeSpan _duration; + private readonly IClock _baseClock; + private IClock _clock; - public TransitionInstance(Clock clock, TimeSpan Duration) + public TransitionInstance(IClock clock, TimeSpan Duration) { - duration = Duration; - _clock = clock; + _duration = Duration; + _baseClock = clock; } private void TimerTick(TimeSpan t) { - var interpVal = (double)(t.Ticks - startTime.Ticks) / duration.Ticks; + var interpVal = (double)t.Ticks / _duration.Ticks; - if (interpVal > 1d - || interpVal < 0d) + if (interpVal > 1d || interpVal < 0d) { PublishCompleted(); return; @@ -42,13 +41,14 @@ namespace Avalonia.Animation protected override void Unsubscribed() { - timerSubscription?.Dispose(); + _timerSubscription?.Dispose(); + _clock.PlayState = PlayState.Stop; } protected override void Subscribed() { - startTime = _clock.CurrentTime; - timerSubscription = _clock.Subscribe(TimerTick); + _clock = new Clock(_baseClock); + _timerSubscription = _clock.Subscribe(TimerTick); PublishNext(0.0d); } } diff --git a/src/Avalonia.Animation/Transition`1.cs b/src/Avalonia.Animation/Transition`1.cs index b54ec8f51c..cd0d5d9ce9 100644 --- a/src/Avalonia.Animation/Transition`1.cs +++ b/src/Avalonia.Animation/Transition`1.cs @@ -14,7 +14,6 @@ namespace Avalonia.Animation public abstract class Transition : AvaloniaObject, ITransition { private AvaloniaProperty _prop; - private Easing _easing; /// /// Gets the duration of the animation. @@ -54,7 +53,5 @@ namespace Avalonia.Animation var transition = DoTransition(new TransitionInstance(clock, Duration), (T)oldValue, (T)newValue); return control.Bind((AvaloniaProperty)Property, transition, Data.BindingPriority.Animation); } - - } } From 3f5ec49b4a32762da011346f0e989cc8970c9524 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 16:36:41 -0500 Subject: [PATCH 33/67] Update iOS and Android projects to use RenderTimers. --- src/Android/Avalonia.Android/AndroidPlatform.cs | 3 ++- ...isplayLinkRenderLoop.cs => DisplayLinkRenderTimer.cs} | 9 +++++---- src/iOS/Avalonia.iOS/iOSPlatform.cs | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) rename src/iOS/Avalonia.iOS/{DisplayLinkRenderLoop.cs => DisplayLinkRenderTimer.cs} (72%) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 2b46bfa492..5f0edadf63 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -52,7 +52,8 @@ namespace Avalonia.Android .Bind().ToTransient() .Bind().ToConstant(Instance) .Bind().ToSingleton() - .Bind().ToConstant(new DefaultRenderLoop(60)) + .Bind().ToConstant(new DefaultRenderTimer(60)) + .Bind().ToConstant(new RenderLoop()) .Bind().ToConstant(new AssetLoader(app.GetType().Assembly)); SkiaPlatform.Initialize(); diff --git a/src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs similarity index 72% rename from src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs rename to src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs index 4f275dd8ea..1357a4f642 100644 --- a/src/iOS/Avalonia.iOS/DisplayLinkRenderLoop.cs +++ b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs @@ -5,11 +5,12 @@ using Foundation; namespace Avalonia.iOS { - class DisplayLinkRenderLoop : IRenderLoop + class DisplayLinkRenderTimer : IRenderTimer { - public event EventHandler Tick; + public event Action Tick; private CADisplayLink _link; - public DisplayLinkRenderLoop() + + public DisplayLinkRenderTimer() { _link = CADisplayLink.Create(OnFrame); @@ -20,7 +21,7 @@ namespace Avalonia.iOS { try { - Tick?.Invoke(this, new EventArgs()); + Tick?.Invoke(Environment.TickCount); } catch (Exception) { diff --git a/src/iOS/Avalonia.iOS/iOSPlatform.cs b/src/iOS/Avalonia.iOS/iOSPlatform.cs index abaebca489..7b50040bf2 100644 --- a/src/iOS/Avalonia.iOS/iOSPlatform.cs +++ b/src/iOS/Avalonia.iOS/iOSPlatform.cs @@ -41,7 +41,8 @@ namespace Avalonia.iOS .Bind().ToConstant(PlatformThreadingInterface.Instance) .Bind().ToSingleton() .Bind().ToSingleton() - .Bind().ToSingleton(); + .Bind().ToSingleton() + .Bind().ToSingleton(); } } } From 8357bc86b02980a13a597d52cf72c7e5798b6755 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 18:10:03 -0500 Subject: [PATCH 34/67] Add tests for the new RenderLoop logic. --- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 16 +-- .../Rendering/DeferredRendererTests.cs | 64 ++++------ .../Rendering/RenderLoopTests.cs | 119 ++++++++++++++++++ 3 files changed, 146 insertions(+), 53 deletions(-) create mode 100644 tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index a850b99c5e..d920be2706 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; using Avalonia.Logging; using Avalonia.Threading; @@ -89,18 +90,7 @@ namespace Avalonia.Rendering { try { - var needsUpdate = false; - - foreach (var i in _items) - { - if (i.NeedsUpdate) - { - needsUpdate = true; - break; - } - } - - if (needsUpdate) + if (_items.Any(item => item.NeedsUpdate)) { await _dispatcher.InvokeAsync(() => { @@ -108,7 +98,7 @@ namespace Avalonia.Rendering { i.Update(tickCount); } - }).ConfigureAwait(false); + }, DispatcherPriority.Render).ConfigureAwait(false); } foreach (var i in _items) diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index 1af9a9499d..e2a5c0c54c 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Subjects; - +using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Media; @@ -22,27 +22,9 @@ namespace Avalonia.Visuals.UnitTests.Rendering { public class DeferredRendererTests { - [Fact] - public void First_Frame_Calls_UpdateScene_On_Dispatcher() - { - var root = new TestRoot(); - - var dispatcher = new Mock(); - dispatcher.Setup(x => x.Post(It.IsAny(), DispatcherPriority.Render)) - .Callback((a, p) => a()); - - CreateTargetAndRunFrame(root, dispatcher: dispatcher.Object); - - dispatcher.Verify(x => - x.Post( - It.Is(a => a.Method.Name == "UpdateScene"), - DispatcherPriority.Render)); - } - [Fact] public void First_Frame_Calls_SceneBuilder_UpdateAll() { - var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -54,6 +36,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Frame_Does_Not_Call_SceneBuilder_If_No_Dirty_Controls() { + var dispatcher = new ImmediateDispatcher(); var loop = new Mock(); var root = new TestRoot(); var sceneBuilder = MockSceneBuilder(root); @@ -63,8 +46,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering sceneBuilder: sceneBuilder.Object); target.Start(); - IgnoreFirstFrame(loop, sceneBuilder); - RunFrame(loop); + IgnoreFirstFrame(target, sceneBuilder); + RunFrame(target); sceneBuilder.Verify(x => x.UpdateAll(It.IsAny()), Times.Never); sceneBuilder.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); @@ -73,8 +56,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Update_Dirty_Controls_In_Order() { - var loop = new Mock(); var dispatcher = new ImmediateDispatcher(); + var loop = new Mock(); Border border; Decorator decorator; @@ -98,7 +81,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering dispatcher: dispatcher); target.Start(); - IgnoreFirstFrame(loop, sceneBuilder); + IgnoreFirstFrame(target, sceneBuilder); target.AddDirty(border); target.AddDirty(canvas); target.AddDirty(root); @@ -108,7 +91,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering sceneBuilder.Setup(x => x.Update(It.IsAny(), It.IsAny())) .Callback((_, v) => result.Add(v)); - RunFrame(loop); + RunFrame(target); Assert.Equal(new List { root, decorator, border, canvas }, result); } @@ -198,7 +181,6 @@ namespace Avalonia.Visuals.UnitTests.Rendering [Fact] public void Should_Create_Layer_For_Root() { - var loop = new Mock(); var root = new TestRoot(); var rootLayer = new Mock(); @@ -239,19 +221,19 @@ namespace Avalonia.Visuals.UnitTests.Rendering root.Measure(Size.Infinity); root.Arrange(new Rect(root.DesiredSize)); - var loop = new Mock(); - var target = CreateTargetAndRunFrame(root, loop: loop); + var timer = new Mock(); + var target = CreateTargetAndRunFrame(root, timer); Assert.Equal(new[] { root }, target.Layers.Select(x => x.LayerRoot)); var animation = new BehaviorSubject(0.5); border.Bind(Border.OpacityProperty, animation, BindingPriority.Animation); - RunFrame(loop); + RunFrame(target); Assert.Equal(new IVisual[] { root, border }, target.Layers.Select(x => x.LayerRoot)); animation.OnCompleted(); - RunFrame(loop); + RunFrame(target); Assert.Equal(new[] { root }, target.Layers.Select(x => x.LayerRoot)); } @@ -280,8 +262,8 @@ namespace Avalonia.Visuals.UnitTests.Rendering root.Measure(Size.Infinity); root.Arrange(new Rect(root.DesiredSize)); - var loop = new Mock(); - var target = CreateTargetAndRunFrame(root, loop: loop); + var timer = new Mock(); + var target = CreateTargetAndRunFrame(root, timer); Assert.Single(target.Layers); } @@ -345,19 +327,20 @@ namespace Avalonia.Visuals.UnitTests.Rendering private DeferredRenderer CreateTargetAndRunFrame( TestRoot root, - Mock loop = null, + Mock timer = null, ISceneBuilder sceneBuilder = null, IDispatcher dispatcher = null) { - loop = loop ?? new Mock(); + timer = timer ?? new Mock(); + dispatcher = dispatcher ?? new ImmediateDispatcher(); var target = new DeferredRenderer( root, - loop.Object, + new RenderLoop(timer.Object, dispatcher), sceneBuilder: sceneBuilder, - dispatcher: dispatcher ?? new ImmediateDispatcher()); + dispatcher: dispatcher); root.Renderer = target; target.Start(); - RunFrame(loop); + RunFrame(target); return target; } @@ -366,15 +349,16 @@ namespace Avalonia.Visuals.UnitTests.Rendering return Mock.Get(renderer.Layers[layerRoot].Bitmap.Item.CreateDrawingContext(null)); } - private void IgnoreFirstFrame(Mock loop, Mock sceneBuilder) + private void IgnoreFirstFrame(IRenderLoopTask task, Mock sceneBuilder) { - RunFrame(loop); + RunFrame(task); sceneBuilder.ResetCalls(); } - private void RunFrame(Mock loop) + private void RunFrame(IRenderLoopTask task) { - //loop.Raise(x => x.Tick += null, EventArgs.Empty); + task.Update(0); + task.Render(); } private IRenderTargetBitmapImpl CreateLayer() diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs new file mode 100644 index 0000000000..30ef35a2bb --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Avalonia.Rendering; +using Avalonia.Threading; +using Moq; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Rendering +{ + public class RenderLoopTests + { + [Fact] + public void RenderLoop_Update_Runs_On_Dispatcher() + { + var dispatcher = new Mock(); + + bool inDispatcher = false; + + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => + { + inDispatcher = true; + a(); + inDispatcher = false; + }) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + + var loop = new RenderLoop(timer.Object, dispatcher.Object); + + var renderTask = new Mock(); + + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + renderTask.Setup(t => t.Update(It.IsAny())) + .Callback((long _) => Assert.True(inDispatcher)); + + loop.Add(renderTask.Object); + + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + } + + [Fact] + public void RenderLoop_Does_Not_Update_When_No_Tasks_Need_Update() + { + var dispatcher = new Mock(); + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => a()) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + var renderTask = new Mock(); + renderTask.Setup(t => t.NeedsUpdate).Returns(false); + + loop.Add(renderTask.Object); + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); + } + + [Fact] + public void RenderLoop_Render_Runs_Off_Dispatcher() + { + var dispatcher = new Mock(); + bool inDispatcher = false; + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => + { + inDispatcher = true; + a(); + inDispatcher = false; + }) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + + var renderTask = new Mock(); + + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + renderTask.Setup(t => t.Render()) + .Callback(() => Assert.False(inDispatcher)); + + loop.Add(renderTask.Object); + timer.Raise(t => t.Tick += null, 0L); + + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + } + + [Fact] + public void RenderLoop_Passes_Tick_Count_To_Update() + { + var dispatcher = new Mock(); + dispatcher.Setup( + d => d.InvokeAsync(It.IsAny(), DispatcherPriority.Render)) + .Callback((Action a, DispatcherPriority _) => a()) + .Returns(Task.CompletedTask); + + var timer = new Mock(); + var loop = new RenderLoop(timer.Object, dispatcher.Object); + var renderTask = new Mock(); + renderTask.Setup(t => t.NeedsUpdate).Returns(true); + + loop.Add(renderTask.Object); + var tickCount = 12345L; + timer.Raise(t => t.Tick += null, tickCount); + + renderTask.Verify(t => t.Update(tickCount), Times.Once()); + } + } +} From ce95625d66509c15bb9f20607596eff900eef3a7 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Fri, 7 Sep 2018 18:45:06 -0500 Subject: [PATCH 35/67] Only add the clock to the render loop if there is a render loop. --- src/Avalonia.Controls/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 8c03bac61a..586a73b75c 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -341,7 +341,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable .Bind().ToConstant(clock) - .GetService().Add(clock); + .GetService()?.Add(clock); } } } From afdfb28da8b507370771438133c9e0a4916ea010 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 02:02:50 +0200 Subject: [PATCH 36/67] Added failing tests for brush invalidation. --- .../BorderTests.cs | 21 ++++++++ .../Avalonia.Controls.UnitTests/PanelTests.cs | 21 ++++++++ .../ContentPresenterTests_Standalone.cs | 20 +++++++- .../Shapes/RectangleTests.cs | 48 +++++++++++++++++++ .../TextBlockTests.cs | 38 +++++++++++++++ tests/Avalonia.UnitTests/TestRoot.cs | 7 +++ 6 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs diff --git a/tests/Avalonia.Controls.UnitTests/BorderTests.cs b/tests/Avalonia.Controls.UnitTests/BorderTests.cs index 9a6a041ec7..0ac9392bc6 100644 --- a/tests/Avalonia.Controls.UnitTests/BorderTests.cs +++ b/tests/Avalonia.Controls.UnitTests/BorderTests.cs @@ -1,6 +1,10 @@ // Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -42,5 +46,22 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new Rect(6, 6, 0, 0), content.Bounds); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new Border() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.Controls.UnitTests/PanelTests.cs b/tests/Avalonia.Controls.UnitTests/PanelTests.cs index ed239120d6..4a404ea97e 100644 --- a/tests/Avalonia.Controls.UnitTests/PanelTests.cs +++ b/tests/Avalonia.Controls.UnitTests/PanelTests.cs @@ -3,7 +3,11 @@ using System.Linq; using Avalonia.LogicalTree; +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; using Avalonia.VisualTree; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -115,5 +119,22 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(new[] { child2, child1 }, panel.GetLogicalChildren()); Assert.Equal(new[] { child2, child1 }, panel.GetVisualChildren()); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new Panel() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs index 6716456c78..9d65f2cba7 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs @@ -13,6 +13,7 @@ using System; using System.Linq; using Xunit; using Avalonia.Rendering; +using Avalonia.Media; namespace Avalonia.Controls.UnitTests.Presenters { @@ -203,5 +204,22 @@ namespace Avalonia.Controls.UnitTests.Presenters Assert.NotEqual(foo, logicalChildren.First()); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new ContentPresenter() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } -} \ No newline at end of file +} diff --git a/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs b/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs new file mode 100644 index 0000000000..0ec73edec0 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/Shapes/RectangleTests.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Controls.Shapes; +using Avalonia.Media; +using Avalonia.UnitTests; +using Moq; +using Xunit; + +namespace Avalonia.Controls.UnitTests.Shapes +{ + public class RectangleTests + { + [Fact] + public void Changing_Fill_Brush_Color_Should_Invalidate_Visual() + { + var target = new Rectangle() + { + Fill = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Fill).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + + [Fact] + public void Changing_Stroke_Brush_Color_Should_Invalidate_Visual() + { + var target = new Rectangle() + { + Stroke = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Stroke).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + } +} diff --git a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs index 9a1140fc05..45e683455b 100644 --- a/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs +++ b/tests/Avalonia.Controls.UnitTests/TextBlockTests.cs @@ -2,6 +2,10 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using Avalonia.Data; +using Avalonia.Media; +using Avalonia.Rendering; +using Avalonia.UnitTests; +using Moq; using Xunit; namespace Avalonia.Controls.UnitTests @@ -25,5 +29,39 @@ namespace Avalonia.Controls.UnitTests "", textBlock.Text); } + + [Fact] + public void Changing_Background_Brush_Color_Should_Invalidate_Visual() + { + var target = new TextBlock() + { + Background = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Background).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } + + [Fact] + public void Changing_Foreground_Brush_Color_Should_Invalidate_Visual() + { + var target = new TextBlock() + { + Foreground = new SolidColorBrush(Colors.Red), + }; + + var root = new TestRoot(target); + var renderer = Mock.Get(root.Renderer); + renderer.ResetCalls(); + + ((SolidColorBrush)target.Foreground).Color = Colors.Green; + + renderer.Verify(x => x.AddDirty(target), Times.Once); + } } } diff --git a/tests/Avalonia.UnitTests/TestRoot.cs b/tests/Avalonia.UnitTests/TestRoot.cs index 884df33fc0..972b1d78c0 100644 --- a/tests/Avalonia.UnitTests/TestRoot.cs +++ b/tests/Avalonia.UnitTests/TestRoot.cs @@ -19,6 +19,13 @@ namespace Avalonia.UnitTests public TestRoot() { + Renderer = Mock.Of(); + } + + public TestRoot(IControl child) + : this() + { + Child = child; } event EventHandler INameScope.Registered From a5a5b36ddc2f8a1334bc152438600f0eb93a7912 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 02:06:30 +0200 Subject: [PATCH 37/67] Invalidate controls when brush changed. --- src/Avalonia.Controls/Border.cs | 3 +- src/Avalonia.Controls/Panel.cs | 1 + .../Presenters/ContentPresenter.cs | 3 +- src/Avalonia.Controls/Shapes/Shape.cs | 4 +- src/Avalonia.Controls/TextBlock.cs | 2 +- src/Avalonia.Visuals/Visual.cs | 40 +++++++++++++++++++ 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/Border.cs b/src/Avalonia.Controls/Border.cs index 5f84421c64..c4bc121a27 100644 --- a/src/Avalonia.Controls/Border.cs +++ b/src/Avalonia.Controls/Border.cs @@ -43,8 +43,9 @@ namespace Avalonia.Controls /// static Border() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty); + BrushAffectsRender(BackgroundProperty, BorderBrushProperty); } /// diff --git a/src/Avalonia.Controls/Panel.cs b/src/Avalonia.Controls/Panel.cs index c0d211effb..9b768749df 100644 --- a/src/Avalonia.Controls/Panel.cs +++ b/src/Avalonia.Controls/Panel.cs @@ -30,6 +30,7 @@ namespace Avalonia.Controls /// static Panel() { + BrushAffectsRender(BackgroundProperty); ClipToBoundsProperty.OverrideDefaultValue(true); } diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index 6badf91367..6c1d6e2cc1 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -90,8 +90,9 @@ namespace Avalonia.Controls.Presenters /// static ContentPresenter() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty, PaddingProperty); + BrushAffectsRender(BackgroundProperty, BorderBrushProperty); ContentProperty.Changed.AddClassHandler(x => x.ContentChanged); ContentTemplateProperty.Changed.AddClassHandler(x => x.ContentChanged); TemplatedParentProperty.Changed.AddClassHandler(x => x.TemplatedParentChanged); diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index 604051ef28..d2a4a37531 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -30,11 +30,11 @@ namespace Avalonia.Controls.Shapes private Geometry _renderedGeometry; bool _calculateTransformOnArrange = false; - static Shape() { AffectsMeasure(StretchProperty, StrokeThicknessProperty); - AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); + AffectsRender(StrokeDashArrayProperty); + BrushAffectsRender(FillProperty, StrokeProperty); } public Geometry DefiningGeometry diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index e91d2e8fa7..8689d11e13 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -99,10 +99,10 @@ namespace Avalonia.Controls static TextBlock() { ClipToBoundsProperty.OverrideDefaultValue(true); - AffectsRender(ForegroundProperty); AffectsRender(FontWeightProperty); AffectsRender(FontSizeProperty); AffectsRender(FontStyleProperty); + BrushAffectsRender(BackgroundProperty, ForegroundProperty); } /// diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 81e1a93a6f..8631d2001d 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -328,6 +328,44 @@ namespace Avalonia } } + /// + /// Indicates that a brush property change should cause to be + /// called. + /// + /// The properties. + /// + /// This method should be called in a control's static constructor with each property + /// on the control which when changed should cause a redraw. It not only triggers an + /// invalidation when the property itself changes, but also when the brush raises + /// the event. + /// + protected static void BrushAffectsRender(params AvaloniaProperty[] properties) + where T : Visual + { + void Invalidate(AvaloniaPropertyChangedEventArgs e) + { + if (e.Sender is T sender) + { + if (e.OldValue is IMutableBrush oldValue) + { + oldValue.Changed -= sender.BrushChanged; + } + + if (e.NewValue is IMutableBrush newValue) + { + newValue.Changed += sender.BrushChanged; + } + + sender.InvalidateVisual(); + } + } + + foreach (var property in properties) + { + property.Changed.Subscribe(Invalidate); + } + } + /// /// Calls the method /// for this control and all of its visual descendants. @@ -530,6 +568,8 @@ namespace Avalonia OnVisualParentChanged(old, value); } + private void BrushChanged(object sender, EventArgs e) => InvalidateVisual(); + /// /// Called when the collection changes. /// From 460d63736da2bd98f5b216b4a1860aa0354ff53f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 9 Sep 2018 03:17:28 +0200 Subject: [PATCH 38/67] Use immutable brush as default Foreground value. Prevents memory leak. --- src/Avalonia.Controls/TextBlock.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 8689d11e13..ee3e3e361a 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -6,6 +6,7 @@ using System.Reactive; using System.Reactive.Linq; using Avalonia.LogicalTree; using Avalonia.Media; +using Avalonia.Media.Immutable; using Avalonia.Metadata; namespace Avalonia.Controls @@ -65,7 +66,7 @@ namespace Avalonia.Controls public static readonly AttachedProperty ForegroundProperty = AvaloniaProperty.RegisterAttached( nameof(Foreground), - new SolidColorBrush(0xff000000), + Brushes.Black, inherits: true); /// From ec695433dec3a6b66967f692b66538e95366a842 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Mon, 10 Sep 2018 01:02:35 +0800 Subject: [PATCH 39/67] Cancel an animation instance when the selector turns false. --- src/Avalonia.Animation/Animator`1.cs | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index f0ef55aa9e..74f09d5488 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -37,12 +37,29 @@ namespace Avalonia.Animation if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - return match + var matchStream = match + .DistinctUntilChanged() + .Publish() + .RefCount(); + + var activeInstance = matchStream .Where(p => p) - .Subscribe(_ => - { - var timerObs = RunKeyFrames(animation, control, onComplete); - }); + .Select(p => RunKeyFrames(animation, control, onComplete)); + + var negationStream = matchStream + .Where(p => !p); + + return Observable + .WithLatestFrom( + negationStream, + activeInstance, + (isMatch, instance) => + { + if (!isMatch && animation.RepeatCount.IsLoop) + instance?.Dispose(); + return true; + }) + .Subscribe(); } /// From 1bf495433469fdfdfdb9a81582fd8518f5996b27 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Mon, 10 Sep 2018 10:50:36 +0200 Subject: [PATCH 40/67] Downgrade SharpDX to 4.0.1 --- build/SharpDX.props | 10 ++--- .../Media/FormattedTextImpl.cs | 43 +++++++------------ 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/build/SharpDX.props b/build/SharpDX.props index fdc802f135..69aa817a01 100644 --- a/build/SharpDX.props +++ b/build/SharpDX.props @@ -1,9 +1,9 @@  - - - - - + + + + + diff --git a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs index e2c33a51f1..7164ec7c0d 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs @@ -9,8 +9,6 @@ using DWrite = SharpDX.DirectWrite; namespace Avalonia.Direct2D1.Media { - using System; - public class FormattedTextImpl : IFormattedTextImpl { public FormattedTextImpl( @@ -23,22 +21,21 @@ namespace Avalonia.Direct2D1.Media { Text = text; - var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface); - - textFormat.WordWrapping = - wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap; - - TextLayout = new DWrite.TextLayout( - Direct2D1Platform.DirectWriteFactory, - Text ?? string.Empty, - textFormat, - (float)constraint.Width, - (float)constraint.Height) + using (var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface)) { - TextAlignment = textAlignment.ToDirect2D() - }; - - textFormat.Dispose(); + textFormat.WordWrapping = + wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap; + + TextLayout = new DWrite.TextLayout( + Direct2D1Platform.DirectWriteFactory, + Text ?? string.Empty, + textFormat, + (float)constraint.Width, + (float)constraint.Height) + { + TextAlignment = textAlignment.ToDirect2D() + }; + } if (spans != null) { @@ -109,17 +106,7 @@ namespace Avalonia.Direct2D1.Media private Size Measure() { - DWrite.TextMetrics metrics; - - // SharpDX bug - try - { - metrics = TextLayout.Metrics; - } - catch (ObjectDisposedException) - { - metrics = TextLayout.Metrics; - } + var metrics = TextLayout.Metrics; var width = metrics.WidthIncludingTrailingWhitespace; From 50f15b1154a28e58ce40253554df2111adec4440 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Mon, 10 Sep 2018 10:54:31 +0200 Subject: [PATCH 41/67] Namespace fix --- .../Media/Imaging/D2DBitmapImpl.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs index b65e32bbbb..b0edb9c3bf 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/Imaging/D2DBitmapImpl.cs @@ -1,14 +1,13 @@ -using System; +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; using System.IO; -using SharpDX.Direct2D1; +using SharpDX.WIC; +using Bitmap = SharpDX.Direct2D1.Bitmap; namespace Avalonia.Direct2D1.Media { - using SharpDX.WIC; - - using Bitmap = SharpDX.Direct2D1.Bitmap; - using PixelFormat = SharpDX.Direct2D1.PixelFormat; - /// /// A Direct2D Bitmap implementation that uses a GPU memory bitmap as its image. /// From e625f03c1659a76fbd5d601de75a2ff25d68c953 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Mon, 10 Sep 2018 20:24:26 +0200 Subject: [PATCH 42/67] Use ID2D1Device instead of ID2D1Device1 to keep wi --- src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs index 38c49924bc..c330377e20 100644 --- a/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs +++ b/src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs @@ -33,7 +33,7 @@ namespace Avalonia.Direct2D1 public static SharpDX.Direct2D1.Factory1 Direct2D1Factory { get; private set; } - public static SharpDX.Direct2D1.Device1 Direct2D1Device { get; private set; } + public static SharpDX.Direct2D1.Device Direct2D1Device { get; private set; } public static SharpDX.DirectWrite.Factory1 DirectWriteFactory { get; private set; } @@ -97,10 +97,7 @@ namespace Avalonia.Direct2D1 DxgiDevice = Direct3D11Device.QueryInterface(); - using (var device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice)) - { - Direct2D1Device = device.QueryInterface(); - } + Direct2D1Device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice); s_initialized = true; } From 64a4a6d82af01a3ba2c69182b2fcd16c33ddc153 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Tue, 11 Sep 2018 15:04:12 +0800 Subject: [PATCH 43/67] Simplify Fix; Invalidate when IsIndeterminate property changes. --- src/Avalonia.Animation/Animator`1.cs | 39 ++++++++++------------------ src/Avalonia.Controls/ProgressBar.cs | 8 +++++- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index 74f09d5488..ab82bfb35d 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -17,7 +17,7 @@ namespace Avalonia.Animation /// List of type-converted keyframes. /// private readonly List _convertedKeyframes = new List(); - + private bool _isVerifiedAndConverted; /// @@ -28,38 +28,25 @@ namespace Avalonia.Animation public Animator() { // Invalidate keyframes when changed. - this.CollectionChanged += delegate { _isVerifiedAndConverted = false; }; + this.CollectionChanged += delegate { _isVerifiedAndConverted = false; }; } /// public virtual IDisposable Apply(Animation animation, Animatable control, IObservable match, Action onComplete) { - if (!_isVerifiedAndConverted) + if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - var matchStream = match - .DistinctUntilChanged() - .Publish() - .RefCount(); - - var activeInstance = matchStream - .Where(p => p) - .Select(p => RunKeyFrames(animation, control, onComplete)); - - var negationStream = matchStream - .Where(p => !p); - - return Observable - .WithLatestFrom( - negationStream, - activeInstance, - (isMatch, instance) => + return match + .DistinctUntilChanged() + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) + .Buffer(2, 1) + .Where(x => x.Count > 1) + .Subscribe(x => { - if (!isMatch && animation.RepeatCount.IsLoop) - instance?.Dispose(); - return true; - }) - .Subscribe(); + if (animation.RepeatCount.IsLoop) + x[0]?.Dispose(); + }); } /// @@ -72,7 +59,7 @@ namespace Avalonia.Animation /// The time parameter, relative to the total animation time protected (double IntraKFTime, KeyFramePair KFPair) GetKFPairAndIntraKFTime(double t) { - AnimatorKeyFrame firstCue, lastCue ; + AnimatorKeyFrame firstCue, lastCue; int kvCount = _convertedKeyframes.Count; if (kvCount > 2) { diff --git a/src/Avalonia.Controls/ProgressBar.cs b/src/Avalonia.Controls/ProgressBar.cs index 7f4b549849..fe7b8e64c7 100644 --- a/src/Avalonia.Controls/ProgressBar.cs +++ b/src/Avalonia.Controls/ProgressBar.cs @@ -38,6 +38,7 @@ namespace Avalonia.Controls PseudoClass(IsIndeterminateProperty, ":indeterminate"); ValueProperty.Changed.AddClassHandler(x => x.ValueChanged); + IsIndeterminateProperty.Changed.AddClassHandler(x => x.IsIndeterminateChanged); } public bool IsIndeterminate @@ -118,5 +119,10 @@ namespace Avalonia.Controls { UpdateIndicator(Bounds.Size); } + + private void IsIndeterminateChanged(AvaloniaPropertyChangedEventArgs e) + { + UpdateIndicator(Bounds.Size); + } } -} +} \ No newline at end of file From 388df18e52efa177e694156607e369390e9de22e Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 7 Sep 2018 13:58:02 +0200 Subject: [PATCH 44/67] Initial commit of Event Viewer. --- .../Avalonia.Diagnostics.csproj | 8 ++ src/Avalonia.Diagnostics/DevTools.xaml | 1 + src/Avalonia.Diagnostics/DevTools.xaml.cs | 23 +++++ src/Avalonia.Diagnostics/Models/ChainLink.cs | 34 +++++++ .../ViewModels/ControlTreeNode.cs | 65 +++++++++++++ .../ViewModels/DevToolsViewModel.cs | 5 + .../ViewModels/EventEntryTreeNode.cs | 97 +++++++++++++++++++ .../ViewModels/EventTreeNode.cs | 80 +++++++++++++++ .../ViewModels/EventsViewModel.cs | 75 ++++++++++++++ .../ViewModels/FiredEvent.cs | 93 ++++++++++++++++++ .../Views/EventsView.xaml | 53 ++++++++++ .../Views/EventsView.xaml.cs | 28 ++++++ 12 files changed, 562 insertions(+) create mode 100644 src/Avalonia.Diagnostics/Models/ChainLink.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs create mode 100644 src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs create mode 100644 src/Avalonia.Diagnostics/Views/EventsView.xaml create mode 100644 src/Avalonia.Diagnostics/Views/EventsView.xaml.cs diff --git a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj index a2ff0ecf02..b807571a38 100644 --- a/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj +++ b/src/Avalonia.Diagnostics/Avalonia.Diagnostics.csproj @@ -2,6 +2,9 @@ netstandard2.0 + + + @@ -17,4 +20,9 @@ + + + MSBuild:Compile + + \ No newline at end of file diff --git a/src/Avalonia.Diagnostics/DevTools.xaml b/src/Avalonia.Diagnostics/DevTools.xaml index 844670e794..a3b12f0ec5 100644 --- a/src/Avalonia.Diagnostics/DevTools.xaml +++ b/src/Avalonia.Diagnostics/DevTools.xaml @@ -3,6 +3,7 @@ + diff --git a/src/Avalonia.Diagnostics/DevTools.xaml.cs b/src/Avalonia.Diagnostics/DevTools.xaml.cs index be0863954a..d084ec3014 100644 --- a/src/Avalonia.Diagnostics/DevTools.xaml.cs +++ b/src/Avalonia.Diagnostics/DevTools.xaml.cs @@ -10,6 +10,7 @@ using Avalonia.Input; using Avalonia.Input.Raw; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; +using Avalonia.Rendering; using Avalonia.VisualTree; namespace Avalonia @@ -28,6 +29,7 @@ namespace Avalonia.Diagnostics public class DevTools : UserControl { private static Dictionary s_open = new Dictionary(); + private static HashSet s_visualTreeRoots = new HashSet(); private IDisposable _keySubscription; public DevTools(IControl root) @@ -79,6 +81,7 @@ namespace Avalonia.Diagnostics devToolsWindow.Closed += devTools.DevToolsClosed; s_open.Add(control, devToolsWindow); + MarkAsDevTool(devToolsWindow); devToolsWindow.Show(); } } @@ -89,6 +92,7 @@ namespace Avalonia.Diagnostics var devToolsWindow = (Window)sender; var devTools = (DevTools)devToolsWindow.Content; s_open.Remove((TopLevel)devTools.Root); + RemoveDevTool(devToolsWindow); _keySubscription.Dispose(); devToolsWindow.Closed -= DevToolsClosed; } @@ -116,5 +120,24 @@ namespace Avalonia.Diagnostics } } } + + /// + /// Marks a visual as part of the DevTools, so it can be excluded from event tracking. + /// + /// The visual whose root is to be marked. + public static void MarkAsDevTool(IVisual visual) + { + s_visualTreeRoots.Add(visual.GetVisualRoot()); + } + + public static void RemoveDevTool(IVisual visual) + { + s_visualTreeRoots.Remove(visual.GetVisualRoot()); + } + + public static bool BelongsToDevTool(IVisual visual) + { + return s_visualTreeRoots.Contains(visual.GetVisualRoot()); + } } } diff --git a/src/Avalonia.Diagnostics/Models/ChainLink.cs b/src/Avalonia.Diagnostics/Models/ChainLink.cs new file mode 100644 index 0000000000..9ea99ff1ae --- /dev/null +++ b/src/Avalonia.Diagnostics/Models/ChainLink.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.Models +{ + internal class ChainLink + { + public object Handler { get; private set; } + public string HandlerName + { + get + { + if (Handler is INamed named && !string.IsNullOrEmpty(named.Name)) + { + return named.Name + " (" + Handler.GetType().Name + ")"; + } + return Handler.GetType().Name; + } + } + public bool Handled { get; private set; } + public RoutingStrategies Route { get; private set; } + + public ChainLink(object handler, bool handled, RoutingStrategies route) + { + Contract.Requires(handler != null); + + this.Handler = handler; + this.Handled = handled; + this.Route = route; + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs new file mode 100644 index 0000000000..d8a6a3bdc3 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/ControlTreeNode.cs @@ -0,0 +1,65 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class ControlTreeNode : EventTreeNode + { + public ControlTreeNode(Type type, IEnumerable events, EventsViewModel vm) + : base(null, type.Name) + { + this.Children = new AvaloniaList(events.OrderBy(e => e.Name).Select(e => new EventEntryTreeNode(this, e, vm) { IsEnabled = IsDefault(e) })); + this.IsExpanded = true; + } + + RoutedEvent[] defaultEvents = new RoutedEvent[] + { + Button.ClickEvent, + InputElement.KeyDownEvent, + InputElement.KeyUpEvent, + InputElement.TextInputEvent, + InputElement.PointerReleasedEvent, + InputElement.PointerPressedEvent, + }; + + private bool IsDefault(RoutedEvent e) + { + return defaultEvents.Contains(e); + } + + public override bool? IsEnabled + { + get => base.IsEnabled; + set + { + if (base.IsEnabled != value) + { + base.IsEnabled = value; + if (_updateChildren && value != null) + { + foreach (var child in Children) + { + try + { + child._updateParent = false; + child.IsEnabled = value; + } + finally + { + child._updateParent = true; + } + } + } + } + } + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs index ce8ad36c17..c6d3f02e8b 100644 --- a/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs +++ b/src/Avalonia.Diagnostics/ViewModels/DevToolsViewModel.cs @@ -14,6 +14,7 @@ namespace Avalonia.Diagnostics.ViewModels private int _selectedTab; private TreePageViewModel _logicalTree; private TreePageViewModel _visualTree; + private EventsViewModel _eventsView; private string _focusedControl; private string _pointerOverElement; @@ -21,6 +22,7 @@ namespace Avalonia.Diagnostics.ViewModels { _logicalTree = new TreePageViewModel(LogicalTreeNode.Create(root)); _visualTree = new TreePageViewModel(VisualTreeNode.Create(root)); + _eventsView = new EventsViewModel(root); UpdateFocusedControl(); KeyboardDevice.Instance.PropertyChanged += (s, e) => @@ -57,6 +59,9 @@ namespace Avalonia.Diagnostics.ViewModels case 1: Content = _visualTree; break; + case 2: + Content = _eventsView; + break; } RaisePropertyChanged(); diff --git a/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs new file mode 100644 index 0000000000..8eead869d5 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventEntryTreeNode.cs @@ -0,0 +1,97 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using Avalonia.Diagnostics.Models; +using Avalonia.Interactivity; +using Avalonia.Threading; +using Avalonia.VisualTree; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class EventEntryTreeNode : EventTreeNode + { + RoutedEvent _event; + EventsViewModel _parentViewModel; + bool _isRegistered; + FiredEvent _currentEvent; + + public EventEntryTreeNode(ControlTreeNode parent, RoutedEvent @event, EventsViewModel vm) + : base(parent, @event.Name) + { + Contract.Requires(@event != null); + Contract.Requires(vm != null); + + this._event = @event; + this._parentViewModel = vm; + } + + public override bool? IsEnabled + { + get => base.IsEnabled; + set + { + if (base.IsEnabled != value) + { + base.IsEnabled = value; + UpdateTracker(); + if (Parent != null && _updateParent) + { + try + { + Parent._updateChildren = false; + Parent.UpdateChecked(); + } + finally + { + Parent._updateChildren = true; + } + } + } + } + } + + private void UpdateTracker() + { + if (IsEnabled.GetValueOrDefault() && !_isRegistered) + { + _event.AddClassHandler(typeof(object), HandleEvent, (RoutingStrategies)7, handledEventsToo: true); + _isRegistered = true; + } + } + + private void HandleEvent(object sender, RoutedEventArgs e) + { + if (!_isRegistered || IsEnabled == false) + return; + if (sender is IVisual v && DevTools.BelongsToDevTool(v)) + return; + + var s = sender; + var handled = e.Handled; + var route = e.Route; + + Action handler = delegate + { + if (_currentEvent == null || !_currentEvent.IsPartOfSameEventChain(e)) + { + _currentEvent = new FiredEvent(e, new ChainLink(s, handled, route)); + + _parentViewModel.RecordedEvents.Add(_currentEvent); + + while (_parentViewModel.RecordedEvents.Count > 100) + _parentViewModel.RecordedEvents.RemoveAt(0); + } + else + { + _currentEvent.AddToChain(new ChainLink(s, handled, route)); + } + }; + + if (!Dispatcher.UIThread.CheckAccess()) + Dispatcher.UIThread.Post(handler); + else + handler(); + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs new file mode 100644 index 0000000000..50ea0c9c31 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventTreeNode.cs @@ -0,0 +1,80 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Diagnostics; +using Avalonia.Collections; +using Avalonia.VisualTree; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal abstract class EventTreeNode : ViewModelBase + { + internal bool _updateChildren = true; + internal bool _updateParent = true; + private bool _isExpanded; + private bool? _isEnabled = false; + + public EventTreeNode(EventTreeNode parent, string text) + { + this.Parent = parent; + this.Text = text; + } + + public IAvaloniaReadOnlyList Children + { + get; + protected set; + } + + public bool IsExpanded + { + get { return _isExpanded; } + set { RaiseAndSetIfChanged(ref _isExpanded, value); } + } + + public virtual bool? IsEnabled + { + get { return _isEnabled; } + set { RaiseAndSetIfChanged(ref _isEnabled, value); } + } + + public EventTreeNode Parent + { + get; + } + + public string Text + { + get; + private set; + } + + internal void UpdateChecked() + { + IsEnabled = GetValue(); + + bool? GetValue() + { + if (Children == null) + return false; + bool? value = false; + for (int i = 0; i < Children.Count; i++) + { + if (i == 0) + { + value = Children[i].IsEnabled; + continue; + } + + if (value != Children[i].IsEnabled) + { + value = null; + break; + } + } + + return value; + } + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs new file mode 100644 index 0000000000..a9a6334689 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/EventsViewModel.cs @@ -0,0 +1,75 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Windows.Input; +using Avalonia.Collections; +using Avalonia.Controls; +using Avalonia.Data.Converters; +using Avalonia.Interactivity; +using Avalonia.Media; +using Avalonia.Threading; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class EventsViewModel : ViewModelBase + { + private IControl _root; + private FiredEvent _selectedEvent; + private ICommand ClearCommand { get; } + + public EventsViewModel(IControl root) + { + this._root = root; + this.Nodes = RoutedEventRegistry.Instance.GetAllRegistered() + .GroupBy(e => e.OwnerType) + .OrderBy(e => e.Key.Name) + .Select(g => new ControlTreeNode(g.Key, g, this)) + .ToArray(); + } + + private void ClearExecute() + { + Action action = delegate + { + RecordedEvents.Clear(); + }; + if (!Dispatcher.UIThread.CheckAccess()) + { + Dispatcher.UIThread.Post(action); + } + else + { + action(); + } + } + + public EventTreeNode[] Nodes { get; } + + public ObservableCollection RecordedEvents { get; } = new ObservableCollection(); + + public FiredEvent SelectedEvent + { + get => _selectedEvent; + set => RaiseAndSetIfChanged(ref _selectedEvent, value); + } + } + + internal class BoolToBrushConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return (bool)value ? Brushes.LightGreen : Brushes.Transparent; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs new file mode 100644 index 0000000000..523525b634 --- /dev/null +++ b/src/Avalonia.Diagnostics/ViewModels/FiredEvent.cs @@ -0,0 +1,93 @@ +// Copyright (c) The Avalonia Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections.ObjectModel; +using Avalonia.Diagnostics.Models; +using Avalonia.Interactivity; + +namespace Avalonia.Diagnostics.ViewModels +{ + internal class FiredEvent : ViewModelBase + { + private RoutedEventArgs _eventArgs; + + private ChainLink _handledBy; + private ChainLink _originator; + + public FiredEvent(RoutedEventArgs eventArgs, ChainLink originator) + { + Contract.Requires(eventArgs != null); + Contract.Requires(originator != null); + + this._eventArgs = eventArgs; + this._originator = originator; + AddToChain(originator); + } + + public bool IsPartOfSameEventChain(RoutedEventArgs e) + { + return e == _eventArgs; + } + + public RoutedEvent Event => _eventArgs.RoutedEvent; + + public bool IsHandled => HandledBy?.Handled == true; + + public ObservableCollection EventChain { get; } = new ObservableCollection(); + + public string DisplayText + { + get + { + if (IsHandled) + { + return $"{Event.Name} on {Originator.HandlerName};" + Environment.NewLine + + $"strategies: {Event.RoutingStrategies}; handled by: {HandledBy.HandlerName}"; + } + return $"{Event.Name} on {Originator.HandlerName}; strategies: {Event.RoutingStrategies}"; + } + } + + public ChainLink Originator + { + get { return _originator; } + set + { + if (_originator != value) + { + _originator = value; + RaisePropertyChanged(); + RaisePropertyChanged(nameof(DisplayText)); + } + } + } + + public ChainLink HandledBy + { + get { return _handledBy; } + set + { + if (_handledBy != value) + { + _handledBy = value; + RaisePropertyChanged(); + RaisePropertyChanged(nameof(IsHandled)); + RaisePropertyChanged(nameof(DisplayText)); + } + } + } + + public void AddToChain(object handler, bool handled, RoutingStrategies route) + { + AddToChain(new ChainLink(handler, handled, route)); + } + + public void AddToChain(ChainLink link) + { + EventChain.Add(link); + if (HandledBy == null && link.Handled) + HandledBy = link; + } + } +} diff --git a/src/Avalonia.Diagnostics/Views/EventsView.xaml b/src/Avalonia.Diagnostics/Views/EventsView.xaml new file mode 100644 index 0000000000..79f78d2bba --- /dev/null +++ b/src/Avalonia.Diagnostics/Views/EventsView.xaml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + static Border() { - AffectsRender(BorderThicknessProperty, CornerRadiusProperty); + AffectsRender( + BackgroundProperty, + BorderBrushProperty, + BorderThicknessProperty, + CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty); - BrushAffectsRender(BackgroundProperty, BorderBrushProperty); } /// diff --git a/src/Avalonia.Controls/Panel.cs b/src/Avalonia.Controls/Panel.cs index 9b768749df..5415f3974d 100644 --- a/src/Avalonia.Controls/Panel.cs +++ b/src/Avalonia.Controls/Panel.cs @@ -30,7 +30,7 @@ namespace Avalonia.Controls /// static Panel() { - BrushAffectsRender(BackgroundProperty); + AffectsRender(BackgroundProperty); ClipToBoundsProperty.OverrideDefaultValue(true); } diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index a8019fc306..8d703cfc1c 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -90,9 +90,8 @@ namespace Avalonia.Controls.Presenters /// static ContentPresenter() { - AffectsRender(BorderThicknessProperty, CornerRadiusProperty); + AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); AffectsMeasure(BorderThicknessProperty, PaddingProperty); - BrushAffectsRender(BackgroundProperty, BorderBrushProperty); ContentProperty.Changed.AddClassHandler(x => x.ContentChanged); ContentTemplateProperty.Changed.AddClassHandler(x => x.ContentChanged); TemplatedParentProperty.Changed.AddClassHandler(x => x.TemplatedParentChanged); diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index 7ca0867031..f77c43acd0 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -33,8 +33,7 @@ namespace Avalonia.Controls.Shapes static Shape() { AffectsMeasure(StretchProperty, StrokeThicknessProperty); - AffectsRender(StrokeDashArrayProperty); - BrushAffectsRender(FillProperty, StrokeProperty); + AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); } public Geometry DefiningGeometry diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index 5a98ef3ecc..af7b0f835e 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -101,10 +101,11 @@ namespace Avalonia.Controls { ClipToBoundsProperty.OverrideDefaultValue(true); AffectsRender( + BackgroundProperty, + ForegroundProperty, FontWeightProperty, FontSizeProperty, FontStyleProperty); - BrushAffectsRender(BackgroundProperty, ForegroundProperty); } /// diff --git a/src/Avalonia.Visuals/Media/Brush.cs b/src/Avalonia.Visuals/Media/Brush.cs index 8ba7c1be04..c2c041f073 100644 --- a/src/Avalonia.Visuals/Media/Brush.cs +++ b/src/Avalonia.Visuals/Media/Brush.cs @@ -19,7 +19,7 @@ namespace Avalonia.Media AvaloniaProperty.Register(nameof(Opacity), 1.0); /// - public event EventHandler Changed; + public event EventHandler Invalidated; /// /// Gets or sets the opacity of the brush. @@ -63,14 +63,14 @@ namespace Avalonia.Media /// The properties. /// /// After a call to this method in a brush's static constructor, any change to the - /// property will cause the event to be raised on the brush. + /// property will cause the event to be raised on the brush. /// protected static void AffectsRender(params AvaloniaProperty[] properties) where T : Brush { void Invalidate(AvaloniaPropertyChangedEventArgs e) { - (e.Sender as T)?.RaiseChanged(EventArgs.Empty); + (e.Sender as T)?.RaiseInvalidated(EventArgs.Empty); } foreach (var property in properties) @@ -80,9 +80,9 @@ namespace Avalonia.Media } /// - /// Raises the event. + /// Raises the event. /// /// The event args. - protected void RaiseChanged(EventArgs e) => Changed?.Invoke(this, e); + protected void RaiseInvalidated(EventArgs e) => Invalidated?.Invoke(this, e); } } diff --git a/src/Avalonia.Visuals/Media/GradientBrush.cs b/src/Avalonia.Visuals/Media/GradientBrush.cs index c123813cee..8fd2dcf27f 100644 --- a/src/Avalonia.Visuals/Media/GradientBrush.cs +++ b/src/Avalonia.Visuals/Media/GradientBrush.cs @@ -80,18 +80,18 @@ namespace Avalonia.Media brush._gradientStopsSubscription = newValue.TrackItemPropertyChanged(brush.GradientStopChanged); } - brush.RaiseChanged(EventArgs.Empty); + brush.RaiseInvalidated(EventArgs.Empty); } } private void GradientStopsChanged(object sender, NotifyCollectionChangedEventArgs e) { - RaiseChanged(EventArgs.Empty); + RaiseInvalidated(EventArgs.Empty); } private void GradientStopChanged(Tuple e) { - RaiseChanged(EventArgs.Empty); + RaiseInvalidated(EventArgs.Empty); } } } diff --git a/src/Avalonia.Visuals/Media/IAffectsRender.cs b/src/Avalonia.Visuals/Media/IAffectsRender.cs new file mode 100644 index 0000000000..0024195ce5 --- /dev/null +++ b/src/Avalonia.Visuals/Media/IAffectsRender.cs @@ -0,0 +1,16 @@ +using System; + +namespace Avalonia.Media +{ + /// + /// Signals to a self-rendering control that changes to the resource should invoke + /// . + /// + public interface IAffectsRender + { + /// + /// Raised when the resource changes visually. + /// + event EventHandler Invalidated; + } +} diff --git a/src/Avalonia.Visuals/Media/IMutableBrush.cs b/src/Avalonia.Visuals/Media/IMutableBrush.cs index 762731a6a8..415db61d68 100644 --- a/src/Avalonia.Visuals/Media/IMutableBrush.cs +++ b/src/Avalonia.Visuals/Media/IMutableBrush.cs @@ -5,13 +5,8 @@ namespace Avalonia.Media /// /// Represents a mutable brush which can return an immutable clone of itself. /// - public interface IMutableBrush : IBrush + public interface IMutableBrush : IBrush, IAffectsRender { - /// - /// Raised when the brush changes visually. - /// - event EventHandler Changed; - /// /// Creates an immutable clone of the brush. /// diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index fdff16494f..e5fcf1ba1d 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -338,45 +338,20 @@ namespace Avalonia /// FrameworkPropertyMetadata.AffectsRender flag. /// protected static void AffectsRender(params AvaloniaProperty[] properties) - where T : class, IVisual - { - void Invalidate(AvaloniaPropertyChangedEventArgs e) - { - (e.Sender as T)?.InvalidateVisual(); - } - - foreach (var property in properties) - { - property.Changed.Subscribe(Invalidate); - } - } - - /// - /// Indicates that a brush property change should cause to be - /// called. - /// - /// The properties. - /// - /// This method should be called in a control's static constructor with each property - /// on the control which when changed should cause a redraw. It not only triggers an - /// invalidation when the property itself changes, but also when the brush raises - /// the event. - /// - protected static void BrushAffectsRender(params AvaloniaProperty[] properties) where T : Visual { void Invalidate(AvaloniaPropertyChangedEventArgs e) { if (e.Sender is T sender) { - if (e.OldValue is IMutableBrush oldValue) + if (e.OldValue is IAffectsRender oldValue) { - oldValue.Changed -= sender.BrushChanged; + oldValue.Invalidated -= sender.AffectsRenderInvalidated; } - if (e.NewValue is IMutableBrush newValue) + if (e.NewValue is IAffectsRender newValue) { - newValue.Changed += sender.BrushChanged; + newValue.Invalidated += sender.AffectsRenderInvalidated; } sender.InvalidateVisual(); @@ -582,7 +557,7 @@ namespace Avalonia OnVisualParentChanged(old, value); } - private void BrushChanged(object sender, EventArgs e) => InvalidateVisual(); + private void AffectsRenderInvalidated(object sender, EventArgs e) => InvalidateVisual(); /// /// Called when the collection changes. diff --git a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs index f843a6e333..ff7b94105a 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/ImageBrushTests.cs @@ -11,14 +11,14 @@ namespace Avalonia.Visuals.UnitTests.Media public class ImageBrushTests { [Fact] - public void Changing_Source_Raises_Changed() + public void Changing_Source_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); var target = new ImageBrush(bitmap1); var raised = false; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.Source = bitmap2; Assert.True(raised); diff --git a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs index 62f53108e6..b3f78dfe8f 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/LinearGradientBrushTests.cs @@ -9,7 +9,7 @@ namespace Avalonia.Visuals.UnitTests.Media public class LinearGradientBrushTests { [Fact] - public void Changing_StartPoint_Raises_Changed() + public void Changing_StartPoint_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -17,14 +17,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.StartPoint = new RelativePoint(); - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.StartPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); Assert.True(raised); } [Fact] - public void Changing_EndPoint_Raises_Changed() + public void Changing_EndPoint_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -32,14 +32,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.EndPoint = new RelativePoint(); - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.EndPoint = new RelativePoint(10, 10, RelativeUnit.Absolute); Assert.True(raised); } [Fact] - public void Changing_GradientStops_Raises_Changed() + public void Changing_GradientStops_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -47,14 +47,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops = new GradientStops { new GradientStop(Colors.Green, 0) }; Assert.True(raised); } [Fact] - public void Adding_GradientStop_Raises_Changed() + public void Adding_GradientStop_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -62,14 +62,14 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops.Add(new GradientStop(Colors.Green, 1)); Assert.True(raised); } [Fact] - public void Changing_GradientStop_Offset_Raises_Changed() + public void Changing_GradientStop_Offset_Raises_Invalidated() { var bitmap1 = Mock.Of(); var bitmap2 = Mock.Of(); @@ -77,7 +77,7 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.GradientStops = new GradientStops { new GradientStop(Colors.Red, 0) }; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.GradientStops[0].Offset = 0.5; Assert.True(raised); diff --git a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs index 4e87b7081d..bcf63f9660 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/SolidColorBrushTests.cs @@ -7,12 +7,12 @@ namespace Avalonia.Visuals.UnitTests.Media public class SolidColorBrushTests { [Fact] - public void Changing_Color_Raises_Changed() + public void Changing_Color_Raises_Invalidated() { var target = new SolidColorBrush(Colors.Red); var raised = false; - target.Changed += (s, e) => raised = true; + target.Invalidated += (s, e) => raised = true; target.Color = Colors.Green; Assert.True(raised); From 3cbcd0ac0fab750a413b0e9aaeba88d092faa238 Mon Sep 17 00:00:00 2001 From: Jumar Macato Date: Wed, 12 Sep 2018 10:42:44 +0800 Subject: [PATCH 46/67] Match CSS's behavior on selectors & animations. --- src/Avalonia.Animation/Animator`1.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Avalonia.Animation/Animator`1.cs b/src/Avalonia.Animation/Animator`1.cs index ab82bfb35d..e4af0f356d 100644 --- a/src/Avalonia.Animation/Animator`1.cs +++ b/src/Avalonia.Animation/Animator`1.cs @@ -37,16 +37,11 @@ namespace Avalonia.Animation if (!_isVerifiedAndConverted) VerifyConvertKeyFrames(); - return match - .DistinctUntilChanged() - .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) - .Buffer(2, 1) - .Where(x => x.Count > 1) - .Subscribe(x => - { - if (animation.RepeatCount.IsLoop) - x[0]?.Dispose(); - }); + return match.DistinctUntilChanged() + .Select(x => x ? RunKeyFrames(animation, control, onComplete) : null) + .Buffer(2, 1) + .Where(x => x.Count > 1) + .Subscribe(x => x[0]?.Dispose()); } /// From 5fce271ff8bfff57987199d0fde5839c4788d650 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 11 Sep 2018 21:43:05 -0500 Subject: [PATCH 47/67] PR Feedback --- src/Avalonia.Animation/IAnimation.cs | 4 ++-- src/Avalonia.Animation/IAnimator.cs | 2 +- src/Avalonia.Animation/IGlobalClock.cs | 10 ++++++++++ src/Avalonia.Controls/Application.cs | 2 +- .../InternalPlatformThreadingInterface.cs | 9 ++++++--- src/Avalonia.Controls/TopLevel.cs | 1 - src/Avalonia.Visuals/Animation/RenderLoopClock.cs | 6 +++--- .../Rendering/DefaultRenderTimer.cs | 12 +++++++----- .../Rendering/DeferredRenderer.cs | 2 +- src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs | 15 +-------------- src/Avalonia.Visuals/Rendering/IRenderTimer.cs | 4 ++-- src/Avalonia.Visuals/Rendering/RenderLoop.cs | 4 ++-- .../Rendering/RenderLoopTests.cs | 14 +++++++------- 13 files changed, 43 insertions(+), 42 deletions(-) create mode 100644 src/Avalonia.Animation/IGlobalClock.cs diff --git a/src/Avalonia.Animation/IAnimation.cs b/src/Avalonia.Animation/IAnimation.cs index 34b0a5d769..ff85535d8a 100644 --- a/src/Avalonia.Animation/IAnimation.cs +++ b/src/Avalonia.Animation/IAnimation.cs @@ -9,12 +9,12 @@ namespace Avalonia.Animation public interface IAnimation { /// - /// Apply the animation to the specified control + /// Apply the animation to the specified control and run it when produces true. /// IDisposable Apply(Animatable control, IClock clock, IObservable match, Action onComplete = null); /// - /// Run the animation to the specified control + /// Run the animation on the specified control. /// Task RunAsync(Animatable control, IClock clock); } diff --git a/src/Avalonia.Animation/IAnimator.cs b/src/Avalonia.Animation/IAnimator.cs index 04bad8e112..d0fb173c54 100644 --- a/src/Avalonia.Animation/IAnimator.cs +++ b/src/Avalonia.Animation/IAnimator.cs @@ -16,6 +16,6 @@ namespace Avalonia.Animation /// /// Applies the current KeyFrame group to the specified control. /// - IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable obsMatch, Action onComplete); + IDisposable Apply(Animation animation, Animatable control, IClock clock, IObservable match, Action onComplete); } } diff --git a/src/Avalonia.Animation/IGlobalClock.cs b/src/Avalonia.Animation/IGlobalClock.cs new file mode 100644 index 0000000000..b0455e2c80 --- /dev/null +++ b/src/Avalonia.Animation/IGlobalClock.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Animation +{ + public interface IGlobalClock : IClock + { + } +} diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index 586a73b75c..37796ff9ba 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -340,7 +340,7 @@ namespace Avalonia var clock = new RenderLoopClock(); AvaloniaLocator.CurrentMutable - .Bind().ToConstant(clock) + .Bind().ToConstant(clock) .GetService()?.Add(clock); } } diff --git a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs index 501e15653a..bb357453ff 100644 --- a/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs +++ b/src/Avalonia.Controls/Platform/InternalPlatformThreadingInterface.cs @@ -14,7 +14,10 @@ namespace Avalonia.Controls.Platform public InternalPlatformThreadingInterface() { TlsCurrentThreadIsLoopThread = true; - StartTimer(DispatcherPriority.Render, new TimeSpan(0, 0, 0, 0, 66), () => Tick?.Invoke(Environment.TickCount)); + StartTimer( + DispatcherPriority.Render, + new TimeSpan(0, 0, 0, 0, 66), + () => Tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount))); } private readonly AutoResetEvent _signaled = new AutoResetEvent(false); @@ -105,7 +108,7 @@ namespace Avalonia.Controls.Platform public bool CurrentThreadIsLoopThread => TlsCurrentThreadIsLoopThread; public event Action Signaled; - public event Action Tick; + public event Action Tick; } -} \ No newline at end of file +} diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index fb5b932fd8..630753396f 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -96,7 +96,6 @@ namespace Avalonia.Controls _applicationLifecycle = TryGetService(dependencyResolver); _renderInterface = TryGetService(dependencyResolver); - var renderLoop = TryGetService(dependencyResolver); Renderer = impl.CreateRenderer(this); impl.SetInputRoot(this); diff --git a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs index e59b3aac0d..504caef461 100644 --- a/src/Avalonia.Visuals/Animation/RenderLoopClock.cs +++ b/src/Avalonia.Visuals/Animation/RenderLoopClock.cs @@ -5,7 +5,7 @@ using Avalonia.Rendering; namespace Avalonia.Animation { - public class RenderLoopClock : ClockBase, IRenderLoopTask + public class RenderLoopClock : ClockBase, IRenderLoopTask, IGlobalClock { protected override void Stop() { @@ -18,9 +18,9 @@ namespace Avalonia.Animation { } - void IRenderLoopTask.Update(long tickCount) + void IRenderLoopTask.Update(TimeSpan time) { - Pulse(TimeSpan.FromMilliseconds(tickCount)); + Pulse(time); } } } diff --git a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs index a83334ff5e..d0eb181c65 100644 --- a/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/DefaultRenderTimer.cs @@ -19,7 +19,7 @@ namespace Avalonia.Rendering { private IRuntimePlatform _runtime; private int _subscriberCount; - private Action _tick; + private Action _tick; private IDisposable _subscription; /// @@ -39,7 +39,7 @@ namespace Avalonia.Rendering public int FramesPerSecond { get; } /// - public event Action Tick + public event Action Tick { add { @@ -78,14 +78,16 @@ namespace Avalonia.Rendering /// This can be overridden by platform implementations to use a specialized timer /// implementation. /// - protected virtual IDisposable StartCore(Action tick) + protected virtual IDisposable StartCore(Action tick) { if (_runtime == null) { _runtime = AvaloniaLocator.Current.GetService(); } - return _runtime.StartSystemTimer(TimeSpan.FromSeconds(1.0 / FramesPerSecond), () => tick(Environment.TickCount)); + return _runtime.StartSystemTimer( + TimeSpan.FromSeconds(1.0 / FramesPerSecond), + () => tick(TimeSpan.FromMilliseconds(Environment.TickCount))); } /// @@ -97,7 +99,7 @@ namespace Avalonia.Rendering _subscription = null; } - private void InternalTick(long tickCount) + private void InternalTick(TimeSpan tickCount) { _tick(tickCount); } diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index 3221dd85c6..fc67b5c461 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -166,7 +166,7 @@ namespace Avalonia.Rendering bool IRenderLoopTask.NeedsUpdate => _dirty == null || _dirty.Count > 0; - void IRenderLoopTask.Update(long tickCount) => UpdateScene(); + void IRenderLoopTask.Update(TimeSpan time) => UpdateScene(); void IRenderLoopTask.Render() { diff --git a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs index b031bf00df..15f0afc797 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderLoopTask.cs @@ -6,20 +6,7 @@ namespace Avalonia.Rendering public interface IRenderLoopTask { bool NeedsUpdate { get; } - void Update(long tickCount); + void Update(TimeSpan time); void Render(); } - - public class MockRenderLoopTask : IRenderLoopTask - { - public bool NeedsUpdate => true; - - public void Render() - { - } - - public void Update(long tickCount) - { - } - } } diff --git a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs index 78f6183994..d333e928a0 100644 --- a/src/Avalonia.Visuals/Rendering/IRenderTimer.cs +++ b/src/Avalonia.Visuals/Rendering/IRenderTimer.cs @@ -15,6 +15,6 @@ namespace Avalonia.Rendering /// This event can be raised on any thread; it is the responsibility of the subscriber to /// switch execution to the right thread. /// - event Action Tick; + event Action Tick; } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Rendering/RenderLoop.cs b/src/Avalonia.Visuals/Rendering/RenderLoop.cs index d920be2706..d0d5b2250d 100644 --- a/src/Avalonia.Visuals/Rendering/RenderLoop.cs +++ b/src/Avalonia.Visuals/Rendering/RenderLoop.cs @@ -84,7 +84,7 @@ namespace Avalonia.Rendering } } - private async void TimerTick(long tickCount) + private async void TimerTick(TimeSpan time) { if (Interlocked.CompareExchange(ref inTick, 1, 0) == 0) { @@ -96,7 +96,7 @@ namespace Avalonia.Rendering { foreach (var i in _items) { - i.Update(tickCount); + i.Update(time); } }, DispatcherPriority.Render).ConfigureAwait(false); } diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs index 30ef35a2bb..bf992f4027 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/RenderLoopTests.cs @@ -35,14 +35,14 @@ namespace Avalonia.Visuals.UnitTests.Rendering var renderTask = new Mock(); renderTask.Setup(t => t.NeedsUpdate).Returns(true); - renderTask.Setup(t => t.Update(It.IsAny())) + renderTask.Setup(t => t.Update(It.IsAny())) .Callback((long _) => Assert.True(inDispatcher)); loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } [Fact] @@ -62,7 +62,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Never()); } [Fact] @@ -92,7 +92,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering loop.Add(renderTask.Object); timer.Raise(t => t.Tick += null, 0L); - renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); + renderTask.Verify(t => t.Update(It.IsAny()), Times.Once()); } [Fact] @@ -110,10 +110,10 @@ namespace Avalonia.Visuals.UnitTests.Rendering renderTask.Setup(t => t.NeedsUpdate).Returns(true); loop.Add(renderTask.Object); - var tickCount = 12345L; - timer.Raise(t => t.Tick += null, tickCount); + var time = new TimeSpan(123456789L); + timer.Raise(t => t.Tick += null, time); - renderTask.Verify(t => t.Update(tickCount), Times.Once()); + renderTask.Verify(t => t.Update(time), Times.Once()); } } } From df014f3b1e2ec60f7eb8651e5dd01b90c4fa8d5c Mon Sep 17 00:00:00 2001 From: ahopper Date: Wed, 12 Sep 2018 03:46:46 +0100 Subject: [PATCH 48/67] Fix drawing presenter clipping (#1882) * fix clipping offset * simplified clipping calculation --- src/Avalonia.Controls/DrawingPresenter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/DrawingPresenter.cs b/src/Avalonia.Controls/DrawingPresenter.cs index 34ce598218..b30a8668fd 100644 --- a/src/Avalonia.Controls/DrawingPresenter.cs +++ b/src/Avalonia.Controls/DrawingPresenter.cs @@ -49,7 +49,7 @@ namespace Avalonia.Controls if (Drawing != null) { using (context.PushPreTransform(_transform)) - using (context.PushClip(Bounds)) + using (context.PushClip(new Rect(Bounds.Size))) { Drawing.Draw(context); } From 0aa5866ea862ae9a4836dc1899361fd90f7b0739 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 11 Sep 2018 22:40:36 -0500 Subject: [PATCH 49/67] Fix missed changes in IRenderTimer. --- src/OSX/Avalonia.MonoMac/RenderTimer.cs | 4 ++-- src/Windows/Avalonia.Win32/RenderTimer.cs | 4 ++-- src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs | 4 ++-- .../Rendering/DeferredRendererTests.cs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/OSX/Avalonia.MonoMac/RenderTimer.cs b/src/OSX/Avalonia.MonoMac/RenderTimer.cs index 22ad2e81a2..f3c49828d6 100644 --- a/src/OSX/Avalonia.MonoMac/RenderTimer.cs +++ b/src/OSX/Avalonia.MonoMac/RenderTimer.cs @@ -12,7 +12,7 @@ namespace Avalonia.MonoMac { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { return AvaloniaLocator.Current.GetService().StartSystemTimer( TimeSpan.FromSeconds(1.0 / FramesPerSecond), @@ -20,7 +20,7 @@ namespace Avalonia.MonoMac { using (new NSAutoreleasePool()) { - tick?.Invoke(Environment.TickCount); + tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount)); } }); } diff --git a/src/Windows/Avalonia.Win32/RenderTimer.cs b/src/Windows/Avalonia.Win32/RenderTimer.cs index c911bc3adf..7dbb745a23 100644 --- a/src/Windows/Avalonia.Win32/RenderTimer.cs +++ b/src/Windows/Avalonia.Win32/RenderTimer.cs @@ -29,12 +29,12 @@ namespace Avalonia.Win32 { } - protected override IDisposable StartCore(Action tick) + protected override IDisposable StartCore(Action tick) { EnsureTimerQueueCreated(); var msPerFrame = 1000 / FramesPerSecond; - timerDelegate = (_, __) => tick(Environment.TickCount); + timerDelegate = (_, __) => tick(TimeSpan.FromMilliseconds(Environment.TickCount)); UnmanagedMethods.CreateTimerQueueTimer( out var timer, diff --git a/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs index 1357a4f642..0cefba7f19 100644 --- a/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs +++ b/src/iOS/Avalonia.iOS/DisplayLinkRenderTimer.cs @@ -7,7 +7,7 @@ namespace Avalonia.iOS { class DisplayLinkRenderTimer : IRenderTimer { - public event Action Tick; + public event Action Tick; private CADisplayLink _link; public DisplayLinkRenderTimer() @@ -21,7 +21,7 @@ namespace Avalonia.iOS { try { - Tick?.Invoke(Environment.TickCount); + Tick?.Invoke(TimeSpan.FromMilliseconds(Environment.TickCount)); } catch (Exception) { diff --git a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs index e2a5c0c54c..8c103360d4 100644 --- a/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Rendering/DeferredRendererTests.cs @@ -357,7 +357,7 @@ namespace Avalonia.Visuals.UnitTests.Rendering private void RunFrame(IRenderLoopTask task) { - task.Update(0); + task.Update(TimeSpan.Zero); task.Render(); } From 4f11ecdd38a6dc7bd50a342b22537f6970de7e95 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 12 Sep 2018 13:07:32 +0200 Subject: [PATCH 50/67] Added an example of dynamic menus to the ControlCatalog. Creates a menu using `DataTemplates` on the MenuPage. --- samples/ControlCatalog/App.xaml | 9 ++- samples/ControlCatalog/Pages/MenuPage.xaml | 59 ++++++++++++------- samples/ControlCatalog/Pages/MenuPage.xaml.cs | 28 +++++++++ 3 files changed, 72 insertions(+), 24 deletions(-) diff --git a/samples/ControlCatalog/App.xaml b/samples/ControlCatalog/App.xaml index 43971dec4f..95d515ec60 100644 --- a/samples/ControlCatalog/App.xaml +++ b/samples/ControlCatalog/App.xaml @@ -14,6 +14,11 @@ - + + + - \ No newline at end of file + diff --git a/samples/ControlCatalog/Pages/MenuPage.xaml b/samples/ControlCatalog/Pages/MenuPage.xaml index 296cfa8704..6eeb86a00f 100644 --- a/samples/ControlCatalog/Pages/MenuPage.xaml +++ b/samples/ControlCatalog/Pages/MenuPage.xaml @@ -7,29 +7,44 @@ Margin="0,16,0,0" HorizontalAlignment="Center" Spacing="16"> - - - - - - - + + Defined in XAML + + + + + + + + + + + + + + + + + + - - - - + + - - - - - - - - - - + + + + + Dyanamically generated + + + + + + - \ No newline at end of file + diff --git a/samples/ControlCatalog/Pages/MenuPage.xaml.cs b/samples/ControlCatalog/Pages/MenuPage.xaml.cs index d637c172e1..880d4bc59a 100644 --- a/samples/ControlCatalog/Pages/MenuPage.xaml.cs +++ b/samples/ControlCatalog/Pages/MenuPage.xaml.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Markup.Xaml; @@ -8,6 +9,27 @@ namespace ControlCatalog.Pages public MenuPage() { this.InitializeComponent(); + DataContext = new[] + { + new MenuItemViewModel + { + Header = "_File", + Items = + { + new MenuItemViewModel { Header = "_Open..." }, + new MenuItemViewModel { Header = "Save" }, + } + }, + new MenuItemViewModel + { + Header = "_Edit", + Items = + { + new MenuItemViewModel { Header = "_Copy" }, + new MenuItemViewModel { Header = "_Paste" }, + } + } + }; } private void InitializeComponent() @@ -15,4 +37,10 @@ namespace ControlCatalog.Pages AvaloniaXamlLoader.Load(this); } } + + public class MenuItemViewModel + { + public string Header { get; set; } + public IList Items { get; } = new List(); + } } From b44900395800eec65d3fc1b7b6bcef8152638a43 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 12 Sep 2018 15:21:14 +0200 Subject: [PATCH 51/67] Allow MenuItems to be separators. If `MenuItem.Header == "-"` then apply the `:separator` pseudoclass to the `MenuItem` and display it as a separator. --- samples/ControlCatalog/Pages/MenuPage.xaml.cs | 16 ++++++++++++--- src/Avalonia.Controls/MenuItem.cs | 20 +++++++++++++++---- src/Avalonia.Themes.Default/MenuItem.xaml | 12 ++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/samples/ControlCatalog/Pages/MenuPage.xaml.cs b/samples/ControlCatalog/Pages/MenuPage.xaml.cs index 880d4bc59a..01add3e76e 100644 --- a/samples/ControlCatalog/Pages/MenuPage.xaml.cs +++ b/samples/ControlCatalog/Pages/MenuPage.xaml.cs @@ -14,16 +14,26 @@ namespace ControlCatalog.Pages new MenuItemViewModel { Header = "_File", - Items = + Items = new[] { new MenuItemViewModel { Header = "_Open..." }, new MenuItemViewModel { Header = "Save" }, + new MenuItemViewModel { Header = "-" }, + new MenuItemViewModel + { + Header = "Recent", + Items = new[] + { + new MenuItemViewModel { Header = "File1.txt" }, + new MenuItemViewModel { Header = "File2.txt" }, + } + }, } }, new MenuItemViewModel { Header = "_Edit", - Items = + Items = new[] { new MenuItemViewModel { Header = "_Copy" }, new MenuItemViewModel { Header = "_Paste" }, @@ -41,6 +51,6 @@ namespace ControlCatalog.Pages public class MenuItemViewModel { public string Header { get; set; } - public IList Items { get; } = new List(); + public IList Items { get; set; } } } diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs index 7b57783c5a..03a43b5164 100644 --- a/src/Avalonia.Controls/MenuItem.cs +++ b/src/Avalonia.Controls/MenuItem.cs @@ -105,6 +105,7 @@ namespace Avalonia.Controls ClickEvent.AddClassHandler(x => x.OnClick); SubmenuOpenedEvent.AddClassHandler(x => x.OnSubmenuOpened); IsSubMenuOpenProperty.Changed.AddClassHandler(x => x.SubMenuOpenChanged); + PseudoClass(HeaderProperty, x => x as string == "-", ":separator"); } public MenuItem() @@ -357,10 +358,21 @@ namespace Avalonia.Controls { base.OnTemplateApplied(e); - _popup = e.NameScope.Get("PART_Popup"); - _popup.DependencyResolver = DependencyResolver.Instance; - _popup.Opened += PopupOpened; - _popup.Closed += PopupClosed; + if (_popup != null) + { + _popup.Opened -= PopupOpened; + _popup.Closed -= PopupClosed; + _popup.DependencyResolver = null; + } + + _popup = e.NameScope.Find("PART_Popup"); + + if (_popup != null) + { + _popup.DependencyResolver = DependencyResolver.Instance; + _popup.Opened += PopupOpened; + _popup.Closed += PopupClosed; + } } /// diff --git a/src/Avalonia.Themes.Default/MenuItem.xaml b/src/Avalonia.Themes.Default/MenuItem.xaml index 8a2ed2a802..07faf7a632 100644 --- a/src/Avalonia.Themes.Default/MenuItem.xaml +++ b/src/Avalonia.Themes.Default/MenuItem.xaml @@ -73,7 +73,17 @@ - + + +