-
-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only pause GLSurfaceView if activity is moved to the background, and not if it just loses focus #1942
Conversation
What about this: axmol/core/platform/android/jni/Java_org_axmol_lib_AxmolRenderer.cpp Lines 56 to 71 in 59592d3
I think firstTime hack should be removed and better be managed in Java's side, since it's responsible for managing the app's state.
Also I think |
|
Good point. I'll go through the code to see what that and the background event are used for, and update it accordingly. |
Yes, the calls in |
…pp start Remove work-around in nativeOnResume to avoid calling applicationWillEnterForeground twice since it is no longer required Remove redundant setRenderMode calls in AxmolGLSurfaceView
I've tested this out, and as far as I can tell, it's working well. If anyone else could possibly test it out as well then that would be great. |
I'll try to find some time for testing tomorrow. |
|
|
Looking into this now.
I don't see this at all. They are definitely not recreated, so I am a little confused as to why you're seeing that. Add a breakpoint to Now, move the app to the background, then bring it back to the foreground, and since the context is lost,
Out of curiosity, is Also, are you forcing the context loss via |
@smilediver I cannot reproduce what you are seeing. Using logging calls, this is the output from the steps you mentioned:
To force the context loss, I've set this to false |
So with these settings moving app to background doesn't destroy GL context. To destroy the GL context I change the font size in the Android's settings. It seems this is also related to this: https://developer.android.com/guide/topics/resources/runtime-changes, where some Android events can relaunch the activity. Maybe there are two different situations: one, where only GL context is lost; another when GL context is lost and activity is relaunched (possibly in these cases GL context is lost due to activity being relaunched). |
Yes, that's most likely the case, and since the activity is completely restarted, it isn't actually coming from the background to the foreground; it's like a cold-start. Is the entire application process killed though, or just the activity? |
Well, it's not a cold start, there's some warmness in there... :D No, the app is not killed, just the activity is restarted and GL context is lost. The fix probably just needs to use static variables instead of members to store the state of the native side. |
Cocos2d-x never handled this situation either, so this is something completely new. I'll take a look at this scenario in a day or so, because it may impact more than just the EGL context related code. For the time being, do the changes in this PR address the most common scenario, which is an EGL context loss (not an activity restart)? That's what this PR is about, so perhaps it's better to verify for this specific scenario, and deal with the other scenario in a separate PR. |
With |
With If set to true, then the EGL context may be preserved when the GLSurfaceView is paused. Prior to API level 11, whether the EGL context is actually preserved or not depends upon whether the Android device can support an arbitrary number of EGL contexts or not. Devices that can only support a limited number of EGL contexts must release the EGL context in order to allow multiple applications to share the GPU. If set to false, the EGL context will be released when the GLSurfaceView is paused, and recreated when the GLSurfaceView is resumed. So, having it as |
I'm aware about guarantees, just saying that I don't know how to reproduce only GL context loss with |
Setting to |
Describe your changes
At the moment, there are several issues on Android:
1 - There are instances where
applicationWillEnterForeground()
is called twice. This should also not be called on initial app start-up, since the behaviour would be different to other platforms.2 - The
GLSurfaceView
should not be paused if the activity has simply lost focus, but still visible to the user. According to the Android documentation, the recommendation is that it only be paused if the application enters the background, meaning inActivity.onStop
.3 - If the
GLSurfaceView
is paused, there is a possibility that Android will discard the EGL context under certain conditions. If the application has simply lost focus and is still visible to the user, the view should remain valid.4 -
Application::applicationDidEnterBackground()
is also called fromGLSurfaceView.onPause()
, but this should not occur if the application simply lost focus and is still visible.3 -
Activity.onPause()
should not take too much time complete, and the current implementation which callsGLSurfaceView.onPause()
fromActivity.onPause()
, and queues a call toApplication::applicationDidEnterBackground()
on the GL thread. There is no guarantee that the game state can be saved before the application goes into the background if this is a time-intensive process.The changes in this PR should resolve all the issues listed above.
1 -
applicationWillEnterForeground()
should not be called on initial app start-up.2 - If the application activity loses focus, then the
GLSurfaceView
should not be paused, but simply put into a more efficient rendering mode, usingRENDERMODE_WHEN_DIRTY
instead ofRENDERMODE_CONTINUOUSLY
.3 - Android will not detect the
GLSurfaceView
as being paused if it is not yet in the background, which means there is no reason for the EGL context to be discarded.4 - Now that
GLSurfaceView.onPause()
is called fromActivity.onStop()
, then there are no longer any issues with time-intensive processing of saving game state inApplication::applicationDidEnterBackground()
.Issue ticket number and link
Related to #1211
Checklist before requesting a review
For each PR
Add Copyright if it missed:
-
"Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md)."
I have performed a self-review of my code.
Optional:
For core/new feature PR