AndroidGameView.Pause without losing GL context

ArandaAranda Aranda MorrisonUSMember

Currently it always loses GL context when paused, but it's not necessary to do so in all cases. Some gpus will support multiple EGL contexts.

I'm part of the MonoGame team and have implemented reloading of textures on resume. This works but it's still a very poor user experience. The larger the game the longer it takes to resume and even showing a fullscreen ad causes the context to be lost and forces a long resume.

As far as I can tell, AndroidGameView plays a similar role to Android's built in GLSurfaceView. This class has the same problem. Newer SDKs offer 'setPreserveEGLContextOnPause' but people have fixed (or worked around) it in older SDKs by customising this class:

http://stackoverflow.com/questions/2112768/prevent-onpause-from-trashing-opengl-context/11167948#11167948

Can someone from Xamarin please take a look at whether doing something like this would be an option for AndroidGameView? Currently it seems that we are stuck with the reloading textures even on the latest SDKs.

Aranda

Best Answers

  • DeanEllisDeanEllis Dean Ellis US Xamurai
    Answer ✓

    Hi Aranda

    OK, I've done some rework on the AndroidGameView, one of the issues was that when ever the surface was destroyed the context was as well. I've reworked the code a bit so that it only destroys the eglSurface when the SurfaceDestroyed (ISurfaceHolder holder) callback is called. It will now hang on to the Context Object. Now when we then get a surfaceCreate we create a new surface and then attempt to use the previous context, if we get a ContextLost error we create a new one.

    I am also adding a OnContextLost virtual method as well as a ContextLost event to the game view so people can hook into when this happens.

    I have your email so I'll sent you a test package , let me know how it goes

    Dean

«1

