diff --git a/src/Avalonia.Base/Rendering/Composition/Compositor.Factories.cs b/src/Avalonia.Base/Rendering/Composition/Compositor.Factories.cs index 6dba18704f..801dd32d59 100644 --- a/src/Avalonia.Base/Rendering/Composition/Compositor.Factories.cs +++ b/src/Avalonia.Base/Rendering/Composition/Compositor.Factories.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using Avalonia.Platform; using Avalonia.Rendering.Composition.Animations; using Avalonia.Rendering.Composition.Server; @@ -15,7 +14,7 @@ public partial class Compositor /// public CompositionTarget CreateCompositionTarget(Func> surfaces) { - return new CompositionTarget(this, new ServerCompositionTarget(_server, surfaces)); + return new CompositionTarget(this, new ServerCompositionTarget(_server, surfaces, DiagnosticTextRenderer)); } public CompositionContainerVisual CreateContainerVisual() => new(this, new ServerCompositionContainerVisual(_server)); diff --git a/src/Avalonia.Base/Rendering/Composition/Compositor.cs b/src/Avalonia.Base/Rendering/Composition/Compositor.cs index aea4df525d..153b32c5f3 100644 --- a/src/Avalonia.Base/Rendering/Composition/Compositor.cs +++ b/src/Avalonia.Base/Rendering/Composition/Compositor.cs @@ -35,11 +35,14 @@ namespace Avalonia.Rendering.Composition private Task? _pendingBatch; private readonly object _pendingBatchLock = new(); private List _pendingServerCompositorJobs = new(); + private DiagnosticTextRenderer? _diagnosticTextRenderer; internal IEasing DefaultEasing { get; } + private DiagnosticTextRenderer DiagnosticTextRenderer + => _diagnosticTextRenderer ??= new(Typeface.Default.GlyphTypeface, 12.0); + internal event Action? AfterCommit; - /// /// Creates a new compositor on a specified render loop that would use a particular GPU diff --git a/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs b/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs index 6e97f00681..b01fb46aa3 100644 --- a/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs +++ b/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs @@ -14,7 +14,21 @@ namespace Avalonia.Rendering.Composition.Server private readonly GlyphRun[] _runs = new GlyphRun[LastChar - FirstChar + 1]; - public double MaxHeight { get; } + public double GetMaxHeight() + { + var maxHeight = 0.0; + + for (var c = FirstChar; c <= LastChar; c++) + { + var height = _runs[c - FirstChar].Size.Height; + if (height > maxHeight) + { + maxHeight = height; + } + } + + return maxHeight; + } public DiagnosticTextRenderer(IGlyphTypeface typeface, double fontRenderingEmSize) { @@ -24,9 +38,7 @@ namespace Avalonia.Rendering.Composition.Server var index = c - FirstChar; chars[index] = c; var glyph = typeface.GetGlyph(c); - var run = new GlyphRun(typeface, fontRenderingEmSize, chars.AsMemory(index, 1), new[] { glyph }); - _runs[index] = run; - MaxHeight = Math.Max(run.Size.Height, MaxHeight); + _runs[index] = new GlyphRun(typeface, fontRenderingEmSize, chars.AsMemory(index, 1), new[] { glyph }); } } diff --git a/src/Avalonia.Base/Rendering/Composition/Server/FrameTimeGraph.cs b/src/Avalonia.Base/Rendering/Composition/Server/FrameTimeGraph.cs index c926c75c52..d103b068a6 100644 --- a/src/Avalonia.Base/Rendering/Composition/Server/FrameTimeGraph.cs +++ b/src/Avalonia.Base/Rendering/Composition/Server/FrameTimeGraph.cs @@ -44,7 +44,7 @@ internal sealed class FrameTimeGraph _graphPen = new ImmutablePen(Brushes.Blue); _frameValues = new double[maxFrames]; _size = size; - _headerSize = new Size(size.Width, textRenderer.MaxHeight + HeaderPadding * 2.0); + _headerSize = new Size(size.Width, textRenderer.GetMaxHeight() + HeaderPadding * 2.0); _graphSize = new Size(size.Width, size.Height - _headerSize.Height); _defaultMaxY = defaultMaxY; _title = title; diff --git a/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs b/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs index 8d959a9765..e7db80f90c 100644 --- a/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs +++ b/src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs @@ -21,9 +21,9 @@ namespace Avalonia.Rendering.Composition.Server { private readonly ServerCompositor _compositor; private readonly Func> _surfaces; + private readonly DiagnosticTextRenderer _diagnosticTextRenderer; private static long s_nextId = 1; private IRenderTarget? _renderTarget; - private DiagnosticTextRenderer? _diagnosticTextRenderer; private FpsCounter? _fpsCounter; private FrameTimeGraph? _renderTimeGraph; private FrameTimeGraph? _layoutTimeGraph; @@ -42,11 +42,8 @@ namespace Avalonia.Rendering.Composition.Server public ReadbackIndices Readback { get; } = new(); public int RenderedVisuals { get; set; } - private DiagnosticTextRenderer DiagnosticTextRenderer - => _diagnosticTextRenderer ??= new DiagnosticTextRenderer(Typeface.Default.GlyphTypeface, 12.0); - private FpsCounter FpsCounter - => _fpsCounter ??= new FpsCounter(DiagnosticTextRenderer); + => _fpsCounter ??= new FpsCounter(_diagnosticTextRenderer); private FrameTimeGraph LayoutTimeGraph => _layoutTimeGraph ??= CreateTimeGraph("Layout"); @@ -54,16 +51,18 @@ namespace Avalonia.Rendering.Composition.Server private FrameTimeGraph RenderTimeGraph => _renderTimeGraph ??= CreateTimeGraph("Render"); - public ServerCompositionTarget(ServerCompositor compositor, Func> surfaces) : - base(compositor) + public ServerCompositionTarget(ServerCompositor compositor, Func> surfaces, + DiagnosticTextRenderer diagnosticTextRenderer) + : base(compositor) { _compositor = compositor; _surfaces = surfaces; + _diagnosticTextRenderer = diagnosticTextRenderer; Id = Interlocked.Increment(ref s_nextId); } private FrameTimeGraph CreateTimeGraph(string title) - => new(360, new Size(360.0, 64.0), 1000.0 / 60.0, title, DiagnosticTextRenderer); + => new(360, new Size(360.0, 64.0), 1000.0 / 60.0, title, _diagnosticTextRenderer); partial void OnIsEnabledChanged() {