Browse Source

Optimize drawing

Equbuxu 3 years ago
parent
commit
35a6ce5d5c

+ 0 - 3
PixiEditor/Models/Controllers/BitmapManager.cs

@@ -12,7 +12,6 @@ using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Diagnostics;
 using System.Linq;
-using System.Windows;
 using System.Windows.Input;
 
 namespace PixiEditor.Models.Controllers
@@ -246,11 +245,9 @@ namespace PixiEditor.Models.Controllers
                 cachedPixels = BitmapPixelChanges.FromSingleColoredArray(cachedHighlight, new SKColor(0, 0, 0, 77));
 
                 ActiveDocument.PreviewLayer.SetPixels(cachedPixels);
-                ActiveDocument.PreviewLayer.ClipCanvas();
             }
 
             Coordinates start = newPosition - halfSize;
-            ActiveDocument.PreviewLayer.Offset = new Thickness(start.X, start.Y, 0, 0);
 
             if (!IsInsideBounds(cachedHighlight))
             {

+ 5 - 1
PixiEditor/Models/Controllers/LayerStackRenderer.cs

@@ -132,7 +132,11 @@ namespace PixiEditor.Models.Controllers
                 }
             }
             finalBitmap.Lock();
-            finalSurface.SkiaSurface.Draw(backingSurface.Canvas, 0, 0, Surface.ReplacingPaint);
+            using (var snapshot = finalSurface.SkiaSurface.Snapshot())
+            {
+                SKRect rect = new(dirtyRectangle.X, dirtyRectangle.Y, dirtyRectangle.X + dirtyRectangle.Width, dirtyRectangle.Y + dirtyRectangle.Height);
+                backingSurface.Canvas.DrawImage(snapshot, rect, rect, Surface.ReplacingPaint);
+            }
 
             finalBitmap.AddDirtyRect(dirtyRectangle);
             finalBitmap.Unlock();

+ 2 - 2
PixiEditor/Models/Controllers/SurfaceRenderer.cs

@@ -30,8 +30,8 @@ namespace PixiEditor.Models.Controllers
             BackingSurface.Canvas.Clear();
             FinalBitmap.Lock();
             BlendingPaint.Color = new SKColor(255, 255, 255, opacity);
-            //otherSurface.SkiaSurface.Draw(BackingSurface.Canvas, 0, 0, BlendingPaint);
-            BackingSurface.Canvas.DrawImage(otherSurface.SkiaSurface.Snapshot(), new SKRect(0, 0, FinalBitmap.PixelWidth, FinalBitmap.PixelHeight));
+            using (var snapshot = otherSurface.SkiaSurface.Snapshot())
+                BackingSurface.Canvas.DrawImage(snapshot, new SKRect(0, 0, FinalBitmap.PixelWidth, FinalBitmap.PixelHeight));
             FinalBitmap.AddDirtyRect(new Int32Rect(0, 0, FinalBitmap.PixelWidth, FinalBitmap.PixelHeight));
             FinalBitmap.Unlock();
         }

+ 13 - 3
PixiEditor/Models/Layers/Layer.cs

