Direct2D1Platform.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.Collections.Generic;
  5. using System.IO;
  6. using Avalonia.Controls;
  7. using Avalonia.Controls.Platform.Surfaces;
  8. using Avalonia.Direct2D1.Media;
  9. using Avalonia.Direct2D1.Media.Imaging;
  10. using Avalonia.Media;
  11. using Avalonia.Platform;
  12. namespace Avalonia
  13. {
  14. public static class Direct2DApplicationExtensions
  15. {
  16. public static T UseDirect2D1<T>(this T builder) where T : AppBuilderBase<T>, new()
  17. {
  18. builder.UseRenderingSubsystem(Direct2D1.Direct2D1Platform.Initialize, "Direct2D1");
  19. return builder;
  20. }
  21. }
  22. }
  23. namespace Avalonia.Direct2D1
  24. {
  25. public class Direct2D1Platform : IPlatformRenderInterface
  26. {
  27. private static readonly Direct2D1Platform s_instance = new Direct2D1Platform();
  28. public static SharpDX.Direct3D11.Device Direct3D11Device { get; private set; }
  29. public static SharpDX.Direct2D1.Factory1 Direct2D1Factory { get; private set; }
  30. public static SharpDX.Direct2D1.Device Direct2D1Device { get; private set; }
  31. public static SharpDX.DirectWrite.Factory1 DirectWriteFactory { get; private set; }
  32. public static SharpDX.WIC.ImagingFactory ImagingFactory { get; private set; }
  33. public static SharpDX.DXGI.Device1 DxgiDevice { get; private set; }
  34. public IEnumerable<string> InstalledFontNames
  35. {
  36. get
  37. {
  38. var cache = Direct2D1FontCollectionCache.s_installedFontCollection;
  39. var length = cache.FontFamilyCount;
  40. for (int i = 0; i < length; i++)
  41. {
  42. var names = cache.GetFontFamily(i).FamilyNames;
  43. yield return names.GetString(0);
  44. }
  45. }
  46. }
  47. private static readonly object s_initLock = new object();
  48. private static bool s_initialized = false;
  49. internal static void InitializeDirect2D()
  50. {
  51. lock (s_initLock)
  52. {
  53. if (s_initialized)
  54. {
  55. return;
  56. }
  57. #if DEBUG
  58. try
  59. {
  60. Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
  61. SharpDX.Direct2D1.FactoryType.MultiThreaded,
  62. SharpDX.Direct2D1.DebugLevel.Error);
  63. }
  64. catch
  65. {
  66. //
  67. }
  68. #endif
  69. if (Direct2D1Factory == null)
  70. {
  71. Direct2D1Factory = new SharpDX.Direct2D1.Factory1(
  72. SharpDX.Direct2D1.FactoryType.MultiThreaded,
  73. SharpDX.Direct2D1.DebugLevel.None);
  74. }
  75. using (var factory = new SharpDX.DirectWrite.Factory())
  76. {
  77. DirectWriteFactory = factory.QueryInterface<SharpDX.DirectWrite.Factory1>();
  78. }
  79. ImagingFactory = new SharpDX.WIC.ImagingFactory();
  80. var featureLevels = new[]
  81. {
  82. SharpDX.Direct3D.FeatureLevel.Level_11_1,
  83. SharpDX.Direct3D.FeatureLevel.Level_11_0,
  84. SharpDX.Direct3D.FeatureLevel.Level_10_1,
  85. SharpDX.Direct3D.FeatureLevel.Level_10_0,
  86. SharpDX.Direct3D.FeatureLevel.Level_9_3,
  87. SharpDX.Direct3D.FeatureLevel.Level_9_2,
  88. SharpDX.Direct3D.FeatureLevel.Level_9_1,
  89. };
  90. Direct3D11Device = new SharpDX.Direct3D11.Device(
  91. SharpDX.Direct3D.DriverType.Hardware,
  92. SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport,
  93. featureLevels);
  94. DxgiDevice = Direct3D11Device.QueryInterface<SharpDX.DXGI.Device1>();
  95. Direct2D1Device = new SharpDX.Direct2D1.Device(Direct2D1Factory, DxgiDevice);
  96. s_initialized = true;
  97. }
  98. }
  99. public static void Initialize()
  100. {
  101. InitializeDirect2D();
  102. AvaloniaLocator.CurrentMutable.Bind<IPlatformRenderInterface>().ToConstant(s_instance);
  103. SharpDX.Configuration.EnableReleaseOnFinalizer = true;
  104. }
  105. public IBitmapImpl CreateBitmap(PixelSize size, Vector dpi)
  106. {
  107. return new WicBitmapImpl(size, dpi);
  108. }
  109. public IFormattedTextImpl CreateFormattedText(
  110. string text,
  111. Typeface typeface,
  112. TextAlignment textAlignment,
  113. TextWrapping wrapping,
  114. Size constraint,
  115. IReadOnlyList<FormattedTextStyleSpan> spans)
  116. {
  117. return new FormattedTextImpl(
  118. text,
  119. typeface,
  120. textAlignment,
  121. wrapping,
  122. constraint,
  123. spans);
  124. }
  125. public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
  126. {
  127. foreach (var s in surfaces)
  128. {
  129. if (s is IPlatformHandle nativeWindow)
  130. {
  131. if (nativeWindow.HandleDescriptor != "HWND")
  132. {
  133. throw new NotSupportedException("Don't know how to create a Direct2D1 renderer from " +
  134. nativeWindow.HandleDescriptor);
  135. }
  136. return new HwndRenderTarget(nativeWindow);
  137. }
  138. if (s is IExternalDirect2DRenderTargetSurface external)
  139. {
  140. return new ExternalRenderTarget(external);
  141. }
  142. if (s is IFramebufferPlatformSurface fb)
  143. {
  144. return new FramebufferShimRenderTarget(fb);
  145. }
  146. }
  147. throw new NotSupportedException("Don't know how to create a Direct2D1 renderer from any of provided surfaces");
  148. }
  149. public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi)
  150. {
  151. return new WicRenderTargetBitmapImpl(size, dpi);
  152. }
  153. public IWriteableBitmapImpl CreateWriteableBitmap(PixelSize size, Vector dpi, PixelFormat? format = null)
  154. {
  155. return new WriteableWicBitmapImpl(size, dpi, format);
  156. }
  157. public IStreamGeometryImpl CreateStreamGeometry()
  158. {
  159. return new StreamGeometryImpl();
  160. }
  161. public IBitmapImpl LoadBitmap(string fileName)
  162. {
  163. return new WicBitmapImpl(fileName);
  164. }
  165. public IBitmapImpl LoadBitmap(Stream stream)
  166. {
  167. return new WicBitmapImpl(stream);
  168. }
  169. public IBitmapImpl LoadBitmap(PixelFormat format, IntPtr data, PixelSize size, Vector dpi, int stride)
  170. {
  171. return new WicBitmapImpl(format, data, size, dpi, stride);
  172. }
  173. }
  174. }