Browse Source

Previews are now color space correct

flabbet 7 months ago
parent
commit
b3bc4f404c

+ 27 - 1
src/PixiEditor.ChangeableDocument/Rendering/DocumentRenderer.cs

@@ -18,6 +18,8 @@ public class DocumentRenderer : IPreviewRenderable
         BlendMode = BlendMode.Src, Color = Drawie.Backend.Core.ColorsImpl.Colors.Transparent
         BlendMode = BlendMode.Src, Color = Drawie.Backend.Core.ColorsImpl.Colors.Transparent
     };
     };
 
 
+    private Texture renderTexture;
+    
     public DocumentRenderer(IReadOnlyDocument document)
     public DocumentRenderer(IReadOnlyDocument document)
     {
     {
         Document = document;
         Document = document;
@@ -126,7 +128,21 @@ public class DocumentRenderer : IPreviewRenderable
     public bool RenderPreview(DrawingSurface renderOn, RenderContext context,
     public bool RenderPreview(DrawingSurface renderOn, RenderContext context,
         string elementToRenderName)
         string elementToRenderName)
     {
     {
+        IsBusy = true;
+        
+        if(renderTexture == null || renderTexture.Size != Document.Size)
+        {
+            renderTexture?.Dispose();
+            renderTexture = Texture.ForProcessing(Document.Size, Document.ProcessingColorSpace);
+        }
+        
+        renderTexture.DrawingSurface.Canvas.Clear();
+        context.RenderSurface = renderTexture.DrawingSurface;
         Document.NodeGraph.Execute(context);
         Document.NodeGraph.Execute(context);
+        
+        renderOn.Canvas.DrawSurface(renderTexture.DrawingSurface, 0, 0);
+        
+        IsBusy = false;
 
 
         return true;
         return true;
     }
     }
@@ -134,8 +150,18 @@ public class DocumentRenderer : IPreviewRenderable
     public void RenderDocument(DrawingSurface toRenderOn, KeyFrameTime frameTime)
     public void RenderDocument(DrawingSurface toRenderOn, KeyFrameTime frameTime)
     {
     {
         IsBusy = true;
         IsBusy = true;
-        RenderContext context = new(toRenderOn, frameTime, ChunkResolution.Full, Document.Size, Document.ProcessingColorSpace) { FullRerender = true };
+        
+        if(renderTexture == null || renderTexture.Size != Document.Size)
+        {
+            renderTexture?.Dispose();
+            renderTexture = Texture.ForProcessing(Document.Size, Document.ProcessingColorSpace);
+        }
+        
+        renderTexture.DrawingSurface.Canvas.Clear();
+        RenderContext context = new(renderTexture.DrawingSurface, frameTime, ChunkResolution.Full, Document.Size, Document.ProcessingColorSpace) { FullRerender = true };
         Document.NodeGraph.Execute(context);
         Document.NodeGraph.Execute(context);
+        
+        toRenderOn.Canvas.DrawSurface(renderTexture.DrawingSurface, 0, 0);
         IsBusy = false;
         IsBusy = false;
     }
     }
 }
 }

+ 18 - 4
src/PixiEditor/Models/Rendering/PreviewPainter.cs

@@ -1,4 +1,6 @@
-using ChunkyImageLib.DataHolders;
+using Avalonia;
+using ChunkyImageLib.DataHolders;
+using Drawie.Backend.Core;
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Animations;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
 using Drawie.Backend.Core.Surfaces;
 using Drawie.Backend.Core.Surfaces;
@@ -17,6 +19,8 @@ public class PreviewPainter
     public KeyFrameTime FrameTime { get; set; }
     public KeyFrameTime FrameTime { get; set; }
     public VecI DocumentSize { get; set; }
     public VecI DocumentSize { get; set; }
     
     
