Browse Source

publish ParallelHelper and RowInterval API-s

af/merge-core
Anton Firszov 7 years ago
parent
commit
15cc234b96
  1. 2
      src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor{TPixel}.cs
  2. 27
      src/ImageSharp/Common/ParallelUtils/ParallelHelper.cs
  3. 6
      src/ImageSharp/Memory/Buffer2D{T}.cs
  4. 43
      src/ImageSharp/Memory/RowInterval.cs

2
src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor{TPixel}.cs

@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
int width = maxX - minX;
var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
Rectangle workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY);
IBrush brush = this.definition.Brush;
GraphicsOptions options = this.definition.Options;

27
src/ImageSharp/Common/ParallelUtils/ParallelHelper.cs

@ -17,19 +17,14 @@ namespace SixLabors.ImageSharp.ParallelUtils
/// Parallel execution is optimized for image processing.
/// Use this instead of direct <see cref="Parallel"/> calls!
/// </summary>
internal static class ParallelHelper
public static class ParallelHelper
{
/// <summary>
/// Get the default <see cref="ParallelExecutionSettings"/> for a <see cref="Configuration"/>
/// </summary>
public static ParallelExecutionSettings GetParallelSettings(this Configuration configuration)
{
return new ParallelExecutionSettings(configuration.MaxDegreeOfParallelism, configuration.MemoryAllocator);
}
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
/// </summary>
/// <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 IterateRows(Rectangle rectangle, Configuration configuration, Action<RowInterval> body)
{
ParallelExecutionSettings parallelSettings = configuration.GetParallelSettings();
@ -40,7 +35,7 @@ namespace SixLabors.ImageSharp.ParallelUtils
/// <summary>
/// Iterate through the rows of a rectangle in optimized batches defined by <see cref="RowInterval"/>-s.
/// </summary>
public static void IterateRows(
internal static void IterateRows(
Rectangle rectangle,
in ParallelExecutionSettings parallelSettings,
Action<RowInterval> body)
@ -77,11 +72,19 @@ namespace SixLabors.ImageSharp.ParallelUtils
});
}
/// <summary>
/// Get the default <see cref="ParallelExecutionSettings"/> for a <see cref="Configuration"/>
/// </summary>
internal static ParallelExecutionSettings GetParallelSettings(this Configuration configuration)
{
return new ParallelExecutionSettings(configuration.MaxDegreeOfParallelism, configuration.MemoryAllocator);
}
/// <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>
public static void IterateRowsWithTempBuffer<T>(
internal static void IterateRowsWithTempBuffer<T>(
Rectangle rectangle,
in ParallelExecutionSettings parallelSettings,
Action<RowInterval, Memory<T>> body)
@ -133,7 +136,7 @@ namespace SixLabors.ImageSharp.ParallelUtils
/// 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>
public static void IterateRowsWithTempBuffer<T>(
internal static void IterateRowsWithTempBuffer<T>(
Rectangle rectangle,
Configuration configuration,
Action<RowInterval, Memory<T>> body)

6
src/ImageSharp/Memory/Buffer2D{T}.cs

@ -12,7 +12,11 @@ namespace SixLabors.ImageSharp.Memory
/// Represents a buffer of value type objects
/// interpreted as a 2D region of <see cref="Width"/> x <see cref="Height"/> elements.
/// </summary>
/// <remarks>
/// Before RC1, this class might be target of API changes, use it on your own risk!
/// </remarks>
/// <typeparam name="T">The value type.</typeparam>
// TODO: Consider moving this type to the SixLabors.Memory namespace (SixLabors.Core).
public sealed class Buffer2D<T> : IDisposable
where T : struct
{
@ -38,7 +42,7 @@ namespace SixLabors.ImageSharp.Memory
/// <summary>
/// Gets the height.
/// </summary>
/// </summary>Bu
public int Height { get; private set; }
/// <summary>

43
src/ImageSharp/Memory/RowInterval.cs

@ -10,26 +10,32 @@ namespace SixLabors.ImageSharp.Memory
/// <summary>
/// Represents an interval of rows in a <see cref="Rectangle"/> and/or <see cref="Buffer2D{T}"/>
/// </summary>
internal readonly struct RowInterval : IEquatable<RowInterval>
/// <remarks>
/// Before RC1, this class might be target of API changes, use it on your own risk!
/// </remarks>
// TODO: Consider moving this type to the SixLabors.Memory namespace (SixLabors.Core).
public readonly struct RowInterval : IEquatable<RowInterval>
{
/// <summary>
/// Initializes a new instance of the <see cref="RowInterval"/> struct.
/// </summary>
/// <param name="min">The inclusive minimum row.</param>
/// <param name="max">The exclusive maximum row.</param>
public RowInterval(int min, int max)
{
DebugGuard.MustBeLessThan(min, max, nameof(min));
Guard.MustBeLessThan(min, max, nameof(min));
this.Min = min;
this.Max = max;
}
/// <summary>
/// Gets the INCLUSIVE minimum.
/// Gets the inclusive minimum row.
/// </summary>
public int Min { get; }
/// <summary>
/// Gets the EXCLUSIVE maximum.
/// Gets the exclusive maximum row.
/// </summary>
public int Max { get; }
@ -38,33 +44,48 @@ namespace SixLabors.ImageSharp.Memory
/// </summary>
public int Height => this.Max - this.Min;
/// <summary>
/// Returns a boolean indicating whether the given two <see cref="RowInterval"/>-s are equal.
/// </summary>
/// <param name="left">The first <see cref="RowInterval"/> to compare.</param>
/// <param name="right">The second <see cref="RowInterval"/> to compare.</param>
/// <returns>True if the given <see cref="RowInterval"/>-s are equal; False otherwise.</returns>
public static bool operator ==(RowInterval left, RowInterval right)
{
return left.Equals(right);
}
/// <summary>
/// Returns a boolean indicating whether the given two <see cref="RowInterval"/>-s are not equal.
/// </summary>
/// <param name="left">The first <see cref="RowInterval"/> to compare.</param>
/// <param name="right">The second <see cref="RowInterval"/> to compare.</param>
/// <returns>True if the given <see cref="RowInterval"/>-s are not equal; False otherwise.</returns>
public static bool operator !=(RowInterval left, RowInterval right)
{
return !left.Equals(right);
}
/// <inheritdoc />
public override string ToString() => $"RowInterval [{this.Min}->{this.Max}]";
public RowInterval Slice(int start) => new RowInterval(this.Min + start, this.Max);
public RowInterval Slice(int start, int length) => new RowInterval(this.Min + start, this.Min + start + length);
public bool Equals(RowInterval other)
{
return this.Min == other.Min && this.Max == other.Max;
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return !ReferenceEquals(null, obj) && obj is RowInterval other && this.Equals(other);
}
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(this.Min, this.Max);
/// <inheritdoc />
public override string ToString() => $"RowInterval [{this.Min}->{this.Max}]";
internal RowInterval Slice(int start) => new RowInterval(this.Min + start, this.Max);
internal RowInterval Slice(int start, int length) => new RowInterval(this.Min + start, this.Min + start + length);
}
}
}

Loading…
Cancel
Save