-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathFillTiger.cs
More file actions
150 lines (125 loc) · 4.51 KB
/
FillTiger.cs
File metadata and controls
150 lines (125 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Drawing;
using System.Drawing.Drawing2D;
using BenchmarkDotNet.Attributes;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Drawing.Tests;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SkiaSharp;
using SDColor = System.Drawing.Color;
using SDPen = System.Drawing.Pen;
using SDSolidBrush = System.Drawing.SolidBrush;
namespace SixLabors.ImageSharp.Drawing.Benchmarks.Drawing;
/// <summary>
/// Benchmarks rendering the Ghostscript Tiger SVG (~240 path elements with fills and strokes)
/// across SkiaSharp, System.Drawing, ImageSharp (CPU), and ImageSharp (WebGPU).
/// </summary>
public class FillTiger
{
private static readonly string SvgFilePath =
TestFile.GetInputFileFullPath(TestImages.Svg.GhostscriptTiger);
private SKSurface skSurface;
private List<(SKPath Path, SKPaint FillPaint, SKPaint StrokePaint)> skElements;
private Bitmap sdBitmap;
private Graphics sdGraphics;
private List<(GraphicsPath Path, SDSolidBrush Fill, SDPen Stroke)> sdElements;
private Image<Rgba32> image;
private List<(IPath Path, Processing.SolidBrush Fill, SolidPen Stroke)> isElements;
[Params(1000, 100)]
public int Dimensions { get; set; }
[GlobalSetup]
public void Setup()
{
int width = this.Dimensions;
int height = this.Dimensions;
float scale = this.Dimensions / 200f;
ThreadPool.GetMinThreads(out int minWorkerThreads, out int minCompletionPortThreads);
int desiredWorkerThreads = Math.Max(minWorkerThreads, Environment.ProcessorCount);
ThreadPool.SetMinThreads(desiredWorkerThreads, minCompletionPortThreads);
Parallel.For(0, desiredWorkerThreads, static _ => { });
List<SvgBenchmarkHelper.SvgElement> elements = SvgBenchmarkHelper.ParseSvg(SvgFilePath);
this.skSurface = SKSurface.Create(new SKImageInfo(width, height));
this.skElements = SvgBenchmarkHelper.BuildSkiaElements(elements, scale);
this.sdBitmap = new Bitmap(width, height);
this.sdGraphics = Graphics.FromImage(this.sdBitmap);
this.sdGraphics.SmoothingMode = SmoothingMode.AntiAlias;
this.sdElements = SvgBenchmarkHelper.BuildSystemDrawingElements(elements, scale);
this.image = new Image<Rgba32>(width, height);
this.isElements = SvgBenchmarkHelper.BuildImageSharpElements(elements, scale);
}
[IterationSetup]
public void IterationSetup()
{
this.sdGraphics.Clear(SDColor.Transparent);
this.skSurface.Canvas.Clear(SKColors.Transparent);
}
[GlobalCleanup]
public void Cleanup()
{
foreach ((SKPath path, SKPaint fill, SKPaint stroke) in this.skElements)
{
path.Dispose();
fill?.Dispose();
stroke?.Dispose();
}
this.skSurface.Dispose();
foreach ((GraphicsPath path, SDSolidBrush fill, SDPen stroke) in this.sdElements)
{
path.Dispose();
fill?.Dispose();
stroke?.Dispose();
}
this.sdGraphics.Dispose();
this.sdBitmap.Dispose();
this.image.Dispose();
}
[Benchmark(Baseline = true)]
public void SkiaSharp()
{
SKCanvas canvas = this.skSurface.Canvas;
foreach ((SKPath path, SKPaint fillPaint, SKPaint strokePaint) in this.skElements)
{
if (fillPaint is not null)
{
canvas.DrawPath(path, fillPaint);
}
if (strokePaint is not null)
{
canvas.DrawPath(path, strokePaint);
}
}
}
[Benchmark]
public void SystemDrawing()
{
foreach ((GraphicsPath path, SDSolidBrush fill, SDPen stroke) in this.sdElements)
{
if (fill is not null)
{
this.sdGraphics.FillPath(fill, path);
}
if (stroke is not null)
{
this.sdGraphics.DrawPath(stroke, path);
}
}
}
[Benchmark]
public void ImageSharp()
=> this.image.Mutate(c =>
{
foreach ((IPath path, Processing.SolidBrush fill, SolidPen stroke) in this.isElements)
{
if (fill is not null)
{
c.Fill(fill, path);
}
if (stroke is not null)
{
c.Draw(stroke, path);
}
}
});
}