Answers

  • ArandaAranda Aranda Morrison USMember

    Just wanted to bump this thread and also point out a couple of things:

    • Many games handle the switch to OS and back to the game without pause to reload textures (just try Angry Birds). Be a shame if AndroidGameView can't do it.
    • ExEn framework manages this too. Instead of AndroidGameView it's handling it's own surface and context lifetime using the Javax.Microedition.Khronos.Egl namespace. The crux being that when the surface is destroyed the context is not necessarily lost.
  • ArandaAranda Aranda Morrison USMember

    Perhaps we could look at handling our own surface and context lifetime for a future release?

    I've had a look at doing this but it's not obvious where to start.

  • ArandaAranda Aranda Morrison USMember

    ExEn's implementation uses Javax.Microedition.Khronos.Egl, which only has EGL1. I can't find an equivalent for EGl2 and it's not clear how to use OpenTK's bindings in a similar way. Thankfully Dean has said he'll investigate the internals of AndroidGameView.

  • ArandaAranda Aranda Morrison USMember

    Hi Dean,

    Thanks for taking a look. I'll gladly test out any packages you want to send through.

  • StephenWilliamsStephenWilliams Stephen Williams AUMember, Beta

    Excellent work, @Dean.

  • ArandaAranda Aranda Morrison USMember

    @dellis has now sent me through two samples (with old and new MfA code) and I've confirmed that he's fixed the issue on my Galaxy S2. Excellent work indeed!

  • DeanEllisDeanEllis Dean Ellis USMember, Xamarin Team Xamurai

    Hi

    Here are the links to the 2 test applications.

    1) The old version which should demonstrate the issue. After pressing the Home button, the textures will be white.
    https://dl.dropbox.com/u/18881050/mono.samples.texturedcube.apk

    2) The new version with the fix
    https://dl.dropbox.com/u/18881050/mono.samples.texturedcubenew.apk

    Can I ask if you can test the apps and report back the results please (along with some device info like device, os version). I only have 2 android devices and would like to get this tested on as many as possible to ensure I haven't broken anything.

    Dean

  • ArandaAranda Aranda Morrison USMember

    All devices tested have behaved as expected for both apks (old one shows white cube after resume and new one has correct texture):

    • Samsung Galaxy S2, Android 4.0.4
    • HTC Desire, Android 2.3.7
    • Zenithink ZT ICS (tablet), Android 4.0.3
  • DeanEllisDeanEllis Dean Ellis USMember, Xamarin Team Xamurai

    Works for me on

    • Samsung Galaxy S, Android 2.3.6
    • Kindle Fire 7" HD, Android 4.0
  • jacobandersonjacobanderson jacob anderson USMember
    edited January 2013

    Samsung Galaxy Player 5 YP-G70 - does not demonstrate this bug using the control APK. This device is running 2.3.6.

    Correction. If I leave the device in portrait and go to the lock screen and then return, the textures are fine.

    If I launch, then move to landscape, then press the power button to enter the lock screen, then resume, that's when I see the white cube (fail condition).

    Using the "new" APK, start the app in portrait, switch to landscape (have to interact with the scene to switch orientation), click power button, return to portrait and unlock screen, in landscape mode the cube appears with textures.

  • jacobandersonjacobanderson jacob anderson USMember
    edited January 2013

    Acer Iconia A500 with lock sceen set to swipe. Does not demonstrate the fail condition using the control APK. This device has a portrait and landscape mode lock screen. This device is running 4.0.3.

  • jacobandersonjacobanderson jacob anderson USMember
    edited January 2013

    Samsung Galaxy Tab 2 GT-P3113 updated today to 4.0.4 does not demonstrate the failure condition using the control APK. This device has a portrait and landscape mode lock screen. Note the "new" APK also works fine on this device.

  • jacobandersonjacobanderson jacob anderson USMember

    SONY XPERIA LT26i running 4.0.4 does not demonstrate the failure using the control APK. I tried several times in landscape mode for the app, then switch to portrait mode to unlock and back to landscape, never did the textures disappear to white. This device does NOT have a dual orientation lock screen.

  • ArandaAranda Aranda Morrison USMember

    @jacobanderson bizarre that none of your devices show the problem. Just to be sure, are you pressing Home button to reproduce this? I don't think just locking the device or changing orientation will exhibit the problem.

  • StephenWilliamsStephenWilliams Stephen Williams AUMember, Beta
    edited January 2013

    Nexus 7 (Android 4.2.1): Both APKs work as expected.

    Nexus S (Android 4.1.2): Both APKs work as expected.

  • jacobandersonjacobanderson jacob anderson USMember

    hey Aranda. If I just press the home button then both APKs work well.

  • ArandaAranda Aranda Morrison USMember

    hey Aranda. If I just press the home button then both APKs work well.

    Right. So it would seem you have some nice devices that manage to not lose context most of the time. I also just read your correction about the Samsung Galaxy Player 5 YP-G70, so you can reproduce the issue, and fix, on at least one device.

    It seems pretty conclusive so far. Looks like you fixed it Dean... great work! Now to get it integrated with MonoGame :)

  • ArandaAranda Aranda Morrison USMember

    Bump!

    Just wondering if there's been any movement? Seems like @dellis had a fix, so it'd be great if Xamarin could give some priority to this issue.

  • MikeDussault.8139MikeDussault.8139 Mike Dussault USMember

    Anyone? Bueller? This is a pretty severe shortcoming of Xamarin (and apps built using it). Someone not using Xamarin can just use GLSurfaceView.setPreserveEGLContextOnPause, but Xamarin encourages/forces apps to use AndroidGameView, but that doesn't derive from GLSurfaceView so we can't use setPreserveEGLContextOnPause.

  • ArandaAranda Aranda Morrison USMember

    @MikeDussault.8139 agreed. It's nearly been a year since I reported this problem. @dellis are you able to raise it internally again? If fixing AndroidGameView is viewed as a potentially breaking change, perhaps Xamarin could provide a sample showing how to roll our own game view and use GLSurfaceView.setPreserveEGLContextOnPause?

  • DeanEllisDeanEllis Dean Ellis USMember, Xamarin Team Xamurai

    @Aranda I have moved teams since this was looked at, I passed on my findings and code to the OpenTK maintainer within Xamarin to get it into a release. There is some work going on to update the mono/opentk stuff and I believe the android changes including the AndoridGameView are going to be open sourced (see http://forums.xamarin.com/discussion/comment/28872/#Comment_28872). I don't have any time scales on this, I will bump internally again and see what happens.

  • KakCATKakCAT Mr X ESMember

    Hello,

    any updates on this issue? I can't port any game from iOS to Android without tons of work due to this problem.

  • ArandaAranda Aranda Morrison USMember

    I just got an update from Bugzilla which says this is now fixed and may be in the next alpha. Yay!

    https://bugzilla.xamarin.com/show_bug.cgi?id=9494#c4

  • MrHelmutMrHelmut Thomas Altenburger FRMember

    Hello,

    I would like to know if there is any chance that this fix lands on a public channel anytime soon?
    In some situation, it can be quite severe. It affects anything that switch to new activities, like:
    - social media integration (twitter/facebook)
    - interstitial ads (admob)
    - in-app billing

    We are getting a lot of bad reviews on the Play Store with comments about this issue and a much lower conversion rate compared to other platforms.

    Could we please have an estimated time of deployment? It would greatly help us to know how to proceed with our upcoming game due next week.

    Thanks for the support.

  • IanWeeksIanWeeks Ian Weeks USMember

    Has anything been done about this recently? I'm using Monogame to develop an Android game and there's no way I could release the game in this state - switching between apps and returning to the game takes several seconds which is an extremely poor experience for the user. I would delete the game from my phone immediately if I experienced this.

    I fail to see how lots of other Android games manage this sort of switching and returning to the game instantly and we can't.

    Any update as to how to work around the problem or when this supposed fix is going to be available will be greatly appreciated!

  • ArandaAranda Aranda Morrison USMember

    @DeanEllis‌ can you please follow this up internally again? There was a post in the associated Bugzilla saying it was fixed (https://bugzilla.xamarin.com/show_bug.cgi?id=9494#c4), but I've not had any reports of it appearing in a beta build yet.

«1
Sign In or Register to comment.