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.56k stars 543 forks source link

[iOS, 3.0 Preview 3.1] SkiaSharp's memory becomes corrupted and it stops to function in .NET 9.0 Preview 6 #2840

Open janne-hmp opened 7 months ago

janne-hmp commented 7 months 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 3.1 with .NET MAUI 9.0 Preview 6, SkiaSharp's memory gets corrupted, and it stops drawing the canvases or draws a black canvas only after showing and hiding various layouts / grids many times.

The problem seems to be with just SKCanvasViews, but the problem emerges more quickly if both SKCanvasViews and SKGLViews are present on the page.

Code

https://github.com/hyvanmielenpelit/GnollHack

Expected Behavior

Game and SKCanvasView work as in Xamarin 2.88.8 on iOS without corrupting the memory.

Actual Behavior

After opening many pages and showing and hiding layouts / grids, SkiaSharp's memory gets corrupted and it stops drawing on iOS. It is enough to press Play Game and then Cancel many times. If you do this with SKGLViews activated, 1-3 Play Game presses corrupts the memory; with only SKCanvasViews, 4-10 presses are needed. (Pressing Play Game opens a modal GamePage on the top of MainPage, and then on GamePage the MenuGrid covering the whole page on the top of main canvasView element. There are lots of SKCanvasViews everywhere.)

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.5.1

Devices

iPad 11,7

Relevant Screenshots

No response

Relevant Log Output

No response

Code of Conduct

janne-hmp commented 7 months 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 7 months 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 7 months 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 7 months 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 7 months 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 7 months 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 7 months 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.

janne-hmp commented 6 months ago

@mattleibow With .NET MAUI 9.0 Preview 4, things seem to be now a bit more stable on iOS in SkiaSharp 3.0 Preview 3.1. I still managed to get the menu canvas to stop redrawing itself by doing a lot of random stuff in the UI first, but it does not seem to draw garbage anymore, just not redrawing itself. Also, the menu canvas was missing canvas.Flush() from the end of its PaintSurface, which I now added. This may have further contributed to stability, as I couldn't make the menu canvas stop working anymore with a short test, but this is subject to further testing naturally. Not sure if a missing Flush() could have caused things to malfunction with SKGLCanvas earlier?

janne-hmp commented 5 months ago

Maybe fixed with Preview 5 and Flush.

janne-hmp commented 4 months ago

This seems to be still problem in 9.0 Preview 6 in MAUI. Xamarin iOS with SKGLView works fine. Maui with SKCanvasView work fine, too.

We are getting in Output > Debug window the following error messages many tens of times:

INFO: 2024-07-10 21:36:53.790 GnollHackM[23250:6973420] Failed to bind EAGLDrawable: <CAEAGLLayer: 0x30125b9c0> to GL_RENDERBUFFER 1
2024-07-10 21:36:53.790 GnollHackM[23250:6973420] Failed to make complete multisample framebuffer object 8cd6
2024-07-10 21:36:53.790 GnollHackM[23250:6973420] Failed to make complete framebuffer object 8cd6

Practically what happens is that that if I start the game about 4 times by pressing Play Game button on MainPage (opens GamePage that has the only SKGLView in the app) and then selecting Cancel to return to MainPage on the next screen (and repeat this 4 times), most of the SkiaSharp canvases start consistently drawing black.

I tested for memory leaks using MemoryToolkit.Maui, but there seems to be none.

@mattleibow Any thoughts what could be a problem here?

janne-hmp commented 4 months ago

If it helps anything, there are also the following error messages:

INFO: 2024-07-10 21:36:17.108 GnollHackM[23250:6973420] You've implemented -[<UIApplicationDelegate> application:performFetchWithCompletionHandler:], but you still need to add "fetch" to the list of your supported UIBackgroundModes in your Info.plist.

INFO: 2024-07-10 21:36:40.373 GnollHackM[23250:6973420] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.
janne-hmp commented 4 months ago

@mattleibow If the SKGLView implementation is the same as in Xamarin, it feels like this might be a .NET MAUI composition engine problem (possibly a threading problem / missing BeginInvokeOnMainThread), since the SKGLView is not really drawing anything meaningful when pressing Play Game, and another grid with a few SKCanvasViews appears on the top of it. It is sufficient just to make that grid appear 1-4 times (press cancel and Play Game again), and the corruption materializes.

janne-hmp commented 4 months ago

@mattleibow I actually now got the corruption effect without using SKGLView's, i.e., just by having SKCanvasView's in the game. However, the corruption seems to happen more often with both SKGLView and SKCanvasView present. But it looks like the culprit is SKCanvasView rather than SKGLView. Perhaps this problem is related to #2923.

janne-hmp commented 4 months ago

I can also confirm that the memory leak happens only when opening and closing pages, and not by showing and hiding grids via IsVisible, which makes sense, since a new page is generally created along with new controls before PushModalAsync.

janne-hmp commented 1 month ago

This is the iOS memory leak problem that hopefully is already fixed, pending new SkiaSharp release. I just tested this in .NET MAUI 9.0 RC2, and the problem still persists there with SkiaSharp 3.0 Preview 4.1, so a new SkiaSharp version is indeed needed. @mattleibow Do we have any indications currently what might be the timeframe for releasing the fixed version?

janne-hmp commented 1 month ago

@mattleibow I tried the game now on iOS with SkiaSharp 3.0 Preview 5.4, and the memory leak problem seems to persist, just as before.