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.14k stars 522 forks source link

[BUG] On iOS, with .NET 9.0 Preview 2 and 3 and SkiaSharp 3.0 Preview 2.1 / 3.1, SKGLView corrupts SkiaSharp memory and stops SkiaSharp from functioning #2840

Open janne-hmp opened 4 weeks ago

janne-hmp commented 4 weeks ago

Description

My game GnollHack has several SKCanvasViews and a few SKGLViews, and they all work well in SkiaSharp 2.88.8 (Xamarin). However, on iOS in SkiaSharp 3.0 Preview 2.1 / 3.1 with .NET MAUI 9.0 Preview 2 or 3, SkiaSharp's memory gets corrupted, and it stops drawing the canvases / draws a black canvas only / draws garbage or partial images.

This seems to be a problem when SKGLViews are used in the app. When you change them to SKCanvasView, the problem seems to disappear.

Code

https://github.com/hyvanmielenpelit/GnollHack

Expected Behavior

Game and SKGLView works as in Xamarin 2.88.8 on iOS.

Actual Behavior

After a minute or so, especially after opening many pages and showing and hiding layouts, SkiaSharp's memory gets corrupted and it stops drawing or draws garbage when using SKGLView on iOS.

Version of SkiaSharp

3.x (Alpha)

Last Known Good Version of SkiaSharp

2.88.8 in Xamarin (Android works, too)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

iOS

Platform / Operating System Version

iOS 17.4.3

Devices

iPad 11,7

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

janne-hmp commented 4 weeks ago

This may be because there's (still) something wrong with .NET MAUI 9.0 Preview 3 iOS workload download, and perhaps it downloads (a non-functioning) Preview 4 prematurely instead of the correct workload version. @rolfbjarne Any update there?

janne-hmp commented 4 weeks ago

I tested this again today with a clean install, and I can confirm just changing to .NET 9.0 Preview 3 is sufficient to make SkiaSharp to stop working after a while on iOS, while working with SkiaSharp 3.0 Preview 2.1 (and other libraries exactly like in .NET 9.0 Preview 2).

janne-hmp commented 4 weeks ago

This seems to be an issue also in .NET 9.0 Preview 2, and not sure at all where this is coming from on iOS.

janne-hmp commented 4 weeks ago

I tested whether this is caused by special iOS features like animations etc., but none of them are the culprit. Happens in all combinations of .NET 9.0 Preview 2 / 3 and SkiaSharp 3.0 Preview 2.1 and 3.1. Always only on iOS. @mattleibow Any idea how to work out what is causing the problem?

It is always the SKGLView in the menu screen that stops working first (though it is on the same page--as an (in)visible grid--as the main game) and then SKCanvasView that draws the background for another page. And then finally the other SKGLViews and some SKCanvasViews. But this may be dependent on that these are the screens that I test and use the most.

janne-hmp commented 4 weeks ago

Touch events seem to still work, but all canvases are drawing just black in the end, apart from a a few small ones inside custom controls that still seem to work. Happens consistently after playing for a while. Memory consumption seems to be reasonable at 30MB, so that is not a problem. And happens also with default gpu cache size, so changing that is mostly likely not a problem. And also confirmed that Android does work fine.

janne-hmp commented 4 weeks ago

I changed now SKGLViews to SKCanvasViews and the problems seems to have been fixed. @mattleibow Can you investigate why SKGLView on iOS is corrupting the memory?

janne-hmp commented 4 weeks ago

I added one more observation here, which some luck could be interrelated this memory corruption problem: https://github.com/mono/SkiaSharp/issues/2842 ; SKGLView seems to hinder the speed of .NET layout, making the page with SKGLView look wobbly for the couple of first 100s of milliseconds. This has been a problem on iOS at least since 2.80.3, but I realized now for the first time that this is related to SKGLView being present on the page.