Browse Source

Fixed clip to layer

flabbet 10 months ago
parent
commit
c2ebd6dc96

+ 1 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ImageLayerNode.cs

@@ -90,8 +90,7 @@ public class ImageLayerNode : LayerNode, IReadOnlyImageNode
                 ctx.ChunkToUpdate * ctx.ChunkResolution.PixelSize(),
                 ctx.ChunkToUpdate * ctx.ChunkResolution.PixelSize(),
                 blendPaint) && shouldClear)
                 blendPaint) && shouldClear)
         {
         {
-            // TODO: Is it necessary to clear the surface?
-            //workingSurface.Canvas.DrawRect((RectD)CalculateDestinationRect(ctx), clearPaint);
+            workingSurface.Canvas.DrawRect((RectD)CalculateDestinationRect(ctx), clearPaint);
         }
         }
     }
     }
 
 

+ 22 - 17
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/LayerNode.cs

@@ -32,15 +32,15 @@ public abstract class LayerNode : StructureNode, IReadOnlyLayerNode
 
 
         /*if (FilterlessOutput.Connections.Count > 0)
         /*if (FilterlessOutput.Connections.Count > 0)
         {
         {
-            var filterlessWorkingSurface = TryInitWorkingSurface(targetSize, context, 0);
+            var filterlessWorkingSurface = TryInitWorkingSurface(targetSize, sceneContext.ChunkResolution, 0);
 
 
             if (Background.Value != null)
             if (Background.Value != null)
             {
             {
-                DrawBackground(filterlessWorkingSurface.DrawingSurface, context);
-                blendPaint.BlendMode = RenderingContext.GetDrawingBlendMode(BlendMode.Value);
+                DrawBackground(filterlessWorkingSurface.DrawingSurface, sceneContext);
+                blendPaint.BlendMode = RenderContext.GetDrawingBlendMode(BlendMode.Value);
             }
             }
 
 
-            DrawLayer(context, filterlessWorkingSurface, shouldClear, useFilters: false);
+            DrawLayer(sceneContext, filterlessWorkingSurface.DrawingSurface, shouldClear, useFilters: false);
             blendPaint.BlendMode = DrawingApi.Core.Surfaces.BlendMode.Src;
             blendPaint.BlendMode = DrawingApi.Core.Surfaces.BlendMode.Src;
 
 
             FilterlessOutput.Value = filterlessWorkingSurface;
             FilterlessOutput.Value = filterlessWorkingSurface;
@@ -57,43 +57,48 @@ public abstract class LayerNode : StructureNode, IReadOnlyLayerNode
         {
         {
             if (!HasOperations())
             if (!HasOperations())
             {
             {
-                /*
                 if (Background.Value != null)
                 if (Background.Value != null)
                 {
                 {
-                    DrawBackground(outputWorkingSurface.DrawingSurface, context);
                     blendPaint.BlendMode = RenderContext.GetDrawingBlendMode(BlendMode.Value);
                     blendPaint.BlendMode = RenderContext.GetDrawingBlendMode(BlendMode.Value);
                 }
                 }
-                */
-
-                DrawLayer(context, renderOnto, shouldClear);
+                
+                DrawLayer(context, renderOnto, false);
 
 
                 return;
                 return;
             }
             }
 
 
-            //var outputWorkingSurface = TryInitWorkingSurface(size, context.ChunkResolution, 1);
+            var outputWorkingSurface = TryInitWorkingSurface(size, context.ChunkResolution, 1);
             
             
-            DrawLayer(context, renderOnto, true);
+            DrawLayer(context, outputWorkingSurface.DrawingSurface, true);
 
 
             // shit gets downhill with mask on big canvases, TODO: optimize
             // shit gets downhill with mask on big canvases, TODO: optimize
-            ApplyMaskIfPresent(renderOnto, context);
+            ApplyMaskIfPresent(outputWorkingSurface.DrawingSurface, context);
 
 
             if (Background.Value != null)
             if (Background.Value != null)
             {
             {
-                Texture tempSurface = RequestTexture(4, size);
-                DrawBackground(tempSurface.DrawingSurface, context);
-                ApplyRasterClip(renderOnto, tempSurface.DrawingSurface);
+                using Texture tempSurface = RequestTexture(4, size);
+                if (Background.Connection.Node is LayerNode layerNode)
+                {
+                    // TODO: This probably should work with StructureMembers not Layers only
+                    DrawPreviousLayer(tempSurface.DrawingSurface, layerNode, context);
+                }
+
+                ApplyRasterClip(outputWorkingSurface.DrawingSurface, tempSurface.DrawingSurface);
                 blendPaint.BlendMode = RenderContext.GetDrawingBlendMode(BlendMode.Value);
                 blendPaint.BlendMode = RenderContext.GetDrawingBlendMode(BlendMode.Value);
-                tempSurface.DrawingSurface.Canvas.DrawSurface(renderOnto, 0, 0,
+                tempSurface.DrawingSurface.Canvas.DrawSurface(outputWorkingSurface.DrawingSurface, 0, 0,
                     blendPaint);
                     blendPaint);
 
 
                 renderOnto.Canvas.DrawSurface(tempSurface.DrawingSurface, VecI.Zero, blendPaint);
                 renderOnto.Canvas.DrawSurface(tempSurface.DrawingSurface, VecI.Zero, blendPaint);
+                return;
             }
             }
+
+            renderOnto.Canvas.DrawSurface(outputWorkingSurface.DrawingSurface, 0, 0, blendPaint);
         }
         }
     }
     }
 
 
     protected abstract VecI GetTargetSize(RenderContext ctx);
     protected abstract VecI GetTargetSize(RenderContext ctx);
 
 
