ImmediateRenderer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 Avalonia.Media;
  7. using Avalonia.Platform;
  8. using Avalonia.VisualTree;
  9. namespace Avalonia.Rendering
  10. {
  11. /// <summary>
  12. /// A renderer which renders the state of the visual tree without an intermediate scene graph
  13. /// representation.
  14. /// </summary>
  15. /// <remarks>
  16. /// The immediate renderer supports only clip-bound-based hit testing; a control's geometry is
  17. /// not taken into account.
  18. /// </remarks>
  19. public class ImmediateRenderer : RendererBase, IRenderer, IVisualBrushRenderer
  20. {
  21. private readonly IVisual _root;
  22. private readonly IRenderRoot _renderRoot;
  23. private IRenderTarget _renderTarget;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="ImmediateRenderer"/> class.
  26. /// </summary>
  27. /// <param name="root">The control to render.</param>
  28. public ImmediateRenderer(IVisual root)
  29. {
  30. Contract.Requires<ArgumentNullException>(root != null);
  31. _root = root;
  32. _renderRoot = root as IRenderRoot;
  33. }
  34. /// <inheritdoc/>
  35. public bool DrawFps { get; set; }
  36. /// <inheritdoc/>
  37. public bool DrawDirtyRects { get; set; }
  38. /// <inheritdoc/>
  39. public event EventHandler<SceneInvalidatedEventArgs> SceneInvalidated;
  40. /// <inheritdoc/>
  41. public void Paint(Rect rect)
  42. {
  43. if (_renderTarget == null)
  44. {
  45. _renderTarget = ((IRenderRoot)_root).CreateRenderTarget();
  46. }
  47. try
  48. {
  49. using (var context = new DrawingContext(_renderTarget.CreateDrawingContext(this)))
  50. {
  51. context.PlatformImpl.Clear(Colors.Transparent);
  52. using (context.PushTransformContainer())
  53. {
  54. Render(context, _root, _root.Bounds);
  55. }
  56. if (DrawDirtyRects)
  57. {
  58. var color = (uint)new Random().Next(0xffffff) | 0x44000000;
  59. context.FillRectangle(
  60. new SolidColorBrush(color),
  61. rect);
  62. }
  63. if (DrawFps)
  64. {
  65. RenderFps(context.PlatformImpl, _root.Bounds, null);
  66. }
  67. }
  68. }
  69. catch (RenderTargetCorruptedException ex)
  70. {
  71. Logging.Logger.Information("Renderer", this, "Render target was corrupted. Exception: {0}", ex);
  72. _renderTarget.Dispose();
  73. _renderTarget = null;
  74. }
  75. SceneInvalidated?.Invoke(this, new SceneInvalidatedEventArgs((IRenderRoot)_root, rect));
  76. }
  77. /// <inheritdoc/>
  78. public void Resized(Size size)
  79. {
  80. }
  81. /// <summary>
  82. /// Renders a visual to a render target.
  83. /// </summary>
  84. /// <param name="visual">The visual.</param>
  85. /// <param name="target">The render target.</param>
  86. public static void Render(IVisual visual, IRenderTarget target)
  87. {
  88. using (var renderer = new ImmediateRenderer(visual))
  89. using (var context = new DrawingContext(target.CreateDrawingContext(renderer)))
  90. {
  91. renderer.Render(context, visual, visual.Bounds);
  92. }
  93. }
  94. /// <summary>
  95. /// Renders a visual to a drawing context.
  96. /// </summary>
  97. /// <param name="visual">The visual.</param>
  98. /// <param name="context">The drawing context.</param>
  99. public static void Render(IVisual visual, DrawingContext context)
  100. {
  101. using (var renderer = new ImmediateRenderer(visual))
  102. {
  103. renderer.Render(context, visual, visual.Bounds);
  104. }
  105. }
  106. /// <inheritdoc/>
  107. public void AddDirty(IVisual visual)
  108. {
  109. if (visual.Bounds != Rect.Empty)
  110. {
  111. var m = visual.TransformToVisual(_root);
  112. if (m.HasValue)
  113. {
  114. var bounds = new Rect(visual.Bounds.Size).TransformToAABB(m.Value);
  115. //use transformedbounds as previous render state of the visual bounds
  116. //so we can invalidate old and new bounds of a control in case it moved/shrinked
  117. if (visual.TransformedBounds.HasValue)
  118. {
  119. var trb = visual.TransformedBounds.Value;
  120. var trBounds = trb.Bounds.TransformToAABB(trb.Transform);
  121. if (trBounds != bounds)
  122. {
  123. _renderRoot?.Invalidate(trBounds);
  124. }
  125. }
  126. _renderRoot?.Invalidate(bounds);
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Ends the operation of the renderer.
  132. /// </summary>
  133. public void Dispose()
  134. {
  135. _renderTarget?.Dispose();
  136. }
  137. /// <inheritdoc/>
  138. public IEnumerable<IVisual> HitTest(Point p, IVisual root, Func<IVisual, bool> filter)
  139. {
  140. return HitTest(root, p, filter);
  141. }
  142. /// <inheritdoc/>
  143. public void RecalculateChildren(IVisual visual) => AddDirty(visual);
  144. /// <inheritdoc/>
  145. public void Start()
  146. {
  147. }
  148. /// <inheritdoc/>
  149. public void Stop()
  150. {
  151. }
  152. /// <inheritdoc/>
  153. Size IVisualBrushRenderer.GetRenderTargetSize(IVisualBrush brush)
  154. {
  155. (brush.Visual as IVisualBrushInitialize)?.EnsureInitialized();
  156. return brush.Visual?.Bounds.Size ?? Size.Empty;
  157. }
  158. /// <inheritdoc/>
  159. void IVisualBrushRenderer.RenderVisualBrush(IDrawingContextImpl context, IVisualBrush brush)
  160. {
  161. var visual = brush.Visual;
  162. Render(new DrawingContext(context), visual, visual.Bounds);
  163. }
  164. private static void ClearTransformedBounds(IVisual visual)
  165. {
  166. foreach (var e in visual.GetSelfAndVisualDescendants())
  167. {
  168. visual.TransformedBounds = null;
  169. }
  170. }
  171. private static Rect GetTransformedBounds(IVisual visual)
  172. {
  173. if (visual.RenderTransform == null)
  174. {
  175. return visual.Bounds;
  176. }
  177. else
  178. {
  179. var origin = visual.RenderTransformOrigin.ToPixels(new Size(visual.Bounds.Width, visual.Bounds.Height));
  180. var offset = Matrix.CreateTranslation(visual.Bounds.Position + origin);
  181. var m = (-offset) * visual.RenderTransform.Value * (offset);
  182. return visual.Bounds.TransformToAABB(m);
  183. }
  184. }
  185. private static IEnumerable<IVisual> HitTest(
  186. IVisual visual,
  187. Point p,
  188. Func<IVisual, bool> filter)
  189. {
  190. Contract.Requires<ArgumentNullException>(visual != null);
  191. if (filter?.Invoke(visual) != false)
  192. {
  193. bool containsPoint = false;
  194. if (visual is ICustomSimpleHitTest custom)
  195. {
  196. containsPoint = custom.HitTest(p);
  197. }
  198. else
  199. {
  200. containsPoint = visual.TransformedBounds?.Contains(p) == true;
  201. }
  202. if ((containsPoint || !visual.ClipToBounds) && visual.VisualChildren.Count > 0)
  203. {
  204. foreach (var child in visual.VisualChildren.SortByZIndex())
  205. {
  206. foreach (var result in HitTest(child, p, filter))
  207. {
  208. yield return result;
  209. }
  210. }
  211. }
  212. if (containsPoint)
  213. {
  214. yield return visual;
  215. }
  216. }
  217. }
  218. private void Render(DrawingContext context, IVisual visual, Rect clipRect)
  219. {
  220. var opacity = visual.Opacity;
  221. var clipToBounds = visual.ClipToBounds;
  222. var bounds = new Rect(visual.Bounds.Size);
  223. if (visual.IsVisible && opacity > 0)
  224. {
  225. var m = Matrix.CreateTranslation(visual.Bounds.Position);
  226. var renderTransform = Matrix.Identity;
  227. if (visual.RenderTransform != null)
  228. {
  229. var origin = visual.RenderTransformOrigin.ToPixels(new Size(visual.Bounds.Width, visual.Bounds.Height));
  230. var offset = Matrix.CreateTranslation(origin);
  231. renderTransform = (-offset) * visual.RenderTransform.Value * (offset);
  232. }
  233. m = renderTransform * m;
  234. if (clipToBounds)
  235. {
  236. if (visual.RenderTransform != null)
  237. {
  238. clipRect = new Rect(visual.Bounds.Size);
  239. }
  240. else
  241. {
  242. clipRect = clipRect.Intersect(new Rect(visual.Bounds.Size));
  243. }
  244. }
  245. using (context.PushPostTransform(m))
  246. using (context.PushOpacity(opacity))
  247. using (clipToBounds ? context.PushClip(bounds) : default(DrawingContext.PushedState))
  248. using (visual.Clip != null ? context.PushGeometryClip(visual.Clip) : default(DrawingContext.PushedState))
  249. using (visual.OpacityMask != null ? context.PushOpacityMask(visual.OpacityMask, bounds) : default(DrawingContext.PushedState))
  250. using (context.PushTransformContainer())
  251. {
  252. visual.Render(context);
  253. #pragma warning disable 0618
  254. var transformed =
  255. new TransformedBounds(bounds, new Rect(), context.CurrentContainerTransform);
  256. #pragma warning restore 0618
  257. visual.TransformedBounds = transformed;
  258. foreach (var child in visual.VisualChildren.OrderBy(x => x, ZIndexComparer.Instance))
  259. {
  260. var childBounds = GetTransformedBounds(child);
  261. if (!child.ClipToBounds || clipRect.Intersects(childBounds))
  262. {
  263. var childClipRect = clipRect.Translate(-childBounds.Position);
  264. Render(context, child, childClipRect);
  265. }
  266. else
  267. {
  268. ClearTransformedBounds(child);
  269. }
  270. }
  271. }
  272. }
  273. if (!visual.IsVisible)
  274. {
  275. ClearTransformedBounds(visual);
  276. }
  277. }
  278. }
  279. }