Browse Source

Create D2D1 device with a factory.

Jeremy Koritzinsky 9 years ago
parent
commit
d02b7cbe92

+ 11 - 3
src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs

@@ -8,7 +8,6 @@ using Avalonia.Media;
 using Avalonia.Platform;
 using Avalonia.Controls;
 using Avalonia.Rendering;
-using SharpDX.Direct3D11;
 
 namespace Avalonia
 {
@@ -28,6 +27,12 @@ namespace Avalonia.Direct2D1
     {
         private static readonly Direct2D1Platform s_instance = new Direct2D1Platform();
 
+        private static readonly SharpDX.Direct2D1.Factory s_d2D1Factory =
+#if DEBUG
+            new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.Error);
+#else
+            new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.None);
+#endif
         private static readonly SharpDX.DirectWrite.Factory s_dwfactory = new SharpDX.DirectWrite.Factory();
 
         private static readonly SharpDX.WIC.ImagingFactory s_imagingFactory = new SharpDX.WIC.ImagingFactory();
@@ -59,13 +64,16 @@ namespace Avalonia.Direct2D1
                 s_dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
             }
 
-            s_d2D1Device = new SharpDX.Direct2D1.Device(s_dxgiDevice);
+            using (var factory1 = s_d2D1Factory.QueryInterface<SharpDX.Direct2D1.Factory1>())
+            {
+                s_d2D1Device = new SharpDX.Direct2D1.Device(factory1, s_dxgiDevice);
+            }
         }
 
         public static void Initialize() => AvaloniaLocator.CurrentMutable
             .Bind<IPlatformRenderInterface>().ToConstant(s_instance)
             .Bind<IRendererFactory>().ToConstant(s_instance)
-            .BindToSelf(s_d2D1Device.Factory)
+            .BindToSelf(s_d2D1Factory)
             .BindToSelf(s_dwfactory)
             .BindToSelf(s_imagingFactory)
             .BindToSelf(s_dxgiDevice)

+ 0 - 79
src/Windows/Avalonia.Direct2D1/RenderTarget.cs

@@ -13,42 +13,11 @@ namespace Avalonia.Direct2D1
 {
     public class RenderTarget : IRenderTarget
     {
-        private readonly IntPtr _hwnd;
-        private Size2 _savedSize;
-        private Size2F _savedDpi;
-
         /// <summary>
         /// The render target.
         /// </summary>
         private readonly SharpDX.Direct2D1.RenderTarget _renderTarget;
 
-        /// <summary>
-        /// Initializes a new instance of the <see cref="RenderTarget"/> class.
-        /// </summary>
-        /// <param name="hwnd">The window handle.</param>
-        public RenderTarget(IntPtr hwnd)
-        {
-            _hwnd = hwnd;
-            Direct2DFactory = AvaloniaLocator.Current.GetService<Factory>();
-            DirectWriteFactory = AvaloniaLocator.Current.GetService<DwFactory>();
-
-            RenderTargetProperties renderTargetProperties = new RenderTargetProperties
-            {
-            };
-
-            HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties
-            {
-                Hwnd = hwnd,
-                PixelSize = _savedSize = GetWindowSize(),
-                PresentOptions = PresentOptions.Immediately,
-            };
-
-            _renderTarget = new WindowRenderTarget(
-                Direct2DFactory,
-                renderTargetProperties,
-                hwndProperties);
-        }
-
         /// <summary>
         /// Initializes a new instance of the <see cref="RenderTarget"/> class.
         /// </summary>
@@ -82,24 +51,6 @@ namespace Avalonia.Direct2D1
         /// <returns>An <see cref="Avalonia.Media.DrawingContext"/>.</returns>
         public DrawingContext CreateDrawingContext()
         {
-            var window = _renderTarget as WindowRenderTarget;
-
-            if (window != null)
-            {
-                var size = GetWindowSize();
-                var dpi = GetWindowDpi();
-
-                if (size != _savedSize)
-                {
-                    window.Resize(_savedSize = size);
-                }
-
-                if (dpi != _savedDpi)
-                {
-                    window.DotsPerInch = _savedDpi = dpi;
-                }
-            }
-
             return new DrawingContext(new Media.DrawingContext(_renderTarget, DirectWriteFactory));
         }
 
@@ -107,35 +58,5 @@ namespace Avalonia.Direct2D1
         {
             _renderTarget.Dispose();
         }
-
-        private Size2F GetWindowDpi()
-        {
-            if (UnmanagedMethods.ShCoreAvailable)
-            {
-                uint dpix, dpiy;
-
-                var monitor = UnmanagedMethods.MonitorFromWindow(
-                    _hwnd,
-                    UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST);
-
-                if (UnmanagedMethods.GetDpiForMonitor(
-                        monitor,
-                        UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI,
-                        out dpix,
-                        out dpiy) == 0)
-                {
-                    return new Size2F(dpix, dpiy);
-                }
-            }
-
-            return new Size2F(96, 96);
-        }
-
-        private Size2 GetWindowSize()
-        {
-            UnmanagedMethods.RECT rc;
-            UnmanagedMethods.GetClientRect(_hwnd, out rc);
-            return new Size2(rc.right - rc.left, rc.bottom - rc.top);
-        }
     }
 }

+ 2 - 2
src/Windows/Avalonia.Direct2D1/SwapChainRenderTarget.cs

@@ -73,8 +73,8 @@ namespace Avalonia.Direct2D1
 
         public void Dispose()
         {
-            _deviceContext.Dispose();
-            _swapChain.Dispose();
+            _deviceContext?.Dispose();
+            _swapChain?.Dispose();
         }
 
         private void CreateSwapChain()