-    protected virtual void DrawLayer(SceneObjectRenderContext ctx, DrawingSurface workingSurface, bool shouldClear,
+    protected internal virtual void DrawLayer(SceneObjectRenderContext ctx, DrawingSurface workingSurface, bool shouldClear,
         bool useFilters = true)
         bool useFilters = true)
     {
     {
         blendPaint.Color = blendPaint.Color.WithAlpha((byte)Math.Round(Opacity.Value * 255));
         blendPaint.Color = blendPaint.Color.WithAlpha((byte)Math.Round(Opacity.Value * 255));

+ 5 - 4
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/StructureNode.cs

@@ -140,10 +140,10 @@ public abstract class StructureNode : Node, IReadOnlyStructureNode, IBackgroundI
         return (MaskIsVisible.Value && (EmbeddedMask != null || CustomMask.Value != null)) || ClipToPreviousMember;
         return (MaskIsVisible.Value && (EmbeddedMask != null || CustomMask.Value != null)) || ClipToPreviousMember;
     }
     }
 
 
-    protected void DrawBackground(DrawingSurface drawOnto, RenderContext context)
+    protected void DrawPreviousLayer(DrawingSurface drawOnto, LayerNode previousNode, SceneObjectRenderContext context)
     {
     {
         blendPaint.Color = Colors.White;
         blendPaint.Color = Colors.White;
-        DrawSurface(drawOnto, Background.Value, context, null);
+        previousNode.DrawLayer(context, drawOnto, false);
     }
     }
 
 
     protected void DrawSurface(DrawingSurface workingSurface, DrawingSurface source, RenderContext context,
     protected void DrawSurface(DrawingSurface workingSurface, DrawingSurface source, RenderContext context,
@@ -155,9 +155,10 @@ public abstract class StructureNode : Node, IReadOnlyStructureNode, IBackgroundI
         RectI targetRect = CalculateDestinationRect(context);
         RectI targetRect = CalculateDestinationRect(context);
         using var snapshot = source.DrawingSurface.Snapshot(sourceRect);
         using var snapshot = source.DrawingSurface.Snapshot(sourceRect);
         */
         */
-
+        
         blendPaint.SetFilters(filter);
         blendPaint.SetFilters(filter);
-        workingSurface.Canvas.DrawSurface(source, 0, 0, blendPaint);
+        
+        workingSurface.Canvas.DrawSurface(source, source.DeviceClipBounds.X, source.DeviceClipBounds.Y, blendPaint);
     }
     }
 
 
     protected RectI CalculateSourceRect(VecI targetSize, VecI sourceSize, RenderContext context)
     protected RectI CalculateSourceRect(VecI targetSize, VecI sourceSize, RenderContext context)

+ 2 - 0
src/PixiEditor.DrawingApi.Core/Bridge/Operations/ISurfaceImplementation.cs

@@ -2,6 +2,7 @@
 using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Core.Surfaces.ImageData;
 using PixiEditor.DrawingApi.Core.Surfaces.ImageData;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
+using PixiEditor.Numerics;
 
 
 namespace PixiEditor.DrawingApi.Core.Bridge.Operations;
 namespace PixiEditor.DrawingApi.Core.Bridge.Operations;
 
 
@@ -18,5 +19,6 @@ public interface ISurfaceImplementation
     public object GetNativeSurface(IntPtr objectPointer);
     public object GetNativeSurface(IntPtr objectPointer);
     public void Flush(DrawingSurface drawingSurface);
     public void Flush(DrawingSurface drawingSurface);
     public DrawingSurface FromNative(object native);
     public DrawingSurface FromNative(object native);
+    public RectI GetDeviceClipBounds(DrawingSurface surface);
 }
 }
 
 

+ 6 - 0
src/PixiEditor.DrawingApi.Core/Surfaces/DrawingSurface.cs

@@ -10,6 +10,12 @@ namespace PixiEditor.DrawingApi.Core.Surfaces
     {
     {
         public override object Native => DrawingBackendApi.Current.SurfaceImplementation.GetNativeSurface(ObjectPointer);
         public override object Native => DrawingBackendApi.Current.SurfaceImplementation.GetNativeSurface(ObjectPointer);
         public Canvas Canvas { get; private set; }
         public Canvas Canvas { get; private set; }
+
+        public RectI DeviceClipBounds
+        {
+            get => DrawingBackendApi.Current.SurfaceImplementation.GetDeviceClipBounds(this);
+        }
+
         public event SurfaceChangedEventHandler? Changed;
         public event SurfaceChangedEventHandler? Changed;
 
 
         public DrawingSurface(IntPtr objPtr, Canvas canvas) : base(objPtr)
         public DrawingSurface(IntPtr objPtr, Canvas canvas) : base(objPtr)

+ 6 - 0
src/PixiEditor.DrawingApi.Skia/Implementations/SkiaSurfaceImplementation.cs

@@ -166,5 +166,11 @@ namespace PixiEditor.DrawingApi.Skia.Implementations
 
 
             return CreateDrawingSurface(skSurface);
             return CreateDrawingSurface(skSurface);
         }
         }
+
+        public RectI GetDeviceClipBounds(DrawingSurface surface)
+        {
+            SKRectI rect = ManagedInstances[surface.ObjectPointer].Canvas.DeviceClipBounds;
+            return new RectI(rect.Left, rect.Top, rect.Width, rect.Height);
+        }
     }
     }
 }
 }