Skip to content

Commit ecc069f

Browse files
Fix benchmark resize handling
1 parent 0f450fc commit ecc069f

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

samples/DrawingBackendBenchmark/CpuBenchmarkBackend.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ internal sealed class CpuBenchmarkBackend : IBenchmarkBackend
1616
{
1717
private readonly Configuration configuration;
1818
private Image<Bgra32>? image;
19+
private int cachedWidth;
20+
private int cachedHeight;
1921

2022
/// <summary>
2123
/// Initializes a new instance of the <see cref="CpuBenchmarkBackend"/> class.
@@ -63,12 +65,15 @@ public void Dispose()
6365
/// <returns>The cached image.</returns>
6466
private Image<Bgra32> EnsureImage(int width, int height)
6567
{
66-
if (this.image is not null)
68+
if (this.image is not null && this.cachedWidth == width && this.cachedHeight == height)
6769
{
6870
return this.image;
6971
}
7072

73+
this.image?.Dispose();
7174
this.image = new Image<Bgra32>(width, height);
75+
this.cachedWidth = width;
76+
this.cachedHeight = height;
7277
return this.image;
7378
}
7479
}

samples/DrawingBackendBenchmark/WebGpuBenchmarkBackend.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ namespace DrawingBackendBenchmark;
1616
internal sealed class WebGpuBenchmarkBackend : IBenchmarkBackend
1717
{
1818
private WebGPURenderTarget<Bgra32>? renderTarget;
19+
private int cachedWidth;
20+
private int cachedHeight;
1921

2022
private WebGpuBenchmarkBackend()
2123
{
@@ -88,12 +90,15 @@ public void Dispose()
8890

8991
private WebGPURenderTarget<Bgra32> EnsureRenderTarget(int width, int height)
9092
{
91-
if (this.renderTarget is not null)
93+
if (this.renderTarget is not null && this.cachedWidth == width && this.cachedHeight == height)
9294
{
9395
return this.renderTarget;
9496
}
9597

98+
this.renderTarget?.Dispose();
9699
this.renderTarget = new WebGPURenderTarget<Bgra32>(width, height);
100+
this.cachedWidth = width;
101+
this.cachedHeight = height;
97102
return this.renderTarget;
98103
}
99104
}

tests/ImageSharp.Drawing.Tests/Processing/Backends/WebGPUDrawingBackendTests.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,7 @@ private static void DebugSaveBackendPair<TPixel>(
870870
TestImageProvider<TPixel> provider,
871871
string testName,
872872
Image<TPixel> defaultImage,
873-
Image<TPixel> nativeSurfaceImage,
874-
float tolerantPercentage = 0.0003F)
873+
Image<TPixel> nativeSurfaceImage)
875874
where TPixel : unmanaged, IPixel<TPixel>
876875
{
877876
defaultImage.DebugSave(
@@ -1508,7 +1507,7 @@ public void FillPath_WithLinearGradientBrush_MatchesDefaultOutput<TPixel>(TestIm
15081507
nativeSurfaceInitialImage);
15091508

15101509
// MacOS on CI has some outliers with this test, so using a slightly higher tolerance here to avoid noise.
1511-
DebugSaveBackendPair(provider, "FillPath_LinearGradient", defaultImage, nativeSurfaceImage, tolerantPercentage: 0.0007F);
1510+
DebugSaveBackendPair(provider, "FillPath_LinearGradient", defaultImage, nativeSurfaceImage);
15121511
AssertBackendPairSimilarity(defaultImage, nativeSurfaceImage, 0.03F);
15131512
AssertBackendPairReferenceOutputs(
15141513
provider,
@@ -1759,8 +1758,7 @@ public void FillPath_WithSweepGradientBrush_PartialArc_MatchesDefaultOutput<TPix
17591758
provider,
17601759
"FillPath_SweepGradient_PartialArc",
17611760
defaultImage,
1762-
nativeSurfaceImage,
1763-
tolerantPercentage: 0.0280F);
1761+
nativeSurfaceImage);
17641762
AssertBackendPairSimilarity(defaultImage, nativeSurfaceImage, 0.0280F);
17651763
AssertBackendPairReferenceOutputs(
17661764
provider,

0 commit comments

Comments
 (0)