From 334178e7fdc94d226154ba96a09a02c63e760058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= Date: Tue, 4 Jun 2024 19:57:35 +0200 Subject: [PATCH] Replace ALooper_pollAll with ALooper_pollOnce (#477) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit is sourced from a downstream patch. The incoming NDK r27 removes `ALooper_pollAll` as it can't be called safely. See https://github.com/android/ndk/discussions/2020 for more context. While this change strives to introduce as little behavior change as possible, Some behavior changes might be happening. 1. We are failing more explicitly when `ALooper_pollOnce` returns `ALOOPER_POLL_ERROR`. When this error is returned, there is no point in retrying polling in the current thread. 2. Compared with `ALooper_pollAll`, `ALooper_pollOnce` can return more frequently with `ALOOPER_POLL_CALLBACK`. This can lead to "on_idle" logic being executed more frequently. Signed-off-by: Nathan Gauër Co-authored-by: google-yfyang --- src/android/main.cpp | 22 +++++++++++++--------- src/ppx/window_android.cpp | 19 +++++++++++++------ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/android/main.cpp b/src/android/main.cpp index 6e03ee479..12d8a9e36 100644 --- a/src/android/main.cpp +++ b/src/android/main.cpp @@ -1,6 +1,7 @@ #include "ppx/profiler.h" #include +#include #include #include #include @@ -101,15 +102,18 @@ void WaitForNonIdleState(struct android_app* pApp) pApp->userData = nullptr; while (gApplicationState == READY) { - int events; - android_poll_source* pSource; - if (ALooper_pollAll(/* timeoutMillis= */ 0, - /* outFd= */ nullptr, - /* outEvents= */ &events, - /* outData= */ (void**)&pSource) >= 0) { - if (pSource) { - pSource->process(pApp, pSource); - } + android_poll_source* pSource = nullptr; + auto result = ALooper_pollOnce( + /* timeoutMillis= */ 0, + /* outFd= */ nullptr, + /* outEvents= */ nullptr, + /* outData= */ (void**)&pSource); + if (result == ALOOPER_POLL_ERROR) { + PPX_ASSERT_MSG(false, "ALooper_pollOnce returned an error."); + return; + } + if (pSource) { + pSource->process(pApp, pSource); } } } diff --git a/src/ppx/window_android.cpp b/src/ppx/window_android.cpp index a7094d2e9..acd5036da 100644 --- a/src/ppx/window_android.cpp +++ b/src/ppx/window_android.cpp @@ -18,6 +18,7 @@ #include "ppx/window.h" #include "ppx/grfx/grfx_swapchain.h" +#include #include #include "backends/imgui_impl_android.h" @@ -115,12 +116,18 @@ bool WindowImplAndroid::IsRunning() const void WindowImplAndroid::ProcessEvent() { - int events; - android_poll_source* pSource; - if (ALooper_pollAll(0, nullptr, &events, (void**)&pSource) >= 0) { - if (pSource) { - pSource->process(mAndroidApp, pSource); - } + android_poll_source* pSource = nullptr; + auto result = ALooper_pollOnce( + /* timeoutMillis= */ 0, + /* outFd= */ nullptr, + /* outEvents= */ nullptr, + /* outData= */ (void**)&pSource); + if (result == ALOOPER_POLL_ERROR) { + PPX_ASSERT_MSG(false, "ALooper_pollOnce returned an error."); + return; + } + if (pSource) { + pSource->process(mAndroidApp, pSource); } }