Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DrawLoop test to work without hanging the UI and add options #2575

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 65 additions & 14 deletions test/Eto.Test/Sections/Drawing/DrawLoopSection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace Eto.Test.Sections.Drawing
{
[Section("Drawing", "Draw Loop")]
Expand All @@ -14,6 +16,7 @@ class Status
{
public bool Stop { get; set; }
}
ManualResetEvent mre = new ManualResetEvent(false);

public DrawLoopSection()
{
Expand All @@ -22,25 +25,40 @@ public DrawLoopSection()
Style = "direct",
BackgroundColor = Colors.Black
};
drawable.Paint += (sender, e) => renderer.DrawFrame(e.Graphics, drawable.Size);
drawable.Paint += (sender, e) =>
{
renderer.DrawFrame(e.Graphics, drawable.Size);
Application.Instance.AsyncInvoke(() => mre.Set());
};
renderer = new DirectDrawingRenderer();

var layout = new DynamicLayout { DefaultSpacing = new Size(5, 5), Padding = new Padding(10) };
layout.AddSeparateRow(UseTexturesAndGradients(), UseTextCoordinates(), UseCreateGraphics());
layout.AddSeparateRow(UseTexturesAndGradients(), UseTextCoordinates(), UseCreateGraphics(), NumberOfElements());
layout.Add(content = new Panel { Content = drawable });
this.Content = layout;
}

Control NumberOfElements()
{
var control = new Slider { MinValue = 2, MaxValue = 300 };
control.ValueBinding.Bind(renderer, r => r.NumberOfElements);

var amount = new Label();
amount.TextBinding.Bind(renderer, Binding.Property((DirectDrawingRenderer r) => r.NumberOfElements).Convert(r => $"({r})"));
return new TableLayout(new TableRow(new TableCell(control, true), amount));
}

async void DrawLoop(object data)
void DrawLoop(object data)
{
var currentStatus = (Status)data;
renderer.RestartFPS();
while (!currentStatus.Stop)
{
mre.Reset();
var draw = drawFrame;
if (draw != null)
Application.Instance.Invoke(draw);
await Task.Delay(0);
Application.Instance.AsyncInvoke(draw);
mre.WaitOne(1000);
}
}

Expand Down Expand Up @@ -162,20 +180,51 @@ protected override void Dispose(bool disposing)
}
}

public class DirectDrawingRenderer
public class DirectDrawingRenderer : INotifyPropertyChanged
{
readonly Image texture;
readonly Font font;
readonly SolidBrush textBrush;
readonly SolidBrush eraseBrush;
int _NumberOfElements = 20;
Size? _canvasSize;

public readonly Stopwatch Watch = new Stopwatch();
public int TotalFrames { get; set; }
public long PreviousFrameStartTicks { get; set; }
public readonly List<Box> Boxes = new List<Box>();

public event PropertyChangedEventHandler PropertyChanged;


public bool UseTexturesAndGradients { get; set; }
public bool ShowTextCoordinates { get; set; }
public bool EraseBoxes { get; set; }
public int NumberOfElements
{
get => _NumberOfElements;
set
{
_NumberOfElements = value;
if (_canvasSize != null)
{
if (Boxes.Count > _NumberOfElements)
{
var diff = Boxes.Count - _NumberOfElements;
Boxes.RemoveRange(Boxes.Count - diff - 1, diff);
}
if (Boxes.Count < _NumberOfElements)
{
InitializeBoxes(_canvasSize.Value, _NumberOfElements - Boxes.Count);
}
}
OnPropertyChanged();
RestartFPS();
}
}

private void OnPropertyChanged([CallerMemberName] string name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));


public DirectDrawingRenderer()
{
Expand Down Expand Up @@ -355,9 +404,9 @@ public void Draw(Graphics graphics)
}
}

void InitializeBoxes(Size canvasSize)
void InitializeBoxes(Size canvasSize, int count)
{
for (int i = 0; i < 20; i++)
for (int i = 0; i < count; i++)
Boxes.Add(new Box(canvasSize, UseTexturesAndGradients, ShowTextCoordinates, this));
}

Expand All @@ -367,20 +416,16 @@ public void DrawFrame(Graphics graphics, Size canvasSize)
return;
lock (Boxes)
{
_canvasSize = canvasSize;
if (Boxes.Count == 0 && canvasSize.Width > 1 && canvasSize.Height > 1)
InitializeBoxes(canvasSize);
InitializeBoxes(canvasSize, NumberOfElements);

var fps = TotalFrames / Watch.Elapsed.TotalSeconds;
// The frames per second as determined by the last frame. Measuring a single frame
// must include EndDraw, since that is when the pipeline is flushed to the device.
var frameTicks = Watch.ElapsedTicks - PreviousFrameStartTicks;
var lastFrameFps = Stopwatch.Frequency / Math.Max(frameTicks, 1);
PreviousFrameStartTicks = Watch.ElapsedTicks;
var fpsText = string.Format("Frames per second since start: {0:0.00}, last: {1:0.00}", fps, lastFrameFps);
var start = Watch.ElapsedTicks;
if (EraseBoxes)
graphics.FillRectangle(Colors.Black, new RectangleF(graphics.MeasureString(font, fpsText)));
graphics.DrawText(font, textBrush, 0, 0, fpsText);

var bounds = canvasSize;
graphics.AntiAlias = false;
Expand All @@ -395,6 +440,12 @@ public void DrawFrame(Graphics graphics, Size canvasSize)
box.Draw(graphics);
box.CoordianateDisplay(graphics, font, textBrush);
}

var fpsText = string.Format("Frames per second since start: {0:0.00}, last: {1:0.00}", fps, lastFrameFps);
if (EraseBoxes)
graphics.FillRectangle(Colors.Black, new RectangleF(graphics.MeasureString(font, fpsText)));
graphics.DrawText(font, textBrush, 0, 0, fpsText);

TotalFrames++;
}
}
Expand Down
Loading