Skip to content

Commit

Permalink
fix(Studio): Game Info rendering on WPF
Browse files Browse the repository at this point in the history
  • Loading branch information
psyGamer committed Jan 26, 2025
1 parent 17f534f commit d146611
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions Studio/CelesteStudio.WPF/SkiaDrawableHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,27 @@ protected override void OnRender(DrawingContext drawingContext) {
int width = (int)(drawable.DrawWidth * dpiX);
int height = (int)(drawable.DrawHeight * dpiY);

if (width == 0 || height == 0) {
return;
}

if (drawable.CanDraw) {
if (bitmap == null || surface == null || width != bitmap.PixelWidth || height != bitmap.PixelHeight || Settings.Instance.WPFSkiaHack) {
const double bitmapDpi = 96.0;
bitmap = new WriteableBitmap(width, height, bitmapDpi * dpiX, bitmapDpi * dpiY, PixelFormats.Pbgra32, null);

surface?.Dispose();
surface = SKSurface.Create(new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul), bitmap.BackBuffer, bitmap.BackBufferStride, new SKSurfaceProperties(SKPixelGeometry.Unknown));
surface.Canvas.Scale((float)dpiX, (float)dpiY);
surface.Canvas.Save();
if (width == 0 || height == 0) {
// A zero sized surface causes issues, so use a null 1x1
// drawable.Draw() still needs to be called, so simply skipping render is not an option
bitmap = null;

surface?.Dispose();
surface = SKSurface.CreateNull(1, 1);
} else {
const double bitmapDpi = 96.0;
bitmap = new WriteableBitmap(width, height, bitmapDpi * dpiX, bitmapDpi * dpiY, PixelFormats.Pbgra32, null);

surface?.Dispose();
surface = SKSurface.Create(new SKImageInfo(width, height, SKImageInfo.PlatformColorType, SKAlphaType.Premul), bitmap.BackBuffer, bitmap.BackBufferStride, new SKSurfaceProperties(SKPixelGeometry.Unknown));
surface.Canvas.Scale((float)dpiX, (float)dpiY);
surface.Canvas.Save();
}
}

bitmap.Lock();
bitmap?.Lock();

var canvas = surface.Canvas;
using (new SKAutoCanvasRestore(surface.Canvas, true)) {
Expand All @@ -54,9 +59,11 @@ protected override void OnRender(DrawingContext drawingContext) {
}
canvas.Flush();

bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
drawingContext.DrawImage(bitmap, new Rect(drawable.DrawX, drawable.DrawY, width / dpiX, height / dpiY));
bitmap.Unlock();
if (bitmap != null) {
bitmap.AddDirtyRect(new Int32Rect(0, 0, width, height));
drawingContext.DrawImage(bitmap, new Rect(drawable.DrawX, drawable.DrawY, width / dpiX, height / dpiY));
bitmap.Unlock();
}
} else {
drawable.Invalidate();
}
Expand Down

0 comments on commit d146611

Please sign in to comment.