RenderDataNodes.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Media;
  4. using Avalonia.Platform;
  5. using Avalonia.Rendering.SceneGraph;
  6. using Avalonia.Threading;
  7. using Avalonia.Utilities;
  8. namespace Avalonia.Rendering.Composition.Drawing.Nodes;
  9. enum RenderDataPopNodeType
  10. {
  11. Transform,
  12. Clip,
  13. GeometryClip,
  14. Opacity,
  15. OpacityMask
  16. }
  17. interface IRenderDataServerResourcesCollector
  18. {
  19. void AddRenderDataServerResource(object? obj);
  20. }
  21. interface IRenderDataItemWithServerResources : IRenderDataItem
  22. {
  23. void Collect(IRenderDataServerResourcesCollector collector);
  24. }
  25. struct RenderDataNodeRenderContext : IDisposable
  26. {
  27. private Stack<Matrix>? _stack;
  28. private static readonly ThreadSafeObjectPool<Stack<Matrix>> s_matrixStackPool = new();
  29. public RenderDataNodeRenderContext(IDrawingContextImpl context)
  30. {
  31. Context = context;
  32. }
  33. public IDrawingContextImpl Context { get; }
  34. public Stack<Matrix> MatrixStack
  35. {
  36. get => _stack ??= s_matrixStackPool.Get();
  37. }
  38. public void Dispose()
  39. {
  40. if (_stack != null)
  41. {
  42. _stack.Clear();
  43. s_matrixStackPool.ReturnAndSetNull(ref _stack);
  44. }
  45. }
  46. }
  47. interface IRenderDataItem
  48. {
  49. /// <summary>
  50. /// Renders the node to a drawing context.
  51. /// </summary>
  52. /// <param name="context">The drawing context.</param>
  53. void Invoke(ref RenderDataNodeRenderContext context);
  54. /// <summary>
  55. /// Gets the bounds of the visible content in the node in global coordinates.
  56. /// </summary>
  57. Rect? Bounds { get; }
  58. /// <summary>
  59. /// Hit test the geometry in this node.
  60. /// </summary>
  61. /// <param name="p">The point in global coordinates.</param>
  62. /// <returns>True if the point hits the node's geometry; otherwise false.</returns>
  63. /// <remarks>
  64. /// This method does not recurse to childs, if you want
  65. /// to hit test children they must be hit tested manually.
  66. /// </remarks>
  67. bool HitTest(Point p);
  68. }
  69. class RenderDataCustomNode : IRenderDataItem
  70. {
  71. public ICustomDrawOperation? Operation { get; set; }
  72. public bool HitTest(Point p) => Operation?.HitTest(p) ?? false;
  73. public void Invoke(ref RenderDataNodeRenderContext context) => Operation?.Render(new(context.Context, false));
  74. public Rect? Bounds => Operation?.Bounds;
  75. }
  76. abstract class RenderDataPushNode : IRenderDataItem, IDisposable
  77. {
  78. public PooledInlineList<IRenderDataItem> Children;
  79. public abstract void Push(ref RenderDataNodeRenderContext context);
  80. public abstract void Pop(ref RenderDataNodeRenderContext context);
  81. public void Invoke(ref RenderDataNodeRenderContext context)
  82. {
  83. if (Children.Count == 0)
  84. return;
  85. Push(ref context);
  86. foreach (var ch in Children)
  87. ch.Invoke(ref context);
  88. Pop(ref context);
  89. }
  90. public virtual Rect? Bounds
  91. {
  92. get
  93. {
  94. if (Children.Count == 0)
  95. return null;
  96. Rect? union = null;
  97. foreach (var i in Children)
  98. union = Rect.Union(union, i.Bounds);
  99. return union;
  100. }
  101. }
  102. public virtual bool HitTest(Point p)
  103. {
  104. if (Children.Count == 0)
  105. return false;
  106. foreach(var ch in Children)
  107. if (ch.HitTest(p))
  108. return true;
  109. return false;
  110. }
  111. public void Dispose()
  112. {
  113. if (Children.Count > 0)
  114. {
  115. foreach(var ch in Children)
  116. if (ch is RenderDataPushNode node)
  117. node.Dispose();
  118. Children.Dispose();
  119. }
  120. }
  121. }
  122. class RenderDataClipNode : RenderDataPushNode
  123. {
  124. public RoundedRect Rect { get; set; }
  125. public override void Push(ref RenderDataNodeRenderContext context) =>
  126. context.Context.PushClip(Rect);
  127. public override void Pop(ref RenderDataNodeRenderContext context) =>
  128. context.Context.PopClip();
  129. public override bool HitTest(Point p)
  130. {
  131. if (!Rect.Rect.Contains(p))
  132. return false;
  133. return base.HitTest(p);
  134. }
  135. }
  136. class RenderDataGeometryClipNode : RenderDataPushNode
  137. {
  138. public IGeometryImpl? Geometry { get; set; }
  139. public bool Contains(Point p) => Geometry?.FillContains(p) ?? false;
  140. public override void Push(ref RenderDataNodeRenderContext context)
  141. {
  142. if (Geometry != null)
  143. context.Context.PushGeometryClip(Geometry);
  144. }
  145. public override void Pop(ref RenderDataNodeRenderContext context)
  146. {
  147. if (Geometry != null)
  148. context.Context.PopGeometryClip();
  149. }
  150. public override bool HitTest(Point p)
  151. {
  152. if (Geometry != null && !Geometry.FillContains(p))
  153. return false;
  154. return base.HitTest(p);
  155. }
  156. }
  157. class RenderDataOpacityNode : RenderDataPushNode
  158. {
  159. public double Opacity { get; set; }
  160. public Rect BoundsRect { get; set; }
  161. public override void Push(ref RenderDataNodeRenderContext context)
  162. {
  163. if (Opacity != 1)
  164. context.Context.PushOpacity(Opacity, BoundsRect);
  165. }
  166. public override void Pop(ref RenderDataNodeRenderContext context)
  167. {
  168. if (Opacity != 1)
  169. context.Context.PopOpacity();
  170. }
  171. }
  172. abstract class RenderDataBrushAndPenNode : IRenderDataItemWithServerResources
  173. {
  174. public IBrush? ServerBrush { get; set; }
  175. public IPen? ServerPen { get; set; }
  176. public IPen? ClientPen { get; set; }
  177. public void Collect(IRenderDataServerResourcesCollector collector)
  178. {
  179. collector.AddRenderDataServerResource(ServerBrush);
  180. collector.AddRenderDataServerResource(ServerPen);
  181. }
  182. public abstract void Invoke(ref RenderDataNodeRenderContext context);
  183. public abstract Rect? Bounds { get; }
  184. public abstract bool HitTest(Point p);
  185. }