@@ -428,6 +428,11 @@ namespace PixiEditor.Models.Layers
         /// </summary>
         public void DynamicResize(int newMaxX, int newMaxY, int newMinX, int newMinY)
         {
+            if (newMinX < 0) newMinX = 0;
+            if (newMinY < 0) newMinY = 0;
+            if (newMaxX > MaxWidth) newMaxX = MaxWidth;
+            if (newMaxY > MaxHeight) newMaxY = MaxHeight;
+
             if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
             {
                 IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
@@ -466,10 +471,14 @@ namespace PixiEditor.Models.Layers
         /// </summary>
         public void Clear()
         {
+            var dirtyRect = new Int32Rect(OffsetX, OffsetY, Width, Height);
+            LayerBitmap?.Dispose();
+            LayerBitmap = new Surface(1, 1);
+            Width = 1;
+            Height = 1;
+            Offset = new Thickness(0, 0, 0, 0);
             IsCleared = true;
-            LayerBitmap.SkiaSurface.Canvas.Clear();
-            ClipCanvas();
-            InvokeLayerBitmapChange();
+            LayerBitmapChanged?.Invoke(this, dirtyRect);
         }
 
         /// <summary>
@@ -605,6 +614,7 @@ namespace PixiEditor.Models.Layers
             Surface result = new Surface(newWidth, newHeight);
 
             LayerBitmap.SkiaSurface.Draw(result.SkiaSurface.Canvas, offsetX - offsetXSrc, offsetY - offsetYSrc, Surface.ReplacingPaint);
+            LayerBitmap?.Dispose();
             LayerBitmap = result;
             Width = newWidth;
             Height = newHeight;

+ 6 - 9
PixiEditor/Models/Tools/Tools/LineTool.cs

@@ -15,6 +15,7 @@ namespace PixiEditor.Models.Tools.Tools
     {
         private readonly CircleTool circleTool;
         private List<Coordinates> linePoints = new List<Coordinates>();
+        private SKPaint paint = new SKPaint() { Style = SKPaintStyle.Stroke };
 
         public LineTool()
         {
@@ -73,15 +74,11 @@ namespace PixiEditor.Models.Tools.Tools
 
             layer.DynamicResize(expanded.X + expanded.Width - 1, expanded.Y + expanded.Height - 1, expanded.X, expanded.Y);
 
-            using (SKPaint paint = new SKPaint())
-            {
-                paint.StrokeWidth = thickness;
-                paint.Style = SKPaintStyle.Stroke;
-                paint.Color = color;
-                paint.BlendMode = blendMode;
-                paint.StrokeCap = strokeCap;
-                layer.LayerBitmap.SkiaSurface.Canvas.DrawLine(x, y, x1, y1, paint);
-            }
+            paint.StrokeWidth = thickness;
+            paint.Color = color;
+            paint.BlendMode = blendMode;
+            paint.StrokeCap = strokeCap;
+            layer.LayerBitmap.SkiaSurface.Canvas.DrawLine(x, y, x1, y1, paint);
 
             layer.InvokeLayerBitmapChange(dirtyRect);
         }

+ 1 - 1
PixiEditor/Models/Tools/Tools/PenTool.cs

@@ -77,7 +77,7 @@ namespace PixiEditor.Models.Tools.Tools
 
             if (previewLayer != null && previewLayer.GetPixelWithOffset(latestCords.X, latestCords.Y).Alpha > 0)
             {
-                confirmedPixels.Add(latestCords);
+                //confirmedPixels.Add(latestCords);
             }
 
             lineTool.DrawLine(layer, startingCoords, latestCords, color, 1, blendMode, SKStrokeCap.Square);

+ 13 - 4
PixiEditor/Views/UserControls/PlainLayerView.xaml.cs

@@ -18,6 +18,8 @@ namespace PixiEditor.Views.UserControls
         }
 
         private SurfaceRenderer renderer;
+        private int prevLayerWidth = -1;
+        private int prevLayerHeight = -1;
 
         public PlainLayerView()
         {
@@ -54,14 +56,13 @@ namespace PixiEditor.Views.UserControls
         {
             if (TargetLayer == null)
                 return;
-            MaybeResize(e.NewSize);
+            ResizeWithOptimized(e.NewSize);
         }
 
-        private bool MaybeResize(Size newSize)
+        private void ResizeWithOptimized(Size newSize)
         {
             var (w, h) = GetOptimizedDimensions(TargetLayer.Width, TargetLayer.Height, newSize.Width, newSize.Height);
             Resize(w, h);
-            return true;
         }
 
         private (int, int) GetOptimizedDimensions(int width, int height, double viewWidth, double viewHeight)
@@ -88,8 +89,16 @@ namespace PixiEditor.Views.UserControls
 
         private void OnLayerBitmapChanged(object sender, Int32Rect e)
         {
-            if (!MaybeResize(RenderSize))
+            if (TargetLayer.Width != prevLayerWidth || TargetLayer.Height != prevLayerHeight)
+            {
+                ResizeWithOptimized(RenderSize);
+                prevLayerWidth = TargetLayer.Width;
+                prevLayerHeight = TargetLayer.Height;
+            }
+            else
+            {
                 Update();
+            }
         }
     }
 }