+    private Texture renderTexture;
+    
     public PreviewPainter(IPreviewRenderable previewRenderable, KeyFrameTime frameTime, VecI documentSize, ColorSpace processingColorSpace, string elementToRenderName = "")
     public PreviewPainter(IPreviewRenderable previewRenderable, KeyFrameTime frameTime, VecI documentSize, ColorSpace processingColorSpace, string elementToRenderName = "")
     {
     {
         PreviewRenderable = previewRenderable;
         PreviewRenderable = previewRenderable;
@@ -26,16 +30,26 @@ public class PreviewPainter
         DocumentSize = documentSize;
         DocumentSize = documentSize;
     }
     }
 
 
-    public void Paint(DrawingSurface renderOn) 
+    public void Paint(DrawingSurface renderOn, VecI boundsSize) 
     {
     {
         if (PreviewRenderable == null)
         if (PreviewRenderable == null)
         {
         {
             return;
             return;
         }
         }
 
 
-        RenderContext context = new(renderOn, FrameTime, ChunkResolution.Full, DocumentSize, ProcessingColorSpace);
+        if (renderTexture == null || renderTexture.Size != boundsSize)
+        {
+            renderTexture?.Dispose();
+            renderTexture = Texture.ForProcessing(boundsSize, ProcessingColorSpace);
+        }
+        
+        renderTexture.DrawingSurface.Canvas.Clear();
+        
+        RenderContext context = new(renderTexture.DrawingSurface, FrameTime, ChunkResolution.Full, DocumentSize, ProcessingColorSpace);
 
 
-        PreviewRenderable.RenderPreview(renderOn, context, ElementToRenderName);
+        PreviewRenderable.RenderPreview(renderTexture.DrawingSurface, context, ElementToRenderName);
+        
+        renderOn.Canvas.DrawSurface(renderTexture.DrawingSurface, 0, 0);
     }
     }
 
 
     public void Repaint()
     public void Repaint()

+ 5 - 8
src/PixiEditor/ViewModels/Document/DocumentViewModel.cs

@@ -502,17 +502,14 @@ internal partial class DocumentViewModel : PixiObservableObject, IDocument
             Surface finalSurface = null;
             Surface finalSurface = null;
             DrawingBackendApi.Current.RenderingDispatcher.Invoke(() =>
             DrawingBackendApi.Current.RenderingDispatcher.Invoke(() =>
             {
             {
-                using Texture texture = Texture.ForProcessing(renderSize, Internals.Tracker.Document.ProcessingColorSpace);
-                texture.DrawingSurface.Canvas.Save();
+                finalSurface = new Surface(renderSize);
+                finalSurface.DrawingSurface.Canvas.Save();
                 VecD scaling = new VecD(renderSize.X / (double)SizeBindable.X, renderSize.Y / (double)SizeBindable.Y);
                 VecD scaling = new VecD(renderSize.X / (double)SizeBindable.X, renderSize.Y / (double)SizeBindable.Y);
 
 
-                texture.DrawingSurface.Canvas.Scale((float)scaling.X, (float)scaling.Y);
-                Renderer.RenderDocument(texture.DrawingSurface, frameTime);
+                finalSurface.DrawingSurface.Canvas.Scale((float)scaling.X, (float)scaling.Y);
+                Renderer.RenderDocument(finalSurface.DrawingSurface, frameTime);
 
 
-                texture.DrawingSurface.Canvas.Restore();
-                
-                finalSurface = new Surface(renderSize);
-                finalSurface.DrawingSurface.Canvas.DrawSurface(texture.DrawingSurface, 0, 0);
+                finalSurface.DrawingSurface.Canvas.Restore();
             });
             });
 
 
             return finalSurface;
             return finalSurface;

+ 1 - 1
src/PixiEditor/Views/Visuals/PreviewPainterControl.cs

@@ -79,7 +79,7 @@ public class PreviewPainterControl : DrawieControl
             UniformScale(x, y, surface, previewBounds.Value);
             UniformScale(x, y, surface, previewBounds.Value);
         }
         }
 
 
-        PreviewPainter.Paint(surface);
+        PreviewPainter.Paint(surface, new VecI((int)Bounds.Size.Width, (int)Bounds.Size.Height));
 
 
         surface.Canvas.Restore();
         surface.Canvas.Restore();
     }
     }