OxySKElement.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="OxySKElement.cs" company="OxyPlot">
  3. // Copyright (c) 2020 OxyPlot contributors
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. /*
  7. This is a modified copy of https://github.com/mono/SkiaSharp/blob/2550d14410839ebba2d541ec5b423261b9236197/source/SkiaSharp.Views/SkiaSharp.Views.WPF/SKElement.cs.
  8. License of the original file:
  9. ------------------------------------------
  10. Copyright (c) 2015-2016 Xamarin, Inc.
  11. Copyright (c) 2017-2018 Microsoft Corporation.
  12. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. ------------------------------------------
  16. Mmodifications include
  17. - A workaround for https://github.com/mono/SkiaSharp/issues/1236. Once the issue is fixed on their side, we should remove this file and replace usages by SKElement from the SkiaSharp.Views.WPF nuget package.
  18. - Simple RenderTransform detection to allow for non-unity scaling per https://github.com/oxyplot/oxyplot/issues/1785.
  19. */
  20. namespace OxyPlot.SkiaSharp.Wpf
  21. {
  22. using global::SkiaSharp;
  23. using global::SkiaSharp.Views.Desktop;
  24. using System;
  25. using System.ComponentModel;
  26. using System.Windows;
  27. using System.Windows.Media;
  28. using System.Windows.Media.Imaging;
  29. /// <summary>
  30. /// Provides a surface on which to render with the SkiaSharp graphics APIs.
  31. /// </summary>
  32. public class OxySKElement : FrameworkElement
  33. {
  34. /// <summary>
  35. /// A value indicating whether the element is being presented in design mode.
  36. /// </summary>
  37. private readonly bool designMode;
  38. /// <summary>
  39. /// The bitmap to which to render.
  40. /// </summary>
  41. private WriteableBitmap bitmap;
  42. /// <summary>
  43. /// A value indicating whether to ignore pixel scaling when determining the render buffer bitmap dimensions.
  44. /// </summary>
  45. private bool ignorePixelScaling;
  46. /// <summary>
  47. /// Initialises an instance of the <see cref="OxySKElement"/> class.
  48. /// </summary>
  49. public OxySKElement()
  50. {
  51. this.designMode = DesignerProperties.GetIsInDesignMode(this);
  52. RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor);
  53. }
  54. /// <summary>
  55. /// Invoked when the surface is painting.
  56. /// </summary>
  57. [Category("Appearance")]
  58. public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
  59. /// <summary>
  60. /// Gets the size of the render buffer bitmap, or <c>SKSize.Empty</c> if there is no current render buffer bitmap.
  61. /// </summary>
  62. [Bindable(false)]
  63. [Browsable(false)]
  64. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  65. [EditorBrowsable(EditorBrowsableState.Never)]
  66. public SKSize CanvasSize => this.bitmap == null ? SKSize.Empty : new SKSize(this.bitmap.PixelWidth, this.bitmap.PixelHeight);
  67. /// <summary>
  68. /// Gets or sets a value indicating whether to ignore pixel scaling when determining the render buffer bitmap dimensions.
  69. /// </summary>
  70. public bool IgnorePixelScaling
  71. {
  72. get { return this.ignorePixelScaling; }
  73. set
  74. {
  75. this.ignorePixelScaling = value;
  76. this.InvalidateVisual();
  77. }
  78. }
  79. /// <summary>
  80. /// Raises the <see cref="PaintSurface"/> event with the given <see cref="SKPaintSurfaceEventArgs"/>.
  81. /// </summary>
  82. /// <param name="e">The skia surface and associated information ready for drawing.</param>
  83. protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
  84. {
  85. // invoke the event
  86. PaintSurface?.Invoke(this, e);
  87. }
  88. /// <inheritdoc/>
  89. protected override void OnRender(DrawingContext drawingContext)
  90. {
  91. if (this.designMode)
  92. {
  93. return;
  94. }
  95. if (this.Visibility != Visibility.Visible)
  96. {
  97. return;
  98. }
  99. var size = this.CreateSize(out var scaleX, out var scaleY);
  100. var renderScale = this.GetRenderScale();
  101. // scale bitmap according to the renderScale
  102. var bitmapPixelWidth = (int)(size.Width * renderScale);
  103. var bitmapPixelHeight = (int)(size.Height * renderScale);
  104. if (size.Width <= 0 || size.Height <= 0)
  105. {
  106. return;
  107. }
  108. var info = new SKImageInfo(bitmapPixelWidth, bitmapPixelHeight, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
  109. // reset the bitmap if the size has changed
  110. if (this.bitmap == null || info.Width != this.bitmap.PixelWidth || info.Height != this.bitmap.PixelHeight)
  111. {
  112. this.bitmap = new WriteableBitmap(bitmapPixelWidth, bitmapPixelHeight, 96 * scaleX, 96 * scaleY, PixelFormats.Pbgra32, null);
  113. }
  114. // draw on the bitmap
  115. this.bitmap.Lock();
  116. using (var surface = SKSurface.Create(info, this.bitmap.BackBuffer, this.bitmap.BackBufferStride))
  117. {
  118. this.OnPaintSurface(new SKPaintSurfaceEventArgs(surface, info));
  119. }
  120. // draw the bitmap to the screen
  121. this.bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmapPixelWidth, bitmapPixelHeight));
  122. this.bitmap.Unlock();
  123. // get window to screen offset
  124. var ancestor = GetAncestorVisualFromVisualTree(this);
  125. var visualOffset = ancestor != null ? this.TransformToAncestor(ancestor).Transform(default) : default;
  126. // calculate offset to physical pixels
  127. var offsetX = ((visualOffset.X * scaleX) % 1) / scaleX;
  128. var offsetY = ((visualOffset.Y * scaleY) % 1) / scaleY;
  129. // draw, scaling back down from the (rounded) bitmap dimensions
  130. drawingContext.DrawImage(this.bitmap, new Rect(-offsetX, -offsetY, this.bitmap.Width / renderScale, this.bitmap.Height / renderScale));
  131. }
  132. /// <inheritdoc/>
  133. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  134. {
  135. base.OnRenderSizeChanged(sizeInfo);
  136. this.InvalidateVisual();
  137. }
  138. /// <summary>
  139. /// Determines the size of the render buffer bitmap, and the scale at which to draw.
  140. /// </summary>
  141. /// <param name="scaleX">The horizontal scale.</param>
  142. /// <param name="scaleY">The vertical scale.</param>
  143. /// <returns>The size in pixels of the bitmap.</returns>
  144. private SKSizeI CreateSize(out double scaleX, out double scaleY)
  145. {
  146. scaleX = 1.0;
  147. scaleY = 1.0;
  148. var w = this.ActualWidth;
  149. var h = this.ActualHeight;
  150. if (!IsPositive(w) || !IsPositive(h))
  151. {
  152. return SKSizeI.Empty;
  153. }
  154. if (this.IgnorePixelScaling)
  155. {
  156. return new SKSizeI((int)w, (int)h);
  157. }
  158. var compositionTarget = PresentationSource.FromVisual(this)?.CompositionTarget;
  159. if (compositionTarget != null)
  160. {
  161. var m = compositionTarget.TransformToDevice;
  162. scaleX = m.M11;
  163. scaleY = m.M22;
  164. }
  165. return new SKSizeI((int)(w * scaleX), (int)(h * scaleY));
  166. static bool IsPositive(double value)
  167. {
  168. return !double.IsNaN(value) && !double.IsInfinity(value) && value > 0;
  169. }
  170. }
  171. /// <summary>
  172. /// Returns a reference to the visual object that hosts the dependency object in the visual tree.
  173. /// </summary>
  174. /// <returns> The host visual from the visual tree.</returns>
  175. private Visual GetAncestorVisualFromVisualTree(DependencyObject startElement)
  176. {
  177. DependencyObject child = startElement;
  178. DependencyObject parent = VisualTreeHelper.GetParent(child);
  179. while (parent != null)
  180. {
  181. child = parent;
  182. parent = VisualTreeHelper.GetParent(child);
  183. }
  184. return child is Visual visualChild ? visualChild : Window.GetWindow(this);
  185. }
  186. /// <summary>
  187. /// Determines the scaling transform applied to the control.
  188. /// </summary>
  189. /// <returns>The scale factor.</returns>
  190. public virtual double GetRenderScale()
  191. {
  192. var transform = VisualTreeHelper.GetTransform(this)?.Value ?? Matrix.Identity;
  193. DependencyObject control = VisualTreeHelper.GetParent(this);
  194. while (control != null)
  195. {
  196. if (control is Visual v && VisualTreeHelper.GetTransform(v) is Transform vt)
  197. {
  198. transform *= vt.Value;
  199. }
  200. control = VisualTreeHelper.GetParent(control);
  201. }
  202. return Math.Max(transform.M11, transform.M22);
  203. }
  204. }
  205. }