Browse Source

Added ColorSpace to ChunkPool

Krzysztof Krysiński 4 months ago
parent
commit
274fc5c4d5
3 changed files with 20 additions and 15 deletions
  1. 4 1
      src/ChunkyImageLib/Chunk.cs
  2. 15 13
      src/ChunkyImageLib/ChunkPool.cs
  3. 1 1
      src/Drawie

+ 4 - 1
src/ChunkyImageLib/Chunk.cs

@@ -45,6 +45,8 @@ public class Chunk : IDisposable
     /// The resolution of the chunk
     /// </summary>
     public ChunkResolution Resolution { get; }
+
+    public ColorSpace ColorSpace { get; }
     
     public bool Disposed => returned;
 
@@ -54,6 +56,7 @@ public class Chunk : IDisposable
         int size = resolution.PixelSize();
 
         Resolution = resolution;
+        ColorSpace = colorSpace;
         PixelSize = new(size, size);
         internalSurface = new Surface(new ImageInfo(size, size, ColorType.RgbaF16, AlphaType.Premul, colorSpace));
     }
@@ -63,7 +66,7 @@ public class Chunk : IDisposable
     /// </summary>
     public static Chunk Create(ColorSpace chunkCs, ChunkResolution resolution = ChunkResolution.Full)
     {
-        var chunk = ChunkPool.Instance.Get(resolution) ?? new Chunk(resolution, chunkCs);
+        var chunk = ChunkPool.Instance.Get(resolution, chunkCs) ?? new Chunk(resolution, chunkCs);
         chunk.returned = false;
         Interlocked.Increment(ref chunkCounter);
         return chunk;

+ 15 - 13
src/ChunkyImageLib/ChunkPool.cs

@@ -1,5 +1,6 @@
 using ChunkyImageLib.DataHolders;
 using System.Collections.Concurrent;
+using Drawie.Backend.Core.Surfaces.ImageData;
 
 namespace ChunkyImageLib;
 
@@ -28,26 +29,27 @@ internal class ChunkPool
         }
     }
 
-    private readonly ConcurrentBag<Chunk> fullChunks = new();
-    private readonly ConcurrentBag<Chunk> halfChunks = new();
-    private readonly ConcurrentBag<Chunk> quarterChunks = new();
-    private readonly ConcurrentBag<Chunk> eighthChunks = new();
-    
+    private readonly ConcurrentDictionary<ColorSpace, ConcurrentBag<Chunk>> fullChunks = new();
+    private readonly ConcurrentDictionary<ColorSpace, ConcurrentBag<Chunk>> halfChunks = new();
+    private readonly ConcurrentDictionary<ColorSpace, ConcurrentBag<Chunk>> quarterChunks = new();
+    private readonly ConcurrentDictionary<ColorSpace, ConcurrentBag<Chunk>> eighthChunks = new();
+
     /// <summary>
     /// Tries to take a chunk from the pool, returns null if there's no Chunk available
     /// </summary>
     /// <param name="resolution">The resolution for the chunk</param>
-    internal Chunk? Get(ChunkResolution resolution) => GetBag(resolution).TryTake(out Chunk? item) ? item : null;
+    /// <param name="chunkCs"></param>
+    internal Chunk? Get(ChunkResolution resolution, ColorSpace chunkCs) => GetBag(resolution, chunkCs).TryTake(out Chunk? item) ? item : null;
 
-    private ConcurrentBag<Chunk> GetBag(ChunkResolution resolution)
+    private ConcurrentBag<Chunk> GetBag(ChunkResolution resolution, ColorSpace colorSpace)
     {
         return resolution switch
         {
-            ChunkResolution.Full => fullChunks,
-            ChunkResolution.Half => halfChunks,
-            ChunkResolution.Quarter => quarterChunks,
-            ChunkResolution.Eighth => eighthChunks,
-            _ => fullChunks
+            ChunkResolution.Full => fullChunks.GetOrAdd(colorSpace, _ => new ConcurrentBag<Chunk>()),
+            ChunkResolution.Half => halfChunks.GetOrAdd(colorSpace, _ => new ConcurrentBag<Chunk>()),
+            ChunkResolution.Quarter => quarterChunks.GetOrAdd(colorSpace, _ => new ConcurrentBag<Chunk>()),
+            ChunkResolution.Eighth => eighthChunks.GetOrAdd(colorSpace, _ => new ConcurrentBag<Chunk>()),
+            _ => fullChunks.GetOrAdd(colorSpace, _ => new ConcurrentBag<Chunk>()),
         };
     }
 
@@ -56,7 +58,7 @@ internal class ChunkPool
     /// </summary>
     internal void Push(Chunk chunk)
     {
-        var chunks = GetBag(chunk.Resolution);
+        var chunks = GetBag(chunk.Resolution, chunk.ColorSpace);
         //a race condition can cause the count to go above 200, but likely not by much
         if (chunks.Count < 200)
             chunks.Add(chunk);

+ 1 - 1
src/Drawie

@@ -1 +1 @@
-Subproject commit da607779e41aed4a24c95273b72f866ae91027c0
+Subproject commit 75401debddfa69cc21f617042aeb42b3e2b43c64