Skip to content

Commit 8ebefc2

Browse files
committed
make the lines benchmark resizable
1 parent f61551d commit 8ebefc2

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

samples/DrawingBackendBenchmark/BenchmarkForm.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ namespace DrawingBackendBenchmark;
1414
/// </summary>
1515
internal sealed class BenchmarkForm : Form
1616
{
17-
private const int BenchmarkWidth = 600;
18-
private const int BenchmarkHeight = 400;
19-
2017
private readonly ComboBox backendSelector;
2118
private readonly NumericUpDown iterationSelector;
2219
private readonly TextBox statusTextBox;
@@ -31,7 +28,7 @@ internal sealed class BenchmarkForm : Form
3128
public BenchmarkForm()
3229
{
3330
this.Text = "Drawing Backend Benchmark";
34-
this.ClientSize = new System.Drawing.Size(780, 560);
31+
this.ClientSize = new System.Drawing.Size(1600, 1200);
3532
this.StartPosition = FormStartPosition.CenterScreen;
3633

3734
FlowLayoutPanel toolbar = new()
@@ -63,6 +60,7 @@ public BenchmarkForm()
6360
toolbar.Controls.Add(this.CreateRunButton("1k", 1_000));
6461
toolbar.Controls.Add(this.CreateRunButton("10k", 10_000));
6562
toolbar.Controls.Add(this.CreateRunButton("100k", 100_000));
63+
toolbar.Controls.Add(this.CreateRunButton("200k", 200_000));
6664

6765
this.statusTextBox = new TextBox
6866
{
@@ -80,23 +78,23 @@ public BenchmarkForm()
8078
{
8179
Dock = DockStyle.Fill,
8280
AutoScroll = true,
83-
BackColor = System.Drawing.Color.FromArgb(24, 36, 56),
81+
BackColor = System.Drawing.Color.FromArgb(50, 36, 56),
8482
Padding = new Padding(12),
8583
};
8684

8785
this.previewBox = new PictureBox
8886
{
87+
Dock = DockStyle.Fill,
8988
BackColor = System.Drawing.Color.FromArgb(24, 36, 56),
90-
SizeMode = PictureBoxSizeMode.Normal,
91-
Size = new System.Drawing.Size(BenchmarkWidth, BenchmarkHeight),
89+
SizeMode = PictureBoxSizeMode.Normal
9290
};
9391
this.previewHost.Controls.Add(this.previewBox);
9492

9593
this.Controls.Add(this.previewHost);
9694
this.Controls.Add(this.statusTextBox);
9795
this.Controls.Add(toolbar);
98-
this.Resize += (_, _) => this.LayoutPreview();
9996

97+
// Fake 1x1 SKGLControl to create a GRContext
10098
this.glControl = new SKGLControl
10199
{
102100
Size = new System.Drawing.Size(1, 1),
@@ -139,7 +137,6 @@ public BenchmarkForm()
139137
this.backendSelector.SelectedIndex = 0;
140138
}
141139

142-
this.LayoutPreview();
143140
this.Shown += (_, _) => this.backendSelector.Focus();
144141
}
145142

@@ -158,6 +155,10 @@ protected override void Dispose(bool disposing)
158155
base.Dispose(disposing);
159156
}
160157

158+
private int BenchmarkWidth => this.previewBox.Width;
159+
160+
private int BenchmarkHeight => this.previewBox.Height;
161+
161162
/// <summary>
162163
/// Creates one toolbar button that runs the benchmark with the requested line count.
163164
/// </summary>
@@ -195,13 +196,14 @@ private void RunBenchmark(int lineCount)
195196

196197
Cursor previousCursor = this.Cursor;
197198
this.Cursor = Cursors.WaitCursor;
199+
VisualLine[] lines = GenerateLines(lineCount, this.BenchmarkWidth, this.BenchmarkHeight, rng);
200+
198201
try
199202
{
200203
for (int i = 0; i < iterations; i++)
201204
{
202-
VisualLine[] lines = GenerateLines(lineCount, BenchmarkWidth, BenchmarkHeight, rng);
203205
bool capturePreview = i == iterations - 1;
204-
using BenchmarkRenderResult result = backend.Render(lines, BenchmarkWidth, BenchmarkHeight, capturePreview);
206+
using BenchmarkRenderResult result = backend.Render(lines, this.BenchmarkWidth, this.BenchmarkHeight, capturePreview);
205207

206208
samples.Add(result.RenderMilliseconds);
207209
this.UpdatePreview(result, capturePreview);
@@ -221,16 +223,6 @@ private void RunBenchmark(int lineCount)
221223
}
222224
}
223225

224-
/// <summary>
225-
/// Lays out the fixed-size preview surface in the middle of the scroll host.
226-
/// </summary>
227-
private void LayoutPreview()
228-
{
229-
int x = Math.Max(this.previewHost.Padding.Left, (this.previewHost.ClientSize.Width - this.previewBox.Width) / 2);
230-
int y = Math.Max(this.previewHost.Padding.Top, (this.previewHost.ClientSize.Height - this.previewBox.Height) / 2);
231-
this.previewBox.Location = new System.Drawing.Point(x, y);
232-
}
233-
234226
/// <summary>
235227
/// Replaces the preview image with the final captured frame from the current run.
236228
/// </summary>

samples/DrawingBackendBenchmark/SkiaSharpBenchmarkBackend.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Diagnostics;
55
using SixLabors.ImageSharp;
6+
using SixLabors.ImageSharp.Advanced;
67
using SixLabors.ImageSharp.PixelFormats;
78
using SkiaSharp;
89

@@ -60,15 +61,26 @@ public BenchmarkRenderResult Render(ReadOnlySpan<VisualLine> lines, int width, i
6061
if (capturePreview)
6162
{
6263
preview = new Image<Bgra32>(width, height);
63-
if (preview.DangerousTryGetSinglePixelMemory(out Memory<Bgra32> memory))
64+
int rowBytes = width * 4;
65+
byte[] readbackBuffer = new byte[height * rowBytes];
66+
SKImageInfo readbackInfo = new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
67+
unsafe
6468
{
65-
SKImageInfo readbackInfo = new(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
66-
using System.Buffers.MemoryHandle pin = memory.Pin();
67-
unsafe
69+
fixed (byte* ptr = readbackBuffer)
6870
{
69-
this.surface.ReadPixels(readbackInfo, (nint)pin.Pointer, width * 4, 0, 0);
71+
this.surface.ReadPixels(readbackInfo, (nint)ptr, rowBytes, 0, 0);
7072
}
7173
}
74+
75+
preview.ProcessPixelRows(accessor =>
76+
{
77+
for (int y = 0; y < height; y++)
78+
{
79+
System.Runtime.InteropServices.MemoryMarshal
80+
.Cast<byte, Bgra32>(readbackBuffer.AsSpan(y * rowBytes, rowBytes))
81+
.CopyTo(accessor.GetRowSpan(y));
82+
}
83+
});
7284
}
7385

7486
return new BenchmarkRenderResult(stopwatch.Elapsed.TotalMilliseconds, preview, usedGpu: this.context != null);

0 commit comments

Comments
 (0)