Skip to content

Commit

Permalink
Possible fix for ANR #2 (#6172)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
AGulev committed Nov 26, 2021
1 parent 8fa8ef0 commit cbfdee6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion engine/dlib/src/dmsdk/dlib/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace dmThread
* Detach thread. When a detached thread terminates, its resources are
* automatically released back to the system without the need for another
* thread to join with the terminated thread.
* @name dmThread::Join
* @name dmThread::Detach
* @param thread Thread to detach
*/
void Detach(Thread thread);
Expand Down
5 changes: 5 additions & 0 deletions engine/engine/src/engine_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ int engine_main(int argc, char *argv[])
{
glfwAndroidPollEvents();
dmTime::Sleep(0);
if (g_AndroidApp->destroyRequested) {
// App requested exit. It doesn't wait when thread work finished because app is in background already.
// App will never end up here from within the app itself, only using OS functions.
return 0;
}
}
dmThread::Join(t);

Expand Down
4 changes: 1 addition & 3 deletions engine/glfw/lib/android/android_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,7 @@ void _glfwAndroidHandleCommand(struct android_app* app, int32_t cmd) {
computeIconifiedState();
break;
case APP_CMD_DESTROY:
_glfwWin.opened = 0;
final_gl(&_glfwWinAndroid);
computeIconifiedState();
androidDestroyWindow();
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions engine/glfw/lib/android/android_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ void* acquire_gl_aux_context(_GLFWwin_android* win);
void unacquire_gl_aux_context(_GLFWwin_android* win);

void computeIconifiedState();
void androidDestroyWindow();

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


// Should only called after an error
// returns 1 if we the window/surface was ok
// returns 0 if we the window/surface was bad
Expand Down
47 changes: 31 additions & 16 deletions engine/glfw/lib/android/android_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ void glfwAndroidFlushEvents()
spinlock_unlock(&g_EventLock);
}

void androidDestroyWindow( void )
{
if (_glfwWin.opened) {
_glfwWin.opened = 0;
final_gl(&_glfwWinAndroid);
computeIconifiedState();
}
}

// Called from the engine thread
void _glfwPlatformPollEvents( void )
{
Expand All @@ -438,22 +447,28 @@ void _glfwPlatformPollEvents( void )
// Called from the looper thread
void glfwAndroidPollEvents()
{
int ident;
int events;
struct android_poll_source* source;

int timeoutMillis = 0;
if (_glfwWin.iconified) {
timeoutMillis = 1000 * 3;
}
while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0)
{
timeoutMillis = 0;
// Process this event.
if (source != NULL) {
source->process(_glfwWinAndroid.app, source);
}
}
int ident;
int events;
struct android_poll_source* source;

int timeoutMillis = 0;
if (_glfwWin.iconified) {
timeoutMillis = 1000 * 2;
}
while ((ident=ALooper_pollAll(timeoutMillis, NULL, &events, (void**)&source)) >= 0)
{
timeoutMillis = 0;
// Process this event.
if (source != NULL) {
source->process(_glfwWinAndroid.app, source);
}

if (_glfwWinAndroid.app->destroyRequested) {
androidDestroyWindow();
// OS is destroyng the app. All the other events doesn't matter in this case.
return;
}
}
}

//========================================================================
Expand Down

0 comments on commit cbfdee6

Please sign in to comment.