Texture2D.FromStream causing Hang on OSX

I have an issue on OSX where the entire game hangs for upwards of 2 minutes on first launch when I call the following code:

using (var fileStream = new FileStream("Content/img.png", FileMode.Open))
{
      var LevelStartImage = Texture2D.FromStream(GraphicsDevice, fileStream);
}

I’m loading user-created levels which come with a screenshot which is why I’m not using the ContentManager. Calling ‘Texture2D.FromStream’ calls System.Drawing, which when it’s first called on a fresh install, is causing the entire game to hang for upwards of 2 minutes.

I’ve narrowed it down to a font-cache being built during that first launch when System.Drawing is called. This has happened on DesktopGL, Xamarin.Mac and Mac Classic builds.

I’ve had users complain and note that they thought the game had frozen. While this is a Mono bug, it’s arrising from a call in MonoGame (Texture2D.FromStream(…)) and so I’m wondering if others here have had/found a work around/solution to this issue. I can add a notification to the user saying “building cache, please wait…” but this is an absolute last-resort.

I don’t really know anything about this platform yet… but a couple of thoughts I figure I’ll share in hopes it helps!

  1. Is it only during first launch? Can you test to see if starting this texture load process incurs the same delay at any other points in your game, perhaps dynamically loading it just before it needs to be shown?

  2. Can you circumvent the Texture2D.FromStream entirely by loading the data directly from the file into an array, then using Texture2D.SetData?

  3. Out of curiosity, any issues with general file access on iOS? For example, can you read the contents of a text file (of some appropriate size) without incurring any delay at roughly the same point in your code?

Anyway, I dunno if that helps… just some things that occurred to me.

Hi Trinith,

So it’s only on Mac OSX.

  1. It only happens on first run as the font cache isn’t created yet. Whenever the game runs, System.Drawing must be checking for this font cache I mentioned, and if it’s not present, creates it. I’ve found the files it creates, and if I remove them then it does the same 2-minute hang, but if I put them back, then there’s no hang.

  2. I’m exploring that option. the issue is though I’ll need the width and height for the Texture2D constructor, and this isn’t always the same.

  3. This is the only issue I’ve had with Mac OSX.

I don’t really have much to add on 1 and 3, but on 2…

Only thing I can think of here would be to download some pre-built component that reads in PNG files. Actually, for a quick and dirty test, does Xamarin provide System.Drawing.Bitmap? Either way, reading the file “manually” would allow you to get the dimensions as well.

I’d need to find a library which doesn’t use System.Drawing. Its when a function from this library is called is when the hang occurs.

I’ll definitely try to look for an alternate lib.

I can’t have been the only one who has ran into this issue, right?!

Try to use TitleContainer.OpenStream This works on Windows and Android on my case not sure if it will work on OSX too ^_^Y.

using (var fileStream =  TitleContainer.OpenStream("Content/img.png") )
{
      var LevelStartImage = Texture2D.FromStream(GraphicsDevice, fileStream);
      fileStream.Close();
}

Hi @DexterZ, Thanks I hadn’t considered it but unfortunately it’s the same result, the issue seems to be from the Texture2D.FromStream call.

What I’ve done in the meantime is to copy over some font cache files that I’ve shipped with the game that potentially could solve the problem, and then have a ‘test’ Texture2D.FromStream load run on a background worker thread during launch, so that even if the copied font cache files don’t solve the problem, then the proper files will be generated during that first FromStream call. I notify the users that this is a ‘First Launch Set Up’ so that they don’t just think the game’s frozen or stalled out.

It’s strange though that I’m the only one who seems to have run into this issue.

1 Like

Yo Bro! Glad you find some work around… I also experienced this hang time loading from Texture2D.FromStream on Android not sure if it also related to OSX … the file path and name should be case sensitive .