From 5206c25619153a6d804599dd42e71cccb87e0f6e Mon Sep 17 00:00:00 2001 From: Hugues Evrard Date: Mon, 11 Mar 2019 10:03:39 +0100 Subject: [PATCH] Fix GAPIR Android app termination (#2657) After calling ANativeActivity_finish(), the native app should keep on polling events and only terminate when app->destroyRequested becomes true. This ensure a proper termination of the GAPIR app on Android. --- cmd/gapir/cc/main.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/cmd/gapir/cc/main.cpp b/cmd/gapir/cc/main.cpp index 26a02f320a..0b45282c48 100644 --- a/cmd/gapir/cc/main.cpp +++ b/cmd/gapir/cc/main.cpp @@ -374,31 +374,46 @@ void android_main(struct android_app* app) { app->onAppCmd = android_process; + bool finishing = false; bool alive = true; while (alive) { int fdesc; int events; + const int timeoutMilliseconds = 1000; struct android_poll_source* source; - while (ALooper_pollAll(1000, &fdesc, &events, (void**)&source) >= 0) { + while (ALooper_pollAll(timeoutMilliseconds, &fdesc, &events, + (void**)&source) >= 0) { // process this event if (source) { source->process(app, source); } if (app->destroyRequested) { - unlink(socket_file_path.c_str()); + // Clean up and exit the main loop server->shutdown(); alive = false; break; } } - if (serverIsDone) { - unlink(socket_file_path.c_str()); - alive = false; + if (serverIsDone && !finishing) { + // Start termination of the app + ANativeActivity_finish(app->activity); + + // Note that we need to keep on polling events, eventually APP_CMD_DESTROY + // will pop-up after which app->destroyRequested will be true, enabling us + // to properly exit the main loop. + + // Meanwhile, remember that we are finishing to avoid calling + // ANativeActivity_finish() several times. + finishing = true; } } + + // Final clean up waiting_thread.join(); - ANativeActivity_finish(app->activity); + unlink(socket_file_path.c_str()); + GAPID_INFO("End of Graphics API Replay"); + return; } #else // TARGET_OS == GAPID_OS_ANDROID