Skip to content

Commit 44d0e74

Browse files
Honor MaxDegreeOfParallelism in flush
1 parent 073e2b7 commit 44d0e74

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/ImageSharp.Drawing/Processing/Backends/DefaultDrawingBackend.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public void FlushCompositions<TPixel>(
3636
using FlushScene scene = FlushScene.Create(
3737
compositionScene,
3838
target.Bounds,
39-
configuration.MemoryAllocator);
39+
configuration.MemoryAllocator,
40+
configuration.MaxDegreeOfParallelism);
4041

4142
if (scene.RowCount == 0)
4243
{
@@ -85,9 +86,15 @@ private static void ExecuteScene<TPixel>(
8586
}
8687
}
8788

89+
int requestedParallelism = configuration.MaxDegreeOfParallelism;
90+
int partitionCount = Math.Min(
91+
scene.RowCount,
92+
requestedParallelism == -1 ? Environment.ProcessorCount : requestedParallelism);
93+
8894
_ = Parallel.For(
8995
fromInclusive: 0,
9096
toExclusive: scene.RowCount,
97+
parallelOptions: new ParallelOptions { MaxDegreeOfParallelism = partitionCount },
9198
localInit: () => new WorkerState<TPixel>(configuration.MemoryAllocator, destinationFrame.Width, scene.MaxLayerDepth + 1),
9299
body: (rowIndex, _, state) =>
93100
{

src/ImageSharp.Drawing/Processing/Backends/FlushScene.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,15 @@ private FlushScene(
119119
/// <param name="scene">The prepared composition scene.</param>
120120
/// <param name="targetBounds">The destination bounds of the flush.</param>
121121
/// <param name="allocator">The allocator used for retained row storage.</param>
122+
/// <param name="maxDegreeOfParallelism">
123+
/// The maximum degree of parallelism to use when building the scene, or -1 to use the default number of processors.
124+
/// </param>
122125
/// <returns>A flush-ready scene.</returns>
123126
public static FlushScene Create(
124127
CompositionScene scene,
125128
in Rectangle targetBounds,
126-
MemoryAllocator allocator)
129+
MemoryAllocator allocator,
130+
int maxDegreeOfParallelism)
127131
{
128132
int commandCount = scene.CommandCount;
129133

@@ -145,12 +149,14 @@ public static FlushScene Create(
145149

146150
FillSceneItem?[] fillItems = new FillSceneItem?[commandCount];
147151
StrokeSceneItem?[] strokeItems = new StrokeSceneItem?[commandCount];
148-
int partitionCount = Math.Min(commandCount, Math.Min(Environment.ProcessorCount, targetRowCount));
152+
int availableParallelism = maxDegreeOfParallelism == -1 ? Environment.ProcessorCount : maxDegreeOfParallelism;
153+
int partitionCount = Math.Min(commandCount, Math.Min(availableParallelism, targetRowCount));
149154
PartitionState[] partitions = new PartitionState[partitionCount];
150155

151156
_ = Parallel.For(
152157
0,
153158
partitionCount,
159+
new ParallelOptions { MaxDegreeOfParallelism = partitionCount },
154160
partitionIndex =>
155161
{
156162
// Integer division splits the commands into contiguous half-open ranges,

0 commit comments

Comments
 (0)