Skip to content

Rendering performance improvement #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions Pixed.Application/Controls/ImageGrid.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Pixed.Application.Utils;
using Avalonia;
using Pixed.Application.Utils;
using Pixed.Core;
using SkiaSharp;
using System;


namespace Pixed.Application.Controls;
internal class ImageGrid : OverlayControl
{
public const int MinGridSize = 15;
public ImageGrid()
{
ClipToBounds = false;
Expand All @@ -21,18 +24,44 @@
{
float stepX = GridWidth.ToFloat();
float stepY = GridHeight.ToFloat();
float left = Bounds.Left.ToFloat();
float top = Bounds.Top.ToFloat();
float right = Bounds.Right.ToFloat() / Zoom.ToFloat();
float bottom = Bounds.Bottom.ToFloat() / Zoom.ToFloat();
float leftBounds = Bounds.Left.ToFloat() / Zoom.ToFloat();
float topBounds = Bounds.Top.ToFloat() / Zoom.ToFloat();
float rightBounds = Bounds.Right.ToFloat() / Zoom.ToFloat();
float bottomBounds = Bounds.Bottom.ToFloat() / Zoom.ToFloat();
float firstStepX = stepX;
float firstStepY = stepY;
float left = leftBounds;
float top = topBounds;
float right = rightBounds;
float bottom = bottomBounds;

for (float x = stepX; x < (int)right; x += stepX)
if (VisualToZoomMatrix.HasValue && ZoomBorder != null)
{
canvas.DrawLine(new SKPoint(left + x, top), new SKPoint(left + x, bottom), new SKPaint() { Color = GridColor });
var inverted = VisualToZoomMatrix.Value.Invert();
var gridSize = new Point(GridWidth * Zoom, GridHeight * Zoom);

if(gridSize.X < MinGridSize | gridSize.Y < MinGridSize)

Check failure on line 43 in Pixed.Application/Controls/ImageGrid.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Pixed.Application/Controls/ImageGrid.cs#L43

Correct this '|' to '||' and extract the right operand to a variable if it should always be evaluated.
{
return;
}
var firstTransform = inverted.Transform(new Point(0, 0));
var lastTransform = inverted.Transform(new Point(ZoomBorder.Bounds.Size.Width, ZoomBorder.Bounds.Size.Height));

left = Math.Max(left, Math.Round(firstTransform.X).ToFloat() - 1f);
top = Math.Max(top, Math.Round(firstTransform.Y).ToFloat() - 1f);
bottom = Math.Min(bottomBounds, Math.Round(lastTransform.Y).ToFloat() + 1f);
right = Math.Min(rightBounds, Math.Round(lastTransform.X).ToFloat() + 1f);
firstStepX = ((int)left / (int)stepX) * stepX;
firstStepY = ((int)top / (int)stepY) * stepY;
}

for (float x = firstStepX; x < (int)right; x += stepX)
{
canvas.DrawLine(new SKPoint(leftBounds + x, topBounds), new SKPoint(leftBounds + x, bottomBounds), new SKPaint() { Color = GridColor });
}
for (float y = stepY; y < (int)bottom; y += stepY)
for (float y = firstStepY; y < MathF.Floor(bottom); y += stepY)
{
canvas.DrawLine(new SKPoint(left, top + y), new SKPoint(right, top + y), new SKPaint() { Color = GridColor });
canvas.DrawLine(new SKPoint(leftBounds, topBounds + y), new SKPoint(rightBounds, topBounds + y), new SKPaint() { Color = GridColor });
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions Pixed.Application/Controls/OverlayControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Avalonia.Rendering.SceneGraph;
using Avalonia.Skia;
using Avalonia.Threading;
using Pixed.Application.Zoom;
using SkiaSharp;

namespace Pixed.Application.Controls;
Expand Down Expand Up @@ -42,10 +43,21 @@ public void Render(ImmediateDrawingContext context)
}

public virtual double Zoom { get; set; } = 1;
protected ZoomBorder? ZoomBorder { get; private set; }
protected Matrix? VisualToZoomMatrix { get; private set; }

public void AttachToZoomBorder(ZoomBorder zoomBorder)
{
ZoomBorder = zoomBorder;
}

public override void Render(DrawingContext context)
{
context.Custom(new DrawOperation(new Rect(Bounds.X, Bounds.Y, Bounds.Width, Bounds.Height), this));
if(ZoomBorder != null)
{
VisualToZoomMatrix = this.TransformToVisual(ZoomBorder);
}
Dispatcher.UIThread.InvokeAsync(InvalidateVisual, DispatcherPriority.Background);
}

Expand Down
4 changes: 2 additions & 2 deletions Pixed.Application/Controls/PaintCanvas.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<Border BorderBrush="{StaticResource Accent}" BorderThickness="0 2 0 0">
<Border BorderBrush="{StaticResource ButtonBorder}" BorderThickness="2 0 2 2">
<zoom:ZoomBorder Name="zoomBorder" ZoomSpeed="1.2" ClipToBounds="True" GestureZoomEnabled="{Binding GestureZoomEnabled, Mode=TwoWay}">
<Canvas
<zoom:ZoomCanvas
Name="imageGrid"
Width="{Binding GridWidth, Mode=TwoWay}"
Height="{Binding GridHeight, Mode=TwoWay}"
Expand All @@ -37,7 +37,7 @@
<controls:PaintCanvasImageControl Name="image" RenderOptions.BitmapInterpolationMode="None" Source="{Binding RenderModel}"/>
<controls:ImageGrid Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="gridCanvas" IsHitTestVisible="False"/>
<controls:SelectionOverlay Width="{Binding ScaledGridWidth}" Height="{Binding ScaledGridHeight}" Name="selectionOverlay" IsHitTestVisible="False"/>
</Canvas>
</zoom:ZoomCanvas>
</zoom:ZoomBorder>
</Border>
</Border>
Expand Down
52 changes: 32 additions & 20 deletions Pixed.Application/Controls/TransparentBackground.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
using Pixed.Application.Utils;
using Avalonia.Interactivity;
using Pixed.Application.Utils;
using Pixed.Core;
using SkiaSharp;
using System;

namespace Pixed.Application.Controls;
internal class TransparentBackground : OverlayControl
{
private readonly SKBitmap _transparentBackground;
private readonly SKShader _shader;
private readonly SKPaint _paint;
public TransparentBackground()
{
ClipToBounds = true;
_transparentBackground = CreateTransparentBackground();
_shader = SKShader.CreateBitmap(_transparentBackground, SKShaderTileMode.Repeat, SKShaderTileMode.Repeat);
_paint = new SKPaint()
{
Shader = _shader,
};
}
public override void Render(SKCanvas canvas)

protected override void OnUnloaded(RoutedEventArgs e)
{
SKPaint paint1 = new() { Color = new UniColor(76, 76, 76), Style = SKPaintStyle.Fill };
SKPaint paint2 = new() { Color = new UniColor(85, 85, 85), Style = SKPaintStyle.Fill };
base.OnUnloaded(e);
_transparentBackground.Dispose();
_shader.Dispose();
_paint.Dispose();
}

float right = Bounds.Right.ToFloat();
float bottom = Bounds.Bottom.ToFloat();
int xCount = ((int)right / 32)+1;
int yCount = ((int)bottom / 32)+1;
float size = 32f / Zoom.ToFloat();
public override void Render(SKCanvas canvas)
{
canvas.DrawRect(SKRect.Create(Bounds.Width.ToFloat() / Zoom.ToFloat(), Bounds.Height.ToFloat() / Zoom.ToFloat()), _paint);
}

for(int x = 0; x < xCount; x++)
{
for(int y = 0; y < yCount; y++)
{
int c = (x + y) % 2;
float cellRight = (x.ToFloat() * size) + size;
float cellBottom = (y.ToFloat() * size) + size;
canvas.DrawRect(new SKRect(x.ToFloat() * size, y.ToFloat() * size, MathF.Min(cellRight, right / Zoom.ToFloat()), MathF.Min(cellBottom, bottom / Zoom.ToFloat())), c == 0 ? paint1 : paint2);
}
}
private static SKBitmap CreateTransparentBackground()
{
UniColor color1 = new(76, 76, 76);
UniColor color2 = new(85, 85, 85);
SKBitmap bitmap = new(2, 2);
bitmap.SetPixel(0, 0, color1);
bitmap.SetPixel(1, 0, color2);
bitmap.SetPixel(1, 1, color1);
bitmap.SetPixel(0, 1, color2);
return bitmap;
}
}
12 changes: 12 additions & 0 deletions Pixed.Application/Zoom/ZoomBorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Avalonia.Media;
using Avalonia.Media.Transformation;
using Avalonia.Reactive;
using Avalonia.VisualTree;
using Pixed.Application.Controls;
using System;
using System.Linq;

Expand Down Expand Up @@ -205,6 +207,16 @@ private void ChildChanged(Control? element)
if (element != null && element != _element)
{
AttachElement(element);

var children = element.GetVisualChildren();

foreach( var child in children )
{
if(child is OverlayControl control)
{
control.AttachToZoomBorder(this);
}
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions Pixed.Application/Zoom/ZoomCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia.Controls;
using Pixed.Application.Controls;
using System.Collections.Specialized;

namespace Pixed.Application.Zoom;
internal class ZoomCanvas : Canvas
{
protected override void ChildrenChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if(Parent is ZoomBorder zoomBorder)
{
foreach (var child in Children)
{
if (child is OverlayControl control)
{
control.AttachToZoomBorder(zoomBorder);
}
}
}

base.ChildrenChanged(sender, e);
}
}
5 changes: 3 additions & 2 deletions Pixed.Core/Utils/SkiaUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
canvas.DrawLine(p0, p1, paint);
}

public static void DrawPatternLine(this SKCanvas canvas, SKPoint p0, SKPoint p1, float[] pattern, UniColor color)
public static void DrawPatternLine(this SKCanvas canvas, SKPoint p0, SKPoint p1, float[] pattern, UniColor color, float lineWidth = 1f)

Check failure on line 102 in Pixed.Core/Utils/SkiaUtils.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Pixed.Core/Utils/SkiaUtils.cs#L102

Use the overloading mechanism instead of the optional parameters.
{
SKPath path = new();
path.MoveTo(p0.X, p0.Y);
Expand All @@ -111,7 +111,8 @@
{
Color = color,
PathEffect = effect,
Style = SKPaintStyle.Stroke
Style = SKPaintStyle.Stroke,
StrokeWidth = lineWidth
};

canvas.DrawPath(path, paint);
Expand Down
Loading