Skip to content

Commit

Permalink
Optimize drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Equbuxu committed Oct 13, 2021
1 parent d7acc86 commit 35a6ce5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 23 deletions.
3 changes: 0 additions & 3 deletions PixiEditor/Models/Controllers/BitmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace PixiEditor.Models.Controllers
Expand Down Expand Up @@ -246,11 +245,9 @@ private void HighlightPixels(Coordinates newPosition)
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))
{
Expand Down
6 changes: 5 additions & 1 deletion PixiEditor/Models/Controllers/LayerStackRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ private void Update(Int32Rect dirtyRectangle)
}
}
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();
Expand Down
4 changes: 2 additions & 2 deletions PixiEditor/Models/Controllers/SurfaceRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public void Draw(Surface otherSurface, byte opacity)
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();
}
Expand Down
16 changes: 13 additions & 3 deletions PixiEditor/Models/Layers/Layer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ public void DynamicResize(BitmapPixelChanges pixels)
/// </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);
Expand Down Expand Up @@ -466,10 +471,14 @@ public void ClipCanvas()
/// </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>
Expand Down Expand Up @@ -605,6 +614,7 @@ private void ResizeCanvas(int offsetX, int offsetY, int offsetXSrc, int offsetYS
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;
Expand Down
15 changes: 6 additions & 9 deletions PixiEditor/Models/Tools/Tools/LineTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LineTool : ShapeTool
{
private readonly CircleTool circleTool;
private List<Coordinates> linePoints = new List<Coordinates>();
private SKPaint paint = new SKPaint() { Style = SKPaintStyle.Stroke };

public LineTool()
{
Expand Down Expand Up @@ -73,15 +74,11 @@ public override void Use(Layer layer, List<Coordinates> coordinates, SKColor col

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);
}
Expand Down
2 changes: 1 addition & 1 deletion PixiEditor/Models/Tools/Tools/PenTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override void Use(Layer layer, List<Coordinates> coordinates, SKColor col

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);
Expand Down
17 changes: 13 additions & 4 deletions PixiEditor/Views/UserControls/PlainLayerView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public Layer TargetLayer
}

private SurfaceRenderer renderer;
private int prevLayerWidth = -1;
private int prevLayerHeight = -1;

public PlainLayerView()
{
Expand Down Expand Up @@ -54,14 +56,13 @@ private void OnControlSizeChanged(object sender, SizeChangedEventArgs e)
{
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)
Expand All @@ -88,8 +89,16 @@ private bool MaybeResize(Size newSize)

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();
}
}
}
}

0 comments on commit 35a6ce5

Please sign in to comment.