Skip to content

Commit eadd5ee

Browse files
committed
Added BenchmarkScope to compare performance of the renderers.
1 parent 49bab6d commit eadd5ee

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

SvgFileType/BenchmarkScope.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2025 Osman Tunçelli. Allrights reserved.
2+
// Use of this source code is governed by GNU General Public License (GPL-2.0) that can be found in the COPYING file.
3+
4+
using System;
5+
using System.Diagnostics;
6+
using System.Text;
7+
using System.Threading;
8+
9+
namespace SvgFileTypePlugin;
10+
11+
internal struct BenchmarkScope : IDisposable
12+
{
13+
private readonly Stopwatch timer;
14+
private readonly string? name;
15+
private int disposed;
16+
17+
public BenchmarkScope(string? name) : this()
18+
{
19+
this.name = name;
20+
}
21+
22+
public BenchmarkScope()
23+
{
24+
timer = Stopwatch.StartNew();
25+
}
26+
27+
public void Dispose()
28+
{
29+
if (Interlocked.CompareExchange(ref disposed, 1, 0) == 0)
30+
{
31+
StringBuilder sb = new StringBuilder(nameof(BenchmarkScope));
32+
sb.Append(' ');
33+
if (name is not null)
34+
sb.Append($"({name}) ");
35+
sb.Append($"took {timer.ElapsedMilliseconds} ms.");
36+
Logger.WriteLine(sb.ToString());
37+
}
38+
}
39+
}

SvgFileType/Import/Direct2DSvgRenderer.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected override Document GetFlatDocument(string svgdata, SvgImportConfig conf
2929

3030
ctoken.ThrowIfCancellationRequested();
3131
(int width, int height) = (config.RasterWidth, config.RasterHeight);
32+
using BenchmarkScope _ = new BenchmarkScope();
3233
using MemoryFailPoint mfp = GetMemoryFailPoint(width, height, 1);
3334
ResetProgress(1);
3435
Surface surface = new Surface(width, height, SurfaceCreationFlags.DoNotZeroFillHint);
@@ -64,13 +65,14 @@ protected override Document GetLayeredDocument(string svgdata, SvgImportConfig c
6465
ArgumentException.ThrowIfNullOrWhiteSpace(svgdata);
6566
ArgumentNullException.ThrowIfNull(config);
6667

68+
using BenchmarkScope _ = new BenchmarkScope();
6769
SvgDocument svg = SvgDocument.FromSvg<SvgDocument>(svgdata);
68-
using IDisposable _ = svg.UseSetRasterDimensions(config);
70+
using IDisposable _1 = svg.UseSetRasterDimensions(config);
6971

7072
List<SvgVisualElement> elements = GetSvgVisualElements(svg, config, ctoken);
7173
ctoken.ThrowIfCancellationRequested();
7274

73-
using MemoryFailPoint _1 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count); // We will probably need a bit more that this but that's okay.
75+
using MemoryFailPoint _2 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count);
7476

