-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(Studio): Use custom-platform-control for SkiaSharp on GTK
- Loading branch information
Showing
10 changed files
with
159 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Cairo; | ||
using CelesteStudio.Controls; | ||
using Eto.GtkSharp.Forms; | ||
using SkiaSharp; | ||
using SkiaSharp.Views.Gtk; | ||
using System; | ||
|
||
namespace CelesteStudio.GTK; | ||
|
||
public class SkiaDrawableHandler : GtkPanel<Gtk.EventBox, SkiaDrawable, SkiaDrawable.ICallback>, SkiaDrawable.IHandler { | ||
private Gtk.Box content = null!; | ||
|
||
public void Create() { | ||
Control = new SkiaEventBox(Widget); | ||
Control.Events |= Gdk.EventMask.ExposureMask; | ||
Control.CanFocus = false; | ||
Control.CanDefault = true; | ||
Control.Events |= Gdk.EventMask.ButtonPressMask; | ||
|
||
content = new Gtk.Box(Gtk.Orientation.Vertical, 0); | ||
Control.Add(content); | ||
} | ||
|
||
private class SkiaEventBox(SkiaDrawable drawable) : Gtk.EventBox { | ||
private SKBitmap? bitmap; | ||
private SKSurface? surface; | ||
private ImageSurface? imageSurface; | ||
|
||
protected override bool OnDrawn(Context cr) { | ||
if (base.OnDrawn(cr)) { | ||
return true; | ||
} | ||
|
||
int drawWidth = drawable.DrawWidth, drawHeight = drawable.DrawHeight; | ||
if (drawWidth != bitmap?.Width || drawHeight != bitmap?.Height) { | ||
var colorType = SKImageInfo.PlatformColorType; | ||
|
||
bitmap?.Dispose(); | ||
bitmap = new SKBitmap(drawWidth, drawHeight, colorType, SKAlphaType.Premul); | ||
IntPtr pixels = bitmap.GetPixels(); | ||
|
||
surface?.Dispose(); | ||
surface = SKSurface.Create(new SKImageInfo(bitmap.Info.Width, bitmap.Info.Height, colorType, SKAlphaType.Premul), pixels, bitmap.Info.RowBytes); | ||
surface.Canvas.Flush(); | ||
|
||
imageSurface?.Dispose(); | ||
imageSurface = new ImageSurface(pixels, Format.Argb32, bitmap.Width, bitmap.Height, bitmap.Width * 4); | ||
} | ||
|
||
using (new SKAutoCanvasRestore(surface!.Canvas, true)) { | ||
drawable.Draw(surface); | ||
} | ||
|
||
imageSurface!.MarkDirty(); | ||
cr.SetSourceSurface(imageSurface, drawable.DrawX, drawable.DrawY); | ||
cr.Paint(); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public bool CanFocus { | ||
get => Control.CanFocus; | ||
set => Control.CanFocus = value; | ||
} | ||
|
||
protected override void SetContainerContent(Gtk.Widget containerContent) | ||
{ | ||
content.Add(containerContent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters