|
|
|
@ -88,7 +88,7 @@ namespace SixLabors.ImageSharp.Advanced |
|
|
|
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
|
|
|
|
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
|
|
|
|
public static void IterateRows<T, TBuffer>(Rectangle rectangle, Configuration configuration, in T body) |
|
|
|
where T : struct, IRowIntervalAction<TBuffer> |
|
|
|
where T : struct, IRowAction<TBuffer> |
|
|
|
where TBuffer : unmanaged |
|
|
|
{ |
|
|
|
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration); |
|
|
|
@ -100,69 +100,6 @@ namespace SixLabors.ImageSharp.Advanced |
|
|
|
/// instantiating a temporary buffer for each <paramref name="body"/> invocation.
|
|
|
|
/// </summary>
|
|
|
|
internal static void IterateRows<T, TBuffer>( |
|
|
|
Rectangle rectangle, |
|
|
|
in ParallelExecutionSettings parallelSettings, |
|
|
|
in T body) |
|
|
|
where T : struct, IRowIntervalAction<TBuffer> |
|
|
|
where TBuffer : unmanaged |
|
|
|
{ |
|
|
|
ValidateRectangle(rectangle); |
|
|
|
|
|
|
|
int top = rectangle.Top; |
|
|
|
int bottom = rectangle.Bottom; |
|
|
|
int width = rectangle.Width; |
|
|
|
int height = rectangle.Height; |
|
|
|
|
|
|
|
int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask); |
|
|
|
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps); |
|
|
|
MemoryAllocator allocator = parallelSettings.MemoryAllocator; |
|
|
|
|
|
|
|
// Avoid TPL overhead in this trivial case:
|
|
|
|
if (numOfSteps == 1) |
|
|
|
{ |
|
|
|
var rows = new RowInterval(top, bottom); |
|
|
|
using (IMemoryOwner<TBuffer> buffer = allocator.Allocate<TBuffer>(width)) |
|
|
|
{ |
|
|
|
Unsafe.AsRef(body).Invoke(rows, buffer.Memory); |
|
|
|
} |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
int verticalStep = DivideCeil(height, numOfSteps); |
|
|
|
var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = numOfSteps }; |
|
|
|
var rowInfo = new WrappingRowIntervalInfo(top, bottom, verticalStep, width); |
|
|
|
var rowAction = new WrappingRowIntervalBufferAction<T, TBuffer>(in rowInfo, allocator, in body); |
|
|
|
|
|
|
|
Parallel.For( |
|
|
|
0, |
|
|
|
numOfSteps, |
|
|
|
parallelOptions, |
|
|
|
rowAction.Invoke); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s
|
|
|
|
/// instantiating a temporary buffer for each <paramref name="body"/> invocation.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The type of row action to perform.</typeparam>
|
|
|
|
/// <typeparam name="TBuffer">The type of buffer elements.</typeparam>
|
|
|
|
/// <param name="rectangle">The <see cref="Rectangle"/>.</param>
|
|
|
|
/// <param name="configuration">The <see cref="Configuration"/> to get the parallel settings from.</param>
|
|
|
|
/// <param name="body">The method body defining the iteration logic on a single <see cref="RowInterval"/>.</param>
|
|
|
|
public static void IterateRows2<T, TBuffer>(Rectangle rectangle, Configuration configuration, in T body) |
|
|
|
where T : struct, IRowAction<TBuffer> |
|
|
|
where TBuffer : unmanaged |
|
|
|
{ |
|
|
|
var parallelSettings = ParallelExecutionSettings.FromConfiguration(configuration); |
|
|
|
IterateRows2<T, TBuffer>(rectangle, in parallelSettings, in body); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s
|
|
|
|
/// instantiating a temporary buffer for each <paramref name="body"/> invocation.
|
|
|
|
/// </summary>
|
|
|
|
internal static void IterateRows2<T, TBuffer>( |
|
|
|
Rectangle rectangle, |
|
|
|
in ParallelExecutionSettings parallelSettings, |
|
|
|
in T body) |
|
|
|
|