7577
ResetProgress(elements.Count);
7678
List<BitmapLayer> layers = [];
@@ -146,36 +148,35 @@ protected override Document GetLayeredDocument(string svgdata, SvgImportConfig c
146148

147149
public override Document GetNoPathDocument()
148150
{
149-
string text = SR.NoPath;
150151
IDirectWriteFactory dw = Services.Get<IDirectWriteFactory>();
151152
IDirect2DFactory d2d = Services.Get<IDirect2DFactory>();
152153
using ITextFormat textFormat = dw.CreateTextFormat("Arial", null, FontWeight.Bold, FontStyle.Normal, FontStretch.Normal, UIScaleFactor.ConvertFontPointsToDips(24));
153-
using ITextLayout textLayout = dw.CreateTextLayout(text, textFormat);
154+
using ITextLayout textLayout = dw.CreateTextLayout(SR.NoPath, textFormat);
154155
textLayout.WordWrapping = WordWrapping.NoWrap;
155-
TextMeasurement tm = textLayout.Measure();
156-
int width = (int)tm.Width;
157-
int height = (int)tm.Height;
158-
// StrokeStyleProperties ssprops = StrokeStyleProperties.Default;
159-
// ssprops.LineJoin = LineJoin.Bevel;
160-
// ssprops.MiterLimit = 0;
156+
TextMeasurement textMeasurement = textLayout.Measure();
157+
int width = (int)textMeasurement.Width;
158+
int height = (int)textMeasurement.Height;
159+
//StrokeStyleProperties strokeStyleProperties = StrokeStyleProperties.Default;
160+
//strokeStyleProperties.LineJoin = LineJoin.Bevel;
161+
//strokeStyleProperties.MiterLimit = 0;
161162
Surface surface = new Surface(width, height, SurfaceCreationFlags.DoNotZeroFillHint);
162163
using (IBitmap<ColorBgra32> shared = surface.CreateSharedBitmap())
163164
using (IBitmap<ColorPbgra32> premltd = shared.CreatePremultipliedAdapter(PremultipliedAdapterOptions.UnPremultiplyOnDispose))
164165
using (IDeviceContext dc = d2d.CreateBitmapDeviceContext(premltd))
165166
using (ISolidColorBrush textBrush = dc.CreateSolidColorBrush(Color.Black))
166167
//using (ISolidColorBrush strokeBrush = dc.CreateSolidColorBrush(Color.Red))
167-
//using (IGeometry textGeometry = d2d.CreateGeometryFromTextLayout(layout))
168-
//using (IStrokeStyle strokeStyle = d2d.CreateStrokeStyle(ssprops))
169-
//using (IGeometry widenedGeometry = textGeometry.Widen(10, strokeStyle))
168+
//using (IGeometry textGeometry = d2d.CreateGeometryFromTextLayout(textLayout))
169+
//using (IStrokeStyle strokeStyle = d2d.CreateStrokeStyle(strokeStyleProperties))
170+
//using (IGeometry widenedGeometry = textGeometry.Widen(1, strokeStyle))
170171
//using (IGeometry outerStrokeGeometry = widenedGeometry.CombineWithGeometry(textGeometry, GeometryCombineMode.Exclude))
171172
using (dc.UseTextRenderingMode(TextRenderingMode.Default))
172173
using (dc.UseTextAntialiasMode(TextAntialiasMode.Grayscale))
173174
using (dc.UseBeginDraw())
174175
{
175176
dc.Clear(Color.LightGray);
176177
dc.DrawTextLayout(0, 0, textLayout, textBrush, DrawTextOptions.EnableColorFont);
177-
// dc.FillGeometry(textGeometry, textBrush);
178-
// dc.FillGeometry(outerStrokeGeometry, strokeBrush);
178+
//dc.FillGeometry(textGeometry, textBrush);
179+
//dc.FillGeometry(outerStrokeGeometry, strokeBrush);
179180
}
180181
return surface.CreateSingleLayerDocument(takeOwnership: true);
181182
}

SvgFileType/Import/GdipSvgRenderer.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ protected override Document GetFlatDocument(string svgdata, SvgImportConfig conf
2020
ArgumentNullException.ThrowIfNull(config);
2121

2222
ctoken.ThrowIfCancellationRequested();
23+
24+
using BenchmarkScope _ = new BenchmarkScope();
2325
SvgDocument svg = SvgDocument.FromSvg<SvgDocument>(svgdata);
24-
using IDisposable _ = svg.UseSetRasterDimensions(config);
26+
using IDisposable _1 = svg.UseSetRasterDimensions(config);
2527

2628
List<SvgVisualElement> elements = GetSvgVisualElements(svg, config, ctoken);
27-
using MemoryFailPoint _1 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, 2);
29+
using MemoryFailPoint _2 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, 2);
2830

2931
ResetProgress(elements.Count);
3032
using Bitmap bmp = new Bitmap(config.RasterWidth, config.RasterHeight);
@@ -53,13 +55,14 @@ protected override Document GetLayeredDocument(string svgdata, SvgImportConfig c
5355
ArgumentException.ThrowIfNullOrWhiteSpace(svgdata);
5456
ArgumentNullException.ThrowIfNull(config);
5557

58+
using BenchmarkScope _ = new BenchmarkScope();
5659
SvgDocument svg = SvgDocument.FromSvg<SvgDocument>(svgdata);
57-
using IDisposable _ = svg.UseSetRasterDimensions(config);
60+
using IDisposable _1 = svg.UseSetRasterDimensions(config);
5861

5962
List<SvgVisualElement> elements = GetSvgVisualElements(svg, config, ctoken);
6063
ctoken.ThrowIfCancellationRequested();
6164

62-
using MemoryFailPoint _1 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count);
65+
using MemoryFailPoint _2 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count);
6366

6467
ResetProgress(elements.Count);
6568
List<BitmapLayer> layers = [];

SvgFileType/Import/ResvgSvgRenderer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ protected override Document GetFlatDocument(string svgdata, SvgImportConfig conf
2222

2323
ctoken.ThrowIfCancellationRequested();
2424
(int width, int height) = (config.RasterWidth, config.RasterHeight);
25+
using BenchmarkScope _ = new BenchmarkScope();
2526
using MemoryFailPoint mfp = GetMemoryFailPoint(width, height, 1);
2627
ResetProgress(1);
2728
Surface surface = new Surface(width, height, SurfaceCreationFlags.DoNotZeroFillHint);
@@ -51,13 +52,14 @@ protected override Document GetLayeredDocument(string svgdata, SvgImportConfig c
5152
ArgumentException.ThrowIfNullOrWhiteSpace(svgdata);
5253
ArgumentNullException.ThrowIfNull(config);
5354

55+
using BenchmarkScope _ = new BenchmarkScope();
5456
SvgDocument svg = SvgDocument.FromSvg<SvgDocument>(svgdata);
55-
using IDisposable _ = svg.UseSetRasterDimensions(config);
57+
using IDisposable _1 = svg.UseSetRasterDimensions(config);
5658

5759
List<SvgVisualElement> elements = GetSvgVisualElements(svg, config, ctoken);
5860
ctoken.ThrowIfCancellationRequested();
5961

60-
using MemoryFailPoint _1 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count);
62+
using MemoryFailPoint _2 = GetMemoryFailPoint(config.RasterWidth, config.RasterHeight, elements.Count);
6163

6264
ResetProgress(elements.Count);
6365
List<BitmapLayer> layers = [];

0 commit comments

Comments
 (0)