Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Android GL context loss  (Read 3892 times)

0 Members and 1 Guest are viewing this topic.

Fizix

  • Newbie
  • *
  • Posts: 18
    • View Profile
Android GL context loss
« on: April 15, 2015, 11:19:09 am »
It is well known that Android will "randomly" decide to kill the GL context of apps that is switched to the background or if your device goes to sleep, etc.  If that happens all your textures are lost when the context is lost.

Its hard to find a device to reproduce this problem consistently, so debugging/fixing is not easy.  We only hear about it from people using the app.

Does SFML recreate the GL context when it is lost?  The way to do it, according to Google, is when you try to do a eglSwapBuffers, and get an error, the error returned will be that one of the 3 parts of the context is lost.  Surface/Display/<Something else>.  So doing a proper shut down of everything and recreating when that happens seems like a simple thing to do.

The harder part is to reload all the textures that is lost, but this is not SFML's responsibility. There should however be some way to tell if the context was recreated or not at runtime so that the app can respond appropriately.

Some input would be appreciated.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Android GL context loss
« Reply #1 on: April 15, 2015, 11:43:38 am »
It is well known that Android will "randomly" decide to kill the GL context of apps that is switched to the background or if your device goes to sleep, etc.  If that happens all your textures are lost when the context is lost.
This is not random. Like I have mentioned before, this happens when the device decides it needs to flush the memory of its current contents in order to guarantee another application enough memory to function correctly. This depends on a number of factors, such as if other applications even request GL resources and how the subsystem chooses to compact everything. Logically, if nobody else requires resources, there would be no need to free up the space we take up. The point in time at which the system decides this should happen might be determined by some heuristic, but even that is deterministic. Computers are really bad at doing things "randomly", even when we want them to. What people perceive as random is often just something they don't fully understand, either because they lack the knowledge (e.g. proprietary software), or they don't know where to look for more information.

Does SFML recreate the GL context when it is lost? The way to do it, according to Google, is when you try to do a eglSwapBuffers, and get an error, the error returned will be that one of the 3 parts of the context is lost.  Surface/Display/<Something else>.
To my knowledge, no. And judging by the implementation, it doesn't seem to be the case. I am not the primary maintainer of the Android port, so this is all based on my brief inspection of the code.

So doing a proper shut down of everything and recreating when that happens seems like a simple thing to do.
This would be the only sane thing to do, yes.

The harder part is to reload all the textures that is lost, but this is not SFML's responsibility. There should however be some way to tell if the context was recreated or not at runtime so that the app can respond appropriately.
Your idea of leaving it up to the user to reload all their resources is indeed the simplest solution. Like you said, all that is needed for this is some way to notify them of this event (perhaps through sf::Event) and let them take corrective action on their own. The only other solution would be for SFML to store a client-side copy of every GL resource and re-upload it when the context is automatically recreated. This would mean a lot of space overhead, and just a general mess of code all over the place. It would also mean that we need a separate implementation of each GlResource class just for EGL platforms. This would become a nightmare to maintain as well.

Like I said, I'm not the primary maintainer of the mobile ports although I do work on the desktop GL implementation, so this is just my preference regarding how it should be done. Leave it to the user to sort out. It keeps the implementation simple and uniform across desktop/mobile and might even be what other libraries already do (I haven't looked around that much in this respect).
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Fizix

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Android GL context loss
« Reply #2 on: April 15, 2015, 12:16:21 pm »
Of course it is not random, hence the quotes. ;)

Keeping track of all the GL resources internally would not be a good idea.  I think it is just one of those things that users should be aware of when using textures on the Android platform and deal with it appropriately.

This would be a great opportunity for thrid party libraries, like thor, to include this in their custom resource managers perhaps.

I don't think adding a new event to handle this specific situation on this specific platform is part of the SFML way.  So using an existing event would be preferred I guess?  The focus change event seems like a good place to do it, but then we need some state to check to see if the context was recreated or not.  I think this is where the biggest questions come in as far as SFML development goes.

Can we put it in the ActivityState?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Android GL context loss
« Reply #3 on: April 15, 2015, 01:44:41 pm »
This would be a great opportunity for thrid party libraries, like thor, to include this in their custom resource managers perhaps.
Do you have something concrete in mind, from a user perspective? Can an external library really help here?

I don't think adding a new event to handle this specific situation on this specific platform is part of the SFML way.  So using an existing event would be preferred I guess?
I'm not sure about the semantics of the focus event on Android, but I assume it means something different (namely switching between applications, while leaving them paused). I don't think it makes any sense to recycle that event under that assumptions.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Fizix

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Android GL context loss
« Reply #4 on: April 15, 2015, 03:08:38 pm »
Do you have something concrete in mind, from a user perspective? Can an external library really help here?

Not really.  I went through the thor resource manager a few weeks ago and remembered it also kept track of the resources loaded.  Our own resource manager keeps track of the location where a texture was loaded from as well as the actual sf::Texture object.  So for us it is easy to just go through each one and reload them from disk in a single call.

This has some flaws, like not being able to recreate textures that were generated and stored in memory, other than of course keeping them stored in main memory.  But this isn't a requirement for our system.

 

anything