Skip to content

Commit

Permalink
perf(Studio): Optimize Skia-rendering within scrollables
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Oct 27, 2024
1 parent 649f781 commit fd77d45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Studio/CelesteStudio/Controls/SkiaDrawable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@ public abstract class SkiaDrawable : Drawable {
private Bitmap? image = null;
private SKImageInfo imageInfo = SKImageInfo.Empty;

protected virtual int DrawX => 0;
protected virtual int DrawY => 0;
protected virtual int DrawWidth => Width;
protected virtual int DrawHeight => Height;

protected abstract void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo info);

protected override void OnPaint(PaintEventArgs e)
{
try {
if (Width <= 0 || Height <= 0) {
int drawWidth = DrawWidth, drawHeight = DrawHeight;

if (drawWidth <= 0 || drawHeight <= 0) {
return;
}

if (Size != image?.Size)
if (drawWidth != image?.Size.Width || drawHeight != image?.Size.Height)
{
image?.Dispose();
image = new Bitmap(Size, PixelFormat.Format32bppRgba);
imageInfo = new SKImageInfo(Width, Height, colorType, SKAlphaType.Unpremul);
image = new Bitmap(new Size(drawWidth, drawHeight), PixelFormat.Format32bppRgba);
imageInfo = new SKImageInfo(drawWidth, drawHeight, colorType, SKAlphaType.Unpremul);
}

using var bmp = image.Lock();
using var surface = SKSurface.Create(imageInfo, bmp.Data, bmp.ScanWidth);

Draw(e, surface, imageInfo);

e.Graphics.DrawImage(image, PointF.Empty);
e.Graphics.DrawImage(image, DrawX, DrawY);
}
catch (Exception ex)
{
Expand Down
6 changes: 6 additions & 0 deletions Studio/CelesteStudio/Editing/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3620,9 +3620,15 @@ private void UpdateMouseCursor(PointF location, Keys modifiers) {

#region Drawing

protected override int DrawX => scrollablePosition.X;
protected override int DrawY => scrollablePosition.Y;
protected override int DrawWidth => scrollable.Width;
protected override int DrawHeight => scrollable.Height;

protected override void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo imageInfo) {
var canvas = surface.Canvas;
canvas.Clear();
canvas.Translate(-scrollablePosition.X, -scrollablePosition.Y);

using var strokePaint = new SKPaint();
strokePaint.Style = SKPaintStyle.Stroke;
Expand Down
6 changes: 6 additions & 0 deletions Studio/CelesteStudio/Editing/PopupMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ public ContentDrawable(PopupMenu menu) {
menu.Scroll += (_, _) => Invalidate();
}

protected override int DrawX => menu.ScrollPosition.X;
protected override int DrawY => menu.ScrollPosition.Y;
protected override int DrawWidth => menu.Width;
protected override int DrawHeight => menu.Height;

protected override void Draw(PaintEventArgs e, SKSurface surface, SKImageInfo imageInfo) {
surface.Canvas.Clear();
surface.Canvas.Translate(-menu.ScrollPosition.X, -menu.ScrollPosition.Y);

if (menu.shownEntries.Length == 0) {
return;
Expand Down

0 comments on commit fd77d45

Please sign in to comment.