Skip to content

Commit cbfdee6

Browse files
authored
Possible fix for ANR #2 (#6172)
* Possible fix for ANR #2 * typo fixes * Exit the main loop * change place where we call `androidDestroyWindow();` * add missed include * Revert "add missed include" This reverts commit ea8f07c. * Revert "change place where we call `androidDestroyWindow();`" This reverts commit dc12019. * immediate app exit when exit requested * Added comments
1 parent 8fa8ef0 commit cbfdee6

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

engine/dlib/src/dmsdk/dlib/thread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace dmThread
106106
* Detach thread. When a detached thread terminates, its resources are
107107
* automatically released back to the system without the need for another
108108
* thread to join with the terminated thread.
109-
* @name dmThread::Join
109+
* @name dmThread::Detach
110110
* @param thread Thread to detach
111111
*/
112112
void Detach(Thread thread);

engine/engine/src/engine_main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ int engine_main(int argc, char *argv[])
117117
{
118118
glfwAndroidPollEvents();
119119
dmTime::Sleep(0);
120+
if (g_AndroidApp->destroyRequested) {
121+
// App requested exit. It doesn't wait when thread work finished because app is in background already.
122+
// App will never end up here from within the app itself, only using OS functions.
123+
return 0;
124+
}
120125
}
121126
dmThread::Join(t);
122127

engine/glfw/lib/android/android_init.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,7 @@ void _glfwAndroidHandleCommand(struct android_app* app, int32_t cmd) {
296296
computeIconifiedState();
297297
break;
298298
case APP_CMD_DESTROY:
299-
_glfwWin.opened = 0;
300-
final_gl(&_glfwWinAndroid);
301-
computeIconifiedState();
299+
androidDestroyWindow();
302300
break;
303301
}
304302
}

engine/glfw/lib/android/android_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ void* acquire_gl_aux_context(_GLFWwin_android* win);
6767
void unacquire_gl_aux_context(_GLFWwin_android* win);
6868

6969
void computeIconifiedState();
70+
void androidDestroyWindow();
7071

7172
void _glfwAndroidHandleCommand(struct android_app* app, int32_t cmd);
7273
int32_t _glfwAndroidHandleInput(struct android_app* app, JNIEnv* env, struct InputEvent* event);
7374

75+
7476
// Should only called after an error
7577
// returns 1 if we the window/surface was ok
7678
// returns 0 if we the window/surface was bad

engine/glfw/lib/android/android_window.c

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,15 @@ void glfwAndroidFlushEvents()
418418
spinlock_unlock(&g_EventLock);
419419
}
420420

421+
void androidDestroyWindow( void )
422+
{
423+
if (_glfwWin.opened) {
424+
_glfwWin.opened = 0;
425+
final_gl(&_glfwWinAndroid);
426+
computeIconifiedState();
427+
}
428+
}
429+
421430
// Called from the engine thread
422431
void _glfwPlatformPollEvents( void )
423432
{
@@ -438,22 +447,28 @@ void _glfwPlatformPollEvents( void )
438447
// Called from the looper thread
439448
void glfwAndroidPollEvents()
440449
{
441-
int ident;
442-
int events;
443-
struct android_poll_source* source;
444-
445-
int timeoutMillis = 0;
446-
if (_glfwWin.iconified) {
447-
timeoutMillis = 1000 * 3;
448-
}
449-
while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0)
450-
{
451-
timeoutMillis = 0;
452-
// Process this event.
453-
if (source != NULL) {
454-
source->process(_glfwWinAndroid.app, source);
455-
}
456-
}
450+
int ident;
451+
int events;
452+
struct android_poll_source* source;
453+
454+
int timeoutMillis = 0;
455+
if (_glfwWin.iconified) {
456+
timeoutMillis = 1000 * 2;
457+
}
458+
while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0)
459+
{
460+
timeoutMillis = 0;
461+
// Process this event.
462+
if (source != NULL) {
463+
source->process(_glfwWinAndroid.app, source);
464+
}
465+
466+
if (_glfwWinAndroid.app->destroyRequested) {
467+
androidDestroyWindow();
468+
// OS is destroyng the app. All the other events doesn't matter in this case.
469+
return;
470+
}
471+
}
457472
}
458473

459474
//========================================================================

0 commit comments

Comments
 (0)