mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
108 changed files with 2257 additions and 1989 deletions
@ -0,0 +1,42 @@ |
|||
// <copyright file="PixelPool{TColor}.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp |
|||
{ |
|||
using System; |
|||
using System.Buffers; |
|||
|
|||
/// <summary>
|
|||
/// Provides a resource pool that enables reusing instances of type <see cref="T:TColor[]"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TColor">The pixel format.</typeparam>
|
|||
public static class PixelPool<TColor> |
|||
where TColor : struct, IPackedPixel, IEquatable<TColor> |
|||
{ |
|||
/// <summary>
|
|||
/// The <see cref="ArrayPool{T}"/> used to pool data. TODO: Choose sensible default size and count
|
|||
/// </summary>
|
|||
private static readonly ArrayPool<TColor> ArrayPool = ArrayPool<TColor>.Create(int.MaxValue, 50); |
|||
|
|||
/// <summary>
|
|||
/// Rents the pixel array from the pool.
|
|||
/// </summary>
|
|||
/// <param name="minimumLength">The minimum length of the array to return.</param>
|
|||
/// <returns>The <see cref="T:TColor[]"/></returns>
|
|||
public static TColor[] RentPixels(int minimumLength) |
|||
{ |
|||
return ArrayPool.Rent(minimumLength); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns the rented pixel array back to the pool.
|
|||
/// </summary>
|
|||
/// <param name="array">The array to return to the buffer pool.</param>
|
|||
public static void ReturnPixels(TColor[] array) |
|||
{ |
|||
ArrayPool.Return(array, true); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,74 @@ |
|||
// <copyright file="PixelPoolTests.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Tests |
|||
{ |
|||
using System.Linq; |
|||
|
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="PixelAccessor"/> class.
|
|||
/// </summary>
|
|||
public class PixelPoolTests |
|||
{ |
|||
[Fact] |
|||
public void PixelPoolRentsMinimumSize() |
|||
{ |
|||
Color[] pixels = PixelPool<Color>.RentPixels(1024); |
|||
|
|||
Assert.True(pixels.Length >= 1024); |
|||
} |
|||
|
|||
[Fact] |
|||
public void PixelPoolRentsEmptyArray() |
|||
{ |
|||
for (int i = 16; i < 1024; i += 16) |
|||
{ |
|||
Color[] pixels = PixelPool<Color>.RentPixels(i); |
|||
|
|||
Assert.True(pixels.All(p => p == default(Color))); |
|||
|
|||
PixelPool<Color>.ReturnPixels(pixels); |
|||
} |
|||
|
|||
for (int i = 16; i < 1024; i += 16) |
|||
{ |
|||
Color[] pixels = PixelPool<Color>.RentPixels(i); |
|||
|
|||
Assert.True(pixels.All(p => p == default(Color))); |
|||
|
|||
PixelPool<Color>.ReturnPixels(pixels); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void PixelPoolDoesNotThrowWhenReturningNonPooled() |
|||
{ |
|||
Color[] pixels = new Color[1024]; |
|||
|
|||
PixelPool<Color>.ReturnPixels(pixels); |
|||
|
|||
Assert.True(pixels.Length >= 1024); |
|||
} |
|||
|
|||
[Fact] |
|||
public void PixelPoolCleansRentedArray() |
|||
{ |
|||
Color[] pixels = PixelPool<Color>.RentPixels(256); |
|||
|
|||
for (int i = 0; i < pixels.Length; i++) |
|||
{ |
|||
pixels[i] = Color.Azure; |
|||
} |
|||
|
|||
Assert.True(pixels.All(p => p == Color.Azure)); |
|||
|
|||
PixelPool<Color>.ReturnPixels(pixels); |
|||
|
|||
Assert.True(pixels.All(p => p == default(Color))); |
|||
} |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue