VisualNode.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.Linq;
  6. using System.Reactive.Disposables;
  7. using Avalonia.Media;
  8. using Avalonia.Platform;
  9. using Avalonia.Utilities;
  10. using Avalonia.VisualTree;
  11. namespace Avalonia.Rendering.SceneGraph
  12. {
  13. /// <summary>
  14. /// A node in the low-level scene graph representing an <see cref="IVisual"/>.
  15. /// </summary>
  16. internal class VisualNode : IVisualNode
  17. {
  18. private static readonly IReadOnlyList<IVisualNode> EmptyChildren = new IVisualNode[0];
  19. private static readonly IReadOnlyList<IRef<IDrawOperation>> EmptyDrawOperations = new IRef<IDrawOperation>[0];
  20. private Rect? _bounds;
  21. private double _opacity;
  22. private List<IVisualNode> _children;
  23. private List<IRef<IDrawOperation>> _drawOperations;
  24. private IRef<IDisposable> _drawOperationsRefCounter;
  25. private bool _drawOperationsCloned;
  26. private Matrix transformRestore;
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="VisualNode"/> class.
  29. /// </summary>
  30. /// <param name="visual">The visual that this node represents.</param>
  31. /// <param name="parent">The parent scene graph node, if any.</param>
  32. public VisualNode(IVisual visual, IVisualNode parent)
  33. {
  34. Contract.Requires<ArgumentNullException>(visual != null);
  35. Visual = visual;
  36. Parent = parent;
  37. HasAncestorGeometryClip = parent != null &&
  38. (parent.HasAncestorGeometryClip || parent.GeometryClip != null);
  39. }
  40. /// <inheritdoc/>
  41. public IVisual Visual { get; }
  42. /// <inheritdoc/>
  43. public IVisualNode Parent { get; }
  44. /// <inheritdoc/>
  45. public Matrix Transform { get; set; }
  46. /// <inheritdoc/>
  47. public Rect Bounds => _bounds ?? CalculateBounds();
  48. /// <inheritdoc/>
  49. public Rect ClipBounds { get; set; }
  50. /// <inheritdoc/>
  51. public bool ClipToBounds { get; set; }
  52. /// <inheritdoc/>
  53. public IGeometryImpl GeometryClip { get; set; }
  54. /// <inheritdoc/>
  55. public bool HasAncestorGeometryClip { get; }
  56. /// <inheritdoc/>
  57. public double Opacity
  58. {
  59. get { return _opacity; }
  60. set
  61. {
  62. if (_opacity != value)
  63. {
  64. _opacity = value;
  65. OpacityChanged = true;
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets the opacity mask for the scnee graph node.
  71. /// </summary>
  72. public IBrush OpacityMask { get; set; }
  73. /// <summary>
  74. /// Gets a value indicating whether this node in the scene graph has already
  75. /// been updated in the current update pass.
  76. /// </summary>
  77. public bool SubTreeUpdated { get; set; }
  78. /// <summary>
  79. /// Gets a value indicating whether the <see cref="Opacity"/> property has changed.
  80. /// </summary>
  81. public bool OpacityChanged { get; private set; }
  82. public IVisual LayerRoot { get; set; }
  83. /// <inheritdoc/>
  84. public IReadOnlyList<IVisualNode> Children => _children ?? EmptyChildren;
  85. /// <inheritdoc/>
  86. public IReadOnlyList<IRef<IDrawOperation>> DrawOperations => _drawOperations ?? EmptyDrawOperations;
  87. /// <summary>
  88. /// Adds a child to the <see cref="Children"/> collection.
  89. /// </summary>
  90. /// <param name="child">The child to add.</param>
  91. public void AddChild(IVisualNode child)
  92. {
  93. if (child.Disposed)
  94. {
  95. throw new ObjectDisposedException("Visual node for {node.Visual}");
  96. }
  97. EnsureChildrenCreated();
  98. _children.Add(child);
  99. }
  100. /// <summary>
  101. /// Adds an operation to the <see cref="DrawOperations"/> collection.
  102. /// </summary>
  103. /// <param name="operation">The operation to add.</param>
  104. public void AddDrawOperation(IRef<IDrawOperation> operation)
  105. {
  106. EnsureDrawOperationsCreated();
  107. _drawOperations.Add(operation.Clone());
  108. }
  109. /// <summary>
  110. /// Removes a child from the <see cref="Children"/> collection.
  111. /// </summary>
  112. /// <param name="child">The child to remove.</param>
  113. public void RemoveChild(IVisualNode child)
  114. {
  115. EnsureChildrenCreated();
  116. _children.Remove(child);
  117. }
  118. /// <summary>
  119. /// Replaces a child in the <see cref="Children"/> collection.
  120. /// </summary>
  121. /// <param name="index">The child to be replaced.</param>
  122. /// <param name="node">The child to add.</param>
  123. public void ReplaceChild(int index, IVisualNode node)
  124. {
  125. if (node.Disposed)
  126. {
  127. throw new ObjectDisposedException("Visual node for {node.Visual}");
  128. }
  129. EnsureChildrenCreated();
  130. _children[index] = node;
  131. }
  132. /// <summary>
  133. /// Replaces an item in the <see cref="DrawOperations"/> collection.
  134. /// </summary>
  135. /// <param name="index">The opeation to be replaced.</param>
  136. /// <param name="operation">The operation to add.</param>
  137. public void ReplaceDrawOperation(int index, IRef<IDrawOperation> operation)
  138. {
  139. EnsureDrawOperationsCreated();
  140. var old = _drawOperations[index];
  141. _drawOperations[index] = operation.Clone();
  142. old.Dispose();
  143. }
  144. /// <summary>
  145. /// Removes items in the <see cref="Children"/> collection from the specified index
  146. /// to the end.
  147. /// </summary>
  148. /// <param name="first">The index of the first child to be removed.</param>
  149. public void TrimChildren(int first)
  150. {
  151. if (first < _children?.Count)
  152. {
  153. EnsureChildrenCreated();
  154. for (int i = first; i < _children.Count - first; i++)
  155. {
  156. _children[i].Dispose();
  157. }
  158. _children.RemoveRange(first, _children.Count - first);
  159. }
  160. }
  161. /// <summary>
  162. /// Removes items in the <see cref="DrawOperations"/> collection from the specified index
  163. /// to the end.
  164. /// </summary>
  165. /// <param name="first">The index of the first operation to be removed.</param>
  166. public void TrimDrawOperations(int first)
  167. {
  168. if (first < _drawOperations?.Count)
  169. {
  170. EnsureDrawOperationsCreated();
  171. for (int i = first; i < _drawOperations.Count; i++)
  172. {
  173. _drawOperations[i].Dispose();
  174. }
  175. _drawOperations.RemoveRange(first, _drawOperations.Count - first);
  176. }
  177. }
  178. /// <summary>
  179. /// Makes a copy of the node
  180. /// </summary>
  181. /// <param name="parent">The new parent node.</param>
  182. /// <returns>A cloned node.</returns>
  183. public VisualNode Clone(IVisualNode parent)
  184. {
  185. return new VisualNode(Visual, parent)
  186. {
  187. Transform = Transform,
  188. ClipBounds = ClipBounds,
  189. ClipToBounds = ClipToBounds,
  190. GeometryClip = GeometryClip,
  191. _opacity = Opacity,
  192. OpacityMask = OpacityMask,
  193. _drawOperations = _drawOperations,
  194. _drawOperationsRefCounter = _drawOperationsRefCounter?.Clone(),
  195. _drawOperationsCloned = true,
  196. LayerRoot= LayerRoot,
  197. };
  198. }
  199. /// <inheritdoc/>
  200. public bool HitTest(Point p)
  201. {
  202. foreach (var operation in DrawOperations)
  203. {
  204. if (operation.Item.HitTest(p) == true)
  205. {
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /// <inheritdoc/>
  212. public void BeginRender(IDrawingContextImpl context, bool skipOpacity)
  213. {
  214. transformRestore = context.Transform;
  215. if (ClipToBounds)
  216. {
  217. context.Transform = Matrix.Identity;
  218. context.PushClip(ClipBounds);
  219. }
  220. context.Transform = Transform;
  221. if (Opacity != 1 && !skipOpacity)
  222. {
  223. context.PushOpacity(Opacity);
  224. }
  225. if (GeometryClip != null)
  226. {
  227. context.PushGeometryClip(GeometryClip);
  228. }
  229. if (OpacityMask != null)
  230. {
  231. context.PushOpacityMask(OpacityMask, ClipBounds);
  232. }
  233. }
  234. /// <inheritdoc/>
  235. public void EndRender(IDrawingContextImpl context, bool skipOpacity)
  236. {
  237. if (OpacityMask != null)
  238. {
  239. context.PopOpacityMask();
  240. }
  241. if (GeometryClip != null)
  242. {
  243. context.PopGeometryClip();
  244. }
  245. if (Opacity != 1 && !skipOpacity)
  246. {
  247. context.PopOpacity();
  248. }
  249. if (ClipToBounds)
  250. {
  251. context.Transform = Matrix.Identity;
  252. context.PopClip();
  253. }
  254. context.Transform = transformRestore;
  255. }
  256. private Rect CalculateBounds()
  257. {
  258. var result = new Rect();
  259. foreach (var operation in DrawOperations)
  260. {
  261. result = result.Union(operation.Item.Bounds);
  262. }
  263. _bounds = result;
  264. return result;
  265. }
  266. private void EnsureChildrenCreated()
  267. {
  268. if (_children == null)
  269. {
  270. _children = new List<IVisualNode>();
  271. }
  272. }
  273. private void EnsureDrawOperationsCreated()
  274. {
  275. if (_drawOperations == null)
  276. {
  277. _drawOperations = new List<IRef<IDrawOperation>>();
  278. _drawOperationsRefCounter = RefCountable.Create(Disposable.Create(DisposeDrawOperations));
  279. _drawOperationsCloned = false;
  280. }
  281. else if (_drawOperationsCloned)
  282. {
  283. _drawOperations = new List<IRef<IDrawOperation>>(_drawOperations.Select(op => op.Clone()));
  284. _drawOperationsRefCounter.Dispose();
  285. _drawOperationsRefCounter = RefCountable.Create(Disposable.Create(DisposeDrawOperations));
  286. _drawOperationsCloned = false;
  287. }
  288. }
  289. public bool Disposed { get; }
  290. public void Dispose()
  291. {
  292. _drawOperationsRefCounter?.Dispose();
  293. }
  294. private void DisposeDrawOperations()
  295. {
  296. foreach (var operation in DrawOperations)
  297. {
  298. operation.Dispose();
  299. }
  300. }
  301. }
  302. }