Skip to content

Commit 334178e

Browse files
Replace ALooper_pollAll with ALooper_pollOnce (#477)
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 <[email protected]> Co-authored-by: google-yfyang <[email protected]>
1 parent dcb5f06 commit 334178e

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/android/main.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ppx/profiler.h"
22

33
#include <android_native_app_glue.h>
4+
#include <android/looper.h>
45
#include <jni.h>
56
#include <string>
67
#include <vector>
@@ -101,15 +102,18 @@ void WaitForNonIdleState(struct android_app* pApp)
101102
pApp->userData = nullptr;
102103

103104
while (gApplicationState == READY) {
104-
int events;
105-
android_poll_source* pSource;
106-
if (ALooper_pollAll(/* timeoutMillis= */ 0,
107-
/* outFd= */ nullptr,
108-
/* outEvents= */ &events,
109-
/* outData= */ (void**)&pSource) >= 0) {
110-
if (pSource) {
111-
pSource->process(pApp, pSource);
112-
}
105+
android_poll_source* pSource = nullptr;
106+
auto result = ALooper_pollOnce(
107+
/* timeoutMillis= */ 0,
108+
/* outFd= */ nullptr,
109+
/* outEvents= */ nullptr,
110+
/* outData= */ (void**)&pSource);
111+
if (result == ALOOPER_POLL_ERROR) {
112+
PPX_ASSERT_MSG(false, "ALooper_pollOnce returned an error.");
113+
return;
114+
}
115+
if (pSource) {
116+
pSource->process(pApp, pSource);
113117
}
114118
}
115119
}

src/ppx/window_android.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ppx/window.h"
1919
#include "ppx/grfx/grfx_swapchain.h"
2020

21+
#include <android/looper.h>
2122
#include <android_native_app_glue.h>
2223

2324
#include "backends/imgui_impl_android.h"
@@ -115,12 +116,18 @@ bool WindowImplAndroid::IsRunning() const
115116

116117
void WindowImplAndroid::ProcessEvent()
117118
{
118-
int events;
119-
android_poll_source* pSource;
120-
if (ALooper_pollAll(0, nullptr, &events, (void**)&pSource) >= 0) {
121-
if (pSource) {
122-
pSource->process(mAndroidApp, pSource);
123-
}
119+
android_poll_source* pSource = nullptr;
120+
auto result = ALooper_pollOnce(
121+
/* timeoutMillis= */ 0,
122+
/* outFd= */ nullptr,
123+
/* outEvents= */ nullptr,
124+
/* outData= */ (void**)&pSource);
125+
if (result == ALOOPER_POLL_ERROR) {
126+
PPX_ASSERT_MSG(false, "ALooper_pollOnce returned an error.");
127+
return;
128+
}
129+
if (pSource) {
130+
pSource->process(mAndroidApp, pSource);
124131
}
125132
}
126133

0 commit comments

Comments
 (0)