Created
February 3, 2024 20:23
-
-
Save mattleibow/7b1d07dba8588c23f0a376cc14545408 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Runtime.Versioning; | |
using CoreAnimation; | |
using Foundation; | |
using Microsoft.Maui.Handlers; | |
using SkiaSharp.Views.iOS; | |
using SkiaSharp.Views.Maui.Platform; | |
using UIKit; | |
namespace SkiaSharp.Views.Maui.Handlers | |
{ | |
public partial class SKGLViewHandler : ViewHandler<ISKGLView, SKMetalView> | |
{ | |
private SKSizeI lastCanvasSize; | |
private GRContext? lastGRContext; | |
private SKTouchHandler? touchHandler; | |
protected override SKMetalView CreatePlatformView() => | |
new MauiSKGLView | |
{ | |
BackgroundColor = UIColor.Clear, | |
Opaque = false, | |
}; | |
protected override void ConnectHandler(SKMetalView platformView) | |
{ | |
platformView.PaintSurface += OnPaintSurface; | |
base.ConnectHandler(platformView); | |
} | |
protected override void DisconnectHandler(SKMetalView platformView) | |
{ | |
touchHandler?.Detach(platformView); | |
touchHandler = null; | |
platformView.PaintSurface -= OnPaintSurface; | |
base.DisconnectHandler(platformView); | |
} | |
// Mapper actions / properties | |
public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args) | |
{ | |
handler.PlatformView.SetNeedsDisplay(); | |
} | |
public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view) | |
{ | |
if (handler.PlatformView is MauiSKGLView pv) | |
{ | |
pv.IgnorePixelScaling = view.IgnorePixelScaling; | |
pv.SetNeedsDisplay(); | |
} | |
} | |
public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view) | |
{ | |
handler.PlatformView.EnableSetNeedsDisplay = !view.HasRenderLoop; | |
} | |
public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view) | |
{ | |
handler.touchHandler ??= new SKTouchHandler( | |
args => view.OnTouch(args), | |
(x, y) => handler.OnGetScaledCoord(x, y)); | |
handler.touchHandler?.SetEnabled(handler.PlatformView, view.EnableTouchEvents); | |
} | |
// helper methods | |
private void OnPaintSurface(object? sender, iOS.SKPaintMetalSurfaceEventArgs e) | |
{ | |
var newCanvasSize = e.Info.Size; | |
if (lastCanvasSize != newCanvasSize) | |
{ | |
lastCanvasSize = newCanvasSize; | |
VirtualView?.OnCanvasSizeChanged(newCanvasSize); | |
} | |
if (sender is SKMetalView platformView) | |
{ | |
var newGRContext = platformView.GRContext; | |
if (lastGRContext != newGRContext) | |
{ | |
lastGRContext = newGRContext; | |
VirtualView?.OnGRContextChanged(newGRContext); | |
} | |
} | |
VirtualView?.OnPaintSurface(new SKPaintGLSurfaceEventArgs(e.Surface, e.BackendRenderTarget, e.Origin, e.Info, e.RawInfo)); | |
} | |
private SKPoint OnGetScaledCoord(double x, double y) | |
{ | |
if (VirtualView?.IgnorePixelScaling == false && PlatformView != null) | |
{ | |
var scale = PlatformView.ContentScaleFactor; | |
x *= scale; | |
y *= scale; | |
} | |
return new SKPoint((float)x, (float)y); | |
} | |
private class MauiSKGLView : SKMetalView | |
{ | |
public bool IgnorePixelScaling { get; set; } | |
protected override void OnPaintSurface(iOS.SKPaintMetalSurfaceEventArgs e) | |
{ | |
if (IgnorePixelScaling) | |
{ | |
var userVisibleSize = new SKSizeI((int)Bounds.Width, (int)Bounds.Height); | |
var canvas = e.Surface.Canvas; | |
canvas.Scale((float)ContentScaleFactor); | |
canvas.Save(); | |
e = new iOS.SKPaintMetalSurfaceEventArgs(e.Surface, e.BackendRenderTarget, e.Origin, e.Info.WithSize(userVisibleSize), e.Info); | |
} | |
base.OnPaintSurface(e); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment