Skip to content

Commit

Permalink
Replace ALooper_pollAll with ALooper_pollOnce (#477)
Browse files Browse the repository at this point in the history
This commit is sourced from a downstream patch.

The incoming NDK r27 removes `ALooper_pollAll` as it can't be called
safely.

See android/ndk#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 <brioche@google.com>
Co-authored-by: google-yfyang <yfyang@google.com>
  • Loading branch information
Keenuts and google-yfyang authored Jun 4, 2024
1 parent dcb5f06 commit 334178e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
22 changes: 13 additions & 9 deletions src/android/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ppx/profiler.h"

#include <android_native_app_glue.h>
#include <android/looper.h>
#include <jni.h>
#include <string>
#include <vector>
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/ppx/window_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ppx/window.h"
#include "ppx/grfx/grfx_swapchain.h"

#include <android/looper.h>
#include <android_native_app_glue.h>

#include "backends/imgui_impl_android.h"
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 334178e

Please sign in to comment.