mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.35k stars 532 forks source link

SKMetalView unexpectedly draws on a timer #2762

Open charlesroddie opened 6 months ago

charlesroddie commented 6 months ago

Description

An SKMetalView takes the Apple native default of "Timed updates". This draws on a timer and is intended for 3D games. This is not suitable for SkiaSharp, which is not a 3D graphics library and therefore is mainly used by apps not 3D games

The appropriate mode for SkiaSharp is "Draw notifications", which the user needs to manually enable by setting

base.Paused = true;
base.EnableSetNeedsDisplay = true;

We were getting out of memory crashes that were very hard to detect and diagnose which went away when setting the expected mode in apps.

I suggest either the default is changed, or documentation needs to be added on the SkiaSharp side indicating that the default needs to be changed by end users.

Expected Behavior

https://developer.apple.com/documentation/metalkit/mtkview#Configuring-the-Drawing-Behavior Draw notifications: The view redraws itself when something invalidates its contents, usually because of a call to setNeedsDisplay() or some other view-related behavior. In this case, set isPaused and enableSetNeedsDisplay to true. Use this mode for apps with a more traditional workflow, where updates happen when data changes, but not on a regular timed interval.

Actual Behavior

https://developer.apple.com/documentation/metalkit/mtkview#Configuring-the-Drawing-Behavior Timed updates: The view redraws its contents based on an internal timer. In this case, which is the default behavior, both isPaused and enableSetNeedsDisplay are set to false. Use this mode for games and other animated content that’s regularly updated.

Version of SkiaSharp

2.88.3 (Current)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio (macOS)

Platform / Operating System

iOS, macOS

Platform / Operating System Version

IOS MACOS

Devices

Crashes seen on iPhone, iPad

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

mattleibow commented 6 months ago

For the platform views, I try not to change defaults. If this default is absolutely the worst option, maybe we can. But, since you are using the native view, I just kept the native values.

It will be more interesting to find the real issue causing the crash because there are several libraries using the accelerated views for rendering content: such as maps or animations.

For the Maui views, I do have an option to disable this behaviour: HasRenderLoop