Browse Source

Moved Direct2D initialization away from static constructor

Nikita Tsukanov 8 years ago
parent
commit
06aa7780fb

+ 1 - 1
src/Windows/Avalonia.Direct2D1/Avalonia.Direct2D1.csproj

@@ -73,7 +73,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="RenderTarget.cs" />
     <Compile Include="SwapChainRenderTarget.cs" />
-    <Compile Include="WindowsVersionChecker.cs" />
+    <Compile Include="Direct2DChecker.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />

+ 59 - 32
src/Windows/Avalonia.Direct2D1/Direct2D1Platform.cs

@@ -31,49 +31,76 @@ 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 SharpDX.Direct2D1.Factory s_d2D1Factory;
 
-        private static readonly SharpDX.WIC.ImagingFactory s_imagingFactory = new SharpDX.WIC.ImagingFactory();
+        private static SharpDX.DirectWrite.Factory s_dwfactory;
 
-        private static readonly SharpDX.DXGI.Device s_dxgiDevice;
+        private static SharpDX.WIC.ImagingFactory s_imagingFactory;
 
-        private static readonly SharpDX.Direct2D1.Device s_d2D1Device;
+        private static SharpDX.DXGI.Device s_dxgiDevice;
 
-        static Direct2D1Platform()
-        {
-            var featureLevels = new[]
-            {
-                SharpDX.Direct3D.FeatureLevel.Level_11_1,
-                SharpDX.Direct3D.FeatureLevel.Level_11_0,
-                SharpDX.Direct3D.FeatureLevel.Level_10_1,
-                SharpDX.Direct3D.FeatureLevel.Level_10_0,
-                SharpDX.Direct3D.FeatureLevel.Level_9_3,
-                SharpDX.Direct3D.FeatureLevel.Level_9_2,
-                SharpDX.Direct3D.FeatureLevel.Level_9_1,
-            };
-
-            using (var d3dDevice = new SharpDX.Direct3D11.Device(
-                SharpDX.Direct3D.DriverType.Hardware,
-                SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
-                featureLevels))
-            {
-                s_dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
-            }
+        private static SharpDX.Direct2D1.Device s_d2D1Device;
+
+        private static readonly object s_initLock = new object();
+        private static bool s_initialized = false;
 
-            using (var factory1 = s_d2D1Factory.QueryInterface<SharpDX.Direct2D1.Factory1>())
+        internal static void InitializeDirect2D()
+        {
+            lock (s_initLock)
             {
-                s_d2D1Device = new SharpDX.Direct2D1.Device(factory1, s_dxgiDevice);
+                if (s_initialized)
+                    return;
+#if DEBUG
+                try
+                {
+                    s_d2D1Factory =
+
+                        new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded,
+                            SharpDX.Direct2D1.DebugLevel.Error);
+                }
+                catch
+                {
+                    //
+                }
+#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,
+                        SharpDX.Direct2D1.DebugLevel.None);
+
+
+                var featureLevels = new[]
+                {
+                    SharpDX.Direct3D.FeatureLevel.Level_11_1,
+                    SharpDX.Direct3D.FeatureLevel.Level_11_0,
+                    SharpDX.Direct3D.FeatureLevel.Level_10_1,
+                    SharpDX.Direct3D.FeatureLevel.Level_10_0,
+                    SharpDX.Direct3D.FeatureLevel.Level_9_3,
+                    SharpDX.Direct3D.FeatureLevel.Level_9_2,
+                    SharpDX.Direct3D.FeatureLevel.Level_9_1,
+                };
+
+                using (var d3dDevice = new SharpDX.Direct3D11.Device(
+                    SharpDX.Direct3D.DriverType.Hardware,
+                    SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport |
+                    SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
+                    featureLevels))
+                {
+                    s_dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
+                }
+
+                using (var factory1 = s_d2D1Factory.QueryInterface<SharpDX.Direct2D1.Factory1>())
+                {
+                    s_d2D1Device = new SharpDX.Direct2D1.Device(factory1, s_dxgiDevice);
+                }
+                s_initialized = true;
             }
         }
 
         public static void Initialize()
         {
+            InitializeDirect2D();
             AvaloniaLocator.CurrentMutable
                         .Bind<IPlatformRenderInterface>().ToConstant(s_instance)
                         .BindToSelf(s_d2D1Factory)

+ 29 - 0
src/Windows/Avalonia.Direct2D1/Direct2DChecker.cs

@@ -0,0 +1,29 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Avalonia.Platform;
+
+namespace Avalonia.Direct2D1
+{
+    class Direct2DChecker : IModuleEnvironmentChecker
+    {
+        //Direct2D backend doesn't work on some machines anymore
+        public bool IsCompatible
+        {
+            get
+            {
+                try
+                {
+                    Direct2D1Platform.InitializeDirect2D();
+                    return true;
+                }
+                catch
+                {
+                    return false;
+                }
+            }
+        }
+    }
+}

+ 1 - 1
src/Windows/Avalonia.Direct2D1/Properties/AssemblyInfo.cs

@@ -7,5 +7,5 @@ using Avalonia.Direct2D1;
 
 [assembly: AssemblyTitle("Avalonia.Direct2D1")]
 [assembly: ExportRenderingSubsystem(OperatingSystemType.WinNT, 1, "Direct2D1", typeof(Direct2D1Platform), nameof(Direct2D1Platform.Initialize),
-    typeof(WindowsVersionChecker))]
+    typeof(Direct2DChecker))]
 

+ 0 - 15
src/Windows/Avalonia.Direct2D1/WindowsVersionChecker.cs

@@ -1,15 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using Avalonia.Platform;
-
-namespace Avalonia.Direct2D1
-{
-    class WindowsVersionChecker : IModuleEnvironmentChecker
-    {
-        //Direct2D backend doesn't work with Win7 anymore
-        public bool IsCompatible => Environment.OSVersion.Version >= new Version(6, 2);
-    }
-}