Browse Source

Create DiagnosticTextRenderer on the UI thread

pull/10141/head
Julien Lebosquain 3 years ago
parent
commit
cba57d8008
  1. 3
      src/Avalonia.Base/Rendering/Composition/Compositor.Factories.cs
  2. 5
      src/Avalonia.Base/Rendering/Composition/Compositor.cs
  3. 20
      src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs
  4. 2
      src/Avalonia.Base/Rendering/Composition/Server/FrameTimeGraph.cs
  5. 15
      src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs

3
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
/// <returns></returns>
public CompositionTarget CreateCompositionTarget(Func<IEnumerable<object>> 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));

5
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<Action> _pendingServerCompositorJobs = new();
private DiagnosticTextRenderer? _diagnosticTextRenderer;
internal IEasing DefaultEasing { get; }
private DiagnosticTextRenderer DiagnosticTextRenderer
=> _diagnosticTextRenderer ??= new(Typeface.Default.GlyphTypeface, 12.0);
internal event Action? AfterCommit;
/// <summary>
/// Creates a new compositor on a specified render loop that would use a particular GPU

20
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 });
}
}

2
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;

15
src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs

@ -21,9 +21,9 @@ namespace Avalonia.Rendering.Composition.Server
{
private readonly ServerCompositor _compositor;
private readonly Func<IEnumerable<object>> _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<IEnumerable<object>> surfaces) :
base(compositor)
public ServerCompositionTarget(ServerCompositor compositor, Func<IEnumerable<object>> 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()
{

Loading…
Cancel
Save