From 66bebd0c1b5a5c4ece8c08c675c0ef926b41e3c9 Mon Sep 17 00:00:00 2001 From: Benedikt Schroeder Date: Wed, 5 Sep 2018 16:45:27 +0200 Subject: [PATCH 01/29] 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/29] 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/29] 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/29] 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/29] 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 fad2e317ba413b0624de7600ebc1bdeaa02a3272 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 7 Sep 2018 02:13:57 +0200 Subject: [PATCH 06/29] Make AffectsMeasure/Arrange/Render typed. --- src/Avalonia.Controls/Border.cs | 4 ++-- src/Avalonia.Controls/Decorator.cs | 2 +- src/Avalonia.Controls/DrawingPresenter.cs | 6 ++--- src/Avalonia.Controls/Image.cs | 3 +-- .../Presenters/ContentPresenter.cs | 4 ++-- .../Presenters/ScrollContentPresenter.cs | 2 +- .../Presenters/TextPresenter.cs | 2 +- .../Primitives/AccessText.cs | 2 +- src/Avalonia.Controls/Primitives/Track.cs | 2 +- src/Avalonia.Controls/Shapes/Shape.cs | 5 ++--- src/Avalonia.Controls/StackPanel.cs | 4 ++-- src/Avalonia.Controls/TabControl.cs | 2 +- src/Avalonia.Controls/TextBlock.cs | 9 ++++---- src/Avalonia.Controls/TopLevel.cs | 2 +- src/Avalonia.Controls/WrapPanel.cs | 2 +- src/Avalonia.Layout/Layoutable.cs | 22 +++++++++++-------- src/Avalonia.Visuals/Visual.cs | 12 +++++----- 17 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/Avalonia.Controls/Border.cs b/src/Avalonia.Controls/Border.cs index 5f84421c64..8b2a45b090 100644 --- a/src/Avalonia.Controls/Border.cs +++ b/src/Avalonia.Controls/Border.cs @@ -43,8 +43,8 @@ namespace Avalonia.Controls /// static Border() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); - AffectsMeasure(BorderThicknessProperty); + AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsMeasure(BorderThicknessProperty); } /// diff --git a/src/Avalonia.Controls/Decorator.cs b/src/Avalonia.Controls/Decorator.cs index 389cf66d34..15651b918e 100644 --- a/src/Avalonia.Controls/Decorator.cs +++ b/src/Avalonia.Controls/Decorator.cs @@ -28,7 +28,7 @@ namespace Avalonia.Controls /// static Decorator() { - AffectsMeasure(ChildProperty, PaddingProperty); + AffectsMeasure(ChildProperty, PaddingProperty); ChildProperty.Changed.AddClassHandler(x => x.ChildChanged); } diff --git a/src/Avalonia.Controls/DrawingPresenter.cs b/src/Avalonia.Controls/DrawingPresenter.cs index af3665fabc..34ce598218 100644 --- a/src/Avalonia.Controls/DrawingPresenter.cs +++ b/src/Avalonia.Controls/DrawingPresenter.cs @@ -8,8 +8,8 @@ namespace Avalonia.Controls { static DrawingPresenter() { - AffectsMeasure(DrawingProperty); - AffectsRender(DrawingProperty); + AffectsMeasure(DrawingProperty); + AffectsRender(DrawingProperty); } public static readonly StyledProperty DrawingProperty = @@ -56,4 +56,4 @@ namespace Avalonia.Controls } } } -} \ No newline at end of file +} diff --git a/src/Avalonia.Controls/Image.cs b/src/Avalonia.Controls/Image.cs index f146e3571c..802b700a07 100644 --- a/src/Avalonia.Controls/Image.cs +++ b/src/Avalonia.Controls/Image.cs @@ -25,8 +25,7 @@ namespace Avalonia.Controls static Image() { - AffectsRender(SourceProperty); - AffectsRender(StretchProperty); + AffectsRender(SourceProperty, StretchProperty); } /// diff --git a/src/Avalonia.Controls/Presenters/ContentPresenter.cs b/src/Avalonia.Controls/Presenters/ContentPresenter.cs index 6badf91367..8d703cfc1c 100644 --- a/src/Avalonia.Controls/Presenters/ContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ContentPresenter.cs @@ -90,8 +90,8 @@ namespace Avalonia.Controls.Presenters /// static ContentPresenter() { - AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); - AffectsMeasure(BorderThicknessProperty, PaddingProperty); + AffectsRender(BackgroundProperty, BorderBrushProperty, BorderThicknessProperty, CornerRadiusProperty); + AffectsMeasure(BorderThicknessProperty, PaddingProperty); ContentProperty.Changed.AddClassHandler(x => x.ContentChanged); ContentTemplateProperty.Changed.AddClassHandler(x => x.ContentChanged); TemplatedParentProperty.Changed.AddClassHandler(x => x.TemplatedParentChanged); diff --git a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs index 2ef7941b55..c05c1672f8 100644 --- a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs @@ -72,7 +72,7 @@ namespace Avalonia.Controls.Presenters { ClipToBoundsProperty.OverrideDefaultValue(typeof(ScrollContentPresenter), true); ChildProperty.Changed.AddClassHandler(x => x.ChildChanged); - AffectsArrange(OffsetProperty); + AffectsArrange(OffsetProperty); } /// diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index a30d9bfc48..f73a335de5 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -38,7 +38,7 @@ namespace Avalonia.Controls.Presenters static TextPresenter() { - AffectsRender(PasswordCharProperty); + AffectsRender(PasswordCharProperty); } public TextPresenter() diff --git a/src/Avalonia.Controls/Primitives/AccessText.cs b/src/Avalonia.Controls/Primitives/AccessText.cs index 32a0efc440..5adc8d2448 100644 --- a/src/Avalonia.Controls/Primitives/AccessText.cs +++ b/src/Avalonia.Controls/Primitives/AccessText.cs @@ -28,7 +28,7 @@ namespace Avalonia.Controls.Primitives /// static AccessText() { - AffectsRender(ShowAccessKeyProperty); + AffectsRender(ShowAccessKeyProperty); } /// diff --git a/src/Avalonia.Controls/Primitives/Track.cs b/src/Avalonia.Controls/Primitives/Track.cs index 648fe5f4b0..8ff3ced770 100644 --- a/src/Avalonia.Controls/Primitives/Track.cs +++ b/src/Avalonia.Controls/Primitives/Track.cs @@ -42,7 +42,7 @@ namespace Avalonia.Controls.Primitives ThumbProperty.Changed.AddClassHandler(x => x.ThumbChanged); IncreaseButtonProperty.Changed.AddClassHandler(x => x.ButtonChanged); DecreaseButtonProperty.Changed.AddClassHandler(x => x.ButtonChanged); - AffectsArrange(MinimumProperty, MaximumProperty, ValueProperty, OrientationProperty); + AffectsArrange(MinimumProperty, MaximumProperty, ValueProperty, OrientationProperty); } public double Minimum diff --git a/src/Avalonia.Controls/Shapes/Shape.cs b/src/Avalonia.Controls/Shapes/Shape.cs index 604051ef28..f77c43acd0 100644 --- a/src/Avalonia.Controls/Shapes/Shape.cs +++ b/src/Avalonia.Controls/Shapes/Shape.cs @@ -30,11 +30,10 @@ namespace Avalonia.Controls.Shapes private Geometry _renderedGeometry; bool _calculateTransformOnArrange = false; - static Shape() { - AffectsMeasure(StretchProperty, StrokeThicknessProperty); - AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); + AffectsMeasure(StretchProperty, StrokeThicknessProperty); + AffectsRender(FillProperty, StrokeProperty, StrokeDashArrayProperty); } public Geometry DefiningGeometry diff --git a/src/Avalonia.Controls/StackPanel.cs b/src/Avalonia.Controls/StackPanel.cs index 645cdbd926..df0c113cc0 100644 --- a/src/Avalonia.Controls/StackPanel.cs +++ b/src/Avalonia.Controls/StackPanel.cs @@ -29,8 +29,8 @@ namespace Avalonia.Controls /// static StackPanel() { - AffectsMeasure(SpacingProperty); - AffectsMeasure(OrientationProperty); + AffectsMeasure(SpacingProperty); + AffectsMeasure(OrientationProperty); } /// diff --git a/src/Avalonia.Controls/TabControl.cs b/src/Avalonia.Controls/TabControl.cs index 70cf8b4e05..3aae256858 100644 --- a/src/Avalonia.Controls/TabControl.cs +++ b/src/Avalonia.Controls/TabControl.cs @@ -44,7 +44,7 @@ namespace Avalonia.Controls { SelectionModeProperty.OverrideDefaultValue(SelectionMode.AlwaysSelected); FocusableProperty.OverrideDefaultValue(false); - AffectsMeasure(TabStripPlacementProperty); + AffectsMeasure(TabStripPlacementProperty); } /// diff --git a/src/Avalonia.Controls/TextBlock.cs b/src/Avalonia.Controls/TextBlock.cs index e91d2e8fa7..541e55625a 100644 --- a/src/Avalonia.Controls/TextBlock.cs +++ b/src/Avalonia.Controls/TextBlock.cs @@ -99,10 +99,11 @@ namespace Avalonia.Controls static TextBlock() { ClipToBoundsProperty.OverrideDefaultValue(true); - AffectsRender(ForegroundProperty); - AffectsRender(FontWeightProperty); - AffectsRender(FontSizeProperty); - AffectsRender(FontStyleProperty); + AffectsRender( + ForegroundProperty, + FontWeightProperty, + FontSizeProperty, + FontStyleProperty); } /// diff --git a/src/Avalonia.Controls/TopLevel.cs b/src/Avalonia.Controls/TopLevel.cs index 1161ded25f..5f8eac1fe3 100644 --- a/src/Avalonia.Controls/TopLevel.cs +++ b/src/Avalonia.Controls/TopLevel.cs @@ -59,7 +59,7 @@ namespace Avalonia.Controls /// static TopLevel() { - AffectsMeasure(ClientSizeProperty); + AffectsMeasure(ClientSizeProperty); } /// diff --git a/src/Avalonia.Controls/WrapPanel.cs b/src/Avalonia.Controls/WrapPanel.cs index 8ee0636124..597734d400 100644 --- a/src/Avalonia.Controls/WrapPanel.cs +++ b/src/Avalonia.Controls/WrapPanel.cs @@ -30,7 +30,7 @@ namespace Avalonia.Controls /// static WrapPanel() { - AffectsMeasure(OrientationProperty); + AffectsMeasure(OrientationProperty); } /// diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs index 54bdbb5d48..b8b24e6d31 100644 --- a/src/Avalonia.Layout/Layoutable.cs +++ b/src/Avalonia.Layout/Layoutable.cs @@ -140,7 +140,7 @@ namespace Avalonia.Layout /// static Layoutable() { - AffectsMeasure( + AffectsMeasure( IsVisibleProperty, WidthProperty, HeightProperty, @@ -427,11 +427,12 @@ namespace Avalonia.Layout /// After a call to this method in a control's static constructor, any change to the /// property will cause to be called on the element. /// - protected static void AffectsMeasure(params AvaloniaProperty[] properties) + protected static void AffectsMeasure(params AvaloniaProperty[] properties) + where T : class, ILayoutable { foreach (var property in properties) { - property.Changed.Subscribe(AffectsMeasureInvalidate); + property.Changed.Subscribe(AffectsMeasureInvalidate); } } @@ -443,11 +444,12 @@ namespace Avalonia.Layout /// After a call to this method in a control's static constructor, any change to the /// property will cause to be called on the element. /// - protected static void AffectsArrange(params AvaloniaProperty[] properties) + protected static void AffectsArrange(params AvaloniaProperty[] properties) + where T : class, ILayoutable { foreach (var property in properties) { - property.Changed.Subscribe(AffectsArrangeInvalidate); + property.Changed.Subscribe(AffectsArrangeInvalidate); } } @@ -636,9 +638,10 @@ namespace Avalonia.Layout /// Calls on the control on which a property changed. /// /// The event args. - private static void AffectsMeasureInvalidate(AvaloniaPropertyChangedEventArgs e) + private static void AffectsMeasureInvalidate(AvaloniaPropertyChangedEventArgs e) + where T : class, ILayoutable { - ILayoutable control = e.Sender as ILayoutable; + var control = e.Sender as T; control?.InvalidateMeasure(); } @@ -646,9 +649,10 @@ namespace Avalonia.Layout /// Calls on the control on which a property changed. /// /// The event args. - private static void AffectsArrangeInvalidate(AvaloniaPropertyChangedEventArgs e) + private static void AffectsArrangeInvalidate(AvaloniaPropertyChangedEventArgs e) + where T : class, ILayoutable { - ILayoutable control = e.Sender as ILayoutable; + var control = e.Sender as T; control?.InvalidateArrange(); } diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 81e1a93a6f..c2db20306e 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -100,7 +100,7 @@ namespace Avalonia /// static Visual() { - AffectsRender( + AffectsRender( BoundsProperty, ClipProperty, ClipToBoundsProperty, @@ -320,11 +320,12 @@ namespace Avalonia /// on the control which when changed should cause a redraw. This is similar to WPF's /// FrameworkPropertyMetadata.AffectsRender flag. /// - protected static void AffectsRender(params AvaloniaProperty[] properties) + protected static void AffectsRender(params AvaloniaProperty[] properties) + where T : class, IVisual { foreach (var property in properties) { - property.Changed.Subscribe(AffectsRenderInvalidate); + property.Changed.Subscribe(AffectsRenderInvalidate); } } @@ -416,9 +417,10 @@ namespace Avalonia /// Called when a property changes that should invalidate the visual. /// /// The event args. - private static void AffectsRenderInvalidate(AvaloniaPropertyChangedEventArgs e) + private static void AffectsRenderInvalidate(AvaloniaPropertyChangedEventArgs e) + where T : class, IVisual { - (e.Sender as Visual)?.InvalidateVisual(); + (e.Sender as T)?.InvalidateVisual(); } /// From 576cc915731892c2c6f55b928ed0588d5ecac889 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 7 Sep 2018 02:30:00 +0200 Subject: [PATCH 07/29] Make Pseudoclass method typed. --- src/Avalonia.Controls/Button.cs | 2 +- src/Avalonia.Controls/ButtonSpinner.cs | 6 +++--- src/Avalonia.Controls/ContentControl.cs | 4 ++-- src/Avalonia.Controls/Expander.cs | 10 +++++----- src/Avalonia.Controls/Primitives/ScrollBar.cs | 4 ++-- .../Primitives/ToggleButton.cs | 6 +++--- src/Avalonia.Controls/Primitives/Track.cs | 2 ++ src/Avalonia.Controls/ProgressBar.cs | 6 +++--- src/Avalonia.Controls/Slider.cs | 2 ++ src/Avalonia.Input/InputElement.cs | 6 +++--- src/Avalonia.Styling/StyledElement.cs | 20 +++++++++++-------- 11 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/Avalonia.Controls/Button.cs b/src/Avalonia.Controls/Button.cs index fa69d72d67..24b2af7996 100644 --- a/src/Avalonia.Controls/Button.cs +++ b/src/Avalonia.Controls/Button.cs @@ -80,7 +80,7 @@ namespace Avalonia.Controls FocusableProperty.OverrideDefaultValue(typeof(Button), true); CommandProperty.Changed.Subscribe(CommandChanged); IsDefaultProperty.Changed.Subscribe(IsDefaultChanged); - PseudoClass(IsPressedProperty, ":pressed"); + PseudoClass