// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Drawing.Processing {
///
/// Worker-local scratch workspace used by prepared brushes during row composition.
///
/// The target pixel format.
public sealed class BrushWorkspace : IDisposable
where TPixel : unmanaged, IPixel
{
private readonly IMemoryOwner amountsOwner;
private readonly IMemoryOwner overlaysOwner;
private readonly IMemoryOwner blendScratchOwner;
internal BrushWorkspace(MemoryAllocator allocator, int rowWidth)
{
int capacity = Math.Max(1, rowWidth);
this.amountsOwner = allocator.Allocate(capacity);
this.overlaysOwner = allocator.Allocate(capacity);
this.blendScratchOwner = allocator.Allocate(capacity * 3);
}
///
/// Gets the shared amount buffer for the requested length.
///
/// The number of elements required.
/// A slice of the worker-local pooled amount buffer.
public Span GetAmounts(int length)
{
ArgumentOutOfRangeException.ThrowIfNegative(length);
return this.amountsOwner.Memory.Span[..length];
}
///
/// Gets the shared overlay buffer for the requested length.
///
/// The number of elements required.
/// A slice of the worker-local pooled overlay buffer.
public Span GetOverlays(int length)
{
ArgumentOutOfRangeException.ThrowIfNegative(length);
return this.overlaysOwner.Memory.Span[..length];
}
///
/// Gets the shared vector scratch for the requested row length and vector row count.
///
/// The number of pixels in the row.
/// The number of temporary vector rows required.
/// A slice of the worker-local pooled vector scratch buffer.
public Span GetBlendScratch(int length, int vectorRows)
{
ArgumentOutOfRangeException.ThrowIfNegative(length);
ArgumentOutOfRangeException.ThrowIfLessThan(vectorRows, 1);
return this.blendScratchOwner.Memory.Span[..(length * vectorRows)];
}
///
public void Dispose()
{
this.amountsOwner.Dispose();
this.overlaysOwner.Dispose();
this.blendScratchOwner.Dispose();
}
}
}