浏览代码

Byte Span deserialization

Krzysztof Krysiński 2 天之前
父节点
当前提交
4bda2dc575

+ 3 - 0
src/ChunkyImageLib/Chunk.cs

@@ -69,6 +69,8 @@ public class Chunk : IDisposable
     public static Chunk Create(ColorSpace chunkCs, ChunkResolution resolution = ChunkResolution.Full)
     {
         return new Chunk(resolution, chunkCs);
+
+        // Leaving this in case chunk pooling turns out to be better
         /*var chunk = ChunkPool.Instance.Get(resolution, chunkCs);
         if (chunk == null || chunk.Disposed)
         {
@@ -140,6 +142,7 @@ public class Chunk : IDisposable
     {
         returned = true;
         internalSurface.Dispose();
+        // Leaving this in case chunk pooling turns out to be better
         /*if (returned)
             return;
         Interlocked.Decrement(ref chunkCounter);

+ 21 - 17
src/PixiEditor/Models/Rendering/AffectedAreasGatherer.cs

@@ -212,28 +212,32 @@ internal class AffectedAreasGatherer
                     AddWholeCanvasToEveryMaskPreview();
                     break;
                 case RefreshPreview_PassthroughAction info:
-                    if (info.SubId == null)
-                    {
-                        if (info.ElementToRender == nameof(StructureNode.EmbeddedMask))
-                        {
-                            AddToMaskPreview(info.Id);
-                        }
-                        else
-                        {
-                            AddToImagePreviews(info.Id);
-                            AddToNodePreviews(info.Id);
-                        }
-                    }
-                    else
-                    {
-                        AddKeyFrame(info.SubId.Value);
-                    }
-
+                    ProcessRefreshPreview(info);
                     break;
             }
         }
     }
 
+    private void ProcessRefreshPreview(RefreshPreview_PassthroughAction info)
+    {
+        if (info.SubId == null)
+        {
+            if (info.ElementToRender == nameof(StructureNode.EmbeddedMask))
+            {
+                AddToMaskPreview(info.Id);
+            }
+            else
+            {
+                AddToImagePreviews(info.Id);
+                AddToNodePreviews(info.Id);
+            }
+        }
+        else
+        {
+            AddKeyFrame(info.SubId.Value);
+        }
+    }
+
     private void AddAllKeyFrames()
     {
         ChangedKeyFrames ??= new HashSet<Guid>();

+ 3 - 6
src/PixiEditor/Models/Serialization/Factories/ByteExtractor.cs

@@ -123,13 +123,10 @@ public class ByteExtractor
         return sb.ToString();
     }
 
-    public byte[] GetByteArray(int length)
+    public Span<byte> GetByteSpan(int length)
     {
-        byte[] value = new byte[length];
-        Array.Copy(_data, Position, value, 0, length);
-
+        Span<byte> span = new Span<byte>(_data, Position, length);
         Position += length;
-
-        return value;
+        return span;
     }
 }

+ 1 - 1
src/PixiEditor/Models/Serialization/Factories/ChunkyImageSerializationFactory.cs

@@ -72,7 +72,7 @@ public class ChunkyImageSerializationFactory : SerializationFactory<byte[], Chun
         {
             VecD chunkPos = byteExtractor.GetVecD();
             int chunkDataLength = byteExtractor.GetInt();
-            byte[] chunkData = byteExtractor.GetByteArray(chunkDataLength);
+            Span<byte> chunkData = byteExtractor.GetByteSpan(chunkDataLength);
             if (!surfaceFactory.TryDeserialize(chunkData, out Surface chunkSurface, serializerData))
             {
                 original.Dispose();

+ 15 - 4
src/PixiEditor/Models/Serialization/Factories/SurfaceSerializationFactory.cs

@@ -22,7 +22,7 @@ public class SurfaceSerializationFactory : SerializationFactory<byte[], Surface>
     {
         if (serialized is byte[] imgBytes)
         {
-            original = DecodeSurface(imgBytes, Config.Encoder, Config.ProcessingColorSpace);
+            original = DecodeSurface(imgBytes, Config.Encoder);
             return true;
         }
 
@@ -30,11 +30,23 @@ public class SurfaceSerializationFactory : SerializationFactory<byte[], Surface>
         return false;
     }
 
+    public bool TryDeserialize(Span<byte> serialized, out Surface original,
+        (string serializerName, string serializerVersion) serializerData)
+    {
+        original = DecodeSurface(serialized, Config.Encoder);
+        return true;
+    }
+
 
-    public static Surface DecodeSurface(byte[] imgBytes, ImageEncoder encoder, ColorSpace processingColorSpace)
+    public static Surface DecodeSurface(byte[] imgBytes, ImageEncoder encoder)
+    {
+        return DecodeSurface(imgBytes.AsSpan(), encoder);
+    }
+
+    public static Surface DecodeSurface(Span<byte> imgSpan, ImageEncoder encoder)
     {
         byte[] decoded =
-            encoder.Decode(imgBytes, out SKImageInfo info);
+            encoder.Decode(imgSpan, out SKImageInfo info);
         ImageInfo finalInfo = info.ToImageInfo();
 
         using Image img = Image.FromPixels(finalInfo, decoded);
@@ -46,6 +58,5 @@ public class SurfaceSerializationFactory : SerializationFactory<byte[], Surface>
         return surface;
     }
 
-
     public override string DeserializationId { get; } = "PixiEditor.Surface";
 }

+ 1 - 4
src/PixiEditor/Views/Visuals/PreviewTextureControl.cs

@@ -29,10 +29,7 @@ public class PreviewTextureControl : DrawieControl
         base.OnAttachedToVisualTree(e);
         if (TexturePreview != null)
         {
-            TexturePreview.Attach(this, () =>
-            {
-                return GetBounds();
-            });
+            TexturePreview.Attach(this, GetBounds);
             TexturePreview.TextureUpdated += QueueNextFrame;
         }
     }

+ 1 - 1
src/PixiParser

@@ -1 +1 @@
-Subproject commit d7a83f53f4a0e6a0e0d011cb045ab1f2075e759b
+Subproject commit 0e9c1ec319cb59ab5dffe2eb0e7b8e4165b732d6