Direct2D1Platform.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. using Avalonia.Direct2D1.Media;
  6. using Avalonia.Media;
  7. using Avalonia.Platform;
  8. using Avalonia.Controls;
  9. using Avalonia.Rendering;
  10. namespace Avalonia
  11. {
  12. public static class Direct2DApplicationExtensions
  13. {
  14. public static T UseDirect2D1<T>(this T builder) where T : AppBuilderBase<T>, new()
  15. {
  16. builder.UseRenderingSubsystem(Direct2D1.Direct2D1Platform.Initialize, "Direct2D1");
  17. return builder;
  18. }
  19. }
  20. }
  21. namespace Avalonia.Direct2D1
  22. {
  23. public class Direct2D1Platform : IPlatformRenderInterface, IRendererFactory
  24. {
  25. private static readonly Direct2D1Platform s_instance = new Direct2D1Platform();
  26. private static readonly SharpDX.Direct2D1.Factory s_d2D1Factory =
  27. #if DEBUG
  28. new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.Error);
  29. #else
  30. new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded, SharpDX.Direct2D1.DebugLevel.None);
  31. #endif
  32. private static readonly SharpDX.DirectWrite.Factory s_dwfactory = new SharpDX.DirectWrite.Factory();
  33. private static readonly SharpDX.WIC.ImagingFactory s_imagingFactory = new SharpDX.WIC.ImagingFactory();
  34. private static readonly SharpDX.DXGI.Device s_dxgiDevice;
  35. private static readonly SharpDX.Direct2D1.Device s_d2D1Device;
  36. static Direct2D1Platform()
  37. {
  38. var featureLevels = new[]
  39. {
  40. SharpDX.Direct3D.FeatureLevel.Level_11_1,
  41. SharpDX.Direct3D.FeatureLevel.Level_11_0,
  42. SharpDX.Direct3D.FeatureLevel.Level_10_1,
  43. SharpDX.Direct3D.FeatureLevel.Level_10_0,
  44. SharpDX.Direct3D.FeatureLevel.Level_9_3,
  45. SharpDX.Direct3D.FeatureLevel.Level_9_2,
  46. SharpDX.Direct3D.FeatureLevel.Level_9_1,
  47. };
  48. using (var d3dDevice = new SharpDX.Direct3D11.Device(
  49. SharpDX.Direct3D.DriverType.Hardware,
  50. SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
  51. featureLevels))
  52. {
  53. s_dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
  54. }
  55. using (var factory1 = s_d2D1Factory.QueryInterface<SharpDX.Direct2D1.Factory1>())
  56. {
  57. s_d2D1Device = new SharpDX.Direct2D1.Device(factory1, s_dxgiDevice);
  58. }
  59. }
  60. public static void Initialize() => AvaloniaLocator.CurrentMutable
  61. .Bind<IPlatformRenderInterface>().ToConstant(s_instance)
  62. .Bind<IRendererFactory>().ToConstant(s_instance)
  63. .BindToSelf(s_d2D1Factory)
  64. .BindToSelf(s_dwfactory)
  65. .BindToSelf(s_imagingFactory)
  66. .BindToSelf(s_dxgiDevice)
  67. .BindToSelf(s_d2D1Device);
  68. public IBitmapImpl CreateBitmap(int width, int height)
  69. {
  70. return new BitmapImpl(s_imagingFactory, width, height);
  71. }
  72. public IFormattedTextImpl CreateFormattedText(
  73. string text,
  74. string fontFamily,
  75. double fontSize,
  76. FontStyle fontStyle,
  77. TextAlignment textAlignment,
  78. FontWeight fontWeight,
  79. TextWrapping wrapping)
  80. {
  81. return new FormattedTextImpl(text, fontFamily, fontSize, fontStyle, textAlignment, fontWeight, wrapping);
  82. }
  83. public IRenderer CreateRenderer(IRenderRoot root, IRenderLoop renderLoop)
  84. {
  85. return new Renderer(root, renderLoop);
  86. }
  87. public IRenderTarget CreateRenderTarget(IPlatformHandle handle)
  88. {
  89. if (handle.HandleDescriptor == "HWND")
  90. {
  91. return new HwndRenderTarget(handle.Handle);
  92. }
  93. else
  94. {
  95. throw new NotSupportedException(string.Format(
  96. "Don't know how to create a Direct2D1 renderer from a '{0}' handle",
  97. handle.HandleDescriptor));
  98. }
  99. }
  100. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(int width, int height)
  101. {
  102. return new RenderTargetBitmapImpl(s_imagingFactory, s_d2D1Device.Factory, width, height);
  103. }
  104. public IStreamGeometryImpl CreateStreamGeometry()
  105. {
  106. return new StreamGeometryImpl();
  107. }
  108. public IBitmapImpl LoadBitmap(string fileName)
  109. {
  110. return new BitmapImpl(s_imagingFactory, fileName);
  111. }
  112. public IBitmapImpl LoadBitmap(Stream stream)
  113. {
  114. return new BitmapImpl(s_imagingFactory, stream);
  115. }
  116. }
  117. }