-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Metal as a backend on Mac Catalyst
- Loading branch information
1 parent
9bb8b23
commit 4ad9a1f
Showing
5 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
120 changes: 114 additions & 6 deletions
120
...arp.Views.Maui/SkiaSharp.Views.Maui.Core/Handlers/SKGLView/SKGLViewHandler.MacCatalyst.cs
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 |
---|---|---|
@@ -1,19 +1,127 @@ | ||
using System; | ||
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, UIView> | ||
public partial class SKGLViewHandler : ViewHandler<ISKGLView, SKMetalView> | ||
{ | ||
protected override UIView CreatePlatformView() => throw new PlatformNotSupportedException("OpenGL-based views (such as SKGLView) are not supported on Mac Catalyst. Instead, use Metal-based views."); | ||
private SKSizeI lastCanvasSize; | ||
private GRContext? lastGRContext; | ||
private SKTouchHandler? touchHandler; | ||
|
||
public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view) { } | ||
protected override SKMetalView CreatePlatformView() => | ||
new MauiSKMetalView | ||
{ | ||
BackgroundColor = UIColor.Clear, | ||
Opaque = false, | ||
}; | ||
|
||
public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view) { } | ||
protected override void ConnectHandler(SKMetalView platformView) | ||
{ | ||
platformView.PaintSurface += OnPaintSurface; | ||
|
||
public static void MapEnableTouchEvents(SKGLViewHandler handler, ISKGLView view) { } | ||
base.ConnectHandler(platformView); | ||
} | ||
|
||
public static void OnInvalidateSurface(SKGLViewHandler handler, ISKGLView view, object? args) { } | ||
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) | ||
{ | ||
if (handler.PlatformView.Paused && handler.PlatformView.EnableSetNeedsDisplay) | ||
handler.PlatformView.SetNeedsDisplay(); | ||
} | ||
|
||
public static void MapIgnorePixelScaling(SKGLViewHandler handler, ISKGLView view) | ||
{ | ||
if (handler.PlatformView is MauiSKMetalView pv) | ||
{ | ||
pv.IgnorePixelScaling = view.IgnorePixelScaling; | ||
handler.PlatformView.SetNeedsDisplay(); | ||
} | ||
} | ||
|
||
public static void MapHasRenderLoop(SKGLViewHandler handler, ISKGLView view) | ||
{ | ||
handler.PlatformView.Paused = !view.HasRenderLoop; | ||
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 MauiSKMetalView : 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); | ||
} | ||
} | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
source/SkiaSharp.Views/SkiaSharp.Views.Shared/GlesInterop/Gles.cs
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